Skip to content
This repository was archived by the owner on Mar 7, 2026. It is now read-only.

Commit de99e5d

Browse files
committed
bmp-v3: Implemented logic for enabling and disabling the secondary UART appropriately based on state changes
1 parent 8b54428 commit de99e5d

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

src/include/platform_support.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,18 @@ const char *platform_ident(void);
8585
#endif
8686

8787
#ifdef PLATFORM_MULTI_UART
88+
typedef enum uart_state {
89+
UART_STATE_UNKNOWN,
90+
UART_STATE_IDLE,
91+
UART_STATE_LOST,
92+
} uart_state_e;
93+
8894
void platform_enable_uart2(void);
8995
void platform_disable_uart2(void);
9096
bool platform_is_uart2_enabled(void);
9197
void platform_switch_dir_uart2(void);
98+
void platform_uart2_state_change(uint32_t state);
99+
uart_state_e platform_uart2_state(void);
92100
#endif
93101

94102
#endif /* INCLUDE_PLATFORM_SUPPORT_H */

src/platforms/bmp-v3/platform.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ static void adc_init(void);
5959

6060
int hwversion = -1;
6161

62+
static uart_state_e uart2_state = UART_STATE_UNKNOWN;
63+
6264
void platform_init(void)
6365
{
6466
hwversion = 0;
@@ -454,8 +456,9 @@ void platform_enable_uart2(void)
454456

455457
void platform_disable_uart2(void)
456458
{
457-
/* Dsiable the UART (so we can go back into being able to change the pin swapping) */
459+
/* Disable the UART (so we can go back into being able to change the pin swapping) */
458460
usart_disable(AUX_UART2);
461+
uart2_state = UART_STATE_UNKNOWN;
459462
/* Reconfigure the GPIOs back to inputs so we can listen for which is high to watch for new connections */
460463
gpio_mode_setup(AUX_UART2_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, AUX_UART2_TX_PIN | AUX_UART2_RX_PIN);
461464
}
@@ -470,3 +473,17 @@ void platform_switch_dir_uart2(void)
470473
/* Swap the directions of the BMPU connector UART pins */
471474
gpio_toggle(AUX_UART2_DIR_PORT, AUX_UART2_DIR_PIN);
472475
}
476+
477+
void platform_uart2_state_change(const uint32_t state)
478+
{
479+
/* Make a note of whether either Idle or Framing Error have occured */
480+
if (state & USART_ISR_IDLE)
481+
uart2_state = UART_STATE_IDLE;
482+
else if (state & USART_ISR_FE)
483+
uart2_state = UART_STATE_LOST;
484+
}
485+
486+
uart_state_e platform_uart2_state(void)
487+
{
488+
return uart2_state;
489+
}

src/platforms/common/aux_serial.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,10 @@ static void aux_serial_receive_isr(const uintptr_t uart, const uint8_t dma_irq)
633633
debug_serial_run();
634634
}
635635

636+
#ifdef PLATFORM_MULTI_UART
637+
if (uart == AUX_UART2)
638+
platform_uart2_state_change(status);
639+
#endif
636640
#ifndef STM32U5
637641
nvic_enable_irq(dma_irq);
638642
#else
@@ -819,6 +823,7 @@ void aux_uart2_rx_detect_isr(void)
819823
* UART2 just became active, so bring it up and disable the EXTI for it, making sure UART1's is
820824
* active in case the user swaps UARTs over
821825
*/
826+
platform_enable_uart2();
822827
aux_serial_activate_uart(AUX_UART2);
823828
exti_reset_request(AUX_UART2_RX_DETECT_EXTI);
824829
exti_disable_request(AUX_UART2_RX_DETECT_EXTI);

src/platforms/common/stm32/timing_stm32.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,23 @@ void sys_tick_handler(void)
160160
/* And reset the counter back to 0 */
161161
uart_ticks = 0U;
162162
}
163-
} else
164-
uart_ticks = 0U;
163+
} else {
164+
/*
165+
* If the UART goes into framing error and that persists for more than a milisecond or two, then
166+
* it's probably safe to assume that the wires became disconnected and the UART is no longer active
167+
* in which case we then want to disable the UART and go back into swap scanning. Additionally, we'll
168+
* want to either make the other UART active, or make all UARTs inactive.
169+
*/
170+
const uart_state_e state = platform_uart2_state();
171+
if (state == UART_STATE_LOST) {
172+
if (++uart_ticks == 2U) {
173+
platform_disable_uart2();
174+
uart_ticks = 0U;
175+
}
176+
} else
177+
/* Otherwise if the UART state is either not known or the UART is idle, reset the tick counter */
178+
uart_ticks = 0U;
179+
}
165180
#endif
166181
}
167182

0 commit comments

Comments
 (0)