Rev 231 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 231 | Rev 274 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | #include <inttypes.h> |
1 | #include <inttypes.h> |
2 | - | ||
3 | #include "ubx.h" |
2 | #include "ubx.h" |
4 | #include <avr/io.h> |
3 | #include "timer0.h" |
Line 5... | Line 4... | ||
5 | 4 | ||
6 | - | ||
7 | // ubx protocol parser state machine |
- | |
8 | #define UBXSTATE_IDLE 0 |
- | |
9 | #define UBXSTATE_SYNC1 1 |
- | |
10 | #define UBXSTATE_SYNC2 2 |
- | |
11 | #define UBXSTATE_CLASS 3 |
- | |
12 | #define UBXSTATE_LEN1 4 |
- | |
13 | #define UBXSTATE_LEN2 5 |
- | |
14 | #define UBXSTATE_DATA 6 |
5 | |
Line -... | Line 6... | ||
- | 6 | // ------------------------------------------------------------------------------------------------ |
|
- | 7 | // defines |
|
- | 8 | ||
- | 9 | #define DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980 722819 // the year 0 does not exist! |
|
- | 10 | #define DAYS_PER_YEAR 365 |
|
- | 11 | #define DAYS_PER_LEAPYEAR 366 |
|
- | 12 | #define DAYS_PER_4YEARS 1461 //((3 * DAYS_PER_YEAR) + DAYS_PER_LEAPYEAR) // years dividable by 4 are leap years |
|
15 | #define UBXSTATE_CKA 7 |
13 | #define DAYS_PER_100YEARS 36524 //((25 * DAYS_PER_4YEARS) - 1) // years dividable by 100 are no leap years |
16 | #define UBXSTATE_CKB 8 |
14 | #define DAYS_PER_400YEARS 146097 //((4 * DAYS_PER_100YEARS) + 1L) // but years dividable by 400 are leap years |
- | 15 | #define SECONDS_PER_MINUTE 60 |
|
- | 16 | #define MINUTES_PER_HOUR 60 |
|
- | 17 | #define HOURS_PER_DAY 24 |
|
- | 18 | #define DAYS_PER_WEEK 7 |
|
- | 19 | #define SECONDS_PER_HOUR 3600 //(SECONDS_PER_MINUTE * MINUTES_PER_HOUR) |
|
- | 20 | #define SECONDS_PER_DAY 86400 //(SECONDS_PER_HOUR * HOURS_PER_DAY) |
|
- | 21 | #define SECONDS_PER_WEEK 604800 //(SECONDS_PER_DAY * DAYS_PER_WEEK) |
|
- | 22 | ||
Line -... | Line 23... | ||
- | 23 | // days per month in normal and leap years |
|
- | 24 | const uint32_t Leap[ 13 ] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }; |
|
- | 25 | const uint32_t Normal[ 13 ] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; |
|
- | 26 | ||
- | 27 | #define LEAP_SECONDS_FROM_1980 15 // the last one was on the Dec 31th 2008 |
|
- | 28 | ||
- | 29 | // message sync bytes |
|
- | 30 | #define UBX_SYNC1_CHAR 0xB5 |
|
17 | 31 | #define UBX_SYNC2_CHAR 0x62 |
|
18 | // ublox protocoll identifier |
32 | // protocoll identifier |
19 | #define UBX_CLASS_NAV 0x01 |
33 | #define UBX_CLASS_NAV 0x01 |
Line -... | Line 34... | ||
- | 34 | // message id |
|
- | 35 | #define UBX_ID_POSLLH 0x02 |
|
- | 36 | #define UBX_ID_SOL 0x06 |
|
- | 37 | #define UBX_ID_VELNED 0x12 |
|
- | 38 | ||
- | 39 | // ------------------------------------------------------------------------------------------------ |
|
- | 40 | // typedefs |
|
- | 41 | ||
- | 42 | ||
- | 43 | // ubx parser state |
|
20 | 44 | typedef enum |
|
- | 45 | { |
|
- | 46 | UBXSTATE_IDLE, |
|
- | 47 | UBXSTATE_SYNC1, |
|
21 | #define UBX_ID_POSLLH 0x02 |
48 | UBXSTATE_SYNC2, |
- | 49 | UBXSTATE_CLASS, |
|
- | 50 | UBXSTATE_LEN1, |
|
- | 51 | UBXSTATE_LEN2, |
|
- | 52 | UBXSTATE_DATA, |
|
- | 53 | UBXSTATE_CKA, |
|
- | 54 | UBXSTATE_CKB |
|
- | 55 | } ubxState_t; |
|
- | 56 | ||
- | 57 | typedef struct |
|
- | 58 | { |
|
- | 59 | uint32_t itow; // ms GPS Millisecond Time of Week |
|
- | 60 | int32_t frac; // ns remainder of rounded ms above |
|
- | 61 | int16_t week; // GPS week |
|
- | 62 | uint8_t GPSfix; // GPSfix Type, range 0..6 |
|
- | 63 | uint8_t Flags; // Navigation Status Flags |
|
- | 64 | int32_t ECEF_X; // cm ECEF X coordinate |
|
- | 65 | int32_t ECEF_Y; // cm ECEF Y coordinate |
|
- | 66 | int32_t ECEF_Z; // cm ECEF Z coordinate |
|
- | 67 | int32_t PAcc; // cm 3D Position Accuracy Estimate |
|
- | 68 | int32_t ECEFVX; // cm/s ECEF X velocity |
|
- | 69 | int32_t ECEFVY; // cm/s ECEF Y velocity |
|
- | 70 | int32_t ECEFVZ; // cm/s ECEF Z velocity |
|
- | 71 | uint32_t SAcc; // cm/s Speed Accuracy Estimate |
|
- | 72 | uint16_t PDOP; // 0.01 Position DOP |
|
- | 73 | uint8_t res1; // reserved |
|
- | 74 | uint8_t numSV; // Number of SVs used in navigation solution |
|
- | 75 | uint32_t res2; // reserved |
|
- | 76 | uint8_t Status; // invalid/newdata/processed |
|
- | 77 | } __attribute__((packed)) ubx_nav_sol_t; |
|
- | 78 | ||
- | 79 | ||
- | 80 | typedef struct |
|
- | 81 | { |
|
- | 82 | uint32_t itow; // ms GPS Millisecond Time of Week |
|
- | 83 | int32_t VEL_N; // cm/s NED north velocity |
|
- | 84 | int32_t VEL_E; // cm/s NED east velocity |
|
- | 85 | int32_t VEL_D; // cm/s NED down velocity |
|
- | 86 | int32_t Speed; // cm/s Speed (3-D) |
|
- | 87 | int32_t GSpeed; // cm/s Ground Speed (2-D) |
|
- | 88 | int32_t Heading; // 1e-05 deg Heading 2-D |
|
- | 89 | uint32_t SAcc; // cm/s Speed Accuracy Estimate |
|
- | 90 | uint32_t CAcc; // deg Course / Heading Accuracy Estimate |
|
- | 91 | uint8_t Status; // invalid/newdata/processed |
|
- | 92 | } __attribute__((packed)) ubx_nav_velned_t; |
|
- | 93 | ||
- | 94 | typedef struct |
|
- | 95 | { |
|
- | 96 | uint32_t itow; // ms GPS Millisecond Time of Week |
|
- | 97 | int32_t LON; // 1e-07 deg Longitude |
|
- | 98 | int32_t LAT; // 1e-07 deg Latitude |
|
- | 99 | int32_t HEIGHT; // mm Height above Ellipsoid |
|
- | 100 | int32_t HMSL; // mm Height above mean sea level |
|
- | 101 | uint32_t Hacc; // mm Horizontal Accuracy Estimate |
|
- | 102 | uint32_t Vacc; // mm Vertical Accuracy Estimate |
|
- | 103 | uint8_t Status; // invalid/newdata/processed |
|
- | 104 | } __attribute__((packed)) ubx_nav_posllh_t; |
|
- | 105 | ||
- | 106 | ||
- | 107 | ||
- | 108 | //------------------------------------------------------------------------------------ |
|
- | 109 | // global variables |
|
- | 110 | ||
- | 111 | // local buffers for the incomming ubx messages |
|
Line 22... | Line 112... | ||
22 | #define UBX_ID_SOL 0x06 |
112 | volatile ubx_nav_sol_t UbxSol = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, INVALID}; |
23 | #define UBX_ID_VELNED 0x12 |
- | |
24 | - | ||
25 | #define UBX_SYNC1_CHAR 0xB5 |
- | |
26 | #define UBX_SYNC2_CHAR 0x62 |
- | |
27 | - | ||
28 | typedef struct { |
- | |
29 | uint32_t ITOW; // ms GPS Millisecond Time of Week |
- | |
30 | int32_t Frac; // ns remainder of rounded ms above |
- | |
31 | int16_t week; // GPS week |
- | |
32 | uint8_t GPSfix; // GPSfix Type, range 0..6 |
- | |
33 | uint8_t Flags; // Navigation Status Flags |
- | |
34 | int32_t ECEF_X; // cm ECEF X coordinate |
- | |
35 | int32_t ECEF_Y; // cm ECEF Y coordinate |
- | |
36 | int32_t ECEF_Z; // cm ECEF Z coordinate |
- | |
37 | uint32_t PAcc; // cm 3D Position Accuracy Estimate |
- | |
38 | int32_t ECEFVX; // cm/s ECEF X velocity |
- | |
39 | int32_t ECEFVY; // cm/s ECEF Y velocity |
- | |
40 | int32_t ECEFVZ; // cm/s ECEF Z velocity |
- | |
41 | uint32_t SAcc; // cm/s Speed Accuracy Estimate |
- | |
42 | uint16_t PDOP; // 0.01 Position DOP |
- | |
43 | uint8_t res1; // reserved |
- | |
44 | uint8_t numSV; // Number of SVs used in navigation solution |
- | |
45 | uint32_t res2; // reserved |
- | |
46 | Status_t Status; |
- | |
47 | } UBX_SOL_t; |
- | |
48 | - | ||
49 | typedef struct { |
- | |
50 | uint32_t ITOW; // ms GPS Millisecond Time of Week |
- | |
51 | int32_t LON; // 1e-07 deg Longitude |
- | |
52 | int32_t LAT; // 1e-07 deg Latitude |
- | |
53 | int32_t HEIGHT; // mm Height above Ellipsoid |
- | |
54 | int32_t HMSL; // mm Height above mean sea level |
- | |
55 | uint32_t Hacc; // mm Horizontal Accuracy Estimate |
- | |
56 | uint32_t Vacc; // mm Vertical Accuracy Estimate |
- | |
57 | Status_t Status; |
- | |
58 | } UBX_POSLLH_t; |
- | |
59 | - | ||
60 | typedef struct { |
- | |
61 | uint32_t ITOW; // ms GPS Millisecond Time of Week |
- | |
62 | int32_t VEL_N; // cm/s NED north velocity |
- | |
63 | int32_t VEL_E; // cm/s NED east velocity |
- | |
64 | int32_t VEL_D; // cm/s NED down velocity |
- | |
65 | uint32_t Speed; // cm/s Speed (3-D) |
- | |
66 | uint32_t GSpeed; // cm/s Ground Speed (2-D) |
- | |
67 | int32_t Heading; // 1e-05 deg Heading 2-D |
- | |
68 | uint32_t SAcc; // cm/s Speed Accuracy Estimate |
- | |
69 | uint32_t CAcc; // deg Course / Heading Accuracy Estimate |
- | |
70 | Status_t Status; |
113 | volatile ubx_nav_posllh_t UbxPosLlh = {0,0,0,0,0,0,0, INVALID}; |
Line -... | Line 114... | ||
- | 114 | volatile ubx_nav_velned_t UbxVelNed = {0,0,0,0,0,0,0,0,0, INVALID}; |
|
71 | } UBX_VELNED_t; |
115 | |
Line 72... | Line 116... | ||
72 | 116 | uint16_t CheckGPSOkay = 0; |
|
73 | UBX_SOL_t UbxSol = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, INVALID}; |
117 | |
- | 118 | // shared buffer |
|
- | 119 | gps_data_t GPSData = {{0,0,0,INVALID},0,0,0,0,0,0,0, INVALID}; |
|
- | 120 | ||
- | 121 | //------------------------------------------------------------------------------------ |
|
- | 122 | // functions |
|
- | 123 | ||
- | 124 | uint8_t IsLeapYear(uint16_t year) |
|
- | 125 | { |
|
- | 126 | if((year%400 == 0) || ( (year%4 == 0) && (year%100 != 0) ) ) return 1; |
|
- | 127 | else return 0; |
|
- | 128 | } |
|
- | 129 | ||
- | 130 | /********************************************************/ |
|
- | 131 | /* Calculates the UTC Time from the GPS week and tow */ |
|
Line -... | Line 132... | ||
- | 132 | /********************************************************/ |
|
- | 133 | void SetGPSTime(DateTime_t * pTimeStruct) |
|
74 | UBX_POSLLH_t UbxPosLlh = {0,0,0,0,0,0,0, INVALID}; |
134 | { |
75 | UBX_VELNED_t UbxVelNed = {0,0,0,0,0,0,0,0,0, INVALID}; |
135 | uint32_t Days, Seconds, Week; |
- | 136 | uint16_t YearPart; |
|
- | 137 | uint32_t * MonthDayTab = 0; |
|
- | 138 | uint8_t i; |
|
76 | GPS_INFO_t GPSInfo = {0,0,0,0,0,0,0,0,0,0, INVALID}; |
139 | |
77 | 140 | ||
78 | volatile uint8_t GPSTimeout = 0; |
- | |
79 | 141 | ||
80 | void UpdateGPSInfo (void) |
- | |
81 | { |
- | |
82 | - | ||
83 | if ((UbxSol.Status == NEWDATA) && (UbxPosLlh.Status == NEWDATA) && (UbxVelNed.Status == NEWDATA)) |
- | |
84 | { |
- | |
85 | if(GPSInfo.status != NEWDATA) |
- | |
86 | { |
- | |
87 | GPSInfo.status = INVALID; |
- | |
88 | // NAV SOL |
142 | // if GPS data show valid time data |
89 | GPSInfo.flags = UbxSol.Flags; |
143 | if((UbxSol.Status != INVALID) && (UbxSol.Flags & FLAG_WKNSET) && (UbxSol.Flags & FLAG_TOWSET) ) |
90 | GPSInfo.satfix = UbxSol.GPSfix; |
- | |
91 | GPSInfo.satnum = UbxSol.numSV; |
- | |
92 | GPSInfo.PAcc = UbxSol.PAcc; |
144 | { |
93 | GPSInfo.VAcc = UbxSol.SAcc; |
- | |
94 | // NAV POSLLH |
- | |
95 | GPSInfo.longitude = UbxPosLlh.LON; |
- | |
Line -... | Line 145... | ||
- | 145 | Seconds = UbxSol.itow / 1000L; |
|
- | 146 | Week = (uint32_t)UbxSol.week; |
|
- | 147 | // correct leap seconds since 1980 |
|
- | 148 | if(Seconds < LEAP_SECONDS_FROM_1980) |
|
- | 149 | { |
|
- | 150 | Week--; |
|
- | 151 | Seconds = SECONDS_PER_WEEK - LEAP_SECONDS_FROM_1980 + Seconds; |
|
- | 152 | } |
|
- | 153 | else Seconds -= LEAP_SECONDS_FROM_1980; |
|
- | 154 | ||
- | 155 | Days = DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980; |
|
- | 156 | Days += (Week * DAYS_PER_WEEK); |
|
- | 157 | Days += Seconds / SECONDS_PER_DAY; // seperate days from GPS seconds of week |
|
- | 158 | ||
- | 159 | pTimeStruct->Year = 1; |
|
- | 160 | YearPart = (uint16_t)(Days / DAYS_PER_400YEARS); |
|
- | 161 | pTimeStruct->Year += YearPart * 400; |
|
- | 162 | Days = Days % DAYS_PER_400YEARS; |
|
- | 163 | YearPart = (uint16_t)(Days / DAYS_PER_100YEARS); |
|
- | 164 | pTimeStruct->Year += YearPart * 100; |
|
- | 165 | Days = Days % DAYS_PER_100YEARS; |
|
- | 166 | YearPart = (uint16_t)(Days / DAYS_PER_4YEARS); |
|
- | 167 | pTimeStruct->Year += YearPart * 4; |
|
- | 168 | Days = Days % DAYS_PER_4YEARS; |
|
- | 169 | if(Days < (3* DAYS_PER_YEAR)) YearPart = (uint16_t)(Days / DAYS_PER_YEAR); |
|
- | 170 | else YearPart = 3; |
|
- | 171 | pTimeStruct->Year += YearPart; |
|
- | 172 | // calculate remaining days of year |
|
- | 173 | Days -= (uint32_t)(YearPart * DAYS_PER_YEAR); |
|
- | 174 | Days += 1; |
|
- | 175 | // check if current year is a leap year |
|
- | 176 | if(IsLeapYear(pTimeStruct->Year)) MonthDayTab = (uint32_t*)Leap; |
|
96 | GPSInfo.latitude = UbxPosLlh.LAT; |
177 | else MonthDayTab = (uint32_t*)Normal; |
- | 178 | // seperate month and day from days of year |
|
97 | GPSInfo.altitude = UbxPosLlh.HEIGHT; |
179 | for ( i = 0; i < 12; i++ ) |
98 | 180 | { |
|
- | 181 | if ( (MonthDayTab[i]< Days) && (Days <= MonthDayTab[i+1]) ) |
|
99 | GPSInfo.veleast = UbxVelNed.VEL_E; |
182 | { |
- | 183 | pTimeStruct->Month = i+1; |
|
100 | GPSInfo.velnorth = UbxVelNed.VEL_N; |
184 | pTimeStruct->Day = Days - MonthDayTab[i]; |
- | 185 | i = 12; |
|
101 | GPSInfo.veltop = -UbxVelNed.VEL_D; |
186 | } |
- | 187 | } |
|
- | 188 | Seconds = Seconds % SECONDS_PER_DAY; // remaining seconds of current day |
|
- | 189 | pTimeStruct->Hour = (uint8_t)(Seconds / SECONDS_PER_HOUR); |
|
- | 190 | Seconds = Seconds % SECONDS_PER_HOUR; // remaining seconds of current hour |
|
- | 191 | pTimeStruct->Min = (uint8_t)(Seconds / SECONDS_PER_MINUTE); |
|
- | 192 | Seconds = Seconds % SECONDS_PER_MINUTE; // remaining seconds of current minute |
|
Line -... | Line 193... | ||
- | 193 | pTimeStruct->Sec = (uint8_t)(Seconds); |
|
- | 194 | pTimeStruct->mSec = (uint16_t)(UbxSol.itow % 1000L); |
|
- | 195 | pTimeStruct->Valid = 1; |
|
- | 196 | } |
|
- | 197 | else |
|
- | 198 | { |
|
- | 199 | pTimeStruct->Valid = 0; |
|
- | 200 | } |
|
- | 201 | } |
|
- | 202 | ||
- | 203 | ||
- | 204 | ||
- | 205 | /********************************************************/ |
|
- | 206 | /* Initialize UBX Parser */ |
|
- | 207 | /********************************************************/ |
|
- | 208 | void UBX_Init(void) |
|
- | 209 | { |
|
- | 210 | // mark msg buffers invalid |
|
- | 211 | UbxSol.Status = INVALID; |
|
- | 212 | UbxPosLlh.Status = INVALID; |
|
- | 213 | UbxVelNed.Status = INVALID; |
|
- | 214 | GPSData.Status = INVALID; |
|
- | 215 | } |
|
- | 216 | ||
- | 217 | /********************************************************/ |
|
- | 218 | /* Upate GPS data stcructure */ |
|
- | 219 | /********************************************************/ |
|
- | 220 | void Update_GPSData (void) |
|
- | 221 | { |
|
- | 222 | static uint16_t Ubx_Timeout = 0; |
|
- | 223 | static uint8_t Msg_Count = 0; |
|
- | 224 | ||
- | 225 | // the timeout is used to detect the delay between two message sets |
|
- | 226 | // and is used for synchronisation so that always a set is collected |
|
- | 227 | // that belongs together |
|
- | 228 | // _______NAVSOL|POSLLH|VELNED|___________________NAVSOL|POSLLH|VELNED|_____________ |
|
- | 229 | // | 8ms | 8ms | 184 ms | | | |
|
- | 230 | // msg_count: 0 1 2 0 1 2 |
|
- | 231 | ||
- | 232 | if(CheckDelay(Ubx_Timeout)) Msg_Count = 0; |
|
- | 233 | else Msg_Count++; |
|
- | 234 | Ubx_Timeout = SetDelay(100); // reset ubx msg timeout |
|
- | 235 | ||
- | 236 | // if a new set of ubx messages was collected |
|
- | 237 | if((Msg_Count >= 2)) |
|
- | 238 | { // if set is complete |
|
- | 239 | if((UbxSol.Status == NEWDATA) && (UbxPosLlh.Status == NEWDATA) && (UbxVelNed.Status == NEWDATA)) |
|
- | 240 | { |
|
- | 241 | CheckGPSOkay++; |
|
- | 242 | // update GPS data only if the status is INVALID or PROCESSED and the last ubx message was received within less than 100 ms |
|
- | 243 | if(GPSData.Status != NEWDATA) // if last data were processed |
|
- | 244 | { // wait for new data at all neccesary ubx messages |
|
- | 245 | GPSData.Status = INVALID; |
|
- | 246 | // NAV SOL |
|
- | 247 | GPSData.Flags = UbxSol.Flags; |
|
- | 248 | GPSData.NumOfSats = UbxSol.numSV; |
|
- | 249 | GPSData.SatFix = UbxSol.GPSfix; |
|
- | 250 | GPSData.Position_Accuracy = UbxSol.PAcc; |
|
- | 251 | GPSData.Speed_Accuracy = UbxSol.SAcc; |
|
- | 252 | SetGPSTime(&SystemTime); // update system time |
|
- | 253 | // NAV POSLLH |
|
- | 254 | GPSData.Position.Status = INVALID; |
|
- | 255 | GPSData.Position.Longitude = UbxPosLlh.LON; |
|
- | 256 | GPSData.Position.Latitude = UbxPosLlh.LAT; |
|
- | 257 | GPSData.Position.Altitude = UbxPosLlh.HMSL; |
|
- | 258 | GPSData.Position.Status = NEWDATA; |
|
- | 259 | // NAV VELNED |
|
- | 260 | GPSData.Speed_East = UbxVelNed.VEL_E; |
|
- | 261 | GPSData.Speed_North = UbxVelNed.VEL_N; |
|
102 | GPSInfo.velground = UbxVelNed.GSpeed; |
262 | GPSData.Speed_Top = -UbxVelNed.VEL_D; |
Line -... | Line 263... | ||
- | 263 | GPSData.Speed_Ground = UbxVelNed.GSpeed; |
|
103 | 264 | GPSData.Heading = UbxVelNed.Heading; |
|
- | 265 | ||
104 | GPSInfo.status = NEWDATA; |
266 | GPSData.Status = NEWDATA; // new data available |
105 | 267 | } // EOF if(GPSData.Status != NEWDATA) |
|
106 | } |
268 | } // EOF all ubx messages received |
107 | // set state to collect new data |
- | |
108 | UbxSol.Status = PROCESSED; // never update old data |
269 | // set state to collect new data |
- | 270 | UbxSol.Status = PROCESSED; // ready for new data |
|
109 | UbxPosLlh.Status = PROCESSED; // never update old data |
271 | UbxPosLlh.Status = PROCESSED; // ready for new data |
- | 272 | UbxVelNed.Status = PROCESSED; // ready for new data |
|
Line 110... | Line 273... | ||
110 | UbxVelNed.Status = PROCESSED; // never update old data |
273 | } |
- | 274 | } |
|
111 | } |
275 | |
112 | 276 | ||
113 | 277 | /********************************************************/ |
|
114 | } |
278 | /* UBX Parser */ |
115 | 279 | /********************************************************/ |
|
Line 116... | Line 280... | ||
116 | 280 | void UBX_Parser(uint8_t c) |
|
117 | // this function should be called within the UART RX ISR |
281 | { |
118 | void ubx_parser(uint8_t c) |
282 | static ubxState_t ubxState = UBXSTATE_IDLE; |
119 | { |
283 | static uint16_t msglen; |
Line 120... | Line 284... | ||
120 | static uint8_t ubxstate = UBXSTATE_IDLE; |
284 | static uint8_t cka, ckb; |
121 | static uint8_t cka, ckb; |
285 | static uint8_t *ubxP, *ubxEp, *ubxSp; // pointers to data currently transfered |
122 | static uint16_t msglen; |
286 | |
123 | static int8_t *ubxP, *ubxEp, *ubxSp; // pointers to data currently transfered |
287 | |
Line 124... | Line 288... | ||
124 | 288 | //state machine |
|
125 | switch(ubxstate) |
289 | switch (ubxState) // ubx message parser |
126 | { |
290 | { |
127 | case UBXSTATE_IDLE: // check 1st sync byte |
291 | case UBXSTATE_IDLE: // check 1st sync byte |
128 | if (c == UBX_SYNC1_CHAR) ubxstate = UBXSTATE_SYNC1; |
292 | if (c == UBX_SYNC1_CHAR) ubxState = UBXSTATE_SYNC1; |
129 | else ubxstate = UBXSTATE_IDLE; // out of synchronization |
293 | else ubxState = UBXSTATE_IDLE; // out of synchronization |
130 | break; |
294 | break; |
131 | 295 | ||
Line 132... | Line 296... | ||
132 | case UBXSTATE_SYNC1: // check 2nd sync byte |
296 | case UBXSTATE_SYNC1: // check 2nd sync byte |
133 | if (c == UBX_SYNC2_CHAR) ubxstate = UBXSTATE_SYNC2; |
297 | if (c == UBX_SYNC2_CHAR) ubxState = UBXSTATE_SYNC2; |
134 | else ubxstate = UBXSTATE_IDLE; // out of synchronization |
298 | else ubxState = UBXSTATE_IDLE; // out of synchronization |
135 | break; |
299 | break; |
136 | 300 | ||
Line 137... | Line 301... | ||
137 | case UBXSTATE_SYNC2: // check msg class to be NAV |
301 | case UBXSTATE_SYNC2: // check msg class to be NAV |
138 | if (c == UBX_CLASS_NAV) ubxstate = UBXSTATE_CLASS; |
302 | if (c == UBX_CLASS_NAV) ubxState = UBXSTATE_CLASS; |
139 | else ubxstate = UBXSTATE_IDLE; // unsupported message class |
303 | else ubxState = UBXSTATE_IDLE; // unsupported message class |
140 | break; |
304 | break; |
141 | 305 | ||
Line 142... | Line 306... | ||
142 | case UBXSTATE_CLASS: // check message identifier |
306 | case UBXSTATE_CLASS: // check message identifier |
143 | switch(c) |
307 | switch(c) |
144 | { |
308 | { |
145 | case UBX_ID_POSLLH: // geodetic position |
309 | case UBX_ID_POSLLH: // geodetic position |
146 | ubxP = (int8_t *)&UbxPosLlh; // data start pointer |
310 | ubxP = (uint8_t *)&UbxPosLlh; // data start pointer |
147 | ubxEp = (int8_t *)(&UbxPosLlh + 1); // data end pointer |
311 | ubxEp = (uint8_t *)(&UbxPosLlh + 1); // data end pointer |
148 | ubxSp = (int8_t *)&UbxPosLlh.Status; // status pointer |
312 | ubxSp = (uint8_t *)&UbxPosLlh.Status; // status pointer |
149 | break; |
313 | break; |
150 | 314 | ||
151 | case UBX_ID_SOL: // navigation solution |
315 | case UBX_ID_SOL: // navigation solution |
152 | ubxP = (int8_t *)&UbxSol; // data start pointer |
316 | ubxP = (uint8_t *)&UbxSol; // data start pointer |
Line 153... | Line 317... | ||
153 | ubxEp = (int8_t *)(&UbxSol + 1); // data end pointer |
317 | ubxEp = (uint8_t *)(&UbxSol + 1); // data end pointer |
154 | ubxSp = (int8_t *)&UbxSol.Status; // status pointer |
318 | ubxSp = (uint8_t *)&UbxSol.Status; // status pointer |
155 | break; |
319 | break; |
156 | 320 | ||
157 | case UBX_ID_VELNED: // velocity vector in tangent plane |
321 | case UBX_ID_VELNED: // velocity vector in tangent plane |
158 | ubxP = (int8_t *)&UbxVelNed; // data start pointer |
322 | ubxP = (uint8_t *)&UbxVelNed; // data start pointer |
Line 159... | Line 323... | ||
159 | ubxEp = (int8_t *)(&UbxVelNed + 1); // data end pointer |
323 | ubxEp = (uint8_t *)(&UbxVelNed + 1); // data end pointer |
160 | ubxSp = (int8_t *)&UbxVelNed.Status; // status pointer |
324 | ubxSp = (uint8_t *)&UbxVelNed.Status; // status pointer |
161 | break; |
325 | break; |
162 | 326 | ||
163 | default: // unsupported identifier |
327 | default: // unsupported identifier |
164 | ubxstate = UBXSTATE_IDLE; |
328 | ubxState = UBXSTATE_IDLE; |
165 | break; |
329 | break; |
166 | } |
330 | } |
167 | if (ubxstate != UBXSTATE_IDLE) |
- | |
168 | { |
331 | if (ubxState != UBXSTATE_IDLE) |
- | 332 | { |
|
169 | ubxstate = UBXSTATE_LEN1; |
333 | ubxState = UBXSTATE_LEN1; |
170 | cka = UBX_CLASS_NAV + c; |
334 | cka = UBX_CLASS_NAV + c; |
171 | ckb = UBX_CLASS_NAV + cka; |
335 | ckb = UBX_CLASS_NAV + cka; |
172 | } |
336 | } |
173 | break; |
337 | break; |
174 | 338 | ||
175 | case UBXSTATE_LEN1: // 1st message length byte |
339 | case UBXSTATE_LEN1: // 1st message length byte |
Line 176... | Line 340... | ||
176 | msglen = c; |
340 | msglen = (uint16_t)c; // lowbyte first |
- | 341 | cka += c; |
|
- | 342 | ckb += cka; |
|
177 | cka += c; |
343 | ubxState = UBXSTATE_LEN2; |
178 | ckb += cka; |
344 | break; |
179 | ubxstate = UBXSTATE_LEN2; |
345 | |
180 | break; |
346 | case UBXSTATE_LEN2: // 2nd message length byte |
- | 347 | msglen += ((uint16_t)c)<<8; // high byte last |
|
- | 348 | cka += c; |
|
- | 349 | ckb += cka; |
|
- | 350 | // if the old data are not processed so far then break parsing now |
|
- | 351 | // to avoid writing new data in ISR during reading by another function |
|
181 | 352 | if ( *ubxSp == NEWDATA ) |
|
Line 182... | Line 353... | ||
182 | case UBXSTATE_LEN2: // 2nd message length byte |
353 | { |
183 | msglen += ((uint16_t)c)<<8; |
354 | ubxState = UBXSTATE_IDLE; |
184 | cka += c; |
355 | Update_GPSData(); //update GPS info respectively |
185 | ckb += cka; |
356 | } |
186 | // if the old data are not processed so far then break parsing now |
357 | else // data invalid or allready processd |
187 | // to avoid writing new data in ISR during reading by another function |
358 | { |
188 | if ( *ubxSp == NEWDATA ) |
359 | *ubxSp = INVALID; // mark invalid during buffer filling |
189 | { |
360 | ubxState = UBXSTATE_DATA; |
Line 190... | Line 361... | ||
190 | UpdateGPSInfo(); //update GPS info respectively |
361 | } |
191 | ubxstate = UBXSTATE_IDLE; |
362 | break; |
192 | } |
363 | |
193 | else // data invalid or allready processd |
364 | case UBXSTATE_DATA: // collecting data |
194 | { |
365 | if (ubxP < ubxEp) |
195 | *ubxSp = INVALID; |
- | |
196 | ubxstate = UBXSTATE_DATA; |
366 | { |
197 | } |
367 | *ubxP++ = c; // copy curent data byte if any space is left |
198 | break; |
368 | cka += c; |
199 | 369 | ckb += cka; |
|
200 | case UBXSTATE_DATA: |
370 | if (--msglen == 0) ubxState = UBXSTATE_CKA; // switch to next state if all data was read |
201 | if (ubxP < ubxEp) *ubxP++ = c; // copy curent data byte if any space is left |
371 | } |
202 | cka += c; |
372 | else // rx buffer overrun |
Line 203... | Line 373... | ||
203 | ckb += cka; |
373 | { |
204 | if (--msglen == 0) ubxstate = UBXSTATE_CKA; // switch to next state if all data was read |
374 | ubxState = UBXSTATE_IDLE; |
205 | break; |
375 | } |
206 | - | ||
Line -... | Line 376... | ||
- | 376 | break; |
|
207 | case UBXSTATE_CKA: |
377 | |
208 | if (c == cka) ubxstate = UBXSTATE_CKB; |
- | |
209 | else |
- |