Skip to content

Commit

Permalink
feat: support new SD pins management
Browse files Browse the repository at this point in the history
Fixe #47

Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
  • Loading branch information
fpistm committed Jun 22, 2023
1 parent 7eca2b0 commit 7464dbe
Show file tree
Hide file tree
Showing 8 changed files with 541 additions and 22 deletions.
108 changes: 101 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ http://www.arduino.cc/en/Reference/SD

## Dependency

This library is based on FatFs, a generic FAT file system module for small embedded systems.
This library is based on FatFs, a generic FAT file system module for small embedded systems.
[http://elm-chan.org/fsw/ff](http://elm-chan.org/fsw/ff/00index_e.html)

The FatFs has been ported as Arduino library [here](https://github.com/stm32duino/FatFs).
Expand All @@ -31,11 +31,105 @@ User can provide his own defined options by adding his configuration in a file n
### SD

Some default definitions can be overridden using:
* board `variant.h`
* `build_opt.h`: see [Customize build options](https://github.com/stm32duino/wiki/wiki/Customize-build-options-using-build_opt.h)

* `hal_conf_extra.h`: see [HAL configuration](https://github.com/stm32duino/wiki/wiki/HAL-configuration)

* board `variant*.h`
* `build_opt.h`: see [Customize build options](https://github.com/stm32duino/Arduino_Core_STM32/wiki/Customize-build-options-using-build_opt.h)

* `hal_conf_extra.h`: see [HAL configuration](https://github.com/stm32duino/Arduino_Core_STM32/wiki/HAL-configuration)

#### SDIO/SDMMC pins definition

Since STM32 core v2.6.0, the `PinMap_SD[]` array defined in the `PeripheralPins*.c` has been split per signals:
```C
PinMap_SD_CK[]
PinMap_SD_DATA0[]
PinMap_SD_DATA1[]
PinMap_SD_DATA2[]
PinMap_SD_DATA3[]
PinMap_SD_DATA4[]
PinMap_SD_DATA5[]
PinMap_SD_DATA6[]
PinMap_SD_DATA7[]
/* Only for SDMMC */
PinMap_SD_CKIN[]
PinMap_SD_CDIR[]
PinMap_SD_D0DIR[]
PinMap_SD_D123DIR[]
```

This allows to define the SDIO/SDMMC pins to use instead of using all the pins defined in the old array.
By default, if no pins are explicitly defined, the first one from each array is used.

* To redefine the default one, use the following macros:

* `SDX_D0`
* `SDX_D1`
* `SDX_D2`
* `SDX_D3`
* `SDX_CMD`
* `SDX_CK`

* Only for `SDMMC`:
* `SDX_CKIN`
* `SDX_CDIR`
* `SDX_D0DIR`
* `SDX_D123DIR`

* or redefine the default one before call of `begin()` of `SDClass` or `init()` of `Sd2Card`, use the following methods:

* `setDx(uint32_t data0, uint32_t data1, uint32_t data2, uint32_t data3)`
* `setCK(uint32_t ck)`
* `setCK(PinName ck)`
* `setCMD(uint32_t cmd)`
* `setCMD(PinName cmd)`

* Only for `SDMMC`:
* `setCKIN(uint32_t ckin)`
* `setCKIN(PinName ckin)`
* `setCDIR(uint32_t cdir)`
* `setCDIR(PinName cdir)`
* `setDxDIR(uint32_t d0dir, uint32_t d123dir)`
* `setDxDIR(PinName d0dir, PinName d123dir)`

*Code snippet:*
```C++
Sd2Card card;
card.setDx(PE12, PE13, PE14, PE15);
card.setCMD(PB_14); // using PinName
card.setCK(PB15);
card.init();
```
or
```C++
SD.setDx(PE12, PE13, PE14, PE15);
SD.setCMD(PB14);
SD.setCK(PB_15); // using PinName
SD.begin();
```

* or using the `begin()` of `SDClass` or `init()` of `Sd2Card` methods:

* For `SDIO`:
* `SDClass`:
* `begin(uint32_t data0, uint32_t data1, uint32_t data2, uint32_t data3, uint32_t ck, uint32_t cmd)`
* `begin(uint32_t detect = SD_DETECT_NONE, uint32_t data0 = SDX_D0, uint32_t data1 = SDX_D1, uint32_t data2 = SDX_D2, uint32_t data3 = SDX_D3, uint32_t ck = SDX_CK, uint32_t cmd = SDX_CMD, uint32_t ckin = SDX_CKIN, uint32_t cdir = SDX_CDIR, uint32_t d0dir = SDX_D0DIR, uint32_t d123dir = SDX_D123DIR);`
* `Sd2Card`:
* `init(uint32_t data0, uint32_t data1, uint32_t data2, uint32_t data3, uint32_t ck, uint32_t cmd)`
* `init(uint32_t detect = SD_DETECT_NONE, uint32_t data0 = SDX_D0, uint32_t data1 = SDX_D1, uint32_t data2 = SDX_D2, uint32_t data3 = SDX_D3, uint32_t ck = SDX_CK, uint32_t cmd = SDX_CMD);`

* For `SDMMC`:
* `SDClass`:
* `begin(uint32_t data0, uint32_t data1, uint32_t data2, uint32_t data3, uint32_t ck, uint32_t cmd, uint32_t ckin, uint32_t cdir, uint32_t d0dir, uint32_t d123dir);`
* `begin(uint32_t detect = SD_DETECT_NONE, uint32_t data0 = SDX_D0, uint32_t data1 = SDX_D1, uint32_t data2 = SDX_D2, uint32_t data3 = SDX_D3, uint32_t ck = SDX_CK, uint32_t cmd = SDX_CMD, uint32_t ckin = SDX_CKIN, uint32_t cdir = SDX_CDIR, uint32_t d0dir = SDX_D0DIR, uint32_t d123dir = SDX_D123DIR);`
*
* * `Sd2Card`:
* `init(uint32_t data0 = SDX_D0, uint32_t data1 = SDX_D1, uint32_t data2 = SDX_D2, uint32_t data3 = SDX_D3, uint32_t ck = SDX_CK, uint32_t cmd = SDX_CMD, uint32_t ckin = SDX_CKIN, uint32_t cdir = SDX_CDIR, uint32_t d0dir = SDX_D0DIR, uint32_t d123dir = SDX_D123DIR)`
* `init(uint32_t detect = SD_DETECT_NONE, uint32_t data0 = SDX_D0, uint32_t data1 = SDX_D1, uint32_t data2 = SDX_D2, uint32_t data3 = SDX_D3, uint32_t ck = SDX_CK, uint32_t cmd = SDX_CMD, uint32_t ckin = SDX_CKIN, uint32_t cdir = SDX_CDIR, uint32_t d0dir = SDX_D0DIR, uint32_t d123dir = SDX_D123DIR);`

*Code snippet:*
```C++
card.init(PE12, PE13, PE14, PE15, PB14, PB15);
SD.begin(SD_DETECT_PIN, PE12, PE13, PE14, PE15, PB14, PB15);
```

#### SD configurations

Expand Down Expand Up @@ -68,4 +162,4 @@ Some default definitions can be overridden using:
#### SD detect and timeout
* `SD_DETECT_PIN` pin number

* `SD_DATATIMEOUT` constant for Read/Write block
* `SD_DATATIMEOUT` constant for Read/Write block
8 changes: 7 additions & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ open KEYWORD2
close KEYWORD2
seek KEYWORD2
position KEYWORD2
size KEYWORD2
size KEYWORD2
setDx KEYWORD2
setCK KEYWORD2
setCMD KEYWORD2
setCKIN KEYWORD2
setCDIR KEYWORD2
setDxDIR KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down
61 changes: 58 additions & 3 deletions src/SD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,68 @@ SDClass SD;
/**
* @brief Link SD, register the file system object to the FatFs mode and configure
* relatives SD IOs including SD Detect Pin if any
* @param None
* @param data0: data0 pin number (default SDX_D0)
* @param data1: data1 pin number (default SDX_D1)
* @param data2: data2 pin number (default SDX_D2)
* @param data3: data3 pin number (default SDX_D3)
* @param ck: ck pin number (default SDX_CK)
* @param cmd: cmd pin number (default SDX_CMD)
* @param ckin: ckin pin number only for SDMMC (default SDX_CKIN)
* @param cdir: cdir pin number only for SDMMC (default SDX_CDIR)
* @param d0dir: d0dir pin number only for SDMMC (default SDX_D0DIR)
* @param d123dir: d123dir pin number only for SDMMC (default SDX_D123DIR)
* @retval true or false
*/
bool SDClass::begin(uint32_t detectpin)
#if defined(SDMMC1) || defined(SDMMC2)
bool SDClass::begin(uint32_t data0, uint32_t data1, uint32_t data2, uint32_t data3,
uint32_t ck, uint32_t cmd, uint32_t ckin, uint32_t cdir,
uint32_t d0dir, uint32_t d123dir)
{
return begin(SD_DETECT_NONE, data0, data1, data2, data3, ck, cmd, ckin, cdir, d0dir, d123dir);
}

#else
bool SDClass::begin(uint32_t data0, uint32_t data1, uint32_t data2,
uint32_t data3, uint32_t ck, uint32_t cmd)
{
return begin(SD_DETECT_NONE, data0, data1, data2, data3, ck, cmd);
}
#endif

/**
* @brief Link SD, register the file system object to the FatFs mode and configure
* relatives SD IOs including SD Detect Pin if any
* @param detect: detect pin number (default SD_DETECT_NONE)
* @param data0: data0 pin number (default SDX_D0)
* @param data1: data1 pin number (default SDX_D1)
* @param data2: data2 pin number (default SDX_D2)
* @param data3: data3 pin number (default SDX_D3)
* @param ck: ck pin number (default SDX_CK)
* @param cmd: cmd pin number (default SDX_CMD)
* @param ckin: ckin pin number only for SDMMC (default SDX_CKIN)
* @param cdir: cdir pin number only for SDMMC (default SDX_CDIR)
* @param d0dir: d0dir pin number only for SDMMC (default SDX_D0DIR)
* @param d123dir: d123dir pin number only for SDMMC (default SDX_D123DIR)
* @retval true or false
*/
#if defined(SDMMC1) || defined(SDMMC2)
bool SDClass::begin(uint32_t detect, uint32_t data0, uint32_t data1, uint32_t data2, uint32_t data3,
uint32_t ck, uint32_t cmd, uint32_t ckin, uint32_t cdir, uint32_t d0dir, uint32_t d123dir)
#else
bool SDClass::begin(uint32_t detect, uint32_t data0, uint32_t data1, uint32_t data2,
uint32_t data3, uint32_t ck, uint32_t cmd)
#endif
{
setDx(data0, data1, data2, data3);
setCK(ck);
setCMD(cmd);
#if defined(SDMMC1) || defined(SDMMC2)
setCKIN(ckin);
setCDIR(cdir);
setDxDIR(d0dir, d123dir);
#endif
/*##-1- Initializes SD IOs #############################################*/
if (_card.init(detectpin)) {
if (_card.init(detect)) {
return _fatFs.init();
}
return false;
Expand Down
70 changes: 68 additions & 2 deletions src/STM32SD.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,75 @@ class File {
class SDClass {

public:

/* Initialize the SD peripheral */
bool begin(uint32_t detectpin = SD_DETECT_NONE);
#if defined(SDMMC1) || defined(SDMMC2)
bool begin(uint32_t data0, uint32_t data1, uint32_t data2, uint32_t data3, uint32_t ck, uint32_t cmd,
uint32_t ckin, uint32_t cdir, uint32_t d0dir, uint32_t d123dir
);
bool begin(uint32_t detect = SD_DETECT_NONE, uint32_t data0 = SDX_D0, uint32_t data1 = SDX_D1,
uint32_t data2 = SDX_D2, uint32_t data3 = SDX_D3, uint32_t ck = SDX_CK, uint32_t cmd = SDX_CMD,
uint32_t ckin = SDX_CKIN, uint32_t cdir = SDX_CDIR, uint32_t d0dir = SDX_D0DIR, uint32_t d123dir = SDX_D123DIR
);
#else
bool begin(uint32_t data0, uint32_t data1, uint32_t data2, uint32_t data3, uint32_t ck, uint32_t cmd);
bool begin(uint32_t detect = SD_DETECT_NONE, uint32_t data0 = SDX_D0, uint32_t data1 = SDX_D1,
uint32_t data2 = SDX_D2, uint32_t data3 = SDX_D3, uint32_t ck = SDX_CMD, uint32_t cmd = SDX_CMD
);
#endif

// set* have to be called before begin()
void setDx(uint32_t data0, uint32_t data1, uint32_t data2, uint32_t data3)
{
_card.setDx(data0, data1, data2, data3);
};
void setCK(uint32_t ck)
{
_card.setCK(ck);
};
void setCMD(uint32_t cmd)
{
_card.setCMD(cmd);
};

void setDx(PinName data0, PinName data1, PinName data2, PinName data3)
{
_card.setDx(data0, data1, data2, data3);
};
void setCK(PinName ck)
{
_card.setCK(ck);
};
void setCMD(PinName cmd)
{
_card.setCMD(cmd);
};
#if defined(SDMMC1) || defined(SDMMC2)
void setCKIN(uint32_t ckin)
{
_card.setCKIN(ckin);
};
void setCDIR(uint32_t cdir)
{
_card.setCDIR(cdir);
};
void setDxDIR(uint32_t d0dir, uint32_t d123dir)
{
_card.setDxDIR(d0dir, d123dir);
};

void setCKIN(PinName ckin)
{
_card.setCKIN(ckin);
};
void setCDIR(PinName cdir)
{
_card.setCDIR(cdir);
};
void setDxDIR(PinName d0dir, PinName d123dir)
{
_card.setDxDIR(d0dir, d123dir);
};
#endif
static File open(const char *filepath, uint8_t mode = FA_READ);
static bool exists(const char *filepath);
static bool mkdir(const char *filepath);
Expand Down
33 changes: 30 additions & 3 deletions src/Sd2Card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,37 @@
#include <Arduino.h>
#include "Sd2Card.h"

bool Sd2Card::init(uint32_t detectpin)
#if defined(SDMMC1) || defined(SDMMC2)
bool Sd2Card::init(uint32_t data0, uint32_t data1, uint32_t data2, uint32_t data3, uint32_t ck, uint32_t cmd,
uint32_t ckin, uint32_t cdir, uint32_t d0dir, uint32_t d123dir)
{
if (detectpin != SD_DETECT_NONE) {
PinName p = digitalPinToPinName(detectpin);
return init(SD_DETECT_NONE, data0, data1, data2, data3, ck, cmd, ckin, cdir, d0dir, d123dir);
}
#else
bool Sd2Card::init(uint32_t data0, uint32_t data1, uint32_t data2, uint32_t data3, uint32_t ck, uint32_t cmd)
{
return init(SD_DETECT_NONE, data0, data1, data2, data3, ck, cmd);
}
#endif

#if defined(SDMMC1) || defined(SDMMC2)
bool Sd2Card::init(uint32_t detect, uint32_t data0, uint32_t data1, uint32_t data2, uint32_t data3,
uint32_t ck, uint32_t cmd, uint32_t ckin, uint32_t cdir, uint32_t d0dir, uint32_t d123dir)
#else
bool Sd2Card::init(uint32_t detect, uint32_t data0, uint32_t data1, uint32_t data2, uint32_t data3,
uint32_t ck, uint32_t cmd)
#endif
{
setDx(data0, data1, data2, data3);
setCK(ck);
setCMD(cmd);
#if defined(SDMMC1) || defined(SDMMC2)
setCKIN(ckin);
setCDIR(cdir);
setDxDIR(d0dir, d123dir);
#endif
if (detect != SD_DETECT_NONE) {
PinName p = digitalPinToPinName(detect);
if ((p == NC) || \
BSP_SD_DetectPin(set_GPIO_Port_Clock(STM_PORT(p)),
STM_LL_GPIO_PIN(p)) != MSD_OK) {
Expand Down
Loading

0 comments on commit 7464dbe

Please sign in to comment.