STM32L4xx_HAL_Driver  1.14.0
SDMMC Low Layer

Low layer module for SD. More...

Modules

 SDMMC Low Layer Exported Functions
 

Functions

static uint32_t SDMMC_GetCmdError (SDMMC_TypeDef *SDMMCx)
 Checks for error conditions for CMD0. More...
 
static uint32_t SDMMC_GetCmdResp1 (SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint32_t Timeout)
 Checks for error conditions for R1 response. More...
 
static uint32_t SDMMC_GetCmdResp2 (SDMMC_TypeDef *SDMMCx)
 Checks for error conditions for R2 (CID or CSD) response. More...
 
static uint32_t SDMMC_GetCmdResp3 (SDMMC_TypeDef *SDMMCx)
 Checks for error conditions for R3 (OCR) response. More...
 
static uint32_t SDMMC_GetCmdResp7 (SDMMC_TypeDef *SDMMCx)
 Checks for error conditions for R7 response. More...
 
static uint32_t SDMMC_GetCmdResp6 (SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint16_t *pRCA)
 Checks for error conditions for R6 (RCA) response. More...
 

Detailed Description

Low layer module for SD.

Function Documentation

◆ SDMMC_GetCmdError()

static uint32_t SDMMC_GetCmdError ( SDMMC_TypeDef *  SDMMCx)
static

Checks for error conditions for CMD0.

Parameters
hsdSD handle
Return values
SDCard error state

Definition at line 1241 of file stm32l4xx_ll_sdmmc.c.

1242 {
1243  /* 8 is the number of required instructions cycles for the below loop statement.
1244  The SDMMC_CMDTIMEOUT is expressed in ms */
1245  register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8U /1000U);
1246 
1247  do
1248  {
1249  if (count-- == 0U)
1250  {
1251  return SDMMC_ERROR_TIMEOUT;
1252  }
1253 
1254  }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CMDSENT));
1255 
1256  /* Clear all the static flags */
1257  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_CMD_FLAGS);
1258 
1259  return SDMMC_ERROR_NONE;
1260 }

◆ SDMMC_GetCmdResp1()

static uint32_t SDMMC_GetCmdResp1 ( SDMMC_TypeDef *  SDMMCx,
uint8_t  SD_CMD,
uint32_t  Timeout 
)
static

Checks for error conditions for R1 response.

Parameters
hsdSD handle
SD_CMDThe sent command index
Return values
SDCard error state

Definition at line 1268 of file stm32l4xx_ll_sdmmc.c.

1269 {
1270  uint32_t response_r1;
1271  uint32_t sta_reg;
1272 
1273  /* 8 is the number of required instructions cycles for the below loop statement.
1274  The Timeout is expressed in ms */
1275  register uint32_t count = Timeout * (SystemCoreClock / 8U /1000U);
1276 
1277  do
1278  {
1279  if (count-- == 0U)
1280  {
1281  return SDMMC_ERROR_TIMEOUT;
1282  }
1283  sta_reg = SDMMCx->STA;
1284 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1285  }while(((sta_reg & (SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT | SDMMC_FLAG_BUSYD0END)) == 0U) ||
1286  ((sta_reg & SDMMC_FLAG_CMDACT) != 0U ));
1287 #else
1288  }while(((sta_reg & (SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) == 0U) ||

◆ SDMMC_GetCmdResp2()

static uint32_t SDMMC_GetCmdResp2 ( SDMMC_TypeDef *  SDMMCx)
static

Checks for error conditions for R2 (CID or CSD) response.

Parameters
hsdSD handle
Return values
SDCard error state

Definition at line 1408 of file stm32l4xx_ll_sdmmc.c.

1409 {
1410  uint32_t sta_reg;
1411  /* 8 is the number of required instructions cycles for the below loop statement.
1412  The SDMMC_CMDTIMEOUT is expressed in ms */
1413  register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8U /1000U);
1414 
1415  do
1416  {
1417  if (count-- == 0U)
1418  {
1419  return SDMMC_ERROR_TIMEOUT;
1420  }
1421  sta_reg = SDMMCx->STA;
1422  }while(((sta_reg & (SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) == 0U) ||
1423  ((sta_reg & SDMMC_FLAG_CMDACT) != 0U ));
1424 
1425  if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1426  {
1427  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1428 
1429  return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1430  }
1431  else if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
1432  {
1433  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
1434 
1435  return SDMMC_ERROR_CMD_CRC_FAIL;
1436  }
1437  else
1438  {
1439  /* No error flag set */
1440  /* Clear all the static flags */
1441  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_CMD_FLAGS);
1442  }
1443 
1444  return SDMMC_ERROR_NONE;
1445 }

◆ SDMMC_GetCmdResp3()

static uint32_t SDMMC_GetCmdResp3 ( SDMMC_TypeDef *  SDMMCx)
static

Checks for error conditions for R3 (OCR) response.

Parameters
hsdSD handle
Return values
SDCard error state

Definition at line 1452 of file stm32l4xx_ll_sdmmc.c.

1453 {
1454  uint32_t sta_reg;
1455  /* 8 is the number of required instructions cycles for the below loop statement.
1456  The SDMMC_CMDTIMEOUT is expressed in ms */
1457  register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8U /1000U);
1458 
1459  do
1460  {
1461  if (count-- == 0U)
1462  {
1463  return SDMMC_ERROR_TIMEOUT;
1464  }
1465  sta_reg = SDMMCx->STA;
1466  }while(((sta_reg & (SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) == 0U) ||
1467  ((sta_reg & SDMMC_FLAG_CMDACT) != 0U ));
1468 
1469  if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1470  {
1471  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1472 
1473  return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1474  }
1475  else
1476  {
1477  /* Clear all the static flags */
1478  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_CMD_FLAGS);
1479  }
1480 
1481  return SDMMC_ERROR_NONE;
1482 }

◆ SDMMC_GetCmdResp6()

static uint32_t SDMMC_GetCmdResp6 ( SDMMC_TypeDef *  SDMMCx,
uint8_t  SD_CMD,
uint16_t *  pRCA 
)
static

Checks for error conditions for R6 (RCA) response.

Parameters
hsdSD handle
SD_CMDThe sent command index
pRCAPointer to the variable that will contain the SD card relative address RCA
Return values
SDCard error state

Definition at line 1492 of file stm32l4xx_ll_sdmmc.c.

1493 {
1494  uint32_t response_r1;
1495  uint32_t sta_reg;
1496 
1497  /* 8 is the number of required instructions cycles for the below loop statement.
1498  The SDMMC_CMDTIMEOUT is expressed in ms */
1499  register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8U /1000U);
1500 
1501  do
1502  {
1503  if (count-- == 0U)
1504  {
1505  return SDMMC_ERROR_TIMEOUT;
1506  }
1507  sta_reg = SDMMCx->STA;
1508  }while(((sta_reg & (SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) == 0U) ||
1509  ((sta_reg & SDMMC_FLAG_CMDACT) != 0U ));
1510 
1511  if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1512  {
1513  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1514 
1515  return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1516  }
1517  else if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
1518  {
1519  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
1520 
1521  return SDMMC_ERROR_CMD_CRC_FAIL;
1522  }
1523  else
1524  {
1525  /* Nothing to do */
1526  }
1527 
1528  /* Check response received is of desired command */
1529  if(SDMMC_GetCommandResponse(SDMMCx) != SD_CMD)
1530  {
1531  return SDMMC_ERROR_CMD_CRC_FAIL;
1532  }
1533 
1534  /* Clear all the static flags */
1535  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_CMD_FLAGS);
1536 
1537  /* We have received response, retrieve it. */
1538  response_r1 = SDMMC_GetResponse(SDMMCx, SDMMC_RESP1);
1539 
1540  if((response_r1 & (SDMMC_R6_GENERAL_UNKNOWN_ERROR | SDMMC_R6_ILLEGAL_CMD | SDMMC_R6_COM_CRC_FAILED)) == SDMMC_ALLZERO)
1541  {
1542  *pRCA = (uint16_t) (response_r1 >> 16);
1543 
1544  return SDMMC_ERROR_NONE;
1545  }
1546  else if((response_r1 & SDMMC_R6_ILLEGAL_CMD) == SDMMC_R6_ILLEGAL_CMD)
1547  {
1548  return SDMMC_ERROR_ILLEGAL_CMD;
1549  }
1550  else if((response_r1 & SDMMC_R6_COM_CRC_FAILED) == SDMMC_R6_COM_CRC_FAILED)
1551  {
1552  return SDMMC_ERROR_COM_CRC_FAILED;
1553  }
1554  else
1555  {
1556  return SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
1557  }
1558 }
uint32_t SDMMC_GetResponse(SDMMC_TypeDef *SDMMCx, uint32_t Response)
Return the response received from the card for the last command.
uint8_t SDMMC_GetCommandResponse(SDMMC_TypeDef *SDMMCx)
Return the command index of last command for which response received.

◆ SDMMC_GetCmdResp7()

static uint32_t SDMMC_GetCmdResp7 ( SDMMC_TypeDef *  SDMMCx)
static

Checks for error conditions for R7 response.

Parameters
hsdSD handle
Return values
SDCard error state

Definition at line 1565 of file stm32l4xx_ll_sdmmc.c.

1566 {
1567  uint32_t sta_reg;
1568  /* 8 is the number of required instructions cycles for the below loop statement.
1569  The SDMMC_CMDTIMEOUT is expressed in ms */
1570  register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8U /1000U);
1571 
1572  do
1573  {
1574  if (count-- == 0U)
1575  {
1576  return SDMMC_ERROR_TIMEOUT;
1577  }
1578  sta_reg = SDMMCx->STA;
1579  }while(((sta_reg & (SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT)) == 0U) ||
1580  ((sta_reg & SDMMC_FLAG_CMDACT) != 0U ));
1581 
1582  if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1583  {
1584  /* Card is SD V2.0 compliant */
1585  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1586 
1587  return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1588  }
1589 
1590  else if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
1591  {
1592  /* Card is SD V2.0 compliant */
1593  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
1594 
1595  return SDMMC_ERROR_CMD_CRC_FAIL;
1596  }
1597  else
1598  {
1599  /* Nothing to do */
1600  }
1601 
1602  if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CMDREND))
1603  {
1604  /* Card is SD V2.0 compliant */
1605  __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CMDREND);
1606  }
1607 
1608  return SDMMC_ERROR_NONE;
1609 
1610 }