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

Commitaf08f5c

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:#1160#2669See also:#1418Signed-off-by: Andrew Yong <me@ndoo.sg>
1 parentdf761bf commitaf08f5c

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

‎cores/arduino/HardwareSerial.cpp‎

Lines changed: 11 additions & 1 deletion
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+
voidHardwareSerial::setRxInvert(void)
672+
{
673+
_rx_invert =true;
674+
}
675+
676+
voidHardwareSerial::setTxInvert(void)
677+
{
678+
_tx_invert =true;
679+
}
680+
671681
#endif// HAL_UART_MODULE_ENABLED && !HAL_UART_MODULE_ONLY

‎cores/arduino/HardwareSerial.h‎

Lines changed: 7 additions & 0 deletions
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
boolisHalfDuplex(void)const;
166168
voidenableHalfDuplexRx(void);
167169

170+
// Enable HW Rx/Tx inversion
171+
// This needs to be done before the call to begin()
172+
voidsetRxInvert(void);
173+
voidsetTxInvert(void);
174+
168175
friendclassSTM32LowPower;
169176

170177
// Interrupt handlers

‎libraries/SrcWrapper/inc/uart.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ struct serial_s {
254254

255255
/* Exported macro ------------------------------------------------------------*/
256256
/* Exported functions ------------------------------------------------------- */
257-
voiduart_init(serial_t*obj,uint32_tbaudrate,uint32_tdatabits,uint32_tparity,uint32_tstopbits);
257+
voiduart_init(serial_t*obj,uint32_tbaudrate,uint32_tdatabits,uint32_tparity,uint32_tstopbits,uint8_trx_invert,uint8_ttx_invert);
258258
voiduart_deinit(serial_t*obj);
259259
#if defined(HAL_PWR_MODULE_ENABLED)&& (defined(UART_IT_WUF)|| defined(LPUART1_BASE))
260260
voiduart_config_lowpower(serial_t*obj);

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

Lines changed: 16 additions & 4 deletions
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-
voiduart_init(serial_t*obj,uint32_tbaudrate,uint32_tdatabits,uint32_tparity,uint32_tstopbits)
118+
voiduart_init(serial_t*obj,uint32_tbaudrate,uint32_tdatabits,uint32_tparity,uint32_tstopbits,uint8_trx_invert,uint8_ttx_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
#ifdefUART_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)

[8]ページ先頭

©2009-2025 Movatter.jp