Skip to content

Commit

Permalink
chores: improve serial / timer conflict detection
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelcoeffic authored and pfeerick committed Oct 1, 2023
1 parent 2f063ce commit 964bd62
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
5 changes: 2 additions & 3 deletions radio/src/targets/common/arm/stm32/stm32_serial_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,15 @@ static void _on_rx_fifo(uint8_t data)

static void* stm32_serial_init(void* hw_def, const etx_serial_init* params)
{
// TODO: check if port is already in use
auto sp = (const stm32_serial_port*)hw_def;
if (!sp) return nullptr;

auto usart = sp->usart;
auto st = stm32_serial_find_state(usart);
if (!st || st->sp) return nullptr;

stm32_usart_init(usart, params);
st->sp = sp; // TODO: only iff init() is sucessful
if (!stm32_usart_init(usart, params)) return nullptr;
st->sp = sp;

if (params->direction & ETX_Dir_TX) {
// prepare for send_byte()
Expand Down
15 changes: 11 additions & 4 deletions radio/src/targets/common/arm/stm32/stm32_softserial_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "stm32_softserial_driver.h"
#include "stm32_exti_driver.h"
#include "stm32_timer.h"

#include <string.h>

Expand Down Expand Up @@ -67,9 +68,13 @@ static inline void _rx_fifo_clear()
rxRidx = 0;
}

static void _softserial_init_rx(const stm32_softserial_rx_port* port,
static bool _softserial_init_rx(const stm32_softserial_rx_port* port,
const etx_serial_init* params)
{
// Test if pin is in reset state
uint32_t mode = LL_GPIO_GetPinMode(port->GPIOx, port->GPIO_Pin);
if (mode != LL_GPIO_MODE_INPUT) return false;

rxBitCount = 0;
rxBuffer = port->buffer.buffer;
rxBufLen = port->buffer.length;
Expand All @@ -80,11 +85,11 @@ static void _softserial_init_rx(const stm32_softserial_rx_port* port,
LL_TIM_StructInit(&timInit);

uint32_t freq = params->baudrate * 16;
if (!freq) return;
if (!freq) return false;

timInit.Prescaler = __LL_TIM_CALC_PSC(port->TIM_Freq, freq);

// TODO: enable_tim_clock(port->TIMx);
stm32_timer_enable_clock(port->TIMx);
LL_TIM_Init(port->TIMx, &timInit);

LL_TIM_ClearFlag_UPDATE(port->TIMx);
Expand Down Expand Up @@ -119,6 +124,8 @@ static void _softserial_init_rx(const stm32_softserial_rx_port* port,
// Configure EXTI for raising edge (start bit; assuming inverted serial)
_softserialPort = port;
stm32_exti_enable(port->EXTI_Line, LL_EXTI_TRIGGER_RISING, _softserial_exti);

return true;
}

static void _softserial_deinit_gpio(const stm32_softserial_rx_port* port)
Expand All @@ -142,7 +149,7 @@ static void _softserial_deinit_rx(const stm32_softserial_rx_port* port)
static void* stm32_softserial_rx_init(void* hw_def, const etx_serial_init* params)
{
auto port = (const stm32_softserial_rx_port*)hw_def;
_softserial_init_rx(port, params);
if (!_softserial_init_rx(port, params)) return nullptr;
return hw_def;
}

Expand Down
17 changes: 14 additions & 3 deletions radio/src/targets/common/arm/stm32/stm32_usart_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,17 @@ void stm32_usart_deinit_rx_dma(const stm32_usart_t* usart)
// - asymmetric bitrates for half-duplex (GHOST)
// - ??? ability to switch RX DMA OFF ??? (-> X12S)
//
void stm32_usart_init(const stm32_usart_t* usart, const etx_serial_init* params)
bool stm32_usart_init(const stm32_usart_t* usart, const etx_serial_init* params)
{
// Test if the GPIO pins are in reset state
uint32_t pins = usart->GPIO_Pin;
while(pins != 0) {
uint32_t pin = 1 << POSITION_VAL(pins);
uint32_t mode = LL_GPIO_GetPinMode(usart->GPIOx, pin);
if (mode != LL_GPIO_MODE_INPUT) return false;
pins ^= pin;
}

enable_usart_clock(usart->USARTx);
LL_USART_DeInit(usart->USARTx);

Expand Down Expand Up @@ -258,8 +267,8 @@ void stm32_usart_init(const stm32_usart_t* usart, const etx_serial_init* params)
break;

default:
// TODO: return some error
return;
stm32_usart_deinit(usart);
return false;
}

usartInit.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
Expand Down Expand Up @@ -302,6 +311,8 @@ void stm32_usart_init(const stm32_usart_t* usart, const etx_serial_init* params)
LL_USART_IsEnabledIT_RXNE(usart->USARTx)) {
_enable_usart_irq(usart);
}

return true;
}

void stm32_usart_deinit(const stm32_usart_t* usart)
Expand Down
2 changes: 1 addition & 1 deletion radio/src/targets/common/arm/stm32/stm32_usart_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct stm32_usart_t {
uint8_t txDMA_IRQ_Prio;
};

void stm32_usart_init(const stm32_usart_t* usart, const etx_serial_init* params);
bool stm32_usart_init(const stm32_usart_t* usart, const etx_serial_init* params);
void stm32_usart_init_rx_dma(const stm32_usart_t* usart, const void* buffer, uint32_t length);
void stm32_usart_enable_tx_irq(const stm32_usart_t* usart);
void stm32_usart_set_idle_irq(const stm32_usart_t* usart, uint32_t enabled);
Expand Down

0 comments on commit 964bd62

Please sign in to comment.