2 ******************************************************************************
3 * @file bsp_driver_sd.c (based on stm324x9i_eval_sd.c)
4 * @brief This file includes a generic uSD card driver.
5 ******************************************************************************
7 * COPYRIGHT(c) 2016 STMicroelectronics
9 * Redistribution and use in source and binary forms, with or without modification,
10 * are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * 3. Neither the name of STMicroelectronics nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ******************************************************************************
34 /* USER CODE BEGIN 0 */
35 /* Includes ------------------------------------------------------------------*/
36 #include "bsp_driver_sd.h"
38 /* Extern variables ---------------------------------------------------------*/
40 extern SD_HandleTypeDef hsd;
41 extern HAL_SD_CardInfoTypedef SDCardInfo;
44 * @brief Initializes the SD card device.
48 uint8_t BSP_SD_Init(void)
50 uint8_t SD_state = MSD_OK;
51 /* Check if the SD card is plugged in the slot */
52 if (BSP_SD_IsDetected() != SD_PRESENT)
56 SD_state = HAL_SD_Init(&hsd, &SDCardInfo);
58 if (SD_state == MSD_OK)
60 if (HAL_SD_WideBusOperation_Config(&hsd, SDIO_BUS_WIDE_4B) != SD_OK)
74 * @brief Configures Interrupt mode for SD detection pin.
76 * @retval Returns 0 in success otherwise 1.
78 uint8_t BSP_SD_ITConfig(void)
80 /* TBI: add user code here depending on the hardware configuration used */
85 /** @brief SD detect IT treatment
89 void BSP_SD_DetectIT(void)
91 /* TBI: add user code here depending on the hardware configuration used */
94 /** @brief SD detect IT detection callback
98 __weak void BSP_SD_DetectCallback(void)
100 /* NOTE: This function Should not be modified, when the callback is needed,
101 the SD_DetectCallback could be implemented in the user file
107 * @brief Reads block(s) from a specified address in an SD card, in polling mode.
108 * @param pData: Pointer to the buffer that will contain the data to transmit
109 * @param ReadAddr: Address from where data is to be read
110 * @param BlockSize: SD card data block size, that should be 512
111 * @param NumOfBlocks: Number of SD blocks to read
114 uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumOfBlocks)
116 if(HAL_SD_ReadBlocks_DMA(&hsd, pData, ReadAddr, BlockSize, NumOfBlocks) != SD_OK)
127 * @brief Writes block(s) to a specified address in an SD card, in polling mode.
128 * @param pData: Pointer to the buffer that will contain the data to transmit
129 * @param WriteAddr: Address from where data is to be written
130 * @param BlockSize: SD card data block size, that should be 512
131 * @param NumOfBlocks: Number of SD blocks to write
134 uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumOfBlocks)
136 if(HAL_SD_WriteBlocks_DMA(&hsd, pData, WriteAddr, BlockSize, NumOfBlocks) != SD_OK)
147 * @brief Reads block(s) from a specified address in an SD card, in DMA mode.
148 * @param pData: Pointer to the buffer that will contain the data to transmit
149 * @param ReadAddr: Address from where data is to be read
150 * @param BlockSize: SD card data block size, that should be 512
151 * @param NumOfBlocks: Number of SD blocks to read
154 uint8_t BSP_SD_ReadBlocks_DMA(uint32_t *pData, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumOfBlocks)
156 uint8_t SD_state = MSD_OK;
158 /* Read block(s) in DMA transfer mode */
159 if(HAL_SD_ReadBlocks_DMA(&hsd, pData, ReadAddr, BlockSize, NumOfBlocks) != SD_OK)
161 SD_state = MSD_ERROR;
164 /* Wait until transfer is complete */
165 if(SD_state == MSD_OK)
167 if(HAL_SD_CheckReadOperation(&hsd, (uint32_t)SD_DATATIMEOUT) != SD_OK)
169 SD_state = MSD_ERROR;
181 * @brief Writes block(s) to a specified address in an SD card, in DMA mode.
182 * @param pData: Pointer to the buffer that will contain the data to transmit
183 * @param WriteAddr: Address from where data is to be written
184 * @param BlockSize: SD card data block size, that should be 512
185 * @param NumOfBlocks: Number of SD blocks to write
188 uint8_t BSP_SD_WriteBlocks_DMA(uint32_t *pData, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumOfBlocks)
190 uint8_t SD_state = SD_OK;
192 /* Write block(s) in DMA transfer mode */
193 if(HAL_SD_WriteBlocks_DMA(&hsd, pData, WriteAddr, BlockSize, NumOfBlocks) != SD_OK)
195 SD_state = MSD_ERROR;
198 /* Wait until transfer is complete */
199 if(SD_state == MSD_OK)
201 if(HAL_SD_CheckWriteOperation(&hsd, (uint32_t)SD_DATATIMEOUT) != SD_OK)
203 SD_state = MSD_ERROR;
215 * @brief Erases the specified memory area of the given SD card.
216 * @param StartAddr: Start byte address
217 * @param EndAddr: End byte address
220 uint8_t BSP_SD_Erase(uint64_t StartAddr, uint64_t EndAddr)
222 if(HAL_SD_Erase(&hsd, StartAddr, EndAddr) != SD_OK)
233 * @brief Handles SD card interrupt request.
237 void BSP_SD_IRQHandler(void)
239 HAL_SD_IRQHandler(&hsd);
243 * @brief Handles SD DMA Tx transfer interrupt request.
247 void BSP_SD_DMA_Tx_IRQHandler(void)
249 HAL_DMA_IRQHandler(hsd.hdmatx);
253 * @brief Handles SD DMA Rx transfer interrupt request.
257 void BSP_SD_DMA_Rx_IRQHandler(void)
259 HAL_DMA_IRQHandler(hsd.hdmarx);
263 * @brief Gets the current SD card data status.
265 * @retval Data transfer state.
266 * This value can be one of the following values:
267 * @arg SD_TRANSFER_OK: No data transfer is acting
268 * @arg SD_TRANSFER_BUSY: Data transfer is acting
269 * @arg SD_TRANSFER_ERROR: Data transfer error
271 HAL_SD_TransferStateTypedef BSP_SD_GetStatus(void)
273 return(HAL_SD_GetStatus(&hsd));
277 * @brief Get SD information about specific SD card.
278 * @param CardInfo: Pointer to HAL_SD_CardInfoTypedef structure
281 void BSP_SD_GetCardInfo(HAL_SD_CardInfoTypedef* CardInfo)
283 /* Get SD card Information */
284 HAL_SD_Get_CardInfo(&hsd, CardInfo);
286 /* USER CODE END 0 */
289 * @brief Detects if SD card is correctly plugged in the memory slot or not.
291 * @retval Returns if SD is detected or not
293 uint8_t BSP_SD_IsDetected(void)
295 __IO uint8_t status = SD_PRESENT;
297 /* USER CODE BEGIN 1 */
298 /* user code can be inserted here */
299 /* USER CODE END 1 */
304 /* USER CODE BEGIN AdditionalCode */
305 /* user code can be inserted here */
306 /* USER CODE END AdditionalCode */
308 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/