stm32f103 spi.h library doesn't work

Good day all,

I would just like to know how to redefine SPI pins. I'm using the an STM32F103 "blue pill".

i have added the board to my boards list and am able to use all my serial ports E.G Serial.begin(9600); or Serial2.begin(9600); both serial ports work just fine. but my SPI ports do not.

the stm32F103 has 2 SPI ports, SPI.begin(); doesn't seem to work with either SPI 1 or SPI 2 and spi2.begin wont compile.

I'm using the standard SPI.h library.

For your board the SPI pins are defined as follows:

#define PIN_SPI_SS              PA4
#define PIN_SPI_MOSI            PA7
#define PIN_SPI_MISO            PA6
#define PIN_SPI_SCK             PA5

the stm32F103 has 2 SPI ports, SPI.begin(); doesn't seem to work with either SPI 1 or SPI 2 and spi2.begin wont compile.

What do you expect SPI.begin() to do? You tell us that it doesn't work, do you get compilation warnings? You cannot get an error code as the method doesn't return anything. An SPI2 is not defined for your board in the standard library (but you might define it yourself).

I think it's best if you post your code or your pins file. It's difficult to visualize the errors you are receiving or what could be wrong.

Good day Pylon , Freaklabs.

Apologies my explanation is a bit vague.

i am trying to use a RC522 with the stm32.

i would like to using the 2nd SPI port.

PIN_SPI_SS              PB12
PIN_SPI_MOSI          PB15
PIN_SPI_MISO          PB14
PIN_SPI_SCK            PB13

I’m not too sure how to manually define the port.

however for testing purposes i am using the 1st SPI port and i had no response from the RC522.

please see attached project.

I think it has to do with the RC522 library itself.

since creating a loop to switch a led on if the serial port is open,

"while (!Serial);"

results in the led being on no matter what i try however,

"while (!Serial1);"

results in the led switching off.

I think the default SPI and Serial wont work they need to be refined in order to work with the library.

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9          // Configurable, see typical pin layout above
#define SS_PIN          14         // changed to PA4 (pin 14 SPI1 NSS) Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance

/**
* Check firmware only once at startup
*/
void setup() {
 Serial1.begin(9600);   // Initialize serial communications with the PC
 while (!Serial1);      // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
 SPI.begin();          // Init SPI bus
 //mfrc522.PCD_Init();   // Init MFRC522 module
 
 Serial1.println(F("*****************************"));
 Serial1.println(F("MFRC522 Digital self test"));
 Serial1.println(F("*****************************"));
 mfrc522.PCD_DumpVersionToSerial();  // Show version of PCD - MFRC522 Card Reader
 Serial1.println(F("-----------------------------"));
 Serial1.println(F("Only known versions supported"));
 Serial1.println(F("-----------------------------"));
 Serial1.println(F("Performing test..."));
 bool result = mfrc522.PCD_PerformSelfTest(); // perform the test
 Serial1.println(F("-----------------------------"));
 Serial1.print(F("Result: "));
 if (result)
   Serial1.println(F("OK"));
 else
   Serial1.println(F("DEFECT or UNKNOWN"));
 Serial1.println();
}

void loop() {} // nothing to do

MFRC522-1.4.3.zip (1.12 MB)

i would like to using the 2nd SPI port.

Actually that variant doesn't have a second SPI port defined. You can try to define your own using the class SPIClass but I'm not an expert with that platform, I don't know which configurations are supported by the hardware.

I think the default SPI and Serial wont work they need to be refined in order to work with the library.

I don't think so but please explain, why you think this is the case (for the SPI).

however for testing purposes i am using the 1st SPI port and i had no response from the RC522.

The library you're using doesn't support to use another SPI interface. Are you willing to patch that library to support multiple SPI interfaces?

Hi Pylon,

I don't think so but please explain, why you think this is the case (for the SPI).

i think so because with a normal Arduino board "I'm using a Pro Mini" everything works 100%.

with the pro mini the project complies with E.G:Serial.begin and SPI.begin, however with the STM32 the project will not compile with Serial.begin , you need to define which port E.G: Serial1.begin.

i suspect the RC522 library is hard coded to use Serial vs Serial1 or so forth as anything i state using the serial port works EG: Serial1.println(F("TEST")); works but since i'm pointing data to the library using

mfrc522.PCD_DumpVersionToSerial();

i suspect the library is trying to use Serial vs Serial1. so even if the spi port is working i have no way of knowing since the library could be using the wrong serial port.

The library you're using doesn't support to use another SPI interface. Are you willing to patch that library to support multiple SPI interfaces?

i have been trying to do this for the last day or so but i honestly cant see where or how its defined.

with the pro mini the project complies with E.G:Serial.begin and SPI.begin, however with the STM32 the project will not compile with Serial.begin , you need to define which port E.G: Serial1.begin.

Bullshit, the following sketch compiles fine for Arduino Mini and for a STM32F103 Bluepill.

#include <SPI.h>

void setup() {
  SPI.begin();
  Serial.begin(9600);
}


void loop() {
  
}

SPIClass mySPI(1); //Create an SPI instance on SPI1 port

Hello,

I want to share what I experienced in making the system
who implemented STM32F103C [STM32F103C8T6] stm32duino

in 2019,
Initially, I used the Arduino IDE v.1.5.x software
and using EThernet_STM.h
but because of the design needs that require A0-A9 as a digital input
10-bit absolute rotary sensor, then SPI must be moved from SPI-1 to SPI-2
in this condition I modify the W550.cpp to be like this
C: \ Users \ [your PC] \ AppData \ Local \ Arduino15 \ packages \ stm32duino \ hardware \ STM32F1 \ 2019.12.31 \ libraries \ Ethernet_STM \ src \ utility


#include <stdio.h>
#include <string.h>
#include <SPI.h> // add by riky

#include “utility / w5100.h”
#if defined (W5500_ETHERNET_SHIELD)

// W5500 controller instance
W5500Class W5100;

#define SPI_CS 10
// # define STM32_SPI_CS PA4 // edit by riky
#define STM32_SPI_CS PB12 // change by riky
// # define SPI2_NSS_PIN PB12 // add by riky
SPIClass SPI_2 (2); // add by riky

void W5500Class :: init (void)


etc


but after I upgraded the Arduino IDE 1.5.x to v.18.x
can no longer … :frowning:
so I have to do more, and pray to the Lord Jesus
… finally God showed me a directory and a file
and directs me to have to modify the variant.h file that’s in
C: \ Users \ [yourPC] \ AppData \ Local \ Arduino15 \ packages \ STM32 \ hardware \ stm32 \ 1.8.0 \ variants \ PILL_F103XX

on the line: [the standard is SPI-1]

// SPI Definitions
#define PIN_SPI_SS PA4
#define PIN_SPI_MOSI PA7
#define PIN_SPI_MISO PA6
#define PIN_SPI_SCK PA5

change to: [SPI-2]

// SPI Definitions
#define PIN_SPI_SS PB12
#define PIN_SPI_MOSI PB15
#define PIN_SPI_MISO PB14
#define PIN_SPI_SCK PB13

Hallelujah … Praise God
works … :slight_smile:

ok, hope it’s useful
Lord Jesus bless you and your family so much - so much - so much
amen!

Halo,

saya mau berbagi apa yang saya alami dalam membuat system
yang menerapkan STM32F103C [STM32F103C8T6] stm32duino

pada tahun 2019,
awalnya, saya mengunakan software Arduino IDE v.1.5.x
dan mengunakan EThernet_STM.h
namun karena kebutuhan design yang membutuhkan A0-A9 sebagai digital input
10-bit absolute rotary sensor, maka SPI harus dipindahkan dari SPI-1 ke SPI-2
pada kondisi ini saya memodifikasi W550.cpp menjadi seperti berikut
C:\Users[your PC]\AppData\Local\Arduino15\packages\stm32duino\hardware\STM32F1\2019.12.31\libraries\Ethernet_STM\src\utility

#include <stdio.h>
#include <string.h>
#include <SPI.h> //add by riky

#include “utility/w5100.h”
#if defined(W5500_ETHERNET_SHIELD)

// W5500 controller instance
W5500Class W5100;

#define SPI_CS 10
//#define STM32_SPI_CS PA4 //edit by riky
#define STM32_SPI_CS PB12 //change by riky
//#define SPI2_NSS_PIN PB12 //add by riky
SPIClass SPI_2(2); //add by riky

void W5500Class::init(void)


dan seterusnya

tetapi setelah saya upgrade Arduino IDE 1.5.x menjadi v.18.x
sudah tidak bisa lagi… :frowning:
jadi saya harus ngoprek lagi, dan berdoa kepada Tuhan Yesus
…akhirnya Tuhan tunjukan ke saya suatu direktroy dan file
dan mengarahkan saya harus memodifikasi file variant.h yang ada di
C:\Users[yourPC]\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\variants\PILL_F103XX

pada baris: [standard nya SPI-1]

// SPI Definitions
#define PIN_SPI_SS PA4
#define PIN_SPI_MOSI PA7
#define PIN_SPI_MISO PA6
#define PIN_SPI_SCK PA5

rubah menjadi : [SPI-2]

// SPI Definitions
#define PIN_SPI_SS PB12
#define PIN_SPI_MOSI PB15
#define PIN_SPI_MISO PB14
#define PIN_SPI_SCK PB13

Haleluya…Puji Tuhan
berhasil…:slight_smile:

ok, semoga bermanfaat
Tuhan Yesus memberkati anda sekeluarga berlimpah-limpah-limpah
amin!