4 years, 7 months ago.

SPI Values are changing with NUCLEO-L432KC

Hey I am having a weird issue where the values of from my ADC are vastly different for this board compared to both the mbed LPC1768F. The same code is used for all three and I checked the connections on the board and there is no issues there. I am just confused why the nucleo l432 is giving different values over my serial terminal. Code is located at link below:


I did some more debugging and I realized the biggest discrepancy I can find is that my clock for the nucleo only has 8 ticks (which is the one that is giving false values) and the LPC1768F has 12 ticks. I am transmitting 12 bits over the SPI protocol

/media/uploads/johnla/problemspi.png /media/uploads/johnla/workingspi-matched.png

There is a lot going on here, so it's hard to say. You are powering the ADC and giving it the same Vref and ground in all cases? Obviously ADC is only gives a scaled value so if you change either the signal or reference you will get a different result. How are you controlling the ADC reading so you are sure it is actually the same each time? If you have an ADC issue you should always grab a multimeter and start measuring voltages and find where it is not matching your expectations. Check the voltages at the ADC both CH input and Vref.

Your serial baud rate seems crazy high. Make sure you are not just garbling the data when you try to display it on the computer.


Is there a reason not to use the built in ADCs? Seems like a lot of work hooking up an external chip when micros are very capable of this.


posted by Graham S. 17 Mar 2017

Hi Graham, we did try using a logic level converter, but that did not fix the issue. The high baud rate works fine in our case, as verified by the logic analyzer. We are not using the built in ADCs as we would eventually have more inputs that the available ADCs. Thanks for the suggestions though!

posted by Michael Chuah 06 Apr 2017

1 Answer

4 years, 6 months ago.

Some devices dont support all SPI word lengths. The nucleo SPI hardware engine may only support 8 or 16 bit format. You may be able to talk to the device using two 8 bit transfers and reconstructing that result back into a 12 bit word.

Hi Wim, thank you for your comment! The Nucleo SPI word length does does seem to be the issue. Using Dave Van Wagner's awesome SWSPI library fixes this issue, and the values are as expected.

However, when looking at the STM32L4 datasheet, it does seem to indicate that it should be able to handle variable frame sizes. From Section 3.26 (pg 41), "The 3-bit prescaler gives 8 master mode frequencies and the frame size is configurable from 4 bits to 16 bits."

Also there is nothing in spi_api.c that indicates that it would be an issue, whereas for the Nordic chip, it clearly states that: if (bits != 8) { error("Only 8-bits SPI is supported\r\n"); }

Would you have any suggestions on how to avoid such issues in the future?

SWSPI (https://developer.mbed.org/users/davervw/code/SWSPI/)

STM32L4 datasheet (http://www.st.com/content/ccc/resource/technical/document/datasheet/24/01/9f/59/f0/83/47/fc/DM00257205.pdf/files/DM00257205.pdf/jcr:content/translations/en.DM00257205.pdf)

STM32L4 spi_api.c (https://developer.mbed.org/users/mbed_official/code/mbed-dev/file/d5399cc887bb/targets/TARGET_STM/TARGET_STM32L4/spi_api.c)

Nordic spi_api.c (https://developer.mbed.org/users/mbed_official/code/mbed-dev/file/d5399cc887bb/targets/TARGET_NORDIC/TARGET_NRF5/spi_api.c)

posted by Michael Chuah 06 Apr 2017

The L4 may support other framesizes, but some other members of the STM family may not. They now use a common lib across all devices for most of the SPI methods. See https://developer.mbed.org/users/mbed_official/code/mbed-dev/file/d5399cc887bb/targets/TARGET_STM/stm_spi_api.c

That file has the following code for the format method:

void spi_format(spi_t *obj, int bits, int mode, int slave)
    struct spi_s *spiobj = SPI_S(obj);
    SPI_HandleTypeDef *handle = &(spiobj->handle);
    DEBUG_PRINTF("spi_format, bits:%d, mode:%d, slave?:%d\r\n", bits, mode, slave);
    // Save new values
    handle->Init.DataSize          = (bits == 16) ? SPI_DATASIZE_16BIT : SPI_DATASIZE_8BIT;

So it doesnt like anything but 16 or 8 bits..

You would have to modify that library code or just bypass it and manipulate the L4 registers for SPI yourself.

posted by Wim Huiskamp 07 Apr 2017