2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 #include <sys/types.h>
26 #include "sky_flash.h"
33 int skytraq_verbose = 1;
36 skytraq_setspeed(int fd, int baud)
53 fprintf (stderr, "Invalid baudrate %d\n", baud);
56 ret = tcgetattr(fd, &term);
68 ret = tcsetattr(fd, TCSAFLUSH, &term);
72 int skytraq_open_time;
75 skytraq_open(const char *path)
80 fd = open(path, O_RDWR | O_NOCTTY);
86 ret = skytraq_setspeed(fd, 9600);
91 skytraq_open_time = skytraq_millis();
97 #define BPS (BAUD/10 * 9/10)
98 #define US_PER_CHAR (1000000 / BPS)
101 skytraq_write(int fd, const char *data, int len)
103 const char *d = data;
107 skytraq_dbg_printf (0, "%4d: ", len);
109 skytraq_dbg_buf(0, data, len);
114 skytraq_dbg_printf(0, ".");
116 r = write(fd, data, this_time);
119 us = r * US_PER_CHAR;
120 usleep(r * US_PER_CHAR);
124 skytraq_dbg_newline();
129 skytraq_setcomm(int fd, int baudrate)
160 msg[0] = 0xa0; /* header */
162 msg[2] = 0x00; /* length */
164 msg[4] = 0x05; /* configure serial port */
165 msg[5] = 0x00; /* COM 1 */
166 msg[6] = target_baudrate;
167 msg[7] = 0x00; /* update to SRAM only */
170 for (i = 4; i < 8; i++)
175 return skytraq_write(fd, msg, 11);
179 skytraq_waitchar(int fd, int timeout)
181 struct pollfd fds[1];
187 fds[0].events = POLLIN;
188 ret = poll(fds, 1, timeout);
190 if (fds[0].revents & POLLIN) {
191 ret = read(fd, &c, 1);
193 skytraq_dbg_char(1, c);
207 skytraq_waitstatus(int fd, const char *status, int timeout)
213 c = skytraq_waitchar(fd, timeout);
215 skytraq_dbg_newline();
218 if ((char) c == *status) {
221 c = skytraq_waitchar(fd, timeout);
223 skytraq_dbg_newline();
229 skytraq_dbg_newline();
239 skytraq_flush(int fd)
241 while (skytraq_waitchar(fd, 1) >= 0)
246 skytraq_cmd_wait(int fd, const char *message, int len, const char *status, int timeout)
249 skytraq_write(fd, message, len);
250 return skytraq_waitstatus(fd, status, timeout);
254 skytraq_cmd_nowait(int fd, const char *message, int len)
257 return skytraq_write(fd, message, len);