Skip to content

HardwareSerial: don't call STM32 HAL for every bytes to transfer #1313

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

Merged
merged 1 commit into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
63 changes: 52 additions & 11 deletions cores/arduino/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ int HardwareSerial::_tx_complete_irq(serial_t *obj)
{
// If interrupts are enabled, there must be more data in the output
// buffer. Send the next byte
obj->tx_tail = (obj->tx_tail + 1) % SERIAL_TX_BUFFER_SIZE;
obj->tx_tail = (obj->tx_tail + obj->tx_size) % SERIAL_TX_BUFFER_SIZE;

if (obj->tx_head == obj->tx_tail) {
return -1;
Expand Down Expand Up @@ -467,8 +467,12 @@ void HardwareSerial::flush()
// the hardware finished tranmission (TXC is set).
}

size_t HardwareSerial::write(uint8_t c)
size_t HardwareSerial::write(const uint8_t *buffer, size_t size)
{
tx_buffer_index_t i;
size_t size_tmp;
size_t ret = size;

_written = true;
if (isHalfDuplex()) {
if (_rx_enabled) {
Expand All @@ -477,22 +481,59 @@ size_t HardwareSerial::write(uint8_t c)
}
}

tx_buffer_index_t i = (_serial.tx_head + 1) % SERIAL_TX_BUFFER_SIZE;
// If necessary split transfert till end of TX buffer
while (_serial.tx_head + size > SERIAL_TX_BUFFER_SIZE) {
size_t size_intermediate = SERIAL_TX_BUFFER_SIZE - _serial.tx_head;

// If the output buffer is full, there's nothing for it other than to
// wait for the interrupt handler to empty it a bit
while (i == _serial.tx_tail) {
// nop, the interrupt handler will free up space for us
write(buffer, size_intermediate);
size -= size_intermediate;
buffer += size_intermediate;
}

// Here size if less or equal to SERIAL_TX_BUFFER_SIZE, but SERIAL_TX_BUFFER_SIZE is not possible as tx_head = tx_tail is ambiguous empty or full
if (size == SERIAL_TX_BUFFER_SIZE) {
size_t size_intermediate = SERIAL_TX_BUFFER_SIZE - 1;

write(buffer, size_intermediate);
size -= size_intermediate;
buffer += size_intermediate;
}

size_tmp = size;

while (size_tmp) {
i = (_serial.tx_head + 1) % SERIAL_TX_BUFFER_SIZE;


// If the output buffer is full, there's nothing for it other than to
// wait for the interrupt handler to empty it a bit
while (i == _serial.tx_tail) {
// nop, the interrupt handler will free up space for us
}
_serial.tx_buff[_serial.tx_head] = *buffer;
_serial.tx_head = i;
size_tmp --;
buffer ++;
}

while ((_serial.tx_head != (_serial.tx_tail + size) % SERIAL_TX_BUFFER_SIZE)) {
// nop, previous transfert no yet completed
}

_serial.tx_buff[_serial.tx_head] = c;
_serial.tx_head = i;
_serial.tx_size = size;

if (!serial_tx_active(&_serial)) {
uart_attach_tx_callback(&_serial, _tx_complete_irq);
uart_attach_tx_callback(&_serial, _tx_complete_irq, size);
}

return 1;
/* There is no real error management so just return transfer size requested*/
return ret;
}

size_t HardwareSerial::write(uint8_t c)
{
uint8_t buff = c;
return write(&buff, 1);
}

void HardwareSerial::setRx(uint32_t _rx)
Expand Down
3 changes: 2 additions & 1 deletion cores/arduino/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ class HardwareSerial : public Stream {
{
return write((uint8_t)n);
}
using Print::write; // pull in write(str) and write(buf, size) from Print
size_t write(const uint8_t *buffer, size_t size);
using Print::write; // pull in write(str) from Print
operator bool()
{
return true;
Expand Down
3 changes: 2 additions & 1 deletion cores/arduino/stm32/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ struct serial_s {
uint16_t tx_head;
volatile uint16_t rx_head;
volatile uint16_t tx_tail;
size_t tx_size;
};

/* Exported constants --------------------------------------------------------*/
Expand Down Expand Up @@ -186,7 +187,7 @@ void uart_config_lowpower(serial_t *obj);
size_t uart_write(serial_t *obj, uint8_t data, uint16_t size);
int uart_getc(serial_t *obj, unsigned char *c);
void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t *));
void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t *));
void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t *), size_t size);

uint8_t serial_tx_active(serial_t *obj);
uint8_t serial_rx_active(serial_t *obj);
Expand Down
11 changes: 4 additions & 7 deletions libraries/SrcWrapper/src/stm32/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ void uart_attach_rx_callback(serial_t *obj, void (*callback)(serial_t *))
* @param callback : function call at the end of transmission
* @retval none
*/
void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t *))
void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t *), size_t size)
{
if (obj == NULL) {
return;
Expand All @@ -734,7 +734,7 @@ void uart_attach_tx_callback(serial_t *obj, int (*callback)(serial_t *))
HAL_NVIC_DisableIRQ(obj->irq);

/* The following function will enable UART_IT_TXE and error interrupts */
HAL_UART_Transmit_IT(uart_handlers[obj->index], &obj->tx_buff[obj->tx_tail], 1);
HAL_UART_Transmit_IT(uart_handlers[obj->index], &obj->tx_buff[obj->tx_tail], size);

/* Enable interrupt */
HAL_NVIC_EnableIRQ(obj->irq);
Expand Down Expand Up @@ -810,11 +810,8 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
serial_t *obj = get_serial_obj(huart);

if (obj && obj->tx_callback(obj) != -1) {
if (HAL_UART_Transmit_IT(huart, &obj->tx_buff[obj->tx_tail], 1) != HAL_OK) {
return;
}
if (obj) {
obj->tx_callback(obj);
}
}

Expand Down