Rev 171 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 171 | Rev 175 | ||
---|---|---|---|
1 | /*#######################################################################################*/ |
1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
3 | /*#######################################################################################*/ |
3 | /*#######################################################################################*/ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
6 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
6 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
7 | // + FOR NON COMMERCIAL USE ONLY |
7 | // + FOR NON COMMERCIAL USE ONLY |
8 | // + www.MikroKopter.com |
8 | // + www.MikroKopter.com |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
22 | // + eindeutig als Ursprung verlinkt werden |
22 | // + eindeutig als Ursprung verlinkt werden |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
25 | // + Benutzung auf eigene Gefahr |
25 | // + Benutzung auf eigene Gefahr |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
28 | // + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
28 | // + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
29 | // + mit unserer Zustimmung zulässig |
29 | // + mit unserer Zustimmung zulässig |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
34 | // + this list of conditions and the following disclaimer. |
34 | // + this list of conditions and the following disclaimer. |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
36 | // + from this software without specific prior written permission. |
36 | // + from this software without specific prior written permission. |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
38 | // + for non-commercial use (directly or indirectly) |
38 | // + for non-commercial use (directly or indirectly) |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
40 | // + with our written permission |
40 | // + with our written permission |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
42 | // + clearly linked as origin |
42 | // + clearly linked as origin |
43 | // + * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
43 | // + * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
44 | // |
44 | // |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | #include "91x_lib.h" |
57 | #include "91x_lib.h" |
58 | #include "uart1.h" |
58 | #include "uart1.h" |
59 | #include "ubx.h" |
59 | #include "ubx.h" |
60 | #include "led.h" |
60 | #include "led.h" |
61 | #include "timer1.h" |
61 | #include "timer1.h" |
62 | 62 | ||
63 | // ------------------------------------------------------------------------------------------------ |
63 | // ------------------------------------------------------------------------------------------------ |
64 | // defines |
64 | // defines |
65 | 65 | ||
66 | #define DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980 722819 // the year 0 does not exist! |
66 | #define DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980 722819 // the year 0 does not exist! |
67 | #define DAYS_PER_YEAR 365 |
67 | #define DAYS_PER_YEAR 365 |
68 | #define DAYS_PER_LEAPYEAR 366 |
68 | #define DAYS_PER_LEAPYEAR 366 |
69 | #define DAYS_PER_4YEARS 1461 //((3 * DAYS_PER_YEAR) + DAYS_PER_LEAPYEAR) // years dividable by 4 are leap years |
69 | #define DAYS_PER_4YEARS 1461 //((3 * DAYS_PER_YEAR) + DAYS_PER_LEAPYEAR) // years dividable by 4 are leap years |
70 | #define DAYS_PER_100YEARS 36524 //((25 * DAYS_PER_4YEARS) - 1) // years dividable by 100 are no leap years |
70 | #define DAYS_PER_100YEARS 36524 //((25 * DAYS_PER_4YEARS) - 1) // years dividable by 100 are no leap years |
71 | #define DAYS_PER_400YEARS 146097 //((4 * DAYS_PER_100YEARS) + 1L) // but years dividable by 400 are leap years |
71 | #define DAYS_PER_400YEARS 146097 //((4 * DAYS_PER_100YEARS) + 1L) // but years dividable by 400 are leap years |
72 | #define SECONDS_PER_MINUTE 60 |
72 | #define SECONDS_PER_MINUTE 60 |
73 | #define MINUTES_PER_HOUR 60 |
73 | #define MINUTES_PER_HOUR 60 |
74 | #define HOURS_PER_DAY 24 |
74 | #define HOURS_PER_DAY 24 |
75 | #define DAYS_PER_WEEK 7 |
75 | #define DAYS_PER_WEEK 7 |
76 | #define SECONDS_PER_HOUR 3600 //(SECONDS_PER_MINUTE * MINUTES_PER_HOUR) |
76 | #define SECONDS_PER_HOUR 3600 //(SECONDS_PER_MINUTE * MINUTES_PER_HOUR) |
77 | #define SECONDS_PER_DAY 86400 //(SECONDS_PER_HOUR * HOURS_PER_DAY) |
77 | #define SECONDS_PER_DAY 86400 //(SECONDS_PER_HOUR * HOURS_PER_DAY) |
78 | #define SECONDS_PER_WEEK 604800 //(SECONDS_PER_DAY * DAYS_PER_WEEK) |
78 | #define SECONDS_PER_WEEK 604800 //(SECONDS_PER_DAY * DAYS_PER_WEEK) |
79 | 79 | ||
80 | // days per month in normal and leap years |
80 | // days per month in normal and leap years |
81 | const u32 Leap[ 13 ] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }; |
81 | const u32 Leap[ 13 ] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }; |
82 | const u32 Normal[ 13 ] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; |
82 | const u32 Normal[ 13 ] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; |
83 | 83 | ||
84 | #define LEAP_SECONDS_FROM_1980 13 |
84 | #define LEAP_SECONDS_FROM_1980 13 |
85 | 85 | ||
86 | // message sync bytes |
86 | // message sync bytes |
87 | #define UBX_SYNC1_CHAR 0xB5 |
87 | #define UBX_SYNC1_CHAR 0xB5 |
88 | #define UBX_SYNC2_CHAR 0x62 |
88 | #define UBX_SYNC2_CHAR 0x62 |
89 | // protocoll identifier |
89 | // protocoll identifier |
90 | #define UBX_CLASS_NAV 0x01 |
90 | #define UBX_CLASS_NAV 0x01 |
91 | // message id |
91 | // message id |
92 | #define UBX_ID_POSLLH 0x02 |
92 | #define UBX_ID_POSLLH 0x02 |
93 | #define UBX_ID_SOL 0x06 |
93 | #define UBX_ID_SOL 0x06 |
94 | #define UBX_ID_VELNED 0x12 |
94 | #define UBX_ID_VELNED 0x12 |
95 | 95 | ||
96 | // ------------------------------------------------------------------------------------------------ |
96 | // ------------------------------------------------------------------------------------------------ |
97 | // typedefs |
97 | // typedefs |
98 | 98 | ||
99 | 99 | ||
100 | // ubx parser state |
100 | // ubx parser state |
101 | typedef enum |
101 | typedef enum |
102 | { |
102 | { |
103 | UBXSTATE_IDLE, |
103 | UBXSTATE_IDLE, |
104 | UBXSTATE_SYNC1, |
104 | UBXSTATE_SYNC1, |
105 | UBXSTATE_SYNC2, |
105 | UBXSTATE_SYNC2, |
106 | UBXSTATE_CLASS, |
106 | UBXSTATE_CLASS, |
107 | UBXSTATE_LEN1, |
107 | UBXSTATE_LEN1, |
108 | UBXSTATE_LEN2, |
108 | UBXSTATE_LEN2, |
109 | UBXSTATE_DATA, |
109 | UBXSTATE_DATA, |
110 | UBXSTATE_CKA, |
110 | UBXSTATE_CKA, |
111 | UBXSTATE_CKB |
111 | UBXSTATE_CKB |
112 | } ubxState_t; |
112 | } ubxState_t; |
113 | 113 | ||
114 | typedef struct |
114 | typedef struct |
115 | { |
115 | { |
116 | u32 itow; // ms GPS Millisecond Time of Week |
116 | u32 itow; // ms GPS Millisecond Time of Week |
117 | s32 frac; // ns remainder of rounded ms above |
117 | s32 frac; // ns remainder of rounded ms above |
118 | s16 week; // GPS week |
118 | s16 week; // GPS week |
119 | u8 GPSfix; // GPSfix Type, range 0..6 |
119 | u8 GPSfix; // GPSfix Type, range 0..6 |
120 | u8 Flags; // Navigation Status Flags |
120 | u8 Flags; // Navigation Status Flags |
121 | s32 ECEF_X; // cm ECEF X coordinate |
121 | s32 ECEF_X; // cm ECEF X coordinate |
122 | s32 ECEF_Y; // cm ECEF Y coordinate |
122 | s32 ECEF_Y; // cm ECEF Y coordinate |
123 | s32 ECEF_Z; // cm ECEF Z coordinate |
123 | s32 ECEF_Z; // cm ECEF Z coordinate |
124 | u32 PAcc; // cm 3D Position Accuracy Estimate |
124 | u32 PAcc; // cm 3D Position Accuracy Estimate |
125 | s32 ECEFVX; // cm/s ECEF X velocity |
125 | s32 ECEFVX; // cm/s ECEF X velocity |
126 | s32 ECEFVY; // cm/s ECEF Y velocity |
126 | s32 ECEFVY; // cm/s ECEF Y velocity |
127 | s32 ECEFVZ; // cm/s ECEF Z velocity |
127 | s32 ECEFVZ; // cm/s ECEF Z velocity |
128 | u32 SAcc; // cm/s Speed Accuracy Estimate |
128 | u32 SAcc; // cm/s Speed Accuracy Estimate |
129 | u16 PDOP; // 0.01 Position DOP |
129 | u16 PDOP; // 0.01 Position DOP |
130 | u8 res1; // reserved |
130 | u8 res1; // reserved |
131 | u8 numSV; // Number of SVs used in navigation solution |
131 | u8 numSV; // Number of SVs used in navigation solution |
132 | u32 res2; // reserved |
132 | u32 res2; // reserved |
133 | u8 Status; // invalid/newdata/processed |
133 | u8 Status; // invalid/newdata/processed |
134 | } __attribute__((packed)) ubx_nav_sol_t; |
134 | } __attribute__((packed)) ubx_nav_sol_t; |
135 | 135 | ||
136 | 136 | ||
137 | typedef struct |
137 | typedef struct |
138 | { |
138 | { |
139 | u32 itow; // ms GPS Millisecond Time of Week |
139 | u32 itow; // ms GPS Millisecond Time of Week |
140 | s32 VEL_N; // cm/s NED north velocity |
140 | s32 VEL_N; // cm/s NED north velocity |
141 | s32 VEL_E; // cm/s NED east velocity |
141 | s32 VEL_E; // cm/s NED east velocity |
142 | s32 VEL_D; // cm/s NED down velocity |
142 | s32 VEL_D; // cm/s NED down velocity |
143 | u32 Speed; // cm/s Speed (3-D) |
143 | u32 Speed; // cm/s Speed (3-D) |
144 | u32 GSpeed; // cm/s Ground Speed (2-D) |
144 | u32 GSpeed; // cm/s Ground Speed (2-D) |
145 | s32 Heading; // 1e-05 deg Heading 2-D |
145 | s32 Heading; // 1e-05 deg Heading 2-D |
146 | u32 SAcc; // cm/s Speed Accuracy Estimate |
146 | u32 SAcc; // cm/s Speed Accuracy Estimate |
147 | u32 CAcc; // deg Course / Heading Accuracy Estimate |
147 | u32 CAcc; // deg Course / Heading Accuracy Estimate |
148 | u8 Status; // invalid/newdata/processed |
148 | u8 Status; // invalid/newdata/processed |
149 | } __attribute__((packed)) ubx_nav_velned_t; |
149 | } __attribute__((packed)) ubx_nav_velned_t; |
150 | 150 | ||
151 | typedef struct |
151 | typedef struct |
152 | { |
152 | { |
153 | u32 itow; // ms GPS Millisecond Time of Week |
153 | u32 itow; // ms GPS Millisecond Time of Week |
154 | s32 LON; // 1e-07 deg Longitude |
154 | s32 LON; // 1e-07 deg Longitude |
155 | s32 LAT; // 1e-07 deg Latitude |
155 | s32 LAT; // 1e-07 deg Latitude |
156 | s32 HEIGHT; // mm Height above Ellipsoid |
156 | s32 HEIGHT; // mm Height above Ellipsoid |
157 | s32 HMSL; // mm Height above mean sea level |
157 | s32 HMSL; // mm Height above mean sea level |
158 | u32 Hacc; // mm Horizontal Accuracy Estimate |
158 | u32 Hacc; // mm Horizontal Accuracy Estimate |
159 | u32 Vacc; // mm Vertical Accuracy Estimate |
159 | u32 Vacc; // mm Vertical Accuracy Estimate |
160 | u8 Status; // invalid/newdata/processed |
160 | u8 Status; // invalid/newdata/processed |
161 | } __attribute__((packed)) ubx_nav_posllh_t; |
161 | } __attribute__((packed)) ubx_nav_posllh_t; |
162 | 162 | ||
163 | 163 | ||
164 | 164 | ||
165 | //------------------------------------------------------------------------------------ |
165 | //------------------------------------------------------------------------------------ |
166 | // global variables |
166 | // global variables |
167 | 167 | ||
168 | // local buffers for the incomming ubx messages |
168 | // local buffers for the incomming ubx messages |
169 | volatile ubx_nav_sol_t UbxSol = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, INVALID}; |
169 | volatile ubx_nav_sol_t UbxSol = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, INVALID}; |
170 | volatile ubx_nav_posllh_t UbxPosLlh = {0,0,0,0,0,0,0, INVALID}; |
170 | volatile ubx_nav_posllh_t UbxPosLlh = {0,0,0,0,0,0,0, INVALID}; |
171 | volatile ubx_nav_velned_t UbxVelNed = {0,0,0,0,0,0,0,0,0, INVALID}; |
171 | volatile ubx_nav_velned_t UbxVelNed = {0,0,0,0,0,0,0,0,0, INVALID}; |
172 | 172 | ||
173 | // shared buffer |
173 | // shared buffer |
174 | gps_data_t GPSData = {{0,0,0,INVALID},0,0,0,0,0,0,0, INVALID}; |
174 | gps_data_t GPSData = {{0,0,0,INVALID},0,0,0,0,0,0,0, INVALID}; |
175 | DateTime_t GPSDateTime = {0,0,0,0,0,0,0, INVALID}; |
175 | DateTime_t GPSDateTime = {0,0,0,0,0,0,0, INVALID}; |
176 | 176 | ||
177 | #define UBX_TIMEOUT 500 // 500 ms |
177 | #define UBX_TIMEOUT 500 // 500 ms |
178 | u32 UBX_Timeout = 0; |
178 | u32 UBX_Timeout = 0; |
179 | 179 | ||
180 | //------------------------------------------------------------------------------------ |
180 | //------------------------------------------------------------------------------------ |
181 | // functions |
181 | // functions |
182 | 182 | ||
183 | u8 IsLeapYear(u16 year) |
183 | u8 IsLeapYear(u16 year) |
184 | { |
184 | { |
185 | if((year%400 == 0) || ( (year%4 == 0) && (year%100 != 0) ) ) return 1; |
185 | if((year%400 == 0) || ( (year%4 == 0) && (year%100 != 0) ) ) return 1; |
186 | else return 0; |
186 | else return 0; |
187 | } |
187 | } |
188 | /********************************************************/ |
188 | /********************************************************/ |
189 | /* Calculates the UTC Time from the GPS week and tow */ |
189 | /* Calculates the UTC Time from the GPS week and tow */ |
190 | /********************************************************/ |
190 | /********************************************************/ |
191 | void SetGPSTime(DateTime_t * pTimeStruct) |
191 | void SetGPSTime(DateTime_t * pTimeStruct) |
192 | { |
192 | { |
193 | u32 Days, Seconds, Week; |
193 | u32 Days, Seconds, Week; |
194 | u16 YearPart; |
194 | u16 YearPart; |
195 | u32 * MonthDayTab = 0; |
195 | u32 * MonthDayTab = 0; |
196 | u8 i; |
196 | u8 i; |
197 | 197 | ||
198 | - | ||
199 | 198 | ||
200 | // if GPS data show valid time data |
199 | // if GPS data show valid time data |
201 | if((UbxSol.Status != INVALID) && (UbxSol.Flags & FLAG_WKNSET) && (UbxSol.Flags & FLAG_TOWSET) ) |
200 | if((UbxSol.Status != INVALID) && (UbxSol.Flags & FLAG_WKNSET) && (UbxSol.Flags & FLAG_TOWSET) ) |
202 | { |
201 | { |
203 | Seconds = UbxSol.itow / 1000L; |
202 | Seconds = UbxSol.itow / 1000L; |
204 | Week = (u32)UbxSol.week; |
203 | Week = (u32)UbxSol.week; |
205 | // correct leap seconds since 1980 |
204 | // correct leap seconds since 1980 |
206 | if(Seconds < LEAP_SECONDS_FROM_1980) |
205 | if(Seconds < LEAP_SECONDS_FROM_1980) |
207 | { |
206 | { |
208 | Week--; |
207 | Week--; |
209 | Seconds = SECONDS_PER_WEEK - LEAP_SECONDS_FROM_1980 + Seconds; |
208 | Seconds = SECONDS_PER_WEEK - LEAP_SECONDS_FROM_1980 + Seconds; |
210 | } |
209 | } |
211 | else Seconds -= LEAP_SECONDS_FROM_1980; |
210 | else Seconds -= LEAP_SECONDS_FROM_1980; |
212 | 211 | ||
213 | Days = DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980; |
212 | Days = DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980; |
214 | Days += (Week * DAYS_PER_WEEK); |
213 | Days += (Week * DAYS_PER_WEEK); |
215 | Days += Seconds / SECONDS_PER_DAY; // seperate days from GPS seconds of week |
214 | Days += Seconds / SECONDS_PER_DAY; // seperate days from GPS seconds of week |
216 | 215 | ||
217 | pTimeStruct->Year = 1; |
216 | pTimeStruct->Year = 1; |
218 | YearPart = (u16)(Days / DAYS_PER_400YEARS); |
217 | YearPart = (u16)(Days / DAYS_PER_400YEARS); |
219 | pTimeStruct->Year += YearPart * 400; |
218 | pTimeStruct->Year += YearPart * 400; |
220 | Days = Days % DAYS_PER_400YEARS; |
219 | Days = Days % DAYS_PER_400YEARS; |
221 | YearPart = (u16)(Days / DAYS_PER_100YEARS); |
220 | YearPart = (u16)(Days / DAYS_PER_100YEARS); |
222 | pTimeStruct->Year += YearPart * 100; |
221 | pTimeStruct->Year += YearPart * 100; |
223 | Days = Days % DAYS_PER_100YEARS; |
222 | Days = Days % DAYS_PER_100YEARS; |
224 | YearPart = (u16)(Days / DAYS_PER_4YEARS); |
223 | YearPart = (u16)(Days / DAYS_PER_4YEARS); |
225 | pTimeStruct->Year += YearPart * 4; |
224 | pTimeStruct->Year += YearPart * 4; |
226 | Days = Days % DAYS_PER_4YEARS; |
225 | Days = Days % DAYS_PER_4YEARS; |
227 | if(Days < (3* DAYS_PER_YEAR)) YearPart = (u16)(Days / DAYS_PER_YEAR); |
226 | if(Days < (3* DAYS_PER_YEAR)) YearPart = (u16)(Days / DAYS_PER_YEAR); |
228 | else YearPart = 3; |
227 | else YearPart = 3; |
229 | pTimeStruct->Year += YearPart; |
228 | pTimeStruct->Year += YearPart; |
230 | // calculate remaining days of year |
229 | // calculate remaining days of year |
231 | Days -= (u32)(YearPart * DAYS_PER_YEAR); |
230 | Days -= (u32)(YearPart * DAYS_PER_YEAR); |
232 | Days += 1; |
231 | Days += 1; |
233 | // check if current year is a leap year |
232 | // check if current year is a leap year |
234 | if(IsLeapYear(pTimeStruct->Year)) MonthDayTab = (u32*)Leap; |
233 | if(IsLeapYear(pTimeStruct->Year)) MonthDayTab = (u32*)Leap; |
235 | else MonthDayTab = (u32*)Normal; |
234 | else MonthDayTab = (u32*)Normal; |
236 | // seperate month and day from days of year |
235 | // seperate month and day from days of year |
237 | for ( i = 0; i < 12; i++ ) |
236 | for ( i = 0; i < 12; i++ ) |
238 | { |
237 | { |
239 | if ( (MonthDayTab[i]< Days) && (Days <= MonthDayTab[i+1]) ) |
238 | if ( (MonthDayTab[i]< Days) && (Days <= MonthDayTab[i+1]) ) |
240 | { |
239 | { |
241 | pTimeStruct->Month = i+1; |
240 | pTimeStruct->Month = i+1; |
242 | pTimeStruct->Day = Days - MonthDayTab[i]; |
241 | pTimeStruct->Day = Days - MonthDayTab[i]; |
243 | i = 12; |
242 | i = 12; |
244 | } |
243 | } |
245 | } |
244 | } |
246 | Seconds = Seconds % SECONDS_PER_DAY; // remaining seconds of current day |
245 | Seconds = Seconds % SECONDS_PER_DAY; // remaining seconds of current day |
247 | pTimeStruct->Hour = (u8)(Seconds / SECONDS_PER_HOUR); |
246 | pTimeStruct->Hour = (u8)(Seconds / SECONDS_PER_HOUR); |
248 | Seconds = Seconds % SECONDS_PER_HOUR; // remaining seconds of current hour |
247 | Seconds = Seconds % SECONDS_PER_HOUR; // remaining seconds of current hour |
249 | pTimeStruct->Min = (u8)(Seconds / SECONDS_PER_MINUTE); |
248 | pTimeStruct->Min = (u8)(Seconds / SECONDS_PER_MINUTE); |
250 | Seconds = Seconds % SECONDS_PER_MINUTE; // remaining seconds of current minute |
249 | Seconds = Seconds % SECONDS_PER_MINUTE; // remaining seconds of current minute |
251 | pTimeStruct->Sec = (u8)(Seconds); |
250 | pTimeStruct->Sec = (u8)(Seconds); |
252 | pTimeStruct->mSec = (u16)(UbxSol.itow % 1000L); |
251 | pTimeStruct->mSec = (u16)(UbxSol.itow % 1000L); |
253 | pTimeStruct->Valid = 1; |
252 | pTimeStruct->Valid = 1; |
254 | } |
253 | } |
255 | else |
254 | else |
256 | { |
255 | { |
257 | pTimeStruct->Valid = 0; |
256 | pTimeStruct->Valid = 0; |
258 | } |
257 | } |
259 | } |
258 | } |
260 | 259 | ||
261 | 260 | ||
262 | 261 | ||
263 | /********************************************************/ |
262 | /********************************************************/ |
264 | /* Initialize UBX Parser */ |
263 | /* Initialize UBX Parser */ |
265 | /********************************************************/ |
264 | /********************************************************/ |
266 | void UBX_Init(void) |
265 | void UBX_Init(void) |
267 | { |
266 | { |
268 | // mark msg buffers invalid |
267 | // mark msg buffers invalid |
269 | UbxSol.Status = INVALID; |
268 | UbxSol.Status = INVALID; |
270 | UbxPosLlh.Status = INVALID; |
269 | UbxPosLlh.Status = INVALID; |
271 | UbxVelNed.Status = INVALID; |
270 | UbxVelNed.Status = INVALID; |
272 | GPSData.Status = INVALID; |
271 | GPSData.Status = INVALID; |
273 | 272 | ||
274 | UBX_Timeout = SetDelay(2 * UBX_Timeout); |
273 | UBX_Timeout = SetDelay(2 * UBX_Timeout); |
275 | } |
274 | } |
276 | 275 | ||
277 | /********************************************************/ |
276 | /********************************************************/ |
278 | /* Upate GPS data stcructure */ |
277 | /* Upate GPS data stcructure */ |
279 | /********************************************************/ |
278 | /********************************************************/ |
280 | void Update_GPSData (void) |
279 | void Update_GPSData (void) |
281 | { |
280 | { |
282 | static u32 Msg_Count_Timeout = 0; |
281 | static u32 Msg_Count_Timeout = 0; |
283 | static u8 Msg_Count = 0; |
282 | static u8 Msg_Count = 0; |
284 | 283 | ||
285 | // the timeout is used to detect the delay between two message sets |
284 | // the timeout is used to detect the delay between two message sets |
286 | // and is used for synchronisation so that always a set is collected |
285 | // and is used for synchronisation so that always a set is collected |
287 | // that belongs together |
286 | // that belongs together |
288 | // _______NAVSOL|POSLLH|VELNED|___________________NAVSOL|POSLLH|VELNED|_____________ |
287 | // _______NAVSOL|POSLLH|VELNED|___________________NAVSOL|POSLLH|VELNED|_____________ |
289 | // | 8ms | 8ms | 184 ms | | | |
288 | // | 8ms | 8ms | 184 ms | | | |
290 | // msg_count: 0 1 2 0 1 2 |
289 | // msg_count: 0 1 2 0 1 2 |
291 | 290 | ||
292 | if(CheckDelay(Msg_Count_Timeout)) Msg_Count = 0; |
291 | if(CheckDelay(Msg_Count_Timeout)) Msg_Count = 0; |
293 | else Msg_Count++; |
292 | else Msg_Count++; |
294 | Msg_Count_Timeout = SetDelay(100); // reset ubx msg timeout |
293 | Msg_Count_Timeout = SetDelay(100); // reset ubx msg timeout |
295 | 294 | ||
296 | // if a new set of ubx messages was collected |
295 | // if a new set of ubx messages was collected |
297 | if((Msg_Count >= 2)) |
296 | if((Msg_Count >= 2)) |
298 | { // if set is complete |
297 | { // if set is complete |
299 | if((UbxSol.Status == NEWDATA) && (UbxPosLlh.Status == NEWDATA) && (UbxVelNed.Status == NEWDATA)) |
298 | if((UbxSol.Status == NEWDATA) && (UbxPosLlh.Status == NEWDATA) && (UbxVelNed.Status == NEWDATA)) |
300 | { |
299 | { |
301 | UBX_Timeout = SetDelay(UBX_TIMEOUT); |
300 | UBX_Timeout = SetDelay(UBX_TIMEOUT); |
302 | DebugOut.Analog[9]++; |
301 | DebugOut.Analog[9]++; |
303 | // update GPS data only if the status is INVALID or PROCESSED and the last ubx message was received within less than 100 ms |
302 | // update GPS data only if the status is INVALID or PROCESSED and the last ubx message was received within less than 100 ms |
304 | if(GPSData.Status != NEWDATA) // if last data were processed |
303 | if(GPSData.Status != NEWDATA) // if last data were processed |
305 | { // wait for new data at all neccesary ubx messages |
304 | { // wait for new data at all neccesary ubx messages |
306 | GPSData.Status = INVALID; |
305 | GPSData.Status = INVALID; |
307 | // NAV SOL |
306 | // NAV SOL |
308 | GPSData.Flags = UbxSol.Flags; |
307 | GPSData.Flags = UbxSol.Flags; |
309 | GPSData.NumOfSats = UbxSol.numSV; |
308 | GPSData.NumOfSats = UbxSol.numSV; |
310 | GPSData.SatFix = UbxSol.GPSfix; |
309 | GPSData.SatFix = UbxSol.GPSfix; |
311 | GPSData.Position_Accuracy = UbxSol.PAcc; |
310 | GPSData.Position_Accuracy = UbxSol.PAcc; |
312 | GPSData.Speed_Accuracy = UbxSol.SAcc; |
311 | GPSData.Speed_Accuracy = UbxSol.SAcc; |
313 | SetGPSTime(&SystemTime); // update system time |
312 | SetGPSTime(&SystemTime); // update system time |
314 | // NAV POSLLH |
313 | // NAV POSLLH |
315 | GPSData.Position.Status = INVALID; |
314 | GPSData.Position.Status = INVALID; |
316 | GPSData.Position.Longitude = UbxPosLlh.LON; |
315 | GPSData.Position.Longitude = UbxPosLlh.LON; |
317 | GPSData.Position.Latitude = UbxPosLlh.LAT; |
316 | GPSData.Position.Latitude = UbxPosLlh.LAT; |
318 | GPSData.Position.Altitude = UbxPosLlh.HMSL; |
317 | GPSData.Position.Altitude = UbxPosLlh.HMSL; |
319 | GPSData.Position.Status = NEWDATA; |
318 | GPSData.Position.Status = NEWDATA; |
320 | // NAV VELNED |
319 | // NAV VELNED |
321 | GPSData.Speed_East = UbxVelNed.VEL_E; |
320 | GPSData.Speed_East = UbxVelNed.VEL_E; |
322 | GPSData.Speed_North = UbxVelNed.VEL_N; |
321 | GPSData.Speed_North = UbxVelNed.VEL_N; |
323 | GPSData.Speed_Top = -UbxVelNed.VEL_D; |
322 | GPSData.Speed_Top = -UbxVelNed.VEL_D; |
324 | GPSData.Speed_Ground = UbxVelNed.GSpeed; |
323 | GPSData.Speed_Ground = UbxVelNed.GSpeed; |
325 | GPSData.Heading = UbxVelNed.Heading; |
324 | GPSData.Heading = UbxVelNed.Heading; |
326 | 325 | ||
327 | GPSData.Status = NEWDATA; // new data available |
326 | GPSData.Status = NEWDATA; // new data available |
328 | } // EOF if(GPSData.Status != NEWDATA) |
327 | } // EOF if(GPSData.Status != NEWDATA) |
329 | } // EOF all ubx messages received |
328 | } // EOF all ubx messages received |
330 | // set state to collect new data |
329 | // set state to collect new data |
331 | UbxSol.Status = PROCESSED; // ready for new data |
330 | UbxSol.Status = PROCESSED; // ready for new data |
332 | UbxPosLlh.Status = PROCESSED; // ready for new data |
331 | UbxPosLlh.Status = PROCESSED; // ready for new data |
333 | UbxVelNed.Status = PROCESSED; // ready for new data |
332 | UbxVelNed.Status = PROCESSED; // ready for new data |
334 | } |
333 | } |
335 | } |
334 | } |
336 | 335 | ||
337 | 336 | ||
338 | /********************************************************/ |
337 | /********************************************************/ |
339 | /* UBX Parser */ |
338 | /* UBX Parser */ |
340 | /********************************************************/ |
339 | /********************************************************/ |
341 | void UBX_Parser(u8 c) |
340 | void UBX_Parser(u8 c) |
342 | { |
341 | { |
343 | static ubxState_t ubxState = UBXSTATE_IDLE; |
342 | static ubxState_t ubxState = UBXSTATE_IDLE; |
344 | static u16 msglen; |
343 | static u16 msglen; |
345 | static u8 cka, ckb; |
344 | static u8 cka, ckb; |
346 | static u8 *ubxP, *ubxEp, *ubxSp; // pointers to data currently transfered |
345 | static u8 *ubxP, *ubxEp, *ubxSp; // pointers to data currently transfered |
347 | 346 | ||
348 | 347 | ||
349 | //state machine |
348 | //state machine |
350 | switch (ubxState) // ubx message parser |
349 | switch (ubxState) // ubx message parser |
351 | { |
350 | { |
352 | case UBXSTATE_IDLE: // check 1st sync byte |
351 | case UBXSTATE_IDLE: // check 1st sync byte |
353 | if (c == UBX_SYNC1_CHAR) ubxState = UBXSTATE_SYNC1; |
352 | if (c == UBX_SYNC1_CHAR) ubxState = UBXSTATE_SYNC1; |
354 | else ubxState = UBXSTATE_IDLE; // out of synchronization |
353 | else ubxState = UBXSTATE_IDLE; // out of synchronization |
355 | break; |
354 | break; |
356 | 355 | ||
357 | case UBXSTATE_SYNC1: // check 2nd sync byte |
356 | case UBXSTATE_SYNC1: // check 2nd sync byte |
358 | if (c == UBX_SYNC2_CHAR) ubxState = UBXSTATE_SYNC2; |
357 | if (c == UBX_SYNC2_CHAR) ubxState = UBXSTATE_SYNC2; |
359 | else ubxState = UBXSTATE_IDLE; // out of synchronization |
358 | else ubxState = UBXSTATE_IDLE; // out of synchronization |
360 | break; |
359 | break; |
361 | 360 | ||
362 | case UBXSTATE_SYNC2: // check msg class to be NAV |
361 | case UBXSTATE_SYNC2: // check msg class to be NAV |
363 | if (c == UBX_CLASS_NAV) ubxState = UBXSTATE_CLASS; |
362 | if (c == UBX_CLASS_NAV) ubxState = UBXSTATE_CLASS; |
364 | else ubxState = UBXSTATE_IDLE; // unsupported message class |
363 | else ubxState = UBXSTATE_IDLE; // unsupported message class |
365 | break; |
364 | break; |
366 | 365 | ||
367 | case UBXSTATE_CLASS: // check message identifier |
366 | case UBXSTATE_CLASS: // check message identifier |
368 | switch(c) |
367 | switch(c) |
369 | { |
368 | { |
370 | case UBX_ID_POSLLH: // geodetic position |
369 | case UBX_ID_POSLLH: // geodetic position |
371 | ubxP = (u8 *)&UbxPosLlh; // data start pointer |
370 | ubxP = (u8 *)&UbxPosLlh; // data start pointer |
372 | ubxEp = (u8 *)(&UbxPosLlh + 1); // data end pointer |
371 | ubxEp = (u8 *)(&UbxPosLlh + 1); // data end pointer |
373 | ubxSp = (u8 *)&UbxPosLlh.Status; // status pointer |
372 | ubxSp = (u8 *)&UbxPosLlh.Status; // status pointer |
374 | break; |
373 | break; |
375 | 374 | ||
376 | case UBX_ID_SOL: // navigation solution |
375 | case UBX_ID_SOL: // navigation solution |
377 | ubxP = (u8 *)&UbxSol; // data start pointer |
376 | ubxP = (u8 *)&UbxSol; // data start pointer |
378 | ubxEp = (u8 *)(&UbxSol + 1); // data end pointer |
377 | ubxEp = (u8 *)(&UbxSol + 1); // data end pointer |
379 | ubxSp = (u8 *)&UbxSol.Status; // status pointer |
378 | ubxSp = (u8 *)&UbxSol.Status; // status pointer |
380 | break; |
379 | break; |
381 | 380 | ||
382 | case UBX_ID_VELNED: // velocity vector in tangent plane |
381 | case UBX_ID_VELNED: // velocity vector in tangent plane |
383 | ubxP = (u8 *)&UbxVelNed; // data start pointer |
382 | ubxP = (u8 *)&UbxVelNed; // data start pointer |
384 | ubxEp = (u8 *)(&UbxVelNed + 1); // data end pointer |
383 | ubxEp = (u8 *)(&UbxVelNed + 1); // data end pointer |
385 | ubxSp = (u8 *)&UbxVelNed.Status; // status pointer |
384 | ubxSp = (u8 *)&UbxVelNed.Status; // status pointer |
386 | break; |
385 | break; |
387 | 386 | ||
388 | default: // unsupported identifier |
387 | default: // unsupported identifier |
389 | ubxState = UBXSTATE_IDLE; |
388 | ubxState = UBXSTATE_IDLE; |
390 | break; |
389 | break; |
391 | } |
390 | } |
392 | if (ubxState != UBXSTATE_IDLE) |
391 | if (ubxState != UBXSTATE_IDLE) |
393 | { |
392 | { |
394 | ubxState = UBXSTATE_LEN1; |
393 | ubxState = UBXSTATE_LEN1; |
395 | cka = UBX_CLASS_NAV + c; |
394 | cka = UBX_CLASS_NAV + c; |
396 | ckb = UBX_CLASS_NAV + cka; |
395 | ckb = UBX_CLASS_NAV + cka; |
397 | } |
396 | } |
398 | break; |
397 | break; |
399 | 398 | ||
400 | case UBXSTATE_LEN1: // 1st message length byte |
399 | case UBXSTATE_LEN1: // 1st message length byte |
401 | msglen = (u16)c; // lowbyte first |
400 | msglen = (u16)c; // lowbyte first |
402 | cka += c; |
401 | cka += c; |
403 | ckb += cka; |
402 | ckb += cka; |
404 | ubxState = UBXSTATE_LEN2; |
403 | ubxState = UBXSTATE_LEN2; |
405 | break; |
404 | break; |
406 | 405 | ||
407 | case UBXSTATE_LEN2: // 2nd message length byte |
406 | case UBXSTATE_LEN2: // 2nd message length byte |
408 | msglen += ((u16)c)<<8; // high byte last |
407 | msglen += ((u16)c)<<8; // high byte last |
409 | cka += c; |
408 | cka += c; |
410 | ckb += cka; |
409 | ckb += cka; |
411 | // if the old data are not processed so far then break parsing now |
410 | // if the old data are not processed so far then break parsing now |
412 | // to avoid writing new data in ISR during reading by another function |
411 | // to avoid writing new data in ISR during reading by another function |
413 | if ( *ubxSp == NEWDATA ) |
412 | if ( *ubxSp == NEWDATA ) |
414 | { |
413 | { |
415 | ubxState = UBXSTATE_IDLE; |
414 | ubxState = UBXSTATE_IDLE; |
416 | Update_GPSData(); //update GPS info respectively |
415 | Update_GPSData(); //update GPS info respectively |
417 | } |
416 | } |
418 | else // data invalid or allready processd |
417 | else // data invalid or allready processd |
419 | { |
418 | { |
420 | *ubxSp = INVALID; // mark invalid during buffer filling |
419 | *ubxSp = INVALID; // mark invalid during buffer filling |
421 | ubxState = UBXSTATE_DATA; |
420 | ubxState = UBXSTATE_DATA; |
422 | } |
421 | } |
423 | break; |
422 | break; |
424 | 423 | ||
425 | case UBXSTATE_DATA: // collecting data |
424 | case UBXSTATE_DATA: // collecting data |
426 | if (ubxP < ubxEp) |
425 | if (ubxP < ubxEp) |
427 | { |
426 | { |
428 | *ubxP++ = c; // copy curent data byte if any space is left |
427 | *ubxP++ = c; // copy curent data byte if any space is left |
429 | cka += c; |
428 | cka += c; |
430 | ckb += cka; |
429 | ckb += cka; |
431 | if (--msglen == 0) ubxState = UBXSTATE_CKA; // switch to next state if all data was read |
430 | if (--msglen == 0) ubxState = UBXSTATE_CKA; // switch to next state if all data was read |
432 | } |
431 | } |
433 | else // rx buffer overrun |
432 | else // rx buffer overrun |
434 | { |
433 | { |
435 | ubxState = UBXSTATE_IDLE; |
434 | ubxState = UBXSTATE_IDLE; |
436 | } |
435 | } |
437 | break; |
436 | break; |
438 | 437 | ||
439 | case UBXSTATE_CKA: |
438 | case UBXSTATE_CKA: |
440 | if (c == cka) ubxState = UBXSTATE_CKB; |
439 | if (c == cka) ubxState = UBXSTATE_CKB; |
441 | else |
440 | else |
442 | { |
441 | { |
443 | *ubxSp = INVALID; |
442 | *ubxSp = INVALID; |
444 | ubxState = UBXSTATE_IDLE; |
443 | ubxState = UBXSTATE_IDLE; |
445 | } |
444 | } |
446 | break; |
445 | break; |
447 | 446 | ||
448 | case UBXSTATE_CKB: |
447 | case UBXSTATE_CKB: |
449 | if (c == ckb) |
448 | if (c == ckb) |
450 | { |
449 | { |
451 | *ubxSp = NEWDATA; // new data are valid |
450 | *ubxSp = NEWDATA; // new data are valid |
452 | Update_GPSData(); //update GPS info respectively |
451 | Update_GPSData(); //update GPS info respectively |
453 | } |
452 | } |
454 | else |
453 | else |
455 | { // if checksum not match then set data invalid |
454 | { // if checksum not match then set data invalid |
456 | *ubxSp = INVALID; |
455 | *ubxSp = INVALID; |
457 | } |
456 | } |
458 | ubxState = UBXSTATE_IDLE; // ready to parse new data |
457 | ubxState = UBXSTATE_IDLE; // ready to parse new data |
459 | break; |
458 | break; |
460 | 459 | ||
461 | default: // unknown ubx state |
460 | default: // unknown ubx state |
462 | ubxState = UBXSTATE_IDLE; |
461 | ubxState = UBXSTATE_IDLE; |
463 | break; |
462 | break; |
464 | 463 | ||
465 | } |
464 | } |
466 | } |
465 | } |
467 | 466 |