First cut at polymorphic serial port implementation. Split serialPort_t into uartPort_t and serialPort_t. Calls to uartWrite() can now be replaced with calls to serialWrite().
This commit is contained in:
parent
c950dbea09
commit
6425877b2c
|
@ -126,7 +126,7 @@ typedef void (* sensorInitFuncPtr)(sensor_align_e align); // sensor init proto
|
||||||
typedef void (* sensorReadFuncPtr)(int16_t *data); // sensor read and align prototype
|
typedef void (* sensorReadFuncPtr)(int16_t *data); // sensor read and align prototype
|
||||||
typedef void (* baroOpFuncPtr)(void); // baro start operation
|
typedef void (* baroOpFuncPtr)(void); // baro start operation
|
||||||
typedef void (* baroCalculateFuncPtr)(int32_t *pressure, int32_t *temperature); // baro calculation (filled params are pressure and temperature)
|
typedef void (* baroCalculateFuncPtr)(int32_t *pressure, int32_t *temperature); // baro calculation (filled params are pressure and temperature)
|
||||||
typedef void (* uartReceiveCallbackPtr)(uint16_t data); // used by uart2 driver to return frames to app
|
typedef void (* serialReceiveCallbackPtr)(uint16_t data); // used by serial drivers to return frames to app
|
||||||
typedef uint16_t (* rcReadRawDataPtr)(uint8_t chan); // used by receiver driver to return channel data
|
typedef uint16_t (* rcReadRawDataPtr)(uint8_t chan); // used by receiver driver to return channel data
|
||||||
typedef void (* pidControllerFuncPtr)(void); // pid controller function prototype
|
typedef void (* pidControllerFuncPtr)(void); // pid controller function prototype
|
||||||
|
|
||||||
|
@ -244,6 +244,7 @@ typedef struct baro_t
|
||||||
#include "drv_l3g4200d.h"
|
#include "drv_l3g4200d.h"
|
||||||
#include "drv_pwm.h"
|
#include "drv_pwm.h"
|
||||||
#include "drv_timer.h"
|
#include "drv_timer.h"
|
||||||
|
#include "drv_serial.h"
|
||||||
#include "drv_uart.h"
|
#include "drv_uart.h"
|
||||||
#include "drv_softserial.h"
|
#include "drv_softserial.h"
|
||||||
#else
|
#else
|
||||||
|
@ -264,6 +265,7 @@ typedef struct baro_t
|
||||||
#include "drv_l3g4200d.h"
|
#include "drv_l3g4200d.h"
|
||||||
#include "drv_pwm.h"
|
#include "drv_pwm.h"
|
||||||
#include "drv_timer.h"
|
#include "drv_timer.h"
|
||||||
|
#include "drv_serial.h"
|
||||||
#include "drv_uart.h"
|
#include "drv_uart.h"
|
||||||
#include "drv_softserial.h"
|
#include "drv_softserial.h"
|
||||||
#include "drv_hcsr04.h"
|
#include "drv_hcsr04.h"
|
||||||
|
|
|
@ -931,8 +931,8 @@ void cliProcess(void)
|
||||||
cliPrompt();
|
cliPrompt();
|
||||||
}
|
}
|
||||||
|
|
||||||
while (isUartAvailable(core.mainport)) {
|
while (isUartAvailable((uartPort_t *)core.mainport)) {
|
||||||
uint8_t c = uartRead(core.mainport);
|
uint8_t c = uartRead((uartPort_t *)core.mainport);
|
||||||
if (c == '\t' || c == '?') {
|
if (c == '\t' || c == '?') {
|
||||||
// do tab completion
|
// do tab completion
|
||||||
const clicmd_t *cmd, *pstart = NULL, *pend = NULL;
|
const clicmd_t *cmd, *pstart = NULL, *pend = NULL;
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
typedef enum portMode_t {
|
||||||
|
MODE_RX = 1,
|
||||||
|
MODE_TX = 2,
|
||||||
|
MODE_RXTX = MODE_RX | MODE_TX
|
||||||
|
} portMode_t;
|
||||||
|
|
||||||
|
typedef struct serialPort {
|
||||||
|
|
||||||
|
const struct serialPortVTable *vTable;
|
||||||
|
|
||||||
|
portMode_t mode;
|
||||||
|
uint32_t baudRate;
|
||||||
|
|
||||||
|
uint32_t rxBufferSize;
|
||||||
|
uint32_t txBufferSize;
|
||||||
|
volatile uint8_t *rxBuffer;
|
||||||
|
volatile uint8_t *txBuffer;
|
||||||
|
uint32_t rxBufferHead;
|
||||||
|
uint32_t rxBufferTail;
|
||||||
|
uint32_t txBufferHead;
|
||||||
|
uint32_t txBufferTail;
|
||||||
|
|
||||||
|
// FIXME rename member to rxCallback
|
||||||
|
serialReceiveCallbackPtr callback;
|
||||||
|
} serialPort_t;
|
||||||
|
|
||||||
|
struct serialPortVTable {
|
||||||
|
void (*serialWrite)(serialPort_t *instance, uint8_t ch);
|
||||||
|
};
|
||||||
|
|
||||||
|
static inline void serialWrite(serialPort_t *instance, uint8_t ch)
|
||||||
|
{
|
||||||
|
instance->vTable->serialWrite(instance, ch);
|
||||||
|
}
|
|
@ -103,10 +103,6 @@ void setupSoftSerial1(uint32_t baud)
|
||||||
softSerial->port.txBufferTail = 0;
|
softSerial->port.txBufferTail = 0;
|
||||||
softSerial->port.txBufferHead = 0;
|
softSerial->port.txBufferHead = 0;
|
||||||
|
|
||||||
// FIXME this uart specific initialisation doesn't belong really here
|
|
||||||
softSerial->port.txDMAChannel = 0;
|
|
||||||
softSerial->port.txDMAChannel = 0;
|
|
||||||
|
|
||||||
softSerial->isTransmittingData = false;
|
softSerial->isTransmittingData = false;
|
||||||
softSerial->isSearchingForStartBit = true;
|
softSerial->isSearchingForStartBit = true;
|
||||||
softSerial->isSearchingForStopBit = false;
|
softSerial->isSearchingForStopBit = false;
|
||||||
|
|
150
src/drv_uart.c
150
src/drv_uart.c
|
@ -4,23 +4,28 @@
|
||||||
Copyright © 2011 Bill Nesbitt
|
Copyright © 2011 Bill Nesbitt
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static serialPort_t serialPort1;
|
static uartPort_t uartPort1;
|
||||||
static serialPort_t serialPort2;
|
static uartPort_t uartPort2;
|
||||||
|
|
||||||
// USART1 - Telemetry (RX/TX by DMA)
|
// USART1 - Telemetry (RX/TX by DMA)
|
||||||
serialPort_t *serialUSART1(uint32_t baudRate, portmode_t mode)
|
uartPort_t *serialUSART1(uint32_t baudRate, portMode_t mode)
|
||||||
{
|
{
|
||||||
serialPort_t *s;
|
uartPort_t *s;
|
||||||
static volatile uint8_t rx1Buffer[UART1_RX_BUFFER_SIZE];
|
static volatile uint8_t rx1Buffer[UART1_RX_BUFFER_SIZE];
|
||||||
static volatile uint8_t tx1Buffer[UART1_TX_BUFFER_SIZE];
|
static volatile uint8_t tx1Buffer[UART1_TX_BUFFER_SIZE];
|
||||||
gpio_config_t gpio;
|
gpio_config_t gpio;
|
||||||
NVIC_InitTypeDef NVIC_InitStructure;
|
NVIC_InitTypeDef NVIC_InitStructure;
|
||||||
|
|
||||||
s = &serialPort1;
|
s = &uartPort1;
|
||||||
s->rxBufferSize = UART1_RX_BUFFER_SIZE;
|
s->port.vTable = UART;
|
||||||
s->txBufferSize = UART1_TX_BUFFER_SIZE;
|
|
||||||
s->rxBuffer = rx1Buffer;
|
s->port.baudRate = baudRate;
|
||||||
s->txBuffer = tx1Buffer;
|
|
||||||
|
s->port.rxBuffer = rx1Buffer;
|
||||||
|
s->port.txBuffer = tx1Buffer;
|
||||||
|
s->port.rxBufferSize = UART1_RX_BUFFER_SIZE;
|
||||||
|
s->port.txBufferSize = UART1_TX_BUFFER_SIZE;
|
||||||
|
|
||||||
s->rxDMAChannel = DMA1_Channel5;
|
s->rxDMAChannel = DMA1_Channel5;
|
||||||
s->txDMAChannel = DMA1_Channel4;
|
s->txDMAChannel = DMA1_Channel4;
|
||||||
|
|
||||||
|
@ -48,20 +53,24 @@ serialPort_t *serialUSART1(uint32_t baudRate, portmode_t mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
// USART2 - GPS or Spektrum or ?? (RX + TX by IRQ)
|
// USART2 - GPS or Spektrum or ?? (RX + TX by IRQ)
|
||||||
serialPort_t *serialUSART2(uint32_t baudRate, portmode_t mode)
|
uartPort_t *serialUSART2(uint32_t baudRate, portMode_t mode)
|
||||||
{
|
{
|
||||||
serialPort_t *s;
|
uartPort_t *s;
|
||||||
static volatile uint8_t rx2Buffer[UART2_RX_BUFFER_SIZE];
|
static volatile uint8_t rx2Buffer[UART2_RX_BUFFER_SIZE];
|
||||||
static volatile uint8_t tx2Buffer[UART2_TX_BUFFER_SIZE];
|
static volatile uint8_t tx2Buffer[UART2_TX_BUFFER_SIZE];
|
||||||
gpio_config_t gpio;
|
gpio_config_t gpio;
|
||||||
NVIC_InitTypeDef NVIC_InitStructure;
|
NVIC_InitTypeDef NVIC_InitStructure;
|
||||||
|
|
||||||
s = &serialPort2;
|
s = &uartPort2;
|
||||||
s->baudRate = baudRate;
|
s->port.vTable = UART;
|
||||||
s->rxBufferSize = UART2_RX_BUFFER_SIZE;
|
|
||||||
s->txBufferSize = UART2_TX_BUFFER_SIZE;
|
s->port.baudRate = baudRate;
|
||||||
s->rxBuffer = rx2Buffer;
|
|
||||||
s->txBuffer = tx2Buffer;
|
s->port.rxBufferSize = UART2_RX_BUFFER_SIZE;
|
||||||
|
s->port.txBufferSize = UART2_TX_BUFFER_SIZE;
|
||||||
|
s->port.rxBuffer = rx2Buffer;
|
||||||
|
s->port.txBuffer = tx2Buffer;
|
||||||
|
|
||||||
s->USARTx = USART2;
|
s->USARTx = USART2;
|
||||||
|
|
||||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
||||||
|
@ -87,11 +96,12 @@ serialPort_t *serialUSART2(uint32_t baudRate, portmode_t mode)
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
serialPort_t *uartOpen(USART_TypeDef *USARTx, uartReceiveCallbackPtr callback, uint32_t baudRate, portmode_t mode)
|
serialPort_t *uartOpen(USART_TypeDef *USARTx, serialReceiveCallbackPtr callback, uint32_t baudRate, portMode_t mode)
|
||||||
{
|
{
|
||||||
DMA_InitTypeDef DMA_InitStructure;
|
DMA_InitTypeDef DMA_InitStructure;
|
||||||
USART_InitTypeDef USART_InitStructure;
|
USART_InitTypeDef USART_InitStructure;
|
||||||
serialPort_t *s = NULL;
|
|
||||||
|
uartPort_t *s = NULL;
|
||||||
|
|
||||||
if (USARTx == USART1)
|
if (USARTx == USART1)
|
||||||
s = serialUSART1(baudRate, mode);
|
s = serialUSART1(baudRate, mode);
|
||||||
|
@ -99,11 +109,14 @@ serialPort_t *uartOpen(USART_TypeDef *USARTx, uartReceiveCallbackPtr callback, u
|
||||||
s = serialUSART2(baudRate, mode);
|
s = serialUSART2(baudRate, mode);
|
||||||
|
|
||||||
s->USARTx = USARTx;
|
s->USARTx = USARTx;
|
||||||
s->rxBufferHead = s->rxBufferTail = 0;
|
|
||||||
s->txBufferHead = s->txBufferTail = 0;
|
// common serial initialisation code should move to serialPort::init()
|
||||||
|
s->port.rxBufferHead = s->port.rxBufferTail = 0;
|
||||||
|
s->port.txBufferHead = s->port.txBufferTail = 0;
|
||||||
// callback for IRQ-based RX ONLY
|
// callback for IRQ-based RX ONLY
|
||||||
s->callback = callback;
|
s->port.callback = callback;
|
||||||
s->mode = mode;
|
s->port.mode = mode;
|
||||||
|
s->port.baudRate = baudRate;
|
||||||
|
|
||||||
USART_InitStructure.USART_BaudRate = baudRate;
|
USART_InitStructure.USART_BaudRate = baudRate;
|
||||||
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
||||||
|
@ -130,10 +143,10 @@ serialPort_t *uartOpen(USART_TypeDef *USARTx, uartReceiveCallbackPtr callback, u
|
||||||
// Receive DMA or IRQ
|
// Receive DMA or IRQ
|
||||||
if (mode & MODE_RX) {
|
if (mode & MODE_RX) {
|
||||||
if (s->rxDMAChannel) {
|
if (s->rxDMAChannel) {
|
||||||
DMA_InitStructure.DMA_BufferSize = s->rxBufferSize;
|
DMA_InitStructure.DMA_BufferSize = s->port.rxBufferSize;
|
||||||
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
|
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
|
||||||
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
|
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
|
||||||
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)s->rxBuffer;
|
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)s->port.rxBuffer;
|
||||||
DMA_DeInit(s->rxDMAChannel);
|
DMA_DeInit(s->rxDMAChannel);
|
||||||
DMA_Init(s->rxDMAChannel, &DMA_InitStructure);
|
DMA_Init(s->rxDMAChannel, &DMA_InitStructure);
|
||||||
DMA_Cmd(s->rxDMAChannel, ENABLE);
|
DMA_Cmd(s->rxDMAChannel, ENABLE);
|
||||||
|
@ -147,7 +160,7 @@ serialPort_t *uartOpen(USART_TypeDef *USARTx, uartReceiveCallbackPtr callback, u
|
||||||
// Transmit DMA or IRQ
|
// Transmit DMA or IRQ
|
||||||
if (mode & MODE_TX) {
|
if (mode & MODE_TX) {
|
||||||
if (s->txDMAChannel) {
|
if (s->txDMAChannel) {
|
||||||
DMA_InitStructure.DMA_BufferSize = s->txBufferSize;
|
DMA_InitStructure.DMA_BufferSize = s->port.txBufferSize;
|
||||||
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
|
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
|
||||||
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
|
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
|
||||||
DMA_DeInit(s->txDMAChannel);
|
DMA_DeInit(s->txDMAChannel);
|
||||||
|
@ -161,10 +174,10 @@ serialPort_t *uartOpen(USART_TypeDef *USARTx, uartReceiveCallbackPtr callback, u
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return s;
|
return (serialPort_t *)s;
|
||||||
}
|
}
|
||||||
|
|
||||||
void uartChangeBaud(serialPort_t *s, uint32_t baudRate)
|
void uartChangeBaud(uartPort_t *s, uint32_t baudRate)
|
||||||
{
|
{
|
||||||
USART_InitTypeDef USART_InitStructure;
|
USART_InitTypeDef USART_InitStructure;
|
||||||
|
|
||||||
|
@ -174,64 +187,67 @@ void uartChangeBaud(serialPort_t *s, uint32_t baudRate)
|
||||||
USART_InitStructure.USART_Parity = USART_Parity_No;
|
USART_InitStructure.USART_Parity = USART_Parity_No;
|
||||||
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
||||||
USART_InitStructure.USART_Mode = 0;
|
USART_InitStructure.USART_Mode = 0;
|
||||||
if (s->mode & MODE_RX)
|
if (s->port.mode & MODE_RX)
|
||||||
USART_InitStructure.USART_Mode |= USART_Mode_Rx;
|
USART_InitStructure.USART_Mode |= USART_Mode_Rx;
|
||||||
if (s->mode & MODE_TX)
|
if (s->port.mode & MODE_TX)
|
||||||
USART_InitStructure.USART_Mode |= USART_Mode_Tx;
|
USART_InitStructure.USART_Mode |= USART_Mode_Tx;
|
||||||
USART_Init(s->USARTx, &USART_InitStructure);
|
USART_Init(s->USARTx, &USART_InitStructure);
|
||||||
|
|
||||||
|
s->port.baudRate = baudRate;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void uartStartTxDMA(serialPort_t *s)
|
static void uartStartTxDMA(uartPort_t *s)
|
||||||
{
|
{
|
||||||
s->txDMAChannel->CMAR = (uint32_t)&s->txBuffer[s->txBufferTail];
|
s->txDMAChannel->CMAR = (uint32_t)&s->port.txBuffer[s->port.txBufferTail];
|
||||||
if (s->txBufferHead > s->txBufferTail) {
|
if (s->port.txBufferHead > s->port.txBufferTail) {
|
||||||
s->txDMAChannel->CNDTR = s->txBufferHead - s->txBufferTail;
|
s->txDMAChannel->CNDTR = s->port.txBufferHead - s->port.txBufferTail;
|
||||||
s->txBufferTail = s->txBufferHead;
|
s->port.txBufferTail = s->port.txBufferHead;
|
||||||
} else {
|
} else {
|
||||||
s->txDMAChannel->CNDTR = s->txBufferSize - s->txBufferTail;
|
s->txDMAChannel->CNDTR = s->port.txBufferSize - s->port.txBufferTail;
|
||||||
s->txBufferTail = 0;
|
s->port.txBufferTail = 0;
|
||||||
}
|
}
|
||||||
s->txDMAEmpty = false;
|
s->txDMAEmpty = false;
|
||||||
DMA_Cmd(s->txDMAChannel, ENABLE);
|
DMA_Cmd(s->txDMAChannel, ENABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isUartAvailable(serialPort_t *s)
|
bool isUartAvailable(uartPort_t *s)
|
||||||
{
|
{
|
||||||
if (s->rxDMAChannel)
|
if (s->rxDMAChannel)
|
||||||
return s->rxDMAChannel->CNDTR != s->rxDMAPos;
|
return s->rxDMAChannel->CNDTR != s->rxDMAPos;
|
||||||
else
|
else
|
||||||
return s->rxBufferTail != s->rxBufferHead;
|
return s->port.rxBufferTail != s->port.rxBufferHead;
|
||||||
}
|
}
|
||||||
|
|
||||||
// BUGBUG TODO TODO FIXME
|
// BUGBUG TODO TODO FIXME
|
||||||
bool isUartTransmitEmpty(serialPort_t *s)
|
bool isUartTransmitEmpty(uartPort_t *s)
|
||||||
{
|
{
|
||||||
if (s->txDMAChannel)
|
if (s->txDMAChannel)
|
||||||
return s->txDMAEmpty;
|
return s->txDMAEmpty;
|
||||||
else
|
else
|
||||||
return s->txBufferTail == s->txBufferHead;
|
return s->port.txBufferTail == s->port.txBufferHead;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t uartRead(serialPort_t *s)
|
uint8_t uartRead(uartPort_t *s)
|
||||||
{
|
{
|
||||||
uint8_t ch;
|
uint8_t ch;
|
||||||
|
|
||||||
if (s->rxDMAChannel) {
|
if (s->rxDMAChannel) {
|
||||||
ch = s->rxBuffer[s->rxBufferSize - s->rxDMAPos];
|
ch = s->port.rxBuffer[s->port.rxBufferSize - s->rxDMAPos];
|
||||||
if (--s->rxDMAPos == 0)
|
if (--s->rxDMAPos == 0)
|
||||||
s->rxDMAPos = s->rxBufferSize;
|
s->rxDMAPos = s->port.rxBufferSize;
|
||||||
} else {
|
} else {
|
||||||
ch = s->rxBuffer[s->rxBufferTail];
|
ch = s->port.rxBuffer[s->port.rxBufferTail];
|
||||||
s->rxBufferTail = (s->rxBufferTail + 1) % s->rxBufferSize;
|
s->port.rxBufferTail = (s->port.rxBufferTail + 1) % s->port.rxBufferSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
void uartWrite(serialPort_t *s, uint8_t ch)
|
void uartWrite(serialPort_t *instance, uint8_t ch)
|
||||||
{
|
{
|
||||||
s->txBuffer[s->txBufferHead] = ch;
|
uartPort_t *s = (uartPort_t *)instance;
|
||||||
s->txBufferHead = (s->txBufferHead + 1) % s->txBufferSize;
|
s->port.txBuffer[s->port.txBufferHead] = ch;
|
||||||
|
s->port.txBufferHead = (s->port.txBufferHead + 1) % s->port.txBufferSize;
|
||||||
|
|
||||||
if (s->txDMAChannel) {
|
if (s->txDMAChannel) {
|
||||||
if (!(s->txDMAChannel->CCR & 1))
|
if (!(s->txDMAChannel->CCR & 1))
|
||||||
|
@ -241,11 +257,15 @@ void uartWrite(serialPort_t *s, uint8_t ch)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void uartPrint(serialPort_t *s, const char *str)
|
const struct serialPortVTable UART[] = {
|
||||||
|
{ uartWrite }
|
||||||
|
};
|
||||||
|
|
||||||
|
void uartPrint(uartPort_t *s, const char *str)
|
||||||
{
|
{
|
||||||
uint8_t ch;
|
uint8_t ch;
|
||||||
while ((ch = *(str++))) {
|
while ((ch = *(str++))) {
|
||||||
uartWrite(s, ch);
|
uartWrite((serialPort_t *)s, ch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -254,11 +274,11 @@ void uartPrint(serialPort_t *s, const char *str)
|
||||||
// USART1 Tx DMA Handler
|
// USART1 Tx DMA Handler
|
||||||
void DMA1_Channel4_IRQHandler(void)
|
void DMA1_Channel4_IRQHandler(void)
|
||||||
{
|
{
|
||||||
serialPort_t *s = &serialPort1;
|
uartPort_t *s = &uartPort1;
|
||||||
DMA_ClearITPendingBit(DMA1_IT_TC4);
|
DMA_ClearITPendingBit(DMA1_IT_TC4);
|
||||||
DMA_Cmd(s->txDMAChannel, DISABLE);
|
DMA_Cmd(s->txDMAChannel, DISABLE);
|
||||||
|
|
||||||
if (s->txBufferHead != s->txBufferTail)
|
if (s->port.txBufferHead != s->port.txBufferTail)
|
||||||
uartStartTxDMA(s);
|
uartStartTxDMA(s);
|
||||||
else
|
else
|
||||||
s->txDMAEmpty = true;
|
s->txDMAEmpty = true;
|
||||||
|
@ -267,13 +287,13 @@ void DMA1_Channel4_IRQHandler(void)
|
||||||
// USART1 Tx IRQ Handler
|
// USART1 Tx IRQ Handler
|
||||||
void USART1_IRQHandler(void)
|
void USART1_IRQHandler(void)
|
||||||
{
|
{
|
||||||
serialPort_t *s = &serialPort1;
|
uartPort_t *s = &uartPort1;
|
||||||
uint16_t SR = s->USARTx->SR;
|
uint16_t SR = s->USARTx->SR;
|
||||||
|
|
||||||
if (SR & USART_FLAG_TXE) {
|
if (SR & USART_FLAG_TXE) {
|
||||||
if (s->txBufferTail != s->txBufferHead) {
|
if (s->port.txBufferTail != s->port.txBufferHead) {
|
||||||
s->USARTx->DR = s->txBuffer[s->txBufferTail];
|
s->USARTx->DR = s->port.txBuffer[s->port.txBufferTail];
|
||||||
s->txBufferTail = (s->txBufferTail + 1) % s->txBufferSize;
|
s->port.txBufferTail = (s->port.txBufferTail + 1) % s->port.txBufferSize;
|
||||||
} else {
|
} else {
|
||||||
USART_ITConfig(s->USARTx, USART_IT_TXE, DISABLE);
|
USART_ITConfig(s->USARTx, USART_IT_TXE, DISABLE);
|
||||||
}
|
}
|
||||||
|
@ -283,22 +303,22 @@ void USART1_IRQHandler(void)
|
||||||
// USART2 Rx/Tx IRQ Handler
|
// USART2 Rx/Tx IRQ Handler
|
||||||
void USART2_IRQHandler(void)
|
void USART2_IRQHandler(void)
|
||||||
{
|
{
|
||||||
serialPort_t *s = &serialPort2;
|
uartPort_t *s = &uartPort2;
|
||||||
uint16_t SR = s->USARTx->SR;
|
uint16_t SR = s->USARTx->SR;
|
||||||
|
|
||||||
if (SR & USART_FLAG_RXNE) {
|
if (SR & USART_FLAG_RXNE) {
|
||||||
// If we registered a callback, pass crap there
|
// If we registered a callback, pass crap there
|
||||||
if (s->callback) {
|
if (s->port.callback) {
|
||||||
s->callback(s->USARTx->DR);
|
s->port.callback(s->USARTx->DR);
|
||||||
} else {
|
} else {
|
||||||
s->rxBuffer[s->rxBufferHead] = s->USARTx->DR;
|
s->port.rxBuffer[s->port.rxBufferHead] = s->USARTx->DR;
|
||||||
s->rxBufferHead = (s->rxBufferHead + 1) % s->rxBufferSize;
|
s->port.rxBufferHead = (s->port.rxBufferHead + 1) % s->port.rxBufferSize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (SR & USART_FLAG_TXE) {
|
if (SR & USART_FLAG_TXE) {
|
||||||
if (s->txBufferTail != s->txBufferHead) {
|
if (s->port.txBufferTail != s->port.txBufferHead) {
|
||||||
s->USARTx->DR = s->txBuffer[s->txBufferTail];
|
s->USARTx->DR = s->port.txBuffer[s->port.txBufferTail];
|
||||||
s->txBufferTail = (s->txBufferTail + 1) % s->txBufferSize;
|
s->port.txBufferTail = (s->port.txBufferTail + 1) % s->port.txBufferSize;
|
||||||
} else {
|
} else {
|
||||||
USART_ITConfig(s->USARTx, USART_IT_TXE, DISABLE);
|
USART_ITConfig(s->USARTx, USART_IT_TXE, DISABLE);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,29 +8,10 @@
|
||||||
#define UART2_TX_BUFFER_SIZE 64
|
#define UART2_TX_BUFFER_SIZE 64
|
||||||
#define MAX_SERIAL_PORTS 2
|
#define MAX_SERIAL_PORTS 2
|
||||||
|
|
||||||
// This is a bitmask
|
|
||||||
typedef enum portmode_t {
|
|
||||||
MODE_RX = 1,
|
|
||||||
MODE_TX = 2,
|
|
||||||
MODE_RXTX = 3
|
|
||||||
} portmode_t;
|
|
||||||
|
|
||||||
// FIXME this is a uart_t really. Move the generic properties into a separate structure (serialPort_t) and update the code to use it
|
// FIXME this is a uart_t really. Move the generic properties into a separate structure (serialPort_t) and update the code to use it
|
||||||
typedef struct {
|
typedef struct {
|
||||||
portmode_t mode;
|
serialPort_t port;
|
||||||
uint32_t baudRate;
|
|
||||||
|
|
||||||
uint32_t rxBufferSize;
|
|
||||||
uint32_t txBufferSize;
|
|
||||||
volatile uint8_t *rxBuffer;
|
|
||||||
volatile uint8_t *txBuffer;
|
|
||||||
uint32_t rxBufferHead;
|
|
||||||
uint32_t rxBufferTail;
|
|
||||||
uint32_t txBufferHead;
|
|
||||||
uint32_t txBufferTail;
|
|
||||||
|
|
||||||
// FIXME rename callback type so something more generic
|
|
||||||
uartReceiveCallbackPtr callback;
|
|
||||||
|
|
||||||
// FIXME these are uart specific and do not belong in here
|
// FIXME these are uart specific and do not belong in here
|
||||||
DMA_Channel_TypeDef *rxDMAChannel;
|
DMA_Channel_TypeDef *rxDMAChannel;
|
||||||
|
@ -43,12 +24,14 @@ typedef struct {
|
||||||
bool txDMAEmpty;
|
bool txDMAEmpty;
|
||||||
|
|
||||||
USART_TypeDef *USARTx;
|
USART_TypeDef *USARTx;
|
||||||
} serialPort_t;
|
} uartPort_t;
|
||||||
|
|
||||||
serialPort_t *uartOpen(USART_TypeDef *USARTx, uartReceiveCallbackPtr callback, uint32_t baudRate, portmode_t mode);
|
extern const struct serialPortVTable UART[];
|
||||||
void uartChangeBaud(serialPort_t *s, uint32_t baudRate);
|
|
||||||
bool isUartAvailable(serialPort_t *s);
|
serialPort_t *uartOpen(USART_TypeDef *USARTx, serialReceiveCallbackPtr callback, uint32_t baudRate, portMode_t mode);
|
||||||
bool isUartTransmitEmpty(serialPort_t *s);
|
void uartChangeBaud(uartPort_t *s, uint32_t baudRate);
|
||||||
uint8_t uartRead(serialPort_t *s);
|
bool isUartAvailable(uartPort_t *s);
|
||||||
void uartWrite(serialPort_t *s, uint8_t ch);
|
bool isUartTransmitEmpty(uartPort_t *s);
|
||||||
void uartPrint(serialPort_t *s, const char *str);
|
uint8_t uartRead(uartPort_t *s);
|
||||||
|
void uartWrite(serialPort_t *instance, uint8_t ch);
|
||||||
|
void uartPrint(uartPort_t *s, const char *str);
|
||||||
|
|
|
@ -51,7 +51,7 @@ void gpsInit(uint32_t baudrate)
|
||||||
|
|
||||||
if (mcfg.gps_type != GPS_NMEA) {
|
if (mcfg.gps_type != GPS_NMEA) {
|
||||||
for (i = 0; i < 5; i++) {
|
for (i = 0; i < 5; i++) {
|
||||||
uartChangeBaud(core.gpsport, init_speed[i]);
|
uartChangeBaud((uartPort_t *)core.gpsport, init_speed[i]);
|
||||||
switch (baudrate) {
|
switch (baudrate) {
|
||||||
case 19200:
|
case 19200:
|
||||||
gpsPrint(gpsInitStrings[offset]);
|
gpsPrint(gpsInitStrings[offset]);
|
||||||
|
@ -70,7 +70,7 @@ void gpsInit(uint32_t baudrate)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uartChangeBaud(core.gpsport, baudrate);
|
uartChangeBaud((uartPort_t *)core.gpsport, baudrate);
|
||||||
if (mcfg.gps_type == GPS_UBLOX) {
|
if (mcfg.gps_type == GPS_UBLOX) {
|
||||||
for (i = 0; i < sizeof(ubloxInit); i++) {
|
for (i = 0; i < sizeof(ubloxInit); i++) {
|
||||||
uartWrite(core.gpsport, ubloxInit[i]); // send ubx init binary
|
uartWrite(core.gpsport, ubloxInit[i]); // send ubx init binary
|
||||||
|
@ -96,7 +96,7 @@ static void gpsPrint(const char *str)
|
||||||
str++;
|
str++;
|
||||||
}
|
}
|
||||||
// wait to send all
|
// wait to send all
|
||||||
while (!isUartTransmitEmpty(core.gpsport));
|
while (!isUartTransmitEmpty((uartPort_t *)core.gpsport));
|
||||||
delay(30);
|
delay(30);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -228,7 +228,7 @@ void tfp_printf(char *fmt, ...)
|
||||||
va_start(va, fmt);
|
va_start(va, fmt);
|
||||||
tfp_format(stdout_putp, stdout_putf, fmt, va);
|
tfp_format(stdout_putp, stdout_putf, fmt, va);
|
||||||
va_end(va);
|
va_end(va);
|
||||||
while (!isUartTransmitEmpty(core.mainport));
|
while (!isUartTransmitEmpty((uartPort_t *)core.mainport));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void putcp(void *p, char c)
|
static void putcp(void *p, char c)
|
||||||
|
|
|
@ -183,7 +183,7 @@ void serialize16(int16_t a)
|
||||||
|
|
||||||
void serialize8(uint8_t a)
|
void serialize8(uint8_t a)
|
||||||
{
|
{
|
||||||
uartWrite(core.mainport, a);
|
serialWrite(core.mainport, a);
|
||||||
checksum ^= a;
|
checksum ^= a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -678,8 +678,8 @@ void serialCom(void)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (isUartAvailable(core.mainport)) {
|
while (isUartAvailable((uartPort_t *)core.mainport)) {
|
||||||
c = uartRead(core.mainport);
|
c = uartRead((uartPort_t *)core.mainport);
|
||||||
|
|
||||||
if (c_state == IDLE) {
|
if (c_state == IDLE) {
|
||||||
c_state = (c == '$') ? HEADER_START : IDLE;
|
c_state = (c == '$') ? HEADER_START : IDLE;
|
||||||
|
@ -715,7 +715,7 @@ void serialCom(void)
|
||||||
c_state = IDLE;
|
c_state = IDLE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!cliMode && !isUartAvailable(core.telemport) && feature(FEATURE_TELEMETRY) && f.ARMED) { // The first 2 conditions should never evaluate to true but I'm putting it here anyway - silpstream
|
if (!cliMode && !isUartAvailable((uartPort_t *)core.telemport) && feature(FEATURE_TELEMETRY) && f.ARMED) { // The first 2 conditions should never evaluate to true but I'm putting it here anyway - silpstream
|
||||||
sendTelemetry();
|
sendTelemetry();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue