Go Down

Topic: stm32f103 spi.h library doesn't work (Read 230 times) previous topic - next topic

Ghostatwar

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.   

pylon

For your board the SPI pins are defined as follows:

Code: [Select]
#define PIN_SPI_SS              PA4
#define PIN_SPI_MOSI            PA7
#define PIN_SPI_MISO            PA6
#define PIN_SPI_SCK             PA5


Quote
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).

freaklabs

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.
FreakLabs
Wireless Technology for the environment, wildlife, and agriculture
https://freaklabs.org
https://hackerfarm.jp
Twitter: @freaklabs

Ghostatwar

#3
Mar 21, 2019, 10:21 pm Last Edit: Mar 21, 2019, 10:41 pm by Ghostatwar
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.


Code: [Select]
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,
Code: [Select]
"while (!Serial);"
 results in the led being on no matter what i try however,
Code: [Select]
"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.


Code: [Select]
#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

  

pylon

Quote
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.

Quote
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).

Quote
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?

Ghostatwar

Hi Pylon,

Code: [Select]
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:
Code: [Select]
Serial1.println(F("TEST")); works but since i'm pointing data to the library using
Code: [Select]
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.

 
Code: [Select]
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.

pylon

Quote
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.

Code: [Select]

#include <SPI.h>

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


void loop() {
 
}

Go Up