Nucleo-F103RB failed in simple SPI bus init routines.

25 Jan 2017

I am trying to use mbed in LoRaPHY testing, which demands simple resources for:

  • external interrupt for DIO0 of SX127X;
  • SPI bus to SX127X;
  • serial port to host;
  • I2C bus to NVMEM.

I have used F103RB and mbed in some practical projects, I shouldn't raise such entry level problems. But it indeed bothers me for some days. If I added SPI into the project, the application will never go to main, it will trapped into an endless loop in class init code inside mbed library.

#include "mbed.h"

DigitalOut myled(LED1);

InterruptIn button(USER_BUTTON);
//AnalogIn testadc11(USERADC1);
AnalogIn testadc(A0);
//SPI device(SPI_MOSI, SPI_MISO, SPI_SCK);
//SPI device(SPI_MOSI, SPI_MISO, SPI_SCK, NC);
I2C i2c(I2C_SDA, I2C_SCL);
Serial pc(SERIAL_TX, SERIAL_RX);

void key_isr() {
    myled = !myled;
}

void setup() {
    button.fall(&key_isr);
}
        
int main() {
    int i=0;
    setup();
    while(1) {
        myled = 1; // LED is ON
        wait(0.2); // 200 ms
        myled = 0; // LED is OFF
        wait(1.0); // 1 sec
        //device.write(0x55);
        //device.write(i++);
        //device.write(0xE0);
        pc.printf("test\r\n");
    }
}

If I comment out SPI code, the LED can toggle, keyboard works as well. If I enable SPI code, main entry loop will not be hit, if we see the code in uvision4, we can find it is in an endless loop.

Is there any wrong in my code or mbed library for STM32F103RB? I use similiar code in LPC812, it works.

25 Jan 2017

Here I CLOSED the issue/question regarding SPI master of Nucleo-STM32F103RB. After checking the Pinnames.h in mbed, I found LED1/2/3/4 are assigned to PA_5, which is conflict with SPI.

DigitalOut myled(LED1); // PA_5
SPI device(SPI_MOSI, SPI_MISO, SPI_SCK);  // PA_7, PA_6, PA_5

So I use another GPIO for LED, problem solved. However, after reviewing schematics of Nucleo, I found the pin layout should be changed for practical project. For example, 2nd SPI is used, 1st I2C is used to GPIO...... I would rather to follow the native pinout of STM32, instead of Arduino pinout.

Since Nucleo-64 is almost identical, I would like to remind other developers to realize this issue. Maybe we should use Morph port instead of Arduino port.

05 Apr 2017

How did you get the SPI Device to work? I think I am having a similar issue using the SPI enabled BLE shield from ST. We are not using the LED1 digital output at all in our code but I imagine mbed still does something with this in the OS which keeps SPI from working. Is there a way to get into the MBED Code and remove the use of "LED1" (we never use it in any of our projects).