HAL LL serial driver
This commit is contained in:
@@ -19,24 +19,40 @@
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "opentx.h"
|
||||
#include "stm32_hal_ll.h"
|
||||
#include "intmodule_serial_driver.h"
|
||||
#include "stm32_usart_driver.h"
|
||||
#include "board.h"
|
||||
|
||||
#include "io/frsky_pxx2.h"
|
||||
ModuleFifo intmoduleFifo;
|
||||
|
||||
#if !defined(INTMODULE_DMA_STREAM)
|
||||
uint8_t * intmoduleTxBufferData;
|
||||
volatile uint8_t intmoduleTxBufferRemaining;
|
||||
|
||||
bool intmoduleTxBufferSend(uint8_t& data)
|
||||
{
|
||||
if (intmoduleTxBufferRemaining) {
|
||||
data = *(intmoduleTxBufferData++);
|
||||
intmoduleTxBufferRemaining--;
|
||||
return true;
|
||||
}
|
||||
|
||||
// buffer is empty
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
struct etx_serial_callbacks_t {
|
||||
void (*on_receive)(uint8_t data);
|
||||
void (*on_error)();
|
||||
static etx_serial_callbacks_t intmodule_driver = {
|
||||
#if !defined(INTMODULE_DMA_STREAM)
|
||||
intmoduleTxBufferSend,
|
||||
#else
|
||||
nullptr,
|
||||
#endif
|
||||
nullptr, nullptr
|
||||
};
|
||||
|
||||
static etx_serial_callbacks_t intmodule_driver = { nullptr, nullptr };
|
||||
|
||||
// TODO: move this somewhere else
|
||||
void intmoduleFifoReceive(uint8_t data)
|
||||
{
|
||||
@@ -48,23 +64,28 @@ void intmoduleFifoError()
|
||||
intmoduleFifo.errors++;
|
||||
}
|
||||
|
||||
static const LL_GPIO_InitTypeDef intmoduleUSART_PinDef = {
|
||||
.Pin = INTMODULE_TX_GPIO_PIN | INTMODULE_RX_GPIO_PIN,
|
||||
.Mode = LL_GPIO_MODE_ALTERNATE,
|
||||
.Speed = LL_GPIO_SPEED_FREQ_LOW,
|
||||
.OutputType = LL_GPIO_OUTPUT_PUSHPULL,
|
||||
.Pull = LL_GPIO_PULL_UP,
|
||||
.Alternate = INTMODULE_GPIO_AF_LL,
|
||||
};
|
||||
|
||||
static const stm32_usart_t intmoduleUSART = {
|
||||
.USARTx = INTMODULE_USART,
|
||||
.GPIOx = INTMODULE_GPIO,
|
||||
.pinInit = &intmoduleUSART_PinDef,
|
||||
.IRQn = INTMODULE_USART_IRQn,
|
||||
.DMAx = INTMODULE_DMA,
|
||||
.DMA_Stream = INTMODULE_DMA_STREAM_LL,
|
||||
.DMA_Channel = INTMODULE_DMA_CHANNEL,
|
||||
};
|
||||
|
||||
void intmoduleStop()
|
||||
{
|
||||
#if defined(INTMODULE_DMA_STREAM)
|
||||
INTMODULE_DMA_STREAM->CR &= ~DMA_SxCR_EN; // Disable DMA
|
||||
#endif
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
GPIO_InitStructure.GPIO_Pin = INTMODULE_TX_GPIO_PIN | INTMODULE_RX_GPIO_PIN;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
||||
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
|
||||
GPIO_Init(INTMODULE_GPIO, &GPIO_InitStructure);
|
||||
|
||||
USART_DeInit(INTMODULE_USART);
|
||||
|
||||
GPIO_ResetBits(INTMODULE_GPIO, INTMODULE_TX_GPIO_PIN | INTMODULE_RX_GPIO_PIN);
|
||||
stm32_usart_deinit(&intmoduleUSART);
|
||||
|
||||
// reset callbacks
|
||||
intmodule_driver.on_receive = nullptr;
|
||||
@@ -78,89 +99,22 @@ void intmoduleSerialStart(const etx_serial_init* params)
|
||||
// TODO: sanity check parameters
|
||||
// - the UART seems to block when initialised with baudrate = 0
|
||||
|
||||
// TODO: remove this!!!
|
||||
INTERNAL_MODULE_ON();
|
||||
|
||||
// init callbacks
|
||||
intmodule_driver.on_receive = params->on_receive;
|
||||
intmodule_driver.on_error = params->on_error;
|
||||
|
||||
uint32_t baudrate = params->baudrate;
|
||||
|
||||
#if !defined(INTMODULE_DMA_STREAM)
|
||||
// IRQ based TX
|
||||
NVIC_InitTypeDef NVIC_InitStructure;
|
||||
NVIC_InitStructure.NVIC_IRQChannel = INTMODULE_USART_IRQn;
|
||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
|
||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&NVIC_InitStructure);
|
||||
|
||||
// rco: workaround for high baudrate not supported with
|
||||
// IRQ based TX.
|
||||
if (baudrate > 1870000) {
|
||||
TRACE("limiting baudrate to 1.87 MBit/s");
|
||||
baudrate = 1870000;
|
||||
}
|
||||
#endif
|
||||
|
||||
GPIO_PinAFConfig(INTMODULE_GPIO, INTMODULE_GPIO_PinSource_TX, INTMODULE_GPIO_AF);
|
||||
GPIO_PinAFConfig(INTMODULE_GPIO, INTMODULE_GPIO_PinSource_RX, INTMODULE_GPIO_AF);
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
GPIO_InitStructure.GPIO_Pin = INTMODULE_TX_GPIO_PIN | INTMODULE_RX_GPIO_PIN;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
||||
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(INTMODULE_GPIO, &GPIO_InitStructure);
|
||||
|
||||
stm32_usart_init(INTMODULE_USART, params);
|
||||
|
||||
if (params->rx_enable) {
|
||||
USART_ITConfig(INTMODULE_USART, USART_IT_RXNE, ENABLE);
|
||||
NVIC_SetPriority(INTMODULE_USART_IRQn, 6);
|
||||
NVIC_EnableIRQ(INTMODULE_USART_IRQn);
|
||||
}
|
||||
stm32_usart_init(&intmoduleUSART, params);
|
||||
}
|
||||
|
||||
#define USART_FLAG_ERRORS (USART_FLAG_ORE | USART_FLAG_NE | USART_FLAG_FE | USART_FLAG_PE)
|
||||
extern "C" void INTMODULE_USART_IRQHandler(void)
|
||||
{
|
||||
#if !defined(INTMODULE_DMA_STREAM)
|
||||
// Send
|
||||
if (USART_GetITStatus(INTMODULE_USART, USART_IT_TXE) != RESET) {
|
||||
if (intmoduleTxBufferRemaining) {
|
||||
USART_SendData(INTMODULE_USART, intmoduleTxBufferData[0]);
|
||||
intmoduleTxBufferData++;
|
||||
intmoduleTxBufferRemaining--;
|
||||
}
|
||||
else {
|
||||
USART_ITConfig(INTMODULE_USART, USART_IT_TXE, DISABLE);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Receive
|
||||
uint32_t status = INTMODULE_USART->SR;
|
||||
while (status & (USART_FLAG_RXNE | USART_FLAG_ERRORS)) {
|
||||
uint8_t data = INTMODULE_USART->DR;
|
||||
if (status & USART_FLAG_ERRORS) {
|
||||
if (intmodule_driver.on_error)
|
||||
intmodule_driver.on_error();
|
||||
}
|
||||
else {
|
||||
if (intmodule_driver.on_receive)
|
||||
intmodule_driver.on_receive(data);
|
||||
}
|
||||
status = INTMODULE_USART->SR;
|
||||
}
|
||||
stm32_usart_isr(&intmoduleUSART, &intmodule_driver);
|
||||
}
|
||||
|
||||
void intmoduleSendByte(uint8_t byte)
|
||||
{
|
||||
while (!(INTMODULE_USART->SR & USART_SR_TXE));
|
||||
USART_SendData(INTMODULE_USART, byte);
|
||||
stm32_usart_send_byte(&intmoduleUSART, byte);
|
||||
}
|
||||
|
||||
void intmoduleSendBuffer(const uint8_t * data, uint8_t size)
|
||||
@@ -168,48 +122,20 @@ void intmoduleSendBuffer(const uint8_t * data, uint8_t size)
|
||||
if (size == 0)
|
||||
return;
|
||||
|
||||
#if defined(INTMODULE_DMA_STREAM)
|
||||
DMA_InitTypeDef DMA_InitStructure;
|
||||
DMA_DeInit(INTMODULE_DMA_STREAM);
|
||||
DMA_InitStructure.DMA_Channel = INTMODULE_DMA_CHANNEL;
|
||||
DMA_InitStructure.DMA_PeripheralBaseAddr = CONVERT_PTR_UINT(&INTMODULE_USART->DR);
|
||||
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
|
||||
DMA_InitStructure.DMA_Memory0BaseAddr = CONVERT_PTR_UINT(data);
|
||||
DMA_InitStructure.DMA_BufferSize = size;
|
||||
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
||||
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
||||
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
|
||||
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
|
||||
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
|
||||
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
|
||||
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
|
||||
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
|
||||
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
|
||||
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
|
||||
DMA_Init(INTMODULE_DMA_STREAM, &DMA_InitStructure);
|
||||
DMA_Cmd(INTMODULE_DMA_STREAM, ENABLE);
|
||||
USART_DMACmd(INTMODULE_USART, USART_DMAReq_Tx, ENABLE);
|
||||
#else
|
||||
#if !defined(INTMODULE_DMA_STREAM)
|
||||
intmoduleTxBufferData = (uint8_t *)data;
|
||||
intmoduleTxBufferRemaining = size;
|
||||
USART_ITConfig(INTMODULE_USART, USART_IT_TXE, ENABLE);
|
||||
#endif
|
||||
stm32_usart_send_buffer(&intmoduleUSART, data, size);
|
||||
}
|
||||
|
||||
void intmoduleWaitForTxCompleted()
|
||||
{
|
||||
#if !defined(SIMU)
|
||||
#if defined(INTMODULE_DMA_STREAM)
|
||||
// TODO: check if everything is properly initialised, this seems to block when
|
||||
// the port has been initialised with a zero baudrate
|
||||
if (DMA_GetCmdStatus(INTMODULE_DMA_STREAM) == ENABLE) {
|
||||
while(DMA_GetFlagStatus(INTMODULE_DMA_STREAM, INTMODULE_DMA_FLAG_TC) == RESET);
|
||||
DMA_ClearFlag(INTMODULE_DMA_STREAM, INTMODULE_DMA_FLAG_TC);
|
||||
}
|
||||
stm32_usart_wait_for_tx_dma(&intmoduleUSART);
|
||||
#else
|
||||
while (intmoduleTxBufferRemaining > 0);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
const etx_serial_driver_t IntmoduleSerialDriver = {
|
||||
|
||||
Reference in New Issue
Block a user