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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 #include <sys/types.h>
25 #include "sky_flash.h"
32 int skytraq_verbose = 1;
35 skytraq_setspeed(int fd, int baud)
52 fprintf (stderr, "Invalid baudrate %d\n", baud);
55 ret = tcgetattr(fd, &term);
67 ret = tcsetattr(fd, TCSAFLUSH, &term);
71 int skytraq_open_time;
74 skytraq_open(const char *path)
79 fd = open(path, O_RDWR | O_NOCTTY);
85 ret = skytraq_setspeed(fd, 9600);
90 skytraq_open_time = skytraq_millis();
96 #define BPS (BAUD/10 * 9/10)
97 #define US_PER_CHAR (1000000 / BPS)
100 skytraq_write(int fd, const char *data, int len)
102 const char *d = data;
106 skytraq_dbg_printf (0, "%4d: ", len);
108 skytraq_dbg_buf(0, data, len);
113 skytraq_dbg_printf(0, ".");
115 r = write(fd, data, this_time);
118 us = r * US_PER_CHAR;
119 usleep(r * US_PER_CHAR);
123 skytraq_dbg_newline();
128 skytraq_setcomm(int fd, int baudrate)
159 msg[0] = 0xa0; /* header */
161 msg[2] = 0x00; /* length */
163 msg[4] = 0x05; /* configure serial port */
164 msg[5] = 0x00; /* COM 1 */
165 msg[6] = target_baudrate;
166 msg[7] = 0x00; /* update to SRAM only */
169 for (i = 4; i < 8; i++)
174 return skytraq_write(fd, msg, 11);
178 skytraq_waitchar(int fd, int timeout)
180 struct pollfd fds[1];
186 fds[0].events = POLLIN;
187 ret = poll(fds, 1, timeout);
189 if (fds[0].revents & POLLIN) {
190 ret = read(fd, &c, 1);
192 skytraq_dbg_char(1, c);
206 skytraq_waitstatus(int fd, const char *status, int timeout)
212 c = skytraq_waitchar(fd, timeout);
214 skytraq_dbg_newline();
217 if ((char) c == *status) {
220 c = skytraq_waitchar(fd, timeout);
222 skytraq_dbg_newline();
228 skytraq_dbg_newline();
238 skytraq_flush(int fd)
240 while (skytraq_waitchar(fd, 1) >= 0)
245 skytraq_cmd_wait(int fd, const char *message, int len, const char *status, int timeout)
248 skytraq_write(fd, message, len);
249 return skytraq_waitstatus(fd, status, timeout);
253 skytraq_cmd_nowait(int fd, const char *message, int len)
256 return skytraq_write(fd, message, len);