1 // -*- coding: utf-8 -*-
4 // This file is part of ruwai.
6 // If you use ruwai in any program or publication, please inform and
7 // acknowledge its author Stefan Mertl (stefan@mertl-research.at).
9 // pSysmon is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <ubx_protocol.h>
28 #include <rw_serial.h>
30 // Used to pack the structures to remove eventual byte overhead.
32 #define PACKED __attribute__((packed))
35 // The mapping of the GPS pins to the Arduino pins.
36 #define GPS_ENABLE_USB 34
37 #define GPS_TP_INTERRUPT 1
39 //! The supported GPSfix types.
40 typedef enum gpsfix_e
{
42 FIX_DEAD_RECKONING_ONLY
= 1,
45 FIX_GPS_AND_DEAD_RECKONING
= 4,
49 typedef enum read_state_e
{
50 WAITING_FOR_SYNC1
= 0,
62 typedef enum target_e
{
73 //! The UBX message receive buffer.
74 typedef union PACKED ubx_msg_buffer_u
{
75 ubx_cfg_tp5_t cfg_tp5
;
76 ubx_cfg_prt_uart_t cfg_prt_uart
;
77 ubx_ack_ack_t ack_ack
;
78 ubx_nav_status_t nav_status
;
79 ubx_nav_timeutc_t nav_timeutc
;
80 ubx_nav_posllh_t nav_posllh
;
85 //! The uBlox GPS class.
87 * Detailed description.
91 /*! The step of the message read flow. */
92 read_state_t read_step
;
94 /* The message class of the read flow. */
95 uint8_t read_msg_class
;
97 /* The message id of the read flow. */
100 /* The payload length of the message in the read flow. */
101 uint16_t read_payload_length
;
103 /* The checksum of the read flow. */
107 /* The counter of the payload in the read flow. */
108 uint16_t read_payload_counter
;
110 /* The state of received ACK messages. */
111 volatile bool msg_ack_changed
;
113 /* The buffer of the received message in the read flow. */
114 ubx_msg_buffer_t read_buffer
;
116 /*! The type of the GPSfix. */
119 /*! The timepulse configuration. */
120 ubx_cfg_tp5_t timepulse_cfg
;
122 /*! The timepulse1 configuration. */
123 ubx_cfg_tp5_t timepulse1_cfg
;
125 /*! The timepulse2 configuration. */
126 ubx_cfg_tp5_t timepulse2_cfg
;
128 /*! The navigation status configuration. */
129 ubx_nav_status_t navigation_status
;
131 /*! The cfg message acknowledge information. */
132 ubx_ack_ack_t msg_ack
;
136 /*! The validity of the GPSfix. */
139 /*! The Arduino Serial Stream object used for communication. */
140 RuwaiSerial
&serial_port
;
142 /*! Send a ublox message. */
143 void send_ublox_message(uint8_t msg_class
, uint8_t msg_id
, void *msg
, uint8_t length
);
145 /*! Parse a ublox UBX message. */
148 /*! Handle an unexpected UBX message. */
149 void handle_unexpected_msg();
151 /*! Send the timepulse2 configuration to the GPS module. */
152 bool send_cfg_tp5(uint8_t tp_idx
);
154 /*! Wait for the acknowledgement message for a sent config message. */
155 bool wait_for_cfg_ack(uint8_t msg_class
, uint8_t msg_id
);
157 /*! Test the serial connection with a given baud rate. */
158 bool test_baud_rate(uint32_t baud_rate
);
160 /*! Poll the baud rate and wait for an answer for a given baud rate. */
161 bool poll_baud_rate_blocking(uint32_t baud_rate
);
164 /*! The constructor. */
165 GpsLea6T(RuwaiSerial
&serial
);
168 /* The state of the polled GPS status fields. */
169 /*! The flag to track changes of the NAV settings. */
170 volatile bool nav_status_changed
;
171 volatile bool nav_timeutc_changed
;
172 volatile bool nav_position_changed
;
174 /*! The flag to track changes of the CFG settings. */
175 volatile bool cfg_timepulse1_changed
;
176 volatile bool cfg_timepulse2_changed
;
177 volatile bool cfg_prt_uart_changed
;
179 /*! The flags used to track the timestamp state. */
180 volatile bool timestamp_init
;
181 volatile bool timestamp_empty
;
182 volatile bool timestamp_buf_empty
;
183 volatile bool timestamp_race_error
;
185 /*! The UTC time information. */
186 ubx_nav_timeutc_t timeutc
;
188 /*! The navigation geodetic position solution. */
189 ubx_nav_posllh_t nav_position
;
191 /*! The time of the next timepulse. */
192 ubx_tim_tp_t next_timepulse
;
193 ubx_tim_tp_t next_timepulse_buf
;
195 ubx_cfg_prt_uart_t cfg_prt_uart
;
197 /*! Initialize the state. */
198 void init_state(void);
200 /*! Start the serial port. */
201 bool start_serial(uint32_t baud_rate
);
203 void stop_serial(void);
205 /*! Set the timepulse1 PPS. */
206 bool set_timepulse1_pps();
208 /*! Set the timepulse2 frequency. */
209 bool set_timepulse2_frequency(uint32_t freq
, uint32_t freq_lock
);
211 /*! Disable the timepulse2 frequency. */
212 bool disable_timepulse2_frequency(void);
214 /* Get the timepulse2 frequency. */
215 uint32_t get_timepulse2_frequency();
217 /*! Get the timepulse2 locked frequency. */
218 uint32_t get_timepulse2_frequency_locked();
220 /*! The the serial port baud rate. */
221 void set_uart1_baudrate(uint32_t baudrate
);
223 /*! The the usb port baud rate. */
224 void set_usb_baudrate(uint32_t baudrate
);
226 /*! Get the GPS fix type. */
227 gpsfix_t
get_gps_fix();
229 /*! Get the GPS fix_ok flag. */
230 bool get_gps_fix_ok();
233 /*! Poll the configuration settings of the UART port. */
234 void poll_cfg_prt_uart();
236 /*! Poll a message configuration. */
237 void poll_cfg_msg(uint8_t msg_class
, uint8_t msg_id
);
239 /*! Send the message rate configuration to the current target. */
240 bool send_cfg_msg(uint8_t msg_class
, uint8_t msg_id
, uint8_t rate
);
241 bool send_cfg_msg(uint8_t msg_class
, uint8_t msg_id
, uint8_t rate
, uint8_t* targets
, uint8_t n_targets
);
243 /*! Poll the timepulse configuration. */
244 void poll_cfg_tp5(uint8_t timepulse_id
);
246 /*! Poll the navigation status information. */
247 void poll_nav_status();
249 /*! Poll the geodetic position solution. */
250 void poll_nav_posllh();
252 /*! Test for a working baud rate. */
253 uint32_t detect_baud_rate(uint32_t baud_1
, uint32_t baud_2
);
256 /*! Disable the default messages on the current target port. */
257 uint8_t disable_default_messages();
259 /*! Enable a message on the current target port. */
260 bool enable_message(uint8_t msg_class
, uint8_t msg_id
, uint8_t rate
);
261 bool enable_message(uint8_t msg_class
, uint8_t msg_id
, uint8_t rate
, uint8_t target
);
262 bool enable_message(uint8_t msg_class
, uint8_t msg_id
, uint8_t rate
, uint8_t* targets
, uint8_t n_targets
);
264 /*! Read data from the serial port. */