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.
19 #define _DEFAULT_SOURCE 1
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 void *d, int len)
103 const char *data = d;
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 usleep(r * US_PER_CHAR);
122 skytraq_dbg_newline();
127 skytraq_setcomm(int fd, int baudrate)
133 int target_baudrate = 0;
158 msg[0] = 0xa0; /* header */
160 msg[2] = 0x00; /* length */
162 msg[4] = 0x05; /* configure serial port */
163 msg[5] = 0x00; /* COM 1 */
164 msg[6] = target_baudrate;
165 msg[7] = 0x00; /* update to SRAM only */
168 for (i = 4; i < 8; i++)
173 return skytraq_write(fd, msg, 11);
177 skytraq_waitchar(int fd, int timeout)
179 struct pollfd fds[1];
185 fds[0].events = POLLIN;
186 ret = poll(fds, 1, timeout);
188 if (fds[0].revents & POLLIN) {
189 ret = read(fd, &c, 1);
191 skytraq_dbg_char(1, c);
205 skytraq_waitstatus(int fd, const char *status, int timeout)
211 c = skytraq_waitchar(fd, timeout);
213 skytraq_dbg_newline();
216 if ((char) c == *status) {
219 c = skytraq_waitchar(fd, timeout);
221 skytraq_dbg_newline();
227 skytraq_dbg_newline();
237 skytraq_flush(int fd)
239 while (skytraq_waitchar(fd, 1) >= 0)
244 skytraq_cmd_wait(int fd, const char *message, int len, const char *status, int timeout)
247 skytraq_write(fd, message, len);
248 return skytraq_waitstatus(fd, status, timeout);
252 skytraq_cmd_nowait(int fd, const char *message, int len)
255 return skytraq_write(fd, message, len);