Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[U(S)ART] hardware flow control support #1634

Merged
merged 6 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions cores/arduino/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ void HardwareSerial::init(PinName _rx, PinName _tx)
_serial.pin_rx = _rx;
}
_serial.pin_tx = _tx;
_serial.pin_rts = NC;
fpistm marked this conversation as resolved.
Show resolved Hide resolved
_serial.pin_cts = NC;
_serial.rx_buff = _rx_buffer;
_serial.rx_head = 0;
_serial.rx_tail = 0;
Expand Down Expand Up @@ -575,6 +577,38 @@ void HardwareSerial::setTx(PinName _tx)
_serial.pin_tx = _tx;
}

void HardwareSerial::setRts(uint32_t _rts)
{
_serial.pin_rts = digitalPinToPinName(_rts);
}

void HardwareSerial::setCts(uint32_t _cts)
{
_serial.pin_cts = digitalPinToPinName(_cts);
}

void HardwareSerial::setRtsCts(uint32_t _rts, uint32_t _cts)
{
_serial.pin_rts = digitalPinToPinName(_rts);
_serial.pin_cts = digitalPinToPinName(_cts);
}

void HardwareSerial::setRts(PinName _rts)
{
_serial.pin_rts = _rts;
}

void HardwareSerial::setCts(PinName _cts)
{
_serial.pin_cts = _cts;
}

void HardwareSerial::setRtsCts(PinName _rts, PinName _cts)
{
_serial.pin_rts = _rts;
_serial.pin_cts = _cts;
}

void HardwareSerial::setHalfDuplex(void)
{
_serial.pin_rx = NC;
Expand Down
8 changes: 8 additions & 0 deletions cores/arduino/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ class HardwareSerial : public Stream {
void setRx(PinName _rx);
void setTx(PinName _tx);

// Enable HW flow control on RTS, CTS or both
void setRts(uint32_t _rts);
void setCts(uint32_t _cts);
void setRtsCts(uint32_t _rts, uint32_t _cts);
void setRts(PinName _rts);
void setCts(PinName _cts);
void setRtsCts(PinName _rts, PinName _cts);

// Enable half-duplex mode by setting the Rx pin to NC
// This needs to be done before the call to begin()
void setHalfDuplex(void);
Expand Down
2 changes: 2 additions & 0 deletions cores/arduino/stm32/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ struct serial_s {
int (*tx_callback)(serial_t *);
PinName pin_tx;
PinName pin_rx;
PinName pin_rts;
PinName pin_cts;
IRQn_Type irq;
uint8_t index;
uint8_t recv;
Expand Down
31 changes: 30 additions & 1 deletion libraries/SrcWrapper/src/stm32/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
/* Determine the U(S)ART peripheral to use (USART1, USART2, ...) */
USART_TypeDef *uart_tx = pinmap_peripheral(obj->pin_tx, PinMap_UART_TX);
USART_TypeDef *uart_rx = pinmap_peripheral(obj->pin_rx, PinMap_UART_RX);
USART_TypeDef *uart_rts = pinmap_peripheral(obj->pin_rts, PinMap_UART_RTS);
USART_TypeDef *uart_cts = pinmap_peripheral(obj->pin_cts, PinMap_UART_CTS);

/* Pin Tx must not be NP */
if (uart_tx == NP) {
Expand All @@ -121,6 +123,16 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
core_debug("ERROR: [U(S)ART] Rx pin has no peripheral!\n");
return;
}
/* Pin RTS must not be NP if flow control is enabled */
if ((obj->pin_rts != NC) && (uart_rts == NP)) {
core_debug("ERROR: [U(S)ART] RTS pin has no peripheral!\n");
return;
}
/* Pin CTS must not be NP if flow control is enabled */
if ((obj->pin_cts != NC) && (uart_cts == NP)) {
core_debug("ERROR: [U(S)ART] CTS pin has no peripheral!\n");
return;
}

/*
* Get the peripheral name (USART1, USART2, ...) from the pin
Expand All @@ -131,6 +143,12 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
if (obj->uart == NP) {
core_debug("ERROR: [U(S)ART] Rx and Tx pins peripherals mismatch!\n");
return;
} else if (uart_rts != NP && obj->uart != uart_rts) {
core_debug("ERROR: [U(S)ART] Rx/Tx and RTS pins peripherals mismatch!\n");
return;
} else if (uart_cts != NP && obj->uart != uart_cts) {
core_debug("ERROR: [U(S)ART] Rx/Tx and CTS pins peripherals mismatch!\n");
return;
}

/* Enable USART clock */
Expand Down Expand Up @@ -290,6 +308,17 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
pinmap_pinout(obj->pin_rx, PinMap_UART_RX);
}

/* Configure flow control */
fpistm marked this conversation as resolved.
Show resolved Hide resolved
uint32_t flow_control = UART_HWCONTROL_NONE;
if (uart_rts != NP) {
flow_control |= UART_HWCONTROL_RTS;
pinmap_pinout(obj->pin_rts, PinMap_UART_RTS);
}
if (uart_cts != NP) {
flow_control |= UART_HWCONTROL_CTS;
pinmap_pinout(obj->pin_cts, PinMap_UART_CTS);
}

/* Configure uart */
uart_handlers[obj->index] = huart;
huart->Instance = (USART_TypeDef *)(obj->uart);
Expand All @@ -298,7 +327,7 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
huart->Init.StopBits = stopbits;
huart->Init.Parity = parity;
huart->Init.Mode = UART_MODE_TX_RX;
huart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart->Init.HwFlowCtl = flow_control;
huart->Init.OverSampling = UART_OVERSAMPLING_16;
#if !defined(STM32F1xx) && !defined(STM32F2xx) && !defined(STM32F4xx)\
&& !defined(STM32L1xx)
Expand Down