SD library SPI pins remap
SD library SPI pins remap
Hi everyone,
I use a Generic STM32F103C8T6 to do a project. My project use the standard arduino SD library to read/write data, but the default SPI pins (PA4-PA7) are used for other purpose.
It seems that there is another set of SPI pins (PA15,PB3-PB5) that can be used, but I can't found any information how to use it instead of the default SPI pins.
Is it possible to use these pins to work with the SD library ? Thank you for any help !
I use a Generic STM32F103C8T6 to do a project. My project use the standard arduino SD library to read/write data, but the default SPI pins (PA4-PA7) are used for other purpose.
It seems that there is another set of SPI pins (PA15,PB3-PB5) that can be used, but I can't found any information how to use it instead of the default SPI pins.
Is it possible to use these pins to work with the SD library ? Thank you for any help !
-
- Posts: 505
- Joined: Fri Dec 27, 2019 4:53 pm
- Location: Munich, Germany
- Contact:
Re: SD library SPI pins remap
On the ST core you can remap the spi, i2c and serial pins: https://github.com/stm32duino/wiki/wiki/API#spi
You can also can use a different library, the default arduino SD library only supports the default SPI, but on other libraries you can use a different SPI port.
You can also can use a different library, the default arduino SD library only supports the default SPI, but on other libraries you can use a different SPI port.
Re: SD library SPI pins remap
Thank you all, I am using the STM32F1/F4 core files (Leaflabs-based core).
I have some working codes based on the standard SD library, changing to another SD library involve to much work and not preferred.
I have some working codes based on the standard SD library, changing to another SD library involve to much work and not preferred.
-
- Posts: 505
- Joined: Fri Dec 27, 2019 4:53 pm
- Location: Munich, Germany
- Contact:
Re: SD library SPI pins remap
You can call in setup
Another option wold be to use SPI2 interface, pins PB12/13/14/15.
In this case you only need to call in setup
before SPI.begin() and it should work.
PS. Admin, please move this under "LIbmaple" core section. Thanks. --> done
Code: Select all
afio_remap(AFIO_REMAP_SPI1);
In this case you only need to call in setup
Code: Select all
SPI.setModule(2);
PS. Admin, please move this under "LIbmaple" core section. Thanks. --> done

Re: SD library SPI pins remap
The SPI2 method works !
But the SPI1 remap method do not work.
Here are the codes I use :
But the SPI1 remap method do not work.
Here are the codes I use :
Code: Select all
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
void setup()
{
Serial.begin(57600);
Serial.println("Boot ...");
delay(50) ;
// using alternate SPI1 pins
// CS=PA15, SCK=PB3, MISO=PB4, MOSI=PB5
afio_remap(AFIO_REMAP_SPI1);
if (!SD.begin(PA15))
{
Serial.println("SD initialization failed !");
while (1) ;
}
else
{
Serial.println("SD OK");
}
}
-
- Posts: 505
- Joined: Fri Dec 27, 2019 4:53 pm
- Location: Munich, Germany
- Contact:
Re: SD library SPI pins remap
For the CS pin you use PA15.
Instead, you could use any other pin as well.
This PA15 is a special pin because is per default reserved for debugging.
In order to use it as normal IO pin you have to call
in your setup. I am not sure about the order relative to remapping, just try both ways (before and after) and let us know which works.
And don't forget to set the CS pin as OUTPUT!
Instead, you could use any other pin as well.
This PA15 is a special pin because is per default reserved for debugging.
In order to use it as normal IO pin you have to call
Code: Select all
enableDebugPorts();
And don't forget to set the CS pin as OUTPUT!

Last edited by stevestrong on Sun Apr 12, 2020 4:18 pm, edited 1 time in total.
Re: SD library SPI pins remap
I added enabledebugPorts() in setup, but during compilation it said : 'enabledebugPorts' was not declared in this scope.
Re: SD library SPI pins remap
enableDebugPorts();ChoCho wrote: Sun Apr 12, 2020 9:49 am I added enabledebugPorts() in setup, but during compilation it said : 'enabledebugPorts' was not declared in this scope.
Re: SD library SPI pins remap
I corrected to enableDebugPorts() and compilation OK. But SD is failed.
I tried using other pins as CS (PA11 or PA12 or PB8), for example :
but SD still not work.
I tried using other pins as CS (PA11 or PA12 or PB8), for example :
Code: Select all
pinMode(PA11, OUTPUT);
afio_remap(AFIO_REMAP_SPI1);
// connections : MOSI= PB5, MISO=PB4, SCK=PB3, CS=PA11
if (!SD.begin(PA11))
{
Serial2.println("DC16(0,48,'SD initialization failed !',1);");
Serial.println("SD initialization failed !");
SD_ok = false ;
while (1) ;
}
else
{
Serial2.println("DC16(0,48,'SD OK !',1);");
Serial.println("SD OK");
}