Jump to content
chrisdf

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.

Share this post


Link to post
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 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...