Various fixes around Companion trainer mode (#7116)
[opentx.git] / radio / src / targets / horus / gps_driver.cpp
blob7a5e464c4df0a12962c7ae79d1cfdb32c0ef742e
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "opentx.h"
23 #if GPS_USART_BAUDRATE > 9600
24 Fifo<uint8_t, 256> gpsRxFifo;
25 #else
26 Fifo<uint8_t, 64> gpsRxFifo;
27 #endif
29 void gpsInit(uint32_t baudrate)
31 GPIO_InitTypeDef GPIO_InitStructure;
32 USART_InitTypeDef USART_InitStructure;
33 NVIC_InitTypeDef NVIC_InitStructure;
35 // RX and TX pins
36 GPIO_PinAFConfig(GPIOA, GPS_TX_GPIO_PinSource, GPS_GPIO_AF);
37 GPIO_PinAFConfig(GPIOA, GPS_RX_GPIO_PinSource, GPS_GPIO_AF);
38 GPIO_InitStructure.GPIO_Pin = (GPS_TX_GPIO_PIN | GPS_RX_GPIO_PIN);
39 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
40 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
41 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
42 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
43 GPIO_Init(GPS_UART_GPIO, &GPIO_InitStructure);
45 // UART config
46 USART_InitStructure.USART_BaudRate = baudrate;
47 USART_InitStructure.USART_Parity = USART_Parity_No;
48 USART_InitStructure.USART_StopBits = USART_StopBits_1;
49 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
50 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
51 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
52 USART_Init(GPS_USART, &USART_InitStructure);
53 USART_Cmd(GPS_USART, ENABLE);
54 USART_ITConfig(GPS_USART, USART_IT_RXNE, ENABLE);
56 NVIC_InitStructure.NVIC_IRQChannel = GPS_USART_IRQn;
57 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x9;
58 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
59 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
60 NVIC_Init(&NVIC_InitStructure);
63 #if defined(DEBUG)
64 uint8_t gpsTraceEnabled = false;
65 #endif
67 Fifo<uint8_t, 64> gpsTxFifo;
69 void gpsSendByte(uint8_t byte)
71 while (gpsTxFifo.isFull());
72 gpsTxFifo.push(byte);
73 USART_ITConfig(GPS_USART, USART_IT_TXE, ENABLE);
76 extern "C" void GPS_USART_IRQHandler(void)
78 // Send
79 if (USART_GetITStatus(GPS_USART, USART_IT_TXE) != RESET) {
80 uint8_t txchar;
81 if (gpsTxFifo.pop(txchar)) {
82 /* Write one byte to the transmit data register */
83 USART_SendData(GPS_USART, txchar);
85 else {
86 USART_ITConfig(GPS_USART, USART_IT_TXE, DISABLE);
90 // Receive
91 uint32_t status = GPS_USART->SR;
92 while (status & (USART_FLAG_RXNE | USART_FLAG_ERRORS)) {
93 uint8_t data = GPS_USART->DR;
94 if (!(status & USART_FLAG_ERRORS)) {
95 gpsRxFifo.push(data);
97 status = GPS_USART->SR;
101 uint8_t gpsGetByte(uint8_t * byte)
103 uint8_t result = gpsRxFifo.pop(*byte);
104 #if defined(DEBUG)
105 if (gpsTraceEnabled) {
106 serialPutc(*byte);
108 #endif
109 return result;