How to switch off SD card?

Hello All,

I am trying to use an SD card with Nano 33 BLE Sense.

I can read/write to the card without issue.
However what I am not able to do is reuse the SPI pins for input or output after ending SD card access and calling SD.end / SPI.end

I have created the sketch below to show the issue using pin 13 (both SPI clock & orange LED).
Example below uses Nano33, and card reader only.
Card reader is 3.3v (not 5v)
Connections are as per example code - with 10 used as CS

I can flash the orange built LED '13' before SD card is used. However after card is accessed and '13' has been used as SPI clock, it is then no longer accessible when the SD activity is complete. This is despite using SD.end and SP.end, which appears to be the accepted method for making the pins available for I/O after use for SPI on other boards.

Issue affects all MISO/MOSI/CLK pins.
I have tried both SD.h and SdFat libraries.
Using or omitting SPI.end does not appear to make any difference.

Any idea if I am missing something fundamental - or am I trying to do something that is not supported by this board?

Many thanks for any thoughts

M

#include <SPI.h>

#include <SD.h>

/*
#include"SdFat.h"
SdFat SD;
*/

File myFile; 

const int chipSelect = 10;                //chip select pin for sd card reader - not defined in SD.h/SdFat.h
                            
void write_to_card()
{

  // Open serial communications and wait for port to open:
  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("1. is a card inserted?");
    Serial.println("2. is your wiring correct?");
    Serial.println("3. did you change the chipSelect pin to match your shield or module?");
    Serial.println("Note: press reset button on the board and reopen this Serial Monitor after fixing your issue!");
    while (true);
  }

  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

SD.end();
delay(50);
/*
SPI.endTransaction();
delay(50);
*/
SPI.end();
delay(50);
}



void flash_builtin()
{
int flashloop=0;
pinMode(13,OUTPUT);       //13 is built in LED and SPI CLOCK
  do{
  digitalWrite(13,HIGH);
  delay(300);
  digitalWrite(13,LOW);
  delay(300);
  flashloop++;
  }while(flashloop<10);
}





void setup() 
{
Serial.begin(9600);
while(!Serial);

Serial.println("FLASH LIGHT BEFORE SD USED");
flash_builtin();

write_to_card();              //set up SD reader, and access card

Serial.println("FLASH LIGHT AFTER SD USED");
flash_builtin();
}



void loop() 
{
}

Show your schematic, because I don't know what it is:

"Connections are as per example code - with 10 used as CS"

Your code correctly records both values ​​(see printout below), but it runs only once, as it's in setup(), and it blinks before and after recording, so I don't understand your difficulty.

Explain your difficulty better.

Simulated at:

“https://wokwi.com/projects/444699652161714177

FLASH LIGHT BEFORE SD USED
Initializing SD card...initialization done.
Writing to test.txt...done.
test.txt:
testing 1, 2, 3.
FLASH LIGHT AFTER SD USED

Thanks for looking at this for me.

The card connections betwwen the nano and sd card reader are:

NANO PIN--------------------------CARD READER
PIN 10-------------------------------CHIP SELECT
PIN 11-------------------------------MOSI
PIN 12-------------------------------MISO
PIN 13-------------------------------CLOCK

The write_to_card() function is based on the Example READWRITE under SD entry in EXAMPLES - pins are the same (apart from CS which is just general I/O pin)

The code is to demonstrate unexpected behaviour of I/O pins after the SD card is used.

The issue I have is:

The first time flash_builtin() is called (before the SD card is accessed) the light flashes.

The second time flash_builtin() is called (after the SD card has been used) the light does not flash.

What I would like to undestand is why the light does not flash the second time flash_builtin() is called, as I believe I have done all that should be needed to stop pin 13 behaving as SPI pin, and expected light to flash.

Hope this makes sense?

I think so too. The pin should be released from the SPI peripheral and you set it to OUTPUT again.

I think it should be one for the Arduino team. The pin nr. of the processor is of course different from the board pin assignment. Your hardware could not really make a difference since it is connected already when the first flash cycle happens. Just in case try setting the CS pin HIGH manually to disable the SD card and see if that helps.

pinMode(chipSelect, OUTPUT);
digitalWrite(chipSelect, HIGH);

Hi,

Many thanks for the input.

I have just tried forcing the CS line high before SD.end, between SD.end and SPI.end and after SPI.end

These attempts did not work.

Please excuse my ignorance, do the Arduino Team monitor these posts, or should I raise it with them another way?

Thanks

Where are card power and ground connected?

Post a close up, focused photo of your setup.

Perhaps @ptillisch will see this.

On an Atmega328P Arduino, you could just write a zero to the SPCR register, and that would turn off all SPI functions and release the pins to normal GPIO. But I don't know how that works on your Nano BLE.

But remember that the SD card and module are still connected to those four pins. Three are inputs to the card, so they should be no problem. But MISO is a card output which could possibly turn on if you let CS go low, which selects the card.

We could just mention @ptillisch, he is the most frequent person from the team on the forum, i suspect they don't monitor all posts.

Hi
This is the board I am using whilst testing today.

3.3v v SD reader is connected to 3.3V pin on board.
Gnd to Gnd pin

C/S,10
mosi,11
miso,12
clock,13

Have also had same behaviour with prototyping board, and PCB.
Basic SD operation is always fine.

Thanks
M

There is a long history of problems with those dirt cheap SD modules. One of them is the failure to release the MISO line, which could be hanging up the the SD library.

If that is the problem, it can be corrected in some cases by modifications to the module. Search the forum for relevant posts.

Alternatively, buy a new SD module from one of the many reputable suppliers who support their products.

Hi.
Many thanks for your input.
I accept the board shown is a cheap and cheerless one.
I have had a look at forum entries about unreleased MISO lines - looks like this may be more applicable to 5V powered ones than 3.3v?
The board pictured is just the connector, 2 caps and 4 resistors.
The PCB I mentioned uses a Molex SD connector - this exhibits same behaviour.
Will search online for more info on the release issue though.
Thanks
M

It may be that you have a defective or incorrectly assembled SD module or card (the SD cards each have an MCU built in). Try another of each, if you have them.

Please post a link to where you purchased the module, and close up pics of both sides of it.

Hi,
I have tried several cards with same result - will have a scrabble round to see if I have another reader board.

This is a listing for the reader (with good pictures)
card reader

This is the connector used on the PCB that also shows same issue
Connector

Thanks
M

Sorry, I'm out of ideas. The Nano 33 BLE Sense (do you have Rev 1 or 2?) seems rather unpopular, as there are not many posts on this forum about it, especially concerning SD cards.

In this case, an SD module problem was solved by buying a different module.

That appears to be just a basic breakout board, with everything at 3.3V. It's hard to see how that could be the problem.

The datasheet says there's a 32-bit register called SPI ENABLE at location "0x500".

I wonder how you could write a zero to that register in Arm. Or how you could read it after the .end statements to see what value it has.

Hi - Firstly - Many thanks again to all for your suggestions.

  • quick update. I managed find/extract/remove/beg/borrow various additional parts last night.

I have retested with:
another couple of SD cards.
another identical SD reader.
a different SD reader (albeit a 5V with the regulator shorted out).
and for completeness even a different breakout board
These all show same LED behaviour with the Nano 33 BLE (R2) board

I then managed to find a standard Nano, A Nano 33 IOT and another Nano 33 BLE (R2).
The standard Nano and IOT unit performed as I would have expected, with light flashing before and after SD card accessed. The second Nano 33 BLE (R2) exhibits same behaviour as the first BLE board.

So this strongly points to the board being the source of the odd behaviour
Looks like next step is to investigate SPI enable register as per @ ShermanP suggestion (Thanks).

Will update if I learn anything.

M

Hi,

I've come to the conclusion that I don't think I am doing anything wrong with the code or wiring.

I've tried looking at accessing the registers to view/modify SPI ENABLE but have reached the limit of my abilty and understanding.

Very happy to hear from anyone else if you get any further than I have.

Many thanks for the help.

M

You might try using this prompt with your favorite AI:

How can I read from and write to peripheral registers of the nRF52840 controller, such as the SPI Enable register at offset 0x500?

Many thanks - your suggested seach term (with google AI) yielded a much more meaningful answer than the searches I tried earlier.

I have just both suggested variations of the code.

uint32_t current_enable_value = NRF_SPIM0->ENABLE;
Serial.print("Start value of NRFSPIM0=");
Serial.println(current_enable_value);
NRF_SPIM0->ENABLE = 0;
current_enable_value = NRF_SPIM0->ENABLE;
Serial.print("New value of NRFSPIM0=");
Serial.println(current_enable_value);

and this

#define NRF_SPIM0_BASE_ADDRESS 0x40003000 // Define the base address for SPIM0
#define NRF_SPIM_ENABLE_OFFSET 0x500      // Define the offset for the ENABLE register
#define NRF_SPIM0_ENABLE_ADDRESS (NRF_SPIM0_BASE_ADDRESS + NRF_SPIM_ENABLE_OFFSET)  // Define the full address for the SPIM0 ENABLE register
volatile uint32_t* spim0_enable_reg = (volatile uint32_t*)NRF_SPIM0_ENABLE_ADDRESS;  // Create a volatile pointer to the register

uint32_t current_enable_value = *spim0_enable_reg;  // Example to read the current value of the register
Serial.print("Start value of NRFSPIM0=");
Serial.println(current_enable_value);
*spim0_enable_reg = 0;
current_enable_value = *spim0_enable_reg;
Serial.print("New value of NRFSPIM0=");
Serial.println(current_enable_value);

.......both before and after the call to SPI.end.

With both versions of code, in tests both before and after SPI.end, I always have 0 reported as register value.
With LED bahaving as before.

(As a test I tried changing register to 1 - this does accept the change and report value accordingly).

Really puzzled now.

I found another reference to force a soft reset of the SPI
(With suggestion this may be used if internal conflict prevents it being disabled)

// Forcefully stop the SPI master, then disable it
NRF_SPIM0->TASKS_STOP = 1;

// Wait for the STOPPED event to ensure the peripheral has fully stopped
while (NRF_SPIM0->EVENTS_STOPPED == 0);

// Clear the STOPPED event
NRF_SPIM0->EVENTS_STOPPED = 0;

// Now, disable the peripheral
NRF_SPIM0->ENABLE = 0;

But still no joy.