1 | /* USER CODE BEGIN Header */ |
---|
2 | /** |
---|
3 | ****************************************************************************** |
---|
4 | * @file bsp_driver_sd.c for H7 (based on stm32h743i_eval_sd.c) |
---|
5 | * @brief This file includes a generic uSD card driver. |
---|
6 | * To be completed by the user according to the board used for the project. |
---|
7 | * @note Some functions generated as weak: they can be overridden by |
---|
8 | * - code in user files |
---|
9 | * - or BSP code from the FW pack files |
---|
10 | * if such files are added to the generated project (by the user). |
---|
11 | ****************************************************************************** |
---|
12 | * @attention |
---|
13 | * |
---|
14 | * Copyright (c) 2025 STMicroelectronics. |
---|
15 | * All rights reserved. |
---|
16 | * |
---|
17 | * This software is licensed under terms that can be found in the LICENSE file |
---|
18 | * in the root directory of this software component. |
---|
19 | * If no LICENSE file comes with this software, it is provided AS-IS. |
---|
20 | * |
---|
21 | ****************************************************************************** |
---|
22 | */ |
---|
23 | /* USER CODE END Header */ |
---|
24 | |
---|
25 | /* USER CODE BEGIN FirstSection */ |
---|
26 | /* can be used to modify / undefine following code or add new definitions */ |
---|
27 | /* USER CODE END FirstSection */ |
---|
28 | /* Includes ------------------------------------------------------------------*/ |
---|
29 | #include "bsp_driver_sd.h" |
---|
30 | |
---|
31 | /* Extern variables ---------------------------------------------------------*/ |
---|
32 | |
---|
33 | extern SD_HandleTypeDef hsd1; |
---|
34 | |
---|
35 | /* USER CODE BEGIN BeforeInitSection */ |
---|
36 | /* can be used to modify / undefine following code or add code */ |
---|
37 | /* USER CODE END BeforeInitSection */ |
---|
38 | /** |
---|
39 | * @brief Initializes the SD card device. |
---|
40 | * @retval SD status |
---|
41 | */ |
---|
42 | __weak uint8_t BSP_SD_Init(void) |
---|
43 | { |
---|
44 | uint8_t sd_state = MSD_OK; |
---|
45 | /* Check if the SD card is plugged in the slot */ |
---|
46 | if (BSP_SD_IsDetected() != SD_PRESENT) |
---|
47 | { |
---|
48 | return MSD_ERROR_SD_NOT_PRESENT; |
---|
49 | } |
---|
50 | /* HAL SD initialization */ |
---|
51 | sd_state = HAL_SD_Init(&hsd1); |
---|
52 | /* Configure SD Bus width (4 bits mode selected) */ |
---|
53 | if (sd_state == MSD_OK) |
---|
54 | { |
---|
55 | /* Enable wide operation */ |
---|
56 | if (HAL_SD_ConfigWideBusOperation(&hsd1, SDMMC_BUS_WIDE_4B) != HAL_OK) |
---|
57 | { |
---|
58 | sd_state = MSD_ERROR; |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | return sd_state; |
---|
63 | } |
---|
64 | /* USER CODE BEGIN AfterInitSection */ |
---|
65 | /* can be used to modify previous code / undefine following code / add code */ |
---|
66 | /* USER CODE END AfterInitSection */ |
---|
67 | |
---|
68 | /* USER CODE BEGIN InterruptMode */ |
---|
69 | /** |
---|
70 | * @brief Configures Interrupt mode for SD detection pin. |
---|
71 | * @retval Returns 0 |
---|
72 | */ |
---|
73 | __weak uint8_t BSP_SD_ITConfig(void) |
---|
74 | { |
---|
75 | /* Code to be updated by the user or replaced by one from the FW pack (in a stmxxxx_sd.c file) */ |
---|
76 | |
---|
77 | return (uint8_t)0; |
---|
78 | } |
---|
79 | |
---|
80 | /* USER CODE END InterruptMode */ |
---|
81 | |
---|
82 | /* USER CODE BEGIN BeforeReadBlocksSection */ |
---|
83 | /* can be used to modify previous code / undefine following code / add code */ |
---|
84 | /* USER CODE END BeforeReadBlocksSection */ |
---|
85 | /** |
---|
86 | * @brief Reads block(s) from a specified address in an SD card, in polling mode. |
---|
87 | * @param pData: Pointer to the buffer that will contain the data to transmit |
---|
88 | * @param ReadAddr: Address from where data is to be read |
---|
89 | * @param NumOfBlocks: Number of SD blocks to read |
---|
90 | * @param Timeout: Timeout for read operation |
---|
91 | * @retval SD status |
---|
92 | */ |
---|
93 | __weak uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout) |
---|
94 | { |
---|
95 | uint8_t sd_state = MSD_OK; |
---|
96 | |
---|
97 | if (HAL_SD_ReadBlocks(&hsd1, (uint8_t *)pData, ReadAddr, NumOfBlocks, Timeout) != HAL_OK) |
---|
98 | { |
---|
99 | sd_state = MSD_ERROR; |
---|
100 | } |
---|
101 | |
---|
102 | return sd_state; |
---|
103 | } |
---|
104 | |
---|
105 | /* USER CODE BEGIN BeforeWriteBlocksSection */ |
---|
106 | /* can be used to modify previous code / undefine following code / add code */ |
---|
107 | /* USER CODE END BeforeWriteBlocksSection */ |
---|
108 | /** |
---|
109 | * @brief Writes block(s) to a specified address in an SD card, in polling mode. |
---|
110 | * @param pData: Pointer to the buffer that will contain the data to transmit |
---|
111 | * @param WriteAddr: Address from where data is to be written |
---|
112 | * @param NumOfBlocks: Number of SD blocks to write |
---|
113 | * @param Timeout: Timeout for write operation |
---|
114 | * @retval SD status |
---|
115 | */ |
---|
116 | __weak uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout) |
---|
117 | { |
---|
118 | uint8_t sd_state = MSD_OK; |
---|
119 | |
---|
120 | if (HAL_SD_WriteBlocks(&hsd1, (uint8_t *)pData, WriteAddr, NumOfBlocks, Timeout) != HAL_OK) |
---|
121 | { |
---|
122 | sd_state = MSD_ERROR; |
---|
123 | } |
---|
124 | |
---|
125 | return sd_state; |
---|
126 | } |
---|
127 | |
---|
128 | /* USER CODE BEGIN BeforeReadDMABlocksSection */ |
---|
129 | /* can be used to modify previous code / undefine following code / add code */ |
---|
130 | /* USER CODE END BeforeReadDMABlocksSection */ |
---|
131 | /** |
---|
132 | * @brief Reads block(s) from a specified address in an SD card, in DMA mode. |
---|
133 | * @param pData: Pointer to the buffer that will contain the data to transmit |
---|
134 | * @param ReadAddr: Address from where data is to be read |
---|
135 | * @param NumOfBlocks: Number of SD blocks to read |
---|
136 | * @retval SD status |
---|
137 | */ |
---|
138 | __weak uint8_t BSP_SD_ReadBlocks_DMA(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks) |
---|
139 | { |
---|
140 | uint8_t sd_state = MSD_OK; |
---|
141 | |
---|
142 | /* Read block(s) in DMA transfer mode */ |
---|
143 | if (HAL_SD_ReadBlocks_DMA(&hsd1, (uint8_t *)pData, ReadAddr, NumOfBlocks) != HAL_OK) |
---|
144 | { |
---|
145 | sd_state = MSD_ERROR; |
---|
146 | } |
---|
147 | |
---|
148 | return sd_state; |
---|
149 | } |
---|
150 | |
---|
151 | /* USER CODE BEGIN BeforeWriteDMABlocksSection */ |
---|
152 | /* can be used to modify previous code / undefine following code / add code */ |
---|
153 | /* USER CODE END BeforeWriteDMABlocksSection */ |
---|
154 | /** |
---|
155 | * @brief Writes block(s) to a specified address in an SD card, in DMA mode. |
---|
156 | * @param pData: Pointer to the buffer that will contain the data to transmit |
---|
157 | * @param WriteAddr: Address from where data is to be written |
---|
158 | * @param NumOfBlocks: Number of SD blocks to write |
---|
159 | * @retval SD status |
---|
160 | */ |
---|
161 | __weak uint8_t BSP_SD_WriteBlocks_DMA(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks) |
---|
162 | { |
---|
163 | uint8_t sd_state = MSD_OK; |
---|
164 | |
---|
165 | /* Write block(s) in DMA transfer mode */ |
---|
166 | if (HAL_SD_WriteBlocks_DMA(&hsd1, (uint8_t *)pData, WriteAddr, NumOfBlocks) != HAL_OK) |
---|
167 | { |
---|
168 | sd_state = MSD_ERROR; |
---|
169 | } |
---|
170 | |
---|
171 | return sd_state; |
---|
172 | } |
---|
173 | |
---|
174 | /* USER CODE BEGIN BeforeEraseSection */ |
---|
175 | /* can be used to modify previous code / undefine following code / add code */ |
---|
176 | /* USER CODE END BeforeEraseSection */ |
---|
177 | /** |
---|
178 | * @brief Erases the specified memory area of the given SD card. |
---|
179 | * @param StartAddr: Start byte address |
---|
180 | * @param EndAddr: End byte address |
---|
181 | * @retval SD status |
---|
182 | */ |
---|
183 | __weak uint8_t BSP_SD_Erase(uint32_t StartAddr, uint32_t EndAddr) |
---|
184 | { |
---|
185 | uint8_t sd_state = MSD_OK; |
---|
186 | |
---|
187 | if (HAL_SD_Erase(&hsd1, StartAddr, EndAddr) != HAL_OK) |
---|
188 | { |
---|
189 | sd_state = MSD_ERROR; |
---|
190 | } |
---|
191 | |
---|
192 | return sd_state; |
---|
193 | } |
---|
194 | |
---|
195 | /* USER CODE BEGIN BeforeGetCardStateSection */ |
---|
196 | /* can be used to modify previous code / undefine following code / add code */ |
---|
197 | /* USER CODE END BeforeGetCardStateSection */ |
---|
198 | |
---|
199 | /** |
---|
200 | * @brief Gets the current SD card data status. |
---|
201 | * @param None |
---|
202 | * @retval Data transfer state. |
---|
203 | * This value can be one of the following values: |
---|
204 | * @arg SD_TRANSFER_OK: No data transfer is acting |
---|
205 | * @arg SD_TRANSFER_BUSY: Data transfer is acting |
---|
206 | */ |
---|
207 | __weak uint8_t BSP_SD_GetCardState(void) |
---|
208 | { |
---|
209 | return ((HAL_SD_GetCardState(&hsd1) == HAL_SD_CARD_TRANSFER ) ? SD_TRANSFER_OK : SD_TRANSFER_BUSY); |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * @brief Get SD information about specific SD card. |
---|
214 | * @param CardInfo: Pointer to HAL_SD_CardInfoTypedef structure |
---|
215 | * @retval None |
---|
216 | */ |
---|
217 | __weak void BSP_SD_GetCardInfo(HAL_SD_CardInfoTypeDef *CardInfo) |
---|
218 | { |
---|
219 | /* Get SD card Information */ |
---|
220 | HAL_SD_GetCardInfo(&hsd1, CardInfo); |
---|
221 | } |
---|
222 | |
---|
223 | /* USER CODE BEGIN BeforeCallBacksSection */ |
---|
224 | /* can be used to modify previous code / undefine following code / add code */ |
---|
225 | /* USER CODE END BeforeCallBacksSection */ |
---|
226 | /** |
---|
227 | * @brief SD Abort callbacks |
---|
228 | * @param hsd: SD handle |
---|
229 | * @retval None |
---|
230 | */ |
---|
231 | void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd) |
---|
232 | { |
---|
233 | BSP_SD_AbortCallback(); |
---|
234 | } |
---|
235 | |
---|
236 | /** |
---|
237 | * @brief Tx Transfer completed callback |
---|
238 | * @param hsd: SD handle |
---|
239 | * @retval None |
---|
240 | */ |
---|
241 | void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd) |
---|
242 | { |
---|
243 | BSP_SD_WriteCpltCallback(); |
---|
244 | } |
---|
245 | |
---|
246 | /** |
---|
247 | * @brief Rx Transfer completed callback |
---|
248 | * @param hsd: SD handle |
---|
249 | * @retval None |
---|
250 | */ |
---|
251 | void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd) |
---|
252 | { |
---|
253 | BSP_SD_ReadCpltCallback(); |
---|
254 | } |
---|
255 | |
---|
256 | /* USER CODE BEGIN CallBacksSection_C */ |
---|
257 | /** |
---|
258 | * @brief BSP SD Abort callback |
---|
259 | * @retval None |
---|
260 | * @note empty (up to the user to fill it in or to remove it if useless) |
---|
261 | */ |
---|
262 | __weak void BSP_SD_AbortCallback(void) |
---|
263 | { |
---|
264 | |
---|
265 | } |
---|
266 | |
---|
267 | /** |
---|
268 | * @brief BSP Tx Transfer completed callback |
---|
269 | * @retval None |
---|
270 | * @note empty (up to the user to fill it in or to remove it if useless) |
---|
271 | */ |
---|
272 | __weak void BSP_SD_WriteCpltCallback(void) |
---|
273 | { |
---|
274 | |
---|
275 | } |
---|
276 | |
---|
277 | /** |
---|
278 | * @brief BSP Rx Transfer completed callback |
---|
279 | * @retval None |
---|
280 | * @note empty (up to the user to fill it in or to remove it if useless) |
---|
281 | */ |
---|
282 | __weak void BSP_SD_ReadCpltCallback(void) |
---|
283 | { |
---|
284 | |
---|
285 | } |
---|
286 | /* USER CODE END CallBacksSection_C */ |
---|
287 | |
---|
288 | /** |
---|
289 | * @brief Detects if SD card is correctly plugged in the memory slot or not. |
---|
290 | * @param None |
---|
291 | * @retval Returns if SD is detected or not |
---|
292 | */ |
---|
293 | __weak uint8_t BSP_SD_IsDetected(void) |
---|
294 | { |
---|
295 | __IO uint8_t status = SD_PRESENT; |
---|
296 | |
---|
297 | if (BSP_PlatformIsDetected() == 0x0) |
---|
298 | { |
---|
299 | status = SD_NOT_PRESENT; |
---|
300 | } |
---|
301 | |
---|
302 | return status; |
---|
303 | } |
---|
304 | |
---|
305 | /* USER CODE BEGIN AdditionalCode */ |
---|
306 | /* user code can be inserted here */ |
---|
307 | /* USER CODE END AdditionalCode */ |
---|