Skip to content

Commit 4d977c9

Browse files
authored
Merge pull request #35 from adafruit/pio-sdio-pin-48
fix rp2350B with gpio up to 48
2 parents 96a164f + b5e4bca commit 4d977c9

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

examples/Rp2040SdioSetup/Rp2040SdioSetup.ino

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ https://learn.adafruit.com/adafruit-microsd-spi-sdio
2121
Wires should be short since signals can be as faster than 50 MHz.
2222
*/
2323
#define DISABLE_FS_H_WARNING // Disable warning for type File not defined.
24-
#include "SdFat.h"
24+
#include "SdFat_Adafruit_Fork.h"
2525
//------------------------------------------------------------------------------
2626
// Example GPIO definitions I use for debug. Edit for your setup.
2727
// Run this example as is to print the symbol for your variant.
@@ -30,6 +30,10 @@ Wires should be short since signals can be as faster than 50 MHz.
3030
#define RP_CLK_GPIO 18
3131
#define RP_CMD_GPIO 19
3232
#define RP_DAT0_GPIO 20 // DAT1: GPIO21, DAT2: GPIO22, DAT3: GPIO23.
33+
#elif defined(ARDUINO_ADAFRUIT_METRO_RP2350) || defined(ARDUINO_ADAFRUIT_FRUITJAM_RP2350)
34+
#define RP_CLK_GPIO 34
35+
#define RP_CMD_GPIO 35
36+
#define RP_DAT0_GPIO 36 // DAT1: GPIO37, DAT2: GPIO38, DAT3: GPIO39.
3337
#elif defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_RASPBERRY_PI_PICO_2)
3438
#define RP_CLK_GPIO 16
3539
#define RP_CMD_GPIO 17

src/SdCard/Rp2040Sdio/PioSdioCard.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const uint PIO_CLK_DIV_INIT = 2;
3838

3939
const uint PIO_CLK_DIV_RUN = 1;
4040

41-
const uint PIN_SDIO_UNDEFINED = 31u;
41+
const uint PIN_SDIO_UNDEFINED = 48u;
4242

4343
const uint DAT_FIFO_DEPTH = 8;
4444
const uint CMD0_RETRIES = 10;
@@ -260,6 +260,26 @@ static void pioEnd() {
260260
static bool pioInit() {
261261
uint pin[] = {g_clkPin, g_cmdPin, g_dat0Pin,
262262
g_dat0Pin + 1, g_dat0Pin + 2, g_dat0Pin + 3};
263+
264+
uint lowest_pin = pin[0];
265+
uint highest_pin = pin[0];
266+
uint gpio_base = 0;
267+
for (uint i = 0; i < 6U; i++) {
268+
if (pin[i] < lowest_pin) {
269+
lowest_pin = pin[i];
270+
}
271+
if (pin[i] > highest_pin) {
272+
highest_pin = pin[i];
273+
}
274+
}
275+
if (highest_pin - lowest_pin > 31) {
276+
sdError(PIO_ERROR_ADD_PROGRAM);
277+
return false;
278+
}
279+
if (highest_pin > 31) {
280+
gpio_base = 16;
281+
}
282+
263283
if (g_wrRespOffset < 0) {
264284
uint16_t patched_inst[rd_data_program.length]; // NOLINT
265285
struct pio_program tmp_program;
@@ -280,6 +300,9 @@ static bool pioInit() {
280300
sdError(PIO_ERROR_ADD_PROGRAM);
281301
goto fail;
282302
}
303+
if (gpio_base > 0) {
304+
pio_set_gpio_base(g_pio, gpio_base);
305+
}
283306
g_sm0 = pio_claim_unused_sm(g_pio, false);
284307
if (g_sm0 < 0) {
285308
sdError(PIO_ERROR_ADD_PROGRAM);
@@ -327,7 +350,7 @@ static bool pioInit() {
327350
}
328351

329352
g_pio->input_sync_bypass |=
330-
(1 << g_clkPin) | (1 << g_cmdPin) | (0XF << g_dat0Pin);
353+
(1 << (g_clkPin-gpio_base)) | (1 << (g_cmdPin-gpio_base)) | (0XF << (g_dat0Pin-gpio_base));
331354
pio_sm_set_consecutive_pindirs(g_pio, g_sm0, g_clkPin, 1, true);
332355
pio_sm_set_consecutive_pindirs(g_pio, g_sm0, g_cmdPin, 1, true);
333356
pio_sm_set_consecutive_pindirs(g_pio, g_sm0, g_dat0Pin, 4, false);

0 commit comments

Comments
 (0)