Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbfdefe2

Browse files
frondersfpistm
authored andcommitted
feat(uart): support UART Tx Rx pin swap function
Resolves:#2538
1 parent82b932a commitbfdefe2

File tree

1 file changed

+43
-12
lines changed
  • libraries/SrcWrapper/src/stm32

1 file changed

+43
-12
lines changed

‎libraries/SrcWrapper/src/stm32/uart.c

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,25 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
121121
USART_TypeDef*uart_rx=pinmap_peripheral(obj->pin_rx,PinMap_UART_RX);
122122
USART_TypeDef*uart_rts=pinmap_peripheral(obj->pin_rts,PinMap_UART_RTS);
123123
USART_TypeDef*uart_cts=pinmap_peripheral(obj->pin_cts,PinMap_UART_CTS);
124+
/* Check if pins are swapped */
125+
#if defined(UART_ADVFEATURE_SWAP_INIT)
126+
USART_TypeDef*uart_tx_swap=pinmap_peripheral(obj->pin_tx,PinMap_UART_RX);
127+
USART_TypeDef*uart_rx_swap=pinmap_peripheral(obj->pin_rx,PinMap_UART_TX);
128+
#else
129+
/* Pin swap not supported */
130+
USART_TypeDef*uart_tx_swap=NP;
131+
USART_TypeDef*uart_rx_swap=NP;
132+
#endif
124133

125134
/* Pin Tx must not be NP */
126-
if (uart_tx==NP) {
135+
if ((uart_tx==NP)&& (uart_tx_swap==NP)) {
127136
if (obj!=&serial_debug) {
128137
core_debug("ERROR: [U(S)ART] Tx pin has no peripheral!\n");
129138
}
130139
return;
131140
}
132141
/* Pin Rx must not be NP if not half-duplex */
133-
if ((obj->pin_rx!=NC)&& (uart_rx==NP)) {
142+
if ((obj->pin_rx!=NC)&& (uart_rx==NP)&& (uart_rx_swap==NP)) {
134143
if (obj!=&serial_debug) {
135144
core_debug("ERROR: [U(S)ART] Rx pin has no peripheral!\n");
136145
}
@@ -156,6 +165,10 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
156165
* and assign it to the object
157166
*/
158167
obj->uart=pinmap_merge_peripheral(uart_tx,uart_rx);
168+
if (obj->uart==NP) {
169+
/* Regular pins not matched, check if they can be swapped */
170+
obj->uart=pinmap_merge_peripheral(uart_tx_swap,uart_rx_swap);
171+
}
159172
/* We also merge RTS/CTS and assert all pins belong to the same instance */
160173
obj->uart=pinmap_merge_peripheral(obj->uart,uart_rts);
161174
obj->uart=pinmap_merge_peripheral(obj->uart,uart_cts);
@@ -328,10 +341,26 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
328341
#endif
329342

330343
/* Configure UART GPIO pins */
331-
pinmap_pinout(obj->pin_tx,PinMap_UART_TX);
332-
if (uart_rx!=NP) {
333-
pinmap_pinout(obj->pin_rx,PinMap_UART_RX);
344+
#if defined(UART_ADVFEATURE_SWAP_INIT)
345+
uint32_tpin_swap=UART_ADVFEATURE_SWAP_DISABLE;
346+
#endif
347+
if (uart_tx!=NP) {
348+
/* Regular GPIO */
349+
pinmap_pinout(obj->pin_tx,PinMap_UART_TX);
350+
if (uart_rx!=NP) {
351+
pinmap_pinout(obj->pin_rx,PinMap_UART_RX);
352+
}
353+
}
354+
#if defined(UART_ADVFEATURE_SWAP_INIT)
355+
elseif (uart_tx_swap!=NP) {
356+
/* Swapped GPIO */
357+
pinmap_pinout(obj->pin_tx,PinMap_UART_RX);
358+
if (uart_rx_swap!=NP) {
359+
pinmap_pinout(obj->pin_rx,PinMap_UART_TX);
360+
}
361+
pin_swap=UART_ADVFEATURE_SWAP_ENABLE;
334362
}
363+
#endif
335364

336365
/* Configure flow control */
337366
uint32_tflow_control=UART_HWCONTROL_NONE;
@@ -354,8 +383,10 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
354383
huart->Init.Mode=UART_MODE_TX_RX;
355384
huart->Init.HwFlowCtl=flow_control;
356385
huart->Init.OverSampling=UART_OVERSAMPLING_16;
357-
#if !defined(STM32F1xx)&& !defined(STM32F2xx)&& !defined(STM32F4xx)\
358-
&& !defined(STM32L1xx)
386+
#if defined(UART_ADVFEATURE_SWAP_INIT)
387+
huart->AdvancedInit.AdvFeatureInit=UART_ADVFEATURE_SWAP_INIT;
388+
huart->AdvancedInit.Swap=pin_swap;
389+
#elif defined(UART_ADVFEATURE_NO_INIT)
359390
huart->AdvancedInit.AdvFeatureInit=UART_ADVFEATURE_NO_INIT;
360391
#endif
361392
#ifdefUART_ONE_BIT_SAMPLE_DISABLE
@@ -391,7 +422,7 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
391422
HAL_UARTEx_DisableStopMode(huart);
392423
}
393424
/* Trying default LPUART clock source */
394-
if (uart_rx==NP) {
425+
if ((uart_rx==NP)&& (uart_rx_swap==NP)) {
395426
if (HAL_HalfDuplex_Init(huart)==HAL_OK) {
396427
return;
397428
}
@@ -416,7 +447,7 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
416447
__HAL_RCC_LPUART3_CONFIG(RCC_LPUART3CLKSOURCE_LSE);
417448
}
418449
#endif
419-
if (uart_rx==NP) {
450+
if ((uart_rx==NP)&& (uart_rx_swap==NP)) {
420451
if (HAL_HalfDuplex_Init(huart)==HAL_OK) {
421452
return;
422453
}
@@ -438,7 +469,7 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
438469
__HAL_RCC_LPUART3_CONFIG(RCC_LPUART3CLKSOURCE_HSI);
439470
}
440471
#endif
441-
if (uart_rx==NP) {
472+
if ((uart_rx==NP)&& (uart_rx_swap==NP)) {
442473
if (HAL_HalfDuplex_Init(huart)==HAL_OK) {
443474
return;
444475
}
@@ -465,7 +496,7 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
465496
__HAL_RCC_LPUART3_CONFIG(RCC_LPUART3CLKSOURCE_PCLK1);
466497
}
467498
#endif
468-
if (uart_rx==NP) {
499+
if ((uart_rx==NP)&& (uart_rx_swap==NP)) {
469500
if (HAL_HalfDuplex_Init(huart)==HAL_OK) {
470501
return;
471502
}
@@ -490,7 +521,7 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
490521
}
491522
#endif
492523

493-
if (uart_rx==NP) {
524+
if ((uart_rx==NP)&& (uart_rx_swap==NP)) {
494525
if (HAL_HalfDuplex_Init(huart)!=HAL_OK) {
495526
return;
496527
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp