STM32L4xx_HAL_Driver  1.14.0
Input and Output operation functions

Data transfer functions. More...

Functions

HAL_StatusTypeDef HAL_MMC_ReadBlocks (MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
 Reads block(s) from a specified address in a card. The Data transfer is managed by polling mode. More...
 
HAL_StatusTypeDef HAL_MMC_WriteBlocks (MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
 Allows to write block(s) to a specified address in a card. The Data transfer is managed by polling mode. More...
 
HAL_StatusTypeDef HAL_MMC_Erase (MMC_HandleTypeDef *hmmc, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
 Erases the specified memory area of the given MMC card. More...
 
HAL_StatusTypeDef HAL_MMC_ReadBlocks_IT (MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
 Reads block(s) from a specified address in a card. The Data transfer is managed in interrupt mode. More...
 
HAL_StatusTypeDef HAL_MMC_WriteBlocks_IT (MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
 Writes block(s) to a specified address in a card. The Data transfer is managed in interrupt mode. More...
 
HAL_StatusTypeDef HAL_MMC_ReadBlocks_DMA (MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
 Reads block(s) from a specified address in a card. The Data transfer is managed by DMA mode. More...
 
HAL_StatusTypeDef HAL_MMC_WriteBlocks_DMA (MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
 Writes block(s) to a specified address in a card. The Data transfer is managed by DMA mode. More...
 
void HAL_MMC_IRQHandler (MMC_HandleTypeDef *hmmc)
 This function handles MMC card interrupt request. More...
 
void HAL_MMC_TxCpltCallback (MMC_HandleTypeDef *hmmc)
 Tx Transfer completed callbacks. More...
 
void HAL_MMC_RxCpltCallback (MMC_HandleTypeDef *hmmc)
 Rx Transfer completed callbacks. More...
 
void HAL_MMC_ErrorCallback (MMC_HandleTypeDef *hmmc)
 MMC error callbacks. More...
 
void HAL_MMC_AbortCallback (MMC_HandleTypeDef *hmmc)
 MMC Abort callbacks. More...
 
HAL_StatusTypeDef HAL_MMC_RegisterCallback (MMC_HandleTypeDef *hmmc, HAL_MMC_CallbackIDTypeDef CallbackId, pMMC_CallbackTypeDef pCallback)
 Register a User MMC Callback To be used instead of the weak (surcharged) predefined callback. More...
 
HAL_StatusTypeDef HAL_MMC_UnRegisterCallback (MMC_HandleTypeDef *hmmc, HAL_MMC_CallbackIDTypeDef CallbackId)
 Unregister a User MMC Callback MMC Callback is redirected to the weak (surcharged) predefined callback. More...
 
HAL_MMC_StateTypeDef HAL_MMC_GetState (MMC_HandleTypeDef *hmmc)
 return the MMC state More...
 
uint32_t HAL_MMC_GetError (MMC_HandleTypeDef *hmmc)
 Return the MMC error code. More...
 

Detailed Description

Data transfer functions.

  ==============================================================================
                        ##### IO operation functions #####
  ==============================================================================
  [..]
    This subsection provides a set of functions allowing to manage the data
    transfer from/to MMC card.

Function Documentation

◆ HAL_MMC_AbortCallback()

__weak void HAL_MMC_AbortCallback ( MMC_HandleTypeDef hmmc)

MMC Abort callbacks.

Parameters
hmmcPointer MMC handle
Return values
None

Definition at line 2001 of file stm32l4xx_hal_mmc.c.

2002 {
2003  /* Prevent unused argument(s) compilation warning */
2004  UNUSED(hmmc);
2005 
2006  /* NOTE : This function should not be modified, when the callback is needed,
2007  the HAL_MMC_AbortCallback can be implemented in the user file
2008  */
2009 }

◆ HAL_MMC_Erase()

HAL_StatusTypeDef HAL_MMC_Erase ( MMC_HandleTypeDef hmmc,
uint32_t  BlockStartAdd,
uint32_t  BlockEndAdd 
)

Erases the specified memory area of the given MMC card.

Note
This API should be followed by a check on the card state through HAL_MMC_GetCardState().
Parameters
hmmcPointer to MMC handle
BlockStartAddStart Block address
BlockEndAddEnd Block address
Return values
HALstatus

Definition at line 1528 of file stm32l4xx_hal_mmc.c.

1529 {
1530  uint32_t errorstate;
1531  uint32_t start_add = BlockStartAdd;
1532  uint32_t end_add = BlockEndAdd;
1533 
1534  if(hmmc->State == HAL_MMC_STATE_READY)
1535  {
1536  hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
1537 
1538  if(end_add < start_add)
1539  {
1540  hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
1541  return HAL_ERROR;
1542  }
1543 
1544  if(end_add > (hmmc->MmcCard.LogBlockNbr))
1545  {
1546  hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
1547  return HAL_ERROR;
1548  }
1549 
1550  hmmc->State = HAL_MMC_STATE_BUSY;
1551 
1552  /* Check if the card command class supports erase command */
1553  if(((hmmc->MmcCard.Class) & SDMMC_CCCC_ERASE) == 0U)
1554  {
1555  /* Clear all the static flags */
1556  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1557  hmmc->ErrorCode |= HAL_MMC_ERROR_REQUEST_NOT_APPLICABLE;
1558  hmmc->State = HAL_MMC_STATE_READY;
1559  return HAL_ERROR;
1560  }
1561 
1562  if((SDMMC_GetResponse(hmmc->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
1563  {
1564  /* Clear all the static flags */
1565  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1566  hmmc->ErrorCode |= HAL_MMC_ERROR_LOCK_UNLOCK_FAILED;
1567  hmmc->State = HAL_MMC_STATE_READY;
1568  return HAL_ERROR;
1569  }
1570 
1571  if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
1572  {
1573  start_add *= 512U;
1574  end_add *= 512U;
1575  }
1576 
1577  /* Send CMD35 MMC_ERASE_GRP_START with argument as addr */
1578  errorstate = SDMMC_CmdEraseStartAdd(hmmc->Instance, start_add);
1579  if(errorstate != HAL_MMC_ERROR_NONE)
1580  {
1581  /* Clear all the static flags */
1582  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1583  hmmc->ErrorCode |= errorstate;
1584  hmmc->State = HAL_MMC_STATE_READY;
1585  return HAL_ERROR;
1586  }
1587 
1588  /* Send CMD36 MMC_ERASE_GRP_END with argument as addr */
1589  errorstate = SDMMC_CmdEraseEndAdd(hmmc->Instance, end_add);
1590  if(errorstate != HAL_MMC_ERROR_NONE)
1591  {
1592  /* Clear all the static flags */
1593  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1594  hmmc->ErrorCode |= errorstate;
1595  hmmc->State = HAL_MMC_STATE_READY;
1596  return HAL_ERROR;
1597  }
1598 
1599  /* Send CMD38 ERASE */
1600  errorstate = SDMMC_CmdErase(hmmc->Instance);
1601  if(errorstate != HAL_MMC_ERROR_NONE)
1602  {
1603  /* Clear all the static flags */
1604  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1605  hmmc->ErrorCode |= errorstate;
1606  hmmc->State = HAL_MMC_STATE_READY;
1607  return HAL_ERROR;
1608  }
1609 
1610  hmmc->State = HAL_MMC_STATE_READY;
1611 
1612  return HAL_OK;
1613  }
1614  else
1615  {
1616  return HAL_BUSY;
1617  }
1618 }
uint32_t SDMMC_CmdErase(SDMMC_TypeDef *SDMMCx)
Send the Erase command and check the response.
return HAL_OK
uint32_t SDMMC_GetResponse(SDMMC_TypeDef *SDMMCx, uint32_t Response)
Return the response received from the card for the last command.
uint32_t SDMMC_CmdEraseEndAdd(SDMMC_TypeDef *SDMMCx, uint32_t EndAdd)
Send the End Address Erase command and check the response.
uint32_t SDMMC_CmdEraseStartAdd(SDMMC_TypeDef *SDMMCx, uint32_t StartAdd)
Send the Start Address Erase command and check the response.

◆ HAL_MMC_ErrorCallback()

__weak void HAL_MMC_ErrorCallback ( MMC_HandleTypeDef hmmc)

MMC error callbacks.

Parameters
hmmcPointer MMC handle
Return values
None

Definition at line 1986 of file stm32l4xx_hal_mmc.c.

1987 {
1988  /* Prevent unused argument(s) compilation warning */
1989  UNUSED(hmmc);
1990 
1991  /* NOTE : This function should not be modified, when the callback is needed,
1992  the HAL_MMC_ErrorCallback can be implemented in the user file
1993  */
1994 }

◆ HAL_MMC_GetError()

uint32_t HAL_MMC_GetError ( MMC_HandleTypeDef hmmc)

Return the MMC error code.

Parameters
hmmc: Pointer to a MMC_HandleTypeDef structure that contains the configuration information.
Return values
MMCError Code

Definition at line 1946 of file stm32l4xx_hal_mmc.c.

1947 {
1948  return hmmc->ErrorCode;
1949 }

◆ HAL_MMC_GetState()

HAL_MMC_StateTypeDef HAL_MMC_GetState ( MMC_HandleTypeDef hmmc)

return the MMC state

Parameters
hmmcPointer to mmc handle
Return values
HALstate

Definition at line 1935 of file stm32l4xx_hal_mmc.c.

1936 {
1937  return hmmc->State;
1938 }

◆ HAL_MMC_IRQHandler()

void HAL_MMC_IRQHandler ( MMC_HandleTypeDef hmmc)

This function handles MMC card interrupt request.

Parameters
hmmcPointer to MMC handle
Return values
None

Definition at line 1625 of file stm32l4xx_hal_mmc.c.

1626 {
1627  uint32_t errorstate;
1628  uint32_t context = hmmc->Context;
1629 
1630  /* Check for SDMMC interrupt flags */
1631  if((__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXFIFOHF) != RESET) && ((context & MMC_CONTEXT_IT) != 0U))
1632  {
1633  MMC_Read_IT(hmmc);
1634  }
1635 
1636  else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DATAEND) != RESET)
1637  {
1638  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_FLAG_DATAEND);
1639 
1640  __HAL_MMC_DISABLE_IT(hmmc, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT |\
1641  SDMMC_IT_TXUNDERR | SDMMC_IT_RXOVERR | SDMMC_IT_TXFIFOHE |\
1642  SDMMC_IT_RXFIFOHF);
1643 
1644 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1645  hmmc->Instance->DCTRL &= ~(SDMMC_DCTRL_DTEN);
1646 #else
1647  __HAL_MMC_DISABLE_IT(hmmc, SDMMC_IT_IDMABTC);
1648  __SDMMC_CMDTRANS_DISABLE( hmmc->Instance);
1649 #endif
1650 
1651  if((context & MMC_CONTEXT_DMA) != 0U)
1652  {
1653 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1654  hmmc->Instance->DLEN = 0;
1655  hmmc->Instance->DCTRL = 0;
1656  hmmc->Instance->IDMACTRL = SDMMC_DISABLE_IDMA ;
1657 
1658  /* Stop Transfer for Write Multi blocks or Read Multi blocks */
1659  if(((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1660  {
1661  errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
1662  if(errorstate != HAL_MMC_ERROR_NONE)
1663  {
1664  hmmc->ErrorCode |= errorstate;
1665 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1666  hmmc->ErrorCallback(hmmc);
1667 #else
1668  HAL_MMC_ErrorCallback(hmmc);
1669 #endif
1670  }
1671  }
1672 
1673  /* Clear all the static flags */
1674  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
1675 
1676  hmmc->State = HAL_MMC_STATE_READY;
1677  if(((context & MMC_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1678  {
1679 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1680  hmmc->TxCpltCallback(hmmc);
1681 #else
1682  HAL_MMC_TxCpltCallback(hmmc);
1683 #endif
1684  }
1685  if(((context & MMC_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1686  {
1687 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1688  hmmc->RxCpltCallback(hmmc);
1689 #else
1690  HAL_MMC_RxCpltCallback(hmmc);
1691 #endif
1692  }
1693 #else
1694  if((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1695  {
1696  errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
1697  if(errorstate != HAL_MMC_ERROR_NONE)
1698  {
1699  hmmc->ErrorCode |= errorstate;
1700 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1701  hmmc->ErrorCallback(hmmc);
1702 #else
1703  HAL_MMC_ErrorCallback(hmmc);
1704 #endif
1705  }
1706  }
1707  if(((context & MMC_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) == 0U))
1708  {
1709  /* Disable the DMA transfer for transmit request by setting the DMAEN bit
1710  in the MMC DCTRL register */
1711  hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
1712 
1713  hmmc->State = HAL_MMC_STATE_READY;
1714 
1715 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1716  hmmc->TxCpltCallback(hmmc);
1717 #else
1718  HAL_MMC_TxCpltCallback(hmmc);
1719 #endif
1720  }
1721 #endif
1722  }
1723  else if((context & MMC_CONTEXT_IT) != 0U)
1724  {
1725  /* Stop Transfer for Write Multi blocks or Read Multi blocks */
1726  if(((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1727  {
1728  errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
1729  if(errorstate != HAL_MMC_ERROR_NONE)
1730  {
1731  hmmc->ErrorCode |= errorstate;
1732 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1733  hmmc->ErrorCallback(hmmc);
1734 #else
1735  HAL_MMC_ErrorCallback(hmmc);
1736 #endif
1737  }
1738  }
1739 
1740  /* Clear all the static flags */
1741  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
1742 
1743  hmmc->State = HAL_MMC_STATE_READY;
1744  if(((context & MMC_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1745  {
1746 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1747  hmmc->RxCpltCallback(hmmc);
1748 #else
1749  HAL_MMC_RxCpltCallback(hmmc);
1750 #endif
1751  }
1752  else
1753  {
1754 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1755  hmmc->TxCpltCallback(hmmc);
1756 #else
1757  HAL_MMC_TxCpltCallback(hmmc);
1758 #endif
1759  }
1760  }
1761  else
1762  {
1763  /* Nothing to do */
1764  }
1765  }
1766 
1767  else if((__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_TXFIFOHE) != RESET) && ((context & MMC_CONTEXT_IT) != 0U))
1768  {
1769  MMC_Write_IT(hmmc);
1770  }
1771 
1772  else if (__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DCRCFAIL| SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_RXOVERR | SDMMC_FLAG_TXUNDERR) != RESET)
1773  {
1774  /* Set Error code */
1775  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_IT_DCRCFAIL) != RESET)
1776  {
1777  hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
1778  }
1779  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_IT_DTIMEOUT) != RESET)
1780  {
1781  hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
1782  }
1783  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_IT_RXOVERR) != RESET)
1784  {
1785  hmmc->ErrorCode |= HAL_MMC_ERROR_RX_OVERRUN;
1786  }
1787  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_IT_TXUNDERR) != RESET)
1788  {
1789  hmmc->ErrorCode |= HAL_MMC_ERROR_TX_UNDERRUN;
1790  }
1791 
1792  /* Clear All flags */
1793  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
1794 
1795  /* Disable all interrupts */
1796  __HAL_MMC_DISABLE_IT(hmmc, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
1797  SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
1798 
1799 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1800  __SDMMC_CMDTRANS_DISABLE( hmmc->Instance);
1801  hmmc->Instance->DCTRL |= SDMMC_DCTRL_FIFORST;
1802  hmmc->Instance->CMD |= SDMMC_CMD_CMDSTOP;
1803 #endif
1804  hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
1805 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1806  hmmc->Instance->CMD &= ~(SDMMC_CMD_CMDSTOP);
1807  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_FLAG_DABORT);
1808 #endif
1809 
1810  if((context & MMC_CONTEXT_IT) != 0U)
1811  {
1812  /* Set the MMC state to ready to be able to start again the process */
1813  hmmc->State = HAL_MMC_STATE_READY;
1814 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1815  hmmc->ErrorCallback(hmmc);
1816 #else
1817  HAL_MMC_ErrorCallback(hmmc);
1818 #endif /* USE_HAL_MMC_REGISTER_CALLBACKS */
1819  }
1820  else if((context & MMC_CONTEXT_DMA) != 0U)
1821  {
1822 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1823  if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
1824  {
1825  /* Disable Internal DMA */
1826  __HAL_MMC_DISABLE_IT(hmmc, SDMMC_IT_IDMABTC);
1827  hmmc->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
1828 
1829  /* Set the MMC state to ready to be able to start again the process */
1830  hmmc->State = HAL_MMC_STATE_READY;
1831 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1832  hmmc->ErrorCallback(hmmc);
1833 #else
1834  HAL_MMC_ErrorCallback(hmmc);
1835 #endif /* USE_HAL_MMC_REGISTER_CALLBACKS */
1836  }
1837 #else
1838  /* Abort the MMC DMA Streams */
1839  if(hmmc->hdmatx != NULL)
1840  {
1841  /* Set the DMA Tx abort callback */
1842  hmmc->hdmatx->XferAbortCallback = MMC_DMATxAbort;
1843  /* Abort DMA in IT mode */
1844  if(HAL_DMA_Abort_IT(hmmc->hdmatx) != HAL_OK)
1845  {
1846  MMC_DMATxAbort(hmmc->hdmatx);
1847  }
1848  }
1849  else if(hmmc->hdmarx != NULL)
1850  {
1851  /* Set the DMA Rx abort callback */
1852  hmmc->hdmarx->XferAbortCallback = MMC_DMARxAbort;
1853  /* Abort DMA in IT mode */
1854  if(HAL_DMA_Abort_IT(hmmc->hdmarx) != HAL_OK)
1855  {
1856  MMC_DMARxAbort(hmmc->hdmarx);
1857  }
1858  }
1859  else
1860  {
1861  hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
1862  hmmc->State = HAL_MMC_STATE_READY;
1863 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1864  hmmc->AbortCpltCallback(hmmc);
1865 #else
1866  HAL_MMC_AbortCallback(hmmc);
1867 #endif
1868  }
1869 #endif
1870  }
1871  else
1872  {
1873  /* Nothing to do */
1874  }
1875  }
1876 
1877 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1878  else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_IDMABTC) != RESET)
1879  {
1880  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_IT_IDMABTC);
1881  if(READ_BIT(hmmc->Instance->IDMACTRL, SDMMC_IDMA_IDMABACT) == 0U)
1882  {
1883  /* Current buffer is buffer0, Transfer complete for buffer1 */
1884  if((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1885  {
1886 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1887  hmmc->Write_DMADblBuf1CpltCallback(hmmc);
1888 #else
1890 #endif
1891  }
1892  else /* MMC_CONTEXT_READ_MULTIPLE_BLOCK */
1893  {
1894 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1895  hmmc->Read_DMADblBuf1CpltCallback(hmmc);
1896 #else
1898 #endif
1899  }
1900  }
1901  else /* MMC_DMA_BUFFER1 */
1902  {
1903  /* Current buffer is buffer1, Transfer complete for buffer0 */
1904  if((context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1905  {
1906 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1907  hmmc->Write_DMADblBuf0CpltCallback(hmmc);
1908 #else
1910 #endif
1911  }
1912  else /* MMC_CONTEXT_READ_MULTIPLE_BLOCK */
1913  {
1914 #if defined (USE_HAL_MMC_REGISTER_CALLBACKS) && (USE_HAL_MMC_REGISTER_CALLBACKS == 1U)
1915  hmmc->Read_DMADblBuf0CpltCallback(hmmc);
1916 #else
1918 #endif
1919  }
1920  }
1921  }
1922 #endif
1923 
1924  else
1925  {
1926  /* Nothing to do */
1927  }
1928 }
void HAL_MMC_AbortCallback(MMC_HandleTypeDef *hmmc)
MMC Abort callbacks.
void HAL_MMCEx_Read_DMADoubleBuffer0CpltCallback(MMC_HandleTypeDef *hmmc)
Read DMA Buffer 0 Transfer completed callbacks.
HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
Aborts the DMA Transfer in Interrupt mode.
static void MMC_Write_IT(MMC_HandleTypeDef *hmmc)
Wrap up writing in non-blocking mode.
void HAL_MMC_ErrorCallback(MMC_HandleTypeDef *hmmc)
MMC error callbacks.
void HAL_MMC_TxCpltCallback(MMC_HandleTypeDef *hmmc)
Tx Transfer completed callbacks.
void HAL_MMCEx_Write_DMADoubleBuffer0CpltCallback(MMC_HandleTypeDef *hmmc)
Write DMA Buffer 0 Transfer completed callbacks.
return HAL_OK
static void MMC_Read_IT(MMC_HandleTypeDef *hmmc)
Wrap up reading in non-blocking mode.
uint32_t SDMMC_CmdStopTransfer(SDMMC_TypeDef *SDMMCx)
Send the Stop Transfer command and check the response.
static void MMC_DMARxAbort(DMA_HandleTypeDef *hdma)
DMA MMC Rx Abort callback.
void HAL_MMCEx_Read_DMADoubleBuffer1CpltCallback(MMC_HandleTypeDef *hmmc)
Read DMA Buffer 1 Transfer completed callbacks.
void HAL_MMCEx_Write_DMADoubleBuffer1CpltCallback(MMC_HandleTypeDef *hmmc)
Write DMA Buffer 1 Transfer completed callbacks.
static void MMC_DMATxAbort(DMA_HandleTypeDef *hdma)
DMA MMC Tx Abort callback.
void HAL_MMC_RxCpltCallback(MMC_HandleTypeDef *hmmc)
Rx Transfer completed callbacks.

◆ HAL_MMC_ReadBlocks()

HAL_StatusTypeDef HAL_MMC_ReadBlocks ( MMC_HandleTypeDef hmmc,
uint8_t *  pData,
uint32_t  BlockAdd,
uint32_t  NumberOfBlocks,
uint32_t  Timeout 
)

Reads block(s) from a specified address in a card. The Data transfer is managed by polling mode.

Note
This API should be followed by a check on the card state through HAL_MMC_GetCardState().
Parameters
hmmcPointer to MMC handle
pDatapointer to the buffer that will contain the received data
BlockAddBlock Address from where data is to be read
NumberOfBlocksNumber of MMC blocks to read
TimeoutSpecify timeout value
Return values
HALstatus

Definition at line 576 of file stm32l4xx_hal_mmc.c.

577 {
578  SDMMC_DataInitTypeDef config;
579  uint32_t errorstate;
580  uint32_t tickstart = HAL_GetTick();
581  uint32_t count, data, dataremaining;
582  uint32_t add = BlockAdd;
583  uint8_t *tempbuff = pData;
584 
585  if(NULL == pData)
586  {
587  hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
588  return HAL_ERROR;
589  }
590 
591  if(hmmc->State == HAL_MMC_STATE_READY)
592  {
593  hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
594 
595  if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
596  {
597  hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
598  return HAL_ERROR;
599  }
600 
601  hmmc->State = HAL_MMC_STATE_BUSY;
602 
603  /* Initialize data control register */
604  hmmc->Instance->DCTRL = 0U;
605 
606  if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
607  {
608  add *= 512U;
609  }
610 
611  /* Set Block Size for Card */
612  errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
613  if(errorstate != HAL_MMC_ERROR_NONE)
614  {
615  /* Clear all the static flags */
616  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
617  hmmc->ErrorCode |= errorstate;
618  hmmc->State = HAL_MMC_STATE_READY;
619  return HAL_ERROR;
620  }
621 
622  /* Configure the MMC DPSM (Data Path State Machine) */
623  config.DataTimeOut = SDMMC_DATATIMEOUT;
624  config.DataLength = NumberOfBlocks * MMC_BLOCKSIZE;
625  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
626  config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
627  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
628 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
629  config.DPSM = SDMMC_DPSM_ENABLE;
630 #else
631  config.DPSM = SDMMC_DPSM_DISABLE;
632 #endif
633  (void)SDMMC_ConfigData(hmmc->Instance, &config);
634 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
635  __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
636 #endif
637 
638  /* Read block(s) in polling mode */
639  if(NumberOfBlocks > 1U)
640  {
641  hmmc->Context = MMC_CONTEXT_READ_MULTIPLE_BLOCK;
642 
643  /* Read Multi Block command */
644  errorstate = SDMMC_CmdReadMultiBlock(hmmc->Instance, add);
645  }
646  else
647  {
648  hmmc->Context = MMC_CONTEXT_READ_SINGLE_BLOCK;
649 
650  /* Read Single Block command */
651  errorstate = SDMMC_CmdReadSingleBlock(hmmc->Instance, add);
652  }
653  if(errorstate != HAL_MMC_ERROR_NONE)
654  {
655  /* Clear all the static flags */
656  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
657  hmmc->ErrorCode |= errorstate;
658  hmmc->State = HAL_MMC_STATE_READY;
659  return HAL_ERROR;
660  }
661 
662  /* Poll on SDMMC flags */
663  dataremaining = config.DataLength;
664  while(!__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
665  {
666  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXFIFOHF) && (dataremaining > 0U))
667  {
668  /* Read data from SDMMC Rx FIFO */
669  for(count = 0U; count < 8U; count++)
670  {
671  data = SDMMC_ReadFIFO(hmmc->Instance);
672  *tempbuff = (uint8_t)(data & 0xFFU);
673  tempbuff++;
674  dataremaining--;
675  *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
676  tempbuff++;
677  dataremaining--;
678  *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
679  tempbuff++;
680  dataremaining--;
681  *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
682  tempbuff++;
683  dataremaining--;
684  }
685  }
686 
687  if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
688  {
689  /* Clear all the static flags */
690  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
691  hmmc->ErrorCode |= HAL_MMC_ERROR_TIMEOUT;
692  hmmc->State= HAL_MMC_STATE_READY;
693  return HAL_TIMEOUT;
694  }
695  }
696 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
697  __SDMMC_CMDTRANS_DISABLE( hmmc->Instance);
698 #endif
699 
700  /* Send stop transmission command in case of multiblock read */
701  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
702  {
703  /* Send stop transmission command */
704  errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
705  if(errorstate != HAL_MMC_ERROR_NONE)
706  {
707  /* Clear all the static flags */
708  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
709  hmmc->ErrorCode |= errorstate;
710  hmmc->State = HAL_MMC_STATE_READY;
711  return HAL_ERROR;
712  }
713  }
714 
715  /* Get error state */
716  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DTIMEOUT))
717  {
718  /* Clear all the static flags */
719  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
720  hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
721  hmmc->State = HAL_MMC_STATE_READY;
722  return HAL_ERROR;
723  }
724  else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DCRCFAIL))
725  {
726  /* Clear all the static flags */
727  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
728  hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
729  hmmc->State = HAL_MMC_STATE_READY;
730  return HAL_ERROR;
731  }
732  else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXOVERR))
733  {
734  /* Clear all the static flags */
735  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
736  hmmc->ErrorCode |= HAL_MMC_ERROR_RX_OVERRUN;
737  hmmc->State = HAL_MMC_STATE_READY;
738  return HAL_ERROR;
739  }
740  else
741  {
742  /* Nothing to do */
743  }
744 
745 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
746  /* Empty FIFO if there is still any data */
747  while ((__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_RXDAVL)) && (dataremaining > 0U))
748  {
749  data = SDMMC_ReadFIFO(hmmc->Instance);
750  *tempbuff = (uint8_t)(data & 0xFFU);
751  tempbuff++;
752  dataremaining--;
753  *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
754  tempbuff++;
755  dataremaining--;
756  *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
757  tempbuff++;
758  dataremaining--;
759  *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
760  tempbuff++;
761  dataremaining--;
762 
763  if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
764  {
765  /* Clear all the static flags */
766  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
767  hmmc->ErrorCode |= HAL_MMC_ERROR_TIMEOUT;
768  hmmc->State= HAL_MMC_STATE_READY;
769  return HAL_ERROR;
770  }
771  }
772 #endif
773 
774  /* Clear all the static flags */
775  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
776 
777  hmmc->State = HAL_MMC_STATE_READY;
778 
779  return HAL_OK;
780  }
781  else
782  {
783  hmmc->ErrorCode |= HAL_MMC_ERROR_BUSY;
784  return HAL_ERROR;
785  }
786 }
uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef *SDMMCx, uint32_t BlockSize)
Send the Data Block Lenght command and check the response.
uint32_t SDMMC_CmdReadSingleBlock(SDMMC_TypeDef *SDMMCx, uint32_t ReadAdd)
Send the Read Single Block command and check the response.
SDMMC Data Control structure.
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
return HAL_OK
uint32_t SDMMC_ReadFIFO(SDMMC_TypeDef *SDMMCx)
Read data (word) from Rx FIFO in blocking mode (polling)
uint32_t SDMMC_CmdStopTransfer(SDMMC_TypeDef *SDMMCx)
Send the Stop Transfer command and check the response.
uint32_t SDMMC_CmdReadMultiBlock(SDMMC_TypeDef *SDMMCx, uint32_t ReadAdd)
Send the Read Multi Block command and check the response.
HAL_StatusTypeDef SDMMC_ConfigData(SDMMC_TypeDef *SDMMCx, SDMMC_DataInitTypeDef *Data)
Configure the SDMMC data path according to the specified parameters in the SDMMC_DataInitTypeDef.

◆ HAL_MMC_ReadBlocks_DMA()

HAL_StatusTypeDef HAL_MMC_ReadBlocks_DMA ( MMC_HandleTypeDef hmmc,
uint8_t *  pData,
uint32_t  BlockAdd,
uint32_t  NumberOfBlocks 
)

Reads block(s) from a specified address in a card. The Data transfer is managed by DMA mode.

Note
This API should be followed by a check on the card state through HAL_MMC_GetCardState().
You could also check the DMA transfer process through the MMC Rx interrupt event.
Parameters
hmmcPointer MMC handle
pDataPointer to the buffer that will contain the received data
BlockAddBlock Address from where data is to be read
NumberOfBlocksNumber of blocks to read.
Return values
HALstatus

Definition at line 1229 of file stm32l4xx_hal_mmc.c.

1230 {
1231  SDMMC_DataInitTypeDef config;
1232  uint32_t errorstate;
1233  uint32_t add = BlockAdd;
1234 
1235  if(NULL == pData)
1236  {
1237  hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
1238  return HAL_ERROR;
1239  }
1240 
1241  if(hmmc->State == HAL_MMC_STATE_READY)
1242  {
1243  hmmc->ErrorCode = HAL_DMA_ERROR_NONE;
1244 
1245  if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
1246  {
1247  hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
1248  return HAL_ERROR;
1249  }
1250 
1251  hmmc->State = HAL_MMC_STATE_BUSY;
1252 
1253  /* Initialize data control register */
1254  hmmc->Instance->DCTRL = 0U;
1255 
1256 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1257  __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1258 
1259  /* Set the DMA transfer complete callback */
1260  hmmc->hdmarx->XferCpltCallback = MMC_DMAReceiveCplt;
1261 
1262  /* Set the DMA error callback */
1263  hmmc->hdmarx->XferErrorCallback = MMC_DMAError;
1264 
1265  /* Set the DMA Abort callback */
1266  hmmc->hdmarx->XferAbortCallback = NULL;
1267 
1268 #else
1269  hmmc->pRxBuffPtr = pData;
1270  hmmc->RxXferSize = MMC_BLOCKSIZE * NumberOfBlocks;
1271 #endif
1272 
1273  if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
1274  {
1275  add *= 512U;
1276  }
1277 
1278  /* Set Block Size for Card */
1279  errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
1280  if(errorstate != HAL_MMC_ERROR_NONE)
1281  {
1282  /* Clear all the static flags */
1283  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1284  hmmc->ErrorCode = errorstate;
1285  hmmc->State = HAL_MMC_STATE_READY;
1286  return HAL_ERROR;
1287  }
1288 
1289 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1290  /* Configure the MMC DPSM (Data Path State Machine) */
1291  config.DataTimeOut = SDMMC_DATATIMEOUT;
1292  config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
1293  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1294  config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1295  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1296  config.DPSM = SDMMC_DPSM_DISABLE;
1297  (void)SDMMC_ConfigData(hmmc->Instance, &config);
1298 
1299  /* Enable transfer interrupts */
1300  __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1301 
1302  __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
1303  hmmc->Instance->IDMABASE0 = (uint32_t) pData ;
1304  hmmc->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
1305 #else
1306  /* Enable the DMA Channel */
1307  if(HAL_DMA_Start_IT(hmmc->hdmarx, (uint32_t)&hmmc->Instance->FIFO, (uint32_t)pData, (uint32_t)(MMC_BLOCKSIZE * NumberOfBlocks)/4) != HAL_OK)
1308  {
1309  __HAL_MMC_DISABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1310  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1311  hmmc->ErrorCode = HAL_MMC_ERROR_DMA;
1312  hmmc->State = HAL_MMC_STATE_READY;
1313  return HAL_ERROR;
1314  }
1315  else
1316  {
1317  /* Enable MMC DMA transfer */
1318  __HAL_MMC_DMA_ENABLE(hmmc);
1319 
1320  /* Configure the MMC DPSM (Data Path State Machine) */
1321  config.DataTimeOut = SDMMC_DATATIMEOUT;
1322  config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
1323  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1324  config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1325  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1326  config.DPSM = SDMMC_DPSM_ENABLE;
1327  (void)SDMMC_ConfigData(hmmc->Instance, &config);
1328 #endif
1329 
1330  /* Read Blocks in DMA mode */
1331  if(NumberOfBlocks > 1U)
1332  {
1333  hmmc->Context = (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_DMA);
1334 
1335  /* Read Multi Block command */
1336  errorstate = SDMMC_CmdReadMultiBlock(hmmc->Instance, add);
1337  }
1338  else
1339  {
1340  hmmc->Context = (MMC_CONTEXT_READ_SINGLE_BLOCK | MMC_CONTEXT_DMA);
1341 
1342  /* Read Single Block command */
1343  errorstate = SDMMC_CmdReadSingleBlock(hmmc->Instance, add);
1344  }
1345  if(errorstate != HAL_MMC_ERROR_NONE)
1346  {
1347  /* Clear all the static flags */
1348  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1349  __HAL_MMC_DISABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1350  hmmc->ErrorCode = errorstate;
1351  hmmc->State = HAL_MMC_STATE_READY;
1352  return HAL_ERROR;
1353  }
1354 
1355  return HAL_OK;
1356 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1357  }
1358 #endif
1359  }
1360  else
1361  {
1362  return HAL_BUSY;
1363  }
1364 }
uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef *SDMMCx, uint32_t BlockSize)
Send the Data Block Lenght command and check the response.
uint32_t SDMMC_CmdReadSingleBlock(SDMMC_TypeDef *SDMMCx, uint32_t ReadAdd)
Send the Read Single Block command and check the response.
SDMMC Data Control structure.
static void MMC_DMAError(DMA_HandleTypeDef *hdma)
DMA MMC communication error callback.
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
Start the DMA Transfer with interrupt enabled.
static void MMC_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
DMA MMC receive process complete callback.
return HAL_OK
uint32_t SDMMC_CmdReadMultiBlock(SDMMC_TypeDef *SDMMCx, uint32_t ReadAdd)
Send the Read Multi Block command and check the response.
HAL_StatusTypeDef SDMMC_ConfigData(SDMMC_TypeDef *SDMMCx, SDMMC_DataInitTypeDef *Data)
Configure the SDMMC data path according to the specified parameters in the SDMMC_DataInitTypeDef.

◆ HAL_MMC_ReadBlocks_IT()

HAL_StatusTypeDef HAL_MMC_ReadBlocks_IT ( MMC_HandleTypeDef hmmc,
uint8_t *  pData,
uint32_t  BlockAdd,
uint32_t  NumberOfBlocks 
)

Reads block(s) from a specified address in a card. The Data transfer is managed in interrupt mode.

Note
This API should be followed by a check on the card state through HAL_MMC_GetCardState().
You could also check the IT transfer process through the MMC Rx interrupt event.
Parameters
hmmcPointer to MMC handle
pDataPointer to the buffer that will contain the received data
BlockAddBlock Address from where data is to be read
NumberOfBlocksNumber of blocks to read.
Return values
HALstatus

Definition at line 1002 of file stm32l4xx_hal_mmc.c.

1003 {
1004  SDMMC_DataInitTypeDef config;
1005  uint32_t errorstate;
1006  uint32_t add = BlockAdd;
1007 
1008  if(NULL == pData)
1009  {
1010  hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
1011  return HAL_ERROR;
1012  }
1013 
1014  if(hmmc->State == HAL_MMC_STATE_READY)
1015  {
1016  hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
1017 
1018  if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
1019  {
1020  hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
1021  return HAL_ERROR;
1022  }
1023 
1024  hmmc->State = HAL_MMC_STATE_BUSY;
1025 
1026  /* Initialize data control register */
1027  hmmc->Instance->DCTRL = 0U;
1028 
1029  hmmc->pRxBuffPtr = pData;
1030  hmmc->RxXferSize = MMC_BLOCKSIZE * NumberOfBlocks;
1031 
1032  __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_FLAG_RXFIFOHF));
1033 
1034  if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
1035  {
1036  add *= 512U;
1037  }
1038 
1039  /* Set Block Size for Card */
1040  errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
1041  if(errorstate != HAL_MMC_ERROR_NONE)
1042  {
1043  /* Clear all the static flags */
1044  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1045  hmmc->ErrorCode |= errorstate;
1046  hmmc->State = HAL_MMC_STATE_READY;
1047  return HAL_ERROR;
1048  }
1049 
1050  /* Configure the MMC DPSM (Data Path State Machine) */
1051  config.DataTimeOut = SDMMC_DATATIMEOUT;
1052  config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
1053  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1054  config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1055  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1056 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1057  config.DPSM = SDMMC_DPSM_ENABLE;
1058 #else
1059  config.DPSM = SDMMC_DPSM_DISABLE;
1060 #endif
1061  (void)SDMMC_ConfigData(hmmc->Instance, &config);
1062 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1063  __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
1064 #endif
1065  /* Read Blocks in IT mode */
1066  if(NumberOfBlocks > 1U)
1067  {
1068  hmmc->Context = (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_IT);
1069 
1070  /* Read Multi Block command */
1071  errorstate = SDMMC_CmdReadMultiBlock(hmmc->Instance, add);
1072  }
1073  else
1074  {
1075  hmmc->Context = (MMC_CONTEXT_READ_SINGLE_BLOCK | MMC_CONTEXT_IT);
1076 
1077  /* Read Single Block command */
1078  errorstate = SDMMC_CmdReadSingleBlock(hmmc->Instance, add);
1079  }
1080 
1081  if(errorstate != HAL_MMC_ERROR_NONE)
1082  {
1083  /* Clear all the static flags */
1084  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1085  hmmc->ErrorCode |= errorstate;
1086  hmmc->State = HAL_MMC_STATE_READY;
1087  return HAL_ERROR;
1088  }
1089 
1090  return HAL_OK;
1091  }
1092  else
1093  {
1094  return HAL_BUSY;
1095  }
1096 }
uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef *SDMMCx, uint32_t BlockSize)
Send the Data Block Lenght command and check the response.
uint32_t SDMMC_CmdReadSingleBlock(SDMMC_TypeDef *SDMMCx, uint32_t ReadAdd)
Send the Read Single Block command and check the response.
SDMMC Data Control structure.
return HAL_OK
uint32_t SDMMC_CmdReadMultiBlock(SDMMC_TypeDef *SDMMCx, uint32_t ReadAdd)
Send the Read Multi Block command and check the response.
HAL_StatusTypeDef SDMMC_ConfigData(SDMMC_TypeDef *SDMMCx, SDMMC_DataInitTypeDef *Data)
Configure the SDMMC data path according to the specified parameters in the SDMMC_DataInitTypeDef.

◆ HAL_MMC_RegisterCallback()

HAL_StatusTypeDef HAL_MMC_RegisterCallback ( MMC_HandleTypeDef hmmc,
HAL_MMC_CallbackIDTypeDef  CallbackId,
pMMC_CallbackTypeDef  pCallback 
)

Register a User MMC Callback To be used instead of the weak (surcharged) predefined callback.

Parameters
hmmc: MMC handle
CallbackId: ID of the callback to be registered This parameter can be one of the following values:
pCallback: pointer to the Callback function
Return values
status

Definition at line 2031 of file stm32l4xx_hal_mmc.c.

2032 {
2033  HAL_StatusTypeDef status = HAL_OK;
2034 
2035  if(pCallback == NULL)
2036  {
2037  /* Update the error code */
2038  hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
2039  return HAL_ERROR;
2040  }
2041 
2042  /* Process locked */
2043  __HAL_LOCK(hmmc);
2044 
2045  if(hmmc->State == HAL_MMC_STATE_READY)
2046  {
2047  switch (CallbackId)
2048  {
2049  case HAL_MMC_TX_CPLT_CB_ID :
2050  hmmc->TxCpltCallback = pCallback;
2051  break;
2052  case HAL_MMC_RX_CPLT_CB_ID :
2053  hmmc->RxCpltCallback = pCallback;
2054  break;
2055  case HAL_MMC_ERROR_CB_ID :
2056  hmmc->ErrorCallback = pCallback;
2057  break;
2058  case HAL_MMC_ABORT_CB_ID :
2059  hmmc->AbortCpltCallback = pCallback;
2060  break;
2061 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
2063  hmmc->Read_DMADblBuf0CpltCallback = pCallback;
2064  break;
2066  hmmc->Read_DMADblBuf1CpltCallback = pCallback;
2067  break;
2069  hmmc->Write_DMADblBuf0CpltCallback = pCallback;
2070  break;
2072  hmmc->Write_DMADblBuf1CpltCallback = pCallback;
2073  break;
2074 #endif
2075  case HAL_MMC_MSP_INIT_CB_ID :
2076  hmmc->MspInitCallback = pCallback;
2077  break;
2079  hmmc->MspDeInitCallback = pCallback;
2080  break;
2081  default :
2082  /* Update the error code */
2083  hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
2084  /* update return status */
2085  status = HAL_ERROR;
2086  break;
2087  }
2088  }
2089  else if (hmmc->State == HAL_MMC_STATE_RESET)
2090  {
2091  switch (CallbackId)
2092  {
2093  case HAL_MMC_MSP_INIT_CB_ID :
2094  hmmc->MspInitCallback = pCallback;
2095  break;
2097  hmmc->MspDeInitCallback = pCallback;
2098  break;
2099  default :
2100  /* Update the error code */
2101  hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
2102  /* update return status */
2103  status = HAL_ERROR;
2104  break;
2105  }
2106  }
2107  else
2108  {
2109  /* Update the error code */
2110  hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
2111  /* update return status */
2112  status = HAL_ERROR;
2113  }
2114 
2115  /* Release Lock */
2116  __HAL_UNLOCK(hmmc);
2117  return status;
2118 }
__HAL_UNLOCK(hrtc)
__HAL_LOCK(hrtc)
return HAL_OK

◆ HAL_MMC_RxCpltCallback()

__weak void HAL_MMC_RxCpltCallback ( MMC_HandleTypeDef hmmc)

Rx Transfer completed callbacks.

Parameters
hmmcPointer MMC handle
Return values
None

Definition at line 1971 of file stm32l4xx_hal_mmc.c.

1972 {
1973  /* Prevent unused argument(s) compilation warning */
1974  UNUSED(hmmc);
1975 
1976  /* NOTE : This function should not be modified, when the callback is needed,
1977  the HAL_MMC_RxCpltCallback can be implemented in the user file
1978  */
1979 }

◆ HAL_MMC_TxCpltCallback()

__weak void HAL_MMC_TxCpltCallback ( MMC_HandleTypeDef hmmc)

Tx Transfer completed callbacks.

Parameters
hmmcPointer to MMC handle
Return values
None

Definition at line 1956 of file stm32l4xx_hal_mmc.c.

1957 {
1958  /* Prevent unused argument(s) compilation warning */
1959  UNUSED(hmmc);
1960 
1961  /* NOTE : This function should not be modified, when the callback is needed,
1962  the HAL_MMC_TxCpltCallback can be implemented in the user file
1963  */
1964 }

◆ HAL_MMC_UnRegisterCallback()

HAL_StatusTypeDef HAL_MMC_UnRegisterCallback ( MMC_HandleTypeDef hmmc,
HAL_MMC_CallbackIDTypeDef  CallbackId 
)

Unregister a User MMC Callback MMC Callback is redirected to the weak (surcharged) predefined callback.

Parameters
hmmc: MMC handle
CallbackId: ID of the callback to be unregistered This parameter can be one of the following values:
Return values
status

Definition at line 2138 of file stm32l4xx_hal_mmc.c.

2139 {
2140  HAL_StatusTypeDef status = HAL_OK;
2141 
2142  /* Process locked */
2143  __HAL_LOCK(hmmc);
2144 
2145  if(hmmc->State == HAL_MMC_STATE_READY)
2146  {
2147  switch (CallbackId)
2148  {
2149  case HAL_MMC_TX_CPLT_CB_ID :
2150  hmmc->TxCpltCallback = HAL_MMC_TxCpltCallback;
2151  break;
2152  case HAL_MMC_RX_CPLT_CB_ID :
2153  hmmc->RxCpltCallback = HAL_MMC_RxCpltCallback;
2154  break;
2155  case HAL_MMC_ERROR_CB_ID :
2156  hmmc->ErrorCallback = HAL_MMC_ErrorCallback;
2157  break;
2158  case HAL_MMC_ABORT_CB_ID :
2159  hmmc->AbortCpltCallback = HAL_MMC_AbortCallback;
2160  break;
2161 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
2163  hmmc->Read_DMADblBuf0CpltCallback = HAL_MMCEx_Read_DMADoubleBuffer0CpltCallback;
2164  break;
2166  hmmc->Read_DMADblBuf1CpltCallback = HAL_MMCEx_Read_DMADoubleBuffer1CpltCallback;
2167  break;
2169  hmmc->Write_DMADblBuf0CpltCallback = HAL_MMCEx_Write_DMADoubleBuffer0CpltCallback;
2170  break;
2172  hmmc->Write_DMADblBuf1CpltCallback = HAL_MMCEx_Write_DMADoubleBuffer1CpltCallback;
2173  break;
2174 #endif
2175  case HAL_MMC_MSP_INIT_CB_ID :
2176  hmmc->MspInitCallback = HAL_MMC_MspInit;
2177  break;
2179  hmmc->MspDeInitCallback = HAL_MMC_MspDeInit;
2180  break;
2181  default :
2182  /* Update the error code */
2183  hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
2184  /* update return status */
2185  status = HAL_ERROR;
2186  break;
2187  }
2188  }
2189  else if (hmmc->State == HAL_MMC_STATE_RESET)
2190  {
2191  switch (CallbackId)
2192  {
2193  case HAL_MMC_MSP_INIT_CB_ID :
2194  hmmc->MspInitCallback = HAL_MMC_MspInit;
2195  break;
2197  hmmc->MspDeInitCallback = HAL_MMC_MspDeInit;
2198  break;
2199  default :
2200  /* Update the error code */
2201  hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
2202  /* update return status */
2203  status = HAL_ERROR;
2204  break;
2205  }
2206  }
2207  else
2208  {
2209  /* Update the error code */
2210  hmmc->ErrorCode |= HAL_MMC_ERROR_INVALID_CALLBACK;
2211  /* update return status */
2212  status = HAL_ERROR;
2213  }
2214 
2215  /* Release Lock */
2216  __HAL_UNLOCK(hmmc);
2217  return status;
2218 }
void HAL_MMC_AbortCallback(MMC_HandleTypeDef *hmmc)
MMC Abort callbacks.
void HAL_MMCEx_Read_DMADoubleBuffer0CpltCallback(MMC_HandleTypeDef *hmmc)
Read DMA Buffer 0 Transfer completed callbacks.
void HAL_MMC_MspInit(MMC_HandleTypeDef *hmmc)
Initializes the MMC MSP.
__HAL_UNLOCK(hrtc)
void HAL_MMC_ErrorCallback(MMC_HandleTypeDef *hmmc)
MMC error callbacks.
__HAL_LOCK(hrtc)
void HAL_MMC_TxCpltCallback(MMC_HandleTypeDef *hmmc)
Tx Transfer completed callbacks.
void HAL_MMCEx_Write_DMADoubleBuffer0CpltCallback(MMC_HandleTypeDef *hmmc)
Write DMA Buffer 0 Transfer completed callbacks.
return HAL_OK
void HAL_MMCEx_Read_DMADoubleBuffer1CpltCallback(MMC_HandleTypeDef *hmmc)
Read DMA Buffer 1 Transfer completed callbacks.
void HAL_MMC_MspDeInit(MMC_HandleTypeDef *hmmc)
De-Initialize MMC MSP.
void HAL_MMCEx_Write_DMADoubleBuffer1CpltCallback(MMC_HandleTypeDef *hmmc)
Write DMA Buffer 1 Transfer completed callbacks.
void HAL_MMC_RxCpltCallback(MMC_HandleTypeDef *hmmc)
Rx Transfer completed callbacks.

◆ HAL_MMC_WriteBlocks()

HAL_StatusTypeDef HAL_MMC_WriteBlocks ( MMC_HandleTypeDef hmmc,
uint8_t *  pData,
uint32_t  BlockAdd,
uint32_t  NumberOfBlocks,
uint32_t  Timeout 
)

Allows to write block(s) to a specified address in a card. The Data transfer is managed by polling mode.

Note
This API should be followed by a check on the card state through HAL_MMC_GetCardState().
Parameters
hmmcPointer to MMC handle
pDatapointer to the buffer that will contain the data to transmit
BlockAddBlock Address where data will be written
NumberOfBlocksNumber of MMC blocks to write
TimeoutSpecify timeout value
Return values
HALstatus

Definition at line 800 of file stm32l4xx_hal_mmc.c.

801 {
802  SDMMC_DataInitTypeDef config;
803  uint32_t errorstate;
804  uint32_t tickstart = HAL_GetTick();
805  uint32_t count, data, dataremaining;
806  uint32_t add = BlockAdd;
807  uint8_t *tempbuff = pData;
808 
809  if(NULL == pData)
810  {
811  hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
812  return HAL_ERROR;
813  }
814 
815  if(hmmc->State == HAL_MMC_STATE_READY)
816  {
817  hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
818 
819  if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
820  {
821  hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
822  return HAL_ERROR;
823  }
824 
825  hmmc->State = HAL_MMC_STATE_BUSY;
826 
827  /* Initialize data control register */
828  hmmc->Instance->DCTRL = 0U;
829 
830  if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
831  {
832  add *= 512U;
833  }
834 
835  /* Set Block Size for Card */
836  errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
837  if(errorstate != HAL_MMC_ERROR_NONE)
838  {
839  /* Clear all the static flags */
840  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
841  hmmc->ErrorCode |= errorstate;
842  hmmc->State = HAL_MMC_STATE_READY;
843  return HAL_ERROR;
844  }
845 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
846  /* Configure the MMC DPSM (Data Path State Machine) */
847  config.DataTimeOut = SDMMC_DATATIMEOUT;
848  config.DataLength = NumberOfBlocks * MMC_BLOCKSIZE;
849  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
850  config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
851  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
852  config.DPSM = SDMMC_DPSM_DISABLE;
853  (void)SDMMC_ConfigData(hmmc->Instance, &config);
854  __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
855 #endif
856 
857  /* Write Blocks in Polling mode */
858  if(NumberOfBlocks > 1U)
859  {
860  hmmc->Context = MMC_CONTEXT_WRITE_MULTIPLE_BLOCK;
861 
862  /* Write Multi Block command */
863  errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, add);
864  }
865  else
866  {
867  hmmc->Context = MMC_CONTEXT_WRITE_SINGLE_BLOCK;
868 
869  /* Write Single Block command */
870  errorstate = SDMMC_CmdWriteSingleBlock(hmmc->Instance, add);
871  }
872  if(errorstate != HAL_MMC_ERROR_NONE)
873  {
874  /* Clear all the static flags */
875  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
876  hmmc->ErrorCode |= errorstate;
877  hmmc->State = HAL_MMC_STATE_READY;
878  return HAL_ERROR;
879  }
880 
881 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
882  /* Configure the MMC DPSM (Data Path State Machine) */
883  config.DataTimeOut = SDMMC_DATATIMEOUT;
884  config.DataLength = NumberOfBlocks * MMC_BLOCKSIZE;
885  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
886  config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
887  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
888  config.DPSM = SDMMC_DPSM_ENABLE;
889  (void)SDMMC_ConfigData(hmmc->Instance, &config);
890 #endif
891 
892  /* Write block(s) in polling mode */
893  dataremaining = config.DataLength;
894  while(!__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
895  {
896  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_TXFIFOHE) && (dataremaining > 0U))
897  {
898  /* Write data to SDMMC Tx FIFO */
899  for(count = 0U; count < 8U; count++)
900  {
901  data = (uint32_t)(*tempbuff);
902  tempbuff++;
903  dataremaining--;
904  data |= ((uint32_t)(*tempbuff) << 8U);
905  tempbuff++;
906  dataremaining--;
907  data |= ((uint32_t)(*tempbuff) << 16U);
908  tempbuff++;
909  dataremaining--;
910  data |= ((uint32_t)(*tempbuff) << 24U);
911  tempbuff++;
912  dataremaining--;
913  (void)SDMMC_WriteFIFO(hmmc->Instance, &data);
914  }
915  }
916 
917  if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
918  {
919  /* Clear all the static flags */
920  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
921  hmmc->ErrorCode |= errorstate;
922  hmmc->State = HAL_MMC_STATE_READY;
923  return HAL_TIMEOUT;
924  }
925  }
926 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
927  __SDMMC_CMDTRANS_DISABLE( hmmc->Instance);
928 #endif
929 
930  /* Send stop transmission command in case of multiblock write */
931  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
932  {
933  /* Send stop transmission command */
934  errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
935  if(errorstate != HAL_MMC_ERROR_NONE)
936  {
937  /* Clear all the static flags */
938  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
939  hmmc->ErrorCode |= errorstate;
940  hmmc->State = HAL_MMC_STATE_READY;
941  return HAL_ERROR;
942  }
943  }
944 
945  /* Get error state */
946  if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DTIMEOUT))
947  {
948  /* Clear all the static flags */
949  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
950  hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
951  hmmc->State = HAL_MMC_STATE_READY;
952  return HAL_ERROR;
953  }
954  else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_DCRCFAIL))
955  {
956  /* Clear all the static flags */
957  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
958  hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
959  hmmc->State = HAL_MMC_STATE_READY;
960  return HAL_ERROR;
961  }
962  else if(__HAL_MMC_GET_FLAG(hmmc, SDMMC_FLAG_TXUNDERR))
963  {
964  /* Clear all the static flags */
965  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
966  hmmc->ErrorCode |= HAL_MMC_ERROR_TX_UNDERRUN;
967  hmmc->State = HAL_MMC_STATE_READY;
968  return HAL_ERROR;
969  }
970  else
971  {
972  /* Nothing to do */
973  }
974 
975  /* Clear all the static flags */
976  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_DATA_FLAGS);
977 
978  hmmc->State = HAL_MMC_STATE_READY;
979 
980  return HAL_OK;
981  }
982  else
983  {
984  hmmc->ErrorCode |= HAL_MMC_ERROR_BUSY;
985  return HAL_ERROR;
986  }
987 }
uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef *SDMMCx, uint32_t BlockSize)
Send the Data Block Lenght command and check the response.
uint32_t SDMMC_CmdWriteMultiBlock(SDMMC_TypeDef *SDMMCx, uint32_t WriteAdd)
Send the Write Multi Block command and check the response.
SDMMC Data Control structure.
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
return HAL_OK
uint32_t SDMMC_CmdWriteSingleBlock(SDMMC_TypeDef *SDMMCx, uint32_t WriteAdd)
Send the Write Single Block command and check the response.
uint32_t SDMMC_CmdStopTransfer(SDMMC_TypeDef *SDMMCx)
Send the Stop Transfer command and check the response.
HAL_StatusTypeDef SDMMC_WriteFIFO(SDMMC_TypeDef *SDMMCx, uint32_t *pWriteData)
Write data (word) to Tx FIFO in blocking mode (polling)
HAL_StatusTypeDef SDMMC_ConfigData(SDMMC_TypeDef *SDMMCx, SDMMC_DataInitTypeDef *Data)
Configure the SDMMC data path according to the specified parameters in the SDMMC_DataInitTypeDef.

◆ HAL_MMC_WriteBlocks_DMA()

HAL_StatusTypeDef HAL_MMC_WriteBlocks_DMA ( MMC_HandleTypeDef hmmc,
uint8_t *  pData,
uint32_t  BlockAdd,
uint32_t  NumberOfBlocks 
)

Writes block(s) to a specified address in a card. The Data transfer is managed by DMA mode.

Note
This API should be followed by a check on the card state through HAL_MMC_GetCardState().
You could also check the DMA transfer process through the MMC Tx interrupt event.
Parameters
hmmcPointer to MMC handle
pDataPointer to the buffer that will contain the data to transmit
BlockAddBlock Address where data will be written
NumberOfBlocksNumber of blocks to write
Return values
HALstatus

Definition at line 1379 of file stm32l4xx_hal_mmc.c.

1380 {
1381  SDMMC_DataInitTypeDef config;
1382  uint32_t errorstate;
1383  uint32_t add = BlockAdd;
1384 
1385  if(NULL == pData)
1386  {
1387  hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
1388  return HAL_ERROR;
1389  }
1390 
1391  if(hmmc->State == HAL_MMC_STATE_READY)
1392  {
1393  hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
1394 
1395  if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
1396  {
1397  hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
1398  return HAL_ERROR;
1399  }
1400 
1401  hmmc->State = HAL_MMC_STATE_BUSY;
1402 
1403  /* Initialize data control register */
1404  hmmc->Instance->DCTRL = 0U;
1405 
1406 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1407  /* Enable MMC Error interrupts */
1408  __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR));
1409 
1410  /* Set the DMA transfer complete callback */
1411  hmmc->hdmatx->XferCpltCallback = MMC_DMATransmitCplt;
1412 
1413  /* Set the DMA error callback */
1414  hmmc->hdmatx->XferErrorCallback = MMC_DMAError;
1415 
1416  /* Set the DMA Abort callback */
1417  hmmc->hdmatx->XferAbortCallback = NULL;
1418 #else
1419  hmmc->pTxBuffPtr = pData;
1420  hmmc->TxXferSize = MMC_BLOCKSIZE * NumberOfBlocks;
1421 #endif
1422 
1423  if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
1424  {
1425  add *= 512U;
1426  }
1427 
1428  /* Set Block Size for Card */
1429  errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
1430  if(errorstate != HAL_MMC_ERROR_NONE)
1431  {
1432  /* Clear all the static flags */
1433  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1434  hmmc->ErrorCode |= errorstate;
1435  hmmc->State = HAL_MMC_STATE_READY;
1436  return HAL_ERROR;
1437  }
1438 
1439 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1440  /* Configure the MMC DPSM (Data Path State Machine) */
1441  config.DataTimeOut = SDMMC_DATATIMEOUT;
1442  config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
1443  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1444  config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1445  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1446  config.DPSM = SDMMC_DPSM_DISABLE;
1447  (void)SDMMC_ConfigData(hmmc->Instance, &config);
1448 
1449  /* Enable transfer interrupts */
1450  __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND));
1451 
1452  __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
1453 
1454  hmmc->Instance->IDMABASE0 = (uint32_t) pData ;
1455  hmmc->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
1456 #endif
1457 
1458  /* Write Blocks in Polling mode */
1459  if(NumberOfBlocks > 1U)
1460  {
1461  hmmc->Context = (MMC_CONTEXT_WRITE_MULTIPLE_BLOCK | MMC_CONTEXT_DMA);
1462 
1463  /* Write Multi Block command */
1464  errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, add);
1465  }
1466  else
1467  {
1468  hmmc->Context = (MMC_CONTEXT_WRITE_SINGLE_BLOCK | MMC_CONTEXT_DMA);
1469 
1470  /* Write Single Block command */
1471  errorstate = SDMMC_CmdWriteSingleBlock(hmmc->Instance, add);
1472  }
1473  if(errorstate != HAL_MMC_ERROR_NONE)
1474  {
1475  /* Clear all the static flags */
1476  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1477  __HAL_MMC_DISABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND));
1478  hmmc->ErrorCode |= errorstate;
1479  hmmc->State = HAL_MMC_STATE_READY;
1480  return HAL_ERROR;
1481  }
1482 
1483 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1484  /* Enable SDMMC DMA transfer */
1485  __HAL_MMC_DMA_ENABLE(hmmc);
1486 
1487  /* Enable the DMA Channel */
1488  if(HAL_DMA_Start_IT(hmmc->hdmatx, (uint32_t)pData, (uint32_t)&hmmc->Instance->FIFO, (uint32_t)(MMC_BLOCKSIZE * NumberOfBlocks)/4) != HAL_OK)
1489  {
1490  __HAL_MMC_DISABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND));
1491  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1492  hmmc->ErrorCode |= HAL_MMC_ERROR_DMA;
1493  hmmc->State = HAL_MMC_STATE_READY;
1494  return HAL_ERROR;
1495  }
1496  else
1497  {
1498  /* Configure the MMC DPSM (Data Path State Machine) */
1499  config.DataTimeOut = SDMMC_DATATIMEOUT;
1500  config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
1501  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1502  config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1503  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1504  config.DPSM = SDMMC_DPSM_ENABLE;
1505  (void)SDMMC_ConfigData(hmmc->Instance, &config);
1506 
1507  return HAL_OK;
1508  }
1509 #else
1510  return HAL_OK;
1511 #endif
1512  }
1513  else
1514  {
1515  return HAL_BUSY;
1516  }
1517 }
uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef *SDMMCx, uint32_t BlockSize)
Send the Data Block Lenght command and check the response.
uint32_t SDMMC_CmdWriteMultiBlock(SDMMC_TypeDef *SDMMCx, uint32_t WriteAdd)
Send the Write Multi Block command and check the response.
SDMMC Data Control structure.
static void MMC_DMAError(DMA_HandleTypeDef *hdma)
DMA MMC communication error callback.
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
Start the DMA Transfer with interrupt enabled.
return HAL_OK
static void MMC_DMATransmitCplt(DMA_HandleTypeDef *hdma)
DMA MMC transmit process complete callback.
uint32_t SDMMC_CmdWriteSingleBlock(SDMMC_TypeDef *SDMMCx, uint32_t WriteAdd)
Send the Write Single Block command and check the response.
HAL_StatusTypeDef SDMMC_ConfigData(SDMMC_TypeDef *SDMMCx, SDMMC_DataInitTypeDef *Data)
Configure the SDMMC data path according to the specified parameters in the SDMMC_DataInitTypeDef.

◆ HAL_MMC_WriteBlocks_IT()

HAL_StatusTypeDef HAL_MMC_WriteBlocks_IT ( MMC_HandleTypeDef hmmc,
uint8_t *  pData,
uint32_t  BlockAdd,
uint32_t  NumberOfBlocks 
)

Writes block(s) to a specified address in a card. The Data transfer is managed in interrupt mode.

Note
This API should be followed by a check on the card state through HAL_MMC_GetCardState().
You could also check the IT transfer process through the MMC Tx interrupt event.
Parameters
hmmcPointer to MMC handle
pDataPointer to the buffer that will contain the data to transmit
BlockAddBlock Address where data will be written
NumberOfBlocksNumber of blocks to write
Return values
HALstatus

Definition at line 1111 of file stm32l4xx_hal_mmc.c.

1112 {
1113  SDMMC_DataInitTypeDef config;
1114  uint32_t errorstate;
1115  uint32_t add = BlockAdd;
1116 
1117  if(NULL == pData)
1118  {
1119  hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
1120  return HAL_ERROR;
1121  }
1122 
1123  if(hmmc->State == HAL_MMC_STATE_READY)
1124  {
1125  hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
1126 
1127  if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
1128  {
1129  hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
1130  return HAL_ERROR;
1131  }
1132 
1133  hmmc->State = HAL_MMC_STATE_BUSY;
1134 
1135  /* Initialize data control register */
1136  hmmc->Instance->DCTRL = 0U;
1137 
1138  hmmc->pTxBuffPtr = pData;
1139  hmmc->TxXferSize = MMC_BLOCKSIZE * NumberOfBlocks;
1140 
1141  /* Enable transfer interrupts */
1142  __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_FLAG_TXFIFOHE));
1143 
1144  if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
1145  {
1146  add *= 512U;
1147  }
1148 
1149  /* Set Block Size for Card */
1150  errorstate = SDMMC_CmdBlockLength(hmmc->Instance, MMC_BLOCKSIZE);
1151  if(errorstate != HAL_MMC_ERROR_NONE)
1152  {
1153  /* Clear all the static flags */
1154  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1155  hmmc->ErrorCode |= errorstate;
1156  hmmc->State = HAL_MMC_STATE_READY;
1157  return HAL_ERROR;
1158  }
1159 
1160 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1161  /* Configure the MMC DPSM (Data Path State Machine) */
1162  config.DataTimeOut = SDMMC_DATATIMEOUT;
1163  config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
1164  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1165  config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1166  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1167  config.DPSM = SDMMC_DPSM_DISABLE;
1168  (void)SDMMC_ConfigData(hmmc->Instance, &config);
1169 
1170  __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
1171 #endif
1172 
1173  /* Write Blocks in Polling mode */
1174  if(NumberOfBlocks > 1U)
1175  {
1176  hmmc->Context = (MMC_CONTEXT_WRITE_MULTIPLE_BLOCK| MMC_CONTEXT_IT);
1177 
1178  /* Write Multi Block command */
1179  errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, add);
1180  }
1181  else
1182  {
1183  hmmc->Context = (MMC_CONTEXT_WRITE_SINGLE_BLOCK | MMC_CONTEXT_IT);
1184 
1185  /* Write Single Block command */
1186  errorstate = SDMMC_CmdWriteSingleBlock(hmmc->Instance, add);
1187  }
1188  if(errorstate != HAL_MMC_ERROR_NONE)
1189  {
1190  /* Clear all the static flags */
1191  __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
1192  hmmc->ErrorCode |= errorstate;
1193  hmmc->State = HAL_MMC_STATE_READY;
1194  return HAL_ERROR;
1195  }
1196 
1197 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1198  /* Configure the MMC DPSM (Data Path State Machine) */
1199  config.DataTimeOut = SDMMC_DATATIMEOUT;
1200  config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
1201  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1202  config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1203  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1204  config.DPSM = SDMMC_DPSM_ENABLE;
1205  (void)SDMMC_ConfigData(hmmc->Instance, &config);
1206 #endif
1207 
1208  return HAL_OK;
1209  }
1210  else
1211  {
1212  return HAL_BUSY;
1213  }
1214 }
uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef *SDMMCx, uint32_t BlockSize)
Send the Data Block Lenght command and check the response.
uint32_t SDMMC_CmdWriteMultiBlock(SDMMC_TypeDef *SDMMCx, uint32_t WriteAdd)
Send the Write Multi Block command and check the response.
SDMMC Data Control structure.
return HAL_OK
uint32_t SDMMC_CmdWriteSingleBlock(SDMMC_TypeDef *SDMMCx, uint32_t WriteAdd)
Send the Write Single Block command and check the response.
HAL_StatusTypeDef SDMMC_ConfigData(SDMMC_TypeDef *SDMMCx, SDMMC_DataInitTypeDef *Data)
Configure the SDMMC data path according to the specified parameters in the SDMMC_DataInitTypeDef.