Page 1 of 1

SPI remapping

Posted: Mon Jan 20, 2020 10:55 am
by forfrends
Hello!
I want to connect nrf24l01 to stm32f103c8t6. It is standardly connected to pins PA5 (SCK1), PA6 (MOSO1), PA7 (MOSI1). I have these pins busy, I already ordered a printed circuit board, so I won’t be able to use them. SPI2 pins are also busy. At the same time, there is another SPI port on stm32f103c8t6, also under the number "1" (Why under the number "1" ???):
PB3 (SCK1), PB4 (MISO1), PB5 (MOSI1)
Question: how to indicate in the code what exactly these pins will be used for SPI?
Image

Re: SPI remapping

Posted: Mon Jan 20, 2020 3:35 pm
by fpiSTM
Which core you used ?

Re: SPI remapping

Posted: Tue Jan 21, 2020 7:47 am
by BennehBoy
Some peripherals can be switched to 'alternate' pins, it's still the same peripheral though hence why it shows as SPI1 for both pin sets. How to do it depends on which core you've used, Rogers/Steve's maple based cores, or the official STM HAL based core.

Re: SPI remapping

Posted: Tue Jan 21, 2020 8:39 am
by forfrends
All figured out. It was only necessary to update the library. In the new version of the library, you can specify pins SPI:

Code: Select all

SPIClass SPI_3(PB5,  PB4,  PB3, PA15);

Re: SPI remapping

Posted: Tue Jan 21, 2020 9:07 am
by fpiSTM
Right. So your topic is solved?

Re: SPI remapping

Posted: Tue Jan 21, 2020 12:03 pm
by forfrends
Can't get it to work!
If I'm flashing a test sketch, then SPI works:

Code: Select all

#include <SPI.h>
#define SPI2_NSS_PIN PA15   //SPI_2 Chip Select pin is PB12. You can change it to the STM32 pin you want.
//             mosi, miso, sclk, ssel
SPIClass SPI_2(PB5,  PB4,  PB3, SPI2_NSS_PIN); //Create an instance of the SPI Class called SPI_2 that uses the 2nd SPI Port
byte data;

void setup() {
  // Setup SPI 2
  SPI_2.begin(); //Initialize the SPI_2 port.
  SPI_2.setBitOrder(MSBFIRST); // Set the SPI_2 bit order
  SPI_2.setDataMode(SPI_MODE0); //Set the  SPI_2 data mode 0
  SPI_2.setClockDivider(SPI_CLOCK_DIV16);  // Use a different speed to SPI 1
  pinMode(SPI2_NSS_PIN, OUTPUT);
}

void loop() {
  sendSPI2();
  delayMicroseconds(10);    //Delay 10 micro seconds.
}

void sendSPI2()
{
  digitalWrite(SPI2_NSS_PIN, LOW); // manually take CSN low for SPI_2 transmission
  data = SPI_2.transfer(0x55); //Send the HEX data 0x55 over SPI-2 port and store the received byte to the <data> variable.
  digitalWrite(SPI2_NSS_PIN, HIGH); // manually take CSN high between spi transmissions
}
I checked with an oscilloscope, the data is being transmitted.
But if I connect the nrf24l01 library, the SPI port stops working. I am using the RF24 library: https://github.com/nRF24/RF24
Maybe you know a library that will work stably?

Re: SPI remapping

Posted: Sun Jun 21, 2020 8:03 am
by nopnop2002
If you are using STM32 Core, setClockDivider() will not work.

Because STM32 Core supports many CPU clocks.

setClockDivider() has a speed that depends on the CPU clock.

The following code will work with any CPU clock.

Code: Select all

SPI.begin()
spiSettings = SPISettings(SPI_SPEED, MSBFIRST, SPI_MODE0);
・
・
・
SPI.beginTransaction(spiSettings)
SPI.transfer(data);
SPI.endTransaction()