Fast SPI with large data on STM32U5 ( HAL vs register level)
- January 27, 2022
- 4 replies
- 4800 views
Dear all,
Can someone help me on that topic ?
Goal :
I need to send a large amount of 8 bit via SPI as fast as possible (SPI display).
The frame buffer is around 160 000b so size need to be uint32_t.
Strategy :
Use SPI register to avoid 16bit size HAL limitation.
Use HAL_SPI_TRANSFERT as reference in terms of performance, register manipulation should give us a speed rate >= HAL.
Start with small framebuffer ie : uint8_t image[1000]
Environment:
I am working with a nucloSTM32U5 on SPI2 with no slave attached, but CLK and MOSI monitored.
See attached configuration
The code:
void xSPI_Send_Data( SPI_TypeDef *spi, uint8_t *pdata, uint32_t size)
{
// Disable SPI2
CLEAR_BIT(spi->CR1, SPI_CR1_SPE);
// Set TSIZE to 0 (defaut value) for unknown size transfert
MODIFY_REG(spi->CR2, SPI_CR2_TSIZE, 0);
// Enable SPI2
if ((spi->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) {
// If disabled, I enable it
SET_BIT(spi->CR1, SPI_CR1_SPE);
}
// Master transfert start
SET_BIT(spi->CR1, SPI_CR1_CSTART);
// Transfert in 8 bit Mode
while (size > 0)
{
if(SPI_IsActiveFlag_TXP)
{
*(volatile uint8_t *)&spi->TXDR = *pdata;
pdata += sizeof(uint8_t);
size--;
//HAL_Delay(10);
}
}
// Disable SPI2
CLEAR_BIT(spi->CR1, SPI_CR1_SPE);
}
uint8_t SPI_IsActiveFlag_TXP(SPI_TypeDef *SPIx)
{
return ((READ_BIT(SPIx->SR, SPI_SR_TXP) == (SPI_SR_TXP)) ? 1 : 0);
}Results :
Can't manage to get all the datas (see attached) and in terms of speed rate i am about 10-15%% faster then HAL library but with data corruption.
Next ? :
I tried to add some tests on the register code, datas are fine but the speed rate is divided by 2.
Can someone point me out what I am missing here ?
Thanks
