-
Notifications
You must be signed in to change notification settings - Fork 28
/
gps_uart.c
221 lines (182 loc) · 8.29 KB
/
gps_uart.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <string.h>
#include <minmea.h>
#include "gps_uart.h"
typedef enum {
WorkerEvtStop = (1 << 0),
WorkerEvtRxDone = (1 << 1),
} WorkerEvtFlags;
#define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)
static void gps_uart_on_irq_cb(FuriHalSerialHandle* handle, FuriHalSerialRxEvent ev, void* context) {
GpsUart* gps_uart = (GpsUart*)context;
if(ev == FuriHalSerialRxEventData) {
uint8_t data = furi_hal_serial_async_rx(handle);
furi_stream_buffer_send(gps_uart->rx_stream, &data, 1, 0);
furi_thread_flags_set(furi_thread_get_id(gps_uart->thread), WorkerEvtRxDone);
}
}
static void gps_uart_serial_init(GpsUart* gps_uart) {
furi_assert(!gps_uart->serial_handle);
gps_uart->serial_handle = furi_hal_serial_control_acquire(UART_CH);
furi_assert(gps_uart->serial_handle);
furi_hal_serial_init(gps_uart->serial_handle, gps_uart->baudrate);
furi_hal_serial_async_rx_start(gps_uart->serial_handle, gps_uart_on_irq_cb, gps_uart, false);
}
static void gps_uart_serial_deinit(GpsUart* gps_uart) {
furi_assert(gps_uart->serial_handle);
furi_hal_serial_deinit(gps_uart->serial_handle);
furi_hal_serial_control_release(gps_uart->serial_handle);
gps_uart->serial_handle = NULL;
}
static void gps_uart_parse_nmea(GpsUart* gps_uart, char* line) {
switch(minmea_sentence_id(line, false)) {
case MINMEA_SENTENCE_RMC: {
struct minmea_sentence_rmc frame;
if(minmea_parse_rmc(&frame, line)) {
gps_uart->status.valid = frame.valid;
gps_uart->status.latitude = minmea_tocoord(&frame.latitude);
gps_uart->status.longitude = minmea_tocoord(&frame.longitude);
gps_uart->status.speed = minmea_tofloat(&frame.speed);
gps_uart->status.course = minmea_tofloat(&frame.course);
gps_uart->status.time_hours = frame.time.hours;
gps_uart->status.time_minutes = frame.time.minutes;
gps_uart->status.time_seconds = frame.time.seconds;
notification_message_block(gps_uart->notifications, &sequence_blink_green_10);
}
} break;
case MINMEA_SENTENCE_GGA: {
struct minmea_sentence_gga frame;
if(minmea_parse_gga(&frame, line)) {
gps_uart->status.latitude = minmea_tocoord(&frame.latitude);
gps_uart->status.longitude = minmea_tocoord(&frame.longitude);
gps_uart->status.altitude = minmea_tofloat(&frame.altitude);
gps_uart->status.altitude_units = frame.altitude_units;
gps_uart->status.fix_quality = frame.fix_quality;
gps_uart->status.satellites_tracked = frame.satellites_tracked;
gps_uart->status.time_hours = frame.time.hours;
gps_uart->status.time_minutes = frame.time.minutes;
gps_uart->status.time_seconds = frame.time.seconds;
notification_message_block(gps_uart->notifications, &sequence_blink_magenta_10);
}
} break;
case MINMEA_SENTENCE_GLL: {
struct minmea_sentence_gll frame;
if(minmea_parse_gll(&frame, line)) {
gps_uart->status.latitude = minmea_tocoord(&frame.latitude);
gps_uart->status.longitude = minmea_tocoord(&frame.longitude);
gps_uart->status.time_hours = frame.time.hours;
gps_uart->status.time_minutes = frame.time.minutes;
gps_uart->status.time_seconds = frame.time.seconds;
notification_message_block(gps_uart->notifications, &sequence_blink_red_10);
}
} break;
default:
break;
}
}
static int32_t gps_uart_worker(void* context) {
GpsUart* gps_uart = (GpsUart*)context;
size_t rx_offset = 0;
while(1) {
uint32_t events =
furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
furi_check((events & FuriFlagError) == 0);
if(events & WorkerEvtStop) {
break;
}
if(events & WorkerEvtRxDone) {
size_t len = 0;
do {
// receive serial bytes into rx_buf, starting at rx_offset from the start of the buffer
// the maximum we can receive is RX_BUF_SIZE - 1 - rx_offset
len = furi_stream_buffer_receive(gps_uart->rx_stream, gps_uart->rx_buf + rx_offset, RX_BUF_SIZE - 1 - rx_offset, 0);
if(len > 0) {
// increase rx_offset by the number of bytes received, and null-terminate rx_buf
rx_offset += len;
gps_uart->rx_buf[rx_offset] = '\0';
// look for strings ending in newlines, starting at the start of rx_buf
char* line_current = (char*)gps_uart->rx_buf;
while(1) {
// skip null characters
while(*line_current == '\0' &&
line_current < (char*)gps_uart->rx_buf + rx_offset - 1) {
line_current++;
}
// find the next newline
char* newline = strchr(line_current, '\n');
if(newline) // newline found
{
// put a null terminator in place of the newline, to delimit the line string
*newline = '\0';
// attempt to parse the line as a NMEA sentence
gps_uart_parse_nmea(gps_uart, line_current);
// move the cursor to the character after the newline
line_current = newline + 1;
} else // no more newlines found
{
if(line_current >
(char*)gps_uart->rx_buf) // at least one line was found
{
// clear parsed lines, and move any leftover bytes to the start of rx_buf
rx_offset = 0;
while(
*line_current) // stop when the original rx_offset terminator is reached
{
gps_uart->rx_buf[rx_offset++] = *(line_current++);
}
}
break; // go back to receiving bytes from the serial stream
}
}
}
} while(len > 0);
}
}
gps_uart_serial_deinit(gps_uart);
furi_stream_buffer_free(gps_uart->rx_stream);
return 0;
}
void gps_uart_init_thread(GpsUart* gps_uart) {
furi_assert(gps_uart);
gps_uart->status.valid = false;
gps_uart->status.latitude = 0.0;
gps_uart->status.longitude = 0.0;
gps_uart->status.speed = 0.0;
gps_uart->status.course = 0.0;
gps_uart->status.altitude = 0.0;
gps_uart->status.altitude_units = ' ';
gps_uart->status.fix_quality = 0;
gps_uart->status.satellites_tracked = 0;
gps_uart->status.time_hours = 0;
gps_uart->status.time_minutes = 0;
gps_uart->status.time_seconds = 0;
gps_uart->rx_stream = furi_stream_buffer_alloc(RX_BUF_SIZE * 5, 1);
gps_uart->thread = furi_thread_alloc();
furi_thread_set_name(gps_uart->thread, "GpsUartWorker");
furi_thread_set_stack_size(gps_uart->thread, 1024);
furi_thread_set_context(gps_uart->thread, gps_uart);
furi_thread_set_callback(gps_uart->thread, gps_uart_worker);
furi_thread_start(gps_uart->thread);
gps_uart_serial_init(gps_uart);
}
void gps_uart_deinit_thread(GpsUart* gps_uart) {
furi_assert(gps_uart);
furi_thread_flags_set(furi_thread_get_id(gps_uart->thread), WorkerEvtStop);
furi_thread_join(gps_uart->thread);
furi_thread_free(gps_uart->thread);
}
GpsUart* gps_uart_enable() {
GpsUart* gps_uart = malloc(sizeof(GpsUart));
gps_uart->notifications = furi_record_open(RECORD_NOTIFICATION);
gps_uart->baudrate = gps_baudrates[current_gps_baudrate];
gps_uart->changing_baudrate = false;
gps_uart->backlight_on = false;
gps_uart->speed_units = KNOTS;
gps_uart_init_thread(gps_uart);
return gps_uart;
}
void gps_uart_disable(GpsUart* gps_uart) {
furi_assert(gps_uart);
gps_uart_deinit_thread(gps_uart);
furi_record_close(RECORD_NOTIFICATION);
free(gps_uart);
}