Loading...
Searching...
No Matches
gpsacquisition.c
1/* ===================================================================== *
2 * GPS *
3 * ===================================================================== */
4
5#include "gpsacquisition.h"
6
7#include "packets.h"
8
9extern MessageBufferHandle_t xLoRaTxBuff;
10extern MessageBufferHandle_t xUsbTxBuff;
11extern StreamBufferHandle_t xGpsRxBuff;
12extern SemaphoreHandle_t xUsbMutex;
13
14uint8_t gpsRxBuff[GPS_RX_SIZE];
15uint8_t gpsRxBuffIdx = 0;
16
17void vGpsTransmit(void *argument) {
18 const TickType_t xFrequency = pdMS_TO_TICKS(500);
19 const TickType_t blockTime = pdMS_TO_TICKS(250);
20 char gpsString[100];
21
22 SAM_M10Q_t *gps = DeviceList_getDeviceHandle(DEVICE_GPS).device;
23 UART_t *usb = DeviceList_getDeviceHandle(DEVICE_UART_USB).device;
24 enum State *flightState = StateHandle_getHandle("FlightState").state;
25
26 for (;;) {
27 // Block until 500ms interval
28 TickType_t xLastWakeTime = xTaskGetTickCount();
29 vTaskDelayUntil(&xLastWakeTime, xFrequency);
30
31 // Send GPS poll message
32 gps->base.print(&gps->base, GPS_PUBX_POLL);
33
34 // Read string from UART Rx buffer, skip loop if empty
35 if (!xStreamBufferReceive(xGpsRxBuff, (void *)&gpsString, gpsRxBuffIdx, blockTime))
36 continue;
37
38 struct GPS_Data gpsData;
39 gps->decode(gps, gpsString, &gpsData);
40 usb->print(usb, gpsString);
41 gpsRxBuffIdx = 0;
42
43 #ifdef DEBUG
46 if ((xSemaphoreTake(xUsbMutex, pdMS_TO_TICKS(0))) == pdTRUE) {
47 char debugStr[100];
48 snprintf(debugStr, 100, "[GPS] %d:%d:%d\n\r", gpsData.hour, gpsData.minute, gpsData.second);
49 xMessageBufferSend(xUsbTxBuff, (void *)debugStr, 100, 0);
50 xSemaphoreGive(xUsbMutex);
51 }
52 #endif
53
54 SX1272_Packet gpsPacket = SX1272_GPSData(
55 LORA_HEADER_GPS_DATA,
56 gpsData.latitude,
57 gpsData.longitude,
58 (*flightState << 4) | gpsData.lock
59 );
60 // Add packet to queue
61 // TODO: this needs to be refactored to not use the same buffer, or
62 // alternatively (probably better) refactor LoRa task to use
63 // a queue instead of a buffer to allow multiple writers.
64 xMessageBufferSend(xLoRaTxBuff, &gpsPacket, LORA_MSG_LENGTH, blockTime);
65 }
66}
67
68/* =============================================================================== */
77void USART3_IRQHandler() {
78 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
79
80 // Read in data from USART3
81 while ((USART3->SR & USART_SR_RXNE) == 0);
82 uint8_t rxData = USART3->DR & 0xFF;
83
84 //
85 gpsRxBuff[gpsRxBuffIdx++] = rxData;
86 gpsRxBuffIdx %= GPS_RX_SIZE;
87
88 // Send message to buffer on carriage return
89 if (rxData == LINE_FEED) {
90 xStreamBufferSendFromISR(xGpsRxBuff, (void *)gpsRxBuff, gpsRxBuffIdx, &xHigherPriorityTaskWoken);
91 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
92 }
93}
DeviceHandle_t DeviceList_getDeviceHandle(DeviceKey)
Retrieve device handle from list by key.
Definition devicelist.c:36
#define USART_SR_RXNE
void(* print)(struct UART *, char *)
UART print string method.
Definition uart.h:61
Struct definition for UART interface.
Definition uart.h:52