Skip to content

Allow serial buffers of different sizes #113

Open
@jerabaul29

Description

@jerabaul29

It looks like for now all serial buffers have the same size. This is a bit annoying, as if one may expect a lot of input on a single RX channel for example, one needs to use a lot of RAM increasing the size of all serial buffers instead of just the one needed.

Do you think it would be possible to:

  • add a constexpr size_t buffer_size argument in the RingBuffer constructor, suppose that would be here:

RingBuffer( void ) ;

RingBuffer::RingBuffer( void )
{
memset( (void *)_aucBuffer, 0, SERIAL_BUFFER_SIZE ) ;
_iHead=0 ;
_iTail=0 ;
}

  • use this constexpr size as the size of the internal array, and store it in the class so that it remains aware of its size, ie adding 1 internal size_t field in the header declaration, storing it in the constructor, and replacing in the implementation calls to the macro size by call to this field:

int i = (uint32_t)(_iHead + 1) % SERIAL_BUFFER_SIZE ;

  • add to the UART and USART the same constexpr size argument, so that we can propagate it from the UART / USART creation to the buffer

  • specify independently the size of each RX and TX buffer. The values could be given through macros, so that this is easily overwritten by a compiler flag. I think that would take place here, right?

/*
* UART objects
*/
RingBuffer rx_buffer1;
RingBuffer tx_buffer1;
UARTClass Serial(UART, UART_IRQn, ID_UART, &rx_buffer1, &tx_buffer1);
void serialEvent() __attribute__((weak));
void serialEvent() { }
// IT handlers
void UART_Handler(void)
{
Serial.IrqHandler();
}
// ----------------------------------------------------------------------------
/*
* USART objects
*/
RingBuffer rx_buffer2;
RingBuffer rx_buffer3;
RingBuffer rx_buffer4;
RingBuffer tx_buffer2;
RingBuffer tx_buffer3;
RingBuffer tx_buffer4;
USARTClass Serial1(USART0, USART0_IRQn, ID_USART0, &rx_buffer2, &tx_buffer2);
void serialEvent1() __attribute__((weak));
void serialEvent1() { }
USARTClass Serial2(USART1, USART1_IRQn, ID_USART1, &rx_buffer3, &tx_buffer3);
void serialEvent2() __attribute__((weak));
void serialEvent2() { }
USARTClass Serial3(USART3, USART3_IRQn, ID_USART3, &rx_buffer4, &tx_buffer4);
void serialEvent3() __attribute__((weak));
void serialEvent3() { }

Any problem you would see with such an approach? Any difficulty I I forgotten / anything wrong with this idea?

I suppose this would be applicable in general to all Arduino cores, right?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions