Skip to content

SPI with the R4 is slower than with the R3 #28

Closed
@RudolphRiedel

Description

@RudolphRiedel

The SPI with the R4 is really slow, way slower than with the R3 when running the same clock and the same code.

SPI_Arduino_Uno.zip

This has three files for use with Saleae Logic 2.

  • SPI_Arduino_Uno_R3_SPI_Transfer.sal - single byte SPI.transfer()
  • SPI_Arduino_Uno_R4_SPI_Transfer.sal - single byte SPI.transfer()
  • SPI_Arduino_Uno_R4_SPI_Transfer_Buffer.sal - buffer transmit, for 4 bytes and for the whole large buffer

The "large" transfer has 3 bytes address + 228 bytes data.
R3: 524µs including all calculations necessary for that data
R4: 2.52ms including all calculations necessary for that data - so this takes almost five times longer
R4 buffer: 634µs without any calculations, only transferring the buffer

Is this something that is actively looked into so far?

The underlying FSP library from Renesas is rather slow.
It configures the SPI on every single transfer, even going as far as activating and deactivating the SPI thru the enable bit.

On the R3 I get <2µs pauses between two transfers.
On the R4 there are 11µs between two transfers - at three times the core clock.

I modified SPI.cpp to use a busy loop for with TX only for the buffer transfer:

uint8_t *buffer = (uint8_t *) buf;

_spi_ctrl.p_regs->SPCR = 0x4a; /* enable, no irq, master, tx only */

for(size_t index = 0; index < count; index++)
{
    _spi_ctrl.p_regs->SPDR_BY = buffer[index];
    while (0 == _spi_ctrl.p_regs->SPSR_b.SPTEF) {}
}
    while (_spi_ctrl.p_regs->SPSR_b.IDLNF) {}

_spi_ctrl.p_regs->SPCR = 0xb9; /* off, interrupts, master, full duplex */

This brought down the time to transfer the buffer to 310us with 400ns pauses.
Still not what the RA4M1 could do but at least it beats the AVR on the R3.
So this is entirely an software issue.

A couple of function calls into R_SPI_WriteRead() to check out what is going on there I found that
the buffer is send with an interrupt function.
So essentially it looks like the function is spending more time going into and out of that interrupt than it takes to transfer a byte.

And on top the RA4M1 has DMA, please make use of it.
Preferably take inspiration from the Teensy 4 SPI class that has DMA support baked in to allow this:

SPI.transfer(source, dest, count, event);
SPI.transfer( ((uint8_t *) &EVE_dma_buffer[0]) + 1U, NULL, (((EVE_dma_buffer_index) * 4U) - 1U), EVE_spi_event);

This "event" is for a callback function, a custom one that can be attached to the event and allows code to be executed when the DMA is done like setting CS to high again.
And setting "dest" to NULL makes the operation write only.

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