SD library SPI pins remap

Post here all questions related to LibMaple core if you can't find a relevant section!
ChoCho
Posts: 8
Joined: Mon Feb 17, 2020 2:01 am

SD library SPI pins remap

Post 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 !
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: SD library SPI pins remap

Post by stevestrong »

.rpv
Posts: 43
Joined: Wed Dec 18, 2019 10:19 pm

Re: SD library SPI pins remap

Post 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.
ChoCho
Posts: 8
Joined: Mon Feb 17, 2020 2:01 am

Re: SD library SPI pins remap

Post 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.
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: SD library SPI pins remap

Post 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 ;)
ChoCho
Posts: 8
Joined: Mon Feb 17, 2020 2:01 am

Re: SD library SPI pins remap

Post 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"); 
      }
}
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: SD library SPI pins remap

Post 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! ;)
Last edited by stevestrong on Sun Apr 12, 2020 4:18 pm, edited 1 time in total.
ChoCho
Posts: 8
Joined: Mon Feb 17, 2020 2:01 am

Re: SD library SPI pins remap

Post by ChoCho »

I added enabledebugPorts() in setup, but during compilation it said : 'enabledebugPorts' was not declared in this scope.
stas2z
Posts: 131
Joined: Mon Feb 24, 2020 8:17 pm
Answers: 8

Re: SD library SPI pins remap

Post 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();
ChoCho
Posts: 8
Joined: Mon Feb 17, 2020 2:01 am

Re: SD library SPI pins remap

Post 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.
Post Reply

Return to “General discussion”