STM32L4xx_HAL_Driver  1.14.0
Input and Output operation functions

Data transfer functions. More...

Functions

HAL_StatusTypeDef HAL_SD_ReadBlocks (SD_HandleTypeDef *hsd, 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_SD_WriteBlocks (SD_HandleTypeDef *hsd, 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_SD_Erase (SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
 Erases the specified memory area of the given SD card. More...
 
HAL_StatusTypeDef HAL_SD_ReadBlocks_IT (SD_HandleTypeDef *hsd, 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_SD_WriteBlocks_IT (SD_HandleTypeDef *hsd, 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_SD_ReadBlocks_DMA (SD_HandleTypeDef *hsd, 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_SD_WriteBlocks_DMA (SD_HandleTypeDef *hsd, 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_SD_IRQHandler (SD_HandleTypeDef *hsd)
 This function handles SD card interrupt request. More...
 
void HAL_SD_TxCpltCallback (SD_HandleTypeDef *hsd)
 Tx Transfer completed callbacks. More...
 
void HAL_SD_RxCpltCallback (SD_HandleTypeDef *hsd)
 Rx Transfer completed callbacks. More...
 
void HAL_SD_ErrorCallback (SD_HandleTypeDef *hsd)
 SD error callbacks. More...
 
void HAL_SD_AbortCallback (SD_HandleTypeDef *hsd)
 SD Abort callbacks. More...
 
HAL_StatusTypeDef HAL_SD_RegisterCallback (SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID, pSD_CallbackTypeDef pCallback)
 Register a User SD Callback To be used instead of the weak (surcharged) predefined callback. More...
 
HAL_StatusTypeDef HAL_SD_UnRegisterCallback (SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID)
 Unregister a User SD Callback SD Callback is redirected to the weak (surcharged) predefined callback. More...
 
HAL_StatusTypeDef HAL_SD_RegisterTransceiverCallback (SD_HandleTypeDef *hsd, pSD_TransceiverCallbackTypeDef pCallback)
 Register a User SD Transceiver Callback To be used instead of the weak (surcharged) predefined callback. More...
 
HAL_StatusTypeDef HAL_SD_UnRegisterTransceiverCallback (SD_HandleTypeDef *hsd)
 Unregister a User SD Transceiver Callback SD Callback is redirected to the weak (surcharged) predefined callback. More...
 
HAL_SD_StateTypeDef HAL_SD_GetState (SD_HandleTypeDef *hsd)
 return the SD state More...
 
uint32_t HAL_SD_GetError (SD_HandleTypeDef *hsd)
 Return the SD 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 SD card.

Function Documentation

◆ HAL_SD_AbortCallback()

__weak void HAL_SD_AbortCallback ( SD_HandleTypeDef hsd)

SD Abort callbacks.

Parameters
hsdPointer SD handle
Return values
None

Definition at line 2111 of file stm32l4xx_hal_sd.c.

2112 {
2113  /* Prevent unused argument(s) compilation warning */
2114  UNUSED(hsd);
2115 
2116  /* NOTE : This function should not be modified, when the callback is needed,
2117  the HAL_SD_AbortCallback can be implemented in the user file
2118  */
2119 }

◆ HAL_SD_Erase()

HAL_StatusTypeDef HAL_SD_Erase ( SD_HandleTypeDef hsd,
uint32_t  BlockStartAdd,
uint32_t  BlockEndAdd 
)

Erases the specified memory area of the given SD card.

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

Definition at line 1634 of file stm32l4xx_hal_sd.c.

1635 {
1636  uint32_t errorstate;
1637  uint32_t start_add = BlockStartAdd;
1638  uint32_t end_add = BlockEndAdd;
1639 
1640  if(hsd->State == HAL_SD_STATE_READY)
1641  {
1642  hsd->ErrorCode = HAL_SD_ERROR_NONE;
1643 
1644  if(end_add < start_add)
1645  {
1646  hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1647  return HAL_ERROR;
1648  }
1649 
1650  if(end_add > (hsd->SdCard.LogBlockNbr))
1651  {
1652  hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1653  return HAL_ERROR;
1654  }
1655 
1656  hsd->State = HAL_SD_STATE_BUSY;
1657 
1658  /* Check if the card command class supports erase command */
1659  if(((hsd->SdCard.Class) & SDMMC_CCCC_ERASE) == 0U)
1660  {
1661  /* Clear all the static flags */
1662  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1663  hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
1664  hsd->State = HAL_SD_STATE_READY;
1665  return HAL_ERROR;
1666  }
1667 
1668  if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
1669  {
1670  /* Clear all the static flags */
1671  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1672  hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
1673  hsd->State = HAL_SD_STATE_READY;
1674  return HAL_ERROR;
1675  }
1676 
1677  /* Get start and end block for high capacity cards */
1678  if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1679  {
1680  start_add *= 512U;
1681  end_add *= 512U;
1682  }
1683 
1684  /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1685  if(hsd->SdCard.CardType != CARD_SECURED)
1686  {
1687  /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
1688  errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, start_add);
1689  if(errorstate != HAL_SD_ERROR_NONE)
1690  {
1691  /* Clear all the static flags */
1692  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1693  hsd->ErrorCode |= errorstate;
1694  hsd->State = HAL_SD_STATE_READY;
1695  return HAL_ERROR;
1696  }
1697 
1698  /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
1699  errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, end_add);
1700  if(errorstate != HAL_SD_ERROR_NONE)
1701  {
1702  /* Clear all the static flags */
1703  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1704  hsd->ErrorCode |= errorstate;
1705  hsd->State = HAL_SD_STATE_READY;
1706  return HAL_ERROR;
1707  }
1708  }
1709 
1710  /* Send CMD38 ERASE */
1711  errorstate = SDMMC_CmdErase(hsd->Instance);
1712  if(errorstate != HAL_SD_ERROR_NONE)
1713  {
1714  /* Clear all the static flags */
1715  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1716  hsd->ErrorCode |= errorstate;
1717  hsd->State = HAL_SD_STATE_READY;
1718  return HAL_ERROR;
1719  }
1720 
1721  hsd->State = HAL_SD_STATE_READY;
1722 
1723  return HAL_OK;
1724  }
1725  else
1726  {
1727  return HAL_BUSY;
1728  }
1729 }
uint32_t SDMMC_CmdErase(SDMMC_TypeDef *SDMMCx)
Send the Erase command and check the response.
uint32_t SDMMC_CmdSDEraseEndAdd(SDMMC_TypeDef *SDMMCx, uint32_t EndAdd)
Send the End Address Erase command for SD 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_CmdSDEraseStartAdd(SDMMC_TypeDef *SDMMCx, uint32_t StartAdd)
Send the Start Address Erase command for SD and check the response.

◆ HAL_SD_ErrorCallback()

__weak void HAL_SD_ErrorCallback ( SD_HandleTypeDef hsd)

SD error callbacks.

Parameters
hsdPointer SD handle
Return values
None

Definition at line 2096 of file stm32l4xx_hal_sd.c.

2097 {
2098  /* Prevent unused argument(s) compilation warning */
2099  UNUSED(hsd);
2100 
2101  /* NOTE : This function should not be modified, when the callback is needed,
2102  the HAL_SD_ErrorCallback can be implemented in the user file
2103  */
2104 }

◆ HAL_SD_GetError()

uint32_t HAL_SD_GetError ( SD_HandleTypeDef hsd)

Return the SD error code.

Parameters
hsd: Pointer to a SD_HandleTypeDef structure that contains the configuration information.
Return values
SDError Code

Definition at line 2056 of file stm32l4xx_hal_sd.c.

2057 {
2058  return hsd->ErrorCode;
2059 }

◆ HAL_SD_GetState()

HAL_SD_StateTypeDef HAL_SD_GetState ( SD_HandleTypeDef hsd)

return the SD state

Parameters
hsdPointer to sd handle
Return values
HALstate

Definition at line 2045 of file stm32l4xx_hal_sd.c.

2046 {
2047  return hsd->State;
2048 }

◆ HAL_SD_IRQHandler()

void HAL_SD_IRQHandler ( SD_HandleTypeDef hsd)

This function handles SD card interrupt request.

Parameters
hsdPointer to SD handle
Return values
None

Definition at line 1736 of file stm32l4xx_hal_sd.c.

1737 {
1738  uint32_t errorstate;
1739  uint32_t context = hsd->Context;
1740 
1741  /* Check for SDMMC interrupt flags */
1742  if((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1743  {
1744  SD_Read_IT(hsd);
1745  }
1746 
1747  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) != RESET)
1748  {
1749  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DATAEND);
1750 
1751  __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT |\
1752  SDMMC_IT_TXUNDERR | SDMMC_IT_RXOVERR | SDMMC_IT_TXFIFOHE |\
1753  SDMMC_IT_RXFIFOHF);
1754 
1755 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1756  __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_IDMABTC);
1757  __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
1758 #else
1759  hsd->Instance->DCTRL &= ~(SDMMC_DCTRL_DTEN);
1760 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1761 
1762  if((context & SD_CONTEXT_IT) != 0U)
1763  {
1764  if(((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1765  {
1766  errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1767  if(errorstate != HAL_SD_ERROR_NONE)
1768  {
1769  hsd->ErrorCode |= errorstate;
1770 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1771  hsd->ErrorCallback(hsd);
1772 #else
1773  HAL_SD_ErrorCallback(hsd);
1774 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1775  }
1776  }
1777 
1778  /* Clear all the static flags */
1779  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1780 
1781  hsd->State = HAL_SD_STATE_READY;
1782  hsd->Context = SD_CONTEXT_NONE;
1783  if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1784  {
1785 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1786  hsd->RxCpltCallback(hsd);
1787 #else
1788  HAL_SD_RxCpltCallback(hsd);
1789 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1790  }
1791  else
1792  {
1793 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1794  hsd->TxCpltCallback(hsd);
1795 #else
1796  HAL_SD_TxCpltCallback(hsd);
1797 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1798  }
1799  }
1800  else if((context & SD_CONTEXT_DMA) != 0U)
1801  {
1802 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1803  hsd->Instance->DLEN = 0;
1804  hsd->Instance->DCTRL = 0;
1805  hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
1806 
1807  /* Stop Transfer for Write Multi blocks or Read Multi blocks */
1808  if(((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1809  {
1810  errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1811  if(errorstate != HAL_SD_ERROR_NONE)
1812  {
1813  hsd->ErrorCode |= errorstate;
1814 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1815  hsd->ErrorCallback(hsd);
1816 #else
1817  HAL_SD_ErrorCallback(hsd);
1818 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1819  }
1820  }
1821 
1822  hsd->State = HAL_SD_STATE_READY;
1823  hsd->Context = SD_CONTEXT_NONE;
1824  if(((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1825  {
1826 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1827  hsd->TxCpltCallback(hsd);
1828 #else
1829  HAL_SD_TxCpltCallback(hsd);
1830 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1831  }
1832  if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1833  {
1834 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1835  hsd->RxCpltCallback(hsd);
1836 #else
1837  HAL_SD_RxCpltCallback(hsd);
1838 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1839  }
1840 #else
1841  if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1842  {
1843  errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1844  if(errorstate != HAL_SD_ERROR_NONE)
1845  {
1846  hsd->ErrorCode |= errorstate;
1847 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1848  hsd->ErrorCallback(hsd);
1849 #else
1850  HAL_SD_ErrorCallback(hsd);
1851 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1852  }
1853  }
1854  if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == 0U))
1855  {
1856  /* Disable the DMA transfer for transmit request by setting the DMAEN bit
1857  in the SD DCTRL register */
1858  hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
1859 
1860  hsd->State = HAL_SD_STATE_READY;
1861 
1862 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1863  hsd->TxCpltCallback(hsd);
1864 #else
1865  HAL_SD_TxCpltCallback(hsd);
1866 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1867  }
1868 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1869  }
1870  else
1871  {
1872  /* Nothing to do */
1873  }
1874  }
1875 
1876  else if((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1877  {
1878  SD_Write_IT(hsd);
1879  }
1880 
1881  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_RXOVERR | SDMMC_FLAG_TXUNDERR) != RESET)
1882  {
1883  /* Set Error code */
1884  if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DCRCFAIL) != RESET)
1885  {
1886  hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
1887  }
1888  if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DTIMEOUT) != RESET)
1889  {
1890  hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1891  }
1892  if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_RXOVERR) != RESET)
1893  {
1894  hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
1895  }
1896  if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_TXUNDERR) != RESET)
1897  {
1898  hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
1899  }
1900 
1901  /* Clear All flags */
1902  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1903 
1904  /* Disable all interrupts */
1905  __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
1906  SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
1907 
1908 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1909  __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
1910  hsd->Instance->DCTRL |= SDMMC_DCTRL_FIFORST;
1911  hsd->Instance->CMD |= SDMMC_CMD_CMDSTOP;
1912 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1913  hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
1914 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1915  hsd->Instance->CMD &= ~(SDMMC_CMD_CMDSTOP);
1916  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DABORT);
1917 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1918 
1919  if((context & SD_CONTEXT_IT) != 0U)
1920  {
1921  /* Set the SD state to ready to be able to start again the process */
1922  hsd->State = HAL_SD_STATE_READY;
1923  hsd->Context = SD_CONTEXT_NONE;
1924 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1925  hsd->ErrorCallback(hsd);
1926 #else
1927  HAL_SD_ErrorCallback(hsd);
1928 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1929  }
1930  else if((context & SD_CONTEXT_DMA) != 0U)
1931  {
1932 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1933  if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
1934  {
1935  /* Disable Internal DMA */
1936  __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_IDMABTC);
1937  hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
1938 
1939  /* Set the SD state to ready to be able to start again the process */
1940  hsd->State = HAL_SD_STATE_READY;
1941 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1942  hsd->ErrorCallback(hsd);
1943 #else
1944  HAL_SD_ErrorCallback(hsd);
1945 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1946  }
1947 #else
1948  /* Abort the SD DMA channel */
1949  if(((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1950  {
1951  /* Set the DMA Tx abort callback */
1952  hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
1953  /* Abort DMA in IT mode */
1954  if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
1955  {
1956  SD_DMATxAbort(hsd->hdmatx);
1957  }
1958  }
1959  else if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1960  {
1961  /* Set the DMA Rx abort callback */
1962  hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
1963  /* Abort DMA in IT mode */
1964  if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
1965  {
1966  SD_DMARxAbort(hsd->hdmarx);
1967  }
1968  }
1969  else
1970  {
1971  hsd->ErrorCode = HAL_SD_ERROR_NONE;
1972  hsd->State = HAL_SD_STATE_READY;
1973  hsd->Context = SD_CONTEXT_NONE;
1974 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1975  hsd->AbortCpltCallback(hsd);
1976 #else
1977  HAL_SD_AbortCallback(hsd);
1978 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1979  }
1980 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1981  }
1982  else
1983  {
1984  /* Nothing to do */
1985  }
1986  }
1987 
1988 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1989  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_IDMABTC) != RESET)
1990  {
1991  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_IDMABTC);
1992  if(READ_BIT(hsd->Instance->IDMACTRL, SDMMC_IDMA_IDMABACT) == 0U)
1993  {
1994  /* Current buffer is buffer0, Transfer complete for buffer1 */
1995  if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1996  {
1997 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1998  hsd->Write_DMADblBuf1CpltCallback(hsd);
1999 #else
2001 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
2002  }
2003  else /* SD_CONTEXT_READ_MULTIPLE_BLOCK */
2004  {
2005 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
2006  hsd->Read_DMADblBuf1CpltCallback(hsd);
2007 #else
2009 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
2010  }
2011  }
2012  else /* SD_DMA_BUFFER1 */
2013  {
2014  /* Current buffer is buffer1, Transfer complete for buffer0 */
2015  if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
2016  {
2017 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
2018  hsd->Write_DMADblBuf0CpltCallback(hsd);
2019 #else
2021 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
2022  }
2023  else /* SD_CONTEXT_READ_MULTIPLE_BLOCK */
2024  {
2025 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
2026  hsd->Read_DMADblBuf0CpltCallback(hsd);
2027 #else
2029 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
2030  }
2031  }
2032  }
2033 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
2034  else
2035  {
2036  /* Nothing to do */
2037  }
2038 }
void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
SD error callbacks.
HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
Aborts the DMA Transfer in Interrupt mode.
void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
Rx Transfer completed callbacks.
static void SD_DMARxAbort(DMA_HandleTypeDef *hdma)
DMA SD Rx Abort callback.
static void SD_DMATxAbort(DMA_HandleTypeDef *hdma)
DMA SD Tx Abort callback.
void HAL_SDEx_Write_DMADoubleBuffer0CpltCallback(SD_HandleTypeDef *hsd)
Write DMA Buffer 0 Transfer completed callbacks.
void HAL_SDEx_Read_DMADoubleBuffer0CpltCallback(SD_HandleTypeDef *hsd)
Read DMA Buffer 0 Transfer completed callbacks.
void HAL_SDEx_Write_DMADoubleBuffer1CpltCallback(SD_HandleTypeDef *hsd)
Write DMA Buffer 1 Transfer completed callbacks.
return HAL_OK
static void SD_Read_IT(SD_HandleTypeDef *hsd)
Wrap up reading in non-blocking mode.
uint32_t SDMMC_CmdStopTransfer(SDMMC_TypeDef *SDMMCx)
Send the Stop Transfer command and check the response.
void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
SD Abort callbacks.
void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
Tx Transfer completed callbacks.
void HAL_SDEx_Read_DMADoubleBuffer1CpltCallback(SD_HandleTypeDef *hsd)
Read DMA Buffer 1 Transfer completed callbacks.
static void SD_Write_IT(SD_HandleTypeDef *hsd)
Wrap up writing in non-blocking mode.

◆ HAL_SD_ReadBlocks()

HAL_StatusTypeDef HAL_SD_ReadBlocks ( SD_HandleTypeDef hsd,
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_SD_GetCardState().
Parameters
hsdPointer to SD handle
pDatapointer to the buffer that will contain the received data
BlockAddBlock Address from where data is to be read
NumberOfBlocksNumber of SD blocks to read
TimeoutSpecify timeout value
Return values
HALstatus

Definition at line 667 of file stm32l4xx_hal_sd.c.

668 {
669  SDMMC_DataInitTypeDef config;
670  uint32_t errorstate;
671  uint32_t tickstart = HAL_GetTick();
672  uint32_t count, data, dataremaining;
673  uint32_t add = BlockAdd;
674  uint8_t *tempbuff = pData;
675 
676  if(NULL == pData)
677  {
678  hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
679  return HAL_ERROR;
680  }
681 
682  if(hsd->State == HAL_SD_STATE_READY)
683  {
684  hsd->ErrorCode = HAL_SD_ERROR_NONE;
685 
686  if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
687  {
688  hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
689  return HAL_ERROR;
690  }
691 
692  hsd->State = HAL_SD_STATE_BUSY;
693 
694  /* Initialize data control register */
695  hsd->Instance->DCTRL = 0U;
696 
697  if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
698  {
699  add *= 512U;
700  }
701 
702  /* Set Block Size for Card */
703  errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
704  if(errorstate != HAL_SD_ERROR_NONE)
705  {
706  /* Clear all the static flags */
707  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
708  hsd->ErrorCode |= errorstate;
709  hsd->State = HAL_SD_STATE_READY;
710  return HAL_ERROR;
711  }
712 
713  /* Configure the SD DPSM (Data Path State Machine) */
714  config.DataTimeOut = SDMMC_DATATIMEOUT;
715  config.DataLength = NumberOfBlocks * BLOCKSIZE;
716  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
717  config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
718  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
719 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
720  config.DPSM = SDMMC_DPSM_DISABLE;
721 #else
722  config.DPSM = SDMMC_DPSM_ENABLE;
723 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
724  (void)SDMMC_ConfigData(hsd->Instance, &config);
725 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
726  __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
727 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
728 
729  /* Read block(s) in polling mode */
730  if(NumberOfBlocks > 1U)
731  {
732  hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK;
733 
734  /* Read Multi Block command */
735  errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
736  }
737  else
738  {
739  hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK;
740 
741  /* Read Single Block command */
742  errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
743  }
744  if(errorstate != HAL_SD_ERROR_NONE)
745  {
746  /* Clear all the static flags */
747  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
748  hsd->ErrorCode |= errorstate;
749  hsd->State = HAL_SD_STATE_READY;
750  hsd->Context = SD_CONTEXT_NONE;
751  return HAL_ERROR;
752  }
753 
754  /* Poll on SDMMC flags */
755  dataremaining = config.DataLength;
756  while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
757  {
758  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF) && (dataremaining > 0U))
759  {
760  /* Read data from SDMMC Rx FIFO */
761  for(count = 0U; count < 8U; count++)
762  {
763  data = SDMMC_ReadFIFO(hsd->Instance);
764  *tempbuff = (uint8_t)(data & 0xFFU);
765  tempbuff++;
766  dataremaining--;
767  *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
768  tempbuff++;
769  dataremaining--;
770  *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
771  tempbuff++;
772  dataremaining--;
773  *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
774  tempbuff++;
775  dataremaining--;
776  }
777  }
778 
779  if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
780  {
781  /* Clear all the static flags */
782  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
783  hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
784  hsd->State= HAL_SD_STATE_READY;
785  hsd->Context = SD_CONTEXT_NONE;
786  return HAL_TIMEOUT;
787  }
788  }
789 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
790  __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
791 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
792 
793  /* Send stop transmission command in case of multiblock read */
794  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
795  {
796  if(hsd->SdCard.CardType != CARD_SECURED)
797  {
798  /* Send stop transmission command */
799  errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
800  if(errorstate != HAL_SD_ERROR_NONE)
801  {
802  /* Clear all the static flags */
803  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
804  hsd->ErrorCode |= errorstate;
805  hsd->State = HAL_SD_STATE_READY;
806  hsd->Context = SD_CONTEXT_NONE;
807  return HAL_ERROR;
808  }
809  }
810  }
811 
812  /* Get error state */
813  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
814  {
815  /* Clear all the static flags */
816  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
817  hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
818  hsd->State = HAL_SD_STATE_READY;
819  hsd->Context = SD_CONTEXT_NONE;
820  return HAL_ERROR;
821  }
822  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
823  {
824  /* Clear all the static flags */
825  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
826  hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
827  hsd->State = HAL_SD_STATE_READY;
828  hsd->Context = SD_CONTEXT_NONE;
829  return HAL_ERROR;
830  }
831  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
832  {
833  /* Clear all the static flags */
834  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
835  hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
836  hsd->State = HAL_SD_STATE_READY;
837  hsd->Context = SD_CONTEXT_NONE;
838  return HAL_ERROR;
839  }
840  else
841  {
842  /* Nothing to do */
843  }
844 
845 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
846  /* Empty FIFO if there is still any data */
847  while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)) && (dataremaining > 0U))
848  {
849  data = SDMMC_ReadFIFO(hsd->Instance);
850  *tempbuff = (uint8_t)(data & 0xFFU);
851  tempbuff++;
852  dataremaining--;
853  *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
854  tempbuff++;
855  dataremaining--;
856  *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
857  tempbuff++;
858  dataremaining--;
859  *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
860  tempbuff++;
861  dataremaining--;
862 
863  if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
864  {
865  /* Clear all the static flags */
866  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
867  hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
868  hsd->State= HAL_SD_STATE_READY;
869  hsd->Context = SD_CONTEXT_NONE;
870  return HAL_ERROR;
871  }
872  }
873 #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
874 
875  /* Clear all the static flags */
876  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
877 
878  hsd->State = HAL_SD_STATE_READY;
879 
880  return HAL_OK;
881  }
882  else
883  {
884  hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
885  return HAL_ERROR;
886  }
887 }
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_SD_ReadBlocks_DMA()

HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA ( SD_HandleTypeDef hsd,
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_SD_GetCardState().
You could also check the DMA transfer process through the SD Rx interrupt event.
Parameters
hsdPointer SD 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 1335 of file stm32l4xx_hal_sd.c.

1336 {
1337  SDMMC_DataInitTypeDef config;
1338  uint32_t errorstate;
1339  uint32_t add = BlockAdd;
1340 
1341  if(NULL == pData)
1342  {
1343  hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1344  return HAL_ERROR;
1345  }
1346 
1347  if(hsd->State == HAL_SD_STATE_READY)
1348  {
1349  hsd->ErrorCode = HAL_SD_ERROR_NONE;
1350 
1351  if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1352  {
1353  hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1354  return HAL_ERROR;
1355  }
1356 
1357  hsd->State = HAL_SD_STATE_BUSY;
1358 
1359  /* Initialize data control register */
1360  hsd->Instance->DCTRL = 0U;
1361 
1362 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1363  __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1364 
1365  /* Set the DMA transfer complete callback */
1366  hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt;
1367 
1368  /* Set the DMA error callback */
1369  hsd->hdmarx->XferErrorCallback = SD_DMAError;
1370 
1371  /* Set the DMA Abort callback */
1372  hsd->hdmarx->XferAbortCallback = NULL;
1373 
1374  /* Enable the DMA Channel */
1375  if(HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
1376  {
1377  __HAL_SD_DISABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1378  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1379  hsd->ErrorCode |= HAL_SD_ERROR_DMA;
1380  hsd->State = HAL_SD_STATE_READY;
1381  return HAL_ERROR;
1382  }
1383  else
1384  {
1385  /* Enable SD DMA transfer */
1386  __HAL_SD_DMA_ENABLE(hsd);
1387 #else
1388  hsd->pRxBuffPtr = pData;
1389  hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
1390 #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
1391 
1392  if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1393  {
1394  add *= 512U;
1395  }
1396 
1397  /* Set Block Size for Card */
1398  errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1399  if(errorstate != HAL_SD_ERROR_NONE)
1400  {
1401  /* Clear all the static flags */
1402  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1403  hsd->ErrorCode |= errorstate;
1404  hsd->State = HAL_SD_STATE_READY;
1405  return HAL_ERROR;
1406  }
1407 
1408  /* Configure the SD DPSM (Data Path State Machine) */
1409  config.DataTimeOut = SDMMC_DATATIMEOUT;
1410  config.DataLength = BLOCKSIZE * NumberOfBlocks;
1411  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1412  config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1413  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1414 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1415  config.DPSM = SDMMC_DPSM_DISABLE;
1416 #else
1417  config.DPSM = SDMMC_DPSM_ENABLE;
1418 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1419  (void)SDMMC_ConfigData(hsd->Instance, &config);
1420 
1421 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1422  /* Enable transfer interrupts */
1423  __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1424 
1425  __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
1426  hsd->Instance->IDMABASE0 = (uint32_t) pData ;
1427  hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
1428 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1429 
1430  /* Read Blocks in DMA mode */
1431  if(NumberOfBlocks > 1U)
1432  {
1433  hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1434 
1435  /* Read Multi Block command */
1436  errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1437  }
1438  else
1439  {
1440  hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
1441 
1442  /* Read Single Block command */
1443  errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1444  }
1445  if(errorstate != HAL_SD_ERROR_NONE)
1446  {
1447  /* Clear all the static flags */
1448  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1449 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1450  __HAL_SD_DISABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1451 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1452  hsd->ErrorCode |= errorstate;
1453  hsd->State = HAL_SD_STATE_READY;
1454  hsd->Context = SD_CONTEXT_NONE;
1455  return HAL_ERROR;
1456  }
1457 
1458  return HAL_OK;
1459 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1460  }
1461 #endif
1462  }
1463  else
1464  {
1465  return HAL_BUSY;
1466  }
1467 }
static void SD_DMAError(DMA_HandleTypeDef *hdma)
DMA SD communication error callback.
uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef *SDMMCx, uint32_t BlockSize)
Send the Data Block Lenght command and check the response.
static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
DMA SD receive process complete callback.
uint32_t SDMMC_CmdReadSingleBlock(SDMMC_TypeDef *SDMMCx, uint32_t ReadAdd)
Send the Read Single Block command and check the response.
SDMMC Data Control structure.
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
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_SD_ReadBlocks_IT()

HAL_StatusTypeDef HAL_SD_ReadBlocks_IT ( SD_HandleTypeDef hsd,
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_SD_GetCardState().
You could also check the IT transfer process through the SD Rx interrupt event.
Parameters
hsdPointer to SD 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 1106 of file stm32l4xx_hal_sd.c.

1107 {
1108  SDMMC_DataInitTypeDef config;
1109  uint32_t errorstate;
1110  uint32_t add = BlockAdd;
1111 
1112  if(NULL == pData)
1113  {
1114  hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1115  return HAL_ERROR;
1116  }
1117 
1118  if(hsd->State == HAL_SD_STATE_READY)
1119  {
1120  hsd->ErrorCode = HAL_SD_ERROR_NONE;
1121 
1122  if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1123  {
1124  hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1125  return HAL_ERROR;
1126  }
1127 
1128  hsd->State = HAL_SD_STATE_BUSY;
1129 
1130  /* Initialize data control register */
1131  hsd->Instance->DCTRL = 0U;
1132 
1133  hsd->pRxBuffPtr = pData;
1134  hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
1135 
1136  __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_FLAG_RXFIFOHF));
1137 
1138  if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1139  {
1140  add *= 512U;
1141  }
1142 
1143  /* Set Block Size for Card */
1144  errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1145  if(errorstate != HAL_SD_ERROR_NONE)
1146  {
1147  /* Clear all the static flags */
1148  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1149  hsd->ErrorCode |= errorstate;
1150  hsd->State = HAL_SD_STATE_READY;
1151  return HAL_ERROR;
1152  }
1153 
1154  /* Configure the SD DPSM (Data Path State Machine) */
1155  config.DataTimeOut = SDMMC_DATATIMEOUT;
1156  config.DataLength = BLOCKSIZE * NumberOfBlocks;
1157  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1158  config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1159  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1160 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1161  config.DPSM = SDMMC_DPSM_DISABLE;
1162 #else
1163  config.DPSM = SDMMC_DPSM_ENABLE;
1164 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1165  (void)SDMMC_ConfigData(hsd->Instance, &config);
1166 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1167  __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
1168 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1169 
1170  /* Read Blocks in IT mode */
1171  if(NumberOfBlocks > 1U)
1172  {
1173  hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
1174 
1175  /* Read Multi Block command */
1176  errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1177  }
1178  else
1179  {
1180  hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
1181 
1182  /* Read Single Block command */
1183  errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1184  }
1185  if(errorstate != HAL_SD_ERROR_NONE)
1186  {
1187  /* Clear all the static flags */
1188  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1189  hsd->ErrorCode |= errorstate;
1190  hsd->State = HAL_SD_STATE_READY;
1191  hsd->Context = SD_CONTEXT_NONE;
1192  return HAL_ERROR;
1193  }
1194 
1195  return HAL_OK;
1196  }
1197  else
1198  {
1199  return HAL_BUSY;
1200  }
1201 }
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_SD_RegisterCallback()

HAL_StatusTypeDef HAL_SD_RegisterCallback ( SD_HandleTypeDef hsd,
HAL_SD_CallbackIDTypeDef  CallbackID,
pSD_CallbackTypeDef  pCallback 
)

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

Parameters
hsd: SD 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 2141 of file stm32l4xx_hal_sd.c.

2142 {
2143  HAL_StatusTypeDef status = HAL_OK;
2144 
2145  if(pCallback == NULL)
2146  {
2147  /* Update the error code */
2148  hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2149  return HAL_ERROR;
2150  }
2151 
2152  /* Process locked */
2153  __HAL_LOCK(hsd);
2154 
2155  if(hsd->State == HAL_SD_STATE_READY)
2156  {
2157  switch (CallbackID)
2158  {
2159  case HAL_SD_TX_CPLT_CB_ID :
2160  hsd->TxCpltCallback = pCallback;
2161  break;
2162  case HAL_SD_RX_CPLT_CB_ID :
2163  hsd->RxCpltCallback = pCallback;
2164  break;
2165  case HAL_SD_ERROR_CB_ID :
2166  hsd->ErrorCallback = pCallback;
2167  break;
2168  case HAL_SD_ABORT_CB_ID :
2169  hsd->AbortCpltCallback = pCallback;
2170  break;
2171 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
2173  hsd->Read_DMADblBuf0CpltCallback = pCallback;
2174  break;
2176  hsd->Read_DMADblBuf1CpltCallback = pCallback;
2177  break;
2179  hsd->Write_DMADblBuf0CpltCallback = pCallback;
2180  break;
2182  hsd->Write_DMADblBuf1CpltCallback = pCallback;
2183  break;
2184 #endif
2185  case HAL_SD_MSP_INIT_CB_ID :
2186  hsd->MspInitCallback = pCallback;
2187  break;
2189  hsd->MspDeInitCallback = pCallback;
2190  break;
2191  default :
2192  /* Update the error code */
2193  hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2194  /* update return status */
2195  status = HAL_ERROR;
2196  break;
2197  }
2198  }
2199  else if (hsd->State == HAL_SD_STATE_RESET)
2200  {
2201  switch (CallbackID)
2202  {
2203  case HAL_SD_MSP_INIT_CB_ID :
2204  hsd->MspInitCallback = pCallback;
2205  break;
2207  hsd->MspDeInitCallback = pCallback;
2208  break;
2209  default :
2210  /* Update the error code */
2211  hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2212  /* update return status */
2213  status = HAL_ERROR;
2214  break;
2215  }
2216  }
2217  else
2218  {
2219  /* Update the error code */
2220  hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2221  /* update return status */
2222  status = HAL_ERROR;
2223  }
2224 
2225  /* Release Lock */
2226  __HAL_UNLOCK(hsd);
2227  return status;
2228 }
__HAL_UNLOCK(hrtc)
__HAL_LOCK(hrtc)
return HAL_OK

◆ HAL_SD_RegisterTransceiverCallback()

HAL_StatusTypeDef HAL_SD_RegisterTransceiverCallback ( SD_HandleTypeDef hsd,
pSD_TransceiverCallbackTypeDef  pCallback 
)

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

Parameters
hsd: SD handle
pCallback: pointer to the Callback function
Return values
status

Definition at line 2338 of file stm32l4xx_hal_sd.c.

2339 {
2340  HAL_StatusTypeDef status = HAL_OK;
2341 
2342  if(pCallback == NULL)
2343  {
2344  /* Update the error code */
2345  hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2346  return HAL_ERROR;
2347  }
2348 
2349  /* Process locked */
2350  __HAL_LOCK(hsd);
2351 
2352  if(hsd->State == HAL_SD_STATE_READY)
2353  {
2354  hsd->DriveTransceiver_1_8V_Callback = pCallback;
2355  }
2356  else
2357  {
2358  /* Update the error code */
2359  hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2360  /* update return status */
2361  status = HAL_ERROR;
2362  }
2363 
2364  /* Release Lock */
2365  __HAL_UNLOCK(hsd);
2366  return status;
2367 }
__HAL_UNLOCK(hrtc)
__HAL_LOCK(hrtc)
return HAL_OK

◆ HAL_SD_RxCpltCallback()

__weak void HAL_SD_RxCpltCallback ( SD_HandleTypeDef hsd)

Rx Transfer completed callbacks.

Parameters
hsdPointer SD handle
Return values
None

Definition at line 2081 of file stm32l4xx_hal_sd.c.

2082 {
2083  /* Prevent unused argument(s) compilation warning */
2084  UNUSED(hsd);
2085 
2086  /* NOTE : This function should not be modified, when the callback is needed,
2087  the HAL_SD_RxCpltCallback can be implemented in the user file
2088  */
2089 }

◆ HAL_SD_TxCpltCallback()

__weak void HAL_SD_TxCpltCallback ( SD_HandleTypeDef hsd)

Tx Transfer completed callbacks.

Parameters
hsdPointer to SD handle
Return values
None

Definition at line 2066 of file stm32l4xx_hal_sd.c.

2067 {
2068  /* Prevent unused argument(s) compilation warning */
2069  UNUSED(hsd);
2070 
2071  /* NOTE : This function should not be modified, when the callback is needed,
2072  the HAL_SD_TxCpltCallback can be implemented in the user file
2073  */
2074 }

◆ HAL_SD_UnRegisterCallback()

HAL_StatusTypeDef HAL_SD_UnRegisterCallback ( SD_HandleTypeDef hsd,
HAL_SD_CallbackIDTypeDef  CallbackID 
)

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

Parameters
hsd: SD handle
CallbackID: ID of the callback to be unregistered This parameter can be one of the following values:
Return values
status

Definition at line 2248 of file stm32l4xx_hal_sd.c.

2249 {
2250  HAL_StatusTypeDef status = HAL_OK;
2251 
2252  /* Process locked */
2253  __HAL_LOCK(hsd);
2254 
2255  if(hsd->State == HAL_SD_STATE_READY)
2256  {
2257  switch (CallbackID)
2258  {
2259  case HAL_SD_TX_CPLT_CB_ID :
2260  hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
2261  break;
2262  case HAL_SD_RX_CPLT_CB_ID :
2263  hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
2264  break;
2265  case HAL_SD_ERROR_CB_ID :
2266  hsd->ErrorCallback = HAL_SD_ErrorCallback;
2267  break;
2268  case HAL_SD_ABORT_CB_ID :
2269  hsd->AbortCpltCallback = HAL_SD_AbortCallback;
2270  break;
2271 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
2273  hsd->Read_DMADblBuf0CpltCallback = HAL_SDEx_Read_DMADoubleBuffer0CpltCallback;
2274  break;
2276  hsd->Read_DMADblBuf1CpltCallback = HAL_SDEx_Read_DMADoubleBuffer1CpltCallback;
2277  break;
2279  hsd->Write_DMADblBuf0CpltCallback = HAL_SDEx_Write_DMADoubleBuffer0CpltCallback;
2280  break;
2282  hsd->Write_DMADblBuf1CpltCallback = HAL_SDEx_Write_DMADoubleBuffer1CpltCallback;
2283  break;
2284 #endif
2285  case HAL_SD_MSP_INIT_CB_ID :
2286  hsd->MspInitCallback = HAL_SD_MspInit;
2287  break;
2289  hsd->MspDeInitCallback = HAL_SD_MspDeInit;
2290  break;
2291  default :
2292  /* Update the error code */
2293  hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2294  /* update return status */
2295  status = HAL_ERROR;
2296  break;
2297  }
2298  }
2299  else if (hsd->State == HAL_SD_STATE_RESET)
2300  {
2301  switch (CallbackID)
2302  {
2303  case HAL_SD_MSP_INIT_CB_ID :
2304  hsd->MspInitCallback = HAL_SD_MspInit;
2305  break;
2307  hsd->MspDeInitCallback = HAL_SD_MspDeInit;
2308  break;
2309  default :
2310  /* Update the error code */
2311  hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2312  /* update return status */
2313  status = HAL_ERROR;
2314  break;
2315  }
2316  }
2317  else
2318  {
2319  /* Update the error code */
2320  hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2321  /* update return status */
2322  status = HAL_ERROR;
2323  }
2324 
2325  /* Release Lock */
2326  __HAL_UNLOCK(hsd);
2327  return status;
2328 }
void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
Initializes the SD MSP.
void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
SD error callbacks.
void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
Rx Transfer completed callbacks.
__HAL_UNLOCK(hrtc)
void HAL_SDEx_Write_DMADoubleBuffer0CpltCallback(SD_HandleTypeDef *hsd)
Write DMA Buffer 0 Transfer completed callbacks.
__HAL_LOCK(hrtc)
void HAL_SDEx_Read_DMADoubleBuffer0CpltCallback(SD_HandleTypeDef *hsd)
Read DMA Buffer 0 Transfer completed callbacks.
void HAL_SDEx_Write_DMADoubleBuffer1CpltCallback(SD_HandleTypeDef *hsd)
Write DMA Buffer 1 Transfer completed callbacks.
return HAL_OK
void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
SD Abort callbacks.
void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
Tx Transfer completed callbacks.
void HAL_SDEx_Read_DMADoubleBuffer1CpltCallback(SD_HandleTypeDef *hsd)
Read DMA Buffer 1 Transfer completed callbacks.
void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
De-Initialize SD MSP.

◆ HAL_SD_UnRegisterTransceiverCallback()

HAL_StatusTypeDef HAL_SD_UnRegisterTransceiverCallback ( SD_HandleTypeDef hsd)

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

Parameters
hsd: SD handle
Return values
status

Definition at line 2375 of file stm32l4xx_hal_sd.c.

2376 {
2377  HAL_StatusTypeDef status = HAL_OK;
2378 
2379  /* Process locked */
2380  __HAL_LOCK(hsd);
2381 
2382  if(hsd->State == HAL_SD_STATE_READY)
2383  {
2384  hsd->DriveTransceiver_1_8V_Callback = HAL_SDEx_DriveTransceiver_1_8V_Callback;
2385  }
2386  else
2387  {
2388  /* Update the error code */
2389  hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2390  /* update return status */
2391  status = HAL_ERROR;
2392  }
2393 
2394  /* Release Lock */
2395  __HAL_UNLOCK(hsd);
2396  return status;
2397 }
__HAL_UNLOCK(hrtc)
__HAL_LOCK(hrtc)
return HAL_OK
void HAL_SDEx_DriveTransceiver_1_8V_Callback(FlagStatus status)
Enable/Disable the SD Transceiver 1.8V Mode Callback.

◆ HAL_SD_WriteBlocks()

HAL_StatusTypeDef HAL_SD_WriteBlocks ( SD_HandleTypeDef hsd,
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_SD_GetCardState().
Parameters
hsdPointer to SD handle
pDatapointer to the buffer that will contain the data to transmit
BlockAddBlock Address where data will be written
NumberOfBlocksNumber of SD blocks to write
TimeoutSpecify timeout value
Return values
HALstatus

Definition at line 901 of file stm32l4xx_hal_sd.c.

902 {
903  SDMMC_DataInitTypeDef config;
904  uint32_t errorstate;
905  uint32_t tickstart = HAL_GetTick();
906  uint32_t count, data, dataremaining;
907  uint32_t add = BlockAdd;
908  uint8_t *tempbuff = pData;
909 
910  if(NULL == pData)
911  {
912  hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
913  return HAL_ERROR;
914  }
915 
916  if(hsd->State == HAL_SD_STATE_READY)
917  {
918  hsd->ErrorCode = HAL_SD_ERROR_NONE;
919 
920  if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
921  {
922  hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
923  return HAL_ERROR;
924  }
925 
926  hsd->State = HAL_SD_STATE_BUSY;
927 
928  /* Initialize data control register */
929  hsd->Instance->DCTRL = 0U;
930 
931  if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
932  {
933  add *= 512U;
934  }
935 
936  /* Set Block Size for Card */
937  errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
938  if(errorstate != HAL_SD_ERROR_NONE)
939  {
940  /* Clear all the static flags */
941  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
942  hsd->ErrorCode |= errorstate;
943  hsd->State = HAL_SD_STATE_READY;
944  return HAL_ERROR;
945  }
946 
947  /* Configure the SD DPSM (Data Path State Machine) */
948  config.DataTimeOut = SDMMC_DATATIMEOUT;
949  config.DataLength = NumberOfBlocks * BLOCKSIZE;
950  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
951  config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
952  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
953 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
954  config.DPSM = SDMMC_DPSM_DISABLE;
955 #else
956  config.DPSM = SDMMC_DPSM_ENABLE;
957 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
958  (void)SDMMC_ConfigData(hsd->Instance, &config);
959 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
960  __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
961 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
962 
963  /* Write Blocks in Polling mode */
964  if(NumberOfBlocks > 1U)
965  {
966  hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
967 
968  /* Write Multi Block command */
969  errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
970  }
971  else
972  {
973  hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
974 
975  /* Write Single Block command */
976  errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
977  }
978  if(errorstate != HAL_SD_ERROR_NONE)
979  {
980  /* Clear all the static flags */
981  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
982  hsd->ErrorCode |= errorstate;
983  hsd->State = HAL_SD_STATE_READY;
984  hsd->Context = SD_CONTEXT_NONE;
985  return HAL_ERROR;
986  }
987 
988  /* Write block(s) in polling mode */
989  dataremaining = config.DataLength;
990  while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
991  {
992  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE) && (dataremaining > 0U))
993  {
994  /* Write data to SDMMC Tx FIFO */
995  for(count = 0U; count < 8U; count++)
996  {
997  data = (uint32_t)(*tempbuff);
998  tempbuff++;
999  dataremaining--;
1000  data |= ((uint32_t)(*tempbuff) << 8U);
1001  tempbuff++;
1002  dataremaining--;
1003  data |= ((uint32_t)(*tempbuff) << 16U);
1004  tempbuff++;
1005  dataremaining--;
1006  data |= ((uint32_t)(*tempbuff) << 24U);
1007  tempbuff++;
1008  dataremaining--;
1009  (void)SDMMC_WriteFIFO(hsd->Instance, &data);
1010  }
1011  }
1012 
1013  if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
1014  {
1015  /* Clear all the static flags */
1016  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1017  hsd->ErrorCode |= errorstate;
1018  hsd->State = HAL_SD_STATE_READY;
1019  hsd->Context = SD_CONTEXT_NONE;
1020  return HAL_TIMEOUT;
1021  }
1022  }
1023 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1024  __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
1025 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1026 
1027  /* Send stop transmission command in case of multiblock write */
1028  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
1029  {
1030  if(hsd->SdCard.CardType != CARD_SECURED)
1031  {
1032  /* Send stop transmission command */
1033  errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1034  if(errorstate != HAL_SD_ERROR_NONE)
1035  {
1036  /* Clear all the static flags */
1037  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1038  hsd->ErrorCode |= errorstate;
1039  hsd->State = HAL_SD_STATE_READY;
1040  hsd->Context = SD_CONTEXT_NONE;
1041  return HAL_ERROR;
1042  }
1043  }
1044  }
1045 
1046  /* Get error state */
1047  if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
1048  {
1049  /* Clear all the static flags */
1050  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1051  hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1052  hsd->State = HAL_SD_STATE_READY;
1053  hsd->Context = SD_CONTEXT_NONE;
1054  return HAL_ERROR;
1055  }
1056  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
1057  {
1058  /* Clear all the static flags */
1059  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1060  hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
1061  hsd->State = HAL_SD_STATE_READY;
1062  hsd->Context = SD_CONTEXT_NONE;
1063  return HAL_ERROR;
1064  }
1065  else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR))
1066  {
1067  /* Clear all the static flags */
1068  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1069  hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
1070  hsd->State = HAL_SD_STATE_READY;
1071  hsd->Context = SD_CONTEXT_NONE;
1072  return HAL_ERROR;
1073  }
1074  else
1075  {
1076  /* Nothing to do */
1077  }
1078 
1079  /* Clear all the static flags */
1080  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1081 
1082  hsd->State = HAL_SD_STATE_READY;
1083 
1084  return HAL_OK;
1085  }
1086  else
1087  {
1088  hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
1089  return HAL_ERROR;
1090  }
1091 }
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_SD_WriteBlocks_DMA()

HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA ( SD_HandleTypeDef hsd,
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_SD_GetCardState().
You could also check the DMA transfer process through the SD Tx interrupt event.
Parameters
hsdPointer to SD 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 1482 of file stm32l4xx_hal_sd.c.

1483 {
1484  SDMMC_DataInitTypeDef config;
1485  uint32_t errorstate;
1486  uint32_t add = BlockAdd;
1487 
1488  if(NULL == pData)
1489  {
1490  hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1491  return HAL_ERROR;
1492  }
1493 
1494  if(hsd->State == HAL_SD_STATE_READY)
1495  {
1496  hsd->ErrorCode = HAL_SD_ERROR_NONE;
1497 
1498  if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1499  {
1500  hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1501  return HAL_ERROR;
1502  }
1503 
1504  hsd->State = HAL_SD_STATE_BUSY;
1505 
1506  /* Initialize data control register */
1507  hsd->Instance->DCTRL = 0U;
1508 
1509 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1510  hsd->pTxBuffPtr = pData;
1511  hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
1512 #else
1513  /* Enable SD Error interrupts */
1514  __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR));
1515 
1516  /* Set the DMA transfer complete callback */
1517  hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt;
1518 
1519  /* Set the DMA error callback */
1520  hsd->hdmatx->XferErrorCallback = SD_DMAError;
1521 
1522  /* Set the DMA Abort callback */
1523  hsd->hdmatx->XferAbortCallback = NULL;
1524 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1525 
1526  if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1527  {
1528  add *= 512U;
1529  }
1530 
1531  /* Set Block Size for Card */
1532  errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1533  if(errorstate != HAL_SD_ERROR_NONE)
1534  {
1535  /* Clear all the static flags */
1536  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1537  hsd->ErrorCode |= errorstate;
1538  hsd->State = HAL_SD_STATE_READY;
1539  return HAL_ERROR;
1540  }
1541 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1542  /* Configure the SD DPSM (Data Path State Machine) */
1543  config.DataTimeOut = SDMMC_DATATIMEOUT;
1544  config.DataLength = BLOCKSIZE * NumberOfBlocks;
1545  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1546  config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1547  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1548  config.DPSM = SDMMC_DPSM_DISABLE;
1549  (void)SDMMC_ConfigData(hsd->Instance, &config);
1550 
1551  /* Enable transfer interrupts */
1552  __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND));
1553 
1554  __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
1555 
1556  hsd->Instance->IDMABASE0 = (uint32_t) pData ;
1557  hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
1558 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1559 
1560  /* Write Blocks in Polling mode */
1561  if(NumberOfBlocks > 1U)
1562  {
1563  hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1564 
1565  /* Write Multi Block command */
1566  errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1567  }
1568  else
1569  {
1570  hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
1571 
1572  /* Write Single Block command */
1573  errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1574  }
1575  if(errorstate != HAL_SD_ERROR_NONE)
1576  {
1577  /* Clear all the static flags */
1578  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1579 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1580  __HAL_SD_DISABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND));
1581 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1582  hsd->ErrorCode |= errorstate;
1583  hsd->State = HAL_SD_STATE_READY;
1584  hsd->Context = SD_CONTEXT_NONE;
1585  return HAL_ERROR;
1586  }
1587 
1588 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1589  /* Enable SDMMC DMA transfer */
1590  __HAL_SD_DMA_ENABLE(hsd);
1591 
1592  /* Enable the DMA Channel */
1593  if(HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
1594  {
1595  __HAL_SD_DISABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR));
1596  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1597  hsd->ErrorCode |= HAL_SD_ERROR_DMA;
1598  hsd->State = HAL_SD_STATE_READY;
1599  hsd->Context = SD_CONTEXT_NONE;
1600  return HAL_ERROR;
1601  }
1602  else
1603  {
1604  /* Configure the SD DPSM (Data Path State Machine) */
1605  config.DataTimeOut = SDMMC_DATATIMEOUT;
1606  config.DataLength = BLOCKSIZE * NumberOfBlocks;
1607  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1608  config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1609  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1610  config.DPSM = SDMMC_DPSM_ENABLE;
1611  (void)SDMMC_ConfigData(hsd->Instance, &config);
1612 #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
1613 
1614  return HAL_OK;
1615 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1616  }
1617 #endif
1618  }
1619  else
1620  {
1621  return HAL_BUSY;
1622  }
1623 }
static void SD_DMAError(DMA_HandleTypeDef *hdma)
DMA SD communication error callback.
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.
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 SD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
DMA SD 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_SD_WriteBlocks_IT()

HAL_StatusTypeDef HAL_SD_WriteBlocks_IT ( SD_HandleTypeDef hsd,
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_SD_GetCardState().
You could also check the IT transfer process through the SD Tx interrupt event.
Parameters
hsdPointer to SD 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 1216 of file stm32l4xx_hal_sd.c.

1217 {
1218  SDMMC_DataInitTypeDef config;
1219  uint32_t errorstate;
1220  uint32_t add = BlockAdd;
1221 
1222  if(NULL == pData)
1223  {
1224  hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1225  return HAL_ERROR;
1226  }
1227 
1228  if(hsd->State == HAL_SD_STATE_READY)
1229  {
1230  hsd->ErrorCode = HAL_SD_ERROR_NONE;
1231 
1232  if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1233  {
1234  hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1235  return HAL_ERROR;
1236  }
1237 
1238  hsd->State = HAL_SD_STATE_BUSY;
1239 
1240  /* Initialize data control register */
1241  hsd->Instance->DCTRL = 0U;
1242 
1243  hsd->pTxBuffPtr = pData;
1244  hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
1245 
1246  /* Enable transfer interrupts */
1247  __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_FLAG_TXFIFOHE));
1248 
1249  if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1250  {
1251  add *= 512U;
1252  }
1253 
1254  /* Set Block Size for Card */
1255  errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1256  if(errorstate != HAL_SD_ERROR_NONE)
1257  {
1258  /* Clear all the static flags */
1259  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1260  hsd->ErrorCode |= errorstate;
1261  hsd->State = HAL_SD_STATE_READY;
1262  return HAL_ERROR;
1263  }
1264 
1265 #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
1266  /* Configure the SD DPSM (Data Path State Machine) */
1267  config.DataTimeOut = SDMMC_DATATIMEOUT;
1268  config.DataLength = BLOCKSIZE * NumberOfBlocks;
1269  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1270  config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1271  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1272  config.DPSM = SDMMC_DPSM_DISABLE;
1273  (void)SDMMC_ConfigData(hsd->Instance, &config);
1274 
1275  __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
1276 #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
1277 
1278  /* Write Blocks in Polling mode */
1279  if(NumberOfBlocks > 1U)
1280  {
1281  hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT);
1282 
1283  /* Write Multi Block command */
1284  errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1285  }
1286  else
1287  {
1288  hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
1289 
1290  /* Write Single Block command */
1291  errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1292  }
1293  if(errorstate != HAL_SD_ERROR_NONE)
1294  {
1295  /* Clear all the static flags */
1296  __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1297  hsd->ErrorCode |= errorstate;
1298  hsd->State = HAL_SD_STATE_READY;
1299  hsd->Context = SD_CONTEXT_NONE;
1300  return HAL_ERROR;
1301  }
1302 
1303 #if !defined(STM32L4R5xx) && !defined(STM32L4R7xx) && !defined(STM32L4R9xx) && !defined(STM32L4S5xx) && !defined(STM32L4S7xx) && !defined(STM32L4S9xx)
1304  /* Configure the SD DPSM (Data Path State Machine) */
1305  config.DataTimeOut = SDMMC_DATATIMEOUT;
1306  config.DataLength = BLOCKSIZE * NumberOfBlocks;
1307  config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1308  config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1309  config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1310  config.DPSM = SDMMC_DPSM_ENABLE;
1311  (void)SDMMC_ConfigData(hsd->Instance, &config);
1312 #endif /* !STM32L4R5xx && !STM32L4R7xx && !STM32L4R9xx && !STM32L4S5xx && !STM32L4S7xx && !STM32L4S9xx */
1313 
1314  return HAL_OK;
1315  }
1316  else
1317  {
1318  return HAL_BUSY;
1319  }
1320 }
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.