Problem in initializing SPI with ST32L476xG

We are trying to initialize SPI on STM32L476xG with STLink Nucleo L476RG.

I am checking the ‘SPI_SCLK’ signal through oscillopscope, but not getting any output. Could someone please help me with this? If am doing something wrong here?

Here is the code:

#include “mbed.h”
#include “LSM9DS1.h”
#include “mbed_power_mgmt.h”

const PinName spi_MOSI = PinName::PA_7;
const PinName spi_MISO = PinName::PA_6;
const PinName spi_CLK = PinName::PA_5;
const PinName spi_CS = PinName::PB_6;
SPI spi(spi_MOSI, spi_MISO, spi_CLK);
DigitalOut cs(spi_CS);
short spi_addr;
short spi_data;

int main()
{
     //SPI config. 
     cs = 1;

     spi.format(8,3);
     spi.frequency(1000000); //1MHz

     printf("SPI Master Initialized \r\n");
     cs = 0;         // The SCLK should be working when the chip select is low.

     while(1) {
        spi.write(0xFD);
    }
    cs = 1;

    return 0;
}

Please could you let me know if am doing something wrong here? Or what could be the potential issues?

Thanks!
Regards,
Rohan

I think you will only get the clock signal output on SCLK if you invoke a spi.write() or spi.transfer, i.e., only when you’re actually transmitting data. Try sending a few dummy bytes in a while loop after pulling cs low and see what happens.

2 Likes

Thanks a lot for your response! I tried it, and have edited the code (as in the original post). But still not able to observe an SPI clock.
Also, as I just observe that the interface is SPI3 (and not simple SPI). Does that affect how am programming in any way?

Hmm. The code you provided seems to be alright. As shown on NUCLEO-L476RG | Mbed, the PA_7,6,5 pins refer to SPI1, not SPI3? Can you double check which pin on the Nucleo board you’re using / measuring?

1 Like