Subversion Repositories NaviCtrl

Rev

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

Rev 507 Rev 514
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 "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  16
84
#define LEAP_SECONDS_FROM_1980  16
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 identifiers
89
// protocoll identifiers
90
// navigation class
90
// navigation class
91
#define UBX_CLASS_NAV   0x01
91
#define UBX_CLASS_NAV   0x01
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
// global variables
164
// global variables
165
 
165
 
166
// local buffers for the incomming ubx messages
166
// local buffers for the incomming ubx messages
167
ubx_nav_sol_t           UbxSol    = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, INVALID};
167
ubx_nav_sol_t           UbxSol    = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, INVALID};
168
ubx_nav_posllh_t        UbxPosLlh = {0,0,0,0,0,0,0, INVALID};
168
ubx_nav_posllh_t        UbxPosLlh = {0,0,0,0,0,0,0, INVALID};
169
ubx_nav_velned_t        UbxVelNed = {0,0,0,0,0,0,0,0,0, INVALID};
169
ubx_nav_velned_t        UbxVelNed = {0,0,0,0,0,0,0,0,0, INVALID};
170
ubxmsg_t                        UbxMsg;
170
ubxmsg_t                        UbxMsg;
171
 
171
 
172
// shared buffer
172
// shared buffer
173
gps_data_t              GPSData = {200,{0,0,0,INVALID},0,0,0,0,0,0,0, INVALID};
173
gps_data_t              GPSData = {200,{0,0,0,INVALID},0,0,0,0,0,0,0, INVALID};
174
DateTime_t                      GPSDateTime = {0,0,0,0,0,0,0, INVALID};
174
DateTime_t                      GPSDateTime = {0,0,0,0,0,0,0, INVALID};
175
 
175
 
176
#define UBX_TIMEOUT 500 // 500 ms
176
#define UBX_TIMEOUT 500 // 500 ms
177
u32 UBX_Timeout = 0;
177
u32 UBX_Timeout = 0;
178
 
178
 
179
//------------------------------------------------------------------------------------
179
//------------------------------------------------------------------------------------
180
// functions
180
// functions
181
 
181
 
182
u8 IsLeapYear(u16 year)
182
u8 IsLeapYear(u16 year)
183
{
183
{
184
        if((year%400 == 0) || ( (year%4 == 0) && (year%100 != 0) ) ) return 1;
184
        if((year%400 == 0) || ( (year%4 == 0) && (year%100 != 0) ) ) return 1;
185
        else return 0;
185
        else return 0;
186
}
186
}
187
/********************************************************/
187
/********************************************************/
188
/*  Calculates the UTC Time from the GPS week and tow   */
188
/*  Calculates the UTC Time from the GPS week and tow   */
189
/********************************************************/
189
/********************************************************/
190
void SetGPSTime(DateTime_t * pTimeStruct)
190
void SetGPSTime(DateTime_t * pTimeStruct)
191
{
191
{
192
        u32 Days, Seconds, Week;
192
        u32 Days, Seconds, Week;
193
        u16 YearPart;
193
        u16 YearPart;
194
        u32 * MonthDayTab = 0;
194
        u32 * MonthDayTab = 0;
195
        u8 i;
195
        u8 i;
196
 
196
 
197
 
197
 
198
        // if GPS data show valid time data
198
        // if GPS data show valid time data
199
        if((UbxSol.Status != INVALID) && (UbxSol.Flags & FLAG_WKNSET) && (UbxSol.Flags & FLAG_TOWSET) )
199
        if((UbxSol.Status != INVALID) && (UbxSol.Flags & FLAG_WKNSET) && (UbxSol.Flags & FLAG_TOWSET) )
200
        {
200
        {
201
                Seconds = UbxSol.itow / 1000L;
201
                Seconds = UbxSol.itow / 1000L;
202
                Week = (u32)UbxSol.week;
202
                Week = (u32)UbxSol.week;
203
                // correct leap seconds since 1980
203
                // correct leap seconds since 1980
204
                if(Seconds < LEAP_SECONDS_FROM_1980)
204
                if(Seconds < LEAP_SECONDS_FROM_1980)
205
                {
205
                {
206
                        Week--;
206
                        Week--;
207
                        Seconds = SECONDS_PER_WEEK - LEAP_SECONDS_FROM_1980 + Seconds;
207
                        Seconds = SECONDS_PER_WEEK - LEAP_SECONDS_FROM_1980 + Seconds;
208
                }
208
                }
209
                else Seconds -= LEAP_SECONDS_FROM_1980;
209
                else Seconds -= LEAP_SECONDS_FROM_1980;
210
 
210
 
211
                Days = DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980;
211
                Days = DAYS_FROM_JAN01YEAR0001_TO_JAN6_1980;
212
                Days += (Week * DAYS_PER_WEEK);
212
                Days += (Week * DAYS_PER_WEEK);
213
                Days += Seconds / SECONDS_PER_DAY; // seperate days from GPS seconds of week
213
                Days += Seconds / SECONDS_PER_DAY; // seperate days from GPS seconds of week
214
 
214
 
215
                pTimeStruct->Year = 1;
215
                pTimeStruct->Year = 1;
216
                YearPart = (u16)(Days / DAYS_PER_400YEARS);
216
                YearPart = (u16)(Days / DAYS_PER_400YEARS);
217
                pTimeStruct->Year += YearPart * 400;
217
                pTimeStruct->Year += YearPart * 400;
218
                Days = Days % DAYS_PER_400YEARS;
218
                Days = Days % DAYS_PER_400YEARS;
219
                YearPart = (u16)(Days / DAYS_PER_100YEARS);
219
                YearPart = (u16)(Days / DAYS_PER_100YEARS);
220
                pTimeStruct->Year += YearPart * 100;
220
                pTimeStruct->Year += YearPart * 100;
221
                Days = Days % DAYS_PER_100YEARS;
221
                Days = Days % DAYS_PER_100YEARS;
222
                YearPart = (u16)(Days / DAYS_PER_4YEARS);
222
                YearPart = (u16)(Days / DAYS_PER_4YEARS);
223
                pTimeStruct->Year += YearPart * 4;
223
                pTimeStruct->Year += YearPart * 4;
224
                Days = Days % DAYS_PER_4YEARS;
224
                Days = Days % DAYS_PER_4YEARS;
225
                if(Days < (3* DAYS_PER_YEAR)) YearPart = (u16)(Days / DAYS_PER_YEAR);
225
                if(Days < (3* DAYS_PER_YEAR)) YearPart = (u16)(Days / DAYS_PER_YEAR);
226
                else YearPart = 3;
226
                else YearPart = 3;
227
                pTimeStruct->Year += YearPart;
227
                pTimeStruct->Year += YearPart;
228
                // calculate remaining days of year
228
                // calculate remaining days of year
229
                Days -= (u32)(YearPart *  DAYS_PER_YEAR);
229
                Days -= (u32)(YearPart *  DAYS_PER_YEAR);
230
                Days += 1;
230
                Days += 1;
231
                // check if current year is a leap year
231
                // check if current year is a leap year
232
                if(IsLeapYear(pTimeStruct->Year)) MonthDayTab = (u32*)Leap;
232
                if(IsLeapYear(pTimeStruct->Year)) MonthDayTab = (u32*)Leap;
233
                else MonthDayTab = (u32*)Normal;
233
                else MonthDayTab = (u32*)Normal;
234
            // seperate month and day from days of year
234
            // seperate month and day from days of year
235
                for ( i = 0; i < 12; i++ )
235
                for ( i = 0; i < 12; i++ )
236
                {
236
                {
237
                        if ( (MonthDayTab[i]< Days) && (Days <= MonthDayTab[i+1]) )
237
                        if ( (MonthDayTab[i]< Days) && (Days <= MonthDayTab[i+1]) )
238
                        {
238
                        {
239
                                pTimeStruct->Month = i+1;
239
                                pTimeStruct->Month = i+1;
240
                                pTimeStruct->Day = Days - MonthDayTab[i];
240
                                pTimeStruct->Day = Days - MonthDayTab[i];
241
                                i = 12;
241
                                i = 12;
242
                        }
242
                        }
243
                }
243
                }
244
                Seconds = Seconds % SECONDS_PER_DAY; // remaining seconds of current day
244
                Seconds = Seconds % SECONDS_PER_DAY; // remaining seconds of current day
245
                pTimeStruct->Hour = (u8)(Seconds / SECONDS_PER_HOUR);
245
                pTimeStruct->Hour = (u8)(Seconds / SECONDS_PER_HOUR);
246
                Seconds = Seconds % SECONDS_PER_HOUR; // remaining seconds of current hour
246
                Seconds = Seconds % SECONDS_PER_HOUR; // remaining seconds of current hour
247
                pTimeStruct->Min = (u8)(Seconds / SECONDS_PER_MINUTE);
247
                pTimeStruct->Min = (u8)(Seconds / SECONDS_PER_MINUTE);
248
                Seconds = Seconds % SECONDS_PER_MINUTE; // remaining seconds of current minute
248
                Seconds = Seconds % SECONDS_PER_MINUTE; // remaining seconds of current minute
249
                pTimeStruct->Sec = (u8)(Seconds);
249
                pTimeStruct->Sec = (u8)(Seconds);
250
                pTimeStruct->mSec  = (u16)(UbxSol.itow % 1000L);
250
                pTimeStruct->mSec  = (u16)(UbxSol.itow % 1000L);
251
                pTimeStruct->Valid = 1;
251
                pTimeStruct->Valid = 1;
252
        }
252
        }
253
        else
253
        else
254
        {
254
        {
255
                pTimeStruct->Valid = 0;
255
                pTimeStruct->Valid = 0;
256
        }
256
        }
257
}
257
}
258
 
258
 
259
 
259
 
260
 
260
 
261
/********************************************************/
261
/********************************************************/
262
/*                  Initialize UBX Parser               */
262
/*                  Initialize UBX Parser               */
263
/********************************************************/
263
/********************************************************/
264
void UBX_Init(void)
264
void UBX_Init(void)
265
{
265
{
266
        // mark msg buffers invalid
266
        // mark msg buffers invalid
267
        UbxSol.Status = INVALID;
267
        UbxSol.Status = INVALID;
268
        UbxPosLlh.Status = INVALID;
268
        UbxPosLlh.Status = INVALID;
269
        UbxVelNed.Status = INVALID;
269
        UbxVelNed.Status = INVALID;
270
        UbxMsg.Status = INVALID;
270
        UbxMsg.Status = INVALID;
271
        GPSData.Status = INVALID;
271
        GPSData.Status = INVALID;
272
 
272
 
273
        UBX_Timeout = SetDelay(2 * UBX_Timeout);
273
        UBX_Timeout = SetDelay(2 * UBX_Timeout);
274
}
274
}
275
 
275
 
276
/********************************************************/
276
/********************************************************/
277
/*            Upate GPS data stcructure                 */
277
/*            Upate GPS data stcructure                 */
278
/********************************************************/
278
/********************************************************/
279
void Update_GPSData(void)
279
void Update_GPSData(void)
280
{
280
{
281
        static u32 last_itow = 0;
281
        static u32 last_itow = 0;
282
 
282
 
283
        // if a new set of ubx messages was collected
283
        // if a new set of ubx messages was collected
284
        if((UbxSol.Status == NEWDATA) && (UbxPosLlh.Status == NEWDATA) && (UbxVelNed.Status == NEWDATA))
284
        if((UbxSol.Status == NEWDATA) && (UbxPosLlh.Status == NEWDATA) && (UbxVelNed.Status == NEWDATA))
285
        {       // and the itow is equal (same time base)
285
        {       // and the itow is equal (same time base)
286
                UART_VersionInfo.Flags |= NC_VERSION_FLAG_GPS_PRESENT;
286
                UART_VersionInfo.Flags |= NC_VERSION_FLAG_GPS_PRESENT;
287
                if((UbxSol.itow == UbxPosLlh.itow) && (UbxPosLlh.itow == UbxVelNed.itow))
287
                if((UbxSol.itow == UbxPosLlh.itow) && (UbxPosLlh.itow == UbxVelNed.itow))
288
                {
288
                {
289
                        UBX_Timeout = SetDelay(UBX_TIMEOUT);
289
                        UBX_Timeout = SetDelay(UBX_TIMEOUT);
290
                        DebugOut.Analog[9]++;
290
                        DebugOut.Analog[9]++;
291
                        // update GPS data only if the status is INVALID or PROCESSED
291
                        // update GPS data only if the status is INVALID or PROCESSED
292
                        if(GPSData.Status != NEWDATA)  
292
                        if(GPSData.Status != NEWDATA)  
293
                        { // wait for new data at all neccesary ubx messages
293
                        { // wait for new data at all neccesary ubx messages
294
                                GPSData.Status = INVALID;
294
                                GPSData.Status = INVALID;
295
                                // update message cycle time
295
                                // update message cycle time
296
                                GPSData.MsgCycleTime = (u16)(UbxSol.itow-last_itow);
296
                                GPSData.MsgCycleTime = (u16)(UbxSol.itow-last_itow);
297
                                last_itow = UbxSol.itow; // update last itow
297
                                last_itow = UbxSol.itow; // update last itow
298
                                // NAV SOL
298
                                // NAV SOL
299
                                GPSData.Flags =         (GPSData.Flags & 0xf0) | (UbxSol.Flags & 0x0f); // we take only the lower bits
299
                                GPSData.Flags =         (GPSData.Flags & 0xf0) | (UbxSol.Flags & 0x0f); // we take only the lower bits
300
                                GPSData.NumOfSats =                     UbxSol.numSV;
300
                                GPSData.NumOfSats =                     UbxSol.numSV;
301
                                GPSData.SatFix =                                UbxSol.GPSfix;
301
                                GPSData.SatFix =                                UbxSol.GPSfix;
302
                                GPSData.Position_Accuracy =             UbxSol.PAcc;    // in steps of 1cm
302
                                GPSData.Position_Accuracy =             UbxSol.PAcc;    // in steps of 1cm
303
                                GPSData.Speed_Accuracy =                UbxSol.SAcc;    // in steps of 1cm/s
303
                                GPSData.Speed_Accuracy =                UbxSol.SAcc;    // in steps of 1cm/s
304
                                SetGPSTime(&SystemTime); // update system time
304
                                SetGPSTime(&SystemTime); // update system time
305
                                // NAV POSLLH
305
                                // NAV POSLLH
306
                                GPSData.Position.Status =               INVALID;
306
                                GPSData.Position.Status =               INVALID;
307
                                GPSData.Position.Longitude =    UbxPosLlh.LON;  // in steps of 1E-7 deg
307
                                GPSData.Position.Longitude =    UbxPosLlh.LON;  // in steps of 1E-7 deg
308
                                GPSData.Position.Latitude =     UbxPosLlh.LAT;  // in steps of 1E-7 deg
308
                                GPSData.Position.Latitude =     UbxPosLlh.LAT;  // in steps of 1E-7 deg
309
                                GPSData.Position.Altitude =     UbxPosLlh.HMSL; // in steps of 1 mm
309
                                GPSData.Position.Altitude =     UbxPosLlh.HMSL; // in steps of 1 mm
310
                                GPSData.Position.Status =               NEWDATA;
310
                                GPSData.Position.Status =               NEWDATA;
311
                                // NAV VELNED
311
                                // NAV VELNED
312
                                GPSData.Speed_East =                    UbxVelNed.VEL_E;        // in steps of 1cm/s
312
                                GPSData.Speed_East =                    UbxVelNed.VEL_E;        // in steps of 1cm/s
313
                                GPSData.Speed_North =                   UbxVelNed.VEL_N;    // in steps of 1cm/s
313
                                GPSData.Speed_North =                   UbxVelNed.VEL_N;    // in steps of 1cm/s
314
                                GPSData.Speed_Top       =                       -UbxVelNed.VEL_D;   // in steps of 1cm/s
314
                                GPSData.Speed_Top       =                       -UbxVelNed.VEL_D;   // in steps of 1cm/s
315
                                GPSData.Speed_Ground =                  UbxVelNed.GSpeed;       // in steps of 1cm/s
315
                                GPSData.Speed_Ground =                  UbxVelNed.GSpeed;       // in steps of 1cm/s
316
                                GPSData.Heading =                               UbxVelNed.Heading; //in steps of 1E-5 deg
316
                                GPSData.Heading =                               UbxVelNed.Heading; //in steps of 1E-5 deg
317
               
317
               
318
                                GPSData.Status = NEWDATA; // new data available
318
                                GPSData.Status = NEWDATA; // new data available
319
                        } // EOF if(GPSData.Status != NEWDATA)
319
                        } // EOF if(GPSData.Status != NEWDATA)
320
                        // set state to collect new data
320
                        // set state to collect new data
321
                        UbxSol.Status =                                 PROCESSED;      // ready for new data
321
                        UbxSol.Status =                                 PROCESSED;      // ready for new data
322
                        UbxPosLlh.Status =                              PROCESSED;      // ready for new data
322
                        UbxPosLlh.Status =                              PROCESSED;      // ready for new data
323
                        UbxVelNed.Status =                              PROCESSED;      // ready for new data
323
                        UbxVelNed.Status =                              PROCESSED;      // ready for new data
324
                } // EOF all itow are equal
324
                } // EOF all itow are equal
325
        } // EOF all ubx messages received
325
        } // EOF all ubx messages received
326
 
326
 
327
 
327
 
328
//++++++++++++++++++++++++++++++++
328
//++++++++++++++++++++++++++++++++
329
// Please do not delete
329
// Please do not delete
330
// This helps me for testing 
330
// This helps me for testing 
331
//++++++++++++++++++++++++++++++++
331
//++++++++++++++++++++++++++++++++
332
//GPSData.Position.Longitude = 1517409123L;  // Hamilton, Australia
332
//GPSData.Position.Longitude = 1517409123L;  // Hamilton, Australia
333
//GPSData.Position.Latitude = -329294773L;       // Hamilton, Australia
333
//GPSData.Position.Latitude = -329294773L;       // Hamilton, Australia
334
//++++++++
334
//++++++++
335
//GPSData.Position.Longitude =-1556010020L;  // Alaska
335
//GPSData.Position.Longitude =-1556010020L;  // Alaska
336
//GPSData.Position.Latitude = 629581270L;        // Alaska
336
//GPSData.Position.Latitude = 629581270L;        // Alaska
337
//++++++++
337
//++++++++
338
//GPSData.Position.Longitude =-584343419L;   // Buenos aires
338
//GPSData.Position.Longitude =-584343419L;   // Buenos aires
339
//GPSData.Position.Latitude = -345464421L;   // Buenos aires
339
//GPSData.Position.Latitude = -345464421L;   // Buenos aires
340
//++++++++
340
//++++++++
341
//GPSData.Position.Longitude =1683362691L;   // Neuseeland
341
//GPSData.Position.Longitude =1683362691L;   // Neuseeland
342
//GPSData.Position.Latitude = -465945926L;   // Neuseeland
342
//GPSData.Position.Latitude = -465945926L;   // Neuseeland
343
//++++++++
343
//++++++++
344
//GPSData.Position.Longitude = 194140605L;   // Afrika
344
//GPSData.Position.Longitude = 194140605L;   // Afrika
345
//GPSData.Position.Latitude = -345384656L;   // Afrika
345
//GPSData.Position.Latitude = -345384656L;   // Afrika
346
//++++++++
346
//++++++++
347
//GPSData.Position.Longitude =-740443840L;  // Liberty Staue davor
347
//GPSData.Position.Longitude =-740443840L;  // Liberty Staue davor
348
//GPSData.Position.Latitude = 406888880L;        // Liberty Staue 
348
//GPSData.Position.Latitude = 406888880L;        // Liberty Staue 
349
 
349
 
350
//GPSData.Position.Longitude =-740451660L;  // Liberty Staue daneben
350
//GPSData.Position.Longitude =-740451660L;  // Liberty Staue daneben
351
//GPSData.Position.Latitude = 406891880L;        // Liberty Staue 
351
//GPSData.Position.Latitude = 406891880L;        // Liberty Staue 
352
 
352
 
353
//GPSData.Position.Longitude =-740446540L;  // Liberty Staue direkt
353
//GPSData.Position.Longitude =-740446540L;  // Liberty Staue direkt
354
//GPSData.Position.Latitude = 406891590L;        // Liberty Staue 1
354
//GPSData.Position.Latitude = 406891590L;        // Liberty Staue 1
-
 
355
 
-
 
356
//GPSData.Position.Longitude =-1142878694L;  // Flori
-
 
357
//GPSData.Position.Latitude = 483712102L;        // 
-
 
358
 
-
 
359
//GPSData.Position.Longitude =1251674613L;  // Bubble
-
 
360
//GPSData.Position.Latitude = 466058365L;        // 
355
 
361
 
356
}
362
}
357
 
363
 
358
 
364
 
359
/********************************************************/
365
/********************************************************/
360
/*                   UBX Parser                         */
366
/*                   UBX Parser                         */
361
/********************************************************/
367
/********************************************************/
362
void UBX_RxParser(u8 c)
368
void UBX_RxParser(u8 c)
363
{
369
{
364
        static ubxState_t ubxState = UBXSTATE_IDLE;
370
        static ubxState_t ubxState = UBXSTATE_IDLE;
365
        static ubxmsghdr_t RxHdr;
371
        static ubxmsghdr_t RxHdr;
366
        static u8 RxData[UBX_MSG_DATA_SIZE];
372
        static u8 RxData[UBX_MSG_DATA_SIZE];
367
        static u16 RxBytes = 0;
373
        static u16 RxBytes = 0;
368
        static u8 cka, ckb;
374
        static u8 cka, ckb;
369
 
375
 
370
 
376
 
371
        //state machine
377
        //state machine
372
        switch (ubxState)       // ubx message parser
378
        switch (ubxState)       // ubx message parser
373
        {
379
        {
374
                case UBXSTATE_IDLE: // check 1st sync byte
380
                case UBXSTATE_IDLE: // check 1st sync byte
375
                        if (c == UBX_SYNC1_CHAR) ubxState = UBXSTATE_SYNC1;
381
                        if (c == UBX_SYNC1_CHAR) ubxState = UBXSTATE_SYNC1;
376
                        else ubxState = UBXSTATE_IDLE; // out of synchronization
382
                        else ubxState = UBXSTATE_IDLE; // out of synchronization
377
                        break;
383
                        break;
378
 
384
 
379
                case UBXSTATE_SYNC1: // check 2nd sync byte
385
                case UBXSTATE_SYNC1: // check 2nd sync byte
380
                        if (c == UBX_SYNC2_CHAR) ubxState = UBXSTATE_SYNC2;
386
                        if (c == UBX_SYNC2_CHAR) ubxState = UBXSTATE_SYNC2;
381
                        else ubxState = UBXSTATE_IDLE; // out of synchronization
387
                        else ubxState = UBXSTATE_IDLE; // out of synchronization
382
                        break;
388
                        break;
383
 
389
 
384
                case UBXSTATE_SYNC2: // check msg class to be NAV
390
                case UBXSTATE_SYNC2: // check msg class to be NAV
385
                        RxHdr.Class = c;
391
                        RxHdr.Class = c;
386
                        ubxState = UBXSTATE_CLASS;
392
                        ubxState = UBXSTATE_CLASS;
387
                        break;
393
                        break;
388
 
394
 
389
                case UBXSTATE_CLASS: // check message identifier
395
                case UBXSTATE_CLASS: // check message identifier
390
                        RxHdr.Id = c;
396
                        RxHdr.Id = c;
391
                        ubxState = UBXSTATE_LEN1;
397
                        ubxState = UBXSTATE_LEN1;
392
                        cka = RxHdr.Class + RxHdr.Id;
398
                        cka = RxHdr.Class + RxHdr.Id;
393
                        ckb = RxHdr.Class + cka;
399
                        ckb = RxHdr.Class + cka;
394
                        break;
400
                        break;
395
 
401
 
396
                case UBXSTATE_LEN1: // 1st message length byte
402
                case UBXSTATE_LEN1: // 1st message length byte
397
                        RxHdr.Length = (u16)c; // lowbyte first
403
                        RxHdr.Length = (u16)c; // lowbyte first
398
                        cka += c;
404
                        cka += c;
399
                        ckb += cka;
405
                        ckb += cka;
400
                        ubxState = UBXSTATE_LEN2;
406
                        ubxState = UBXSTATE_LEN2;
401
                        break;
407
                        break;
402
 
408
 
403
                case UBXSTATE_LEN2: // 2nd message length byte
409
                case UBXSTATE_LEN2: // 2nd message length byte
404
                        RxHdr.Length += ((u16)c)<<8; // high byte last
410
                        RxHdr.Length += ((u16)c)<<8; // high byte last
405
                        if (RxHdr.Length >= UBX_MSG_DATA_SIZE)
411
                        if (RxHdr.Length >= UBX_MSG_DATA_SIZE)
406
                        {
412
                        {
407
                                ubxState = UBXSTATE_IDLE;
413
                                ubxState = UBXSTATE_IDLE;
408
                        }
414
                        }
409
                        else
415
                        else
410
                        {
416
                        {
411
                                cka += c;
417
                                cka += c;
412
                                ckb += cka;
418
                                ckb += cka;
413
                                RxBytes = 0;    // reset data byte counter
419
                                RxBytes = 0;    // reset data byte counter
414
                                ubxState = UBXSTATE_DATA;
420
                                ubxState = UBXSTATE_DATA;
415
                        }
421
                        }
416
                        break;
422
                        break;
417
 
423
 
418
                case UBXSTATE_DATA: // collecting data
424
                case UBXSTATE_DATA: // collecting data
419
                        if (RxBytes < UBX_MSG_DATA_SIZE)
425
                        if (RxBytes < UBX_MSG_DATA_SIZE)
420
                        {
426
                        {
421
                                RxData[RxBytes++] = c; // copy curent data byte if any space is left
427
                                RxData[RxBytes++] = c; // copy curent data byte if any space is left
422
                                cka += c;
428
                                cka += c;
423
                                ckb += cka;
429
                                ckb += cka;
424
                                if (RxBytes >= RxHdr.Length) ubxState = UBXSTATE_CKA; // switch to next state if all data have been received
430
                                if (RxBytes >= RxHdr.Length) ubxState = UBXSTATE_CKA; // switch to next state if all data have been received
425
                        }
431
                        }
426
                        else // rx buffer overrun
432
                        else // rx buffer overrun
427
                        {
433
                        {
428
                                ubxState = UBXSTATE_IDLE;
434
                                ubxState = UBXSTATE_IDLE;
429
                        }
435
                        }
430
                        break;
436
                        break;
431
 
437
 
432
                case UBXSTATE_CKA:
438
                case UBXSTATE_CKA:
433
                        if (c == cka) ubxState = UBXSTATE_CKB;
439
                        if (c == cka) ubxState = UBXSTATE_CKB;
434
                        else
440
                        else
435
                        {
441
                        {
436
                                ubxState = UBXSTATE_IDLE;
442
                                ubxState = UBXSTATE_IDLE;
437
                        }
443
                        }
438
                        break;
444
                        break;
439
 
445
 
440
                case UBXSTATE_CKB:
446
                case UBXSTATE_CKB:
441
                        if (c == ckb)
447
                        if (c == ckb)
442
                        {       // checksum is ok
448
                        {       // checksum is ok
443
 
449
 
444
                                switch(RxHdr.Class)
450
                                switch(RxHdr.Class)
445
                                {
451
                                {
446
                                        case UBX_CLASS_NAV:
452
                                        case UBX_CLASS_NAV:
447
                                                switch(RxHdr.Id)
453
                                                switch(RxHdr.Id)
448
                                                {
454
                                                {
449
                                                        case UBX_ID_POSLLH: // geodetic position
455
                                                        case UBX_ID_POSLLH: // geodetic position
450
                                                                memcpy((u8*)&UbxPosLlh, RxData, RxHdr.Length);
456
                                                                memcpy((u8*)&UbxPosLlh, RxData, RxHdr.Length);
451
                                                                UbxPosLlh.Status = NEWDATA;
457
                                                                UbxPosLlh.Status = NEWDATA;
452
                                                                break;
458
                                                                break;
453
 
459
 
454
                                                        case UBX_ID_VELNED: // velocity vector in tangent plane 
460
                                                        case UBX_ID_VELNED: // velocity vector in tangent plane 
455
                                                                memcpy((u8*)&UbxVelNed, RxData, RxHdr.Length);
461
                                                                memcpy((u8*)&UbxVelNed, RxData, RxHdr.Length);
456
                                                                UbxVelNed.Status = NEWDATA;
462
                                                                UbxVelNed.Status = NEWDATA;
457
                                                                break;
463
                                                                break;
458
 
464
 
459
                                                        case UBX_ID_SOL: // navigation solution
465
                                                        case UBX_ID_SOL: // navigation solution
460
                                                                memcpy((u8*)&UbxSol, RxData, RxHdr.Length);
466
                                                                memcpy((u8*)&UbxSol, RxData, RxHdr.Length);
461
                                                                UbxSol.Status = NEWDATA;
467
                                                                UbxSol.Status = NEWDATA;
462
                                                                break;
468
                                                                break;
463
 
469
 
464
                                                        default:
470
                                                        default:
465
                                                                break;
471
                                                                break;
466
                                                } // EOF switch(Id)
472
                                                } // EOF switch(Id)
467
                                                Update_GPSData();
473
                                                Update_GPSData();
468
                                                break;
474
                                                break;
469
                                       
475
                                       
470
                                        default:
476
                                        default:
471
                                                break;
477
                                                break;
472
                                } // EOF switch(class)
478
                                } // EOF switch(class)
473
 
479
 
474
                                // check generic msg filter
480
                                // check generic msg filter
475
                                if(UbxMsg.Status != NEWDATA)
481
                                if(UbxMsg.Status != NEWDATA)
476
                                {       // msg buffer is free
482
                                {       // msg buffer is free
477
                                        if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (RxHdr.Class&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (RxHdr.Id&UbxMsg.IdMask)))
483
                                        if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (RxHdr.Class&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (RxHdr.Id&UbxMsg.IdMask)))
478
                                        {       // msg matches to the filter criteria
484
                                        {       // msg matches to the filter criteria
479
                                                UbxMsg.Status = INVALID;
485
                                                UbxMsg.Status = INVALID;
480
                                                UbxMsg.Hdr.Class = RxHdr.Class;
486
                                                UbxMsg.Hdr.Class = RxHdr.Class;
481
                                                UbxMsg.Hdr.Id = RxHdr.Id;
487
                                                UbxMsg.Hdr.Id = RxHdr.Id;
482
                                                UbxMsg.Hdr.Length = RxHdr.Length;
488
                                                UbxMsg.Hdr.Length = RxHdr.Length;
483
                                                if(UbxMsg.Hdr.Length <= UBX_MSG_DATA_SIZE)
489
                                                if(UbxMsg.Hdr.Length <= UBX_MSG_DATA_SIZE)
484
                                                {       // copy data block
490
                                                {       // copy data block
485
                                                        memcpy(UbxMsg.Data, RxData, RxHdr.Length);
491
                                                        memcpy(UbxMsg.Data, RxData, RxHdr.Length);
486
                                                        UbxMsg.Status = NEWDATA;
492
                                                        UbxMsg.Status = NEWDATA;
487
                                                }      
493
                                                }      
488
                                        } // EOF filter matches
494
                                        } // EOF filter matches
489
                                } // EOF != INVALID
495
                                } // EOF != INVALID
490
                        }// EOF crc ok
496
                        }// EOF crc ok
491
                        ubxState = UBXSTATE_IDLE; // ready to parse new data
497
                        ubxState = UBXSTATE_IDLE; // ready to parse new data
492
                        break;
498
                        break;
493
 
499
 
494
                default: // unknown ubx state
500
                default: // unknown ubx state
495
                        ubxState = UBXSTATE_IDLE;
501
                        ubxState = UBXSTATE_IDLE;
496
                        break;
502
                        break;
497
 
503
 
498
        }
504
        }
499
}
505
}
500
 
506
 
501
u8 UBX_CreateMsg(Buffer_t* pBuff, u8* pData, u16 Len)
507
u8 UBX_CreateMsg(Buffer_t* pBuff, u8* pData, u16 Len)
502
{
508
{
503
        u16 i;
509
        u16 i;
504
        u8 cka = 0, ckb = 0;
510
        u8 cka = 0, ckb = 0;
505
        // check if buffer is available
511
        // check if buffer is available
506
        if(pBuff->Locked == TRUE) return(0);
512
        if(pBuff->Locked == TRUE) return(0);
507
        // check if buffer size is sufficient
513
        // check if buffer size is sufficient
508
        if(pBuff->Size < 8 + Len) return(0);
514
        if(pBuff->Size < 8 + Len) return(0);
509
        // lock the buffer
515
        // lock the buffer
510
        pBuff->Locked = TRUE;
516
        pBuff->Locked = TRUE;
511
        // start at begin 
517
        // start at begin 
512
        pBuff->Position = 0;
518
        pBuff->Position = 0;
513
        pBuff->pData[pBuff->Position++] = UBX_SYNC1_CHAR;
519
        pBuff->pData[pBuff->Position++] = UBX_SYNC1_CHAR;
514
        pBuff->pData[pBuff->Position++] = UBX_SYNC2_CHAR;
520
        pBuff->pData[pBuff->Position++] = UBX_SYNC2_CHAR;
515
        for(i=0;i<Len;i++)
521
        for(i=0;i<Len;i++)
516
        {
522
        {
517
                pBuff->pData[pBuff->Position++] = pData[i];
523
                pBuff->pData[pBuff->Position++] = pData[i];
518
        }
524
        }
519
        // calculate checksum
525
        // calculate checksum
520
        for(i=2;i<pBuff->Position;i++)
526
        for(i=2;i<pBuff->Position;i++)
521
        {
527
        {
522
                cka += pBuff->pData[i];
528
                cka += pBuff->pData[i];
523
                ckb += cka;
529
                ckb += cka;
524
        }
530
        }
525
        pBuff->pData[pBuff->Position++] = cka;
531
        pBuff->pData[pBuff->Position++] = cka;
526
        pBuff->pData[pBuff->Position++] = ckb;
532
        pBuff->pData[pBuff->Position++] = ckb;
527
        pBuff->DataBytes = pBuff->Position;
533
        pBuff->DataBytes = pBuff->Position;
528
        pBuff->Position = 0;  // reset buffer position for transmision
534
        pBuff->Position = 0;  // reset buffer position for transmision
529
        return(1);
535
        return(1);
530
}
536
}
531
/*
537
/*
532
switch(ubxclass)
538
switch(ubxclass)
533
                        {
539
                        {
534
                                case UBX_CLASS_NAV:
540
                                case UBX_CLASS_NAV:
535
                                        switch(ubxid)
541
                                        switch(ubxid)
536
                                        {
542
                                        {
537
                                                case UBX_ID_POSLLH: // geodetic position
543
                                                case UBX_ID_POSLLH: // geodetic position
538
                                                        ubxSp =  (u8 *)&UbxPosLlh; // data start pointer
544
                                                        ubxSp =  (u8 *)&UbxPosLlh; // data start pointer
539
                                                        ubxEp = (u8 *)(&UbxPosLlh + 1); // data end pointer
545
                                                        ubxEp = (u8 *)(&UbxPosLlh + 1); // data end pointer
540
                                                        ubxStP = (u8 *)&UbxPosLlh.Status; // status pointer
546
                                                        ubxStP = (u8 *)&UbxPosLlh.Status; // status pointer
541
                                                        break;
547
                                                        break;
542
               
548
               
543
                                                case UBX_ID_SOL: // navigation solution
549
                                                case UBX_ID_SOL: // navigation solution
544
                                                        ubxSp =  (u8 *)&UbxSol; // data start pointer
550
                                                        ubxSp =  (u8 *)&UbxSol; // data start pointer
545
                                                        ubxEp = (u8 *)(&UbxSol + 1); // data end pointer
551
                                                        ubxEp = (u8 *)(&UbxSol + 1); // data end pointer
546
                                                        ubxStP = (u8 *)&UbxSol.Status; // status pointer
552
                                                        ubxStP = (u8 *)&UbxSol.Status; // status pointer
547
                                                        break;
553
                                                        break;
548
               
554
               
549
                                                case UBX_ID_VELNED: // velocity vector in tangent plane
555
                                                case UBX_ID_VELNED: // velocity vector in tangent plane
550
                                                        ubxSp =  (u8 *)&UbxVelNed; // data start pointer
556
                                                        ubxSp =  (u8 *)&UbxVelNed; // data start pointer
551
                                                        ubxEp = (u8 *)(&UbxVelNed + 1); // data end pointer
557
                                                        ubxEp = (u8 *)(&UbxVelNed + 1); // data end pointer
552
                                                        ubxStP = (u8 *)&UbxVelNed.Status; // status pointer
558
                                                        ubxStP = (u8 *)&UbxVelNed.Status; // status pointer
553
                                                        break;
559
                                                        break;
554
               
560
               
555
                                                default:                        // unsupported identifier
561
                                                default:                        // unsupported identifier
556
                                                        ubxState = UBXSTATE_IDLE;
562
                                                        ubxState = UBXSTATE_IDLE;
557
                                                        return;
563
                                                        return;
558
                                        }
564
                                        }
559
                                        break;
565
                                        break;
560
 
566
 
561
                                default: // other  classes
567
                                default: // other  classes
562
                                        if(UbxMsg.Status == NEWDATA) ubxState = UBXSTATE_IDLE;
568
                                        if(UbxMsg.Status == NEWDATA) ubxState = UBXSTATE_IDLE;
563
                                        else if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (ubxclass&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (ubxid&UbxMsg.IdMask)))
569
                                        else if(((UbxMsg.Hdr.Class&UbxMsg.ClassMask) == (ubxclass&UbxMsg.ClassMask)) && ((UbxMsg.Hdr.Id&UbxMsg.IdMask) == (ubxid&UbxMsg.IdMask)))
564
                                        {       // buffer is free and message matches to filter criteria
570
                                        {       // buffer is free and message matches to filter criteria
565
                                                UbxMsg.Status = INVALID;
571
                                                UbxMsg.Status = INVALID;
566
                                                UbxMsg.Hdr.Class = ubxclass;
572
                                                UbxMsg.Hdr.Class = ubxclass;
567
                                                UbxMsg.Hdr.Id = ubxid;
573
                                                UbxMsg.Hdr.Id = ubxid;
568
                                                UbxMsg.Hdr.Length = msglen;
574
                                                UbxMsg.Hdr.Length = msglen;
569
                                                ubxSp =  (u8 *)&(UbxMsg.Data);   // data start pointer
575
                                                ubxSp =  (u8 *)&(UbxMsg.Data);   // data start pointer
570
                                                ubxEp = (u8 *)(&UbxMsg + 1);    // data end pointer
576
                                                ubxEp = (u8 *)(&UbxMsg + 1);    // data end pointer
571
                                                ubxStP = (u8 *)&UbxMsg.Status;  // status pointer      
577
                                                ubxStP = (u8 *)&UbxMsg.Status;  // status pointer      
572
                                        }
578
                                        }
573
                                        else ubxState = UBXSTATE_IDLE;
579
                                        else ubxState = UBXSTATE_IDLE;
574
                                        break;
580
                                        break;
575
                        }
581
                        }
576
*/
582
*/
577
 
583
 
578
 
584