2 * Copyright © 2009 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.
22 #include <sys/types.h>
26 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
27 #define AO_GPS_NUM_SAT_SHIFT (0)
29 #define AO_GPS_VALID (1 << 4)
30 #define AO_GPS_RUNNING (1 << 5)
31 #define AO_GPS_DATE_VALID (1 << 6)
32 #define AO_GPS_COURSE_VALID (1 << 7)
34 struct ao_telemetry_location {
42 int32_t latitude; /* degrees * 10⁷ */
43 int32_t longitude; /* degrees * 10⁷ */
44 int16_t altitude; /* m */
45 uint16_t ground_speed; /* cm/s */
46 uint8_t course; /* degrees / 2 */
47 uint8_t pdop; /* * 5 */
48 uint8_t hdop; /* * 5 */
49 uint8_t vdop; /* * 5 */
50 int16_t climb_rate; /* cm/s */
51 uint16_t h_error; /* m */
52 uint16_t v_error; /* m */
55 #define UBLOX_SAT_STATE_ACQUIRED (1 << 0)
56 #define UBLOX_SAT_STATE_CARRIER_PHASE_VALID (1 << 1)
57 #define UBLOX_SAT_BIT_SYNC_COMPLETE (1 << 2)
58 #define UBLOX_SAT_SUBFRAME_SYNC_COMPLETE (1 << 3)
59 #define UBLOX_SAT_CARRIER_PULLIN_COMPLETE (1 << 4)
60 #define UBLOX_SAT_CODE_LOCKED (1 << 5)
61 #define UBLOX_SAT_ACQUISITION_FAILED (1 << 6)
62 #define UBLOX_SAT_EPHEMERIS_AVAILABLE (1 << 7)
64 struct ao_telemetry_satellite_info {
69 #define AO_TELEMETRY_SATELLITE_MAX_SAT 12
71 struct ao_telemetry_satellite {
73 struct ao_telemetry_satellite_info sats[AO_TELEMETRY_SATELLITE_MAX_SAT];
76 #define ao_gps_orig ao_telemetry_location
77 #define ao_gps_tracking_orig ao_telemetry_satellite
78 #define ao_gps_sat_orig ao_telemetry_satellite_info
80 extern __xdata struct ao_telemetry_location ao_gps_data;
81 extern __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
86 ao_mutex_get(uint8_t *mutex)
91 ao_mutex_put(uint8_t *mutex)
96 static FILE *ao_gps_file;
106 sprintf (line, "\n");
108 sprintf (line, "\\%02x", ((int) c) & 0xff);
110 sprintf (line, "%c", c);
112 write(1, line, strlen(line));
116 #include <sys/time.h>
122 gettimeofday(&tv, NULL);
123 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
126 static uint8_t in_message[4096];
128 static uint16_t recv_len;
130 static void check_ublox_message(char *which, uint8_t *msg);
139 i = getc(ao_gps_file);
146 if (in_len || uc == 0xb5) {
147 in_message[in_len++] = c;
149 recv_len = in_message[4] | (in_message[5] << 8);
150 } else if (in_len > 6 && in_len == recv_len + 8) {
151 check_ublox_message("recv", in_message + 2);
159 #define MESSAGE_LEN 4096
161 static uint8_t message[MESSAGE_LEN];
162 static int message_len;
163 static uint16_t send_len;
166 ao_gps_putchar(char c)
169 uint8_t uc = (uint8_t) c;
171 if (message_len || uc == 0xb5) {
172 if (message_len < MESSAGE_LEN)
173 message[message_len++] = uc;
174 if (message_len == 6) {
175 send_len = message[4] | (message[5] << 8);
176 } else if (message_len > 6 && message_len == send_len + 8) {
177 check_ublox_message("send", message + 2);
183 i = write(ao_gps_fd, &c, 1);
186 if (i < 0 && (errno == EINTR || errno == EAGAIN))
193 #define AO_SERIAL_SPEED_4800 0
194 #define AO_SERIAL_SPEED_9600 1
195 #define AO_SERIAL_SPEED_57600 2
196 #define AO_SERIAL_SPEED_115200 3
199 ao_gps_set_speed(uint8_t speed)
202 struct termios termios;
204 printf ("\t\tset speed %d\n", speed);
206 tcgetattr(fd, &termios);
208 case AO_SERIAL_SPEED_4800:
209 cfsetspeed(&termios, B4800);
211 case AO_SERIAL_SPEED_9600:
212 cfsetspeed(&termios, B9600);
214 case AO_SERIAL_SPEED_57600:
215 cfsetspeed(&termios, B57600);
217 case AO_SERIAL_SPEED_115200:
218 cfsetspeed(&termios, B115200);
221 tcsetattr(fd, TCSAFLUSH, &termios);
222 tcflush(fd, TCIFLUSH);
227 uint8_t ao_task_minimize_latency;
229 #define ao_usb_getchar() 0
231 #include "ao_gps_print.c"
232 #include "ao_gps_show.c"
233 #include "ao_gps_ublox.c"
236 check_ublox_message(char *which, uint8_t *msg)
238 uint8_t class = msg[0];
240 uint16_t len = msg[2] | (msg[3] << 8);
242 struct ao_ublox_cksum cksum_msg = { .a = msg[4 + len],
243 .b = msg[4 + len + 1] };
244 struct ao_ublox_cksum cksum= { 0, 0 };
246 for (i = 0; i < 4 + len; i++) {
247 add_cksum(&cksum, msg[i]);
249 if (cksum.a != cksum_msg.a || cksum.b != cksum_msg.b) {
250 printf ("\t%s: cksum mismatch %02x,%02x != %02x,%02x\n",
261 case UBLOX_NAV_DOP: ;
262 struct ublox_nav_dop *nav_dop = (void *) msg;
263 printf ("\tnav-dop iTOW %9u gDOP %5u dDOP %5u tDOP %5u vDOP %5u hDOP %5u nDOP %5u eDOP %5u\n",
273 case UBLOX_NAV_POSLLH: ;
274 struct ublox_nav_posllh *nav_posllh = (void *) msg;
275 printf ("\tnav-posllh iTOW %9u lon %12.7f lat %12.7f height %10.3f hMSL %10.3f hAcc %10.3f vAcc %10.3f\n",
277 nav_posllh->lon / 1e7,
278 nav_posllh->lat / 1e7,
279 nav_posllh->height / 1e3,
280 nav_posllh->hmsl / 1e3,
281 nav_posllh->hacc / 1e3,
282 nav_posllh->vacc / 1e3);
284 case UBLOX_NAV_SOL: ;
285 struct ublox_nav_sol *nav_sol = (struct ublox_nav_sol *) msg;
286 printf ("\tnav-sol iTOW %9u fTOW %9d week %5d gpsFix %2d flags %02x\n",
287 nav_sol->itow, nav_sol->ftow, nav_sol->week,
288 nav_sol->gpsfix, nav_sol->flags);
290 case UBLOX_NAV_SVINFO: ;
291 struct ublox_nav_svinfo *nav_svinfo = (struct ublox_nav_svinfo *) msg;
292 printf ("\tnav-svinfo iTOW %9u numCH %3d globalFlags %02x\n",
293 nav_svinfo->itow, nav_svinfo->numch, nav_svinfo->globalflags);
295 for (i = 0; i < nav_svinfo->numch; i++) {
296 struct ublox_nav_svinfo_block *nav_svinfo_block = (void *) (msg + 12 + 12 * i);
297 printf ("\t\tchn %3u svid %3u flags %02x quality %3u cno %3u elev %3d azim %6d prRes %9d\n",
298 nav_svinfo_block->chn,
299 nav_svinfo_block->svid,
300 nav_svinfo_block->flags,
301 nav_svinfo_block->quality,
302 nav_svinfo_block->cno,
303 nav_svinfo_block->elev,
304 nav_svinfo_block->azim,
305 nav_svinfo_block->prres);
308 case UBLOX_NAV_VELNED: ;
309 struct ublox_nav_velned *nav_velned = (void *) msg;
310 printf ("\tnav-velned iTOW %9u velN %10.2f velE %10.2f velD %10.2f speed %10.2f gSpeed %10.2f heading %10.5f sAcc %10.2f cAcc %10.5f\n",
312 nav_velned->veln / 1e2,
313 nav_velned->vele / 1e2,
314 nav_velned->veld / 1e2,
315 nav_velned->speed / 1e2,
316 nav_velned->gspeed / 1e2,
317 nav_velned->heading / 1e5,
318 nav_velned->sacc / 1e5,
319 nav_velned->cacc / 1e6);
321 case UBLOX_NAV_TIMEUTC:;
322 struct ublox_nav_timeutc *nav_timeutc = (void *) msg;
323 printf ("\tnav-timeutc iTOW %9u tAcc %5u nano %5d %4u-%2d-%2d %2d:%02d:%02d flags %02x\n",
339 printf ("\t%s: class %02x id %02x len %d:", which, class & 0xff, id & 0xff, len & 0xffff);
340 for (i = 0; i < len; i++)
341 printf (" %02x", msg[4 + i]);
342 printf (" cksum %02x %02x", cksum_msg.a & 0xff, cksum_msg.b & 0xff);
348 ao_dump_state(void *wchan)
350 if (wchan == &ao_gps_data)
356 ao_gps_open(const char *tty)
358 struct termios termios;
361 fd = open (tty, O_RDWR);
365 tcgetattr(fd, &termios);
367 cfsetspeed(&termios, B4800);
368 tcsetattr(fd, TCSAFLUSH, &termios);
371 tcflush(fd, TCIFLUSH);
377 static const struct option options[] = {
378 { .name = "tty", .has_arg = 1, .val = 'T' },
382 static void usage(char *program)
384 fprintf(stderr, "usage: %s [--tty <tty-name>]\n", program);
389 main (int argc, char **argv)
391 char *tty = "/dev/ttyUSB0";
394 while ((c = getopt_long(argc, argv, "T:", options, NULL)) != -1) {
404 ao_gps_fd = ao_gps_open(tty);
409 ao_gps_file = fdopen(ao_gps_fd, "r");