Subversion Repositories NaviCtrl

Rev

Rev 645 | Rev 700 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 645 Rev 659
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + www.MikroKopter.com
5
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
// + Software Nutzungsbedingungen (english version: see below)
7
// + Software Nutzungsbedingungen (english version: see below)
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand
22
// + des Mitverschuldens offen.
22
// + des Mitverschuldens offen.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Software LICENSING TERMS
32
// + Software LICENSING TERMS
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
37
// + The Software may only be used with the Licensor's products.
37
// + The Software may only be used with the Licensor's products.
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// + agreement shall be the property of the Licensor.
40
// + agreement shall be the property of the Licensor.
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
43
// + The customer shall be responsible for taking reasonable precautions
43
// + The customer shall be responsible for taking reasonable precautions
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// + #### END OF LICENSING TERMS ####
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
#include <string.h>
56
#include <string.h>
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 "main.h"
60
#include "main.h"
61
#include "led.h"
61
#include "led.h"
62
#include "timer1.h"
62
#include "timer1.h"
63
#include "GPS.h"
63
#include "GPS.h"
64
 
64
 
65
// ------------------------------------------------------------------------------------------------
65
// ------------------------------------------------------------------------------------------------
66
// defines
66
// defines
67
 
67
 
68
#define DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980 722819 // the year 0 does not exist!
68
#define DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980 722819 // the year 0 does not exist!
69
#define DAYS_PER_YEAR           365
69
#define DAYS_PER_YEAR           365
70
#define DAYS_PER_LEAPYEAR       366
70
#define DAYS_PER_LEAPYEAR       366
71
#define DAYS_PER_4YEARS         1461    //((3 * DAYS_PER_YEAR) + DAYS_PER_LEAPYEAR) // years dividable by 4 are leap years
71
#define DAYS_PER_4YEARS         1461    //((3 * DAYS_PER_YEAR) + DAYS_PER_LEAPYEAR) // years dividable by 4 are leap years
72
#define DAYS_PER_100YEARS       36524   //((25 * DAYS_PER_4YEARS) - 1) // years dividable by 100 are no leap years
72
#define DAYS_PER_100YEARS       36524   //((25 * DAYS_PER_4YEARS) - 1) // years dividable by 100 are no leap years
73
#define DAYS_PER_400YEARS       146097  //((4 * DAYS_PER_100YEARS) + 1L) // but years dividable by 400 are leap years
73
#define DAYS_PER_400YEARS       146097  //((4 * DAYS_PER_100YEARS) + 1L) // but years dividable by 400 are leap years
74
#define SECONDS_PER_MINUTE      60
74
#define SECONDS_PER_MINUTE      60
75
#define MINUTES_PER_HOUR        60
75
#define MINUTES_PER_HOUR        60
76
#define HOURS_PER_DAY           24
76
#define HOURS_PER_DAY           24
77
#define DAYS_PER_WEEK           7
77
#define DAYS_PER_WEEK           7
78
#define SECONDS_PER_HOUR        3600    //(SECONDS_PER_MINUTE * MINUTES_PER_HOUR)
78
#define SECONDS_PER_HOUR        3600    //(SECONDS_PER_MINUTE * MINUTES_PER_HOUR)
79
#define SECONDS_PER_DAY         86400   //(SECONDS_PER_HOUR * HOURS_PER_DAY)
79
#define SECONDS_PER_DAY         86400   //(SECONDS_PER_HOUR * HOURS_PER_DAY)
80
#define SECONDS_PER_WEEK        604800  //(SECONDS_PER_DAY * DAYS_PER_WEEK)
80
#define SECONDS_PER_WEEK        604800  //(SECONDS_PER_DAY * DAYS_PER_WEEK)
81
 
81
 
82
// days per month in normal and leap years
82
// days per month in normal and leap years
83
const u32       Leap[ 13 ]      = { 0,  31,  60,  91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
83
const u32       Leap[ 13 ]      = { 0,  31,  60,  91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
84
const u32 Normal[ 13 ]  = { 0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
84
const u32 Normal[ 13 ]  = { 0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
85
 
85
 
86
#define LEAP_SECONDS_FROM_1980  16
86
#define LEAP_SECONDS_FROM_1980  16
87
 
87
 
88
// message sync bytes
88
// message sync bytes
89
#define UBX_SYNC1_CHAR  0xB5
89
#define UBX_SYNC1_CHAR  0xB5
90
#define UBX_SYNC2_CHAR  0x62
90
#define UBX_SYNC2_CHAR  0x62
91
// protocoll identifiers
91
// protocoll identifiers
92
// navigation class
92
// navigation class
93
#define UBX_CLASS_NAV   0x01
93
#define UBX_CLASS_NAV   0x01
94
#define UBX_ID_POSLLH   0x02
94
#define UBX_ID_POSLLH   0x02
95
#define UBX_ID_SOL              0x06
95
#define UBX_ID_SOL              0x06
96
#define UBX_ID_VELNED   0x12
96
#define UBX_ID_VELNED   0x12
97
 
97
 
98
// ------------------------------------------------------------------------------------------------
98
// ------------------------------------------------------------------------------------------------
99
// typedefs
99
// typedefs
100
 
100
 
101
 
101
 
102
// ubx parser state
102
// ubx parser state
103
typedef enum
103
typedef enum
104
{
104
{
105
        UBXSTATE_IDLE,
105
        UBXSTATE_IDLE,
106
        UBXSTATE_SYNC1,
106
        UBXSTATE_SYNC1,
107
        UBXSTATE_SYNC2,
107
        UBXSTATE_SYNC2,
108
        UBXSTATE_CLASS,
108
        UBXSTATE_CLASS,
109
        UBXSTATE_LEN1,
109
        UBXSTATE_LEN1,
110
        UBXSTATE_LEN2,
110
        UBXSTATE_LEN2,
111
        UBXSTATE_DATA,
111
        UBXSTATE_DATA,
112
        UBXSTATE_CKA,
112
        UBXSTATE_CKA,
113
        UBXSTATE_CKB
113
        UBXSTATE_CKB
114
} ubxState_t;
114
} ubxState_t;
115
 
115
 
116
typedef struct
116
typedef struct
117
{
117
{
118
        u32                     itow;           // ms GPS Millisecond Time of Week
118
        u32                     itow;           // ms GPS Millisecond Time of Week
119
        s32                     frac;           // ns remainder of rounded ms above
119
        s32                     frac;           // ns remainder of rounded ms above
120
        s16                     week;           // GPS week
120
        s16                     week;           // GPS week
121
        u8                      GPSfix;         // GPSfix Type, range 0..6
121
        u8                      GPSfix;         // GPSfix Type, range 0..6
122
        u8                      Flags;          // Navigation Status Flags
122
        u8                      Flags;          // Navigation Status Flags
123
        s32                     ECEF_X;         // cm ECEF X coordinate
123
        s32                     ECEF_X;         // cm ECEF X coordinate
124
        s32                     ECEF_Y;         // cm ECEF Y coordinate
124
        s32                     ECEF_Y;         // cm ECEF Y coordinate
125
        s32                     ECEF_Z;         // cm ECEF Z coordinate
125
        s32                     ECEF_Z;         // cm ECEF Z coordinate
126
        u32                     PAcc;           // cm 3D Position Accuracy Estimate
126
        u32                     PAcc;           // cm 3D Position Accuracy Estimate
127
        s32                     ECEFVX;         // cm/s ECEF X velocity
127
        s32                     ECEFVX;         // cm/s ECEF X velocity
128
        s32                     ECEFVY;         // cm/s ECEF Y velocity
128
        s32                     ECEFVY;         // cm/s ECEF Y velocity
129
        s32                     ECEFVZ;         // cm/s ECEF Z velocity
129
        s32                     ECEFVZ;         // cm/s ECEF Z velocity
130
        u32                     SAcc;           // cm/s Speed Accuracy Estimate
130
        u32                     SAcc;           // cm/s Speed Accuracy Estimate
131
        u16                     PDOP;           // 0.01 Position DOP
131
        u16                     PDOP;           // 0.01 Position DOP
132
        u8                      res1;           // reserved
132
        u8                      res1;           // reserved
133
        u8                      numSV;          // Number of SVs used in navigation solution
133
        u8                      numSV;          // Number of SVs used in navigation solution
134
        u32                     res2;           // reserved
134
        u32                     res2;           // reserved
135
        u8                      Status;     // invalid/newdata/processed
135
        u8                      Status;     // invalid/newdata/processed
136
} __attribute__((packed)) ubx_nav_sol_t;
136
} __attribute__((packed)) ubx_nav_sol_t;
137
 
137
 
138
 
138
 
139
typedef struct
139
typedef struct
140
{
140
{
141
        u32                     itow;           // ms  GPS Millisecond Time of Week
141
        u32                     itow;           // ms  GPS Millisecond Time of Week
142
        s32                     VEL_N;          // cm/s  NED north velocity
142
        s32                     VEL_N;          // cm/s  NED north velocity
143
        s32                     VEL_E;          // cm/s  NED east velocity
143
        s32                     VEL_E;          // cm/s  NED east velocity
144
        s32                     VEL_D;          // cm/s  NED down velocity
144
        s32                     VEL_D;          // cm/s  NED down velocity
145
        u32                     Speed;          // cm/s  Speed (3-D)
145
        u32                     Speed;          // cm/s  Speed (3-D)
146
        u32                     GSpeed;         // cm/s  Ground Speed (2-D)
146
        u32                     GSpeed;         // cm/s  Ground Speed (2-D)
147
        s32                     Heading;        // 1e-05 deg  Heading 2-D
147
        s32                     Heading;        // 1e-05 deg  Heading 2-D
148
        u32                     SAcc;           // cm/s  Speed Accuracy Estimate
148
        u32                     SAcc;           // cm/s  Speed Accuracy Estimate
149
        u32                     CAcc;           // deg  Course / Heading Accuracy Estimate
149
        u32                     CAcc;           // deg  Course / Heading Accuracy Estimate
150
        u8                      Status;         // invalid/newdata/processed
150
        u8                      Status;         // invalid/newdata/processed
151
} __attribute__((packed)) ubx_nav_velned_t;
151
} __attribute__((packed)) ubx_nav_velned_t;
152
 
152
 
153
typedef struct
153
typedef struct
154
{
154
{
155
        u32                     itow;           // ms GPS Millisecond Time of Week
155
        u32                     itow;           // ms GPS Millisecond Time of Week
156
        s32                     LON;            // 1e-07 deg Longitude
156
        s32                     LON;            // 1e-07 deg Longitude
157
        s32                     LAT;            // 1e-07 deg Latitude
157
        s32                     LAT;            // 1e-07 deg Latitude
158
        s32                     HEIGHT;         // mm Height above Ellipsoid
158
        s32                     HEIGHT;         // mm Height above Ellipsoid
159
        s32                     HMSL;           // mm Height above mean sea level
159
        s32                     HMSL;           // mm Height above mean sea level
160
        u32                     Hacc;           // mm Horizontal Accuracy Estimate
160
        u32                     Hacc;           // mm Horizontal Accuracy Estimate
161
        u32                     Vacc;           // mm Vertical Accuracy Estimate
161
        u32                     Vacc;           // mm Vertical Accuracy Estimate
162
        u8                      Status;         // invalid/newdata/processed
162
        u8                      Status;         // invalid/newdata/processed
163
} __attribute__((packed)) ubx_nav_posllh_t;
163
} __attribute__((packed)) ubx_nav_posllh_t;
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
ubx_nav_sol_t           UbxSol    = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, INVALID};
169
ubx_nav_sol_t           UbxSol    = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, INVALID};
170
ubx_nav_posllh_t        UbxPosLlh = {0,0,0,0,0,0,0, INVALID};
170
ubx_nav_posllh_t        UbxPosLlh = {0,0,0,0,0,0,0, INVALID};
171
ubx_nav_velned_t        UbxVelNed = {0,0,0,0,0,0,0,0,0, INVALID};
171
ubx_nav_velned_t        UbxVelNed = {0,0,0,0,0,0,0,0,0, INVALID};
172
ubxmsg_t                        UbxMsg;
172
ubxmsg_t                        UbxMsg;
173
 
173
 
174
// shared buffer
174
// shared buffer
175
gps_data_t              GPSData = {200,{0,0,0,INVALID},0,0,0,0,0,0,0, INVALID};
175
gps_data_t              GPSData = {200,{0,0,0,INVALID},0,0,0,0,0,0,0, INVALID};
176
DateTime_t                      GPSDateTime = {0,0,0,0,0,0,0, INVALID};
176
DateTime_t                      GPSDateTime = {0,0,0,0,0,0,0, INVALID};
177
 
177
 
178
#define UBX_TIMEOUT 500 // 500 ms
178
#define UBX_TIMEOUT 500 // 500 ms
179
u32 UBX_Timeout = 0;
179
u32 UBX_Timeout = 0;
180
 
180
 
181
//------------------------------------------------------------------------------------
181
//------------------------------------------------------------------------------------
182
// functions
182
// functions
183
 
183
 
184
u8 IsLeapYear(u16 year)
184
u8 IsLeapYear(u16 year)
185
{
185
{
186
        if((year%400 == 0) || ( (year%4 == 0) && (year%100 != 0) ) ) return 1;
186
        if((year%400 == 0) || ( (year%4 == 0) && (year%100 != 0) ) ) return 1;
187
        else return 0;
187
        else return 0;
188
}
188
}
189
/********************************************************/
189
/********************************************************/
190
/*  Calculates the UTC Time from the GPS week and tow   */
190
/*  Calculates the UTC Time from the GPS week and tow   */
191
/********************************************************/
191
/********************************************************/
192
void SetGPSTime(DateTime_t * pTimeStruct)
192
void SetGPSTime(DateTime_t * pTimeStruct)
193
{
193
{
194
        u32 Days, Seconds, Week;
194
        u32 Days, Seconds, Week;
195
        u16 YearPart;
195
        u16 YearPart;
196
        u32 * MonthDayTab = 0;
196
        u32 * MonthDayTab = 0;
197
        u8 i;
197
        u8 i;
198
 
198
 
199
 
199
 
200
        // if GPS data show valid time data
200
        // if GPS data show valid time data
201
        if((UbxSol.Status != INVALID) && (UbxSol.Flags & FLAG_WKNSET) && (UbxSol.Flags & FLAG_TOWSET) )
201
        if((UbxSol.Status != INVALID) && (UbxSol.Flags & FLAG_WKNSET) && (UbxSol.Flags & FLAG_TOWSET) )
202
        {
202
        {
203
                Seconds = UbxSol.itow / 1000L;
203
                Seconds = UbxSol.itow / 1000L;
204
                Week = (u32)UbxSol.week;
204
                Week = (u32)UbxSol.week;
205
                // correct leap seconds since 1980
205
                // correct leap seconds since 1980
206
                if(Seconds < LEAP_SECONDS_FROM_1980)
206
                if(Seconds < LEAP_SECONDS_FROM_1980)
207
                {
207
                {
208
                        Week--;
208
                        Week--;
209
                        Seconds = SECONDS_PER_WEEK - LEAP_SECONDS_FROM_1980 + Seconds;
209
                        Seconds = SECONDS_PER_WEEK - LEAP_SECONDS_FROM_1980 + Seconds;
210
                }
210
                }
211
                else Seconds -= LEAP_SECONDS_FROM_1980;
211
                else Seconds -= LEAP_SECONDS_FROM_1980;
212
 
212
 
213
                Days = DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980;
213
                Days = DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980;
214
                Days += (Week * DAYS_PER_WEEK);
214
                Days += (Week * DAYS_PER_WEEK);
215
                Days += Seconds / SECONDS_PER_DAY; // seperate days from GPS seconds of week
215
                Days += Seconds / SECONDS_PER_DAY; // seperate days from GPS seconds of week
216
 
216
 
217
                pTimeStruct->Year = 1;
217
                pTimeStruct->Year = 1;
218
                YearPart = (u16)(Days / DAYS_PER_400YEARS);
218
                YearPart = (u16)(Days / DAYS_PER_400YEARS);
219
                pTimeStruct->Year += YearPart * 400;
219
                pTimeStruct->Year += YearPart * 400;
220
                Days = Days % DAYS_PER_400YEARS;
220
                Days = Days % DAYS_PER_400YEARS;
221
                YearPart = (u16)(Days / DAYS_PER_100YEARS);
221
                YearPart = (u16)(Days / DAYS_PER_100YEARS);
222
                pTimeStruct->Year += YearPart * 100;
222
                pTimeStruct->Year += YearPart * 100;
223
                Days = Days % DAYS_PER_100YEARS;
223
                Days = Days % DAYS_PER_100YEARS;
224
                YearPart = (u16)(Days / DAYS_PER_4YEARS);
224
                YearPart = (u16)(Days / DAYS_PER_4YEARS);
225
                pTimeStruct->Year += YearPart * 4;
225
                pTimeStruct->Year += YearPart * 4;
226
                Days = Days % DAYS_PER_4YEARS;
226
                Days = Days % DAYS_PER_4YEARS;
227
                if(Days < (3* DAYS_PER_YEAR)) YearPart = (u16)(Days / DAYS_PER_YEAR);
227
                if(Days < (3* DAYS_PER_YEAR)) YearPart = (u16)(Days / DAYS_PER_YEAR);
228
                else YearPart = 3;
228
                else YearPart = 3;
229
                pTimeStruct->Year += YearPart;
229
                pTimeStruct->Year += YearPart;
230
                // calculate remaining days of year
230
                // calculate remaining days of year
231
                Days -= (u32)(YearPart *  DAYS_PER_YEAR);
231
                Days -= (u32)(YearPart *  DAYS_PER_YEAR);
232
                Days += 1;
232
                Days += 1;
233
                // check if current year is a leap year
233
                // check if current year is a leap year
234
                if(IsLeapYear(pTimeStruct->Year)) MonthDayTab = (u32*)Leap;
234
                if(IsLeapYear(pTimeStruct->Year)) MonthDayTab = (u32*)Leap;
235
                else MonthDayTab = (u32*)Normal;
235
                else MonthDayTab = (u32*)Normal;
236
                // seperate month and day from days of year
236
                // seperate month and day from days of year
237
                for ( i = 0; i < 12; i++ )
237
                for ( i = 0; i < 12; i++ )
238
                {
238
                {
239
                        if ( (MonthDayTab[i]< Days) && (Days <= MonthDayTab[i+1]) )
239
                        if ( (MonthDayTab[i]< Days) && (Days <= MonthDayTab[i+1]) )
240
                        {
240
                        {
241
                                pTimeStruct->Month = i+1;
241
                                pTimeStruct->Month = i+1;
242
                                pTimeStruct->Day = Days - MonthDayTab[i];
242
                                pTimeStruct->Day = Days - MonthDayTab[i];
243
                                i = 12;
243
                                i = 12;
244
                        }
244
                        }
245
                }
245
                }
246
                Seconds = Seconds % SECONDS_PER_DAY; // remaining seconds of current day
246
                Seconds = Seconds % SECONDS_PER_DAY; // remaining seconds of current day
247
                pTimeStruct->Hour = (u8)(Seconds / SECONDS_PER_HOUR);
247
                pTimeStruct->Hour = (u8)(Seconds / SECONDS_PER_HOUR);
248
                Seconds = Seconds % SECONDS_PER_HOUR; // remaining seconds of current hour
248
                Seconds = Seconds % SECONDS_PER_HOUR; // remaining seconds of current hour
249
                pTimeStruct->Min = (u8)(Seconds / SECONDS_PER_MINUTE);
249
                pTimeStruct->Min = (u8)(Seconds / SECONDS_PER_MINUTE);
250
                Seconds = Seconds % SECONDS_PER_MINUTE; // remaining seconds of current minute
250
                Seconds = Seconds % SECONDS_PER_MINUTE; // remaining seconds of current minute
251
                pTimeStruct->Sec = (u8)(Seconds);
251
                pTimeStruct->Sec = (u8)(Seconds);
252
                pTimeStruct->mSec  = (u16)(UbxSol.itow % 1000L);
252
                pTimeStruct->mSec  = (u16)(UbxSol.itow % 1000L);
253
                pTimeStruct->Valid = 1;
253
                pTimeStruct->Valid = 1;
254
        }
254
        }
255
        else
255
        else
256
        {
256
        {
257
                pTimeStruct->Valid = 0;
257
                pTimeStruct->Valid = 0;
258
        }
258
        }
259
}
259
}
260
 
260
 
261
 
261
 
262
 
262
 
263
/********************************************************/
263
/********************************************************/
264
/*                  Initialize UBX Parser               */
264
/*                  Initialize UBX Parser               */
265
/********************************************************/
265
/********************************************************/
266
void UBX_Init(void)
266
void UBX_Init(void)
267
{
267
{
268
        // mark msg buffers invalid
268
        // mark msg buffers invalid
269
        UbxSol.Status = INVALID;
269
        UbxSol.Status = INVALID;
270
        UbxPosLlh.Status = INVALID;
270
        UbxPosLlh.Status = INVALID;
271
        UbxVelNed.Status = INVALID;
271
        UbxVelNed.Status = INVALID;
272
        UbxMsg.Status = INVALID;
272
        UbxMsg.Status = INVALID;
273
        GPSData.Status = INVALID;
273
        GPSData.Status = INVALID;
274
 
274
 
275
        UBX_Timeout = SetDelay(2 * UBX_Timeout);
275
        UBX_Timeout = SetDelay(2 * UBX_Timeout);
276
}
276
}
277
 
277
 
278
/********************************************************/
278
/********************************************************/
279
/*            Upate GPS data stcructure                 */
279
/*            Upate GPS data stcructure                 */
280
/********************************************************/
280
/********************************************************/
281
void Update_GPSData(void)
281
void Update_GPSData(void)
282
{
282
{
283
        static u32 last_itow = 0;
283
        static u32 last_itow = 0;
284
        static u32 milliseconds = 0;
284
        static u32 milliseconds = 0;
285
 
285
 
286
        // if a new set of ubx messages was collected
286
        // if a new set of ubx messages was collected
287
        if((UbxSol.Status == NEWDATA) && (UbxPosLlh.Status == NEWDATA) && (UbxVelNed.Status == NEWDATA))
287
        if((UbxSol.Status == NEWDATA) && (UbxPosLlh.Status == NEWDATA) && (UbxVelNed.Status == NEWDATA))
288
        {       // and the itow is equal (same time base)
288
        {       // and the itow is equal (same time base)
289
                UART_VersionInfo.Flags |= NC_VERSION_FLAG_GPS_PRESENT;
289
                UART_VersionInfo.Flags |= NC_VERSION_FLAG_GPS_PRESENT;
290
                if((UbxSol.itow == UbxPosLlh.itow) && (UbxPosLlh.itow == UbxVelNed.itow))
290
                if((UbxSol.itow == UbxPosLlh.itow) && (UbxPosLlh.itow == UbxVelNed.itow))
291
                {
291
                {
292
                        UBX_Timeout = SetDelay(UBX_TIMEOUT);
292
                        UBX_Timeout = SetDelay(UBX_TIMEOUT);
293
                        DebugOut.Analog[9]++;
293
                        DebugOut.Analog[9]++;
294
                        // update GPS data only if the status is INVALID or PROCESSED
294
                        // update GPS data only if the status is INVALID or PROCESSED
295
                        if(GPSData.Status != NEWDATA)
295
                        if(GPSData.Status != NEWDATA)
296
                        { // wait for new data at all neccesary ubx messages
296
                        { // wait for new data at all neccesary ubx messages
297
CountNewGpsDataIn5Sec++;
297
                                CountNewGpsDataIn5Sec++;
298
                                GPSData.Status = INVALID;
298
                                GPSData.Status = INVALID;
299
                                // update message cycle time
299
                                // update message cycle time
300
                                GPSData.MsgCycleTime = (u16)(UbxSol.itow-last_itow);
300
                                GPSData.MsgCycleTime = (u16)(UbxSol.itow-last_itow);
301
                                milliseconds += GPSData.MsgCycleTime;
301
                                milliseconds += GPSData.MsgCycleTime;
302
                                last_itow = UbxSol.itow; // update last itow
302
                                last_itow = UbxSol.itow; // update last itow
303
                                // NAV SOL
303
                                // NAV SOL
304
                                GPSData.Flags =         (GPSData.Flags & 0xf0) | (UbxSol.Flags & 0x0f); // we take only the lower bits
304
                                GPSData.Flags =         (GPSData.Flags & 0xf0) | (UbxSol.Flags & 0x0f); // we take only the lower bits
305
                                GPSData.NumOfSats =                     UbxSol.numSV;
305
                                GPSData.NumOfSats =                     UbxSol.numSV;
306
                                GPSData.SatFix =                                UbxSol.GPSfix;
306
                                GPSData.SatFix =                                UbxSol.GPSfix;
307
//if(Parameter.User3 > 100) { GPSData.NumOfSats = 0; GPSData.SatFix = 0;}
307
//if(Parameter.User3 > 100) { GPSData.NumOfSats = 0; GPSData.SatFix = 0;}
308
//if(Parameter.User3 > 100) { GPSData.SatFix = 0;}
308
//if(Parameter.User3 > 100) { GPSData.SatFix = 0;}
309
                                GPSData.Position_Accuracy =             UbxSol.PAcc;    // in steps of 1cm
309
                                GPSData.Position_Accuracy =             UbxSol.PAcc;    // in steps of 1cm
310
                                GPSData.Speed_Accuracy =                UbxSol.SAcc;    // in steps of 1cm/s
310
                                GPSData.Speed_Accuracy =                UbxSol.SAcc;    // in steps of 1cm/s
311
                                SetGPSTime(&SystemTime); // update system time
311
                                SetGPSTime(&SystemTime); // update system time
312
                                // NAV VELNED
312
                                // NAV VELNED
313
                                GPSData.Speed_East =                    UbxVelNed.VEL_E;        // in steps of 1cm/s
313
                                GPSData.Speed_East =                    UbxVelNed.VEL_E;        // in steps of 1cm/s
314
                                GPSData.Speed_North =                   UbxVelNed.VEL_N;    // in steps of 1cm/s
314
                                GPSData.Speed_North =                   UbxVelNed.VEL_N;    // in steps of 1cm/s
315
                                GPSData.Speed_Top       =                       -UbxVelNed.VEL_D;   // in steps of 1cm/s
315
                                GPSData.Speed_Top       =                       -UbxVelNed.VEL_D;   // in steps of 1cm/s
316
                                GPSData.Speed_Ground =                  UbxVelNed.GSpeed;       // in steps of 1cm/s
316
                                GPSData.Speed_Ground =                  UbxVelNed.GSpeed;       // in steps of 1cm/s
317
                                GPSData.Heading =                               UbxVelNed.Heading; //in steps of 1E-5 deg
317
                                GPSData.Heading =                               UbxVelNed.Heading; //in steps of 1E-5 deg
318
                                // NAV POSLLH
318
                                // NAV POSLLH
319
                                GPSData.Position.Status =               INVALID;
319
                                GPSData.Position.Status =               INVALID;
320
                                if(!(SimulationFlags & SIMULATION_ACTIVE))
320
                                if(!(SimulationFlags & SIMULATION_ACTIVE))
321
                                {
321
                                {
322
                                        GPSData.Position.Longitude =    UbxPosLlh.LON;  // in steps of 1E-7 deg
322
                                        GPSData.Position.Longitude =    UbxPosLlh.LON;  // in steps of 1E-7 deg
323
                                        GPSData.Position.Latitude =     UbxPosLlh.LAT;  // in steps of 1E-7 deg
323
                                        GPSData.Position.Latitude =     UbxPosLlh.LAT;  // in steps of 1E-7 deg
324
                                        GPSData.Position.Altitude =     UbxPosLlh.HMSL; // in steps of 1 mm
324
                                        GPSData.Position.Altitude =     UbxPosLlh.HMSL; // in steps of 1 mm
325
                                }
325
                                }
326
                                else // simulation active
326
                                else // simulation active
327
                                {
327
                                {
328
                                 if(GPSData.SatFix != SATFIX_3D || GPSData.NumOfSats < 6)  // simulate satfix
328
                                 if(GPSData.SatFix != SATFIX_3D || GPSData.NumOfSats < 6)  // simulate satfix
329
                                 {
329
                                 {
330
                                   GPSData.SatFix = SATFIX_3D;          // Simulation
330
                                   GPSData.SatFix = SATFIX_3D;          // Simulation
331
                                   GPSData.Flags |= FLAG_GPSFIXOK;      // Simulation
331
                                   GPSData.Flags |= FLAG_GPSFIXOK;      // Simulation
332
                                   GPSData.NumOfSats = 8;                       // Simulation
332
                                   GPSData.NumOfSats = 8;                       // Simulation
333
                                   if(!SystemTime.Valid)
333
                                   if(!SystemTime.Valid)
334
                                   {
334
                                   {
335
                                        UbxSol.Status = 1;
335
                                        UbxSol.Status = 1;
336
                                        UbxSol.Flags |= FLAG_WKNSET;
336
                                        UbxSol.Flags |= FLAG_WKNSET;
337
                                        UbxSol.Flags |= FLAG_TOWSET;
337
                                        UbxSol.Flags |= FLAG_TOWSET;
338
                                        UbxSol.week = 1043;      // starts in year 2000
338
                                        UbxSol.week = 1043;      // starts in year 2000
339
                                        UbxSol.itow = milliseconds;
339
                                        UbxSol.itow = milliseconds;
340
                                        SetGPSTime(&SystemTime); // update system time
340
                                        SetGPSTime(&SystemTime); // update system time
341
                                        SystemTime.Valid = 1; // use the time that is given by the GPS-Module
341
                                        SystemTime.Valid = 1; // use the time that is given by the GPS-Module
342
                                   }
342
                                   }
343
                                 }
343
                                 }
344
                                }
344
                                }
345
                                GPSData.Position.Status =               NEWDATA;
345
                                GPSData.Position.Status =               NEWDATA;
346
 
346
 
347
                                GPSData.Status = NEWDATA; // new data available
347
                                GPSData.Status = NEWDATA; // new data available
348
                        } // EOF if(GPSData.Status != NEWDATA)
348
                        } // EOF if(GPSData.Status != NEWDATA)
349
                        // set state to collect new data
349
                        // set state to collect new data
350
                        UbxSol.Status =                                 PROCESSED;      // ready for new data
350
                        UbxSol.Status =                                 PROCESSED;      // ready for new data
351
                        UbxPosLlh.Status =                              PROCESSED;      // ready for new data
351
                        UbxPosLlh.Status =                              PROCESSED;      // ready for new data
352
                        UbxVelNed.Status =                              PROCESSED;      // ready for new data
352
                        UbxVelNed.Status =                              PROCESSED;      // ready for new data
353
                } // EOF all itow are equal
353
                } // EOF all itow are equal
354
        } // EOF all ubx messages received
354
        } // EOF all ubx messages received
355
 
355
 
356
 
356
 
357
//++++++++++++++++++++++++++++++++
357
//++++++++++++++++++++++++++++++++
358
// Please do not delete
358
// Please do not delete
359
// This helps me for testing
359
// This helps me for testing
360
//++++++++++++++++++++++++++++++++
360
//++++++++++++++++++++++++++++++++
361
//GPSData.Position.Longitude = 1517409123L;  // Hamilton, Australia
361
//GPSData.Position.Longitude = 1517409123L;  // Hamilton, Australia
362
//GPSData.Position.Latitude = -329294773L;       // Hamilton, Australia
362
//GPSData.Position.Latitude = -329294773L;       // Hamilton, Australia
363
//++++++++
363
//++++++++
364
//GPSData.Position.Longitude =-1556010020L;  // Alaska
364
//GPSData.Position.Longitude =-1556010020L;  // Alaska
365
//GPSData.Position.Latitude = 629581270L;        // Alaska
365
//GPSData.Position.Latitude = 629581270L;        // Alaska
366
//++++++++
366
//++++++++
367
//GPSData.Position.Longitude =-584343419L;   // Buenos aires
367
//GPSData.Position.Longitude =-584343419L;   // Buenos aires
368
//GPSData.Position.Latitude = -345464421L;   // Buenos aires
368
//GPSData.Position.Latitude = -345464421L;   // Buenos aires
369
//++++++++
369
//++++++++
370
//GPSData.Position.Longitude =1683362691L;   // Neuseeland
370
//GPSData.Position.Longitude =1683362691L;   // Neuseeland
371
//GPSData.Position.Latitude = -465945926L;   // Neuseeland
371
//GPSData.Position.Latitude = -465945926L;   // Neuseeland
372
//++++++++
372
//++++++++
373
//GPSData.Position.Longitude = 194140605L;   // Afrika
373
//GPSData.Position.Longitude = 194140605L;   // Afrika
374
//GPSData.Position.Latitude = -345384656L;   // Afrika
374
//GPSData.Position.Latitude = -345384656L;   // Afrika
375
//++++++++
375
//++++++++
376
//GPSData.Position.Longitude =-740443840L;  // Liberty Staue davor
376
//GPSData.Position.Longitude =-740443840L;  // Liberty Staue davor
377
//GPSData.Position.Latitude = 406888880L;        // Liberty Staue
377
//GPSData.Position.Latitude = 406888880L;        // Liberty Staue
378
 
378
 
379
//GPSData.Position.Longitude =-740451660L;  // Liberty Staue daneben
379
//GPSData.Position.Longitude =-740451660L;  // Liberty Staue daneben
380
//GPSData.Position.Latitude = 406891880L;        // Liberty Staue
380
//GPSData.Position.Latitude = 406891880L;        // Liberty Staue
381
 
381
 
382
//GPSData.Position.Longitude =-740446540L;  // Liberty Staue direkt
382
//GPSData.Position.Longitude =-740446540L;  // Liberty Staue direkt
383
//GPSData.Position.Latitude = 406891590L;        // Liberty Staue 1
383
//GPSData.Position.Latitude = 406891590L;        // Liberty Staue 1
384
 
384
 
385
//GPSData.Position.Longitude =-1142878694L;  // Flori
385
//GPSData.Position.Longitude =-1142878694L;  // Flori
386
//GPSData.Position.Latitude = 483712102L;        //
386
//GPSData.Position.Latitude = 483712102L;        //
387
 
387
 
388
//GPSData.Position.Longitude =1251674613L;  // Bubble
388
//GPSData.Position.Longitude =1251674613L;  // Bubble
389
//GPSData.Position.Latitude = 466058365L;        //
389
//GPSData.Position.Latitude = 466058365L;        //
390
 
390
 
391
}
391
}
392
 
392
 
393
 
393
 
394
/********************************************************/
394
/********************************************************/
395
/*                   UBX Parser                         */
395
/*                   UBX Parser                         */
396
/********************************************************/
396
/********************************************************/
397
void UBX_RxParser(u8 c)
397
void UBX_RxParser(u8 c)
398
{
398
{
399
        static ubxState_t ubxState = UBXSTATE_IDLE;
399
        static ubxState_t ubxState = UBXSTATE_IDLE;
400
        static ubxmsghdr_t RxHdr;
400
        static ubxmsghdr_t RxHdr;
401
        static u8 RxData[UBX_MSG_DATA_SIZE];
401
        static u8 RxData[UBX_MSG_DATA_SIZE];
402
        static u16 RxBytes = 0;
402
        static u16 RxBytes = 0;
403
        static u8 cka, ckb;
403
        static u8 cka, ckb;
404
 
404
 
405
 
405
 
406
        //state machine
406
        //state machine
407
        switch (ubxState)       // ubx message parser
407
        switch (ubxState)       // ubx message parser
408
        {
408
        {
409
                case UBXSTATE_IDLE: // check 1st sync byte
409
                case UBXSTATE_IDLE: // check 1st sync byte
410
                        if (c == UBX_SYNC1_CHAR) ubxState = UBXSTATE_SYNC1;
410
                        if (c == UBX_SYNC1_CHAR) ubxState = UBXSTATE_SYNC1;
411
                        else ubxState = UBXSTATE_IDLE; // out of synchronization
411
                        else ubxState = UBXSTATE_IDLE; // out of synchronization
412
                        break;
412
                        break;
413
 
413
 
414
                case UBXSTATE_SYNC1: // check 2nd sync byte
414
                case UBXSTATE_SYNC1: // check 2nd sync byte
415
                        if (c == UBX_SYNC2_CHAR) ubxState = UBXSTATE_SYNC2;
415
                        if (c == UBX_SYNC2_CHAR) ubxState = UBXSTATE_SYNC2;
416
                        else ubxState = UBXSTATE_IDLE; // out of synchronization
416
                        else ubxState = UBXSTATE_IDLE; // out of synchronization
417
                        break;
417
                        break;
418
 
418
 
419
                case UBXSTATE_SYNC2: // check msg class to be NAV
419
                case UBXSTATE_SYNC2: // check msg class to be NAV
420
                        RxHdr.Class = c;
420
                        RxHdr.Class = c;
421
                        ubxState = UBXSTATE_CLASS;
421
                        ubxState = UBXSTATE_CLASS;
422
                        break;
422
                        break;
423
 
423
 
424
                case UBXSTATE_CLASS: // check message identifier
424
                case UBXSTATE_CLASS: // check message identifier
425
                        RxHdr.Id = c;
425
                        RxHdr.Id = c;
426
                        ubxState = UBXSTATE_LEN1;
426
                        ubxState = UBXSTATE_LEN1;
427
                        cka = RxHdr.Class + RxHdr.Id;
427
                        cka = RxHdr.Class + RxHdr.Id;
428
                        ckb = RxHdr.Class + cka;
428
                        ckb = RxHdr.Class + cka;
429
                        break;
429
                        break;
430
 
430
 
431
                case UBXSTATE_LEN1: // 1st message length byte
431
                case UBXSTATE_LEN1: // 1st message length byte
432
                        RxHdr.Length = (u16)c; // lowbyte first
432
                        RxHdr.Length = (u16)c; // lowbyte first
433
                        cka += c;
433
                        cka += c;
434
                        ckb += cka;
434
                        ckb += cka;
435
                        ubxState = UBXSTATE_LEN2;
435
                        ubxState = UBXSTATE_LEN2;
436
                        break;
436
                        break;
437
 
437
 
438
                case UBXSTATE_LEN2: // 2nd message length byte
438
                case UBXSTATE_LEN2: // 2nd message length byte
439
                        RxHdr.Length += ((u16)c)<<8; // high byte last
439
                        RxHdr.Length += ((u16)c)<<8; // high byte last
440
                        if (RxHdr.Length >= UBX_MSG_DATA_SIZE)
440
                        if (RxHdr.Length >= UBX_MSG_DATA_SIZE)
441
                        {
441
                        {
442
                                ubxState = UBXSTATE_IDLE;
442
                                ubxState = UBXSTATE_IDLE;
443
                        }
443
                        }
444
                        else
444
                        else
445
                        {
445
                        {
446
                                cka += c;
446
                                cka += c;
447
                                ckb += cka;
447
                                ckb += cka;
448
                                RxBytes = 0;    // reset data byte counter
448
                                RxBytes = 0;    // reset data byte counter
449
                                ubxState = UBXSTATE_DATA;
449
                                ubxState = UBXSTATE_DATA;
450
                        }
450
                        }
451
                        break;
451
                        break;
452
 
452
 
453
                case UBXSTATE_DATA: // collecting data
453
                case UBXSTATE_DATA: // collecting data
454
                        if (RxBytes < UBX_MSG_DATA_SIZE)
454
                        if (RxBytes < UBX_MSG_DATA_SIZE)
455
                        {
455
                        {
456
                                RxData[RxBytes++] = c; // copy curent data byte if any space is left
456
                                RxData[RxBytes++] = c; // copy curent data byte if any space is left
457
                                cka += c;
457
                                cka += c;
458
                                ckb += cka;
458
                                ckb += cka;
459
                                if (RxBytes >= RxHdr.Length) ubxState = UBXSTATE_CKA; // switch to next state if all data have been received
459
                                if (RxBytes >= RxHdr.Length) ubxState = UBXSTATE_CKA; // switch to next state if all data have been received
460
                        }
460
                        }
461
                        else // rx buffer overrun
461
                        else // rx buffer overrun
462
                        {
462
                        {
463
                                ubxState = UBXSTATE_IDLE;
463
                                ubxState = UBXSTATE_IDLE;
464
                        }
464
                        }
465
                        break;
465
                        break;
466
 
466
 
467
                case UBXSTATE_CKA:
467
                case UBXSTATE_CKA:
468
                        if (c == cka) ubxState = UBXSTATE_CKB;
468
                        if (c == cka) ubxState = UBXSTATE_CKB;
469
                        else
469
                        else
470
                        {
470
                        {
471
                                ubxState = UBXSTATE_IDLE;
471
                                ubxState = UBXSTATE_IDLE;
472
                        }
472
                        }
473
                        break;
473
                        break;
474
 
474
 
475
                case UBXSTATE_CKB:
475
                case UBXSTATE_CKB:
476
                        if (c == ckb)
476
                        if (c == ckb)
477
                        {       // checksum is ok
477
                        {       // checksum is ok
478
 
478
 
479
                                switch(RxHdr.Class)
479
                                switch(RxHdr.Class)
480
                                {
480
                                {
481
                                        case UBX_CLASS_NAV:
481
                                        case UBX_CLASS_NAV:
482
                                                switch(RxHdr.Id)
482
                                                switch(RxHdr.Id)
483
                                                {
483
                                                {
484
                                                        case UBX_ID_POSLLH: // geodetic position
484
                                                        case UBX_ID_POSLLH: // geodetic position
485
                                                                memcpy((u8*)&UbxPosLlh, RxData, RxHdr.Length);
485
                                                                memcpy((u8*)&UbxPosLlh, RxData, RxHdr.Length);
486
                                                                UbxPosLlh.Status = NEWDATA;
486
                                                                UbxPosLlh.Status = NEWDATA;
487
                                                                break;
487
                                                                break;
488
 
488
 
489
                                                        case UBX_ID_VELNED: // velocity vector in tangent plane
489
                                                        case UBX_ID_VELNED: // velocity vector in tangent plane
490
                                                                memcpy((u8*)&UbxVelNed, RxData, RxHdr.Length);
490
                                                                memcpy((u8*)&UbxVelNed, RxData, RxHdr.Length);
491
                                                                UbxVelNed.Status = NEWDATA;
491
                                                                UbxVelNed.Status = NEWDATA;
492
                                                                break;
492
                                                                break;
493
 
493
 
494
                                                        case UBX_ID_SOL: // navigation solution
494
                                                        case UBX_ID_SOL: // navigation solution
495
                                                                memcpy((u8*)&UbxSol, RxData, RxHdr.Length);
495
                                                                memcpy((u8*)&UbxSol, RxData, RxHdr.Length);
496
                                                                UbxSol.Status = NEWDATA;
496
                                                                UbxSol.Status = NEWDATA;
497
                                                                break;
497
                                                                break;
498
 
498
 
499
                                                        default:
499
                                                        default:
500
                                                                break;
500
                                                                break;
501
                                                } // EOF switch(Id)
501
                                                } // EOF switch(Id)
502
                                                Update_GPSData();
502
                                                Update_GPSData();
503
                                                break;
503
                                                break;
504
 
504
 
505
                                        default:
505
                                        default:
506
                                                break;
506
                                                break;
507
                                } // EOF switch(class)
507
                                } // EOF switch(class)
508
 
508
 
509
                                // check generic msg filter
509
                                // check generic msg filter
510
                                if(UbxMsg.Status != NEWDATA)
510
                                if(UbxMsg.Status != NEWDATA)
511
                                {       // msg buffer is free
511
                                {       // msg buffer is free
512
                                        if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (RxHdr.Class&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (RxHdr.Id&UbxMsg.IdMask)))
512
                                        if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (RxHdr.Class&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (RxHdr.Id&UbxMsg.IdMask)))
513
                                        {       // msg matches to the filter criteria
513
                                        {       // msg matches to the filter criteria
514
                                                UbxMsg.Status = INVALID;
514
                                                UbxMsg.Status = INVALID;
515
                                                UbxMsg.Hdr.Class = RxHdr.Class;
515
                                                UbxMsg.Hdr.Class = RxHdr.Class;
516
                                                UbxMsg.Hdr.Id = RxHdr.Id;
516
                                                UbxMsg.Hdr.Id = RxHdr.Id;
517
                                                UbxMsg.Hdr.Length = RxHdr.Length;
517
                                                UbxMsg.Hdr.Length = RxHdr.Length;
518
                                                if(UbxMsg.Hdr.Length <= UBX_MSG_DATA_SIZE)
518
                                                if(UbxMsg.Hdr.Length <= UBX_MSG_DATA_SIZE)
519
                                                {       // copy data block
519
                                                {       // copy data block
520
                                                        memcpy(UbxMsg.Data, RxData, RxHdr.Length);
520
                                                        memcpy(UbxMsg.Data, RxData, RxHdr.Length);
521
                                                        UbxMsg.Status = NEWDATA;
521
                                                        UbxMsg.Status = NEWDATA;
522
                                                }
522
                                                }
523
                                        } // EOF filter matches
523
                                        } // EOF filter matches
524
                                } // EOF != INVALID
524
                                } // EOF != INVALID
525
                        }// EOF crc ok
525
                        }// EOF crc ok
526
                        else DebugOut.Analog[13]++; // CRC Error -> since 2.10e (6.2015)
526
                        else DebugOut.Analog[13]++; // CRC Error -> since 2.10e (6.2015)
527
                        ubxState = UBXSTATE_IDLE; // ready to parse new data
527
                        ubxState = UBXSTATE_IDLE; // ready to parse new data
528
                        break;
528
                        break;
529
 
529
 
530
                default: // unknown ubx state
530
                default: // unknown ubx state
531
                        ubxState = UBXSTATE_IDLE;
531
                        ubxState = UBXSTATE_IDLE;
532
                        break;
532
                        break;
533
 
533
 
534
        }
534
        }
535
}
535
}
536
 
536
 
537
u8 UBX_CreateMsg(Buffer_t* pBuff, u8* pData, u16 Len)
537
u8 UBX_CreateMsg(Buffer_t* pBuff, u8* pData, u16 Len)
538
{
538
{
539
        u16 i;
539
        u16 i;
540
        u8 cka = 0, ckb = 0;
540
        u8 cka = 0, ckb = 0;
541
        // check if buffer is available
541
        // check if buffer is available
542
        if(pBuff->Locked == TRUE) return(0);
542
        if(pBuff->Locked == TRUE) return(0);
543
        // check if buffer size is sufficient
543
        // check if buffer size is sufficient
544
        if(pBuff->Size < 8 + Len) return(0);
544
        if(pBuff->Size < 8 + Len) return(0);
545
        // lock the buffer
545
        // lock the buffer
546
        pBuff->Locked = TRUE;
546
        pBuff->Locked = TRUE;
547
        // start at begin
547
        // start at begin
548
        pBuff->Position = 0;
548
        pBuff->Position = 0;
549
        pBuff->pData[pBuff->Position++] = UBX_SYNC1_CHAR;
549
        pBuff->pData[pBuff->Position++] = UBX_SYNC1_CHAR;
550
        pBuff->pData[pBuff->Position++] = UBX_SYNC2_CHAR;
550
        pBuff->pData[pBuff->Position++] = UBX_SYNC2_CHAR;
551
        for(i=0;i<Len;i++)
551
        for(i=0;i<Len;i++)
552
        {
552
        {
553
                pBuff->pData[pBuff->Position++] = pData[i];
553
                pBuff->pData[pBuff->Position++] = pData[i];
554
        }
554
        }
555
        // calculate checksum
555
        // calculate checksum
556
        for(i=2;i<pBuff->Position;i++)
556
        for(i=2;i<pBuff->Position;i++)
557
        {
557
        {
558
                cka += pBuff->pData[i];
558
                cka += pBuff->pData[i];
559
                ckb += cka;
559
                ckb += cka;
560
        }
560
        }
561
        pBuff->pData[pBuff->Position++] = cka;
561
        pBuff->pData[pBuff->Position++] = cka;
562
        pBuff->pData[pBuff->Position++] = ckb;
562
        pBuff->pData[pBuff->Position++] = ckb;
563
        pBuff->DataBytes = pBuff->Position;
563
        pBuff->DataBytes = pBuff->Position;
564
        pBuff->Position = 0;  // reset buffer position for transmision
564
        pBuff->Position = 0;  // reset buffer position for transmision
565
        return(1);
565
        return(1);
566
}
566
}
567
/*
567
/*
568
switch(ubxclass)
568
switch(ubxclass)
569
                        {
569
                        {
570
                                case UBX_CLASS_NAV:
570
                                case UBX_CLASS_NAV:
571
                                        switch(ubxid)
571
                                        switch(ubxid)
572
                                        {
572
                                        {
573
                                                case UBX_ID_POSLLH: // geodetic position
573
                                                case UBX_ID_POSLLH: // geodetic position
574
                                                        ubxSp =  (u8 *)&UbxPosLlh; // data start pointer
574
                                                        ubxSp =  (u8 *)&UbxPosLlh; // data start pointer
575
                                                        ubxEp = (u8 *)(&UbxPosLlh + 1); // data end pointer
575
                                                        ubxEp = (u8 *)(&UbxPosLlh + 1); // data end pointer
576
                                                        ubxStP = (u8 *)&UbxPosLlh.Status; // status pointer
576
                                                        ubxStP = (u8 *)&UbxPosLlh.Status; // status pointer
577
                                                        break;
577
                                                        break;
578
 
578
 
579
                                                case UBX_ID_SOL: // navigation solution
579
                                                case UBX_ID_SOL: // navigation solution
580
                                                        ubxSp =  (u8 *)&UbxSol; // data start pointer
580
                                                        ubxSp =  (u8 *)&UbxSol; // data start pointer
581
                                                        ubxEp = (u8 *)(&UbxSol + 1); // data end pointer
581
                                                        ubxEp = (u8 *)(&UbxSol + 1); // data end pointer
582
                                                        ubxStP = (u8 *)&UbxSol.Status; // status pointer
582
                                                        ubxStP = (u8 *)&UbxSol.Status; // status pointer
583
                                                        break;
583
                                                        break;
584
 
584
 
585
                                                case UBX_ID_VELNED: // velocity vector in tangent plane
585
                                                case UBX_ID_VELNED: // velocity vector in tangent plane
586
                                                        ubxSp =  (u8 *)&UbxVelNed; // data start pointer
586
                                                        ubxSp =  (u8 *)&UbxVelNed; // data start pointer
587
                                                        ubxEp = (u8 *)(&UbxVelNed + 1); // data end pointer
587
                                                        ubxEp = (u8 *)(&UbxVelNed + 1); // data end pointer
588
                                                        ubxStP = (u8 *)&UbxVelNed.Status; // status pointer
588
                                                        ubxStP = (u8 *)&UbxVelNed.Status; // status pointer
589
                                                        break;
589
                                                        break;
590
 
590
 
591
                                                default:                        // unsupported identifier
591
                                                default:                        // unsupported identifier
592
                                                        ubxState = UBXSTATE_IDLE;
592
                                                        ubxState = UBXSTATE_IDLE;
593
                                                        return;
593
                                                        return;
594
                                        }
594
                                        }
595
                                        break;
595
                                        break;
596
 
596
 
597
                                default: // other  classes
597
                                default: // other  classes
598
                                        if(UbxMsg.Status == NEWDATA) ubxState = UBXSTATE_IDLE;
598
                                        if(UbxMsg.Status == NEWDATA) ubxState = UBXSTATE_IDLE;
599
                                        else if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (ubxclass&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (ubxid&UbxMsg.IdMask)))
599
                                        else if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (ubxclass&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (ubxid&UbxMsg.IdMask)))
600
                                        {       // buffer is free and message matches to filter criteria
600
                                        {       // buffer is free and message matches to filter criteria
601
                                                UbxMsg.Status = INVALID;
601
                                                UbxMsg.Status = INVALID;
602
                                                UbxMsg.Hdr.Class = ubxclass;
602
                                                UbxMsg.Hdr.Class = ubxclass;
603
                                                UbxMsg.Hdr.Id = ubxid;
603
                                                UbxMsg.Hdr.Id = ubxid;
604
                                                UbxMsg.Hdr.Length = msglen;
604
                                                UbxMsg.Hdr.Length = msglen;
605
                                                ubxSp =  (u8 *)&(UbxMsg.Data);   // data start pointer
605
                                                ubxSp =  (u8 *)&(UbxMsg.Data);   // data start pointer
606
                                                ubxEp = (u8 *)(&UbxMsg + 1);    // data end pointer
606
                                                ubxEp = (u8 *)(&UbxMsg + 1);    // data end pointer
607
                                                ubxStP = (u8 *)&UbxMsg.Status;  // status pointer
607
                                                ubxStP = (u8 *)&UbxMsg.Status;  // status pointer
608
                                        }
608
                                        }
609
                                        else ubxState = UBXSTATE_IDLE;
609
                                        else ubxState = UBXSTATE_IDLE;
610
                                        break;
610
                                        break;
611
                        }
611
                        }
612
*/
612
*/
613
 
613
 
614
 
614