Skip to content

Commit effce37

Browse files
committed
swap TXRX to avoid DFU hijacking
1 parent 26e13e9 commit effce37

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

src/main/drivers/serial_uart_at32bsp.c

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,33 +63,32 @@ static void usartConfigurePinInversion(uartPort_t *uartPort) {
6363
#endif
6464
}
6565

66+
static uartDevice_t *uartFindDevice(uartPort_t *uartPort)
67+
{
68+
for (uint32_t i = 0; i < UARTDEV_COUNT_MAX; i++) {
69+
uartDevice_t *candidate = uartDevmap[i];
70+
71+
if (&candidate->port == uartPort) {
72+
return candidate;
73+
}
74+
}
75+
return NULL;
76+
}
77+
78+
static void uartConfigurePinSwap(uartPort_t *uartPort)
79+
{
80+
uartDevice_t *uartDevice = uartFindDevice(uartPort);
81+
if (!uartDevice) {
82+
return;
83+
}
84+
85+
if (uartDevice->pinSwap) {
86+
usart_transmit_receive_pin_swap(uartDevice->port.USARTx,TRUE);
87+
}
88+
}
89+
6690
void uartReconfigure(uartPort_t *uartPort)
6791
{
68-
// USART_InitTypeDef USART_InitStructure;
69-
// USART_Cmd(uartPort->USARTx, DISABLE);
70-
// USART_InitStructure.USART_BaudRate = uartPort->port.baudRate;
71-
72-
// according to the stm32 documentation wordlen has to be 9 for parity bits
73-
// this does not seem to matter for rx but will give bad data on tx!
74-
// This seems to cause RX to break on STM32F1, see https://github.com/betaflight/betaflight/pull/1654
75-
// if ( (uartPort->port.options & SERIAL_PARITY_EVEN)) {
76-
// USART_InitStructure.USART_WordLength = USART_WordLength_9b;
77-
// } else {
78-
// USART_InitStructure.USART_WordLength = USART_WordLength_8b;
79-
// }
80-
81-
// USART_InitStructure.USART_StopBits = (uartPort->port.options & SERIAL_STOPBITS_2) ? USART_StopBits_2 : USART_StopBits_1;
82-
// USART_InitStructure.USART_Parity = (uartPort->port.options & SERIAL_PARITY_EVEN) ? USART_Parity_Even : USART_Parity_No;
83-
//
84-
// USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
85-
// USART_InitStructure.USART_Mode = 0;
86-
// if (uartPort->port.mode & MODE_RX)
87-
// USART_InitStructure.USART_Mode |= USART_Mode_Rx;
88-
// if (uartPort->port.mode & MODE_TX)
89-
// USART_InitStructure.USART_Mode |= USART_Mode_Tx;
90-
//
91-
// USART_Init(uartPort->USARTx, &USART_InitStructure);
92-
// void usart_init(usart_type* usart_x, uint32_t baud_rate, usart_data_bit_num_type data_bit, usart_stop_bit_num_type stop_bit)
9392

9493
usart_enable(uartPort->USARTx,DISABLE);
9594
//init
@@ -111,9 +110,12 @@ void uartReconfigure(uartPort_t *uartPort)
111110
if (uartPort->port.mode & MODE_TX)
112111
usart_transmitter_enable(uartPort->USARTx,TRUE);
113112

114-
//config pin swap
113+
//config pin inverter
115114
usartConfigurePinInversion(uartPort);
116115

116+
//config pin swap
117+
uartConfigurePinSwap(uartPort);
118+
117119
if (uartPort->port.options & SERIAL_BIDIR)
118120
// USART_HalfDuplexCmd(uartPort->USARTx, ENABLE);
119121
usart_single_line_halfduplex_select(uartPort->USARTx, TRUE);

0 commit comments

Comments
 (0)