Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /** |
2 | ****************************************************************************** |
||
3 | * @file stm32f1xx_hal_sd.c |
||
4 | * @author MCD Application Team |
||
5 | mjames | 5 | * @version V1.0.4 |
6 | * @date 29-April-2016 |
||
2 | mjames | 7 | * @brief SD card HAL module driver. |
8 | * This file provides firmware functions to manage the following |
||
9 | * functionalities of the Secure Digital (SD) peripheral: |
||
10 | * + Initialization and de-initialization functions |
||
11 | * + IO operation functions |
||
12 | * + Peripheral Control functions |
||
13 | * + Peripheral State functions |
||
14 | * |
||
15 | @verbatim |
||
16 | ============================================================================== |
||
17 | ##### How to use this driver ##### |
||
18 | ============================================================================== |
||
19 | [..] |
||
20 | This driver implements a high level communication layer for read and write from/to |
||
21 | this memory. The needed STM32 hardware resources (SDIO and GPIO) are performed by |
||
22 | the user in HAL_SD_MspInit() function (MSP layer). |
||
23 | Basically, the MSP layer configuration should be the same as we provide in the |
||
24 | examples. |
||
25 | You can easily tailor this configuration according to hardware resources. |
||
26 | |||
27 | [..] |
||
28 | This driver is a generic layered driver for SDIO memories which uses the HAL |
||
29 | SDIO driver functions to interface with SD and uSD cards devices. |
||
30 | It is used as follows: |
||
31 | |||
32 | (#)Initialize the SDIO low level resources by implement the HAL_SD_MspInit() API: |
||
33 | (##) Enable the SDIO interface clock using __HAL_RCC_SDIO_CLK_ENABLE(); |
||
34 | (##) SDIO pins configuration for SD card |
||
35 | (+++) Enable the clock for the SDIO GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE(); |
||
36 | (+++) Configure these SDIO pins as alternate function pull-up using HAL_GPIO_Init() |
||
37 | and according to your pin assignment; |
||
38 | (##) DMA Configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA() |
||
39 | and HAL_SD_WriteBlocks_DMA() APIs). |
||
40 | (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE(); |
||
41 | (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled. |
||
42 | (##) NVIC configuration if you need to use interrupt process when using DMA transfer. |
||
43 | (+++) Configure the SDIO and DMA interrupt priorities using functions |
||
44 | HAL_NVIC_SetPriority(); DMA priority is superior to SDIO's priority |
||
45 | (+++) Enable the NVIC DMA and SDIO IRQs using function HAL_NVIC_EnableIRQ() |
||
46 | (+++) SDIO interrupts are managed using the macros __HAL_SD_SDIO_ENABLE_IT() |
||
47 | and __HAL_SD_SDIO_DISABLE_IT() inside the communication process. |
||
48 | (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_SDIO_GET_IT() |
||
49 | and __HAL_SD_SDIO_CLEAR_IT() |
||
50 | (#) At this stage, you can perform SD read/write/erase operations after SD card initialization |
||
51 | |||
52 | |||
53 | *** SD Card Initialization and configuration *** |
||
54 | ================================================ |
||
55 | [..] |
||
56 | To initialize the SD Card, use the HAL_SD_Init() function. It Initializes |
||
57 | the SD Card and put it into StandBy State (Ready for data transfer). |
||
58 | This function provide the following operations: |
||
59 | |||
60 | (#) Apply the SD Card initialization process at 400KHz and check the SD Card |
||
61 | type (Standard Capacity or High Capacity). You can change or adapt this |
||
62 | frequency by adjusting the "ClockDiv" field. |
||
63 | The SD Card frequency (SDIO_CK) is computed as follows: |
||
64 | |||
65 | SDIO_CK = SDIOCLK / (ClockDiv + 2) |
||
66 | |||
67 | In initialization mode and according to the SD Card standard, |
||
68 | make sure that the SDIO_CK frequency doesn't exceed 400KHz. |
||
69 | |||
70 | (#) Get the SD CID and CSD data. All these information are managed by the SDCardInfo |
||
71 | structure. This structure provide also ready computed SD Card capacity |
||
72 | and Block size. |
||
73 | |||
74 | -@- These information are stored in SD handle structure in case of future use. |
||
75 | |||
76 | (#) Configure the SD Card Data transfer frequency. The card transfer |
||
77 | frequency is set to SDIOCLK / (SDIO_TRANSFER_CLK_DIV + 2). You can change or adapt this frequency by adjusting |
||
78 | the "ClockDiv" field. |
||
79 | The SD Card frequency (SDIO_CK) is computed as follows: |
||
80 | |||
81 | SDIO_CK = SDIOCLK / (ClockDiv + 2) |
||
82 | |||
83 | In transfer mode and according to the SD Card standard, make sure that the |
||
84 | SDIO_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch. |
||
85 | |||
86 | (#) Select the corresponding SD Card according to the address read with the step 2. |
||
87 | |||
88 | (#) Configure the SD Card in wide bus mode: 4-bits data. |
||
89 | |||
90 | *** SD Card Read operation *** |
||
91 | ============================== |
||
92 | [..] |
||
93 | (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks(). |
||
94 | This function support only 512-bytes block length (the block size should be |
||
95 | chosen as 512 bytes). |
||
96 | You can choose either one block read operation or multiple block read operation |
||
97 | by adjusting the "NumberOfBlocks" parameter. |
||
98 | |||
99 | (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA(). |
||
100 | This function support only 512-bytes block length (the block size should be |
||
101 | chosen as 512 bytes). |
||
102 | You can choose either one block read operation or multiple block read operation |
||
103 | by adjusting the "NumberOfBlocks" parameter. |
||
104 | After this, you have to call the function HAL_SD_CheckReadOperation(), to insure |
||
105 | that the read transfer is done correctly in both DMA and SD sides. |
||
106 | |||
107 | *** SD Card Write operation *** |
||
108 | =============================== |
||
109 | [..] |
||
110 | (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks(). |
||
111 | This function support only 512-bytes block length (the block size should be |
||
112 | chosen as 512 bytes). |
||
113 | You can choose either one block read operation or multiple block read operation |
||
114 | by adjusting the "NumberOfBlocks" parameter. |
||
115 | |||
116 | (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA(). |
||
117 | This function support only 512-bytes block length (the block size should be |
||
118 | chosen as 512 byte). |
||
119 | You can choose either one block read operation or multiple block read operation |
||
120 | by adjusting the "NumberOfBlocks" parameter. |
||
121 | After this, you have to call the function HAL_SD_CheckWriteOperation(), to insure |
||
122 | that the write transfer is done correctly in both DMA and SD sides. |
||
123 | |||
124 | *** SD card status *** |
||
125 | ====================== |
||
126 | [..] |
||
127 | (+) At any time, you can check the SD Card status and get the SD card state |
||
128 | by using the HAL_SD_GetStatus() function. This function checks first if the |
||
129 | SD card is still connected and then get the internal SD Card transfer state. |
||
130 | (+) You can also get the SD card SD Status register by using the HAL_SD_SendSDStatus() |
||
131 | function. |
||
132 | |||
133 | *** SD HAL driver macros list *** |
||
134 | ================================== |
||
135 | [..] |
||
136 | Below the list of most used macros in SD HAL driver. |
||
137 | (+) __HAL_SD_SDIO_ENABLE : Enable the SD device |
||
138 | (+) __HAL_SD_SDIO_DISABLE : Disable the SD device |
||
139 | (+) __HAL_SD_SDIO_DMA_ENABLE: Enable the SDIO DMA transfer |
||
140 | (+) __HAL_SD_SDIO_DMA_DISABLE: Disable the SDIO DMA transfer |
||
141 | (+) __HAL_SD_SDIO_ENABLE_IT: Enable the SD device interrupt |
||
142 | (+) __HAL_SD_SDIO_DISABLE_IT: Disable the SD device interrupt |
||
143 | (+) __HAL_SD_SDIO_GET_FLAG:Check whether the specified SD flag is set or not |
||
144 | (+) __HAL_SD_SDIO_CLEAR_FLAG: Clear the SD's pending flags |
||
145 | |||
146 | -@- You can refer to the SD HAL driver header file for more useful macros |
||
147 | |||
148 | @endverbatim |
||
149 | ****************************************************************************** |
||
150 | * @attention |
||
151 | * |
||
5 | mjames | 152 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
2 | mjames | 153 | * |
154 | * Redistribution and use in source and binary forms, with or without modification, |
||
155 | * are permitted provided that the following conditions are met: |
||
156 | * 1. Redistributions of source code must retain the above copyright notice, |
||
157 | * this list of conditions and the following disclaimer. |
||
158 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
||
159 | * this list of conditions and the following disclaimer in the documentation |
||
160 | * and/or other materials provided with the distribution. |
||
161 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
||
162 | * may be used to endorse or promote products derived from this software |
||
163 | * without specific prior written permission. |
||
164 | * |
||
165 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
166 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
167 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
168 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
||
169 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||
170 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
||
171 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||
172 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
||
173 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
174 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
175 | * |
||
176 | ****************************************************************************** |
||
177 | */ |
||
178 | |||
179 | /* Includes ------------------------------------------------------------------*/ |
||
180 | #include "stm32f1xx_hal.h" |
||
181 | |||
182 | #ifdef HAL_SD_MODULE_ENABLED |
||
183 | |||
184 | #if defined(STM32F103xE) || defined(STM32F103xG) |
||
185 | |||
186 | /** @addtogroup STM32F1xx_HAL_Driver |
||
187 | * @{ |
||
188 | */ |
||
189 | |||
190 | /** @defgroup SD SD |
||
191 | * @brief SD HAL module driver |
||
192 | * @{ |
||
193 | */ |
||
194 | |||
195 | /* Private typedef -----------------------------------------------------------*/ |
||
196 | /* Private define ------------------------------------------------------------*/ |
||
197 | |||
198 | /** @defgroup SD_Private_Define SD Private Constant |
||
199 | * @{ |
||
200 | */ |
||
201 | /** |
||
202 | * @brief SDIO Data block size |
||
203 | */ |
||
204 | #define DATA_BLOCK_SIZE ((uint32_t)(9 << 4)) |
||
205 | /** |
||
206 | * @brief SDIO Static flags, TimeOut, FIFO Address |
||
207 | */ |
||
208 | #define SDIO_STATIC_FLAGS ((uint32_t)(SDIO_FLAG_CCRCFAIL | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_CTIMEOUT |\ |
||
209 | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_TXUNDERR | SDIO_FLAG_RXOVERR |\ |
||
210 | SDIO_FLAG_CMDREND | SDIO_FLAG_CMDSENT | SDIO_FLAG_DATAEND |\ |
||
211 | SDIO_FLAG_DBCKEND)) |
||
212 | |||
213 | #define SDIO_CMD0TIMEOUT ((uint32_t)0x00010000) |
||
214 | |||
215 | /** |
||
216 | * @brief Mask for errors Card Status R1 (OCR Register) |
||
217 | */ |
||
218 | #define SD_OCR_ADDR_OUT_OF_RANGE ((uint32_t)0x80000000) |
||
219 | #define SD_OCR_ADDR_MISALIGNED ((uint32_t)0x40000000) |
||
220 | #define SD_OCR_BLOCK_LEN_ERR ((uint32_t)0x20000000) |
||
221 | #define SD_OCR_ERASE_SEQ_ERR ((uint32_t)0x10000000) |
||
222 | #define SD_OCR_BAD_ERASE_PARAM ((uint32_t)0x08000000) |
||
223 | #define SD_OCR_WRITE_PROT_VIOLATION ((uint32_t)0x04000000) |
||
224 | #define SD_OCR_LOCK_UNLOCK_FAILED ((uint32_t)0x01000000) |
||
225 | #define SD_OCR_COM_CRC_FAILED ((uint32_t)0x00800000) |
||
226 | #define SD_OCR_ILLEGAL_CMD ((uint32_t)0x00400000) |
||
227 | #define SD_OCR_CARD_ECC_FAILED ((uint32_t)0x00200000) |
||
228 | #define SD_OCR_CC_ERROR ((uint32_t)0x00100000) |
||
229 | #define SD_OCR_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00080000) |
||
230 | #define SD_OCR_STREAM_READ_UNDERRUN ((uint32_t)0x00040000) |
||
231 | #define SD_OCR_STREAM_WRITE_OVERRUN ((uint32_t)0x00020000) |
||
232 | #define SD_OCR_CID_CSD_OVERWRITE ((uint32_t)0x00010000) |
||
233 | #define SD_OCR_WP_ERASE_SKIP ((uint32_t)0x00008000) |
||
234 | #define SD_OCR_CARD_ECC_DISABLED ((uint32_t)0x00004000) |
||
235 | #define SD_OCR_ERASE_RESET ((uint32_t)0x00002000) |
||
236 | #define SD_OCR_AKE_SEQ_ERROR ((uint32_t)0x00000008) |
||
237 | #define SD_OCR_ERRORBITS ((uint32_t)0xFDFFE008) |
||
238 | |||
239 | /** |
||
240 | * @brief Masks for R6 Response |
||
241 | */ |
||
242 | #define SD_R6_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00002000) |
||
243 | #define SD_R6_ILLEGAL_CMD ((uint32_t)0x00004000) |
||
244 | #define SD_R6_COM_CRC_FAILED ((uint32_t)0x00008000) |
||
245 | |||
246 | #define SD_VOLTAGE_WINDOW_SD ((uint32_t)0x80100000) |
||
247 | #define SD_HIGH_CAPACITY ((uint32_t)0x40000000) |
||
248 | #define SD_STD_CAPACITY ((uint32_t)0x00000000) |
||
249 | #define SD_CHECK_PATTERN ((uint32_t)0x000001AA) |
||
250 | |||
251 | #define SD_MAX_VOLT_TRIAL ((uint32_t)0x0000FFFF) |
||
252 | #define SD_ALLZERO ((uint32_t)0x00000000) |
||
253 | |||
254 | #define SD_WIDE_BUS_SUPPORT ((uint32_t)0x00040000) |
||
255 | #define SD_SINGLE_BUS_SUPPORT ((uint32_t)0x00010000) |
||
256 | #define SD_CARD_LOCKED ((uint32_t)0x02000000) |
||
257 | |||
258 | #define SD_DATATIMEOUT ((uint32_t)0xFFFFFFFF) |
||
259 | #define SD_0TO7BITS ((uint32_t)0x000000FF) |
||
260 | #define SD_8TO15BITS ((uint32_t)0x0000FF00) |
||
261 | #define SD_16TO23BITS ((uint32_t)0x00FF0000) |
||
262 | #define SD_24TO31BITS ((uint32_t)0xFF000000) |
||
263 | #define SD_MAX_DATA_LENGTH ((uint32_t)0x01FFFFFF) |
||
264 | |||
265 | #define SD_HALFFIFO ((uint32_t)0x00000008) |
||
266 | #define SD_HALFFIFOBYTES ((uint32_t)0x00000020) |
||
267 | |||
268 | /** |
||
269 | * @brief Command Class Supported |
||
270 | */ |
||
271 | #define SD_CCCC_LOCK_UNLOCK ((uint32_t)0x00000080) |
||
272 | #define SD_CCCC_WRITE_PROT ((uint32_t)0x00000040) |
||
273 | #define SD_CCCC_ERASE ((uint32_t)0x00000020) |
||
274 | |||
275 | /** |
||
276 | * @brief Following commands are SD Card Specific commands. |
||
277 | * SDIO_APP_CMD should be sent before sending these commands. |
||
278 | */ |
||
279 | #define SD_SDIO_SEND_IF_COND ((uint32_t)SD_CMD_HS_SEND_EXT_CSD) |
||
280 | |||
281 | /** |
||
282 | * @} |
||
283 | */ |
||
284 | |||
285 | /* Private macro -------------------------------------------------------------*/ |
||
286 | /* Private variables ---------------------------------------------------------*/ |
||
287 | /* Private function prototypes -----------------------------------------------*/ |
||
288 | /* Private functions ---------------------------------------------------------*/ |
||
289 | |||
290 | /** @defgroup SD_Private_Functions SD Private Functions |
||
291 | * @{ |
||
292 | */ |
||
293 | |||
294 | static HAL_SD_ErrorTypedef SD_Initialize_Cards(SD_HandleTypeDef *hsd); |
||
295 | static HAL_SD_ErrorTypedef SD_Select_Deselect(SD_HandleTypeDef *hsd, uint64_t Addr); |
||
296 | static HAL_SD_ErrorTypedef SD_PowerON(SD_HandleTypeDef *hsd); |
||
297 | static HAL_SD_ErrorTypedef SD_PowerOFF(SD_HandleTypeDef *hsd); |
||
298 | static HAL_SD_ErrorTypedef SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus); |
||
299 | static HAL_SD_CardStateTypedef SD_GetState(SD_HandleTypeDef *hsd); |
||
300 | static HAL_SD_ErrorTypedef SD_IsCardProgramming(SD_HandleTypeDef *hsd, uint8_t *pStatus); |
||
301 | static HAL_SD_ErrorTypedef SD_CmdError(SD_HandleTypeDef *hsd); |
||
302 | static HAL_SD_ErrorTypedef SD_CmdResp1Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD); |
||
303 | static HAL_SD_ErrorTypedef SD_CmdResp7Error(SD_HandleTypeDef *hsd); |
||
304 | static HAL_SD_ErrorTypedef SD_CmdResp3Error(SD_HandleTypeDef *hsd); |
||
305 | static HAL_SD_ErrorTypedef SD_CmdResp2Error(SD_HandleTypeDef *hsd); |
||
306 | static HAL_SD_ErrorTypedef SD_CmdResp6Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD, uint16_t *pRCA); |
||
307 | static HAL_SD_ErrorTypedef SD_WideBus_Enable(SD_HandleTypeDef *hsd); |
||
308 | static HAL_SD_ErrorTypedef SD_WideBus_Disable(SD_HandleTypeDef *hsd); |
||
309 | static HAL_SD_ErrorTypedef SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR); |
||
310 | static void SD_DMA_RxCplt(DMA_HandleTypeDef *hdma); |
||
311 | static void SD_DMA_RxError(DMA_HandleTypeDef *hdma); |
||
312 | static void SD_DMA_TxCplt(DMA_HandleTypeDef *hdma); |
||
313 | static void SD_DMA_TxError(DMA_HandleTypeDef *hdma); |
||
314 | |||
315 | /** |
||
316 | * @} |
||
317 | */ |
||
318 | |||
319 | /** @defgroup SD_Exported_Functions SD Exported Functions |
||
320 | * @{ |
||
321 | */ |
||
322 | |||
323 | /** @defgroup SD_Exported_Functions_Group1 Initialization and de-initialization functions |
||
324 | * @brief Initialization and Configuration functions |
||
325 | * |
||
326 | @verbatim |
||
327 | =============================================================================== |
||
328 | ##### Initialization and de-initialization functions ##### |
||
329 | =============================================================================== |
||
330 | [..] |
||
331 | This section provides functions allowing to initialize/de-initialize the SD |
||
332 | card device to be ready for use. |
||
333 | |||
334 | |||
335 | @endverbatim |
||
336 | * @{ |
||
337 | */ |
||
338 | |||
339 | /** |
||
340 | * @brief Initializes the SD card according to the specified parameters in the |
||
341 | SD_HandleTypeDef and create the associated handle. |
||
342 | * @param hsd: SD handle |
||
343 | * @param SDCardInfo: HAL_SD_CardInfoTypedef structure for SD card information |
||
344 | * @retval HAL SD error state |
||
345 | */ |
||
346 | HAL_SD_ErrorTypedef HAL_SD_Init(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *SDCardInfo) |
||
347 | { |
||
348 | __IO HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
349 | SD_InitTypeDef tmpinit = {0}; |
||
350 | |||
351 | /* Initialize the low level hardware (MSP) */ |
||
352 | HAL_SD_MspInit(hsd); |
||
353 | |||
354 | /* Default SDIO peripheral configuration for SD card initialization */ |
||
355 | tmpinit.ClockEdge = SDIO_CLOCK_EDGE_RISING; |
||
356 | tmpinit.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE; |
||
357 | tmpinit.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE; |
||
358 | tmpinit.BusWide = SDIO_BUS_WIDE_1B; |
||
359 | tmpinit.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE; |
||
360 | tmpinit.ClockDiv = SDIO_INIT_CLK_DIV; |
||
361 | |||
362 | /* Initialize SDIO peripheral interface with default configuration */ |
||
363 | SDIO_Init(hsd->Instance, tmpinit); |
||
364 | |||
365 | /* Identify card operating voltage */ |
||
366 | errorstate = SD_PowerON(hsd); |
||
367 | |||
368 | if(errorstate != SD_OK) |
||
369 | { |
||
370 | return errorstate; |
||
371 | } |
||
372 | |||
373 | /* Initialize the present SDIO card(s) and put them in idle state */ |
||
374 | errorstate = SD_Initialize_Cards(hsd); |
||
375 | |||
376 | if (errorstate != SD_OK) |
||
377 | { |
||
378 | return errorstate; |
||
379 | } |
||
380 | |||
381 | /* Read CSD/CID MSD registers */ |
||
382 | errorstate = HAL_SD_Get_CardInfo(hsd, SDCardInfo); |
||
383 | |||
384 | if (errorstate == SD_OK) |
||
385 | { |
||
386 | /* Select the Card */ |
||
387 | errorstate = SD_Select_Deselect(hsd, (uint32_t)(((uint32_t)SDCardInfo->RCA) << 16)); |
||
388 | } |
||
389 | |||
390 | /* Configure SDIO peripheral interface */ |
||
391 | SDIO_Init(hsd->Instance, hsd->Init); |
||
392 | |||
393 | return errorstate; |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * @brief De-Initializes the SD card. |
||
398 | * @param hsd: SD handle |
||
399 | * @retval HAL status |
||
400 | */ |
||
401 | HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd) |
||
402 | { |
||
403 | |||
404 | /* Set SD power state to off */ |
||
405 | SD_PowerOFF(hsd); |
||
406 | |||
407 | /* De-Initialize the MSP layer */ |
||
408 | HAL_SD_MspDeInit(hsd); |
||
409 | |||
410 | return HAL_OK; |
||
411 | } |
||
412 | |||
413 | |||
414 | /** |
||
415 | * @brief Initializes the SD MSP. |
||
416 | * @param hsd: SD handle |
||
417 | * @retval None |
||
418 | */ |
||
419 | __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd) |
||
420 | { |
||
5 | mjames | 421 | /* Prevent unused argument(s) compilation warning */ |
422 | UNUSED(hsd); |
||
2 | mjames | 423 | /* NOTE : This function Should not be modified, when the callback is needed, |
424 | the HAL_SD_MspInit could be implemented in the user file |
||
425 | */ |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * @brief De-Initialize SD MSP. |
||
430 | * @param hsd: SD handle |
||
431 | * @retval None |
||
432 | */ |
||
433 | __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd) |
||
434 | { |
||
5 | mjames | 435 | /* Prevent unused argument(s) compilation warning */ |
436 | UNUSED(hsd); |
||
2 | mjames | 437 | /* NOTE : This function Should not be modified, when the callback is needed, |
438 | the HAL_SD_MspDeInit could be implemented in the user file |
||
439 | */ |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * @} |
||
444 | */ |
||
445 | |||
446 | /** @defgroup SD_Exported_Functions_Group2 IO operation functions |
||
447 | * @brief Data transfer functions |
||
448 | * |
||
449 | @verbatim |
||
450 | =============================================================================== |
||
451 | ##### IO operation functions ##### |
||
452 | =============================================================================== |
||
453 | [..] |
||
454 | This subsection provides a set of functions allowing to manage the data |
||
455 | transfer from/to SD card. |
||
456 | |||
457 | @endverbatim |
||
458 | * @{ |
||
459 | */ |
||
460 | |||
461 | /** |
||
462 | * @brief Reads block(s) from a specified address in a card. The Data transfer |
||
463 | * is managed by polling mode. |
||
464 | * @param hsd: SD handle |
||
465 | * @param pReadBuffer: pointer to the buffer that will contain the received data |
||
466 | * @param ReadAddr: Address from where data is to be read |
||
467 | * @param BlockSize: SD card Data block size (in bytes) |
||
468 | * This parameter should be 512 |
||
469 | * @param NumberOfBlocks: Number of SD blocks to read |
||
470 | * @retval SD Card error state |
||
471 | */ |
||
472 | HAL_SD_ErrorTypedef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks) |
||
473 | { |
||
474 | SDIO_CmdInitTypeDef sdio_cmdinitstructure = {0}; |
||
475 | SDIO_DataInitTypeDef sdio_datainitstructure = {0}; |
||
476 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
477 | uint32_t count = 0, *tempbuff = (uint32_t *)pReadBuffer; |
||
478 | |||
479 | /* Initialize data control register */ |
||
480 | hsd->Instance->DCTRL = 0; |
||
481 | |||
482 | if (hsd->CardType == HIGH_CAPACITY_SD_CARD) |
||
483 | { |
||
484 | BlockSize = 512; |
||
485 | ReadAddr /= 512; |
||
486 | } |
||
487 | |||
488 | /* Set Block Size for Card */ |
||
489 | sdio_cmdinitstructure.Argument = (uint32_t) BlockSize; |
||
490 | sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN; |
||
491 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT; |
||
492 | sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO; |
||
493 | sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE; |
||
494 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
495 | |||
496 | /* Check for error conditions */ |
||
497 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN); |
||
498 | |||
499 | if (errorstate != SD_OK) |
||
500 | { |
||
501 | return errorstate; |
||
502 | } |
||
503 | |||
504 | /* Configure the SD DPSM (Data Path State Machine) */ |
||
505 | sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT; |
||
506 | sdio_datainitstructure.DataLength = NumberOfBlocks * BlockSize; |
||
507 | sdio_datainitstructure.DataBlockSize = DATA_BLOCK_SIZE; |
||
508 | sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; |
||
509 | sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK; |
||
510 | sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE; |
||
511 | SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure); |
||
512 | |||
513 | if(NumberOfBlocks > 1) |
||
514 | { |
||
515 | /* Send CMD18 READ_MULT_BLOCK with argument data address */ |
||
516 | sdio_cmdinitstructure.CmdIndex = SD_CMD_READ_MULT_BLOCK; |
||
517 | } |
||
518 | else |
||
519 | { |
||
520 | /* Send CMD17 READ_SINGLE_BLOCK */ |
||
521 | sdio_cmdinitstructure.CmdIndex = SD_CMD_READ_SINGLE_BLOCK; |
||
522 | } |
||
523 | |||
524 | sdio_cmdinitstructure.Argument = (uint32_t)ReadAddr; |
||
525 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
526 | |||
527 | /* Read block(s) in polling mode */ |
||
528 | if(NumberOfBlocks > 1) |
||
529 | { |
||
530 | /* Check for error conditions */ |
||
531 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_MULT_BLOCK); |
||
532 | |||
533 | if (errorstate != SD_OK) |
||
534 | { |
||
535 | return errorstate; |
||
536 | } |
||
537 | |||
538 | /* Poll on SDIO flags */ |
||
539 | while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR)) |
||
540 | { |
||
541 | if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF)) |
||
542 | { |
||
543 | /* Read data from SDIO Rx FIFO */ |
||
544 | for (count = 0; count < 8; count++) |
||
545 | { |
||
546 | *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance); |
||
547 | } |
||
548 | |||
549 | tempbuff += 8; |
||
550 | } |
||
551 | } |
||
552 | } |
||
553 | else |
||
554 | { |
||
555 | /* Check for error conditions */ |
||
556 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_SINGLE_BLOCK); |
||
557 | |||
558 | if (errorstate != SD_OK) |
||
559 | { |
||
560 | return errorstate; |
||
561 | } |
||
562 | |||
563 | /* In case of single block transfer, no need of stop transfer at all */ |
||
564 | while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)) |
||
565 | { |
||
566 | if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF)) |
||
567 | { |
||
568 | /* Read data from SDIO Rx FIFO */ |
||
569 | for (count = 0; count < 8; count++) |
||
570 | { |
||
571 | *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance); |
||
572 | } |
||
573 | |||
574 | tempbuff += 8; |
||
575 | } |
||
576 | } |
||
577 | } |
||
578 | |||
579 | /* Send stop transmission command in case of multiblock read */ |
||
580 | if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1)) |
||
581 | { |
||
582 | if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) ||\ |
||
583 | (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\ |
||
584 | (hsd->CardType == HIGH_CAPACITY_SD_CARD)) |
||
585 | { |
||
586 | /* Send stop transmission command */ |
||
587 | errorstate = HAL_SD_StopTransfer(hsd); |
||
588 | } |
||
589 | } |
||
590 | |||
591 | /* Get error state */ |
||
592 | if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT)) |
||
593 | { |
||
594 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT); |
||
595 | |||
596 | errorstate = SD_DATA_TIMEOUT; |
||
597 | |||
598 | return errorstate; |
||
599 | } |
||
600 | else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL)) |
||
601 | { |
||
602 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL); |
||
603 | |||
604 | errorstate = SD_DATA_CRC_FAIL; |
||
605 | |||
606 | return errorstate; |
||
607 | } |
||
608 | else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR)) |
||
609 | { |
||
610 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR); |
||
611 | |||
612 | errorstate = SD_RX_OVERRUN; |
||
613 | |||
614 | return errorstate; |
||
615 | } |
||
616 | else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR)) |
||
617 | { |
||
618 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR); |
||
619 | |||
620 | errorstate = SD_START_BIT_ERR; |
||
621 | |||
622 | return errorstate; |
||
623 | } |
||
624 | else |
||
625 | { |
||
626 | /* No error flag set */ |
||
627 | } |
||
628 | |||
629 | count = SD_DATATIMEOUT; |
||
630 | |||
631 | /* Empty FIFO if there is still any data */ |
||
632 | while ((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (count > 0)) |
||
633 | { |
||
634 | *tempbuff = SDIO_ReadFIFO(hsd->Instance); |
||
635 | tempbuff++; |
||
636 | count--; |
||
637 | } |
||
638 | |||
639 | /* Clear all the static flags */ |
||
640 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
||
641 | |||
642 | return errorstate; |
||
643 | } |
||
644 | |||
645 | /** |
||
646 | * @brief Allows to write block(s) to a specified address in a card. The Data |
||
647 | * transfer is managed by polling mode. |
||
648 | * @param hsd: SD handle |
||
649 | * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit |
||
650 | * @param WriteAddr: Address from where data is to be written |
||
651 | * @param BlockSize: SD card Data block size (in bytes) |
||
652 | * This parameter should be 512. |
||
653 | * @param NumberOfBlocks: Number of SD blocks to write |
||
654 | * @retval SD Card error state |
||
655 | */ |
||
656 | HAL_SD_ErrorTypedef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks) |
||
657 | { |
||
658 | SDIO_CmdInitTypeDef sdio_cmdinitstructure = {0}; |
||
659 | SDIO_DataInitTypeDef sdio_datainitstructure = {0}; |
||
660 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
661 | uint32_t totalnumberofbytes = 0, bytestransferred = 0, count = 0, restwords = 0; |
||
662 | uint32_t *tempbuff = (uint32_t *)pWriteBuffer; |
||
663 | uint8_t cardstate = 0; |
||
664 | |||
665 | /* Initialize data control register */ |
||
666 | hsd->Instance->DCTRL = 0; |
||
667 | |||
668 | if (hsd->CardType == HIGH_CAPACITY_SD_CARD) |
||
669 | { |
||
670 | BlockSize = 512; |
||
671 | WriteAddr /= 512; |
||
672 | } |
||
673 | |||
674 | /* Set Block Size for Card */ |
||
675 | sdio_cmdinitstructure.Argument = (uint32_t)BlockSize; |
||
676 | sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN; |
||
677 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT; |
||
678 | sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO; |
||
679 | sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE; |
||
680 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
681 | |||
682 | /* Check for error conditions */ |
||
683 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN); |
||
684 | |||
685 | if (errorstate != SD_OK) |
||
686 | { |
||
687 | return errorstate; |
||
688 | } |
||
689 | |||
690 | if(NumberOfBlocks > 1) |
||
691 | { |
||
692 | /* Send CMD25 WRITE_MULT_BLOCK with argument data address */ |
||
693 | sdio_cmdinitstructure.CmdIndex = SD_CMD_WRITE_MULT_BLOCK; |
||
694 | } |
||
695 | else |
||
696 | { |
||
697 | /* Send CMD24 WRITE_SINGLE_BLOCK */ |
||
698 | sdio_cmdinitstructure.CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK; |
||
699 | } |
||
700 | |||
701 | sdio_cmdinitstructure.Argument = (uint32_t)WriteAddr; |
||
702 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
703 | |||
704 | /* Check for error conditions */ |
||
705 | if(NumberOfBlocks > 1) |
||
706 | { |
||
707 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_MULT_BLOCK); |
||
708 | } |
||
709 | else |
||
710 | { |
||
711 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_SINGLE_BLOCK); |
||
712 | } |
||
713 | |||
714 | if (errorstate != SD_OK) |
||
715 | { |
||
716 | return errorstate; |
||
717 | } |
||
718 | |||
719 | /* Set total number of bytes to write */ |
||
720 | totalnumberofbytes = NumberOfBlocks * BlockSize; |
||
721 | |||
722 | /* Configure the SD DPSM (Data Path State Machine) */ |
||
723 | sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT; |
||
724 | sdio_datainitstructure.DataLength = NumberOfBlocks * BlockSize; |
||
725 | sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; |
||
726 | sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_CARD; |
||
727 | sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK; |
||
728 | sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE; |
||
729 | SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure); |
||
730 | |||
731 | /* Write block(s) in polling mode */ |
||
732 | if(NumberOfBlocks > 1) |
||
733 | { |
||
734 | while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR)) |
||
735 | { |
||
736 | if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE)) |
||
737 | { |
||
738 | if ((totalnumberofbytes - bytestransferred) < 32) |
||
739 | { |
||
740 | restwords = ((totalnumberofbytes - bytestransferred) % 4 == 0) ? ((totalnumberofbytes - bytestransferred) / 4) : (( totalnumberofbytes - bytestransferred) / 4 + 1); |
||
741 | |||
742 | /* Write data to SDIO Tx FIFO */ |
||
743 | for (count = 0; count < restwords; count++) |
||
744 | { |
||
745 | SDIO_WriteFIFO(hsd->Instance, tempbuff); |
||
746 | tempbuff++; |
||
747 | bytestransferred += 4; |
||
748 | } |
||
749 | } |
||
750 | else |
||
751 | { |
||
752 | /* Write data to SDIO Tx FIFO */ |
||
753 | for (count = 0; count < 8; count++) |
||
754 | { |
||
755 | SDIO_WriteFIFO(hsd->Instance, (tempbuff + count)); |
||
756 | } |
||
757 | |||
758 | tempbuff += 8; |
||
759 | bytestransferred += 32; |
||
760 | } |
||
761 | } |
||
762 | } |
||
763 | } |
||
764 | else |
||
765 | { |
||
766 | /* In case of single data block transfer no need of stop command at all */ |
||
767 | while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)) |
||
768 | { |
||
769 | if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE)) |
||
770 | { |
||
771 | if ((totalnumberofbytes - bytestransferred) < 32) |
||
772 | { |
||
773 | restwords = ((totalnumberofbytes - bytestransferred) % 4 == 0) ? ((totalnumberofbytes - bytestransferred) / 4) : (( totalnumberofbytes - bytestransferred) / 4 + 1); |
||
774 | |||
775 | /* Write data to SDIO Tx FIFO */ |
||
776 | for (count = 0; count < restwords; count++) |
||
777 | { |
||
778 | SDIO_WriteFIFO(hsd->Instance, tempbuff); |
||
779 | tempbuff++; |
||
780 | bytestransferred += 4; |
||
781 | } |
||
782 | } |
||
783 | else |
||
784 | { |
||
785 | /* Write data to SDIO Tx FIFO */ |
||
786 | for (count = 0; count < 8; count++) |
||
787 | { |
||
788 | SDIO_WriteFIFO(hsd->Instance, (tempbuff + count)); |
||
789 | } |
||
790 | |||
791 | tempbuff += 8; |
||
792 | bytestransferred += 32; |
||
793 | } |
||
794 | } |
||
795 | } |
||
796 | } |
||
797 | |||
798 | /* Send stop transmission command in case of multiblock write */ |
||
799 | if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1)) |
||
800 | { |
||
801 | if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\ |
||
802 | (hsd->CardType == HIGH_CAPACITY_SD_CARD)) |
||
803 | { |
||
804 | /* Send stop transmission command */ |
||
805 | errorstate = HAL_SD_StopTransfer(hsd); |
||
806 | } |
||
807 | } |
||
808 | |||
809 | /* Get error state */ |
||
810 | if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT)) |
||
811 | { |
||
812 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT); |
||
813 | |||
814 | errorstate = SD_DATA_TIMEOUT; |
||
815 | |||
816 | return errorstate; |
||
817 | } |
||
818 | else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL)) |
||
819 | { |
||
820 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL); |
||
821 | |||
822 | errorstate = SD_DATA_CRC_FAIL; |
||
823 | |||
824 | return errorstate; |
||
825 | } |
||
826 | else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR)) |
||
827 | { |
||
828 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_TXUNDERR); |
||
829 | |||
830 | errorstate = SD_TX_UNDERRUN; |
||
831 | |||
832 | return errorstate; |
||
833 | } |
||
834 | else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR)) |
||
835 | { |
||
836 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR); |
||
837 | |||
838 | errorstate = SD_START_BIT_ERR; |
||
839 | |||
840 | return errorstate; |
||
841 | } |
||
842 | else |
||
843 | { |
||
844 | /* No error flag set */ |
||
845 | } |
||
846 | |||
847 | /* Clear all the static flags */ |
||
848 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
||
849 | |||
850 | /* Wait till the card is in programming state */ |
||
851 | errorstate = SD_IsCardProgramming(hsd, &cardstate); |
||
852 | |||
853 | while ((errorstate == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING))) |
||
854 | { |
||
855 | errorstate = SD_IsCardProgramming(hsd, &cardstate); |
||
856 | } |
||
857 | |||
858 | return errorstate; |
||
859 | } |
||
860 | |||
861 | /** |
||
862 | * @brief Reads block(s) from a specified address in a card. The Data transfer |
||
863 | * is managed by DMA mode. |
||
864 | * @note This API should be followed by the function HAL_SD_CheckReadOperation() |
||
865 | * to check the completion of the read process |
||
866 | * @param hsd: SD handle |
||
867 | * @param pReadBuffer: Pointer to the buffer that will contain the received data |
||
868 | * @param ReadAddr: Address from where data is to be read |
||
869 | * @param BlockSize: SD card Data block size |
||
870 | * @note BlockSize must be 512 bytes. |
||
871 | * @param NumberOfBlocks: Number of blocks to read. |
||
872 | * @retval SD Card error state |
||
873 | */ |
||
874 | HAL_SD_ErrorTypedef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks) |
||
875 | { |
||
876 | SDIO_CmdInitTypeDef sdio_cmdinitstructure = {0}; |
||
877 | SDIO_DataInitTypeDef sdio_datainitstructure = {0}; |
||
878 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
879 | |||
880 | /* Initialize data control register */ |
||
881 | hsd->Instance->DCTRL = 0; |
||
882 | |||
883 | /* Initialize handle flags */ |
||
884 | hsd->SdTransferCplt = 0; |
||
885 | hsd->DmaTransferCplt = 0; |
||
886 | hsd->SdTransferErr = SD_OK; |
||
887 | |||
888 | /* Initialize SD Read operation */ |
||
889 | if(NumberOfBlocks > 1) |
||
890 | { |
||
891 | hsd->SdOperation = SD_READ_MULTIPLE_BLOCK; |
||
892 | } |
||
893 | else |
||
894 | { |
||
895 | hsd->SdOperation = SD_READ_SINGLE_BLOCK; |
||
896 | } |
||
897 | |||
898 | /* Enable transfer interrupts */ |
||
899 | __HAL_SD_SDIO_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL |\ |
||
900 | SDIO_IT_DTIMEOUT |\ |
||
901 | SDIO_IT_DATAEND |\ |
||
902 | SDIO_IT_RXOVERR |\ |
||
903 | SDIO_IT_STBITERR)); |
||
904 | |||
905 | /* Enable SDIO DMA transfer */ |
||
906 | __HAL_SD_SDIO_DMA_ENABLE(hsd); |
||
907 | |||
908 | /* Configure DMA user callbacks */ |
||
909 | hsd->hdmarx->XferCpltCallback = SD_DMA_RxCplt; |
||
910 | hsd->hdmarx->XferErrorCallback = SD_DMA_RxError; |
||
911 | |||
912 | /* Enable the DMA Channel */ |
||
913 | HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pReadBuffer, (uint32_t)(BlockSize * NumberOfBlocks)/4); |
||
914 | |||
915 | if (hsd->CardType == HIGH_CAPACITY_SD_CARD) |
||
916 | { |
||
917 | BlockSize = 512; |
||
918 | ReadAddr /= 512; |
||
919 | } |
||
920 | |||
921 | /* Set Block Size for Card */ |
||
922 | sdio_cmdinitstructure.Argument = (uint32_t)BlockSize; |
||
923 | sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN; |
||
924 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT; |
||
925 | sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO; |
||
926 | sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE; |
||
927 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
928 | |||
929 | /* Check for error conditions */ |
||
930 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN); |
||
931 | |||
932 | if (errorstate != SD_OK) |
||
933 | { |
||
934 | return errorstate; |
||
935 | } |
||
936 | |||
937 | /* Configure the SD DPSM (Data Path State Machine) */ |
||
938 | sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT; |
||
939 | sdio_datainitstructure.DataLength = BlockSize * NumberOfBlocks; |
||
940 | sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; |
||
941 | sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; |
||
942 | sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK; |
||
943 | sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE; |
||
944 | SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure); |
||
945 | |||
946 | /* Check number of blocks command */ |
||
947 | if(NumberOfBlocks > 1) |
||
948 | { |
||
949 | /* Send CMD18 READ_MULT_BLOCK with argument data address */ |
||
950 | sdio_cmdinitstructure.CmdIndex = SD_CMD_READ_MULT_BLOCK; |
||
951 | } |
||
952 | else |
||
953 | { |
||
954 | /* Send CMD17 READ_SINGLE_BLOCK */ |
||
955 | sdio_cmdinitstructure.CmdIndex = SD_CMD_READ_SINGLE_BLOCK; |
||
956 | } |
||
957 | |||
958 | sdio_cmdinitstructure.Argument = (uint32_t)ReadAddr; |
||
959 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
960 | |||
961 | /* Check for error conditions */ |
||
962 | if(NumberOfBlocks > 1) |
||
963 | { |
||
964 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_MULT_BLOCK); |
||
965 | } |
||
966 | else |
||
967 | { |
||
968 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_READ_SINGLE_BLOCK); |
||
969 | } |
||
970 | |||
971 | /* Update the SD transfer error in SD handle */ |
||
972 | hsd->SdTransferErr = errorstate; |
||
973 | |||
974 | return errorstate; |
||
975 | } |
||
976 | |||
977 | |||
978 | /** |
||
979 | * @brief Writes block(s) to a specified address in a card. The Data transfer |
||
980 | * is managed by DMA mode. |
||
981 | * @note This API should be followed by the function HAL_SD_CheckWriteOperation() |
||
982 | * to check the completion of the write process (by SD current status polling). |
||
983 | * @param hsd: SD handle |
||
984 | * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit |
||
985 | * @param WriteAddr: Address from where data is to be read |
||
986 | * @param BlockSize: the SD card Data block size |
||
987 | * @note BlockSize must be 512 bytes. |
||
988 | * @param NumberOfBlocks: Number of blocks to write |
||
989 | * @retval SD Card error state |
||
990 | */ |
||
991 | HAL_SD_ErrorTypedef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks) |
||
992 | { |
||
993 | SDIO_CmdInitTypeDef sdio_cmdinitstructure = {0}; |
||
994 | SDIO_DataInitTypeDef sdio_datainitstructure = {0}; |
||
995 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
996 | |||
997 | /* Initialize data control register */ |
||
998 | hsd->Instance->DCTRL = 0; |
||
999 | |||
1000 | /* Initialize handle flags */ |
||
1001 | hsd->SdTransferCplt = 0; |
||
1002 | hsd->DmaTransferCplt = 0; |
||
1003 | hsd->SdTransferErr = SD_OK; |
||
1004 | |||
1005 | /* Initialize SD Write operation */ |
||
1006 | if(NumberOfBlocks > 1) |
||
1007 | { |
||
1008 | hsd->SdOperation = SD_WRITE_MULTIPLE_BLOCK; |
||
1009 | } |
||
1010 | else |
||
1011 | { |
||
1012 | hsd->SdOperation = SD_WRITE_SINGLE_BLOCK; |
||
1013 | } |
||
1014 | |||
1015 | /* Enable transfer interrupts */ |
||
1016 | __HAL_SD_SDIO_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL |\ |
||
1017 | SDIO_IT_DTIMEOUT |\ |
||
1018 | SDIO_IT_DATAEND |\ |
||
1019 | SDIO_IT_TXUNDERR |\ |
||
1020 | SDIO_IT_STBITERR)); |
||
1021 | |||
1022 | /* Configure DMA user callbacks */ |
||
1023 | hsd->hdmatx->XferCpltCallback = SD_DMA_TxCplt; |
||
1024 | hsd->hdmatx->XferErrorCallback = SD_DMA_TxError; |
||
1025 | |||
1026 | /* Enable the DMA Channel */ |
||
1027 | HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pWriteBuffer, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BlockSize * NumberOfBlocks)/4); |
||
1028 | |||
1029 | /* Enable SDIO DMA transfer */ |
||
1030 | __HAL_SD_SDIO_DMA_ENABLE(hsd); |
||
1031 | |||
1032 | if (hsd->CardType == HIGH_CAPACITY_SD_CARD) |
||
1033 | { |
||
1034 | BlockSize = 512; |
||
1035 | WriteAddr /= 512; |
||
1036 | } |
||
1037 | |||
1038 | /* Set Block Size for Card */ |
||
1039 | sdio_cmdinitstructure.Argument = (uint32_t)BlockSize; |
||
1040 | sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN; |
||
1041 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT; |
||
1042 | sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO; |
||
1043 | sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE; |
||
1044 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
1045 | |||
1046 | /* Check for error conditions */ |
||
1047 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN); |
||
1048 | |||
1049 | if (errorstate != SD_OK) |
||
1050 | { |
||
1051 | return errorstate; |
||
1052 | } |
||
1053 | |||
1054 | /* Check number of blocks command */ |
||
1055 | if(NumberOfBlocks <= 1) |
||
1056 | { |
||
1057 | /* Send CMD24 WRITE_SINGLE_BLOCK */ |
||
1058 | sdio_cmdinitstructure.CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK; |
||
1059 | } |
||
1060 | else |
||
1061 | { |
||
1062 | /* Send CMD25 WRITE_MULT_BLOCK with argument data address */ |
||
1063 | sdio_cmdinitstructure.CmdIndex = SD_CMD_WRITE_MULT_BLOCK; |
||
1064 | } |
||
1065 | |||
1066 | sdio_cmdinitstructure.Argument = (uint32_t)WriteAddr; |
||
1067 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
1068 | |||
1069 | /* Check for error conditions */ |
||
1070 | if(NumberOfBlocks > 1) |
||
1071 | { |
||
1072 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_MULT_BLOCK); |
||
1073 | } |
||
1074 | else |
||
1075 | { |
||
1076 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_WRITE_SINGLE_BLOCK); |
||
1077 | } |
||
1078 | |||
1079 | if (errorstate != SD_OK) |
||
1080 | { |
||
1081 | return errorstate; |
||
1082 | } |
||
1083 | |||
1084 | /* Configure the SD DPSM (Data Path State Machine) */ |
||
1085 | sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT; |
||
1086 | sdio_datainitstructure.DataLength = BlockSize * NumberOfBlocks; |
||
1087 | sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; |
||
1088 | sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_CARD; |
||
1089 | sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK; |
||
1090 | sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE; |
||
1091 | SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure); |
||
1092 | |||
1093 | hsd->SdTransferErr = errorstate; |
||
1094 | |||
1095 | return errorstate; |
||
1096 | } |
||
1097 | |||
1098 | /** |
||
1099 | * @brief This function waits until the SD DMA data read transfer is finished. |
||
1100 | * This API should be called after HAL_SD_ReadBlocks_DMA() function |
||
1101 | * to insure that all data sent by the card is already transferred by the |
||
1102 | * DMA controller. |
||
1103 | * @param hsd: SD handle |
||
1104 | * @param Timeout: Timeout duration |
||
1105 | * @retval SD Card error state |
||
1106 | */ |
||
1107 | HAL_SD_ErrorTypedef HAL_SD_CheckReadOperation(SD_HandleTypeDef *hsd, uint32_t Timeout) |
||
1108 | { |
||
1109 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
1110 | uint32_t timeout = Timeout; |
||
1111 | uint32_t tmp1, tmp2; |
||
1112 | HAL_SD_ErrorTypedef tmp3; |
||
1113 | |||
1114 | /* Wait for DMA/SD transfer end or SD error variables to be in SD handle */ |
||
1115 | tmp1 = hsd->DmaTransferCplt; |
||
1116 | tmp2 = hsd->SdTransferCplt; |
||
1117 | tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr; |
||
1118 | |||
1119 | while (((tmp1 & tmp2) == 0) && (tmp3 == SD_OK) && (timeout > 0)) |
||
1120 | { |
||
1121 | tmp1 = hsd->DmaTransferCplt; |
||
1122 | tmp2 = hsd->SdTransferCplt; |
||
1123 | tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr; |
||
1124 | timeout--; |
||
1125 | } |
||
1126 | |||
1127 | timeout = Timeout; |
||
1128 | |||
1129 | /* Wait until the Rx transfer is no longer active */ |
||
1130 | while((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXACT)) && (timeout > 0)) |
||
1131 | { |
||
1132 | timeout--; |
||
1133 | } |
||
1134 | |||
1135 | /* Send stop command in multiblock read */ |
||
1136 | if (hsd->SdOperation == SD_READ_MULTIPLE_BLOCK) |
||
1137 | { |
||
1138 | errorstate = HAL_SD_StopTransfer(hsd); |
||
1139 | } |
||
1140 | |||
1141 | if ((timeout == 0) && (errorstate == SD_OK)) |
||
1142 | { |
||
1143 | errorstate = SD_DATA_TIMEOUT; |
||
1144 | } |
||
1145 | |||
1146 | /* Clear all the static flags */ |
||
1147 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
||
1148 | |||
1149 | /* Return error state */ |
||
1150 | if (hsd->SdTransferErr != SD_OK) |
||
1151 | { |
||
1152 | return (HAL_SD_ErrorTypedef)(hsd->SdTransferErr); |
||
1153 | } |
||
1154 | |||
1155 | return errorstate; |
||
1156 | } |
||
1157 | |||
1158 | /** |
||
1159 | * @brief This function waits until the SD DMA data write transfer is finished. |
||
1160 | * This API should be called after HAL_SD_WriteBlocks_DMA() function |
||
1161 | * to insure that all data sent by the card is already transferred by the |
||
1162 | * DMA controller. |
||
1163 | * @param hsd: SD handle |
||
1164 | * @param Timeout: Timeout duration |
||
1165 | * @retval SD Card error state |
||
1166 | */ |
||
1167 | HAL_SD_ErrorTypedef HAL_SD_CheckWriteOperation(SD_HandleTypeDef *hsd, uint32_t Timeout) |
||
1168 | { |
||
1169 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
1170 | uint32_t timeout = Timeout; |
||
1171 | uint32_t tmp1, tmp2; |
||
1172 | HAL_SD_ErrorTypedef tmp3; |
||
1173 | |||
1174 | /* Wait for DMA/SD transfer end or SD error variables to be in SD handle */ |
||
1175 | tmp1 = hsd->DmaTransferCplt; |
||
1176 | tmp2 = hsd->SdTransferCplt; |
||
1177 | tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr; |
||
1178 | |||
1179 | while (((tmp1 & tmp2) == 0) && (tmp3 == SD_OK) && (timeout > 0)) |
||
1180 | { |
||
1181 | tmp1 = hsd->DmaTransferCplt; |
||
1182 | tmp2 = hsd->SdTransferCplt; |
||
1183 | tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr; |
||
1184 | timeout--; |
||
1185 | } |
||
1186 | |||
1187 | timeout = Timeout; |
||
1188 | |||
1189 | /* Wait until the Tx transfer is no longer active */ |
||
1190 | while((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXACT)) && (timeout > 0)) |
||
1191 | { |
||
1192 | timeout--; |
||
1193 | } |
||
1194 | |||
1195 | /* Send stop command in multiblock write */ |
||
1196 | if (hsd->SdOperation == SD_WRITE_MULTIPLE_BLOCK) |
||
1197 | { |
||
1198 | errorstate = HAL_SD_StopTransfer(hsd); |
||
1199 | } |
||
1200 | |||
1201 | if ((timeout == 0) && (errorstate == SD_OK)) |
||
1202 | { |
||
1203 | errorstate = SD_DATA_TIMEOUT; |
||
1204 | } |
||
1205 | |||
1206 | /* Clear all the static flags */ |
||
1207 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
||
1208 | |||
1209 | /* Return error state */ |
||
1210 | if (hsd->SdTransferErr != SD_OK) |
||
1211 | { |
||
1212 | return (HAL_SD_ErrorTypedef)(hsd->SdTransferErr); |
||
1213 | } |
||
1214 | |||
1215 | /* Wait until write is complete */ |
||
1216 | while(HAL_SD_GetStatus(hsd) != SD_TRANSFER_OK) |
||
1217 | { |
||
1218 | } |
||
1219 | |||
1220 | return errorstate; |
||
1221 | } |
||
1222 | |||
1223 | /** |
||
1224 | * @brief Erases the specified memory area of the given SD card. |
||
1225 | * @param hsd: SD handle |
||
1226 | * @param Startaddr: Start byte address |
||
1227 | * @param Endaddr: End byte address |
||
1228 | * @retval SD Card error state |
||
1229 | */ |
||
1230 | HAL_SD_ErrorTypedef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint64_t Startaddr, uint64_t Endaddr) |
||
1231 | { |
||
1232 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
1233 | SDIO_CmdInitTypeDef sdio_cmdinitstructure = {0}; |
||
1234 | |||
1235 | uint32_t delay = 0; |
||
1236 | __IO uint32_t maxdelay = 0; |
||
1237 | uint8_t cardstate = 0; |
||
1238 | |||
1239 | /* Check if the card command class supports erase command */ |
||
1240 | if (((hsd->CSD[1] >> 20) & SD_CCCC_ERASE) == 0) |
||
1241 | { |
||
1242 | errorstate = SD_REQUEST_NOT_APPLICABLE; |
||
1243 | |||
1244 | return errorstate; |
||
1245 | } |
||
1246 | |||
1247 | /* Get max delay value */ |
||
1248 | maxdelay = 120000 / (((hsd->Instance->CLKCR) & 0xFF) + 2); |
||
1249 | |||
1250 | if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED) |
||
1251 | { |
||
1252 | errorstate = SD_LOCK_UNLOCK_FAILED; |
||
1253 | |||
1254 | return errorstate; |
||
1255 | } |
||
1256 | |||
1257 | /* Get start and end block for high capacity cards */ |
||
1258 | if (hsd->CardType == HIGH_CAPACITY_SD_CARD) |
||
1259 | { |
||
1260 | Startaddr /= 512; |
||
1261 | Endaddr /= 512; |
||
1262 | } |
||
1263 | |||
1264 | /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */ |
||
1265 | if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\ |
||
1266 | (hsd->CardType == HIGH_CAPACITY_SD_CARD)) |
||
1267 | { |
||
1268 | /* Send CMD32 SD_ERASE_GRP_START with argument as addr */ |
||
1269 | sdio_cmdinitstructure.Argument =(uint32_t)Startaddr; |
||
1270 | sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_ERASE_GRP_START; |
||
1271 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT; |
||
1272 | sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO; |
||
1273 | sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE; |
||
1274 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
1275 | |||
1276 | /* Check for error conditions */ |
||
1277 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_ERASE_GRP_START); |
||
1278 | |||
1279 | if (errorstate != SD_OK) |
||
1280 | { |
||
1281 | return errorstate; |
||
1282 | } |
||
1283 | |||
1284 | /* Send CMD33 SD_ERASE_GRP_END with argument as addr */ |
||
1285 | sdio_cmdinitstructure.Argument = (uint32_t)Endaddr; |
||
1286 | sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_ERASE_GRP_END; |
||
1287 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
1288 | |||
1289 | /* Check for error conditions */ |
||
1290 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_ERASE_GRP_END); |
||
1291 | |||
1292 | if (errorstate != SD_OK) |
||
1293 | { |
||
1294 | return errorstate; |
||
1295 | } |
||
1296 | } |
||
1297 | |||
1298 | /* Send CMD38 ERASE */ |
||
1299 | sdio_cmdinitstructure.Argument = 0; |
||
1300 | sdio_cmdinitstructure.CmdIndex = SD_CMD_ERASE; |
||
1301 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
1302 | |||
1303 | /* Check for error conditions */ |
||
1304 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_ERASE); |
||
1305 | |||
1306 | if (errorstate != SD_OK) |
||
1307 | { |
||
1308 | return errorstate; |
||
1309 | } |
||
1310 | |||
1311 | for (; delay < maxdelay; delay++) |
||
1312 | { |
||
1313 | } |
||
1314 | |||
1315 | /* Wait untill the card is in programming state */ |
||
1316 | errorstate = SD_IsCardProgramming(hsd, &cardstate); |
||
1317 | |||
1318 | delay = SD_DATATIMEOUT; |
||
1319 | |||
1320 | while ((delay > 0) && (errorstate == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING))) |
||
1321 | { |
||
1322 | errorstate = SD_IsCardProgramming(hsd, &cardstate); |
||
1323 | delay--; |
||
1324 | } |
||
1325 | |||
1326 | return errorstate; |
||
1327 | } |
||
1328 | |||
1329 | /** |
||
1330 | * @brief This function handles SD card interrupt request. |
||
1331 | * @param hsd: SD handle |
||
1332 | * @retval None |
||
1333 | */ |
||
1334 | void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd) |
||
1335 | { |
||
1336 | /* Check for SDIO interrupt flags */ |
||
1337 | if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_DATAEND)) |
||
1338 | { |
||
1339 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_IT_DATAEND); |
||
1340 | |||
1341 | /* SD transfer is complete */ |
||
1342 | hsd->SdTransferCplt = 1; |
||
1343 | |||
1344 | /* No transfer error */ |
||
1345 | hsd->SdTransferErr = SD_OK; |
||
1346 | |||
1347 | HAL_SD_XferCpltCallback(hsd); |
||
1348 | } |
||
1349 | else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_DCRCFAIL)) |
||
1350 | { |
||
1351 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL); |
||
1352 | |||
1353 | hsd->SdTransferErr = SD_DATA_CRC_FAIL; |
||
1354 | |||
1355 | HAL_SD_XferErrorCallback(hsd); |
||
1356 | |||
1357 | } |
||
1358 | else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_DTIMEOUT)) |
||
1359 | { |
||
1360 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT); |
||
1361 | |||
1362 | hsd->SdTransferErr = SD_DATA_TIMEOUT; |
||
1363 | |||
1364 | HAL_SD_XferErrorCallback(hsd); |
||
1365 | } |
||
1366 | else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_RXOVERR)) |
||
1367 | { |
||
1368 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR); |
||
1369 | |||
1370 | hsd->SdTransferErr = SD_RX_OVERRUN; |
||
1371 | |||
1372 | HAL_SD_XferErrorCallback(hsd); |
||
1373 | } |
||
1374 | else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_TXUNDERR)) |
||
1375 | { |
||
1376 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_TXUNDERR); |
||
1377 | |||
1378 | hsd->SdTransferErr = SD_TX_UNDERRUN; |
||
1379 | |||
1380 | HAL_SD_XferErrorCallback(hsd); |
||
1381 | } |
||
1382 | else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_STBITERR)) |
||
1383 | { |
||
1384 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR); |
||
1385 | |||
1386 | hsd->SdTransferErr = SD_START_BIT_ERR; |
||
1387 | |||
1388 | HAL_SD_XferErrorCallback(hsd); |
||
1389 | } |
||
1390 | else |
||
1391 | { |
||
1392 | /* No error flag set */ |
||
1393 | } |
||
1394 | |||
1395 | /* Disable all SDIO peripheral interrupt sources */ |
||
1396 | __HAL_SD_SDIO_DISABLE_IT(hsd, SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |\ |
||
1397 | SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |\ |
||
1398 | SDIO_IT_RXOVERR | SDIO_IT_STBITERR); |
||
1399 | } |
||
1400 | |||
1401 | |||
1402 | /** |
||
1403 | * @brief SD end of transfer callback. |
||
1404 | * @param hsd: SD handle |
||
1405 | * @retval None |
||
1406 | */ |
||
1407 | __weak void HAL_SD_XferCpltCallback(SD_HandleTypeDef *hsd) |
||
1408 | { |
||
5 | mjames | 1409 | /* Prevent unused argument(s) compilation warning */ |
1410 | UNUSED(hsd); |
||
2 | mjames | 1411 | /* NOTE : This function Should not be modified, when the callback is needed, |
1412 | the HAL_SD_XferCpltCallback could be implemented in the user file |
||
1413 | */ |
||
1414 | } |
||
1415 | |||
1416 | /** |
||
1417 | * @brief SD Transfer Error callback. |
||
1418 | * @param hsd: SD handle |
||
1419 | * @retval None |
||
1420 | */ |
||
1421 | __weak void HAL_SD_XferErrorCallback(SD_HandleTypeDef *hsd) |
||
1422 | { |
||
5 | mjames | 1423 | /* Prevent unused argument(s) compilation warning */ |
1424 | UNUSED(hsd); |
||
2 | mjames | 1425 | /* NOTE : This function Should not be modified, when the callback is needed, |
1426 | the HAL_SD_XferErrorCallback could be implemented in the user file |
||
1427 | */ |
||
1428 | } |
||
1429 | |||
1430 | /** |
||
1431 | * @brief SD Transfer complete Rx callback in non blocking mode. |
||
1432 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
||
1433 | * the configuration information for the specified DMA module. |
||
1434 | * @retval None |
||
1435 | */ |
||
1436 | __weak void HAL_SD_DMA_RxCpltCallback(DMA_HandleTypeDef *hdma) |
||
1437 | { |
||
5 | mjames | 1438 | /* Prevent unused argument(s) compilation warning */ |
1439 | UNUSED(hdma); |
||
2 | mjames | 1440 | /* NOTE : This function Should not be modified, when the callback is needed, |
1441 | the HAL_SD_DMA_RxCpltCallback could be implemented in the user file |
||
1442 | */ |
||
1443 | } |
||
1444 | |||
1445 | /** |
||
1446 | * @brief SD DMA transfer complete Rx error callback. |
||
1447 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
||
1448 | * the configuration information for the specified DMA module. |
||
1449 | * @retval None |
||
1450 | */ |
||
1451 | __weak void HAL_SD_DMA_RxErrorCallback(DMA_HandleTypeDef *hdma) |
||
1452 | { |
||
5 | mjames | 1453 | /* Prevent unused argument(s) compilation warning */ |
1454 | UNUSED(hdma); |
||
2 | mjames | 1455 | /* NOTE : This function Should not be modified, when the callback is needed, |
1456 | the HAL_SD_DMA_RxErrorCallback could be implemented in the user file |
||
1457 | */ |
||
1458 | } |
||
1459 | |||
1460 | /** |
||
1461 | * @brief SD Transfer complete Tx callback in non blocking mode. |
||
1462 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
||
1463 | * the configuration information for the specified DMA module. |
||
1464 | * @retval None |
||
1465 | */ |
||
1466 | __weak void HAL_SD_DMA_TxCpltCallback(DMA_HandleTypeDef *hdma) |
||
1467 | { |
||
5 | mjames | 1468 | /* Prevent unused argument(s) compilation warning */ |
1469 | UNUSED(hdma); |
||
2 | mjames | 1470 | /* NOTE : This function Should not be modified, when the callback is needed, |
1471 | the HAL_SD_DMA_TxCpltCallback could be implemented in the user file |
||
1472 | */ |
||
1473 | } |
||
1474 | |||
1475 | /** |
||
1476 | * @brief SD DMA transfer complete error Tx callback. |
||
1477 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
||
1478 | * the configuration information for the specified DMA module. |
||
1479 | * @retval None |
||
1480 | */ |
||
1481 | __weak void HAL_SD_DMA_TxErrorCallback(DMA_HandleTypeDef *hdma) |
||
1482 | { |
||
5 | mjames | 1483 | /* Prevent unused argument(s) compilation warning */ |
1484 | UNUSED(hdma); |
||
2 | mjames | 1485 | /* NOTE : This function Should not be modified, when the callback is needed, |
1486 | the HAL_SD_DMA_TxErrorCallback could be implemented in the user file |
||
1487 | */ |
||
1488 | } |
||
1489 | |||
1490 | /** |
||
1491 | * @} |
||
1492 | */ |
||
1493 | |||
1494 | /** @defgroup SD_Exported_Functions_Group3 Peripheral Control functions |
||
1495 | * @brief management functions |
||
1496 | * |
||
1497 | @verbatim |
||
1498 | ============================================================================== |
||
1499 | ##### Peripheral Control functions ##### |
||
1500 | ============================================================================== |
||
1501 | [..] |
||
1502 | This subsection provides a set of functions allowing to control the SD card |
||
1503 | operations. |
||
1504 | |||
1505 | @endverbatim |
||
1506 | * @{ |
||
1507 | */ |
||
1508 | |||
1509 | /** |
||
1510 | * @brief Returns information about specific card. |
||
1511 | * @param hsd: SD handle |
||
1512 | * @param pCardInfo: Pointer to a HAL_SD_CardInfoTypedef structure that |
||
1513 | * contains all SD cardinformation |
||
1514 | * @retval SD Card error state |
||
1515 | */ |
||
1516 | HAL_SD_ErrorTypedef HAL_SD_Get_CardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *pCardInfo) |
||
1517 | { |
||
1518 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
1519 | uint32_t tmp = 0; |
||
1520 | |||
1521 | pCardInfo->CardType = (uint8_t)(hsd->CardType); |
||
1522 | pCardInfo->RCA = (uint16_t)(hsd->RCA); |
||
1523 | |||
1524 | /* Byte 0 */ |
||
1525 | tmp = (hsd->CSD[0] & 0xFF000000) >> 24; |
||
1526 | pCardInfo->SD_csd.CSDStruct = (uint8_t)((tmp & 0xC0) >> 6); |
||
1527 | pCardInfo->SD_csd.SysSpecVersion = (uint8_t)((tmp & 0x3C) >> 2); |
||
1528 | pCardInfo->SD_csd.Reserved1 = tmp & 0x03; |
||
1529 | |||
1530 | /* Byte 1 */ |
||
1531 | tmp = (hsd->CSD[0] & 0x00FF0000) >> 16; |
||
1532 | pCardInfo->SD_csd.TAAC = (uint8_t)tmp; |
||
1533 | |||
1534 | /* Byte 2 */ |
||
1535 | tmp = (hsd->CSD[0] & 0x0000FF00) >> 8; |
||
1536 | pCardInfo->SD_csd.NSAC = (uint8_t)tmp; |
||
1537 | |||
1538 | /* Byte 3 */ |
||
1539 | tmp = hsd->CSD[0] & 0x000000FF; |
||
1540 | pCardInfo->SD_csd.MaxBusClkFrec = (uint8_t)tmp; |
||
1541 | |||
1542 | /* Byte 4 */ |
||
1543 | tmp = (hsd->CSD[1] & 0xFF000000) >> 24; |
||
1544 | pCardInfo->SD_csd.CardComdClasses = (uint16_t)(tmp << 4); |
||
1545 | |||
1546 | /* Byte 5 */ |
||
1547 | tmp = (hsd->CSD[1] & 0x00FF0000) >> 16; |
||
1548 | pCardInfo->SD_csd.CardComdClasses |= (uint16_t)((tmp & 0xF0) >> 4); |
||
1549 | pCardInfo->SD_csd.RdBlockLen = (uint8_t)(tmp & 0x0F); |
||
1550 | |||
1551 | /* Byte 6 */ |
||
1552 | tmp = (hsd->CSD[1] & 0x0000FF00) >> 8; |
||
1553 | pCardInfo->SD_csd.PartBlockRead = (uint8_t)((tmp & 0x80) >> 7); |
||
1554 | pCardInfo->SD_csd.WrBlockMisalign = (uint8_t)((tmp & 0x40) >> 6); |
||
1555 | pCardInfo->SD_csd.RdBlockMisalign = (uint8_t)((tmp & 0x20) >> 5); |
||
1556 | pCardInfo->SD_csd.DSRImpl = (uint8_t)((tmp & 0x10) >> 4); |
||
1557 | pCardInfo->SD_csd.Reserved2 = 0; /*!< Reserved */ |
||
1558 | |||
1559 | if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0)) |
||
1560 | { |
||
1561 | pCardInfo->SD_csd.DeviceSize = (tmp & 0x03) << 10; |
||
1562 | |||
1563 | /* Byte 7 */ |
||
1564 | tmp = (uint8_t)(hsd->CSD[1] & 0x000000FF); |
||
1565 | pCardInfo->SD_csd.DeviceSize |= (tmp) << 2; |
||
1566 | |||
1567 | /* Byte 8 */ |
||
1568 | tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000) >> 24); |
||
1569 | pCardInfo->SD_csd.DeviceSize |= (tmp & 0xC0) >> 6; |
||
1570 | |||
1571 | pCardInfo->SD_csd.MaxRdCurrentVDDMin = (tmp & 0x38) >> 3; |
||
1572 | pCardInfo->SD_csd.MaxRdCurrentVDDMax = (tmp & 0x07); |
||
1573 | |||
1574 | /* Byte 9 */ |
||
1575 | tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000) >> 16); |
||
1576 | pCardInfo->SD_csd.MaxWrCurrentVDDMin = (tmp & 0xE0) >> 5; |
||
1577 | pCardInfo->SD_csd.MaxWrCurrentVDDMax = (tmp & 0x1C) >> 2; |
||
1578 | pCardInfo->SD_csd.DeviceSizeMul = (tmp & 0x03) << 1; |
||
1579 | /* Byte 10 */ |
||
1580 | tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00) >> 8); |
||
1581 | pCardInfo->SD_csd.DeviceSizeMul |= (tmp & 0x80) >> 7; |
||
1582 | |||
1583 | pCardInfo->CardCapacity = (pCardInfo->SD_csd.DeviceSize + 1) ; |
||
1584 | pCardInfo->CardCapacity *= (1 << (pCardInfo->SD_csd.DeviceSizeMul + 2)); |
||
1585 | pCardInfo->CardBlockSize = 1 << (pCardInfo->SD_csd.RdBlockLen); |
||
1586 | pCardInfo->CardCapacity *= pCardInfo->CardBlockSize; |
||
1587 | } |
||
1588 | else if (hsd->CardType == HIGH_CAPACITY_SD_CARD) |
||
1589 | { |
||
1590 | /* Byte 7 */ |
||
1591 | tmp = (uint8_t)(hsd->CSD[1] & 0x000000FF); |
||
1592 | pCardInfo->SD_csd.DeviceSize = (tmp & 0x3F) << 16; |
||
1593 | |||
1594 | /* Byte 8 */ |
||
1595 | tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000) >> 24); |
||
1596 | |||
1597 | pCardInfo->SD_csd.DeviceSize |= (tmp << 8); |
||
1598 | |||
1599 | /* Byte 9 */ |
||
1600 | tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000) >> 16); |
||
1601 | |||
1602 | pCardInfo->SD_csd.DeviceSize |= (tmp); |
||
1603 | |||
1604 | /* Byte 10 */ |
||
1605 | tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00) >> 8); |
||
1606 | |||
5 | mjames | 1607 | pCardInfo->CardCapacity = (uint64_t)(((uint64_t)pCardInfo->SD_csd.DeviceSize + 1) * 512 * 1024); |
2 | mjames | 1608 | pCardInfo->CardBlockSize = 512; |
1609 | } |
||
1610 | else |
||
1611 | { |
||
1612 | /* Not supported card type */ |
||
1613 | errorstate = SD_ERROR; |
||
1614 | } |
||
1615 | |||
1616 | pCardInfo->SD_csd.EraseGrSize = (tmp & 0x40) >> 6; |
||
1617 | pCardInfo->SD_csd.EraseGrMul = (tmp & 0x3F) << 1; |
||
1618 | |||
1619 | /* Byte 11 */ |
||
1620 | tmp = (uint8_t)(hsd->CSD[2] & 0x000000FF); |
||
1621 | pCardInfo->SD_csd.EraseGrMul |= (tmp & 0x80) >> 7; |
||
1622 | pCardInfo->SD_csd.WrProtectGrSize = (tmp & 0x7F); |
||
1623 | |||
1624 | /* Byte 12 */ |
||
1625 | tmp = (uint8_t)((hsd->CSD[3] & 0xFF000000) >> 24); |
||
1626 | pCardInfo->SD_csd.WrProtectGrEnable = (tmp & 0x80) >> 7; |
||
1627 | pCardInfo->SD_csd.ManDeflECC = (tmp & 0x60) >> 5; |
||
1628 | pCardInfo->SD_csd.WrSpeedFact = (tmp & 0x1C) >> 2; |
||
1629 | pCardInfo->SD_csd.MaxWrBlockLen = (tmp & 0x03) << 2; |
||
1630 | |||
1631 | /* Byte 13 */ |
||
1632 | tmp = (uint8_t)((hsd->CSD[3] & 0x00FF0000) >> 16); |
||
1633 | pCardInfo->SD_csd.MaxWrBlockLen |= (tmp & 0xC0) >> 6; |
||
1634 | pCardInfo->SD_csd.WriteBlockPaPartial = (tmp & 0x20) >> 5; |
||
1635 | pCardInfo->SD_csd.Reserved3 = 0; |
||
1636 | pCardInfo->SD_csd.ContentProtectAppli = (tmp & 0x01); |
||
1637 | |||
1638 | /* Byte 14 */ |
||
1639 | tmp = (uint8_t)((hsd->CSD[3] & 0x0000FF00) >> 8); |
||
1640 | pCardInfo->SD_csd.FileFormatGrouop = (tmp & 0x80) >> 7; |
||
1641 | pCardInfo->SD_csd.CopyFlag = (tmp & 0x40) >> 6; |
||
1642 | pCardInfo->SD_csd.PermWrProtect = (tmp & 0x20) >> 5; |
||
1643 | pCardInfo->SD_csd.TempWrProtect = (tmp & 0x10) >> 4; |
||
1644 | pCardInfo->SD_csd.FileFormat = (tmp & 0x0C) >> 2; |
||
1645 | pCardInfo->SD_csd.ECC = (tmp & 0x03); |
||
1646 | |||
1647 | /* Byte 15 */ |
||
1648 | tmp = (uint8_t)(hsd->CSD[3] & 0x000000FF); |
||
1649 | pCardInfo->SD_csd.CSD_CRC = (tmp & 0xFE) >> 1; |
||
1650 | pCardInfo->SD_csd.Reserved4 = 1; |
||
1651 | |||
1652 | /* Byte 0 */ |
||
1653 | tmp = (uint8_t)((hsd->CID[0] & 0xFF000000) >> 24); |
||
1654 | pCardInfo->SD_cid.ManufacturerID = tmp; |
||
1655 | |||
1656 | /* Byte 1 */ |
||
1657 | tmp = (uint8_t)((hsd->CID[0] & 0x00FF0000) >> 16); |
||
1658 | pCardInfo->SD_cid.OEM_AppliID = tmp << 8; |
||
1659 | |||
1660 | /* Byte 2 */ |
||
1661 | tmp = (uint8_t)((hsd->CID[0] & 0x000000FF00) >> 8); |
||
1662 | pCardInfo->SD_cid.OEM_AppliID |= tmp; |
||
1663 | |||
1664 | /* Byte 3 */ |
||
1665 | tmp = (uint8_t)(hsd->CID[0] & 0x000000FF); |
||
1666 | pCardInfo->SD_cid.ProdName1 = tmp << 24; |
||
1667 | |||
1668 | /* Byte 4 */ |
||
1669 | tmp = (uint8_t)((hsd->CID[1] & 0xFF000000) >> 24); |
||
1670 | pCardInfo->SD_cid.ProdName1 |= tmp << 16; |
||
1671 | |||
1672 | /* Byte 5 */ |
||
1673 | tmp = (uint8_t)((hsd->CID[1] & 0x00FF0000) >> 16); |
||
1674 | pCardInfo->SD_cid.ProdName1 |= tmp << 8; |
||
1675 | |||
1676 | /* Byte 6 */ |
||
1677 | tmp = (uint8_t)((hsd->CID[1] & 0x0000FF00) >> 8); |
||
1678 | pCardInfo->SD_cid.ProdName1 |= tmp; |
||
1679 | |||
1680 | /* Byte 7 */ |
||
1681 | tmp = (uint8_t)(hsd->CID[1] & 0x000000FF); |
||
1682 | pCardInfo->SD_cid.ProdName2 = tmp; |
||
1683 | |||
1684 | /* Byte 8 */ |
||
1685 | tmp = (uint8_t)((hsd->CID[2] & 0xFF000000) >> 24); |
||
1686 | pCardInfo->SD_cid.ProdRev = tmp; |
||
1687 | |||
1688 | /* Byte 9 */ |
||
1689 | tmp = (uint8_t)((hsd->CID[2] & 0x00FF0000) >> 16); |
||
1690 | pCardInfo->SD_cid.ProdSN = tmp << 24; |
||
1691 | |||
1692 | /* Byte 10 */ |
||
1693 | tmp = (uint8_t)((hsd->CID[2] & 0x0000FF00) >> 8); |
||
1694 | pCardInfo->SD_cid.ProdSN |= tmp << 16; |
||
1695 | |||
1696 | /* Byte 11 */ |
||
1697 | tmp = (uint8_t)(hsd->CID[2] & 0x000000FF); |
||
1698 | pCardInfo->SD_cid.ProdSN |= tmp << 8; |
||
1699 | |||
1700 | /* Byte 12 */ |
||
1701 | tmp = (uint8_t)((hsd->CID[3] & 0xFF000000) >> 24); |
||
1702 | pCardInfo->SD_cid.ProdSN |= tmp; |
||
1703 | |||
1704 | /* Byte 13 */ |
||
1705 | tmp = (uint8_t)((hsd->CID[3] & 0x00FF0000) >> 16); |
||
1706 | pCardInfo->SD_cid.Reserved1 |= (tmp & 0xF0) >> 4; |
||
1707 | pCardInfo->SD_cid.ManufactDate = (tmp & 0x0F) << 8; |
||
1708 | |||
1709 | /* Byte 14 */ |
||
1710 | tmp = (uint8_t)((hsd->CID[3] & 0x0000FF00) >> 8); |
||
1711 | pCardInfo->SD_cid.ManufactDate |= tmp; |
||
1712 | |||
1713 | /* Byte 15 */ |
||
1714 | tmp = (uint8_t)(hsd->CID[3] & 0x000000FF); |
||
1715 | pCardInfo->SD_cid.CID_CRC = (tmp & 0xFE) >> 1; |
||
1716 | pCardInfo->SD_cid.Reserved2 = 1; |
||
1717 | |||
1718 | return errorstate; |
||
1719 | } |
||
1720 | |||
1721 | /** |
||
1722 | * @brief Enables wide bus operation for the requested card if supported by |
||
1723 | * card. |
||
1724 | * @param hsd: SD handle |
||
1725 | * @param WideMode: Specifies the SD card wide bus mode |
||
1726 | * This parameter can be one of the following values: |
||
1727 | * @arg SDIO_BUS_WIDE_8B: 8-bit data transfer (Only for MMC) |
||
1728 | * @arg SDIO_BUS_WIDE_4B: 4-bit data transfer |
||
1729 | * @arg SDIO_BUS_WIDE_1B: 1-bit data transfer |
||
1730 | * @retval SD Card error state |
||
1731 | */ |
||
1732 | HAL_SD_ErrorTypedef HAL_SD_WideBusOperation_Config(SD_HandleTypeDef *hsd, uint32_t WideMode) |
||
1733 | { |
||
1734 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
1735 | SDIO_InitTypeDef init = {0}; |
||
1736 | |||
1737 | /* MMC Card does not support this feature */ |
||
1738 | if (hsd->CardType == MULTIMEDIA_CARD) |
||
1739 | { |
||
1740 | errorstate = SD_UNSUPPORTED_FEATURE; |
||
1741 | } |
||
1742 | else if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\ |
||
1743 | (hsd->CardType == HIGH_CAPACITY_SD_CARD)) |
||
1744 | { |
||
1745 | if (WideMode == SDIO_BUS_WIDE_8B) |
||
1746 | { |
||
1747 | errorstate = SD_UNSUPPORTED_FEATURE; |
||
1748 | } |
||
1749 | else if (WideMode == SDIO_BUS_WIDE_4B) |
||
1750 | { |
||
1751 | errorstate = SD_WideBus_Enable(hsd); |
||
1752 | } |
||
1753 | else if (WideMode == SDIO_BUS_WIDE_1B) |
||
1754 | { |
||
1755 | errorstate = SD_WideBus_Disable(hsd); |
||
1756 | } |
||
1757 | else |
||
1758 | { |
||
1759 | /* WideMode is not a valid argument*/ |
||
1760 | errorstate = SD_INVALID_PARAMETER; |
||
1761 | } |
||
1762 | |||
1763 | if (errorstate == SD_OK) |
||
1764 | { |
||
1765 | /* Configure the SDIO peripheral */ |
||
1766 | init.ClockEdge = hsd->Init.ClockEdge; |
||
1767 | init.ClockBypass = hsd->Init.ClockBypass; |
||
1768 | init.ClockPowerSave = hsd->Init.ClockPowerSave; |
||
1769 | init.BusWide = WideMode; |
||
1770 | init.HardwareFlowControl = hsd->Init.HardwareFlowControl; |
||
1771 | init.ClockDiv = hsd->Init.ClockDiv; |
||
1772 | |||
1773 | /* Configure SDIO peripheral interface */ |
||
1774 | SDIO_Init(hsd->Instance, init); |
||
1775 | } |
||
1776 | else |
||
1777 | { |
||
1778 | /* An error occured while enabling/disabling the wide bus*/ |
||
1779 | } |
||
1780 | } |
||
1781 | else |
||
1782 | { |
||
1783 | /* Not supported card type */ |
||
1784 | errorstate = SD_ERROR; |
||
1785 | } |
||
1786 | |||
1787 | return errorstate; |
||
1788 | } |
||
1789 | |||
1790 | /** |
||
1791 | * @brief Aborts an ongoing data transfer. |
||
1792 | * @param hsd: SD handle |
||
1793 | * @retval SD Card error state |
||
1794 | */ |
||
1795 | HAL_SD_ErrorTypedef HAL_SD_StopTransfer(SD_HandleTypeDef *hsd) |
||
1796 | { |
||
1797 | SDIO_CmdInitTypeDef sdio_cmdinitstructure = {0}; |
||
1798 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
1799 | |||
1800 | /* Send CMD12 STOP_TRANSMISSION */ |
||
1801 | sdio_cmdinitstructure.Argument = 0; |
||
1802 | sdio_cmdinitstructure.CmdIndex = SD_CMD_STOP_TRANSMISSION; |
||
1803 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT; |
||
1804 | sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO; |
||
1805 | sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE; |
||
1806 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
1807 | |||
1808 | /* Check for error conditions */ |
||
1809 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_STOP_TRANSMISSION); |
||
1810 | |||
1811 | return errorstate; |
||
1812 | } |
||
1813 | |||
1814 | /** |
||
1815 | * @brief Switches the SD card to High Speed mode. |
||
1816 | * This API must be used after "Transfer State" |
||
1817 | * @note This operation should be followed by the configuration |
||
1818 | * of PLL to have SDIOCK clock between 67 and 75 MHz |
||
1819 | * @param hsd: SD handle |
||
1820 | * @retval SD Card error state |
||
1821 | */ |
||
1822 | HAL_SD_ErrorTypedef HAL_SD_HighSpeed (SD_HandleTypeDef *hsd) |
||
1823 | { |
||
1824 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
1825 | SDIO_CmdInitTypeDef sdio_cmdinitstructure = {0}; |
||
1826 | SDIO_DataInitTypeDef sdio_datainitstructure = {0}; |
||
1827 | |||
1828 | uint8_t SD_hs[64] = {0}; |
||
1829 | uint32_t SD_scr[2] = {0, 0}; |
||
1830 | uint32_t SD_SPEC = 0 ; |
||
1831 | uint32_t count = 0, *tempbuff = (uint32_t *)SD_hs; |
||
1832 | |||
1833 | /* Initialize the Data control register */ |
||
1834 | hsd->Instance->DCTRL = 0; |
||
1835 | |||
1836 | /* Get SCR Register */ |
||
1837 | errorstate = SD_FindSCR(hsd, SD_scr); |
||
1838 | |||
1839 | if (errorstate != SD_OK) |
||
1840 | { |
||
1841 | return errorstate; |
||
1842 | } |
||
1843 | |||
1844 | /* Test the Version supported by the card*/ |
||
1845 | SD_SPEC = (SD_scr[1] & 0x01000000) | (SD_scr[1] & 0x02000000); |
||
1846 | |||
1847 | if (SD_SPEC != SD_ALLZERO) |
||
1848 | { |
||
1849 | /* Set Block Size for Card */ |
||
1850 | sdio_cmdinitstructure.Argument = (uint32_t)64; |
||
1851 | sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN; |
||
1852 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT; |
||
1853 | sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO; |
||
1854 | sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE; |
||
1855 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
1856 | |||
1857 | /* Check for error conditions */ |
||
1858 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN); |
||
1859 | |||
1860 | if (errorstate != SD_OK) |
||
1861 | { |
||
1862 | return errorstate; |
||
1863 | } |
||
1864 | |||
1865 | /* Configure the SD DPSM (Data Path State Machine) */ |
||
1866 | sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT; |
||
1867 | sdio_datainitstructure.DataLength = 64; |
||
1868 | sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_64B ; |
||
1869 | sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; |
||
1870 | sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK; |
||
1871 | sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE; |
||
1872 | SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure); |
||
1873 | |||
1874 | /* Send CMD6 switch mode */ |
||
1875 | sdio_cmdinitstructure.Argument = 0x80FFFF01; |
||
1876 | sdio_cmdinitstructure.CmdIndex = SD_CMD_HS_SWITCH; |
||
1877 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
1878 | |||
1879 | /* Check for error conditions */ |
||
1880 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_HS_SWITCH); |
||
1881 | |||
1882 | if (errorstate != SD_OK) |
||
1883 | { |
||
1884 | return errorstate; |
||
1885 | } |
||
1886 | |||
1887 | while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)) |
||
1888 | { |
||
1889 | if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF)) |
||
1890 | { |
||
1891 | for (count = 0; count < 8; count++) |
||
1892 | { |
||
1893 | *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance); |
||
1894 | } |
||
1895 | |||
1896 | tempbuff += 8; |
||
1897 | } |
||
1898 | } |
||
1899 | |||
1900 | if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT)) |
||
1901 | { |
||
1902 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT); |
||
1903 | |||
1904 | errorstate = SD_DATA_TIMEOUT; |
||
1905 | |||
1906 | return errorstate; |
||
1907 | } |
||
1908 | else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL)) |
||
1909 | { |
||
1910 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL); |
||
1911 | |||
1912 | errorstate = SD_DATA_CRC_FAIL; |
||
1913 | |||
1914 | return errorstate; |
||
1915 | } |
||
1916 | else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR)) |
||
1917 | { |
||
1918 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR); |
||
1919 | |||
1920 | errorstate = SD_RX_OVERRUN; |
||
1921 | |||
1922 | return errorstate; |
||
1923 | } |
||
1924 | else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR)) |
||
1925 | { |
||
1926 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR); |
||
1927 | |||
1928 | errorstate = SD_START_BIT_ERR; |
||
1929 | |||
1930 | return errorstate; |
||
1931 | } |
||
1932 | else |
||
1933 | { |
||
1934 | /* No error flag set */ |
||
1935 | } |
||
1936 | |||
1937 | count = SD_DATATIMEOUT; |
||
1938 | |||
1939 | while ((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (count > 0)) |
||
1940 | { |
||
1941 | *tempbuff = SDIO_ReadFIFO(hsd->Instance); |
||
1942 | tempbuff++; |
||
1943 | count--; |
||
1944 | } |
||
1945 | |||
1946 | /* Clear all the static flags */ |
||
1947 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
||
1948 | |||
1949 | /* Test if the switch mode HS is ok */ |
||
1950 | if ((SD_hs[13]& 2) != 2) |
||
1951 | { |
||
1952 | errorstate = SD_UNSUPPORTED_FEATURE; |
||
1953 | } |
||
1954 | } |
||
1955 | |||
1956 | return errorstate; |
||
1957 | } |
||
1958 | |||
1959 | /** |
||
1960 | * @} |
||
1961 | */ |
||
1962 | |||
1963 | /** @defgroup SD_Exported_Functions_Group4 Peripheral State functions |
||
1964 | * @brief Peripheral State functions |
||
1965 | * |
||
1966 | @verbatim |
||
1967 | ============================================================================== |
||
1968 | ##### Peripheral State functions ##### |
||
1969 | ============================================================================== |
||
1970 | [..] |
||
1971 | This subsection permits to get in runtime the status of the peripheral |
||
1972 | and the data flow. |
||
1973 | |||
1974 | @endverbatim |
||
1975 | * @{ |
||
1976 | */ |
||
1977 | |||
1978 | /** |
||
1979 | * @brief Returns the current SD card's status. |
||
1980 | * @param hsd: SD handle |
||
1981 | * @param pSDstatus: Pointer to the buffer that will contain the SD card status |
||
1982 | * SD Status register) |
||
1983 | * @retval SD Card error state |
||
1984 | */ |
||
1985 | HAL_SD_ErrorTypedef HAL_SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus) |
||
1986 | { |
||
1987 | SDIO_CmdInitTypeDef sdio_cmdinitstructure = {0}; |
||
1988 | SDIO_DataInitTypeDef sdio_datainitstructure = {0}; |
||
1989 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
1990 | uint32_t count = 0; |
||
1991 | |||
1992 | /* Check SD response */ |
||
1993 | if ((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED) |
||
1994 | { |
||
1995 | errorstate = SD_LOCK_UNLOCK_FAILED; |
||
1996 | |||
1997 | return errorstate; |
||
1998 | } |
||
1999 | |||
2000 | /* Set block size for card if it is not equal to current block size for card */ |
||
2001 | sdio_cmdinitstructure.Argument = 64; |
||
2002 | sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN; |
||
2003 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT; |
||
2004 | sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO; |
||
2005 | sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE; |
||
2006 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
2007 | |||
2008 | /* Check for error conditions */ |
||
2009 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN); |
||
2010 | |||
2011 | if (errorstate != SD_OK) |
||
2012 | { |
||
2013 | return errorstate; |
||
2014 | } |
||
2015 | |||
2016 | /* Send CMD55 */ |
||
2017 | sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16); |
||
2018 | sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD; |
||
2019 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
2020 | |||
2021 | /* Check for error conditions */ |
||
2022 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD); |
||
2023 | |||
2024 | if (errorstate != SD_OK) |
||
2025 | { |
||
2026 | return errorstate; |
||
2027 | } |
||
2028 | |||
2029 | /* Configure the SD DPSM (Data Path State Machine) */ |
||
2030 | sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT; |
||
2031 | sdio_datainitstructure.DataLength = 64; |
||
2032 | sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_64B; |
||
2033 | sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; |
||
2034 | sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK; |
||
2035 | sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE; |
||
2036 | SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure); |
||
2037 | |||
2038 | /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */ |
||
2039 | sdio_cmdinitstructure.Argument = 0; |
||
2040 | sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_STATUS; |
||
2041 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
2042 | |||
2043 | /* Check for error conditions */ |
||
2044 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_APP_STATUS); |
||
2045 | |||
2046 | if (errorstate != SD_OK) |
||
2047 | { |
||
2048 | return errorstate; |
||
2049 | } |
||
2050 | |||
2051 | /* Get status data */ |
||
2052 | while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)) |
||
2053 | { |
||
2054 | if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF)) |
||
2055 | { |
||
2056 | for (count = 0; count < 8; count++) |
||
2057 | { |
||
2058 | *(pSDstatus + count) = SDIO_ReadFIFO(hsd->Instance); |
||
2059 | } |
||
2060 | |||
2061 | pSDstatus += 8; |
||
2062 | } |
||
2063 | } |
||
2064 | |||
2065 | if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT)) |
||
2066 | { |
||
2067 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT); |
||
2068 | |||
2069 | errorstate = SD_DATA_TIMEOUT; |
||
2070 | |||
2071 | return errorstate; |
||
2072 | } |
||
2073 | else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL)) |
||
2074 | { |
||
2075 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL); |
||
2076 | |||
2077 | errorstate = SD_DATA_CRC_FAIL; |
||
2078 | |||
2079 | return errorstate; |
||
2080 | } |
||
2081 | else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR)) |
||
2082 | { |
||
2083 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR); |
||
2084 | |||
2085 | errorstate = SD_RX_OVERRUN; |
||
2086 | |||
2087 | return errorstate; |
||
2088 | } |
||
2089 | else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR)) |
||
2090 | { |
||
2091 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR); |
||
2092 | |||
2093 | errorstate = SD_START_BIT_ERR; |
||
2094 | |||
2095 | return errorstate; |
||
2096 | } |
||
2097 | else |
||
2098 | { |
||
2099 | /* No error flag set */ |
||
2100 | } |
||
2101 | |||
2102 | count = SD_DATATIMEOUT; |
||
2103 | while ((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (count > 0)) |
||
2104 | { |
||
2105 | *pSDstatus = SDIO_ReadFIFO(hsd->Instance); |
||
2106 | pSDstatus++; |
||
2107 | count--; |
||
2108 | } |
||
2109 | |||
2110 | /* Clear all the static status flags*/ |
||
2111 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
||
2112 | |||
2113 | return errorstate; |
||
2114 | } |
||
2115 | |||
2116 | /** |
||
2117 | * @brief Gets the current sd card data status. |
||
2118 | * @param hsd: SD handle |
||
2119 | * @retval Data Transfer state |
||
2120 | */ |
||
2121 | HAL_SD_TransferStateTypedef HAL_SD_GetStatus(SD_HandleTypeDef *hsd) |
||
2122 | { |
||
2123 | HAL_SD_CardStateTypedef cardstate = SD_CARD_TRANSFER; |
||
2124 | |||
2125 | /* Get SD card state */ |
||
2126 | cardstate = SD_GetState(hsd); |
||
2127 | |||
2128 | /* Find SD status according to card state*/ |
||
2129 | if (cardstate == SD_CARD_TRANSFER) |
||
2130 | { |
||
2131 | return SD_TRANSFER_OK; |
||
2132 | } |
||
2133 | else if(cardstate == SD_CARD_ERROR) |
||
2134 | { |
||
2135 | return SD_TRANSFER_ERROR; |
||
2136 | } |
||
2137 | else |
||
2138 | { |
||
2139 | return SD_TRANSFER_BUSY; |
||
2140 | } |
||
2141 | } |
||
2142 | |||
2143 | /** |
||
2144 | * @brief Gets the SD card status. |
||
2145 | * @param hsd: SD handle |
||
2146 | * @param pCardStatus: Pointer to the HAL_SD_CardStatusTypedef structure that |
||
2147 | * will contain the SD card status information |
||
2148 | * @retval SD Card error state |
||
2149 | */ |
||
2150 | HAL_SD_ErrorTypedef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypedef *pCardStatus) |
||
2151 | { |
||
2152 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
2153 | uint32_t tmp = 0; |
||
2154 | uint32_t sd_status[16]; |
||
2155 | |||
2156 | errorstate = HAL_SD_SendSDStatus(hsd, sd_status); |
||
2157 | |||
2158 | if (errorstate != SD_OK) |
||
2159 | { |
||
2160 | return errorstate; |
||
2161 | } |
||
2162 | |||
2163 | /* Byte 0 */ |
||
2164 | tmp = (sd_status[0] & 0xC0) >> 6; |
||
2165 | pCardStatus->DAT_BUS_WIDTH = (uint8_t)tmp; |
||
2166 | |||
2167 | /* Byte 0 */ |
||
2168 | tmp = (sd_status[0] & 0x20) >> 5; |
||
2169 | pCardStatus->SECURED_MODE = (uint8_t)tmp; |
||
2170 | |||
2171 | /* Byte 2 */ |
||
2172 | tmp = (sd_status[2] & 0xFF); |
||
2173 | pCardStatus->SD_CARD_TYPE = (uint8_t)(tmp << 8); |
||
2174 | |||
2175 | /* Byte 3 */ |
||
2176 | tmp = (sd_status[3] & 0xFF); |
||
2177 | pCardStatus->SD_CARD_TYPE |= (uint8_t)tmp; |
||
2178 | |||
2179 | /* Byte 4 */ |
||
2180 | tmp = (sd_status[4] & 0xFF); |
||
2181 | pCardStatus->SIZE_OF_PROTECTED_AREA = (uint8_t)(tmp << 24); |
||
2182 | |||
2183 | /* Byte 5 */ |
||
2184 | tmp = (sd_status[5] & 0xFF); |
||
2185 | pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)(tmp << 16); |
||
2186 | |||
2187 | /* Byte 6 */ |
||
2188 | tmp = (sd_status[6] & 0xFF); |
||
2189 | pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)(tmp << 8); |
||
2190 | |||
2191 | /* Byte 7 */ |
||
2192 | tmp = (sd_status[7] & 0xFF); |
||
2193 | pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)tmp; |
||
2194 | |||
2195 | /* Byte 8 */ |
||
2196 | tmp = (sd_status[8] & 0xFF); |
||
2197 | pCardStatus->SPEED_CLASS = (uint8_t)tmp; |
||
2198 | |||
2199 | /* Byte 9 */ |
||
2200 | tmp = (sd_status[9] & 0xFF); |
||
2201 | pCardStatus->PERFORMANCE_MOVE = (uint8_t)tmp; |
||
2202 | |||
2203 | /* Byte 10 */ |
||
2204 | tmp = (sd_status[10] & 0xF0) >> 4; |
||
2205 | pCardStatus->AU_SIZE = (uint8_t)tmp; |
||
2206 | |||
2207 | /* Byte 11 */ |
||
2208 | tmp = (sd_status[11] & 0xFF); |
||
2209 | pCardStatus->ERASE_SIZE = (uint8_t)(tmp << 8); |
||
2210 | |||
2211 | /* Byte 12 */ |
||
2212 | tmp = (sd_status[12] & 0xFF); |
||
2213 | pCardStatus->ERASE_SIZE |= (uint8_t)tmp; |
||
2214 | |||
2215 | /* Byte 13 */ |
||
2216 | tmp = (sd_status[13] & 0xFC) >> 2; |
||
2217 | pCardStatus->ERASE_TIMEOUT = (uint8_t)tmp; |
||
2218 | |||
2219 | /* Byte 13 */ |
||
2220 | tmp = (sd_status[13] & 0x3); |
||
2221 | pCardStatus->ERASE_OFFSET = (uint8_t)tmp; |
||
2222 | |||
2223 | return errorstate; |
||
2224 | } |
||
2225 | |||
2226 | /** |
||
2227 | * @} |
||
2228 | */ |
||
2229 | |||
2230 | /** |
||
2231 | * @} |
||
2232 | */ |
||
2233 | |||
2234 | /** @addtogroup SD_Private_Functions |
||
2235 | * @{ |
||
2236 | */ |
||
2237 | |||
2238 | /** |
||
2239 | * @brief SD DMA transfer complete Rx callback. |
||
2240 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
||
2241 | * the configuration information for the specified DMA module. |
||
2242 | * @retval None |
||
2243 | */ |
||
2244 | static void SD_DMA_RxCplt(DMA_HandleTypeDef *hdma) |
||
2245 | { |
||
2246 | SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
||
2247 | |||
2248 | /* DMA transfer is complete */ |
||
2249 | hsd->DmaTransferCplt = 1; |
||
2250 | |||
2251 | /* Wait until SD transfer is complete */ |
||
2252 | while(hsd->SdTransferCplt == 0) |
||
2253 | { |
||
2254 | } |
||
2255 | |||
2256 | /* Transfer complete user callback */ |
||
2257 | HAL_SD_DMA_RxCpltCallback(hsd->hdmarx); |
||
2258 | } |
||
2259 | |||
2260 | /** |
||
2261 | * @brief SD DMA transfer Error Rx callback. |
||
2262 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
||
2263 | * the configuration information for the specified DMA module. |
||
2264 | * @retval None |
||
2265 | */ |
||
2266 | static void SD_DMA_RxError(DMA_HandleTypeDef *hdma) |
||
2267 | { |
||
2268 | SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
||
2269 | |||
2270 | /* Transfer complete user callback */ |
||
2271 | HAL_SD_DMA_RxErrorCallback(hsd->hdmarx); |
||
2272 | } |
||
2273 | |||
2274 | /** |
||
2275 | * @brief SD DMA transfer complete Tx callback. |
||
2276 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
||
2277 | * the configuration information for the specified DMA module. |
||
2278 | * @retval None |
||
2279 | */ |
||
2280 | static void SD_DMA_TxCplt(DMA_HandleTypeDef *hdma) |
||
2281 | { |
||
2282 | SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
||
2283 | |||
2284 | /* DMA transfer is complete */ |
||
2285 | hsd->DmaTransferCplt = 1; |
||
2286 | |||
2287 | /* Wait until SD transfer is complete */ |
||
2288 | while(hsd->SdTransferCplt == 0) |
||
2289 | { |
||
2290 | } |
||
2291 | |||
2292 | /* Transfer complete user callback */ |
||
2293 | HAL_SD_DMA_TxCpltCallback(hsd->hdmatx); |
||
2294 | } |
||
2295 | |||
2296 | /** |
||
2297 | * @brief SD DMA transfer Error Tx callback. |
||
2298 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains |
||
2299 | * the configuration information for the specified DMA module. |
||
2300 | * @retval None |
||
2301 | */ |
||
2302 | static void SD_DMA_TxError(DMA_HandleTypeDef *hdma) |
||
2303 | { |
||
2304 | SD_HandleTypeDef *hsd = ( SD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
||
2305 | |||
2306 | /* Transfer complete user callback */ |
||
2307 | HAL_SD_DMA_TxErrorCallback(hsd->hdmatx); |
||
2308 | } |
||
2309 | |||
2310 | /** |
||
2311 | * @brief Returns the SD current state. |
||
2312 | * @param hsd: SD handle |
||
2313 | * @retval SD card current state |
||
2314 | */ |
||
2315 | static HAL_SD_CardStateTypedef SD_GetState(SD_HandleTypeDef *hsd) |
||
2316 | { |
||
2317 | uint32_t resp1 = 0; |
||
2318 | |||
2319 | if (SD_SendStatus(hsd, &resp1) != SD_OK) |
||
2320 | { |
||
2321 | return SD_CARD_ERROR; |
||
2322 | } |
||
2323 | else |
||
2324 | { |
||
2325 | return (HAL_SD_CardStateTypedef)((resp1 >> 9) & 0x0F); |
||
2326 | } |
||
2327 | } |
||
2328 | |||
2329 | /** |
||
2330 | * @brief Initializes all cards or single card as the case may be Card(s) come |
||
2331 | * into standby state. |
||
2332 | * @param hsd: SD handle |
||
2333 | * @retval SD Card error state |
||
2334 | */ |
||
2335 | static HAL_SD_ErrorTypedef SD_Initialize_Cards(SD_HandleTypeDef *hsd) |
||
2336 | { |
||
2337 | SDIO_CmdInitTypeDef sdio_cmdinitstructure = {0}; |
||
2338 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
2339 | uint16_t sd_rca = 1; |
||
2340 | |||
2341 | if(SDIO_GetPowerState(hsd->Instance) == 0) /* Power off */ |
||
2342 | { |
||
2343 | errorstate = SD_REQUEST_NOT_APPLICABLE; |
||
2344 | |||
2345 | return errorstate; |
||
2346 | } |
||
2347 | |||
2348 | if(hsd->CardType != SECURE_DIGITAL_IO_CARD) |
||
2349 | { |
||
2350 | /* Send CMD2 ALL_SEND_CID */ |
||
2351 | sdio_cmdinitstructure.Argument = 0; |
||
2352 | sdio_cmdinitstructure.CmdIndex = SD_CMD_ALL_SEND_CID; |
||
2353 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_LONG; |
||
2354 | sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO; |
||
2355 | sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE; |
||
2356 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
2357 | |||
2358 | /* Check for error conditions */ |
||
2359 | errorstate = SD_CmdResp2Error(hsd); |
||
2360 | |||
2361 | if(errorstate != SD_OK) |
||
2362 | { |
||
2363 | return errorstate; |
||
2364 | } |
||
2365 | |||
2366 | /* Get Card identification number data */ |
||
2367 | hsd->CID[0] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); |
||
2368 | hsd->CID[1] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2); |
||
2369 | hsd->CID[2] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3); |
||
2370 | hsd->CID[3] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4); |
||
2371 | } |
||
2372 | |||
2373 | if((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\ |
||
2374 | (hsd->CardType == SECURE_DIGITAL_IO_COMBO_CARD) || (hsd->CardType == HIGH_CAPACITY_SD_CARD)) |
||
2375 | { |
||
2376 | /* Send CMD3 SET_REL_ADDR with argument 0 */ |
||
2377 | /* SD Card publishes its RCA. */ |
||
2378 | sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_REL_ADDR; |
||
2379 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT; |
||
2380 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
2381 | |||
2382 | /* Check for error conditions */ |
||
2383 | errorstate = SD_CmdResp6Error(hsd, SD_CMD_SET_REL_ADDR, &sd_rca); |
||
2384 | |||
2385 | if(errorstate != SD_OK) |
||
2386 | { |
||
2387 | return errorstate; |
||
2388 | } |
||
2389 | } |
||
2390 | |||
2391 | if (hsd->CardType != SECURE_DIGITAL_IO_CARD) |
||
2392 | { |
||
2393 | /* Get the SD card RCA */ |
||
2394 | hsd->RCA = sd_rca; |
||
2395 | |||
2396 | /* Send CMD9 SEND_CSD with argument as card's RCA */ |
||
2397 | sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16); |
||
2398 | sdio_cmdinitstructure.CmdIndex = SD_CMD_SEND_CSD; |
||
2399 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_LONG; |
||
2400 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
2401 | |||
2402 | /* Check for error conditions */ |
||
2403 | errorstate = SD_CmdResp2Error(hsd); |
||
2404 | |||
2405 | if(errorstate != SD_OK) |
||
2406 | { |
||
2407 | return errorstate; |
||
2408 | } |
||
2409 | |||
2410 | /* Get Card Specific Data */ |
||
2411 | hsd->CSD[0] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); |
||
2412 | hsd->CSD[1] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2); |
||
2413 | hsd->CSD[2] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3); |
||
2414 | hsd->CSD[3] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4); |
||
2415 | } |
||
2416 | |||
2417 | /* All cards are initialized */ |
||
2418 | return errorstate; |
||
2419 | } |
||
2420 | |||
2421 | /** |
||
2422 | * @brief Selects od Deselects the corresponding card. |
||
2423 | * @param hsd: SD handle |
||
2424 | * @param Addr: Address of the card to be selected |
||
2425 | * @retval SD Card error state |
||
2426 | */ |
||
2427 | static HAL_SD_ErrorTypedef SD_Select_Deselect(SD_HandleTypeDef *hsd, uint64_t Addr) |
||
2428 | { |
||
2429 | SDIO_CmdInitTypeDef sdio_cmdinitstructure = {0}; |
||
2430 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
2431 | |||
2432 | /* Send CMD7 SDIO_SEL_DESEL_CARD */ |
||
2433 | sdio_cmdinitstructure.Argument = (uint32_t)Addr; |
||
2434 | sdio_cmdinitstructure.CmdIndex = SD_CMD_SEL_DESEL_CARD; |
||
2435 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT; |
||
2436 | sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO; |
||
2437 | sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE; |
||
2438 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
2439 | |||
2440 | /* Check for error conditions */ |
||
2441 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_SEL_DESEL_CARD); |
||
2442 | |||
2443 | return errorstate; |
||
2444 | } |
||
2445 | |||
2446 | /** |
||
2447 | * @brief Enquires cards about their operating voltage and configures clock |
||
2448 | * controls and stores SD information that will be needed in future |
||
2449 | * in the SD handle. |
||
2450 | * @param hsd: SD handle |
||
2451 | * @retval SD Card error state |
||
2452 | */ |
||
2453 | static HAL_SD_ErrorTypedef SD_PowerON(SD_HandleTypeDef *hsd) |
||
2454 | { |
||
2455 | SDIO_CmdInitTypeDef sdio_cmdinitstructure = {0}; |
||
2456 | __IO HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
2457 | uint32_t response = 0, count = 0, validvoltage = 0; |
||
2458 | uint32_t sdtype = SD_STD_CAPACITY; |
||
2459 | |||
2460 | /* Power ON Sequence -------------------------------------------------------*/ |
||
2461 | /* Disable SDIO Clock */ |
||
2462 | __HAL_SD_SDIO_DISABLE(hsd); |
||
2463 | |||
2464 | /* Set Power State to ON */ |
||
2465 | SDIO_PowerState_ON(hsd->Instance); |
||
2466 | |||
2467 | /* 1ms: required power up waiting time before starting the SD initialization |
||
2468 | sequence */ |
||
2469 | HAL_Delay(1); |
||
2470 | |||
2471 | /* Enable SDIO Clock */ |
||
2472 | __HAL_SD_SDIO_ENABLE(hsd); |
||
2473 | |||
2474 | /* CMD0: GO_IDLE_STATE -----------------------------------------------------*/ |
||
2475 | /* No CMD response required */ |
||
2476 | sdio_cmdinitstructure.Argument = 0; |
||
2477 | sdio_cmdinitstructure.CmdIndex = SD_CMD_GO_IDLE_STATE; |
||
2478 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_NO; |
||
2479 | sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO; |
||
2480 | sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE; |
||
2481 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
2482 | |||
2483 | /* Check for error conditions */ |
||
2484 | errorstate = SD_CmdError(hsd); |
||
2485 | |||
2486 | if(errorstate != SD_OK) |
||
2487 | { |
||
2488 | /* CMD Response TimeOut (wait for CMDSENT flag) */ |
||
2489 | return errorstate; |
||
2490 | } |
||
2491 | |||
2492 | /* CMD8: SEND_IF_COND ------------------------------------------------------*/ |
||
2493 | /* Send CMD8 to verify SD card interface operating condition */ |
||
2494 | /* Argument: - [31:12]: Reserved (shall be set to '0') |
||
2495 | - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V) |
||
2496 | - [7:0]: Check Pattern (recommended 0xAA) */ |
||
2497 | /* CMD Response: R7 */ |
||
2498 | sdio_cmdinitstructure.Argument = SD_CHECK_PATTERN; |
||
2499 | sdio_cmdinitstructure.CmdIndex = SD_SDIO_SEND_IF_COND; |
||
2500 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT; |
||
2501 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
2502 | |||
2503 | /* Check for error conditions */ |
||
2504 | errorstate = SD_CmdResp7Error(hsd); |
||
2505 | |||
2506 | if (errorstate == SD_OK) |
||
2507 | { |
||
2508 | /* SD Card 2.0 */ |
||
2509 | hsd->CardType = STD_CAPACITY_SD_CARD_V2_0; |
||
2510 | sdtype = SD_HIGH_CAPACITY; |
||
2511 | } |
||
2512 | |||
2513 | /* Send CMD55 */ |
||
2514 | sdio_cmdinitstructure.Argument = 0; |
||
2515 | sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD; |
||
2516 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
2517 | |||
2518 | /* Check for error conditions */ |
||
2519 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD); |
||
2520 | |||
2521 | /* If errorstate is Command TimeOut, it is a MMC card */ |
||
2522 | /* If errorstate is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch) |
||
2523 | or SD card 1.x */ |
||
2524 | if(errorstate == SD_OK) |
||
2525 | { |
||
2526 | /* SD CARD */ |
||
2527 | /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */ |
||
2528 | while((!validvoltage) && (count < SD_MAX_VOLT_TRIAL)) |
||
2529 | { |
||
2530 | |||
2531 | /* SEND CMD55 APP_CMD with RCA as 0 */ |
||
2532 | sdio_cmdinitstructure.Argument = 0; |
||
2533 | sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD; |
||
2534 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT; |
||
2535 | sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO; |
||
2536 | sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE; |
||
2537 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
2538 | |||
2539 | /* Check for error conditions */ |
||
2540 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD); |
||
2541 | |||
2542 | if(errorstate != SD_OK) |
||
2543 | { |
||
2544 | return errorstate; |
||
2545 | } |
||
2546 | |||
2547 | /* Send CMD41 */ |
||
2548 | sdio_cmdinitstructure.Argument = SD_VOLTAGE_WINDOW_SD | sdtype; |
||
2549 | sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_OP_COND; |
||
2550 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT; |
||
2551 | sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO; |
||
2552 | sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE; |
||
2553 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
2554 | |||
2555 | /* Check for error conditions */ |
||
2556 | errorstate = SD_CmdResp3Error(hsd); |
||
2557 | |||
2558 | if(errorstate != SD_OK) |
||
2559 | { |
||
2560 | return errorstate; |
||
2561 | } |
||
2562 | |||
2563 | /* Get command response */ |
||
2564 | response = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); |
||
2565 | |||
2566 | /* Get operating voltage*/ |
||
2567 | validvoltage = (((response >> 31) == 1) ? 1 : 0); |
||
2568 | |||
2569 | count++; |
||
2570 | } |
||
2571 | |||
2572 | if(count >= SD_MAX_VOLT_TRIAL) |
||
2573 | { |
||
2574 | errorstate = SD_INVALID_VOLTRANGE; |
||
2575 | |||
2576 | return errorstate; |
||
2577 | } |
||
2578 | |||
2579 | if((response & SD_HIGH_CAPACITY) == SD_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */ |
||
2580 | { |
||
2581 | hsd->CardType = HIGH_CAPACITY_SD_CARD; |
||
2582 | } |
||
2583 | |||
2584 | } /* else MMC Card */ |
||
2585 | |||
2586 | return errorstate; |
||
2587 | } |
||
2588 | |||
2589 | /** |
||
2590 | * @brief Turns the SDIO output signals off. |
||
2591 | * @param hsd: SD handle |
||
2592 | * @retval SD Card error state |
||
2593 | */ |
||
2594 | static HAL_SD_ErrorTypedef SD_PowerOFF(SD_HandleTypeDef *hsd) |
||
2595 | { |
||
2596 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
2597 | |||
2598 | /* Set Power State to OFF */ |
||
2599 | SDIO_PowerState_OFF(hsd->Instance); |
||
2600 | |||
2601 | return errorstate; |
||
2602 | } |
||
2603 | |||
2604 | /** |
||
2605 | * @brief Returns the current card's status. |
||
2606 | * @param hsd: SD handle |
||
2607 | * @param pCardStatus: pointer to the buffer that will contain the SD card |
||
2608 | * status (Card Status register) |
||
2609 | * @retval SD Card error state |
||
2610 | */ |
||
2611 | static HAL_SD_ErrorTypedef SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus) |
||
2612 | { |
||
2613 | SDIO_CmdInitTypeDef sdio_cmdinitstructure = {0}; |
||
2614 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
2615 | |||
2616 | if(pCardStatus == NULL) |
||
2617 | { |
||
2618 | errorstate = SD_INVALID_PARAMETER; |
||
2619 | |||
2620 | return errorstate; |
||
2621 | } |
||
2622 | |||
2623 | /* Send Status command */ |
||
2624 | sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16); |
||
2625 | sdio_cmdinitstructure.CmdIndex = SD_CMD_SEND_STATUS; |
||
2626 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT; |
||
2627 | sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO; |
||
2628 | sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE; |
||
2629 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
2630 | |||
2631 | /* Check for error conditions */ |
||
2632 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_SEND_STATUS); |
||
2633 | |||
2634 | if(errorstate != SD_OK) |
||
2635 | { |
||
2636 | return errorstate; |
||
2637 | } |
||
2638 | |||
2639 | /* Get SD card status */ |
||
2640 | *pCardStatus = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); |
||
2641 | |||
2642 | return errorstate; |
||
2643 | } |
||
2644 | |||
2645 | /** |
||
2646 | * @brief Checks for error conditions for CMD0. |
||
2647 | * @param hsd: SD handle |
||
2648 | * @retval SD Card error state |
||
2649 | */ |
||
2650 | static HAL_SD_ErrorTypedef SD_CmdError(SD_HandleTypeDef *hsd) |
||
2651 | { |
||
2652 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
2653 | uint32_t timeout = SDIO_CMD0TIMEOUT, tmp; |
||
2654 | |||
2655 | tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CMDSENT); |
||
2656 | |||
2657 | while((timeout > 0) && (!tmp)) |
||
2658 | { |
||
2659 | tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CMDSENT); |
||
2660 | timeout--; |
||
2661 | } |
||
2662 | |||
2663 | if(timeout == 0) |
||
2664 | { |
||
2665 | errorstate = SD_CMD_RSP_TIMEOUT; |
||
2666 | return errorstate; |
||
2667 | } |
||
2668 | |||
2669 | /* Clear all the static flags */ |
||
2670 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
||
2671 | |||
2672 | return errorstate; |
||
2673 | } |
||
2674 | |||
2675 | /** |
||
2676 | * @brief Checks for error conditions for R7 response. |
||
2677 | * @param hsd: SD handle |
||
2678 | * @retval SD Card error state |
||
2679 | */ |
||
2680 | static HAL_SD_ErrorTypedef SD_CmdResp7Error(SD_HandleTypeDef *hsd) |
||
2681 | { |
||
2682 | HAL_SD_ErrorTypedef errorstate = SD_ERROR; |
||
2683 | uint32_t timeout = SDIO_CMD0TIMEOUT, tmp; |
||
2684 | |||
2685 | tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT); |
||
2686 | |||
2687 | while((!tmp) && (timeout > 0)) |
||
2688 | { |
||
2689 | tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT); |
||
2690 | timeout--; |
||
2691 | } |
||
2692 | |||
2693 | tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT); |
||
2694 | |||
2695 | if((timeout == 0) || tmp) |
||
2696 | { |
||
2697 | /* Card is not V2.0 compliant or card does not support the set voltage range */ |
||
2698 | errorstate = SD_CMD_RSP_TIMEOUT; |
||
2699 | |||
2700 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT); |
||
2701 | |||
2702 | return errorstate; |
||
2703 | } |
||
2704 | |||
2705 | if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CMDREND)) |
||
2706 | { |
||
2707 | /* Card is SD V2.0 compliant */ |
||
2708 | errorstate = SD_OK; |
||
2709 | |||
2710 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CMDREND); |
||
2711 | |||
2712 | return errorstate; |
||
2713 | } |
||
2714 | |||
2715 | return errorstate; |
||
2716 | } |
||
2717 | |||
2718 | /** |
||
2719 | * @brief Checks for error conditions for R1 response. |
||
2720 | * @param hsd: SD handle |
||
2721 | * @param SD_CMD: The sent command index |
||
2722 | * @retval SD Card error state |
||
2723 | */ |
||
2724 | static HAL_SD_ErrorTypedef SD_CmdResp1Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD) |
||
2725 | { |
||
2726 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
2727 | uint32_t response_r1 = 0; |
||
2728 | |||
2729 | while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)) |
||
2730 | { |
||
2731 | } |
||
2732 | |||
2733 | if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT)) |
||
2734 | { |
||
2735 | errorstate = SD_CMD_RSP_TIMEOUT; |
||
2736 | |||
2737 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT); |
||
2738 | |||
2739 | return errorstate; |
||
2740 | } |
||
2741 | else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL)) |
||
2742 | { |
||
2743 | errorstate = SD_CMD_CRC_FAIL; |
||
2744 | |||
2745 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL); |
||
2746 | |||
2747 | return errorstate; |
||
2748 | } |
||
2749 | else |
||
2750 | { |
||
2751 | /* No error flag set */ |
||
2752 | } |
||
2753 | |||
2754 | /* Check response received is of desired command */ |
||
2755 | if(SDIO_GetCommandResponse(hsd->Instance) != SD_CMD) |
||
2756 | { |
||
2757 | errorstate = SD_ILLEGAL_CMD; |
||
2758 | |||
2759 | return errorstate; |
||
2760 | } |
||
2761 | |||
2762 | /* Clear all the static flags */ |
||
2763 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
||
2764 | |||
2765 | /* We have received response, retrieve it for analysis */ |
||
2766 | response_r1 = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); |
||
2767 | |||
2768 | if((response_r1 & SD_OCR_ERRORBITS) == SD_ALLZERO) |
||
2769 | { |
||
2770 | return errorstate; |
||
2771 | } |
||
2772 | |||
2773 | if((response_r1 & SD_OCR_ADDR_OUT_OF_RANGE) == SD_OCR_ADDR_OUT_OF_RANGE) |
||
2774 | { |
||
2775 | return(SD_ADDR_OUT_OF_RANGE); |
||
2776 | } |
||
2777 | |||
2778 | if((response_r1 & SD_OCR_ADDR_MISALIGNED) == SD_OCR_ADDR_MISALIGNED) |
||
2779 | { |
||
2780 | return(SD_ADDR_MISALIGNED); |
||
2781 | } |
||
2782 | |||
2783 | if((response_r1 & SD_OCR_BLOCK_LEN_ERR) == SD_OCR_BLOCK_LEN_ERR) |
||
2784 | { |
||
2785 | return(SD_BLOCK_LEN_ERR); |
||
2786 | } |
||
2787 | |||
2788 | if((response_r1 & SD_OCR_ERASE_SEQ_ERR) == SD_OCR_ERASE_SEQ_ERR) |
||
2789 | { |
||
2790 | return(SD_ERASE_SEQ_ERR); |
||
2791 | } |
||
2792 | |||
2793 | if((response_r1 & SD_OCR_BAD_ERASE_PARAM) == SD_OCR_BAD_ERASE_PARAM) |
||
2794 | { |
||
2795 | return(SD_BAD_ERASE_PARAM); |
||
2796 | } |
||
2797 | |||
2798 | if((response_r1 & SD_OCR_WRITE_PROT_VIOLATION) == SD_OCR_WRITE_PROT_VIOLATION) |
||
2799 | { |
||
2800 | return(SD_WRITE_PROT_VIOLATION); |
||
2801 | } |
||
2802 | |||
2803 | if((response_r1 & SD_OCR_LOCK_UNLOCK_FAILED) == SD_OCR_LOCK_UNLOCK_FAILED) |
||
2804 | { |
||
2805 | return(SD_LOCK_UNLOCK_FAILED); |
||
2806 | } |
||
2807 | |||
2808 | if((response_r1 & SD_OCR_COM_CRC_FAILED) == SD_OCR_COM_CRC_FAILED) |
||
2809 | { |
||
2810 | return(SD_COM_CRC_FAILED); |
||
2811 | } |
||
2812 | |||
2813 | if((response_r1 & SD_OCR_ILLEGAL_CMD) == SD_OCR_ILLEGAL_CMD) |
||
2814 | { |
||
2815 | return(SD_ILLEGAL_CMD); |
||
2816 | } |
||
2817 | |||
2818 | if((response_r1 & SD_OCR_CARD_ECC_FAILED) == SD_OCR_CARD_ECC_FAILED) |
||
2819 | { |
||
2820 | return(SD_CARD_ECC_FAILED); |
||
2821 | } |
||
2822 | |||
2823 | if((response_r1 & SD_OCR_CC_ERROR) == SD_OCR_CC_ERROR) |
||
2824 | { |
||
2825 | return(SD_CC_ERROR); |
||
2826 | } |
||
2827 | |||
2828 | if((response_r1 & SD_OCR_GENERAL_UNKNOWN_ERROR) == SD_OCR_GENERAL_UNKNOWN_ERROR) |
||
2829 | { |
||
2830 | return(SD_GENERAL_UNKNOWN_ERROR); |
||
2831 | } |
||
2832 | |||
2833 | if((response_r1 & SD_OCR_STREAM_READ_UNDERRUN) == SD_OCR_STREAM_READ_UNDERRUN) |
||
2834 | { |
||
2835 | return(SD_STREAM_READ_UNDERRUN); |
||
2836 | } |
||
2837 | |||
2838 | if((response_r1 & SD_OCR_STREAM_WRITE_OVERRUN) == SD_OCR_STREAM_WRITE_OVERRUN) |
||
2839 | { |
||
2840 | return(SD_STREAM_WRITE_OVERRUN); |
||
2841 | } |
||
2842 | |||
2843 | if((response_r1 & SD_OCR_CID_CSD_OVERWRITE) == SD_OCR_CID_CSD_OVERWRITE) |
||
2844 | { |
||
2845 | return(SD_CID_CSD_OVERWRITE); |
||
2846 | } |
||
2847 | |||
2848 | if((response_r1 & SD_OCR_WP_ERASE_SKIP) == SD_OCR_WP_ERASE_SKIP) |
||
2849 | { |
||
2850 | return(SD_WP_ERASE_SKIP); |
||
2851 | } |
||
2852 | |||
2853 | if((response_r1 & SD_OCR_CARD_ECC_DISABLED) == SD_OCR_CARD_ECC_DISABLED) |
||
2854 | { |
||
2855 | return(SD_CARD_ECC_DISABLED); |
||
2856 | } |
||
2857 | |||
2858 | if((response_r1 & SD_OCR_ERASE_RESET) == SD_OCR_ERASE_RESET) |
||
2859 | { |
||
2860 | return(SD_ERASE_RESET); |
||
2861 | } |
||
2862 | |||
2863 | if((response_r1 & SD_OCR_AKE_SEQ_ERROR) == SD_OCR_AKE_SEQ_ERROR) |
||
2864 | { |
||
2865 | return(SD_AKE_SEQ_ERROR); |
||
2866 | } |
||
2867 | |||
2868 | return errorstate; |
||
2869 | } |
||
2870 | |||
2871 | /** |
||
2872 | * @brief Checks for error conditions for R3 (OCR) response. |
||
2873 | * @param hsd: SD handle |
||
2874 | * @retval SD Card error state |
||
2875 | */ |
||
2876 | static HAL_SD_ErrorTypedef SD_CmdResp3Error(SD_HandleTypeDef *hsd) |
||
2877 | { |
||
2878 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
2879 | |||
2880 | while (!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)) |
||
2881 | { |
||
2882 | } |
||
2883 | |||
2884 | if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT)) |
||
2885 | { |
||
2886 | errorstate = SD_CMD_RSP_TIMEOUT; |
||
2887 | |||
2888 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT); |
||
2889 | |||
2890 | return errorstate; |
||
2891 | } |
||
2892 | |||
2893 | /* Clear all the static flags */ |
||
2894 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
||
2895 | |||
2896 | return errorstate; |
||
2897 | } |
||
2898 | |||
2899 | /** |
||
2900 | * @brief Checks for error conditions for R2 (CID or CSD) response. |
||
2901 | * @param hsd: SD handle |
||
2902 | * @retval SD Card error state |
||
2903 | */ |
||
2904 | static HAL_SD_ErrorTypedef SD_CmdResp2Error(SD_HandleTypeDef *hsd) |
||
2905 | { |
||
2906 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
2907 | |||
2908 | while (!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)) |
||
2909 | { |
||
2910 | } |
||
2911 | |||
2912 | if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT)) |
||
2913 | { |
||
2914 | errorstate = SD_CMD_RSP_TIMEOUT; |
||
2915 | |||
2916 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT); |
||
2917 | |||
2918 | return errorstate; |
||
2919 | } |
||
2920 | else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL)) |
||
2921 | { |
||
2922 | errorstate = SD_CMD_CRC_FAIL; |
||
2923 | |||
2924 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL); |
||
2925 | |||
2926 | return errorstate; |
||
2927 | } |
||
2928 | else |
||
2929 | { |
||
2930 | /* No error flag set */ |
||
2931 | } |
||
2932 | |||
2933 | /* Clear all the static flags */ |
||
2934 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
||
2935 | |||
2936 | return errorstate; |
||
2937 | } |
||
2938 | |||
2939 | /** |
||
2940 | * @brief Checks for error conditions for R6 (RCA) response. |
||
2941 | * @param hsd: SD handle |
||
2942 | * @param SD_CMD: The sent command index |
||
2943 | * @param pRCA: Pointer to the variable that will contain the SD card relative |
||
2944 | * address RCA |
||
2945 | * @retval SD Card error state |
||
2946 | */ |
||
2947 | static HAL_SD_ErrorTypedef SD_CmdResp6Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD, uint16_t *pRCA) |
||
2948 | { |
||
2949 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
2950 | uint32_t response_r1 = 0; |
||
2951 | |||
2952 | while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)) |
||
2953 | { |
||
2954 | } |
||
2955 | |||
2956 | if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT)) |
||
2957 | { |
||
2958 | errorstate = SD_CMD_RSP_TIMEOUT; |
||
2959 | |||
2960 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT); |
||
2961 | |||
2962 | return errorstate; |
||
2963 | } |
||
2964 | else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL)) |
||
2965 | { |
||
2966 | errorstate = SD_CMD_CRC_FAIL; |
||
2967 | |||
2968 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL); |
||
2969 | |||
2970 | return errorstate; |
||
2971 | } |
||
2972 | else |
||
2973 | { |
||
2974 | /* No error flag set */ |
||
2975 | } |
||
2976 | |||
2977 | /* Check response received is of desired command */ |
||
2978 | if(SDIO_GetCommandResponse(hsd->Instance) != SD_CMD) |
||
2979 | { |
||
2980 | errorstate = SD_ILLEGAL_CMD; |
||
2981 | |||
2982 | return errorstate; |
||
2983 | } |
||
2984 | |||
2985 | /* Clear all the static flags */ |
||
2986 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
||
2987 | |||
2988 | /* We have received response, retrieve it. */ |
||
2989 | response_r1 = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); |
||
2990 | |||
2991 | if((response_r1 & (SD_R6_GENERAL_UNKNOWN_ERROR | SD_R6_ILLEGAL_CMD | SD_R6_COM_CRC_FAILED)) == SD_ALLZERO) |
||
2992 | { |
||
2993 | *pRCA = (uint16_t) (response_r1 >> 16); |
||
2994 | |||
2995 | return errorstate; |
||
2996 | } |
||
2997 | |||
2998 | if((response_r1 & SD_R6_GENERAL_UNKNOWN_ERROR) == SD_R6_GENERAL_UNKNOWN_ERROR) |
||
2999 | { |
||
3000 | return(SD_GENERAL_UNKNOWN_ERROR); |
||
3001 | } |
||
3002 | |||
3003 | if((response_r1 & SD_R6_ILLEGAL_CMD) == SD_R6_ILLEGAL_CMD) |
||
3004 | { |
||
3005 | return(SD_ILLEGAL_CMD); |
||
3006 | } |
||
3007 | |||
3008 | if((response_r1 & SD_R6_COM_CRC_FAILED) == SD_R6_COM_CRC_FAILED) |
||
3009 | { |
||
3010 | return(SD_COM_CRC_FAILED); |
||
3011 | } |
||
3012 | |||
3013 | return errorstate; |
||
3014 | } |
||
3015 | |||
3016 | /** |
||
3017 | * @brief Enables the SDIO wide bus mode. |
||
3018 | * @param hsd: SD handle |
||
3019 | * @retval SD Card error state |
||
3020 | */ |
||
3021 | static HAL_SD_ErrorTypedef SD_WideBus_Enable(SD_HandleTypeDef *hsd) |
||
3022 | { |
||
3023 | SDIO_CmdInitTypeDef sdio_cmdinitstructure = {0}; |
||
3024 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
3025 | |||
3026 | uint32_t scr[2] = {0, 0}; |
||
3027 | |||
3028 | if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED) |
||
3029 | { |
||
3030 | errorstate = SD_LOCK_UNLOCK_FAILED; |
||
3031 | |||
3032 | return errorstate; |
||
3033 | } |
||
3034 | |||
3035 | /* Get SCR Register */ |
||
3036 | errorstate = SD_FindSCR(hsd, scr); |
||
3037 | |||
3038 | if(errorstate != SD_OK) |
||
3039 | { |
||
3040 | return errorstate; |
||
3041 | } |
||
3042 | |||
3043 | /* If requested card supports wide bus operation */ |
||
3044 | if((scr[1] & SD_WIDE_BUS_SUPPORT) != SD_ALLZERO) |
||
3045 | { |
||
3046 | /* Send CMD55 APP_CMD with argument as card's RCA.*/ |
||
3047 | sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16); |
||
3048 | sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD; |
||
3049 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT; |
||
3050 | sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO; |
||
3051 | sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE; |
||
3052 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
3053 | |||
3054 | /* Check for error conditions */ |
||
3055 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD); |
||
3056 | |||
3057 | if(errorstate != SD_OK) |
||
3058 | { |
||
3059 | return errorstate; |
||
3060 | } |
||
3061 | |||
3062 | /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */ |
||
3063 | sdio_cmdinitstructure.Argument = 2; |
||
3064 | sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH; |
||
3065 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
3066 | |||
3067 | /* Check for error conditions */ |
||
3068 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_SD_SET_BUSWIDTH); |
||
3069 | |||
3070 | if(errorstate != SD_OK) |
||
3071 | { |
||
3072 | return errorstate; |
||
3073 | } |
||
3074 | |||
3075 | return errorstate; |
||
3076 | } |
||
3077 | else |
||
3078 | { |
||
3079 | errorstate = SD_REQUEST_NOT_APPLICABLE; |
||
3080 | |||
3081 | return errorstate; |
||
3082 | } |
||
3083 | } |
||
3084 | |||
3085 | /** |
||
3086 | * @brief Disables the SDIO wide bus mode. |
||
3087 | * @param hsd: SD handle |
||
3088 | * @retval SD Card error state |
||
3089 | */ |
||
3090 | static HAL_SD_ErrorTypedef SD_WideBus_Disable(SD_HandleTypeDef *hsd) |
||
3091 | { |
||
3092 | SDIO_CmdInitTypeDef sdio_cmdinitstructure = {0}; |
||
3093 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
3094 | |||
3095 | uint32_t scr[2] = {0, 0}; |
||
3096 | |||
3097 | if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED) |
||
3098 | { |
||
3099 | errorstate = SD_LOCK_UNLOCK_FAILED; |
||
3100 | |||
3101 | return errorstate; |
||
3102 | } |
||
3103 | |||
3104 | /* Get SCR Register */ |
||
3105 | errorstate = SD_FindSCR(hsd, scr); |
||
3106 | |||
3107 | if(errorstate != SD_OK) |
||
3108 | { |
||
3109 | return errorstate; |
||
3110 | } |
||
3111 | |||
3112 | /* If requested card supports 1 bit mode operation */ |
||
3113 | if((scr[1] & SD_SINGLE_BUS_SUPPORT) != SD_ALLZERO) |
||
3114 | { |
||
3115 | /* Send CMD55 APP_CMD with argument as card's RCA */ |
||
3116 | sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16); |
||
3117 | sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD; |
||
3118 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT; |
||
3119 | sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO; |
||
3120 | sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE; |
||
3121 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
3122 | |||
3123 | /* Check for error conditions */ |
||
3124 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD); |
||
3125 | |||
3126 | if(errorstate != SD_OK) |
||
3127 | { |
||
3128 | return errorstate; |
||
3129 | } |
||
3130 | |||
3131 | /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */ |
||
3132 | sdio_cmdinitstructure.Argument = 0; |
||
3133 | sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH; |
||
3134 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
3135 | |||
3136 | /* Check for error conditions */ |
||
3137 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_SD_SET_BUSWIDTH); |
||
3138 | |||
3139 | if(errorstate != SD_OK) |
||
3140 | { |
||
3141 | return errorstate; |
||
3142 | } |
||
3143 | |||
3144 | return errorstate; |
||
3145 | } |
||
3146 | else |
||
3147 | { |
||
3148 | errorstate = SD_REQUEST_NOT_APPLICABLE; |
||
3149 | |||
3150 | return errorstate; |
||
3151 | } |
||
3152 | } |
||
3153 | |||
3154 | |||
3155 | /** |
||
3156 | * @brief Finds the SD card SCR register value. |
||
3157 | * @param hsd: SD handle |
||
3158 | * @param pSCR: pointer to the buffer that will contain the SCR value |
||
3159 | * @retval SD Card error state |
||
3160 | */ |
||
3161 | static HAL_SD_ErrorTypedef SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR) |
||
3162 | { |
||
3163 | SDIO_CmdInitTypeDef sdio_cmdinitstructure = {0}; |
||
3164 | SDIO_DataInitTypeDef sdio_datainitstructure = {0}; |
||
3165 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
3166 | uint32_t index = 0; |
||
3167 | uint32_t tempscr[2] = {0, 0}; |
||
3168 | |||
3169 | /* Set Block Size To 8 Bytes */ |
||
3170 | /* Send CMD55 APP_CMD with argument as card's RCA */ |
||
3171 | sdio_cmdinitstructure.Argument = (uint32_t)8; |
||
3172 | sdio_cmdinitstructure.CmdIndex = SD_CMD_SET_BLOCKLEN; |
||
3173 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT; |
||
3174 | sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO; |
||
3175 | sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE; |
||
3176 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
3177 | |||
3178 | /* Check for error conditions */ |
||
3179 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN); |
||
3180 | |||
3181 | if(errorstate != SD_OK) |
||
3182 | { |
||
3183 | return errorstate; |
||
3184 | } |
||
3185 | |||
3186 | /* Send CMD55 APP_CMD with argument as card's RCA */ |
||
3187 | sdio_cmdinitstructure.Argument = (uint32_t)((hsd->RCA) << 16); |
||
3188 | sdio_cmdinitstructure.CmdIndex = SD_CMD_APP_CMD; |
||
3189 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
3190 | |||
3191 | /* Check for error conditions */ |
||
3192 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD); |
||
3193 | |||
3194 | if(errorstate != SD_OK) |
||
3195 | { |
||
3196 | return errorstate; |
||
3197 | } |
||
3198 | sdio_datainitstructure.DataTimeOut = SD_DATATIMEOUT; |
||
3199 | sdio_datainitstructure.DataLength = 8; |
||
3200 | sdio_datainitstructure.DataBlockSize = SDIO_DATABLOCK_SIZE_8B; |
||
3201 | sdio_datainitstructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; |
||
3202 | sdio_datainitstructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK; |
||
3203 | sdio_datainitstructure.DPSM = SDIO_DPSM_ENABLE; |
||
3204 | SDIO_DataConfig(hsd->Instance, &sdio_datainitstructure); |
||
3205 | |||
3206 | /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */ |
||
3207 | sdio_cmdinitstructure.Argument = 0; |
||
3208 | sdio_cmdinitstructure.CmdIndex = SD_CMD_SD_APP_SEND_SCR; |
||
3209 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
3210 | |||
3211 | /* Check for error conditions */ |
||
3212 | errorstate = SD_CmdResp1Error(hsd, SD_CMD_SD_APP_SEND_SCR); |
||
3213 | |||
3214 | if(errorstate != SD_OK) |
||
3215 | { |
||
3216 | return errorstate; |
||
3217 | } |
||
3218 | |||
3219 | while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)) |
||
3220 | { |
||
3221 | if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) |
||
3222 | { |
||
3223 | *(tempscr + index) = SDIO_ReadFIFO(hsd->Instance); |
||
3224 | index++; |
||
3225 | } |
||
3226 | } |
||
3227 | |||
3228 | if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT)) |
||
3229 | { |
||
3230 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT); |
||
3231 | |||
3232 | errorstate = SD_DATA_TIMEOUT; |
||
3233 | |||
3234 | return errorstate; |
||
3235 | } |
||
3236 | else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL)) |
||
3237 | { |
||
3238 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL); |
||
3239 | |||
3240 | errorstate = SD_DATA_CRC_FAIL; |
||
3241 | |||
3242 | return errorstate; |
||
3243 | } |
||
3244 | else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR)) |
||
3245 | { |
||
3246 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR); |
||
3247 | |||
3248 | errorstate = SD_RX_OVERRUN; |
||
3249 | |||
3250 | return errorstate; |
||
3251 | } |
||
3252 | else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR)) |
||
3253 | { |
||
3254 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR); |
||
3255 | |||
3256 | errorstate = SD_START_BIT_ERR; |
||
3257 | |||
3258 | return errorstate; |
||
3259 | } |
||
3260 | else |
||
3261 | { |
||
3262 | /* No error flag set */ |
||
3263 | } |
||
3264 | |||
3265 | /* Clear all the static flags */ |
||
3266 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
||
3267 | |||
3268 | *(pSCR + 1) = ((tempscr[0] & SD_0TO7BITS) << 24) | ((tempscr[0] & SD_8TO15BITS) << 8) |\ |
||
3269 | ((tempscr[0] & SD_16TO23BITS) >> 8) | ((tempscr[0] & SD_24TO31BITS) >> 24); |
||
3270 | |||
3271 | *(pSCR) = ((tempscr[1] & SD_0TO7BITS) << 24) | ((tempscr[1] & SD_8TO15BITS) << 8) |\ |
||
3272 | ((tempscr[1] & SD_16TO23BITS) >> 8) | ((tempscr[1] & SD_24TO31BITS) >> 24); |
||
3273 | |||
3274 | return errorstate; |
||
3275 | } |
||
3276 | |||
3277 | /** |
||
3278 | * @brief Checks if the SD card is in programming state. |
||
3279 | * @param hsd: SD handle |
||
3280 | * @param pStatus: pointer to the variable that will contain the SD card state |
||
3281 | * @retval SD Card error state |
||
3282 | */ |
||
3283 | static HAL_SD_ErrorTypedef SD_IsCardProgramming(SD_HandleTypeDef *hsd, uint8_t *pStatus) |
||
3284 | { |
||
3285 | SDIO_CmdInitTypeDef sdio_cmdinitstructure = {0}; |
||
3286 | HAL_SD_ErrorTypedef errorstate = SD_OK; |
||
3287 | __IO uint32_t responseR1 = 0; |
||
3288 | |||
3289 | sdio_cmdinitstructure.Argument = (uint32_t)(hsd->RCA << 16); |
||
3290 | sdio_cmdinitstructure.CmdIndex = SD_CMD_SEND_STATUS; |
||
3291 | sdio_cmdinitstructure.Response = SDIO_RESPONSE_SHORT; |
||
3292 | sdio_cmdinitstructure.WaitForInterrupt = SDIO_WAIT_NO; |
||
3293 | sdio_cmdinitstructure.CPSM = SDIO_CPSM_ENABLE; |
||
3294 | SDIO_SendCommand(hsd->Instance, &sdio_cmdinitstructure); |
||
3295 | |||
3296 | while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)) |
||
3297 | { |
||
3298 | } |
||
3299 | |||
3300 | if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT)) |
||
3301 | { |
||
3302 | errorstate = SD_CMD_RSP_TIMEOUT; |
||
3303 | |||
3304 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT); |
||
3305 | |||
3306 | return errorstate; |
||
3307 | } |
||
3308 | else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL)) |
||
3309 | { |
||
3310 | errorstate = SD_CMD_CRC_FAIL; |
||
3311 | |||
3312 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL); |
||
3313 | |||
3314 | return errorstate; |
||
3315 | } |
||
3316 | else |
||
3317 | { |
||
3318 | /* No error flag set */ |
||
3319 | } |
||
3320 | |||
3321 | /* Check response received is of desired command */ |
||
3322 | if((uint32_t)SDIO_GetCommandResponse(hsd->Instance) != SD_CMD_SEND_STATUS) |
||
3323 | { |
||
3324 | errorstate = SD_ILLEGAL_CMD; |
||
3325 | |||
3326 | return errorstate; |
||
3327 | } |
||
3328 | |||
3329 | /* Clear all the static flags */ |
||
3330 | __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
||
3331 | |||
3332 | |||
3333 | /* We have received response, retrieve it for analysis */ |
||
3334 | responseR1 = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); |
||
3335 | |||
3336 | /* Find out card status */ |
||
3337 | *pStatus = (uint8_t)((responseR1 >> 9) & 0x0000000F); |
||
3338 | |||
3339 | if((responseR1 & SD_OCR_ERRORBITS) == SD_ALLZERO) |
||
3340 | { |
||
3341 | return errorstate; |
||
3342 | } |
||
3343 | |||
3344 | if((responseR1 & SD_OCR_ADDR_OUT_OF_RANGE) == SD_OCR_ADDR_OUT_OF_RANGE) |
||
3345 | { |
||
3346 | return(SD_ADDR_OUT_OF_RANGE); |
||
3347 | } |
||
3348 | |||
3349 | if((responseR1 & SD_OCR_ADDR_MISALIGNED) == SD_OCR_ADDR_MISALIGNED) |
||
3350 | { |
||
3351 | return(SD_ADDR_MISALIGNED); |
||
3352 | } |
||
3353 | |||
3354 | if((responseR1 & SD_OCR_BLOCK_LEN_ERR) == SD_OCR_BLOCK_LEN_ERR) |
||
3355 | { |
||
3356 | return(SD_BLOCK_LEN_ERR); |
||
3357 | } |
||
3358 | |||
3359 | if((responseR1 & SD_OCR_ERASE_SEQ_ERR) == SD_OCR_ERASE_SEQ_ERR) |
||
3360 | { |
||
3361 | return(SD_ERASE_SEQ_ERR); |
||
3362 | } |
||
3363 | |||
3364 | if((responseR1 & SD_OCR_BAD_ERASE_PARAM) == SD_OCR_BAD_ERASE_PARAM) |
||
3365 | { |
||
3366 | return(SD_BAD_ERASE_PARAM); |
||
3367 | } |
||
3368 | |||
3369 | if((responseR1 & SD_OCR_WRITE_PROT_VIOLATION) == SD_OCR_WRITE_PROT_VIOLATION) |
||
3370 | { |
||
3371 | return(SD_WRITE_PROT_VIOLATION); |
||
3372 | } |
||
3373 | |||
3374 | if((responseR1 & SD_OCR_LOCK_UNLOCK_FAILED) == SD_OCR_LOCK_UNLOCK_FAILED) |
||
3375 | { |
||
3376 | return(SD_LOCK_UNLOCK_FAILED); |
||
3377 | } |
||
3378 | |||
3379 | if((responseR1 & SD_OCR_COM_CRC_FAILED) == SD_OCR_COM_CRC_FAILED) |
||
3380 | { |
||
3381 | return(SD_COM_CRC_FAILED); |
||
3382 | } |
||
3383 | |||
3384 | if((responseR1 & SD_OCR_ILLEGAL_CMD) == SD_OCR_ILLEGAL_CMD) |
||
3385 | { |
||
3386 | return(SD_ILLEGAL_CMD); |
||
3387 | } |
||
3388 | |||
3389 | if((responseR1 & SD_OCR_CARD_ECC_FAILED) == SD_OCR_CARD_ECC_FAILED) |
||
3390 | { |
||
3391 | return(SD_CARD_ECC_FAILED); |
||
3392 | } |
||
3393 | |||
3394 | if((responseR1 & SD_OCR_CC_ERROR) == SD_OCR_CC_ERROR) |
||
3395 | { |
||
3396 | return(SD_CC_ERROR); |
||
3397 | } |
||
3398 | |||
3399 | if((responseR1 & SD_OCR_GENERAL_UNKNOWN_ERROR) == SD_OCR_GENERAL_UNKNOWN_ERROR) |
||
3400 | { |
||
3401 | return(SD_GENERAL_UNKNOWN_ERROR); |
||
3402 | } |
||
3403 | |||
3404 | if((responseR1 & SD_OCR_STREAM_READ_UNDERRUN) == SD_OCR_STREAM_READ_UNDERRUN) |
||
3405 | { |
||
3406 | return(SD_STREAM_READ_UNDERRUN); |
||
3407 | } |
||
3408 | |||
3409 | if((responseR1 & SD_OCR_STREAM_WRITE_OVERRUN) == SD_OCR_STREAM_WRITE_OVERRUN) |
||
3410 | { |
||
3411 | return(SD_STREAM_WRITE_OVERRUN); |
||
3412 | } |
||
3413 | |||
3414 | if((responseR1 & SD_OCR_CID_CSD_OVERWRITE) == SD_OCR_CID_CSD_OVERWRITE) |
||
3415 | { |
||
3416 | return(SD_CID_CSD_OVERWRITE); |
||
3417 | } |
||
3418 | |||
3419 | if((responseR1 & SD_OCR_WP_ERASE_SKIP) == SD_OCR_WP_ERASE_SKIP) |
||
3420 | { |
||
3421 | return(SD_WP_ERASE_SKIP); |
||
3422 | } |
||
3423 | |||
3424 | if((responseR1 & SD_OCR_CARD_ECC_DISABLED) == SD_OCR_CARD_ECC_DISABLED) |
||
3425 | { |
||
3426 | return(SD_CARD_ECC_DISABLED); |
||
3427 | } |
||
3428 | |||
3429 | if((responseR1 & SD_OCR_ERASE_RESET) == SD_OCR_ERASE_RESET) |
||
3430 | { |
||
3431 | return(SD_ERASE_RESET); |
||
3432 | } |
||
3433 | |||
3434 | if((responseR1 & SD_OCR_AKE_SEQ_ERROR) == SD_OCR_AKE_SEQ_ERROR) |
||
3435 | { |
||
3436 | return(SD_AKE_SEQ_ERROR); |
||
3437 | } |
||
3438 | |||
3439 | return errorstate; |
||
3440 | } |
||
3441 | |||
3442 | /** |
||
3443 | * @} |
||
3444 | */ |
||
3445 | |||
3446 | #endif /* STM32F103xE || STM32F103xG */ |
||
3447 | |||
3448 | #endif /* HAL_SD_MODULE_ENABLED */ |
||
3449 | |||
3450 | /** |
||
3451 | * @} |
||
3452 | */ |
||
3453 | |||
3454 | /** |
||
3455 | * @} |
||
3456 | */ |
||
3457 | |||
3458 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |