forked from yanosik/STM32_RTTY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ublox.c
179 lines (155 loc) · 6.31 KB
/
ublox.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// Created by SQ5RWU on 2016-12-27.
//
#include <stm32f10x_usart.h>
#include <string.h>
#include "ublox.h"
#include "delay.h"
#include "init.h"
GPSEntry currentGPSData;
volatile uint8_t active = 0;
void _sendSerialByte(uint8_t message) {
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {
}
USART_SendData(USART1, message);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {
}
}
void send_ublox(uint8_t msgClass, uint8_t msgId, uint8_t *payload, uint16_t payloadSize) {
uBloxChecksum chksum = ublox_calc_checksum(msgClass, msgId, payload, payloadSize);
_sendSerialByte(0xB5);
_sendSerialByte(0x62);
_sendSerialByte(msgClass);
_sendSerialByte(msgId);
_sendSerialByte((uint8_t) (payloadSize & 0xff));
_sendSerialByte((uint8_t) (payloadSize >> 8));
uint16_t i;
for (i = 0; i < payloadSize; ++i) {
_sendSerialByte(payload[i]);
}
_sendSerialByte(chksum.ck_a);
_sendSerialByte(chksum.ck_b);
}
void send_ublox_packet(uBloxPacket * packet){
send_ublox(packet->header.messageClass, packet->header.messageId, (uint8_t*)&packet->data, packet->header.payloadSize);
}
uBloxChecksum ublox_calc_checksum(const uint8_t msgClass, const uint8_t msgId, const uint8_t *message, uint16_t size) {
uBloxChecksum ck = {0, 0};
uint8_t i;
ck.ck_a += msgClass;
ck.ck_b += ck.ck_a;
ck.ck_a += msgId;
ck.ck_b += ck.ck_a;
ck.ck_a += size & 0xff;
ck.ck_b += ck.ck_a;
ck.ck_a += size >> 8;
ck.ck_b += ck.ck_a;
for (i =0;i<size;i++){
ck.ck_a += message[i];
ck.ck_b += ck.ck_a;
}
return ck;
}
void ublox_get_last_data(GPSEntry * gpsEntry){
__disable_irq();
memcpy(gpsEntry, ¤tGPSData, sizeof(GPSEntry));
__enable_irq();
}
void ublox_init(){
uBloxPacket msgcfgrst = {.header = {0xb5, 0x62, .messageClass=0x06, .messageId=0x04, .payloadSize=sizeof(uBloxCFGRSTPayload)},
.data.cfgrst = { .navBbrMask=0xffff, .resetMode=1, .reserved1 = 0}
};
send_ublox_packet(&msgcfgrst);
_delay_ms(800);
uBloxPacket msgcgprt = {.header = {0xb5, 0x62, .messageClass=0x06, .messageId=0x00, .payloadSize=sizeof(uBloxCFGPRTPayload)},
.data.cfgprt = {.portID=1, .reserved1=0, .txReady=0, .mode=0b00100011000000, .baudRate=38400,
.inProtoMask=1, .outProtoMask=1, .flags=0, .reserved2={0,0}}};
send_ublox_packet(&msgcgprt);
init_usart_gps(38400, 1);
_delay_ms(10);
uBloxPacket msgcfgmsg = {.header = {0xb5, 0x62, .messageClass=0x06, .messageId=0x01, .payloadSize=sizeof(uBloxCFGMSGPayload)},
.data.cfgmsg = {.msgClass=0x01, .msgID=0x02, .rate=1}};
send_ublox_packet(&msgcfgmsg);
_delay_ms(10);
uBloxPacket msgcfgrxm = {.header = {0xb5, 0x62, .messageClass=0x06, .messageId=0x11, .payloadSize=sizeof(uBloxCFGRXMPayload)},
.data.cfgrxm = {.reserved1=8, .lpMode=4}};
send_ublox_packet(&msgcfgrxm);
_delay_ms(10);
msgcfgmsg.data.cfgmsg.msgID = 0x6;
send_ublox_packet(&msgcfgmsg);
_delay_ms(10);
msgcfgmsg.data.cfgmsg.msgID = 0x21;
send_ublox_packet(&msgcfgmsg);
_delay_ms(10);
uBloxPacket msgcfgnav5 = {.header = {0xb5, 0x62, .messageClass=0x06, .messageId=0x24, .payloadSize=sizeof(uBloxCFGNAV5Payload)},
.data.cfgnav5={.mask=0b00000001111111111, .dynModel=8, .fixMode=3, .fixedAlt=0, .fixedAltVar=10000, .minElev=15, .drLimit=0, .pDop=60, .tDop=60,
.pAcc=100, .tAcc=300, .staticHoldThresh=0, .dgpsTimeOut=2, .reserved2=0, .reserved3=0, .reserved4=0}};
send_ublox_packet(&msgcfgnav5);
_delay_ms(10);
}
void ublox_handle_incoming_byte(uint8_t data){
static uint8_t sync = 0;
static uint8_t buffer_pos = 0;
static uint8_t incoming_packet_buffer[sizeof(uBloxPacket) + sizeof(uBloxChecksum)];
static uBloxPacket * incoming_packet = (uBloxPacket *) incoming_packet_buffer;
if (!sync){
if (!buffer_pos && data == 0xB5){
buffer_pos = 1;
incoming_packet->header.sc1 = data;
} else if (buffer_pos == 1 && data == 0x62){
sync = 1;
buffer_pos = 2;
incoming_packet->header.sc2 = data;
} else {
buffer_pos = 0;
}
} else {
((uint8_t *)incoming_packet)[buffer_pos] = data;
if ((buffer_pos >= sizeof(uBloxHeader)-1) && (buffer_pos-1 == (incoming_packet->header.payloadSize + sizeof(uBloxHeader) + sizeof(uBloxChecksum)))){
ublox_handle_packet((uBloxPacket *) incoming_packet);
buffer_pos = 0;
sync = 0;
} else {
buffer_pos++;
if (buffer_pos >= sizeof(uBloxPacket) + sizeof(uBloxChecksum)) {
buffer_pos = 0;
sync = 0;
}
}
}
}
void ublox_handle_packet(uBloxPacket *pkt) {
uBloxChecksum cksum = ublox_calc_checksum(pkt->header.messageClass, pkt->header.messageId, (const uint8_t *) &pkt->data, pkt->header.payloadSize);
uBloxChecksum *checksum = (uBloxChecksum *)(((uint8_t*)&pkt->data) + pkt->header.payloadSize);
if (cksum.ck_a != checksum->ck_a || cksum.ck_b != checksum->ck_b) {
currentGPSData.bad_packets += 1;
} else {
currentGPSData.ok_packets += 1;
if (pkt->header.messageClass == 0x01 && pkt->header.messageId == 0x07){
currentGPSData.fix = pkt->data.navpvt.fixType;
currentGPSData.lat_raw = pkt->data.navpvt.lat;
currentGPSData.lon_raw = pkt->data.navpvt.lon;
currentGPSData.alt_raw = pkt->data.navpvt.hMSL;
currentGPSData.hours = pkt->data.navpvt.hour;
currentGPSData.minutes = pkt->data.navpvt.min;
currentGPSData.seconds = pkt->data.navpvt.sec;
currentGPSData.sats_raw = pkt->data.navpvt.numSV;
currentGPSData.speed_raw = pkt->data.navpvt.gSpeed;
} else if (pkt->header.messageClass == 0x01 && pkt->header.messageId == 0x02){
currentGPSData.lat_raw = pkt->data.navposllh.lat;
currentGPSData.lon_raw = pkt->data.navposllh.lon;
currentGPSData.alt_raw = pkt->data.navposllh.hMSL;
} else if (pkt->header.messageClass == 0x01 && pkt->header.messageId == 0x06){
currentGPSData.fix = pkt->data.navsol.gpsFix;
currentGPSData.sats_raw = pkt->data.navsol.numSV;
} else if (pkt->header.messageClass == 0x01 && pkt->header.messageId == 0x21){
currentGPSData.hours = pkt->data.navtimeutc.hour;
currentGPSData.minutes = pkt->data.navtimeutc.min;
currentGPSData.seconds = pkt->data.navtimeutc.sec;
} else if (pkt->header.messageClass == 0x05 && pkt->header.messageId == 0x01){
currentGPSData.fix += 1;
} else if (pkt->header.messageClass == 0x05 && pkt->header.messageId == 0x00){
currentGPSData.fix += 30;
}
}
}