Subversion Repositories NaviCtrl

Rev

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

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