Jump to content

Trap for young players - STM32's SPIv2 has data packing


Recommended Posts

I copied board_ILI9341_spi.h from boards/addons/gdisp for my nucleo32 STM32L432 project

Couldn't get the LCD working, so I attached a logic analyser and ever byte that was being written was followed by a 0x00, which was obviously screwing up the init procedure.

Turns out on the newer STM32's "SPI2->DR = data;" is a 16bit write to the DR register, which puts the low byte on the FIFO, followed by the high byte - always zero in this case as it's supposed to be an 8 bit transfer.

This makes the SPI device perform two 8 bit transfers.

I finally found my google-foo this morning after a few wasted hours last night and you need to cast the DR register to a uint8_t when you do the write:

*((uint8_t*)&SPI3->DR) = (uint8_t) data;

The STM32F4's don't do data packing on the DR register, but the newer L4's do. I assume other recent STM32's do as well.

Link to comment
Share on other sites

It was a bit confusing for me, the first half of my project was SPI to an RF module. This isn't an issue when using ChibiOS's SPI driver (that internally uses DMA, not direct register stuff)

The ILI9341 driver doesn't appear to be DMA friendly, only giving the low level single byte method for data transfers so I didn't try it.

Maybe that will be a later task, writing a DMA friendly driver for it. I assume it would just need refactoring the data transfer method to pass an array/offset/length 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...