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_TELEMETRY_LOCATION_SET_ALTITUDE(l,a) (((l)->altitude_high = (a) >> 16), \
63 ((l)->altitude_low = (a)))
65 #define UBLOX_SAT_STATE_ACQUIRED (1 << 0)
66 #define UBLOX_SAT_STATE_CARRIER_PHASE_VALID (1 << 1)
67 #define UBLOX_SAT_BIT_SYNC_COMPLETE (1 << 2)
68 #define UBLOX_SAT_SUBFRAME_SYNC_COMPLETE (1 << 3)
69 #define UBLOX_SAT_CARRIER_PULLIN_COMPLETE (1 << 4)
70 #define UBLOX_SAT_CODE_LOCKED (1 << 5)
71 #define UBLOX_SAT_ACQUISITION_FAILED (1 << 6)
72 #define UBLOX_SAT_EPHEMERIS_AVAILABLE (1 << 7)
74 struct ao_telemetry_satellite_info {
79 #define AO_TELEMETRY_SATELLITE_MAX_SAT 12
81 struct ao_telemetry_satellite {
83 struct ao_telemetry_satellite_info sats[AO_TELEMETRY_SATELLITE_MAX_SAT];
86 #define ao_gps_orig ao_telemetry_location
87 #define ao_gps_tracking_orig ao_telemetry_satellite
88 #define ao_gps_sat_orig ao_telemetry_satellite_info
90 extern __xdata struct ao_telemetry_location ao_gps_data;
91 extern __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
96 ao_mutex_get(uint8_t *mutex)
101 ao_mutex_put(uint8_t *mutex)
105 static int ao_gps_fd;
106 static FILE *ao_gps_file;
116 sprintf (line, "\n");
118 sprintf (line, "\\%02x", ((int) c) & 0xff);
120 sprintf (line, "%c", c);
122 write(1, line, strlen(line));
126 #include <sys/time.h>
132 gettimeofday(&tv, NULL);
133 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
136 static uint8_t in_message[4096];
138 static uint16_t recv_len;
140 static void check_ublox_message(char *which, uint8_t *msg);
149 i = getc(ao_gps_file);
156 if (in_len || uc == 0xb5) {
157 in_message[in_len++] = c;
159 recv_len = in_message[4] | (in_message[5] << 8);
160 } else if (in_len > 6 && in_len == recv_len + 8) {
161 check_ublox_message("recv", in_message + 2);
169 #define MESSAGE_LEN 4096
171 static uint8_t message[MESSAGE_LEN];
172 static int message_len;
173 static uint16_t send_len;
176 ao_gps_putchar(char c)
179 uint8_t uc = (uint8_t) c;
181 if (message_len || uc == 0xb5) {
182 if (message_len < MESSAGE_LEN)
183 message[message_len++] = uc;
184 if (message_len == 6) {
185 send_len = message[4] | (message[5] << 8);
186 } else if (message_len > 6 && message_len == send_len + 8) {
187 check_ublox_message("send", message + 2);
193 i = write(ao_gps_fd, &c, 1);
196 if (i < 0 && (errno == EINTR || errno == EAGAIN))
203 #define AO_SERIAL_SPEED_4800 0
204 #define AO_SERIAL_SPEED_9600 1
205 #define AO_SERIAL_SPEED_57600 2
206 #define AO_SERIAL_SPEED_115200 3
209 ao_gps_set_speed(uint8_t speed)
212 struct termios termios;
214 printf ("\t\tset speed %d\n", speed);
216 tcgetattr(fd, &termios);
218 case AO_SERIAL_SPEED_4800:
219 cfsetspeed(&termios, B4800);
221 case AO_SERIAL_SPEED_9600:
222 cfsetspeed(&termios, B9600);
224 case AO_SERIAL_SPEED_57600:
225 cfsetspeed(&termios, B57600);
227 case AO_SERIAL_SPEED_115200:
228 cfsetspeed(&termios, B115200);
231 tcsetattr(fd, TCSAFLUSH, &termios);
232 tcflush(fd, TCIFLUSH);
237 uint8_t ao_task_minimize_latency;
239 #define ao_usb_getchar() 0
241 #include "ao_gps_print.c"
242 #include "ao_gps_show.c"
243 #include "ao_gps_ublox.c"
246 check_ublox_message(char *which, uint8_t *msg)
248 uint8_t class = msg[0];
250 uint16_t len = msg[2] | (msg[3] << 8);
252 struct ao_ublox_cksum cksum_msg = { .a = msg[4 + len],
253 .b = msg[4 + len + 1] };
254 struct ao_ublox_cksum cksum= { 0, 0 };
256 for (i = 0; i < 4 + len; i++) {
257 add_cksum(&cksum, msg[i]);
259 if (cksum.a != cksum_msg.a || cksum.b != cksum_msg.b) {
260 printf ("\t%s: cksum mismatch %02x,%02x != %02x,%02x\n",
271 case UBLOX_NAV_DOP: ;
272 struct ublox_nav_dop *nav_dop = (void *) msg;
273 printf ("\tnav-dop iTOW %9u gDOP %5u dDOP %5u tDOP %5u vDOP %5u hDOP %5u nDOP %5u eDOP %5u\n",
283 case UBLOX_NAV_POSLLH: ;
284 struct ublox_nav_posllh *nav_posllh = (void *) msg;
285 printf ("\tnav-posllh iTOW %9u lon %12.7f lat %12.7f height %10.3f hMSL %10.3f hAcc %10.3f vAcc %10.3f\n",
287 nav_posllh->lon / 1e7,
288 nav_posllh->lat / 1e7,
289 nav_posllh->height / 1e3,
290 nav_posllh->hmsl / 1e3,
291 nav_posllh->hacc / 1e3,
292 nav_posllh->vacc / 1e3);
294 case UBLOX_NAV_SOL: ;
295 struct ublox_nav_sol *nav_sol = (struct ublox_nav_sol *) msg;
296 printf ("\tnav-sol iTOW %9u fTOW %9d week %5d gpsFix %2d flags %02x\n",
297 nav_sol->itow, nav_sol->ftow, nav_sol->week,
298 nav_sol->gpsfix, nav_sol->flags);
300 case UBLOX_NAV_SVINFO: ;
301 struct ublox_nav_svinfo *nav_svinfo = (struct ublox_nav_svinfo *) msg;
302 printf ("\tnav-svinfo iTOW %9u numCH %3d globalFlags %02x\n",
303 nav_svinfo->itow, nav_svinfo->numch, nav_svinfo->globalflags);
305 for (i = 0; i < nav_svinfo->numch; i++) {
306 struct ublox_nav_svinfo_block *nav_svinfo_block = (void *) (msg + 12 + 12 * i);
307 printf ("\t\tchn %3u svid %3u flags %02x quality %3u cno %3u elev %3d azim %6d prRes %9d\n",
308 nav_svinfo_block->chn,
309 nav_svinfo_block->svid,
310 nav_svinfo_block->flags,
311 nav_svinfo_block->quality,
312 nav_svinfo_block->cno,
313 nav_svinfo_block->elev,
314 nav_svinfo_block->azim,
315 nav_svinfo_block->prres);
318 case UBLOX_NAV_VELNED: ;
319 struct ublox_nav_velned *nav_velned = (void *) msg;
320 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",
322 nav_velned->veln / 1e2,
323 nav_velned->vele / 1e2,
324 nav_velned->veld / 1e2,
325 nav_velned->speed / 1e2,
326 nav_velned->gspeed / 1e2,
327 nav_velned->heading / 1e5,
328 nav_velned->sacc / 1e5,
329 nav_velned->cacc / 1e6);
331 case UBLOX_NAV_TIMEUTC:;
332 struct ublox_nav_timeutc *nav_timeutc = (void *) msg;
333 printf ("\tnav-timeutc iTOW %9u tAcc %5u nano %5d %4u-%2d-%2d %2d:%02d:%02d flags %02x\n",
349 printf ("\t%s: class %02x id %02x len %d:", which, class & 0xff, id & 0xff, len & 0xffff);
350 for (i = 0; i < len; i++)
351 printf (" %02x", msg[4 + i]);
352 printf (" cksum %02x %02x", cksum_msg.a & 0xff, cksum_msg.b & 0xff);
358 ao_dump_state(void *wchan)
360 if (wchan == &ao_gps_new)
366 ao_gps_open(const char *tty)
368 struct termios termios;
371 fd = open (tty, O_RDWR);
375 tcgetattr(fd, &termios);
377 cfsetspeed(&termios, B4800);
378 tcsetattr(fd, TCSAFLUSH, &termios);
381 tcflush(fd, TCIFLUSH);
387 static const struct option options[] = {
388 { .name = "tty", .has_arg = 1, .val = 'T' },
392 static void usage(char *program)
394 fprintf(stderr, "usage: %s [--tty <tty-name>]\n", program);
399 main (int argc, char **argv)
401 char *tty = "/dev/ttyUSB0";
404 while ((c = getopt_long(argc, argv, "T:", options, NULL)) != -1) {
414 ao_gps_fd = ao_gps_open(tty);
419 ao_gps_file = fdopen(ao_gps_fd, "r");