WiP
This commit is contained in:
@@ -29,7 +29,6 @@ set(STM32USB_SRC
|
||||
set(FIRMWARE_TARGET_SRC
|
||||
${FIRMWARE_TARGET_SRC}
|
||||
../common/arm/stm32/rtc_driver.cpp
|
||||
${AUX_SERIAL_DRIVER}
|
||||
../common/arm/stm32/delays_driver.cpp
|
||||
../common/arm/stm32/usb_bsp.c
|
||||
../common/arm/stm32/usbd_desc.c
|
||||
@@ -40,6 +39,18 @@ set(FIRMWARE_TARGET_SRC
|
||||
../common/arm/stm32/usbd_hid_joystick.c
|
||||
)
|
||||
|
||||
if(AUX_SERIAL OR AUX2_SERIAL)
|
||||
set(FIRMWARE_TARGET_SRC ${FIRMWARE_TARGET_SRC}
|
||||
../common/arm/stm32/aux_serial_driver.cpp
|
||||
)
|
||||
if(AUX_SERIAL)
|
||||
add_definitions(-DAUX_SERIAL)
|
||||
endif()
|
||||
if(AUX2_SERIAL)
|
||||
add_definitions(-DAUX2_SERIAL)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(DEBUG OR CLI OR USB_SERIAL)
|
||||
set(STM32USB_SRC
|
||||
${STM32USB_SRC}
|
||||
@@ -70,7 +81,6 @@ endif()
|
||||
|
||||
set(FIRMWARE_SRC
|
||||
${FIRMWARE_SRC}
|
||||
serial.cpp
|
||||
syscalls.c
|
||||
)
|
||||
|
||||
|
||||
@@ -26,8 +26,71 @@
|
||||
#include "targets/horus/board.h"
|
||||
#include "aux_serial_driver.h"
|
||||
|
||||
#include "fifo.h"
|
||||
#include "dmafifo.h"
|
||||
|
||||
typedef DMAFifo<32> AuxSerialRxFifo;
|
||||
|
||||
struct AuxSerialState
|
||||
{
|
||||
Fifo<uint8_t, 512>* txFifo;
|
||||
AuxSerialRxFifo* rxFifo;
|
||||
|
||||
const stm32_usart_t* usart;
|
||||
};
|
||||
|
||||
static void* aux_serial_init(struct AuxSerialState* st, const etx_serial_init* params)
|
||||
{
|
||||
stm32_usart_init(st->usart, params);
|
||||
|
||||
if (params->rx_enable && st->usart->rxDMA && !params->on_receive) {
|
||||
st->rxFifo->clear();
|
||||
stm32_usart_init_rx_dma(st->usart, st->rxFifo->buffer(), st->rxFifo->size());
|
||||
}
|
||||
|
||||
return st;
|
||||
}
|
||||
|
||||
static void aux_serial_putc(void* ctx, uint8_t c)
|
||||
{
|
||||
AuxSerialState* st = (AuxSerialState*)ctx;
|
||||
if (st->txFifo->isFull()) return;
|
||||
st->txFifo->push(c);
|
||||
|
||||
// Send is based on IRQ, so that this
|
||||
// only enables the corresponding IRQ
|
||||
stm32_usart_send_buffer(st->usart, &c, 1);
|
||||
}
|
||||
|
||||
static void aux_serial_send_buffer(void* ctx, const uint8_t* data, uint8_t size)
|
||||
{
|
||||
while(size > 0) {
|
||||
aux_serial_putc(ctx, *data++);
|
||||
size--;
|
||||
}
|
||||
}
|
||||
|
||||
static void aux_wait_tx_completed(void* ctx)
|
||||
{
|
||||
// TODO
|
||||
(void)ctx;
|
||||
}
|
||||
|
||||
static int aux_get_byte(void* ctx, uint8_t* data)
|
||||
{
|
||||
AuxSerialState* st = (AuxSerialState*)ctx;
|
||||
if (!st->rxFifo) return -1;
|
||||
return st->rxFifo->pop(*data);
|
||||
}
|
||||
|
||||
void aux_serial_deinit(void* ctx)
|
||||
{
|
||||
AuxSerialState* st = (AuxSerialState*)ctx;
|
||||
stm32_usart_deinit(st->usart);
|
||||
}
|
||||
|
||||
#if defined(AUX_SERIAL)
|
||||
uint8_t auxSerialMode = UART_MODE_COUNT; // Prevent debug output before port is setup
|
||||
|
||||
Fifo<uint8_t, 512> auxSerialTxFifo;
|
||||
|
||||
#if defined(AUX_SERIAL_DMA_Stream_RX)
|
||||
@@ -65,166 +128,53 @@ const stm32_usart_t auxUSART = {
|
||||
#endif
|
||||
};
|
||||
|
||||
static bool auxSerialOnSend(uint8_t& data)
|
||||
AuxSerialState auxSerialState = {
|
||||
.txFifo = &auxSerialTxFifo,
|
||||
.rxFifo = &auxSerialRxFifo,
|
||||
.usart = &auxUSART,
|
||||
};
|
||||
|
||||
// TODO: grab context to get buffer
|
||||
static bool auxSerialOnSend(uint8_t* data)
|
||||
{
|
||||
return auxSerialTxFifo.pop(data);
|
||||
return auxSerialTxFifo.pop(*data);
|
||||
}
|
||||
|
||||
// TODO: add to state
|
||||
static etx_serial_callbacks_t auxSerialCb = {
|
||||
.on_send = auxSerialOnSend,
|
||||
.on_receive = nullptr,
|
||||
.on_error = nullptr,
|
||||
};
|
||||
|
||||
void auxSerialSetup(unsigned int baudrate, bool rx_enable, uint8_t word_length,
|
||||
uint8_t parity, uint8_t stop_bits)
|
||||
static void* auxSerialInit(const etx_serial_init* params)
|
||||
{
|
||||
#if defined(AUX_SERIAL_PWR_GPIO)
|
||||
LL_GPIO_InitTypeDef pinInit;
|
||||
LL_GPIO_StructInit(&pinInit);
|
||||
pinInit.Pin = AUX_SERIAL_PWR_GPIO_PIN;
|
||||
pinInit.Mode = LL_GPIO_MODE_OUTPUT;
|
||||
pinInit.Pull = LL_GPIO_PULL_UP;
|
||||
LL_GPIO_Init(AUX_SERIAL_PWR_GPIO, &pinInit);
|
||||
#endif
|
||||
|
||||
#if defined(AUX_SERIAL_TX_INVERT_GPIO_PIN) // NV14
|
||||
// TODO
|
||||
GPIO_InitStructure.GPIO_Pin = AUX_SERIAL_TX_INVERT_GPIO_PIN;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
||||
GPIO_Init(AUX_SERIAL_TX_INVERT_GPIO, &GPIO_InitStructure);
|
||||
GPIO_ResetBits(AUX_SERIAL_TX_INVERT_GPIO, AUX_SERIAL_TX_INVERT_GPIO_PIN);
|
||||
#endif
|
||||
|
||||
#if defined(AUX_SERIAL_RX_INVERT_GPIO_PIN) // NV14
|
||||
// TODO
|
||||
GPIO_InitStructure.GPIO_Pin = AUX_SERIAL_RX_INVERT_GPIO_PIN;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
||||
GPIO_Init(AUX_SERIAL_RX_INVERT_GPIO, &GPIO_InitStructure);
|
||||
GPIO_ResetBits(AUX_SERIAL_RX_INVERT_GPIO, AUX_SERIAL_RX_INVERT_GPIO_PIN);
|
||||
#endif
|
||||
|
||||
etx_serial_init serialInit = {
|
||||
.baudrate = baudrate,
|
||||
.parity = parity,
|
||||
.stop_bits = stop_bits,
|
||||
.word_length = word_length,
|
||||
.rx_enable = rx_enable,
|
||||
.rx_dma_buf = nullptr,
|
||||
.rx_dma_buf_len = 0,
|
||||
.on_receive = nullptr, // TODO
|
||||
.on_error = nullptr, // TODO
|
||||
};
|
||||
|
||||
#if defined(AUX_SERIAL_DMA_Stream_RX)
|
||||
if (rx_enable) {
|
||||
auxSerialRxFifo.clear();
|
||||
serialInit.rx_dma_buf = auxSerialRxFifo.buffer();
|
||||
serialInit.rx_dma_buf_len = auxSerialRxFifo.size();
|
||||
}
|
||||
#endif
|
||||
|
||||
stm32_usart_init(&auxUSART, &serialInit);
|
||||
return aux_serial_init(&auxSerialState, params);
|
||||
}
|
||||
|
||||
const etx_serial_driver_t AuxSerialDriver = {
|
||||
.init = auxSerialInit,
|
||||
.deinit = aux_serial_deinit,
|
||||
.sendByte = aux_serial_putc,
|
||||
.sendBuffer = aux_serial_send_buffer,
|
||||
.waitForTxCompleted = aux_wait_tx_completed,
|
||||
.getByte = aux_get_byte,
|
||||
.setReceiveCb = nullptr,
|
||||
.setOnErrorCb = nullptr,
|
||||
};
|
||||
|
||||
extern "C" void AUX_SERIAL_USART_IRQHandler(void)
|
||||
{
|
||||
DEBUG_INTERRUPT(INT_SER2);
|
||||
stm32_usart_isr(&auxUSART, &auxSerialCb);
|
||||
}
|
||||
|
||||
void auxSerialPutc(uint8_t c)
|
||||
{
|
||||
if (auxSerialTxFifo.isFull()) return;
|
||||
auxSerialTxFifo.push(c);
|
||||
stm32_usart_send_buffer(&auxUSART, &c, 1);
|
||||
}
|
||||
|
||||
void auxSerialStop()
|
||||
{
|
||||
stm32_usart_deinit(&auxUSART);
|
||||
}
|
||||
|
||||
static void serialPushLua(uint8_t data)
|
||||
{
|
||||
// TODO: this should be set by LUA instead of
|
||||
// including some LUA header here.
|
||||
//
|
||||
// if (luaRxFifo)
|
||||
// luaRxFifo->push(data);
|
||||
}
|
||||
|
||||
void auxSerialInit(unsigned int mode, unsigned int protocol)
|
||||
{
|
||||
auxSerialStop();
|
||||
|
||||
auxSerialMode = mode;
|
||||
auxSerialCb.on_receive = nullptr;
|
||||
|
||||
switch (mode) {
|
||||
case UART_MODE_TELEMETRY_MIRROR:
|
||||
// TODO: add a setting for the telemetry protocol ???
|
||||
// -> how do we know the baudrate?
|
||||
#if defined(CROSSFIRE)
|
||||
if (protocol == PROTOCOL_TELEMETRY_CROSSFIRE) {
|
||||
auxSerialSetup(CROSSFIRE_TELEM_MIRROR_BAUDRATE, false);
|
||||
AUX_SERIAL_POWER_ON();
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
auxSerialSetup(FRSKY_TELEM_MIRROR_BAUDRATE, false);
|
||||
AUX_SERIAL_POWER_ON();
|
||||
break;
|
||||
|
||||
#if defined(DEBUG) || defined(CLI)
|
||||
case UART_MODE_DEBUG:
|
||||
auxSerialSetup(DEBUG_BAUDRATE, false);
|
||||
AUX_SERIAL_POWER_OFF();
|
||||
break;
|
||||
#endif
|
||||
|
||||
case UART_MODE_TELEMETRY:
|
||||
if (protocol == PROTOCOL_TELEMETRY_FRSKY_D_SECONDARY) {
|
||||
auxSerialSetup(FRSKY_D_BAUDRATE, true);
|
||||
AUX_SERIAL_POWER_ON();
|
||||
}
|
||||
break;
|
||||
|
||||
case UART_MODE_SBUS_TRAINER:
|
||||
auxSerialSetup(
|
||||
SBUS_BAUDRATE, true, ETX_WordLength_9, ETX_Parity_Even,
|
||||
ETX_StopBits_Two); // USART_WordLength_9b due to parity bit
|
||||
AUX_SERIAL_POWER_ON();
|
||||
break;
|
||||
|
||||
#if defined(LUA)
|
||||
case UART_MODE_LUA:
|
||||
auxSerialSetup(LUA_DEFAULT_BAUDRATE, false);
|
||||
auxSerialCb.on_receive = serialPushLua;
|
||||
AUX_SERIAL_POWER_ON();
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void auxSerialSbusInit()
|
||||
{
|
||||
auxSerialInit(UART_MODE_SBUS_TRAINER, 0);
|
||||
}
|
||||
|
||||
uint8_t auxSerialTracesEnabled()
|
||||
{
|
||||
#if defined(DEBUG)
|
||||
return (auxSerialMode == UART_MODE_DEBUG);
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
#endif // AUX_SERIAL
|
||||
|
||||
#if defined(AUX2_SERIAL)
|
||||
uint8_t aux2SerialMode = UART_MODE_COUNT; // Prevent debug output before port is setup
|
||||
|
||||
Fifo<uint8_t, 512> aux2SerialTxFifo;
|
||||
|
||||
AuxSerialRxFifo aux2SerialRxFifo __DMA (AUX2_SERIAL_DMA_Stream_RX);
|
||||
|
||||
const LL_GPIO_InitTypeDef aux2USARTPinInit = {
|
||||
@@ -250,9 +200,15 @@ const stm32_usart_t aux2USART = {
|
||||
.rxDMA_Channel = AUX2_SERIAL_DMA_Channel_RX,
|
||||
};
|
||||
|
||||
static bool aux2SerialOnSend(uint8_t& data)
|
||||
AuxSerialState aux2SerialState = {
|
||||
.txFifo = &aux2SerialTxFifo,
|
||||
.rxFifo = &aux2SerialRxFifo,
|
||||
.usart = &aux2USART,
|
||||
};
|
||||
|
||||
static bool aux2SerialOnSend(uint8_t* data)
|
||||
{
|
||||
return aux2SerialTxFifo.pop(data);
|
||||
return aux2SerialTxFifo.pop(*data);
|
||||
}
|
||||
|
||||
static etx_serial_callbacks_t aux2SerialCb = {
|
||||
@@ -261,35 +217,9 @@ static etx_serial_callbacks_t aux2SerialCb = {
|
||||
.on_error = nullptr,
|
||||
};
|
||||
|
||||
void aux2SerialSetup(unsigned int baudrate, bool rx_enable, uint8_t word_length,
|
||||
uint8_t parity, uint8_t stop_bits)
|
||||
void* aux2SerialInit(const etx_serial_init* params)
|
||||
{
|
||||
etx_serial_init serialInit = {
|
||||
.baudrate = baudrate,
|
||||
.parity = parity,
|
||||
.stop_bits = stop_bits,
|
||||
.word_length = word_length,
|
||||
.rx_enable = rx_enable,
|
||||
.rx_dma_buf = nullptr,
|
||||
.rx_dma_buf_len = 0,
|
||||
.on_receive = nullptr, // TODO
|
||||
.on_error = nullptr, // TODO
|
||||
};
|
||||
|
||||
if (rx_enable) {
|
||||
aux2SerialRxFifo.clear();
|
||||
serialInit.rx_dma_buf = aux2SerialRxFifo.buffer();
|
||||
serialInit.rx_dma_buf_len = aux2SerialRxFifo.size();
|
||||
}
|
||||
|
||||
stm32_usart_init(&aux2USART, &serialInit);
|
||||
|
||||
// #if defined(AUX2_SERIAL_PWR_GPIO)
|
||||
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
||||
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
||||
// GPIO_InitStructure.GPIO_Pin = AUX2_SERIAL_PWR_GPIO_PIN;
|
||||
// GPIO_Init(AUX2_SERIAL_PWR_GPIO, &GPIO_InitStructure);
|
||||
// #endif
|
||||
return aux_serial_init(&aux2SerialState, params);
|
||||
}
|
||||
|
||||
extern "C" void AUX2_SERIAL_USART_IRQHandler(void)
|
||||
@@ -298,81 +228,15 @@ extern "C" void AUX2_SERIAL_USART_IRQHandler(void)
|
||||
stm32_usart_isr(&aux2USART, &aux2SerialCb);
|
||||
}
|
||||
|
||||
void aux2SerialPutc(uint8_t c)
|
||||
{
|
||||
if (aux2SerialTxFifo.isFull()) return;
|
||||
aux2SerialTxFifo.push(c);
|
||||
stm32_usart_send_buffer(&aux2USART, &c, 1);
|
||||
}
|
||||
|
||||
void aux2SerialStop()
|
||||
{
|
||||
stm32_usart_deinit(&aux2USART);
|
||||
}
|
||||
|
||||
void aux2SerialInit(unsigned int mode, unsigned int protocol)
|
||||
{
|
||||
aux2SerialStop();
|
||||
|
||||
aux2SerialMode = mode;
|
||||
aux2SerialCb.on_receive = nullptr;
|
||||
|
||||
switch (mode) {
|
||||
case UART_MODE_TELEMETRY_MIRROR:
|
||||
#if defined(CROSSFIRE)
|
||||
if (protocol == PROTOCOL_TELEMETRY_CROSSFIRE) {
|
||||
aux2SerialSetup(CROSSFIRE_TELEM_MIRROR_BAUDRATE, false);
|
||||
AUX2_SERIAL_POWER_ON();
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
aux2SerialSetup(FRSKY_TELEM_MIRROR_BAUDRATE, false);
|
||||
AUX2_SERIAL_POWER_ON();
|
||||
break;
|
||||
|
||||
#if defined(DEBUG) || defined(CLI)
|
||||
case UART_MODE_DEBUG:
|
||||
aux2SerialSetup(DEBUG_BAUDRATE, false);
|
||||
AUX2_SERIAL_POWER_OFF();
|
||||
break;
|
||||
#endif
|
||||
|
||||
case UART_MODE_TELEMETRY:
|
||||
if (protocol == PROTOCOL_TELEMETRY_FRSKY_D_SECONDARY) {
|
||||
aux2SerialSetup(FRSKY_D_BAUDRATE, true);
|
||||
AUX2_SERIAL_POWER_ON();
|
||||
}
|
||||
break;
|
||||
|
||||
case UART_MODE_SBUS_TRAINER:
|
||||
aux2SerialSetup(
|
||||
SBUS_BAUDRATE, true, ETX_WordLength_9, ETX_Parity_Even,
|
||||
ETX_StopBits_Two); // 2 stop bits requires USART_WordLength_9b
|
||||
AUX2_SERIAL_POWER_ON();
|
||||
break;
|
||||
|
||||
#if defined(LUA)
|
||||
case UART_MODE_LUA:
|
||||
aux2SerialSetup(LUA_DEFAULT_BAUDRATE, false);
|
||||
aux2SerialCb.on_receive = serialPushLua;
|
||||
AUX2_SERIAL_POWER_ON();
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void aux2SerialSbusInit()
|
||||
{
|
||||
aux2SerialInit(UART_MODE_SBUS_TRAINER, 0);
|
||||
}
|
||||
|
||||
uint8_t aux2SerialTracesEnabled()
|
||||
{
|
||||
#if defined(DEBUG)
|
||||
return (aux2SerialMode == UART_MODE_DEBUG);
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
const etx_serial_driver_t Aux2SerialDriver = {
|
||||
.init = aux2SerialInit,
|
||||
.deinit = aux_serial_deinit,
|
||||
.sendByte = aux_serial_putc,
|
||||
.sendBuffer = aux_serial_send_buffer,
|
||||
.waitForTxCompleted = aux_wait_tx_completed,
|
||||
.getByte = aux_get_byte,
|
||||
.setReceiveCb = nullptr,
|
||||
.setOnErrorCb = nullptr,
|
||||
};
|
||||
|
||||
#endif // AUX2_SERIAL
|
||||
|
||||
@@ -24,28 +24,12 @@
|
||||
#include "hal/serial_driver.h"
|
||||
|
||||
#if defined(AUX_SERIAL)
|
||||
extern uint8_t auxSerialMode;
|
||||
void auxSerialSetup(unsigned int baudrate, bool rx_enable,
|
||||
uint8_t word_length = ETX_WordLength_8,
|
||||
uint8_t parity = ETX_Parity_None,
|
||||
uint8_t stop_bits = ETX_StopBits_One);
|
||||
void auxSerialInit(unsigned int mode, unsigned int protocol);
|
||||
void auxSerialPutc(uint8_t c);
|
||||
void auxSerialSbusInit();
|
||||
void auxSerialStop();
|
||||
uint8_t auxSerialTracesEnabled();
|
||||
|
||||
extern const etx_serial_driver_t AuxSerialDriver;
|
||||
#endif
|
||||
|
||||
#if defined(AUX2_SERIAL)
|
||||
extern uint8_t aux2SerialMode;
|
||||
void aux2SerialSetup(unsigned int baudrate, bool rx_enable,
|
||||
uint8_t word_length = ETX_WordLength_8,
|
||||
uint8_t parity = ETX_Parity_None,
|
||||
uint8_t stop_bits = ETX_StopBits_One);
|
||||
void aux2SerialInit(unsigned int mode, unsigned int protocol);
|
||||
void aux2SerialPutc(uint8_t c);
|
||||
void aux2SerialSbusInit();
|
||||
void aux2SerialStop();
|
||||
uint8_t aux2SerialTracesEnabled();
|
||||
|
||||
extern const etx_serial_driver_t Aux2SerialDriver;
|
||||
#endif
|
||||
|
||||
|
||||
@@ -144,6 +144,7 @@ if(PCB STREQUAL X10 OR PCB STREQUAL X12S OR PCB STREQUAL NV14)
|
||||
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_usart.c
|
||||
../../../../../serial.cpp
|
||||
../../../../../targets/common/arm/stm32/aux_serial_driver.cpp
|
||||
../../../../../targets/common/arm/stm32/stm32_usart_driver.cpp
|
||||
)
|
||||
endif()
|
||||
else()
|
||||
|
||||
@@ -244,12 +244,9 @@ int main()
|
||||
#endif
|
||||
|
||||
delaysInit(); // needed for lcdInit()
|
||||
|
||||
#if defined(DEBUG) && defined(AUX_SERIAL)
|
||||
auxSerialInit(UART_MODE_DEBUG, 0); // default serial mode (None if DEBUG not defined)
|
||||
#endif
|
||||
#if defined(DEBUG) && defined(AUX2_SERIAL)
|
||||
aux2SerialInit(UART_MODE_DEBUG, 0); // default serial mode (None if DEBUG not defined)
|
||||
|
||||
#if defined(DEBUG)
|
||||
initSerialPorts();
|
||||
#endif
|
||||
|
||||
__enable_irq();
|
||||
|
||||
@@ -49,8 +49,6 @@ static const etx_serial_init extmoduleSerialParams = {
|
||||
.stop_bits = ETX_StopBits_One,
|
||||
.word_length = ETX_WordLength_8,
|
||||
.rx_enable = true,
|
||||
.rx_dma_buf = nullptr,
|
||||
.rx_dma_buf_len = 0,
|
||||
.on_receive = extmoduleFifoReceive,
|
||||
.on_error = extmoduleFifoError,
|
||||
};
|
||||
@@ -78,9 +76,9 @@ static const stm32_usart_t extmoduleUSART = {
|
||||
.rxDMA_Channel = 0,
|
||||
};
|
||||
|
||||
static void extmoduleSerialStart(const etx_serial_init* params)
|
||||
static void* extmoduleSerialStart(const etx_serial_init* params)
|
||||
{
|
||||
if (!params) return;
|
||||
if (!params) return nullptr;
|
||||
|
||||
extmodule_driver.on_receive = params->on_receive;
|
||||
extmodule_driver.on_error = params->on_error;
|
||||
@@ -88,6 +86,8 @@ static void extmoduleSerialStart(const etx_serial_init* params)
|
||||
// UART config
|
||||
stm32_usart_init(&extmoduleUSART, params);
|
||||
extmoduleFifo.clear();
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void extmoduleInvertedSerialStart(uint32_t baudrate)
|
||||
@@ -108,7 +108,7 @@ void extmodulePxx1SerialStart()
|
||||
}
|
||||
#endif
|
||||
|
||||
void extmoduleSerialStop()
|
||||
void extmoduleSerialStop(void*)
|
||||
{
|
||||
stm32_usart_deinit(&extmoduleUSART);
|
||||
|
||||
@@ -117,18 +117,18 @@ void extmoduleSerialStop()
|
||||
extmodule_driver.on_error = nullptr;
|
||||
}
|
||||
|
||||
static void extmoduleSendByte(uint8_t byte)
|
||||
static void extmoduleSendByte(void*, uint8_t byte)
|
||||
{
|
||||
stm32_usart_send_byte(&extmoduleUSART, byte);
|
||||
}
|
||||
|
||||
static void extmoduleSendBuffer(const uint8_t * data, uint8_t size)
|
||||
static void extmoduleSendBuffer(void*, const uint8_t * data, uint8_t size)
|
||||
{
|
||||
if (size == 0) return;
|
||||
stm32_usart_send_buffer(&extmoduleUSART, data, size);
|
||||
}
|
||||
|
||||
static void extmoduleWaitForTxCompleted()
|
||||
static void extmoduleWaitForTxCompleted(void*)
|
||||
{
|
||||
stm32_usart_wait_for_tx_dma(&extmoduleUSART);
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ target_include_directories(stm32cube_ll PRIVATE
|
||||
${THIRDPARTY_DIR}/CMSIS/Include)
|
||||
|
||||
set(FIRMWARE_SRC ${FIRMWARE_SRC} $<TARGET_OBJECTS:stm32cube_ll>)
|
||||
set(BOOTLOADER_SRC ${BOOTLOADER_SRC} $<TARGET_OBJECTS:stm32cube_ll>)
|
||||
|
||||
# StdPeriph library
|
||||
set(STM32LIB_DIR ${THIRDPARTY_DIR}/STM32F4xx_DSP_StdPeriph_Lib_V1.8.0/Libraries)
|
||||
|
||||
@@ -28,13 +28,13 @@
|
||||
ModuleFifo intmoduleFifo;
|
||||
|
||||
#if !defined(INTMODULE_DMA_STREAM)
|
||||
uint8_t * intmoduleTxBufferData;
|
||||
volatile uint8_t intmoduleTxBufferRemaining;
|
||||
static uint8_t * intmoduleTxBufferData;
|
||||
static volatile uint8_t intmoduleTxBufferRemaining;
|
||||
|
||||
bool intmoduleTxBufferSend(uint8_t& data)
|
||||
bool intmoduleTxBufferSend(uint8_t* data)
|
||||
{
|
||||
if (intmoduleTxBufferRemaining) {
|
||||
data = *(intmoduleTxBufferData++);
|
||||
*data = *(intmoduleTxBufferData++);
|
||||
intmoduleTxBufferRemaining--;
|
||||
return true;
|
||||
}
|
||||
@@ -96,9 +96,15 @@ void intmoduleStop()
|
||||
intmodule_driver.on_error = nullptr;
|
||||
}
|
||||
|
||||
void intmoduleSerialStart(const etx_serial_init* params)
|
||||
static void intmoduleStop(void* ctx)
|
||||
{
|
||||
if (!params) return;
|
||||
(void)ctx;
|
||||
intmoduleStop();
|
||||
}
|
||||
|
||||
void* intmoduleSerialStart(const etx_serial_init* params)
|
||||
{
|
||||
if (!params) return nullptr;
|
||||
|
||||
// TODO: sanity check parameters
|
||||
// - the UART seems to block when initialised with baudrate = 0
|
||||
@@ -108,6 +114,7 @@ void intmoduleSerialStart(const etx_serial_init* params)
|
||||
intmodule_driver.on_error = params->on_error;
|
||||
|
||||
stm32_usart_init(&intmoduleUSART, params);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
#define USART_FLAG_ERRORS (USART_FLAG_ORE | USART_FLAG_NE | USART_FLAG_FE | USART_FLAG_PE)
|
||||
@@ -116,13 +123,15 @@ extern "C" void INTMODULE_USART_IRQHandler(void)
|
||||
stm32_usart_isr(&intmoduleUSART, &intmodule_driver);
|
||||
}
|
||||
|
||||
void intmoduleSendByte(uint8_t byte)
|
||||
void intmoduleSendByte(void* ctx, uint8_t byte)
|
||||
{
|
||||
(void)ctx;
|
||||
stm32_usart_send_byte(&intmoduleUSART, byte);
|
||||
}
|
||||
|
||||
void intmoduleSendBuffer(const uint8_t * data, uint8_t size)
|
||||
void intmoduleSendBuffer(void* ctx, const uint8_t * data, uint8_t size)
|
||||
{
|
||||
(void)ctx;
|
||||
if (size == 0)
|
||||
return;
|
||||
|
||||
@@ -133,8 +142,9 @@ void intmoduleSendBuffer(const uint8_t * data, uint8_t size)
|
||||
stm32_usart_send_buffer(&intmoduleUSART, data, size);
|
||||
}
|
||||
|
||||
void intmoduleWaitForTxCompleted()
|
||||
void intmoduleWaitForTxCompleted(void* ctx)
|
||||
{
|
||||
(void)ctx;
|
||||
#if defined(INTMODULE_DMA_STREAM)
|
||||
stm32_usart_wait_for_tx_dma(&intmoduleUSART);
|
||||
#else
|
||||
|
||||
@@ -22,8 +22,16 @@
|
||||
#include "stm32_usart_driver.h"
|
||||
#include <string.h>
|
||||
|
||||
static void stm32_usart_init_rx_dma(const stm32_usart_t* usart, void* buffer, uint32_t length)
|
||||
void stm32_usart_init_rx_dma(const stm32_usart_t* usart, void* buffer, uint32_t length)
|
||||
{
|
||||
// Disable IRQ based RX
|
||||
LL_USART_DisableIT_RXNE(usart->USARTx);
|
||||
|
||||
// In case TX DMA is used, disable the ISR completely
|
||||
if (usart->txDMA) {
|
||||
NVIC_DisableIRQ(usart->IRQn);
|
||||
}
|
||||
|
||||
LL_DMA_InitTypeDef dmaInit;
|
||||
LL_DMA_StructInit(&dmaInit);
|
||||
dmaInit.Channel = usart->rxDMA_Channel;
|
||||
@@ -47,6 +55,8 @@ void stm32_usart_init(const stm32_usart_t* usart, const etx_serial_init* params)
|
||||
LL_GPIO_Init(usart->GPIOx, (LL_GPIO_InitTypeDef*)usart->pinInit);
|
||||
|
||||
LL_USART_InitTypeDef usartInit;
|
||||
LL_USART_StructInit(&usartInit);
|
||||
|
||||
usartInit.BaudRate = params->baudrate;
|
||||
|
||||
uint32_t parity = LL_USART_PARITY_NONE;
|
||||
@@ -96,14 +106,8 @@ void stm32_usart_init(const stm32_usart_t* usart, const etx_serial_init* params)
|
||||
|
||||
// Enable RX IRQ
|
||||
if (params->rx_enable) {
|
||||
if (usart->rxDMA) {
|
||||
// RX DMA
|
||||
stm32_usart_init_rx_dma(usart, params->rx_dma_buf,
|
||||
params->rx_dma_buf_len);
|
||||
} else {
|
||||
// IRQ based RX
|
||||
LL_USART_EnableIT_RXNE(usart->USARTx);
|
||||
}
|
||||
// IRQ based RX
|
||||
LL_USART_EnableIT_RXNE(usart->USARTx);
|
||||
}
|
||||
|
||||
if (!usart->txDMA || (params->rx_enable && !usart->rxDMA)) {
|
||||
@@ -159,7 +163,7 @@ void stm32_usart_send_buffer(const stm32_usart_t* usart, const uint8_t * data, u
|
||||
return;
|
||||
} else {
|
||||
// Please note that we don't use the buffer:
|
||||
// it should set internally by the driver user
|
||||
// it should be set internally by the driver user
|
||||
// and each byte is returned individually by on_send()
|
||||
LL_USART_EnableIT_TXE(usart->USARTx);
|
||||
}
|
||||
@@ -204,7 +208,7 @@ void stm32_usart_isr(const stm32_usart_t* usart, etx_serial_callbacks_t* cb)
|
||||
uint32_t status = LL_USART_ReadReg(usart->USARTx, SR);
|
||||
|
||||
// Receive: do it first as it is more time critical
|
||||
if (LL_USART_IsEnabledIT_TXE(usart->USARTx)) {
|
||||
if (LL_USART_IsEnabledIT_RXNE(usart->USARTx)) {
|
||||
|
||||
// Drain RX
|
||||
while (status & (LL_USART_SR_RXNE | USART_FLAG_ERRORS)) {
|
||||
@@ -229,7 +233,7 @@ void stm32_usart_isr(const stm32_usart_t* usart, etx_serial_callbacks_t* cb)
|
||||
if (LL_USART_IsEnabledIT_TXE(usart->USARTx) && (status & LL_USART_SR_TXE)) {
|
||||
|
||||
uint8_t data;
|
||||
if (cb->on_send && cb->on_send(data)) {
|
||||
if (cb->on_send && cb->on_send(&data)) {
|
||||
LL_USART_TransmitData8(usart->USARTx, data);
|
||||
} else {
|
||||
LL_USART_DisableIT_TXE(usart->USARTx);
|
||||
|
||||
@@ -48,10 +48,10 @@ struct stm32_usart_t {
|
||||
};
|
||||
|
||||
void stm32_usart_init(const stm32_usart_t* usart, const etx_serial_init* params);
|
||||
void stm32_usart_init_rx_dma(const stm32_usart_t* usart, void* buffer, uint32_t length);
|
||||
void stm32_usart_deinit(const stm32_usart_t* usart);
|
||||
void stm32_usart_send_byte(const stm32_usart_t* usart, uint8_t byte);
|
||||
void stm32_usart_send_buffer(const stm32_usart_t* usart, const uint8_t * data, uint32_t size);
|
||||
void stm32_usart_wait_for_tx_dma(const stm32_usart_t* usart);
|
||||
void stm32_usart_isr(const stm32_usart_t* usart, etx_serial_callbacks_t* cb);
|
||||
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ int getSelectedUsbMode();
|
||||
void setSelectedUsbMode(int mode);
|
||||
|
||||
uint32_t usbSerialFreeSpace();
|
||||
void usbSerialPutc(uint8_t c);
|
||||
void usbSerialPutc(void*, uint8_t c);
|
||||
|
||||
uint32_t usbSerialBaudRate(void);
|
||||
|
||||
|
||||
@@ -94,6 +94,7 @@ static uint16_t VCP_Init(void)
|
||||
cdcConnected = true;
|
||||
ctrlLineStateCb = NULL;
|
||||
baudRateCb = NULL;
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
@@ -205,7 +206,7 @@ uint32_t usbSerialFreeSpace()
|
||||
1;
|
||||
}
|
||||
|
||||
void usbSerialPutc(uint8_t c)
|
||||
void usbSerialPutc(void*, uint8_t c)
|
||||
{
|
||||
/*
|
||||
Apparently there is no reliable way to tell if the
|
||||
@@ -253,21 +254,8 @@ void usbSerialPutc(uint8_t c)
|
||||
*/
|
||||
static uint16_t VCP_DataRx (uint8_t* Buf, uint32_t Len)
|
||||
{
|
||||
if (receiveDataCb)
|
||||
receiveDataCb(Buf, Len);
|
||||
|
||||
// #if defined(CLI)
|
||||
// // pass data to CLI
|
||||
// cliReceiveData(Buf, Len);
|
||||
// #elif defined(LUA)
|
||||
// // copy data to the LUA FIFO
|
||||
// if (luaRxFifo) {
|
||||
// for (uint32_t i = 0; i < Len; i++) {
|
||||
// luaRxFifo->push(Buf[i]);
|
||||
// }
|
||||
// }
|
||||
// #endif
|
||||
|
||||
auto _rxCb = receiveDataCb;
|
||||
if (_rxCb) _rxCb(Buf, Len);
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user