cancel
Showing results for 
Search instead for 
Did you mean: 

Set sdcard "temporary write protect" bit - STM32f7

TGeof.2
Associate II

Hi.

I'm working on a project with a STM32F722 and a microSD card. I'm using HAL_SD & FatFS.

I want to modify the CSD of the SD card to set the "temporary write protect" bit to 1, to protect the card from accidental writes.

It is my understanding you can either change the CSD directly, or use the dedicated commands, such as defined in stm32f7xx_ll_sdmmc.h:

#define SDMMC_CMD_SET_WRITE_PROT                                28U   /*!< Sets the write protection bit of the addressed group.                                    */
#define SDMMC_CMD_CLR_WRITE_PROT                                29U   /*!< Clears the write protection bit of the addressed group.                                  */
#define SDMMC_CMD_SEND_WRITE_PROT                               30U   /*!< Asks the card to send the status of the write protection bits.                           */

I've tried sending those commands, during various stages of the SD card init, and even "built-in" commands, but the command response times out, and returns this:

#define SDMMC_ERROR_CMD_RSP_TIMEOUT                     0x00000004U    /*!< Command response timeout                                      */

For example, I've even tried sending the same command twice, and the first passes while the second times out:

// stm32f7xx_hal_sd.c
 
static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
{
// ...
if(hsd->SdCard.CardType != CARD_SECURED)
  {
    /* Send CMD2 ALL_SEND_CID */
    errorstate = SDMMC_CmdSendCID(hsd->Instance);
    if(errorstate != HAL_SD_ERROR_NONE)
    {
      return errorstate;
    }
    else
    {
      /* Get Card identification number data */
      hsd->CID[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
      hsd->CID[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
      hsd->CID[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
      hsd->CID[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
    }
  }
 
if(hsd->SdCard.CardType != CARD_SECURED)
  {
    /* Send CMD2 ALL_SEND_CID */
    errorstate = SDMMC_CmdSendCID(hsd->Instance);
    if(errorstate != HAL_SD_ERROR_NONE)
    {
      return errorstate; // HERE IT TIMES OUT !    
// ...

So it seems the SDMMC expects a certain order or commands ?

In any case, I'm not sure where exactly I should be able to send a command, or how to put the SD back into CMD state in order to do so.

Is there something I'm missing wrt to sending commands and receiving responses ? Is there any simpler command I could be using for testing ?

1 REPLY 1

Hello

Not all SD cards have 28,39,30 commands implemented. Check card's datasheet.