3 years, 9 months ago.

Problem with SPI format on Nucleo F767ZI

Nucleo F767ZI Trying to configure SPI to send 16 bits per frame. When I use SPI.format(16,0), SPI sends 32 bits on SPI_MOSI. If I specify anything else (from 1 to 15 and 17) it sends 8 bits.


#include "mbed.h"

DigitalOut cs(D8);

int main()
    device.format(16,0); //16 bits per frame, mode 0
    cs = 1;
    while(1) {
        cs = 0;
        cs = 1;

As shown, SPI_MOSI will send 32 bits. 8 zeros, 0xAA, and 16 zeros.

I can get around it by doing two writes of 8 bits each, leaving cs low, but it seems like 16 should work. I can attach a scope screen cap if helpful.

1 Answer

3 years, 9 months ago.

I think that issue was reported before here. It should have been solved in a later release of the mbed lib. See here. Make sure you update the library (right click mbed lib in your project).

Also please use <<code>> and <</code>> tags on separate lines around your posted code to keep it readable.

Accepted Answer

After updating to latest library (157), it now works for 8 bit and 16 bit frames. So thanks for that suggestion!

But if you specify 4 bits or 12 bits, you will get 8 bit frames, so there is apparently still a problem.

posted by Paul Franklin 22 Dec 2017

Most SPI hardware engines do not support messagesizes that are not a multiple of 8. You will get the nearest 8bit value in case you try selecting that. Check the datasheet to confirm details for your platform. In case you do need such a value you can instead use a SPI software implementation that bit bangs the pins.

posted by Wim Huiskamp 22 Dec 2017