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.
23 #include <sys/types.h>
27 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
28 #define AO_GPS_NUM_SAT_SHIFT (0)
30 #define AO_GPS_NEW_DATA 1
31 #define AO_GPS_NEW_TRACKING 2
33 #define AO_GPS_VALID (1 << 4)
34 #define AO_GPS_RUNNING (1 << 5)
35 #define AO_GPS_DATE_VALID (1 << 6)
36 #define AO_GPS_COURSE_VALID (1 << 7)
38 struct ao_telemetry_location {
46 int32_t latitude; /* degrees * 10⁷ */
47 int32_t longitude; /* degrees * 10⁷ */
48 int16_t altitude_low; /* m */
49 uint16_t ground_speed; /* cm/s */
50 uint8_t course; /* degrees / 2 */
51 uint8_t pdop; /* * 5 */
52 uint8_t hdop; /* * 5 */
53 uint8_t vdop; /* * 5 */
54 int16_t climb_rate; /* cm/s */
55 uint16_t h_error; /* m */
56 uint16_t v_error; /* m */
57 int16_t altitude_high; /* m */
60 typedef int32_t gps_alt_t;
61 #define AO_TELEMETRY_LOCATION_ALTITUDE(l) (((gps_alt_t) (l)->altitude_high << 16) | ((l)->altitude_low))
62 #define AO_GPS_ORIG_ALTITUDE(l) AO_TELEMETRY_LOCATION_ALTITUDE(l)
63 #define AO_TELEMETRY_LOCATION_SET_ALTITUDE(l,a) (((l)->altitude_high = (a) >> 16), \
64 ((l)->altitude_low = (a)))
66 #define UBLOX_SAT_STATE_ACQUIRED (1 << 0)
67 #define UBLOX_SAT_STATE_CARRIER_PHASE_VALID (1 << 1)
68 #define UBLOX_SAT_BIT_SYNC_COMPLETE (1 << 2)
69 #define UBLOX_SAT_SUBFRAME_SYNC_COMPLETE (1 << 3)
70 #define UBLOX_SAT_CARRIER_PULLIN_COMPLETE (1 << 4)
71 #define UBLOX_SAT_CODE_LOCKED (1 << 5)
72 #define UBLOX_SAT_ACQUISITION_FAILED (1 << 6)
73 #define UBLOX_SAT_EPHEMERIS_AVAILABLE (1 << 7)
75 struct ao_telemetry_satellite_info {
80 #define AO_TELEMETRY_SATELLITE_MAX_SAT 12
82 struct ao_telemetry_satellite {
84 struct ao_telemetry_satellite_info sats[AO_TELEMETRY_SATELLITE_MAX_SAT];
87 #define ao_gps_orig ao_telemetry_location
88 #define ao_gps_tracking_orig ao_telemetry_satellite
89 #define ao_gps_sat_orig ao_telemetry_satellite_info
91 extern __xdata struct ao_telemetry_location ao_gps_data;
92 extern __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
97 ao_mutex_get(uint8_t *mutex)
102 ao_mutex_put(uint8_t *mutex)
106 static int ao_gps_fd;
107 static FILE *ao_gps_file;
117 sprintf (line, "\n");
119 sprintf (line, "\\%02x", ((int) c) & 0xff);
121 sprintf (line, "%c", c);
123 write(1, line, strlen(line));
127 #include <sys/time.h>
133 gettimeofday(&tv, NULL);
134 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
137 static uint8_t in_message[4096];
139 static uint16_t recv_len;
141 static void check_ublox_message(char *which, uint8_t *msg);
150 i = getc(ao_gps_file);
157 if (in_len || uc == 0xb5) {
158 in_message[in_len++] = c;
160 recv_len = in_message[4] | (in_message[5] << 8);
161 } else if (in_len > 6 && in_len == recv_len + 8) {
162 check_ublox_message("recv", in_message + 2);
170 #define MESSAGE_LEN 4096
172 static uint8_t message[MESSAGE_LEN];
173 static int message_len;
174 static uint16_t send_len;
177 ao_gps_putchar(char c)
180 uint8_t uc = (uint8_t) c;
182 if (message_len || uc == 0xb5) {
183 if (message_len < MESSAGE_LEN)
184 message[message_len++] = uc;
185 if (message_len == 6) {
186 send_len = message[4] | (message[5] << 8);
187 } else if (message_len > 6 && message_len == send_len + 8) {
188 check_ublox_message("send", message + 2);
194 i = write(ao_gps_fd, &c, 1);
197 if (i < 0 && (errno == EINTR || errno == EAGAIN))
204 #define AO_SERIAL_SPEED_4800 0
205 #define AO_SERIAL_SPEED_9600 1
206 #define AO_SERIAL_SPEED_57600 2
207 #define AO_SERIAL_SPEED_115200 3
210 ao_gps_set_speed(uint8_t speed)
213 struct termios termios;
215 printf ("\t\tset speed %d\n", speed);
217 tcgetattr(fd, &termios);
219 case AO_SERIAL_SPEED_4800:
220 cfsetspeed(&termios, B4800);
222 case AO_SERIAL_SPEED_9600:
223 cfsetspeed(&termios, B9600);
225 case AO_SERIAL_SPEED_57600:
226 cfsetspeed(&termios, B57600);
228 case AO_SERIAL_SPEED_115200:
229 cfsetspeed(&termios, B115200);
232 tcsetattr(fd, TCSAFLUSH, &termios);
233 tcflush(fd, TCIFLUSH);
238 uint8_t ao_task_minimize_latency;
240 #define ao_usb_getchar() 0
242 #include "ao_gps_print.c"
243 #include "ao_gps_show.c"
244 #include "ao_gps_ublox.c"
247 check_ublox_message(char *which, uint8_t *msg)
249 uint8_t class = msg[0];
251 uint16_t len = msg[2] | (msg[3] << 8);
253 struct ao_ublox_cksum cksum_msg = { .a = msg[4 + len],
254 .b = msg[4 + len + 1] };
255 struct ao_ublox_cksum cksum= { 0, 0 };
257 for (i = 0; i < 4 + len; i++) {
258 add_cksum(&cksum, msg[i]);
260 if (cksum.a != cksum_msg.a || cksum.b != cksum_msg.b) {
261 printf ("\t%s: cksum mismatch %02x,%02x != %02x,%02x\n",
272 case UBLOX_NAV_DOP: ;
273 struct ublox_nav_dop *nav_dop = (void *) msg;
274 printf ("\tnav-dop iTOW %9u gDOP %5u dDOP %5u tDOP %5u vDOP %5u hDOP %5u nDOP %5u eDOP %5u\n",
284 case UBLOX_NAV_POSLLH: ;
285 struct ublox_nav_posllh *nav_posllh = (void *) msg;
286 printf ("\tnav-posllh iTOW %9u lon %12.7f lat %12.7f height %10.3f hMSL %10.3f hAcc %10.3f vAcc %10.3f\n",
288 nav_posllh->lon / 1e7,
289 nav_posllh->lat / 1e7,
290 nav_posllh->height / 1e3,
291 nav_posllh->hmsl / 1e3,
292 nav_posllh->hacc / 1e3,
293 nav_posllh->vacc / 1e3);
295 case UBLOX_NAV_SOL: ;
296 struct ublox_nav_sol *nav_sol = (struct ublox_nav_sol *) msg;
297 printf ("\tnav-sol iTOW %9u fTOW %9d week %5d gpsFix %2d flags %02x\n",
298 nav_sol->itow, nav_sol->ftow, nav_sol->week,
299 nav_sol->gpsfix, nav_sol->flags);
301 case UBLOX_NAV_SVINFO: ;
302 struct ublox_nav_svinfo *nav_svinfo = (struct ublox_nav_svinfo *) msg;
303 printf ("\tnav-svinfo iTOW %9u numCH %3d globalFlags %02x\n",
304 nav_svinfo->itow, nav_svinfo->numch, nav_svinfo->globalflags);
306 for (i = 0; i < nav_svinfo->numch; i++) {
307 struct ublox_nav_svinfo_block *nav_svinfo_block = (void *) (msg + 12 + 12 * i);
308 printf ("\t\tchn %3u svid %3u flags %02x quality %3u cno %3u elev %3d azim %6d prRes %9d\n",
309 nav_svinfo_block->chn,
310 nav_svinfo_block->svid,
311 nav_svinfo_block->flags,
312 nav_svinfo_block->quality,
313 nav_svinfo_block->cno,
314 nav_svinfo_block->elev,
315 nav_svinfo_block->azim,
316 nav_svinfo_block->prres);
319 case UBLOX_NAV_VELNED: ;
320 struct ublox_nav_velned *nav_velned = (void *) msg;
321 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",
323 nav_velned->veln / 1e2,
324 nav_velned->vele / 1e2,
325 nav_velned->veld / 1e2,
326 nav_velned->speed / 1e2,
327 nav_velned->gspeed / 1e2,
328 nav_velned->heading / 1e5,
329 nav_velned->sacc / 1e5,
330 nav_velned->cacc / 1e6);
332 case UBLOX_NAV_TIMEUTC:;
333 struct ublox_nav_timeutc *nav_timeutc = (void *) msg;
334 printf ("\tnav-timeutc iTOW %9u tAcc %5u nano %5d %4u-%2d-%2d %2d:%02d:%02d flags %02x\n",
350 printf ("\t%s: class %02x id %02x len %d:", which, class & 0xff, id & 0xff, len & 0xffff);
351 for (i = 0; i < len; i++)
352 printf (" %02x", msg[4 + i]);
353 printf (" cksum %02x %02x", cksum_msg.a & 0xff, cksum_msg.b & 0xff);
359 ao_dump_state(void *wchan)
361 if (wchan == &ao_gps_new)
367 ao_gps_open(const char *tty)
369 struct termios termios;
372 fd = open (tty, O_RDWR);
376 tcgetattr(fd, &termios);
378 cfsetspeed(&termios, B4800);
379 tcsetattr(fd, TCSAFLUSH, &termios);
382 tcflush(fd, TCIFLUSH);
388 static const struct option options[] = {
389 { .name = "tty", .has_arg = 1, .val = 'T' },
393 static void usage(char *program)
395 fprintf(stderr, "usage: %s [--tty <tty-name>]\n", program);
400 main (int argc, char **argv)
402 char *tty = "/dev/ttyUSB0";
405 while ((c = getopt_long(argc, argv, "T:", options, NULL)) != -1) {
415 ao_gps_fd = ao_gps_open(tty);
420 ao_gps_file = fdopen(ao_gps_fd, "r");