Rev 724 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 724 | Rev 726 | ||
---|---|---|---|
1 | #include <inttypes.h> |
1 | #include <inttypes.h> |
2 | #include "ubx.h" |
2 | #include "ubx.h" |
3 | #include "main.h" |
3 | #include "main.h" |
4 | #include <avr/io.h> |
4 | #include <avr/io.h> |
5 | 5 | ||
6 | #include "uart.h" |
6 | #include "uart.h" |
7 | 7 | ||
8 | // ubx protocol parser state machine |
8 | // ubx protocol parser state machine |
9 | #define UBXSTATE_IDLE 0 |
9 | #define UBXSTATE_IDLE 0 |
10 | #define UBXSTATE_SYNC1 1 |
10 | #define UBXSTATE_SYNC1 1 |
11 | #define UBXSTATE_SYNC2 2 |
11 | #define UBXSTATE_SYNC2 2 |
12 | #define UBXSTATE_CLASS 3 |
12 | #define UBXSTATE_CLASS 3 |
13 | #define UBXSTATE_LEN1 4 |
13 | #define UBXSTATE_LEN1 4 |
14 | #define UBXSTATE_LEN2 5 |
14 | #define UBXSTATE_LEN2 5 |
15 | #define UBXSTATE_DATA 6 |
15 | #define UBXSTATE_DATA 6 |
16 | #define UBXSTATE_CKA 7 |
16 | #define UBXSTATE_CKA 7 |
17 | #define UBXSTATE_CKB 8 |
17 | #define UBXSTATE_CKB 8 |
18 | 18 | ||
19 | // ublox protocoll identifier |
19 | // ublox protocoll identifier |
20 | #define UBX_CLASS_NAV 0x01 |
20 | #define UBX_CLASS_NAV 0x01 |
21 | #define UBX_ID_POSLLH 0x02 |
21 | #define UBX_ID_POSLLH 0x02 |
22 | #define UBX_ID_POSUTM 0x08 |
22 | #define UBX_ID_POSUTM 0x08 |
23 | #define UBX_ID_STATUS 0x03 |
23 | #define UBX_ID_STATUS 0x03 |
24 | #define UBX_ID_VELNED 0x12 |
24 | #define UBX_ID_VELNED 0x12 |
25 | #define UBX_SYNC1_CHAR 0xB5 |
25 | #define UBX_SYNC1_CHAR 0xB5 |
26 | #define UBX_SYNC2_CHAR 0x62 |
26 | #define UBX_SYNC2_CHAR 0x62 |
27 | 27 | ||
28 | typedef struct { |
28 | typedef struct { |
29 | uint32_t ITOW; // ms GPS Millisecond Time of Week |
29 | uint32_t ITOW; // ms GPS Millisecond Time of Week |
30 | uint8_t GPSfix; // GPSfix Type, range 0..6 |
30 | uint8_t GPSfix; // GPSfix Type, range 0..6 |
31 | uint8_t Flags; // Navigation Status Flags |
31 | uint8_t Flags; // Navigation Status Flags |
32 | uint8_t DiffS; // Differential Status |
32 | uint8_t DiffS; // Differential Status |
33 | uint8_t res; // reserved |
33 | uint8_t res; // reserved |
34 | uint32_t TTFF; // Time to first fix (millisecond time tag) |
34 | uint32_t TTFF; // Time to first fix (millisecond time tag) |
35 | uint32_t MSSS; // Milliseconds since Startup / Reset |
35 | uint32_t MSSS; // Milliseconds since Startup / Reset |
36 | uint8_t Status; |
36 | uint8_t Status; |
37 | } GPS_STATUS_t; |
37 | } GPS_STATUS_t; |
38 | 38 | ||
39 | typedef struct { |
39 | typedef struct { |
40 | uint32_t ITOW; // ms GPS Millisecond Time of Week |
40 | uint32_t ITOW; // ms GPS Millisecond Time of Week |
41 | int32_t LON; // 1e-07 deg Longitude |
41 | int32_t LON; // 1e-07 deg Longitude |
42 | int32_t LAT; // 1e-07 deg Latitude |
42 | int32_t LAT; // 1e-07 deg Latitude |
43 | int32_t HEIGHT; // mm Height above Ellipsoid |
43 | int32_t HEIGHT; // mm Height above Ellipsoid |
44 | int32_t HMSL; // mm Height above mean sea level |
44 | int32_t HMSL; // mm Height above mean sea level |
45 | uint32_t Hacc; // mm Horizontal Accuracy Estimate |
45 | uint32_t Hacc; // mm Horizontal Accuracy Estimate |
46 | uint32_t Vacc; // mm Vertical Accuracy Estimate |
46 | uint32_t Vacc; // mm Vertical Accuracy Estimate |
47 | uint8_t Status; |
47 | uint8_t Status; |
48 | } GPS_POSLLH_t; |
48 | } GPS_POSLLH_t; |
49 | 49 | ||
50 | typedef struct { |
50 | typedef struct { |
51 | uint32_t ITOW; // ms GPS Millisecond Time of Week |
51 | uint32_t ITOW; // ms GPS Millisecond Time of Week |
52 | int32_t EAST; // cm UTM Easting |
52 | int32_t EAST; // cm UTM Easting |
53 | int32_t NORTH; // cm UTM Nording |
53 | int32_t NORTH; // cm UTM Nording |
54 | int32_t ALT; // cm altitude |
54 | int32_t ALT; // cm altitude |
55 | uint8_t ZONE; // UTM zone number |
55 | uint8_t ZONE; // UTM zone number |
56 | uint8_t HEM; // Hemisphere Indicator (0=North, 1=South) |
56 | uint8_t HEM; // Hemisphere Indicator (0=North, 1=South) |
57 | uint8_t Status; |
57 | uint8_t Status; |
58 | } GPS_POSUTM_t; |
58 | } GPS_POSUTM_t; |
59 | 59 | ||
60 | typedef struct { |
60 | typedef struct { |
61 | uint32_t ITOW; // ms GPS Millisecond Time of Week |
61 | uint32_t ITOW; // ms GPS Millisecond Time of Week |
62 | int32_t VEL_N; // cm/s NED north velocity |
62 | int32_t VEL_N; // cm/s NED north velocity |
63 | int32_t VEL_E; // cm/s NED east velocity |
63 | int32_t VEL_E; // cm/s NED east velocity |
64 | int32_t VEL_D; // cm/s NED down velocity |
64 | int32_t VEL_D; // cm/s NED down velocity |
65 | uint32_t Speed; // cm/s Speed (3-D) |
65 | uint32_t Speed; // cm/s Speed (3-D) |
66 | uint32_t GSpeed; // cm/s Ground Speed (2-D) |
66 | uint32_t GSpeed; // cm/s Ground Speed (2-D) |
67 | int32_t Heading; // 1e-05 deg Heading 2-D |
67 | int32_t Heading; // 1e-05 deg Heading 2-D |
68 | uint32_t SAcc; // cm/s Speed Accuracy Estimate |
68 | uint32_t SAcc; // cm/s Speed Accuracy Estimate |
69 | uint32_t CAcc; // deg Course / Heading Accuracy Estimate |
69 | uint32_t CAcc; // deg Course / Heading Accuracy Estimate |
70 | uint8_t Status; |
70 | uint8_t Status; |
71 | } GPS_VELNED_t; |
71 | } GPS_VELNED_t; |
72 | 72 | ||
73 | 73 | ||
74 | GPS_STATUS_t GpsStatus; |
74 | GPS_STATUS_t GpsStatus = {0,0,0,0,0,0,0, INVALID}; |
75 | GPS_POSLLH_t GpsPosLlh; |
75 | GPS_POSLLH_t GpsPosLlh = {0,0,0,0,0,0,0, INVALID}; |
76 | GPS_POSUTM_t GpsPosUtm; |
76 | GPS_POSUTM_t GpsPosUtm = {0,0,0,0,0,0, INVALID}; |
- | 77 | GPS_VELNED_t GpsVelNed = {0,0,0,0,0,0,0,0,0, INVALID}; |
|
77 | GPS_VELNED_t GpsVelNed; |
78 | GPS_INFO_t GPSInfo = {0,0,0,0,0,0,0,0,0,0, INVALID}; |
78 | 79 | ||
79 | GPS_INFO_t GPSInfo; |
80 | volatile uint8_t GPSTimeout = 0; |
80 | 81 | ||
81 | void UpdateGPSInfo (void) |
82 | void UpdateGPSInfo (void) |
82 | { |
83 | { |
83 | if (GpsStatus.Status == VALID) // valid packet |
84 | if (GpsStatus.Status == VALID) // valid packet |
84 | { |
85 | { |
85 | GPSInfo.satfix = GpsStatus.GPSfix; |
86 | GPSInfo.satfix = GpsStatus.GPSfix; |
86 | GpsStatus.Status = PROCESSED; // never update old data |
87 | GpsStatus.Status = PROCESSED; // never update old data |
87 | } |
88 | } |
88 | if (GpsPosLlh.Status == VALID) // valid packet |
89 | if (GpsPosLlh.Status == VALID) // valid packet |
89 | { |
90 | { |
90 | GPSInfo.longitude = GpsPosLlh.LON; |
91 | GPSInfo.longitude = GpsPosLlh.LON; |
91 | GPSInfo.latitude = GpsPosLlh.LAT; |
92 | GPSInfo.latitude = GpsPosLlh.LAT; |
92 | GPSInfo.altitude = GpsPosLlh.HEIGHT; |
93 | GPSInfo.altitude = GpsPosLlh.HEIGHT; |
93 | GpsPosLlh.Status = PROCESSED; // never update old data |
94 | GpsPosLlh.Status = PROCESSED; // never update old data |
94 | } |
95 | } |
95 | if (GpsPosUtm.Status == VALID) // valid packet |
96 | if (GpsPosUtm.Status == VALID) // valid packet |
96 | { |
97 | { |
97 | GPSInfo.utmeast = GpsPosUtm.EAST; |
98 | GPSInfo.utmeast = GpsPosUtm.EAST; |
98 | GPSInfo.utmnorth = GpsPosUtm.NORTH; |
99 | GPSInfo.utmnorth = GpsPosUtm.NORTH; |
99 | GPSInfo.utmalt = GpsPosUtm.ALT; |
100 | GPSInfo.utmalt = GpsPosUtm.ALT; |
100 | GpsPosUtm.Status = PROCESSED; // never update old data |
101 | GpsPosUtm.Status = PROCESSED; // never update old data |
101 | } |
102 | } |
102 | if (GpsVelNed.Status == VALID) // valid packet |
103 | if (GpsVelNed.Status == VALID) // valid packet |
103 | { |
104 | { |
104 | GPSInfo.veleast = GpsVelNed.VEL_E; |
105 | GPSInfo.veleast = GpsVelNed.VEL_E; |
105 | GPSInfo.velnorth = GpsVelNed.VEL_N; |
106 | GPSInfo.velnorth = GpsVelNed.VEL_N; |
106 | GPSInfo.veltop = -GpsVelNed.VEL_D; |
107 | GPSInfo.veltop = -GpsVelNed.VEL_D; |
107 | GpsVelNed.Status = PROCESSED; // never update old data |
108 | GpsVelNed.Status = PROCESSED; // never update old data |
108 | } |
109 | } |
109 | if (GpsStatus.Status != INVALID) |
110 | if (GpsStatus.Status != INVALID) |
110 | { |
111 | { |
111 | GPSInfo.status = VALID; // set valid if data are updated |
112 | GPSInfo.status = VALID; // set valid if data are updated |
112 | } |
113 | } |
113 | } |
114 | } |
114 | - | ||
115 | void ubx_init(void) |
- | |
116 | { |
- | |
117 | GpsStatus.Status = INVALID; |
- | |
118 | GpsPosLlh.Status = INVALID; |
- | |
119 | GpsPosUtm.Status = INVALID; |
- | |
120 | GpsVelNed.Status = INVALID; |
- | |
121 | GPSInfo.status = INVALID; |
- | |
122 | } |
115 | |
123 | 116 | ||
124 | // this function should be called within the UART RX ISR |
117 | // this function should be called within the UART RX ISR |
125 | void ubx_parser(uint8_t c) |
118 | void ubx_parser(uint8_t c) |
126 | { |
119 | { |
127 | static uint8_t ubxstate = UBXSTATE_IDLE; |
120 | static uint8_t ubxstate = UBXSTATE_IDLE; |
128 | static uint8_t cka, ckb; |
121 | static uint8_t cka, ckb; |
129 | static uint16_t msglen; |
122 | static uint16_t msglen; |
130 | static int8_t *ubxP, *ubxEp, *ubxSp; // pointers to data currently transfered |
123 | static int8_t *ubxP, *ubxEp, *ubxSp; // pointers to data currently transfered |
131 | 124 | ||
132 | switch(ubxstate) |
125 | switch(ubxstate) |
133 | { |
126 | { |
134 | case UBXSTATE_IDLE: // check 1st sync byte |
127 | case UBXSTATE_IDLE: // check 1st sync byte |
135 | if (c == UBX_SYNC1_CHAR) ubxstate = UBXSTATE_SYNC1; |
128 | if (c == UBX_SYNC1_CHAR) ubxstate = UBXSTATE_SYNC1; |
136 | else ubxstate = UBXSTATE_IDLE; // out of synchronization |
129 | else ubxstate = UBXSTATE_IDLE; // out of synchronization |
137 | break; |
130 | break; |
138 | 131 | ||
139 | case UBXSTATE_SYNC1: // check 2nd sync byte |
132 | case UBXSTATE_SYNC1: // check 2nd sync byte |
140 | if (c == UBX_SYNC2_CHAR) ubxstate = UBXSTATE_SYNC2; |
133 | if (c == UBX_SYNC2_CHAR) ubxstate = UBXSTATE_SYNC2; |
141 | else ubxstate = UBXSTATE_IDLE; // out of synchronization |
134 | else ubxstate = UBXSTATE_IDLE; // out of synchronization |
142 | break; |
135 | break; |
143 | 136 | ||
144 | case UBXSTATE_SYNC2: // check msg class to be NAV |
137 | case UBXSTATE_SYNC2: // check msg class to be NAV |
145 | if (c == UBX_CLASS_NAV) ubxstate = UBXSTATE_CLASS; |
138 | if (c == UBX_CLASS_NAV) ubxstate = UBXSTATE_CLASS; |
146 | else ubxstate = UBXSTATE_IDLE; // unsupported message class |
139 | else ubxstate = UBXSTATE_IDLE; // unsupported message class |
147 | break; |
140 | break; |
148 | 141 | ||
149 | case UBXSTATE_CLASS: // check message identifier |
142 | case UBXSTATE_CLASS: // check message identifier |
150 | switch(c) |
143 | switch(c) |
151 | { |
144 | { |
152 | case UBX_ID_POSUTM: // utm position |
145 | case UBX_ID_POSUTM: // utm position |
153 | ubxP = (int8_t *)&GpsPosUtm; // data start pointer |
146 | ubxP = (int8_t *)&GpsPosUtm; // data start pointer |
154 | ubxEp = (int8_t *)(&GpsPosUtm + sizeof(GPS_POSUTM_t)); // data end pointer |
147 | ubxEp = (int8_t *)(&GpsPosUtm + sizeof(GPS_POSUTM_t)); // data end pointer |
155 | ubxSp = (int8_t *)&GpsPosUtm.Status; // status pointer |
148 | ubxSp = (int8_t *)&GpsPosUtm.Status; // status pointer |
156 | break; |
149 | break; |
157 | 150 | ||
158 | case UBX_ID_POSLLH: // geodetic position |
151 | case UBX_ID_POSLLH: // geodetic position |
159 | ubxP = (int8_t *)&GpsPosLlh; // data start pointer |
152 | ubxP = (int8_t *)&GpsPosLlh; // data start pointer |
160 | ubxEp = (int8_t *)(&GpsPosLlh + sizeof(GPS_POSLLH_t)); // data end pointer |
153 | ubxEp = (int8_t *)(&GpsPosLlh + sizeof(GPS_POSLLH_t)); // data end pointer |
161 | ubxSp = (int8_t *)&GpsPosLlh.Status; // status pointer |
154 | ubxSp = (int8_t *)&GpsPosLlh.Status; // status pointer |
162 | break; |
155 | break; |
163 | 156 | ||
164 | case UBX_ID_STATUS: // receiver status |
157 | case UBX_ID_STATUS: // receiver status |
165 | ubxP = (int8_t *)&GpsStatus; // data start pointer |
158 | ubxP = (int8_t *)&GpsStatus; // data start pointer |
166 | ubxEp = (int8_t *)(&GpsStatus + sizeof(GPS_STATUS_t)); // data end pointer |
159 | ubxEp = (int8_t *)(&GpsStatus + sizeof(GPS_STATUS_t)); // data end pointer |
167 | ubxSp = (int8_t *)&GpsStatus.Status; // status pointer |
160 | ubxSp = (int8_t *)&GpsStatus.Status; // status pointer |
168 | break; |
161 | break; |
169 | 162 | ||
170 | case UBX_ID_VELNED: // velocity vector in tangent plane |
163 | case UBX_ID_VELNED: // velocity vector in tangent plane |
171 | ubxP = (int8_t *)&GpsVelNed; // data start pointer |
164 | ubxP = (int8_t *)&GpsVelNed; // data start pointer |
172 | ubxEp = (int8_t *)(&GpsVelNed + sizeof(GPS_VELNED_t)); // data end pointer |
165 | ubxEp = (int8_t *)(&GpsVelNed + sizeof(GPS_VELNED_t)); // data end pointer |
173 | ubxSp = (int8_t *)&GpsVelNed.Status; // status pointer |
166 | ubxSp = (int8_t *)&GpsVelNed.Status; // status pointer |
174 | break; |
167 | break; |
175 | 168 | ||
176 | default: // unsupported identifier |
169 | default: // unsupported identifier |
177 | ubxstate = UBXSTATE_IDLE; |
170 | ubxstate = UBXSTATE_IDLE; |
178 | break; |
171 | break; |
179 | } |
172 | } |
180 | if (ubxstate != UBXSTATE_IDLE) |
173 | if (ubxstate != UBXSTATE_IDLE) |
181 | { |
174 | { |
182 | ubxstate = UBXSTATE_LEN1; |
175 | ubxstate = UBXSTATE_LEN1; |
183 | cka = UBX_CLASS_NAV + c; |
176 | cka = UBX_CLASS_NAV + c; |
184 | ckb = UBX_CLASS_NAV + cka; |
177 | ckb = UBX_CLASS_NAV + cka; |
185 | } |
178 | } |
186 | break; |
179 | break; |
187 | 180 | ||
188 | case UBXSTATE_LEN1: // 1st message length byte |
181 | case UBXSTATE_LEN1: // 1st message length byte |
189 | msglen = c; |
182 | msglen = c; |
190 | cka += c; |
183 | cka += c; |
191 | ckb += cka; |
184 | ckb += cka; |
192 | ubxstate = UBXSTATE_LEN2; |
185 | ubxstate = UBXSTATE_LEN2; |
193 | break; |
186 | break; |
194 | 187 | ||
195 | case UBXSTATE_LEN2: // 2nd message length byte |
188 | case UBXSTATE_LEN2: // 2nd message length byte |
196 | msglen += ((uint16_t)c)<<8; |
189 | msglen += ((uint16_t)c)<<8; |
197 | cka += c; |
190 | cka += c; |
198 | ckb += cka; |
191 | ckb += cka; |
199 | // if the old data are not processed so far then break parsing now |
192 | // if the old data are not processed so far then break parsing now |
200 | // to avoid writing new data in ISR during reading by another function |
193 | // to avoid writing new data in ISR during reading by another function |
201 | if ( *ubxSp == VALID ) ubxstate = UBXSTATE_IDLE; |
194 | if ( *ubxSp == VALID ) ubxstate = UBXSTATE_IDLE; |
202 | else // data invalid or allready processd |
195 | else // data invalid or allready processd |
203 | { |
196 | { |
204 | *ubxSp = INVALID; |
197 | *ubxSp = INVALID; |
205 | ubxstate = UBXSTATE_DATA; |
198 | ubxstate = UBXSTATE_DATA; |
206 | } |
199 | } |
207 | break; |
200 | break; |
208 | 201 | ||
209 | case UBXSTATE_DATA: |
202 | case UBXSTATE_DATA: |
210 | if (ubxP < ubxEp) *ubxP++ = c; // copy curent data byte if any space is left |
203 | if (ubxP < ubxEp) *ubxP++ = c; // copy curent data byte if any space is left |
211 | cka += c; |
204 | cka += c; |
212 | ckb += cka; |
205 | ckb += cka; |
213 | if (--msglen == 0) ubxstate = UBXSTATE_CKA; // switch to next state if all data was read |
206 | if (--msglen == 0) ubxstate = UBXSTATE_CKA; // switch to next state if all data was read |
214 | break; |
207 | break; |
215 | 208 | ||
216 | case UBXSTATE_CKA: |
209 | case UBXSTATE_CKA: |
217 | if (c == cka) ubxstate = UBXSTATE_CKB; |
210 | if (c == cka) ubxstate = UBXSTATE_CKB; |
218 | else |
211 | else |
219 | { |
212 | { |
220 | *ubxSp = INVALID; |
213 | *ubxSp = INVALID; |
221 | ubxstate = UBXSTATE_IDLE; |
214 | ubxstate = UBXSTATE_IDLE; |
222 | } |
215 | } |
223 | break; |
216 | break; |
224 | 217 | ||
225 | case UBXSTATE_CKB: |
218 | case UBXSTATE_CKB: |
226 | if (c == ckb) |
219 | if (c == ckb) |
227 | { |
220 | { |
228 | *ubxSp = VALID; // new data are valid |
221 | *ubxSp = VALID; // new data are valid |
229 | ROT_FLASH; |
222 | ROT_FLASH; |
230 | UpdateGPSInfo(); //update GPS info respectively |
223 | UpdateGPSInfo(); //update GPS info respectively |
- | 224 | GPSTimeout = 255; |
|
231 | } |
225 | } |
232 | else |
226 | else |
233 | { // if checksum not fit then set data invalid |
227 | { // if checksum not fit then set data invalid |
234 | *ubxSp = INVALID; |
228 | *ubxSp = INVALID; |
235 | } |
229 | } |
236 | ubxstate = UBXSTATE_IDLE; // ready to parse new data |
230 | ubxstate = UBXSTATE_IDLE; // ready to parse new data |
237 | break; |
231 | break; |
238 | 232 | ||
239 | default: // unknown ubx state |
233 | default: // unknown ubx state |
240 | ubxstate = UBXSTATE_IDLE; |
234 | ubxstate = UBXSTATE_IDLE; |
241 | break; |
235 | break; |
242 | } |
236 | } |
243 | 237 | ||
244 | } |
238 | } |
245 | 239 | ||
246 | 240 | ||
247 | 241 |