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
|
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 Matthias P. Braendli, Maximilien Cuony
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <inttypes.h>
#include "Core/common.h"
#include "GPIO/usart.h"
#include "GPIO/analog.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
// The ISRs write into these buffer, for NMEA input and Coulomb counter
static char nmea_sentence[MAX_NMEA_SENTENCE_LEN];
static int nmea_sentence_last_written = 0;
static char ccounter_msg[MAX_CCOUNTER_SENTENCE_LEN];
static int ccounter_msg_last_written = 0;
// Once a completed NMEA sentence is received in the ISR,
// it is appended to this queue
static QueueHandle_t usart_nmea_queue;
// Once a full line (ending in \r\n) is received on USART2,
// it is appended to this queue
static QueueHandle_t usart_ccounter_queue;
void usart_gps_init() {
usart_nmea_queue = xQueueCreate(15, MAX_NMEA_SENTENCE_LEN);
if (usart_nmea_queue == 0) {
while(1); /* fatal error */
}
usart_ccounter_queue = xQueueCreate(5, MAX_CCOUNTER_SENTENCE_LEN);
if (usart_ccounter_queue == 0) {
while(1); /* fatal error */
}
usart_gps_specific_init();
}
void usart_gps_puts(const char* str) {
vTaskSuspendAll();
usart_puts(USART3, str);
xTaskResumeAll();
}
#define MAX_MSG_LEN 80
static char usart_debug_message[MAX_MSG_LEN];
void usart_debug_timestamp() {
// Don't call printf here, to reduce stack usage
uint64_t now = timestamp_now();
if (now == 0) {
usart_puts(USART2, "[0] ");
}
else {
char ts_str[64];
int i = 63;
ts_str[i--] = '\0';
ts_str[i--] = ' ';
ts_str[i--] = ']';
while (now > 0 && i >= 0) {
ts_str[i--] = '0' + (now % 10);
now /= 10;
}
ts_str[i] = '[';
usart_puts(USART2, &ts_str[i]);
}
}
void usart_debug(const char *format, ...) {
va_list list;
va_start(list, format);
vsnprintf(usart_debug_message, MAX_MSG_LEN-1, format, list);
vTaskSuspendAll();
usart_debug_timestamp();
usart_puts(USART2, usart_debug_message);
xTaskResumeAll();
va_end(list);
}
void usart_debug_puts(const char* str) {
#ifdef SIMULATOR
fprintf(stderr, "DEBUG: %s", str);
#endif
vTaskSuspendAll();
usart_debug_timestamp();
usart_puts(USART2, str);
xTaskResumeAll();
}
void usart_debug_puts_header(const char* hdr, const char* str) {
vTaskSuspendAll();
usart_debug_timestamp();
usart_puts(USART2, hdr);
usart_puts(USART2, str);
usart_puts(USART2, "\r\n");
xTaskResumeAll();
}
int usart_get_nmea_sentence(char *nmea) {
return xQueueReceive(usart_nmea_queue, nmea, portMAX_DELAY);
}
int usart_get_ccounter_msg(char *msg) {
return xQueueReceive(usart_ccounter_queue, msg, pdMS_TO_TICKS(2000));
}
static void usart_clear_ccounter_buffer(void) {
for (int i = 0; i < MAX_CCOUNTER_SENTENCE_LEN; i++) {
ccounter_msg[i] = '\0';
}
ccounter_msg_last_written = 0;
}
void usart_process_char(char c) {
// Warning: running in interrupt context
BaseType_t require_context_switch = pdFALSE;
if (ccounter_msg_last_written < MAX_CCOUNTER_SENTENCE_LEN) {
ccounter_msg[ccounter_msg_last_written] = c;
ccounter_msg_last_written++;
if (c == '\n') {
int success = xQueueSendToBackFromISR(
usart_ccounter_queue,
ccounter_msg,
&require_context_switch);
if (success == pdFALSE) {
trigger_fault(FAULT_SOURCE_USART);
}
usart_clear_ccounter_buffer();
}
}
else {
// Buffer overrun without a meaningful message.
usart_clear_ccounter_buffer();
}
portYIELD_FROM_ISR(require_context_switch);
}
static void usart_clear_nmea_buffer(void) {
for (int i = 0; i < MAX_NMEA_SENTENCE_LEN; i++) {
nmea_sentence[i] = '\0';
}
nmea_sentence_last_written = 0;
}
void usart_gps_process_char(char c)
{
// Warning: running in interrupt context
BaseType_t require_context_switch = pdFALSE;
if (nmea_sentence_last_written == 0) {
if (c == '$') {
// Likely new start of sentence
nmea_sentence[nmea_sentence_last_written] = c;
nmea_sentence_last_written++;
}
}
else if (nmea_sentence_last_written < MAX_NMEA_SENTENCE_LEN) {
nmea_sentence[nmea_sentence_last_written] = c;
nmea_sentence_last_written++;
if (c == '\n') {
int success = xQueueSendToBackFromISR(
usart_nmea_queue,
nmea_sentence,
&require_context_switch);
if (success == pdFALSE) {
trigger_fault(FAULT_SOURCE_USART);
}
usart_clear_nmea_buffer();
}
}
else {
// Buffer overrun without a meaningful NMEA message.
usart_clear_nmea_buffer();
}
portYIELD_FROM_ISR(require_context_switch);
}
|