Skip to content

Commit af08f5c

Browse files
committed
feat(uart): support UART Tx Rx invert function
This enables UART Tx and Rx invert function on STM32 families that support it. In order to enable Tx and Rx invert, call respectively: ```c++ Serial1.setTxInvert(); Serial1.setRxInvert(); ``` Fixes: stm32duino#1160 stm32duino#2669 See also: stm32duino#1418 Signed-off-by: Andrew Yong <me@ndoo.sg>
1 parent df761bf commit af08f5c

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

cores/arduino/HardwareSerial.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ void HardwareSerial::begin(unsigned long baud, byte config)
446446
break;
447447
}
448448

449-
uart_init(&_serial, (uint32_t)baud, databits, parity, stopbits);
449+
uart_init(&_serial, (uint32_t)baud, databits, parity, stopbits, _rx_invert, _tx_invert);
450450
enableHalfDuplexRx();
451451
uart_attach_rx_callback(&_serial, _rx_complete_irq);
452452
}
@@ -668,4 +668,14 @@ void HardwareSerial::enableHalfDuplexRx(void)
668668
}
669669
}
670670

671+
void HardwareSerial::setRxInvert(void)
672+
{
673+
_rx_invert = true;
674+
}
675+
676+
void HardwareSerial::setTxInvert(void)
677+
{
678+
_tx_invert = true;
679+
}
680+
671681
#endif // HAL_UART_MODULE_ENABLED && !HAL_UART_MODULE_ONLY

cores/arduino/HardwareSerial.h

+7
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class HardwareSerial : public Stream {
9595
protected:
9696
// Has any byte been written to the UART since begin()
9797
bool _written;
98+
bool _rx_invert;
99+
bool _tx_invert;
98100

99101
// Don't put any members after these buffers, since only the first
100102
// 32 bytes of this struct can be accessed quickly using the ldd
@@ -165,6 +167,11 @@ class HardwareSerial : public Stream {
165167
bool isHalfDuplex(void) const;
166168
void enableHalfDuplexRx(void);
167169

170+
// Enable HW Rx/Tx inversion
171+
// This needs to be done before the call to begin()
172+
void setRxInvert(void);
173+
void setTxInvert(void);
174+
168175
friend class STM32LowPower;
169176

170177
// Interrupt handlers

libraries/SrcWrapper/inc/uart.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ struct serial_s {
254254

255255
/* Exported macro ------------------------------------------------------------*/
256256
/* Exported functions ------------------------------------------------------- */
257-
void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t parity, uint32_t stopbits);
257+
void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t parity, uint32_t stopbits, uint8_t rx_invert, uint8_t tx_invert);
258258
void uart_deinit(serial_t *obj);
259259
#if defined(HAL_PWR_MODULE_ENABLED) && (defined(UART_IT_WUF) || defined(LPUART1_BASE))
260260
void uart_config_lowpower(serial_t *obj);

libraries/SrcWrapper/src/stm32/uart.c

+16-4
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ serial_t *get_serial_obj(UART_HandleTypeDef *huart)
115115
* @param obj : pointer to serial_t structure
116116
* @retval None
117117
*/
118-
void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t parity, uint32_t stopbits)
118+
void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t parity, uint32_t stopbits, uint8_t rx_invert, uint8_t tx_invert)
119119
{
120120
if (obj == NULL) {
121121
return;
@@ -408,10 +408,22 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
408408
huart->Init.HwFlowCtl = flow_control;
409409
huart->Init.OverSampling = UART_OVERSAMPLING_16;
410410
#if defined(UART_ADVFEATURE_SWAP_INIT)
411-
huart->AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_SWAP_INIT;
411+
huart->AdvancedInit.AdvFeatureInit |= UART_ADVFEATURE_SWAP_INIT;
412412
huart->AdvancedInit.Swap = pin_swap;
413413
#elif defined(UART_ADVFEATURE_NO_INIT)
414-
huart->AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
414+
huart->AdvancedInit.AdvFeatureInit |= UART_ADVFEATURE_NO_INIT;
415+
#endif
416+
#if defined(UART_ADVFEATURE_RXINVERT_INIT)
417+
if (rx_invert) {
418+
huart->AdvancedInit.AdvFeatureInit |= UART_ADVFEATURE_RXINVERT_INIT;
419+
huart->AdvancedInit.RxPinLevelInvert = UART_ADVFEATURE_RXINV_ENABLE;
420+
}
421+
#endif
422+
#if defined(UART_ADVFEATURE_TXINVERT_INIT)
423+
if (tx_invert) {
424+
huart->AdvancedInit.AdvFeatureInit |= UART_ADVFEATURE_TXINVERT_INIT;
425+
huart->AdvancedInit.TxPinLevelInvert = UART_ADVFEATURE_TXINV_ENABLE;
426+
}
415427
#endif
416428
#ifdef UART_ONE_BIT_SAMPLE_DISABLE
417429
huart->Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
@@ -812,7 +824,7 @@ void uart_debug_init(void)
812824
serial_debug.pin_tx = pinmap_pin(DEBUG_UART, PinMap_UART_TX);
813825
#endif
814826
/* serial_debug.pin_rx set by default to NC to configure in half duplex mode */
815-
uart_init(&serial_debug, DEBUG_UART_BAUDRATE, UART_WORDLENGTH_8B, UART_PARITY_NONE, UART_STOPBITS_1);
827+
uart_init(&serial_debug, DEBUG_UART_BAUDRATE, UART_WORDLENGTH_8B, UART_PARITY_NONE, UART_STOPBITS_1, false, false);
816828
}
817829
}
818830

0 commit comments

Comments
 (0)