It looks like on the stm32duino core the pins are defined using the constructor? So something like SPIClass SPI_2(MOSI,MISO,SCLK,SCS); should create a SPI_2 object with the defined pins.
Btw, there shouldn’t be semi-colons ‘;’ after include statements
This is ancient – the current version is 14.2.0. If you don’t have a strong reason against it you should be using the latest platform version, and with it comes the latest Arduino STM32 core (2.0.0). You might be seeing bugs that have been fixed in the core years ago in your firmware.
Hm, this doesn’t look right. Referencing against the pinout of the Bluepill / STM32F103C8 here shows
SPI1:
MOSI = PB5
MISO = PB4
SCLK = PB3
SPI2:
MOSI = PB15
MISO = PB14
SCLK = PB13
So if the #define P_MISO PB14 is supposed to be for SPI1, which it looks like because you use P_MISO2 etc in the SPI_2 creation, that’s wrong (SPI1 and SPI2 confused). Also, you’ve confused MOSI with MISO (in your code P_MISO2 is PB5 when SPI1_MISO is actually PB4). The latter means you’re most likely crashing at the peripheral pin check.
Also you you shouldn’t give the instantiated SPI object the chip-select pin, because otherwise it’ll assume thaht’s a hardware NSS pin. (Refer here, here and here).
Hi Max,
Thank you for your feedback.
I have updated to the latest core version (There used to be an issue with compilation at version 6.1).
And switched the MOSI MISO which were indeed wrong.
It seems now that the begin and end transactions are successful but when I try to transfer data to the system its crashes.
I have tried running your code with the following change at the end of the loop. Any idea what I am doing wrong? I am guessing it fails at the data transfer but I don;t really understand why.
uint8_t reg,value;
Serial.println("Sending data Completed");
Serial.println("SPI(1) data transfer begin");
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
reg = 0x00;
reg &= ~0x80u;
digitalWrite(P_CS_DN, LOW);
delayMicroseconds(5);
SPI.transfer(reg);
delayMicroseconds(5);
value = SPI.transfer(0);
delayMicroseconds(20);
digitalWrite(P_CS_DN, HIGH);
SPI.endTransaction();
Serial.println("SPI(1) data transfer end");
delay(500);
Serial.println("SPI(2) data transfer begin");
delay(200);
SPI_2.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
reg = 0x00;
reg &= ~0x80u;
digitalWrite(P_CS_UP, LOW);
delayMicroseconds(5);
SPI_2.transfer(reg);
delayMicroseconds(5);
value = SPI_2.transfer(0);
delayMicroseconds(20);
digitalWrite(P_CS_UP, HIGH);
SPI_2.endTransaction();
Serial.println("SPI(2) data transfer end");
delay(500);
That’s my serial output:
SPI(1).begin() start
SPI(1).begin() done
SPI(1) transaction done
SPI_2.begin() start
SPI_2.begin() end
SPI(2) transaction done
Sending data Completed
SPI(1) data transfer begin
SPI(1) data transfer end
SPI(2) data transfer begin
The MISO/MOSI pins for SPI2 are still incorrect. It can’t find the peripheral (SPI2) associated with MISO/MOSI and is thus not doing the internal initialization (setting obj->spi).
Aka, MOSI + MISO, not MOSI + MISO. The errors in the pin definitions with switching them and the constructor call here actually canceled out previously in your example.