Page 1 of 2

SD library SPI pins remap

Posted: Mon Apr 06, 2020 7:03 am
by ChoCho
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 !

Re: SD library SPI pins remap

Posted: Mon Apr 06, 2020 7:19 am
by stevestrong

Re: SD library SPI pins remap

Posted: Tue Apr 07, 2020 12:19 am
by .rpv
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.

Re: SD library SPI pins remap

Posted: Tue Apr 07, 2020 2:21 am
by ChoCho
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.

Re: SD library SPI pins remap

Posted: Tue Apr 07, 2020 7:22 am
by stevestrong
You can call in setup

Code: Select all

afio_remap(AFIO_REMAP_SPI1);
Another option wold be to use SPI2 interface, pins PB12/13/14/15.
In this case you only need to call in setup

Code: Select all

SPI.setModule(2);
before SPI.begin() and it should work.

PS. Admin, please move this under "LIbmaple" core section. Thanks. --> done ;)

Re: SD library SPI pins remap

Posted: Wed Apr 08, 2020 2:18 pm
by ChoCho
The SPI2 method works !

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"); 
      }
}

Re: SD library SPI pins remap

Posted: Wed Apr 08, 2020 7:03 pm
by stevestrong
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

Code: Select all

enableDebugPorts();
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! ;)

Re: SD library SPI pins remap

Posted: Sun Apr 12, 2020 9:49 am
by ChoCho
I added enabledebugPorts() in setup, but during compilation it said : 'enabledebugPorts' was not declared in this scope.

Re: SD library SPI pins remap

Posted: Sun Apr 12, 2020 10:14 am
by stas2z
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.
enableDebugPorts();

Re: SD library SPI pins remap

Posted: Thu Apr 16, 2020 6:21 am
by ChoCho
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 :

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"); 
      }
but SD still not work.