Subversion Repositories LedShow

Rev

Go to most recent revision | Details | 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
  * @brief   SD card HAL module driver.
6
  *          This file provides firmware functions to manage the following
7
  *          functionalities of the Secure Digital (SD) peripheral:
8
  *           + Initialization and de-initialization functions
9
  *           + IO operation functions
10
  *           + Peripheral Control functions
11
  *           + SD card Control functions
12
  *        
13
  @verbatim
14
  ==============================================================================
15
                        ##### How to use this driver #####
16
  ==============================================================================
17
  [..]
18
    This driver implements a high level communication layer for read and write from/to
19
    this memory. The needed STM32 hardware resources (SDIO and GPIO) are performed by
20
    the user in HAL_SD_MspInit() function (MSP layer).                            
21
    Basically, the MSP layer configuration should be the same as we provide in the
22
    examples.
23
    You can easily tailor this configuration according to hardware resources.
24
 
25
  [..]
26
    This driver is a generic layered driver for SDIO memories which uses the HAL
27
    SDIO driver functions to interface with SD and uSD cards devices.
28
    It is used as follows:
29
 
30
    (#)Initialize the SDIO low level resources by implement the HAL_SD_MspInit() API:
31
        (##) Enable the SDIO interface clock using __HAL_RCC_SDIO_CLK_ENABLE();
32
        (##) SDIO pins configuration for SD card
33
            (+++) Enable the clock for the SDIO GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();  
34
            (+++) Configure these SDIO pins as alternate function pull-up using HAL_GPIO_Init()
35
                  and according to your pin assignment;
36
        (##) DMA Configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA()
37
             and HAL_SD_WriteBlocks_DMA() APIs).
38
            (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE();
39
            (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled.
40
        (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
41
            (+++) Configure the SDIO and DMA interrupt priorities using functions
42
                  HAL_NVIC_SetPriority(); DMA priority is superior to SDIO's priority
43
            (+++) Enable the NVIC DMA and SDIO IRQs using function HAL_NVIC_EnableIRQ()
44
            (+++) SDIO interrupts are managed using the macros __HAL_SD_ENABLE_IT()
45
                  and __HAL_SD_DISABLE_IT() inside the communication process.
46
            (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
47
                  and __HAL_SD_CLEAR_IT()
48
        (##) NVIC configuration if you need to use interrupt process (HAL_SD_ReadBlocks_IT()
49
             and HAL_SD_WriteBlocks_IT() APIs).
50
            (+++) Configure the SDIO interrupt priorities using function
51
                  HAL_NVIC_SetPriority();
52
            (+++) Enable the NVIC SDIO IRQs using function HAL_NVIC_EnableIRQ()
53
            (+++) SDIO interrupts are managed using the macros __HAL_SD_ENABLE_IT()
54
                  and __HAL_SD_DISABLE_IT() inside the communication process.
55
            (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
56
                  and __HAL_SD_CLEAR_IT()
57
    (#) At this stage, you can perform SD read/write/erase operations after SD card initialization  
58
 
59
 
60
  *** SD Card Initialization and configuration ***
61
  ================================================    
62
  [..]
63
    To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
64
    SDIO IP(STM32 side) and the SD Card, and put it into StandBy State (Ready for data transfer).
65
    This function provide the following operations:
66
 
67
    (#) Initialize the SDIO peripheral interface with defaullt configuration.
68
        The initialization process is done at 400KHz. You can change or adapt
69
        this frequency by adjusting the "ClockDiv" field.
70
        The SD Card frequency (SDIO_CK) is computed as follows:
71
 
72
           SDIO_CK = SDIOCLK / (ClockDiv + 2)
73
 
74
        In initialization mode and according to the SD Card standard,
75
        make sure that the SDIO_CK frequency doesn't exceed 400KHz.
76
 
77
        This phase of initialization is done through SDIO_Init() and
78
        SDIO_PowerState_ON() SDIO low level APIs.
79
 
80
    (#) Initialize the SD card. The API used is HAL_SD_InitCard().
81
        This phase allows the card initialization and identification
82
        and check the SD Card type (Standard Capacity or High Capacity)
83
        The initialization flow is compatible with SD standard.
84
 
85
        This API (HAL_SD_InitCard()) could be used also to reinitialize the card in case
86
        of plug-off plug-in.
87
 
88
    (#) Configure the SD Card Data transfer frequency. By Default, the card transfer
89
        frequency is set to 24MHz. You can change or adapt this frequency by adjusting
90
        the "ClockDiv" field.
91
        In transfer mode and according to the SD Card standard, make sure that the
92
        SDIO_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch.
93
        To be able to use a frequency higher than 24MHz, you should use the SDIO
94
        peripheral in bypass mode. Refer to the corresponding reference manual
95
        for more details.
96
 
97
    (#) Select the corresponding SD Card according to the address read with the step 2.
98
 
99
    (#) Configure the SD Card in wide bus mode: 4-bits data.
100
 
101
  *** SD Card Read operation ***
102
  ==============================
103
  [..]
104
    (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks().
105
        This function allows the read of 512 bytes blocks.
106
        You can choose either one block read operation or multiple block read operation
107
        by adjusting the "NumberOfBlocks" parameter.
108
        After this, you have to ensure that the transfer is done correctly. The check is done
109
        through HAL_SD_GetCardState() function for SD card state.
110
 
111
    (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
112
        This function allows the read of 512 bytes blocks.
113
        You can choose either one block read operation or multiple block read operation
114
        by adjusting the "NumberOfBlocks" parameter.
115
        After this, you have to ensure that the transfer is done correctly. The check is done
116
        through HAL_SD_GetCardState() function for SD card state.
117
        You could also check the DMA transfer process through the SD Rx interrupt event.
118
 
119
    (+) You can read from SD card in Interrupt mode by using function HAL_SD_ReadBlocks_IT().
120
        This function allows the read of 512 bytes blocks.
121
        You can choose either one block read operation or multiple block read operation
122
        by adjusting the "NumberOfBlocks" parameter.
123
        After this, you have to ensure that the transfer is done correctly. The check is done
124
        through HAL_SD_GetCardState() function for SD card state.
125
        You could also check the IT transfer process through the SD Rx interrupt event.
126
 
127
  *** SD Card Write operation ***
128
  ===============================
129
  [..]
130
    (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks().
131
        This function allows the read of 512 bytes blocks.
132
        You can choose either one block read operation or multiple block read operation
133
        by adjusting the "NumberOfBlocks" parameter.
134
        After this, you have to ensure that the transfer is done correctly. The check is done
135
        through HAL_SD_GetCardState() function for SD card state.
136
 
137
    (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
138
        This function allows the read of 512 bytes blocks.
139
        You can choose either one block read operation or multiple block read operation
140
        by adjusting the "NumberOfBlocks" parameter.
141
        After this, you have to ensure that the transfer is done correctly. The check is done
142
        through HAL_SD_GetCardState() function for SD card state.
143
        You could also check the DMA transfer process through the SD Tx interrupt event.  
144
 
145
    (+) You can write to SD card in Interrupt mode by using function HAL_SD_WriteBlocks_IT().
146
        This function allows the read of 512 bytes blocks.
147
        You can choose either one block read operation or multiple block read operation
148
        by adjusting the "NumberOfBlocks" parameter.
149
        After this, you have to ensure that the transfer is done correctly. The check is done
150
        through HAL_SD_GetCardState() function for SD card state.
151
        You could also check the IT transfer process through the SD Tx interrupt event.
152
 
153
  *** SD card status ***
154
  ======================
155
  [..]
156
    (+) The SD Status contains status bits that are related to the SD Memory
157
        Card proprietary features. To get SD card status use the HAL_SD_GetCardStatus().
158
 
159
  *** SD card information ***
160
  ===========================
161
  [..]
162
    (+) To get SD card information, you can use the function HAL_SD_GetCardInfo().
163
        It returns useful information about the SD card such as block size, card type,
164
        block number ...
165
 
166
  *** SD card CSD register ***
167
  ============================
168
  [..]
169
    (+) The HAL_SD_GetCardCSD() API allows to get the parameters of the CSD register.
170
        Some of the CSD parameters are useful for card initialization and identification.
171
 
172
  *** SD card CID register ***
173
  ============================
174
  [..]
175
    (+) The HAL_SD_GetCardCID() API allows to get the parameters of the CID register.
176
        Some of the CSD parameters are useful for card initialization and identification.
177
 
178
  *** SD HAL driver macros list ***
179
  ==================================
180
  [..]
181
    Below the list of most used macros in SD HAL driver.
182
 
183
    (+) __HAL_SD_ENABLE : Enable the SD device
184
    (+) __HAL_SD_DISABLE : Disable the SD device
185
    (+) __HAL_SD_DMA_ENABLE: Enable the SDIO DMA transfer
186
    (+) __HAL_SD_DMA_DISABLE: Disable the SDIO DMA transfer
187
    (+) __HAL_SD_ENABLE_IT: Enable the SD device interrupt
188
    (+) __HAL_SD_DISABLE_IT: Disable the SD device interrupt
189
    (+) __HAL_SD_GET_FLAG:Check whether the specified SD flag is set or not
190
    (+) __HAL_SD_CLEAR_FLAG: Clear the SD's pending flags
191
 
192
   [..]
193
    (@) You can refer to the SD HAL driver header file for more useful macros
194
 
195
  @endverbatim
196
  ******************************************************************************
197
  * @attention
198
  *
199
  * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
200
  *
201
  * Redistribution and use in source and binary forms, with or without modification,
202
  * are permitted provided that the following conditions are met:
203
  *   1. Redistributions of source code must retain the above copyright notice,
204
  *      this list of conditions and the following disclaimer.
205
  *   2. Redistributions in binary form must reproduce the above copyright notice,
206
  *      this list of conditions and the following disclaimer in the documentation
207
  *      and/or other materials provided with the distribution.
208
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
209
  *      may be used to endorse or promote products derived from this software
210
  *      without specific prior written permission.
211
  *
212
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
213
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
214
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
215
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
216
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
217
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
218
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
219
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
220
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
221
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
222
  *
223
  ******************************************************************************
224
  */
225
 
226
/* Includes ------------------------------------------------------------------*/
227
#include "stm32f1xx_hal.h"
228
 
229
#if defined(STM32F103xE) || defined(STM32F103xG)
230
 
231
/** @addtogroup STM32F1xx_HAL_Driver
232
  * @{
233
  */
234
 
235
/** @addtogroup SD
236
  * @{
237
  */
238
#ifdef HAL_SD_MODULE_ENABLED
239
 
240
/* Private typedef -----------------------------------------------------------*/
241
/* Private define ------------------------------------------------------------*/
242
/** @addtogroup SD_Private_Defines
243
  * @{
244
  */
245
 
246
/**
247
  * @}
248
  */
249
 
250
/* Private macro -------------------------------------------------------------*/
251
/* Private variables ---------------------------------------------------------*/
252
/* Private function prototypes -----------------------------------------------*/
253
/* Private functions ---------------------------------------------------------*/
254
/** @defgroup SD_Private_Functions SD Private Functions
255
  * @{
256
  */
257
static uint32_t SD_InitCard(SD_HandleTypeDef *hsd);
258
static uint32_t SD_PowerON(SD_HandleTypeDef *hsd);                      
259
static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus);
260
static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
261
static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd);
262
static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd);
263
static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR);
264
static HAL_StatusTypeDef SD_PowerOFF(SD_HandleTypeDef *hsd);
265
static HAL_StatusTypeDef SD_Write_IT(SD_HandleTypeDef *hsd);
266
static HAL_StatusTypeDef SD_Read_IT(SD_HandleTypeDef *hsd);
267
static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
268
static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
269
static void SD_DMAError(DMA_HandleTypeDef *hdma);
270
static void SD_DMATxAbort(DMA_HandleTypeDef *hdma);
271
static void SD_DMARxAbort(DMA_HandleTypeDef *hdma);
272
/**
273
  * @}
274
  */
275
 
276
/* Exported functions --------------------------------------------------------*/
277
/** @addtogroup SD_Exported_Functions
278
  * @{
279
  */
280
 
281
/** @addtogroup SD_Exported_Functions_Group1
282
 *  @brief   Initialization and de-initialization functions
283
 *
284
@verbatim    
285
  ==============================================================================
286
          ##### Initialization and de-initialization functions #####
287
  ==============================================================================
288
  [..]  
289
    This section provides functions allowing to initialize/de-initialize the SD
290
    card device to be ready for use.
291
 
292
@endverbatim
293
  * @{
294
  */
295
 
296
/**
297
  * @brief  Initializes the SD according to the specified parameters in the
298
            SD_HandleTypeDef and create the associated handle.
299
  * @param  hsd: Pointer to the SD handle  
300
  * @retval HAL status
301
  */
302
HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd)
303
{
304
  /* Check the SD handle allocation */
305
  if(hsd == NULL)
306
  {
307
    return HAL_ERROR;
308
  }
309
 
310
  /* Check the parameters */
311
  assert_param(IS_SDIO_ALL_INSTANCE(hsd->Instance));
312
  assert_param(IS_SDIO_CLOCK_EDGE(hsd->Init.ClockEdge));
313
  assert_param(IS_SDIO_CLOCK_BYPASS(hsd->Init.ClockBypass));
314
  assert_param(IS_SDIO_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave));
315
  assert_param(IS_SDIO_BUS_WIDE(hsd->Init.BusWide));
316
  assert_param(IS_SDIO_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl));
317
  assert_param(IS_SDIO_CLKDIV(hsd->Init.ClockDiv));
318
 
319
  if(hsd->State == HAL_SD_STATE_RESET)
320
  {
321
    /* Allocate lock resource and initialize it */
322
    hsd->Lock = HAL_UNLOCKED;
323
    /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
324
    HAL_SD_MspInit(hsd);
325
  }
326
 
327
  hsd->State = HAL_SD_STATE_BUSY;
328
 
329
  /* Initialize the Card parameters */
330
  HAL_SD_InitCard(hsd);
331
 
332
  /* Initialize the error code */
333
  hsd->ErrorCode = HAL_DMA_ERROR_NONE;
334
 
335
  /* Initialize the SD operation */
336
  hsd->Context = SD_CONTEXT_NONE;
337
 
338
  /* Initialize the SD state */
339
  hsd->State = HAL_SD_STATE_READY;
340
 
341
  return HAL_OK;
342
}
343
 
344
/**
345
  * @brief  Initializes the SD Card.
346
  * @param  hsd: Pointer to SD handle
347
  * @note   This function initializes the SD card. It could be used when a card
348
            re-initialization is needed.
349
  * @retval HAL status
350
  */
351
HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
352
{
353
  uint32_t errorstate = HAL_SD_ERROR_NONE;
354
  SD_InitTypeDef Init;
355
 
356
  /* Default SDIO peripheral configuration for SD card initialization */
357
  Init.ClockEdge           = SDIO_CLOCK_EDGE_RISING;
358
  Init.ClockBypass         = SDIO_CLOCK_BYPASS_DISABLE;
359
  Init.ClockPowerSave      = SDIO_CLOCK_POWER_SAVE_DISABLE;
360
  Init.BusWide             = SDIO_BUS_WIDE_1B;
361
  Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
362
  Init.ClockDiv            = SDIO_INIT_CLK_DIV;
363
 
364
  /* Initialize SDIO peripheral interface with default configuration */
365
  SDIO_Init(hsd->Instance, Init);
366
 
367
  /* Disable SDIO Clock */
368
  __HAL_SD_DISABLE(hsd);
369
 
370
  /* Set Power State to ON */
371
  SDIO_PowerState_ON(hsd->Instance);
372
 
373
  /* Enable SDIO Clock */
374
  __HAL_SD_ENABLE(hsd);
375
 
376
  /* Required power up waiting time before starting the SD initialization
377
  sequence */
378
  HAL_Delay(2U);
379
 
380
  /* Identify card operating voltage */
381
  errorstate = SD_PowerON(hsd);
382
  if(errorstate != HAL_SD_ERROR_NONE)
383
  {
384
    hsd->State = HAL_SD_STATE_READY;
385
    hsd->ErrorCode |= errorstate;
386
    return HAL_ERROR;
387
  }
388
 
389
  /* Card initialization */
390
  errorstate = SD_InitCard(hsd);
391
  if(errorstate != HAL_SD_ERROR_NONE)
392
  {
393
    hsd->State = HAL_SD_STATE_READY;
394
    hsd->ErrorCode |= errorstate;
395
    return HAL_ERROR;
396
  }
397
 
398
  return HAL_OK;
399
}
400
 
401
/**
402
  * @brief  De-Initializes the SD card.
403
  * @param  hsd: Pointer to SD handle
404
  * @retval HAL status
405
  */
406
HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
407
{
408
  /* Check the SD handle allocation */
409
  if(hsd == NULL)
410
  {
411
    return HAL_ERROR;
412
  }
413
 
414
  /* Check the parameters */
415
  assert_param(IS_SDIO_ALL_INSTANCE(hsd->Instance));
416
 
417
  hsd->State = HAL_SD_STATE_BUSY;
418
 
419
  /* Set SD power state to off */
420
  SD_PowerOFF(hsd);
421
 
422
  /* De-Initialize the MSP layer */
423
  HAL_SD_MspDeInit(hsd);
424
 
425
  hsd->ErrorCode = HAL_SD_ERROR_NONE;
426
  hsd->State = HAL_SD_STATE_RESET;
427
 
428
  return HAL_OK;
429
}
430
 
431
 
432
/**
433
  * @brief  Initializes the SD MSP.
434
  * @param  hsd: Pointer to SD handle
435
  * @retval None
436
  */
437
__weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
438
{
439
  /* Prevent unused argument(s) compilation warning */
440
  UNUSED(hsd);
441
 
442
  /* NOTE : This function Should not be modified, when the callback is needed,
443
            the HAL_SD_MspInit could be implemented in the user file
444
   */
445
}
446
 
447
/**
448
  * @brief  De-Initialize SD MSP.
449
  * @param  hsd: Pointer to SD handle
450
  * @retval None
451
  */
452
__weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
453
{
454
  /* Prevent unused argument(s) compilation warning */
455
  UNUSED(hsd);
456
 
457
  /* NOTE : This function Should not be modified, when the callback is needed,
458
            the HAL_SD_MspDeInit could be implemented in the user file
459
   */
460
}
461
 
462
/**
463
  * @}
464
  */
465
 
466
/** @addtogroup SD_Exported_Functions_Group2
467
 *  @brief   Data transfer functions
468
 *
469
@verbatim  
470
  ==============================================================================
471
                        ##### IO operation functions #####
472
  ==============================================================================  
473
  [..]
474
    This subsection provides a set of functions allowing to manage the data
475
    transfer from/to SD card.
476
 
477
@endverbatim
478
  * @{
479
  */
480
 
481
/**
482
  * @brief  Reads block(s) from a specified address in a card. The Data transfer
483
  *         is managed by polling mode.
484
  * @note   This API should be followed by a check on the card state through
485
  *         HAL_SD_GetCardState().
486
  * @param  hsd: Pointer to SD handle
487
  * @param  pData: pointer to the buffer that will contain the received data
488
  * @param  BlockAdd: Block Address from where data is to be read
489
  * @param  NumberOfBlocks: Number of SD blocks to read
490
  * @param  Timeout: Specify timeout value
491
  * @retval HAL status
492
  */
493
HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
494
{
495
  SDIO_DataInitTypeDef config;
496
  uint32_t errorstate = HAL_SD_ERROR_NONE;
497
  uint32_t tickstart = HAL_GetTick();
498
  uint32_t count = 0U, *tempbuff = (uint32_t *)pData;
499
 
500
  if(NULL == pData)
501
  {
502
    hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
503
    return HAL_ERROR;
504
  }
505
 
506
  if(hsd->State == HAL_SD_STATE_READY)
507
  {
508
    hsd->ErrorCode = HAL_DMA_ERROR_NONE;
509
 
510
    if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
511
    {
512
      hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
513
      return HAL_ERROR;
514
    }
515
 
516
    hsd->State = HAL_SD_STATE_BUSY;
517
 
518
    /* Initialize data control register */
519
    hsd->Instance->DCTRL = 0U;
520
 
521
    if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
522
    {
523
      BlockAdd *= 512U;
524
    }
525
 
526
    /* Set Block Size for Card */
527
    errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
528
    if(errorstate != HAL_SD_ERROR_NONE)
529
    {
530
      /* Clear all the static flags */
531
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);      
532
      hsd->ErrorCode |= errorstate;
533
      hsd->State = HAL_SD_STATE_READY;
534
      return HAL_ERROR;
535
    }
536
 
537
    /* Configure the SD DPSM (Data Path State Machine) */
538
    config.DataTimeOut   = SDMMC_DATATIMEOUT;
539
    config.DataLength    = NumberOfBlocks * BLOCKSIZE;
540
    config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
541
    config.TransferDir   = SDIO_TRANSFER_DIR_TO_SDIO;
542
    config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
543
    config.DPSM          = SDIO_DPSM_ENABLE;
544
    SDIO_ConfigData(hsd->Instance, &config);
545
 
546
    /* Read block(s) in polling mode */
547
    if(NumberOfBlocks > 1U)
548
    {
549
      hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK;
550
 
551
      /* Read Multi Block command */
552
      errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd);
553
    }
554
    else
555
    {
556
      hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK;
557
 
558
      /* Read Single Block command */
559
      errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd);
560
    }
561
    if(errorstate != HAL_SD_ERROR_NONE)
562
    {
563
      /* Clear all the static flags */
564
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
565
      hsd->ErrorCode |= errorstate;
566
      hsd->State = HAL_SD_STATE_READY;
567
      return HAL_ERROR;
568
    }
569
 
570
    /* Poll on SDIO flags */
571
    while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_STA_STBITERR))
572
    {
573
      if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
574
      {
575
        /* Read data from SDIO Rx FIFO */
576
        for(count = 0U; count < 8U; count++)
577
        {
578
          *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance);
579
        }
580
        tempbuff += 8U;
581
      }
582
 
583
      if((Timeout == 0U)||((HAL_GetTick()-tickstart) >=  Timeout))
584
      {
585
        /* Clear all the static flags */
586
        __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
587
        hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
588
        hsd->State= HAL_SD_STATE_READY;
589
        return HAL_TIMEOUT;
590
      }
591
    }
592
 
593
    /* Send stop transmission command in case of multiblock read */
594
    if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
595
    {    
596
      if(hsd->SdCard.CardType != CARD_SECURED)
597
      {
598
        /* Send stop transmission command */
599
        errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
600
        if(errorstate != HAL_SD_ERROR_NONE)
601
        {
602
          /* Clear all the static flags */
603
          __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
604
          hsd->ErrorCode |= errorstate;
605
          hsd->State = HAL_SD_STATE_READY;
606
          return HAL_ERROR;
607
        }
608
      }
609
    }
610
 
611
    /* Get error state */
612
    if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
613
    {
614
      /* Clear all the static flags */
615
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
616
      hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
617
      hsd->State = HAL_SD_STATE_READY;
618
      return HAL_ERROR;
619
    }
620
    else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
621
    {
622
      /* Clear all the static flags */
623
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
624
      hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
625
      hsd->State = HAL_SD_STATE_READY;
626
      return HAL_ERROR;
627
    }
628
    else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
629
    {
630
      /* Clear all the static flags */
631
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
632
      hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
633
      hsd->State = HAL_SD_STATE_READY;
634
      return HAL_ERROR;
635
    }
636
 
637
    /* Empty FIFO if there is still any data */
638
    while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)))
639
    {
640
      *tempbuff = SDIO_ReadFIFO(hsd->Instance);
641
      tempbuff++;
642
 
643
      if((Timeout == 0U)||((HAL_GetTick()-tickstart) >=  Timeout))
644
      {
645
        /* Clear all the static flags */
646
        __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);        
647
        hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
648
        hsd->State= HAL_SD_STATE_READY;
649
        return HAL_ERROR;
650
      }
651
    }
652
 
653
    /* Clear all the static flags */
654
    __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
655
 
656
    hsd->State = HAL_SD_STATE_READY;
657
 
658
    return HAL_OK;
659
  }
660
  else
661
  {
662
    hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
663
    return HAL_ERROR;
664
  }
665
}
666
 
667
/**
668
  * @brief  Allows to write block(s) to a specified address in a card. The Data
669
  *         transfer is managed by polling mode.
670
  * @note   This API should be followed by a check on the card state through
671
  *         HAL_SD_GetCardState().
672
  * @param  hsd: Pointer to SD handle
673
  * @param  pData: pointer to the buffer that will contain the data to transmit
674
  * @param  BlockAdd: Block Address where data will be written  
675
  * @param  NumberOfBlocks: Number of SD blocks to write
676
  * @param  Timeout: Specify timeout value
677
  * @retval HAL status
678
  */
679
HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
680
{
681
  SDIO_DataInitTypeDef config;
682
  uint32_t errorstate = HAL_SD_ERROR_NONE;
683
  uint32_t tickstart = HAL_GetTick();
684
  uint32_t count = 0U;
685
  uint32_t *tempbuff = (uint32_t *)pData;
686
 
687
  if(NULL == pData)
688
  {
689
    hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
690
    return HAL_ERROR;
691
  }
692
 
693
  if(hsd->State == HAL_SD_STATE_READY)
694
  {
695
    hsd->ErrorCode = HAL_DMA_ERROR_NONE;
696
 
697
    if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
698
    {
699
      hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
700
      return HAL_ERROR;
701
    }
702
 
703
    hsd->State = HAL_SD_STATE_BUSY;
704
 
705
    /* Initialize data control register */
706
    hsd->Instance->DCTRL = 0U;
707
 
708
    if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
709
    {
710
      BlockAdd *= 512U;
711
    }
712
 
713
    /* Set Block Size for Card */
714
    errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
715
    if(errorstate != HAL_SD_ERROR_NONE)
716
    {
717
      /* Clear all the static flags */
718
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);  
719
      hsd->ErrorCode |= errorstate;
720
      hsd->State = HAL_SD_STATE_READY;
721
      return HAL_ERROR;
722
    }
723
 
724
    /* Write Blocks in Polling mode */
725
    if(NumberOfBlocks > 1U)
726
    {
727
      hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
728
 
729
      /* Write Multi Block command */
730
      errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd);
731
    }
732
    else
733
    {
734
      hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
735
 
736
      /* Write Single Block command */
737
      errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd);
738
    }
739
    if(errorstate != HAL_SD_ERROR_NONE)
740
    {
741
      /* Clear all the static flags */
742
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);  
743
      hsd->ErrorCode |= errorstate;
744
      hsd->State = HAL_SD_STATE_READY;
745
      return HAL_ERROR;
746
    }
747
 
748
    /* Configure the SD DPSM (Data Path State Machine) */
749
    config.DataTimeOut   = SDMMC_DATATIMEOUT;
750
    config.DataLength    = NumberOfBlocks * BLOCKSIZE;
751
    config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
752
    config.TransferDir   = SDIO_TRANSFER_DIR_TO_CARD;
753
    config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
754
    config.DPSM          = SDIO_DPSM_ENABLE;
755
    SDIO_ConfigData(hsd->Instance, &config);
756
 
757
    /* Write block(s) in polling mode */
758
    while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
759
    {
760
      if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE))
761
      {
762
        /* Write data to SDIO Tx FIFO */
763
        for(count = 0U; count < 8U; count++)
764
        {
765
          SDIO_WriteFIFO(hsd->Instance, (tempbuff + count));
766
        }
767
        tempbuff += 8U;
768
      }
769
 
770
      if((Timeout == 0U)||((HAL_GetTick()-tickstart) >=  Timeout))
771
      {
772
        /* Clear all the static flags */
773
        __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);  
774
        hsd->ErrorCode |= errorstate;
775
        hsd->State = HAL_SD_STATE_READY;
776
        return HAL_TIMEOUT;
777
      }
778
    }
779
 
780
    /* Send stop transmission command in case of multiblock write */
781
    if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
782
    {
783
      if(hsd->SdCard.CardType != CARD_SECURED)
784
      {
785
        /* Send stop transmission command */
786
        errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
787
        if(errorstate != HAL_SD_ERROR_NONE)
788
        {
789
          /* Clear all the static flags */
790
          __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);  
791
          hsd->ErrorCode |= errorstate;
792
          hsd->State = HAL_SD_STATE_READY;
793
          return HAL_ERROR;
794
        }
795
      }
796
    }
797
 
798
    /* Get error state */
799
    if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
800
    {
801
      /* Clear all the static flags */
802
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
803
      hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
804
      hsd->State = HAL_SD_STATE_READY;
805
      return HAL_ERROR;
806
    }
807
    else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
808
    {
809
      /* Clear all the static flags */
810
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
811
      hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;      
812
      hsd->State = HAL_SD_STATE_READY;
813
      return HAL_ERROR;
814
    }
815
    else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR))
816
    {
817
      /* Clear all the static flags */
818
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
819
      hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
820
      hsd->State = HAL_SD_STATE_READY;
821
      return HAL_ERROR;
822
    }
823
 
824
    /* Clear all the static flags */
825
    __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
826
 
827
    hsd->State = HAL_SD_STATE_READY;
828
 
829
    return HAL_OK;
830
  }
831
  else
832
  {
833
    hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
834
    return HAL_ERROR;
835
  }
836
}
837
 
838
/**
839
  * @brief  Reads block(s) from a specified address in a card. The Data transfer
840
  *         is managed in interrupt mode.
841
  * @note   This API should be followed by a check on the card state through
842
  *         HAL_SD_GetCardState().
843
  * @note   You could also check the IT transfer process through the SD Rx
844
  *         interrupt event.
845
  * @param  hsd: Pointer to SD handle                
846
  * @param  pData: Pointer to the buffer that will contain the received data
847
  * @param  BlockAdd: Block Address from where data is to be read
848
  * @param  NumberOfBlocks: Number of blocks to read.
849
  * @retval HAL status
850
  */
851
HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
852
{
853
  SDIO_DataInitTypeDef config;
854
  uint32_t errorstate = HAL_SD_ERROR_NONE;
855
 
856
  if(NULL == pData)
857
  {
858
    hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
859
    return HAL_ERROR;
860
  }
861
 
862
  if(hsd->State == HAL_SD_STATE_READY)
863
  {
864
    hsd->ErrorCode = HAL_DMA_ERROR_NONE;
865
 
866
    if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
867
    {
868
      hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
869
      return HAL_ERROR;
870
    }
871
 
872
    hsd->State = HAL_SD_STATE_BUSY;
873
 
874
    /* Initialize data control register */
875
    hsd->Instance->DCTRL = 0U;
876
 
877
    hsd->pRxBuffPtr = (uint32_t *)pData;
878
    hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
879
 
880
    __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF | SDIO_IT_STBITERR));
881
 
882
    if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
883
    {
884
      BlockAdd *= 512U;
885
    }
886
 
887
    /* Configure the SD DPSM (Data Path State Machine) */
888
    config.DataTimeOut   = SDMMC_DATATIMEOUT;
889
    config.DataLength    = BLOCKSIZE * NumberOfBlocks;
890
    config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
891
    config.TransferDir   = SDIO_TRANSFER_DIR_TO_SDIO;
892
    config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
893
    config.DPSM          = SDIO_DPSM_ENABLE;
894
    SDIO_ConfigData(hsd->Instance, &config);
895
 
896
    /* Set Block Size for Card */
897
    errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
898
    if(errorstate != HAL_SD_ERROR_NONE)
899
    {
900
      /* Clear all the static flags */
901
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
902
      hsd->ErrorCode |= errorstate;
903
      hsd->State = HAL_SD_STATE_READY;
904
      return HAL_ERROR;
905
    }
906
 
907
    /* Read Blocks in IT mode */
908
    if(NumberOfBlocks > 1U)
909
    {
910
      hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
911
 
912
      /* Read Multi Block command */
913
      errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd);
914
    }
915
    else
916
    {
917
      hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
918
 
919
      /* Read Single Block command */
920
      errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd);
921
    }
922
    if(errorstate != HAL_SD_ERROR_NONE)
923
    {
924
      /* Clear all the static flags */
925
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
926
      hsd->ErrorCode |= errorstate;
927
      hsd->State = HAL_SD_STATE_READY;
928
      return HAL_ERROR;
929
    }
930
 
931
    return HAL_OK;
932
  }
933
  else
934
  {
935
    return HAL_BUSY;
936
  }
937
}
938
 
939
/**
940
  * @brief  Writes block(s) to a specified address in a card. The Data transfer
941
  *         is managed in interrupt mode.
942
  * @note   This API should be followed by a check on the card state through
943
  *         HAL_SD_GetCardState().
944
  * @note   You could also check the IT transfer process through the SD Tx
945
  *         interrupt event.
946
  * @param  hsd: Pointer to SD handle
947
  * @param  pData: Pointer to the buffer that will contain the data to transmit
948
  * @param  BlockAdd: Block Address where data will be written    
949
  * @param  NumberOfBlocks: Number of blocks to write
950
  * @retval HAL status
951
  */
952
HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
953
{
954
  SDIO_DataInitTypeDef config;
955
  uint32_t errorstate = HAL_SD_ERROR_NONE;
956
 
957
  if(NULL == pData)
958
  {
959
    hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
960
    return HAL_ERROR;
961
  }
962
 
963
  if(hsd->State == HAL_SD_STATE_READY)
964
  {
965
    hsd->ErrorCode = HAL_DMA_ERROR_NONE;
966
 
967
    if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
968
    {
969
      hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
970
      return HAL_ERROR;
971
    }
972
 
973
    hsd->State = HAL_SD_STATE_BUSY;
974
 
975
    /* Initialize data control register */
976
    hsd->Instance->DCTRL = 0U;
977
 
978
    hsd->pTxBuffPtr = (uint32_t *)pData;
979
    hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
980
 
981
    /* Enable transfer interrupts */
982
    __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE | SDIO_IT_STBITERR));    
983
 
984
    if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
985
    {
986
      BlockAdd *= 512U;
987
    }
988
 
989
    /* Set Block Size for Card */
990
    errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
991
    if(errorstate != HAL_SD_ERROR_NONE)
992
    {
993
      /* Clear all the static flags */
994
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
995
      hsd->ErrorCode |= errorstate;
996
      hsd->State = HAL_SD_STATE_READY;
997
      return HAL_ERROR;
998
    }
999
 
1000
    /* Write Blocks in Polling mode */
1001
    if(NumberOfBlocks > 1U)
1002
    {
1003
      hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT);
1004
 
1005
      /* Write Multi Block command */
1006
      errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd);
1007
    }
1008
    else
1009
    {
1010
      hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
1011
 
1012
      /* Write Single Block command */
1013
      errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd);
1014
    }
1015
    if(errorstate != HAL_SD_ERROR_NONE)
1016
    {
1017
      /* Clear all the static flags */
1018
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1019
      hsd->ErrorCode |= errorstate;
1020
      hsd->State = HAL_SD_STATE_READY;
1021
      return HAL_ERROR;
1022
    }
1023
 
1024
    /* Configure the SD DPSM (Data Path State Machine) */
1025
    config.DataTimeOut   = SDMMC_DATATIMEOUT;
1026
    config.DataLength    = BLOCKSIZE * NumberOfBlocks;
1027
    config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1028
    config.TransferDir   = SDIO_TRANSFER_DIR_TO_CARD;
1029
    config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
1030
    config.DPSM          = SDIO_DPSM_ENABLE;
1031
    SDIO_ConfigData(hsd->Instance, &config);
1032
 
1033
    return HAL_OK;
1034
  }
1035
  else
1036
  {
1037
    return HAL_BUSY;
1038
  }
1039
}
1040
 
1041
/**
1042
  * @brief  Reads block(s) from a specified address in a card. The Data transfer
1043
  *         is managed by DMA mode.
1044
  * @note   This API should be followed by a check on the card state through
1045
  *         HAL_SD_GetCardState().
1046
  * @note   You could also check the DMA transfer process through the SD Rx
1047
  *         interrupt event.
1048
  * @param  hsd: Pointer SD handle                
1049
  * @param  pData: Pointer to the buffer that will contain the received data
1050
  * @param  BlockAdd: Block Address from where data is to be read  
1051
  * @param  NumberOfBlocks: Number of blocks to read.
1052
  * @retval HAL status
1053
  */
1054
HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1055
{
1056
  SDIO_DataInitTypeDef config;
1057
  uint32_t errorstate = HAL_SD_ERROR_NONE;
1058
 
1059
  if(NULL == pData)
1060
  {
1061
    hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1062
    return HAL_ERROR;
1063
  }
1064
 
1065
  if(hsd->State == HAL_SD_STATE_READY)
1066
  {
1067
    hsd->ErrorCode = HAL_DMA_ERROR_NONE;
1068
 
1069
    if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1070
    {
1071
      hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1072
      return HAL_ERROR;
1073
    }
1074
 
1075
    hsd->State = HAL_SD_STATE_BUSY;
1076
 
1077
    /* Initialize data control register */
1078
    hsd->Instance->DCTRL = 0U;
1079
 
1080
    __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_IT_STBITERR));
1081
 
1082
    /* Set the DMA transfer complete callback */
1083
    hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt;
1084
 
1085
    /* Set the DMA error callback */
1086
    hsd->hdmarx->XferErrorCallback = SD_DMAError;
1087
 
1088
    /* Set the DMA Abort callback */
1089
    hsd->hdmarx->XferAbortCallback = NULL;
1090
 
1091
    /* Enable the DMA Channel */
1092
    HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4);
1093
 
1094
    /* Enable SD DMA transfer */
1095
    __HAL_SD_DMA_ENABLE(hsd);
1096
 
1097
    if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1098
    {
1099
      BlockAdd *= 512U;
1100
    }
1101
 
1102
    /* Configure the SD DPSM (Data Path State Machine) */
1103
    config.DataTimeOut   = SDMMC_DATATIMEOUT;
1104
    config.DataLength    = BLOCKSIZE * NumberOfBlocks;
1105
    config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1106
    config.TransferDir   = SDIO_TRANSFER_DIR_TO_SDIO;
1107
    config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
1108
    config.DPSM          = SDIO_DPSM_ENABLE;
1109
    SDIO_ConfigData(hsd->Instance, &config);
1110
 
1111
    /* Set Block Size for Card */
1112
    errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1113
    if(errorstate != HAL_SD_ERROR_NONE)
1114
    {
1115
      /* Clear all the static flags */
1116
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1117
      hsd->ErrorCode |= errorstate;
1118
      hsd->State = HAL_SD_STATE_READY;
1119
      return HAL_ERROR;
1120
    }
1121
 
1122
    /* Read Blocks in DMA mode */
1123
    if(NumberOfBlocks > 1U)
1124
    {
1125
      hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1126
 
1127
      /* Read Multi Block command */
1128
      errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd);
1129
    }
1130
    else
1131
    {
1132
      hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
1133
 
1134
      /* Read Single Block command */
1135
      errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd);
1136
    }
1137
    if(errorstate != HAL_SD_ERROR_NONE)
1138
    {
1139
      /* Clear all the static flags */
1140
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1141
      hsd->ErrorCode |= errorstate;
1142
      hsd->State = HAL_SD_STATE_READY;
1143
      return HAL_ERROR;
1144
    }
1145
 
1146
    return HAL_OK;
1147
  }
1148
  else
1149
  {
1150
    return HAL_BUSY;
1151
  }
1152
}
1153
 
1154
/**
1155
  * @brief  Writes block(s) to a specified address in a card. The Data transfer
1156
  *         is managed by DMA mode.
1157
  * @note   This API should be followed by a check on the card state through
1158
  *         HAL_SD_GetCardState().
1159
  * @note   You could also check the DMA transfer process through the SD Tx
1160
  *         interrupt event.
1161
  * @param  hsd: Pointer to SD handle
1162
  * @param  pData: Pointer to the buffer that will contain the data to transmit
1163
  * @param  BlockAdd: Block Address where data will be written  
1164
  * @param  NumberOfBlocks: Number of blocks to write
1165
  * @retval HAL status
1166
  */
1167
HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1168
{
1169
  SDIO_DataInitTypeDef config;
1170
  uint32_t errorstate = HAL_SD_ERROR_NONE;
1171
 
1172
  if(NULL == pData)
1173
  {
1174
    hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1175
    return HAL_ERROR;
1176
  }
1177
 
1178
  if(hsd->State == HAL_SD_STATE_READY)
1179
  {
1180
    hsd->ErrorCode = HAL_DMA_ERROR_NONE;
1181
 
1182
    if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1183
    {
1184
      hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1185
      return HAL_ERROR;
1186
    }
1187
 
1188
    hsd->State = HAL_SD_STATE_BUSY;
1189
 
1190
    /* Initialize data control register */
1191
    hsd->Instance->DCTRL = 0U;
1192
 
1193
    /* Enable SD Error interrupts */  
1194
    __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR));    
1195
 
1196
    /* Set the DMA transfer complete callback */
1197
    hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt;
1198
 
1199
    /* Set the DMA error callback */
1200
    hsd->hdmatx->XferErrorCallback = SD_DMAError;
1201
 
1202
    /* Set the DMA Abort callback */
1203
    hsd->hdmatx->XferAbortCallback = NULL;
1204
 
1205
    if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1206
    {
1207
      BlockAdd *= 512U;
1208
    }
1209
 
1210
    /* Set Block Size for Card */
1211
    errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1212
    if(errorstate != HAL_SD_ERROR_NONE)
1213
    {
1214
      /* Clear all the static flags */
1215
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1216
      hsd->ErrorCode |= errorstate;
1217
      hsd->State = HAL_SD_STATE_READY;
1218
      return HAL_ERROR;
1219
    }
1220
 
1221
    /* Write Blocks in Polling mode */
1222
    if(NumberOfBlocks > 1U)
1223
    {
1224
      hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1225
 
1226
      /* Write Multi Block command */
1227
      errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd);
1228
    }
1229
    else
1230
    {
1231
      hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
1232
 
1233
      /* Write Single Block command */
1234
      errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd);
1235
    }
1236
    if(errorstate != HAL_SD_ERROR_NONE)
1237
    {
1238
      /* Clear all the static flags */
1239
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1240
      hsd->ErrorCode |= errorstate;
1241
      hsd->State = HAL_SD_STATE_READY;
1242
      return HAL_ERROR;
1243
    }
1244
 
1245
    /* Enable SDIO DMA transfer */
1246
    __HAL_SD_DMA_ENABLE(hsd);
1247
 
1248
    /* Enable the DMA Channel */
1249
    HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4);
1250
 
1251
    /* Configure the SD DPSM (Data Path State Machine) */
1252
    config.DataTimeOut   = SDMMC_DATATIMEOUT;
1253
    config.DataLength    = BLOCKSIZE * NumberOfBlocks;
1254
    config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1255
    config.TransferDir   = SDIO_TRANSFER_DIR_TO_CARD;
1256
    config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
1257
    config.DPSM          = SDIO_DPSM_ENABLE;
1258
    SDIO_ConfigData(hsd->Instance, &config);
1259
 
1260
    return HAL_OK;
1261
  }
1262
  else
1263
  {
1264
    return HAL_BUSY;
1265
  }
1266
}
1267
 
1268
/**
1269
  * @brief  Erases the specified memory area of the given SD card.
1270
  * @note   This API should be followed by a check on the card state through
1271
  *         HAL_SD_GetCardState().
1272
  * @param  hsd: Pointer to SD handle
1273
  * @param  BlockStartAdd: Start Block address
1274
  * @param  BlockEndAdd: End Block address
1275
  * @retval HAL status
1276
  */
1277
HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
1278
{
1279
  uint32_t errorstate = HAL_SD_ERROR_NONE;
1280
 
1281
  if(hsd->State == HAL_SD_STATE_READY)
1282
  {
1283
    hsd->ErrorCode = HAL_DMA_ERROR_NONE;
1284
 
1285
    if(BlockEndAdd < BlockStartAdd)
1286
    {
1287
      hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1288
      return HAL_ERROR;
1289
    }
1290
 
1291
    if(BlockEndAdd > (hsd->SdCard.LogBlockNbr))
1292
    {
1293
      hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1294
      return HAL_ERROR;
1295
    }
1296
 
1297
    hsd->State = HAL_SD_STATE_BUSY;
1298
 
1299
    /* Check if the card command class supports erase command */
1300
    if(((hsd->SdCard.Class) & SDIO_CCCC_ERASE) == 0U)
1301
    {
1302
      /* Clear all the static flags */
1303
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1304
      hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
1305
      hsd->State = HAL_SD_STATE_READY;
1306
      return HAL_ERROR;
1307
    }
1308
 
1309
    if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
1310
    {
1311
      /* Clear all the static flags */
1312
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);  
1313
      hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
1314
      hsd->State = HAL_SD_STATE_READY;
1315
      return HAL_ERROR;
1316
    }
1317
 
1318
    /* Get start and end block for high capacity cards */
1319
    if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1320
    {
1321
      BlockStartAdd *= 512U;
1322
      BlockEndAdd   *= 512U;
1323
    }
1324
 
1325
    /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1326
    if(hsd->SdCard.CardType != CARD_SECURED)
1327
    {
1328
      /* Send CMD32 SD_ERASE_GRP_START with argument as addr  */
1329
      errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, BlockStartAdd);
1330
      if(errorstate != HAL_SD_ERROR_NONE)
1331
      {
1332
        /* Clear all the static flags */
1333
        __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1334
        hsd->ErrorCode |= errorstate;
1335
        hsd->State = HAL_SD_STATE_READY;
1336
        return HAL_ERROR;
1337
      }
1338
 
1339
      /* Send CMD33 SD_ERASE_GRP_END with argument as addr  */
1340
      errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, BlockEndAdd);
1341
      if(errorstate != HAL_SD_ERROR_NONE)
1342
      {
1343
        /* Clear all the static flags */
1344
        __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1345
        hsd->ErrorCode |= errorstate;
1346
        hsd->State = HAL_SD_STATE_READY;
1347
        return HAL_ERROR;
1348
      }
1349
    }
1350
 
1351
    /* Send CMD38 ERASE */
1352
    errorstate = SDMMC_CmdErase(hsd->Instance);
1353
    if(errorstate != HAL_SD_ERROR_NONE)
1354
    {
1355
      /* Clear all the static flags */
1356
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1357
      hsd->ErrorCode |= errorstate;
1358
      hsd->State = HAL_SD_STATE_READY;
1359
      return HAL_ERROR;
1360
    }
1361
 
1362
    hsd->State = HAL_SD_STATE_READY;
1363
 
1364
    return HAL_OK;
1365
  }
1366
  else
1367
  {
1368
    return HAL_BUSY;
1369
  }
1370
}
1371
 
1372
/**
1373
  * @brief  This function handles SD card interrupt request.
1374
  * @param  hsd: Pointer to SD handle
1375
  * @retval None
1376
  */
1377
void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
1378
{
1379
  uint32_t errorstate = HAL_SD_ERROR_NONE;
1380
 
1381
  /* Check for SDIO interrupt flags */
1382
  if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DATAEND) != RESET)
1383
  {
1384
    __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DATAEND);
1385
 
1386
    __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
1387
                             SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR | SDIO_IT_STBITERR);
1388
 
1389
    if((hsd->Context & SD_CONTEXT_IT) != RESET)
1390
    {
1391
      if(((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != RESET) || ((hsd->Context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET))
1392
      {
1393
        errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1394
        if(errorstate != HAL_SD_ERROR_NONE)
1395
        {
1396
          hsd->ErrorCode |= errorstate;
1397
          HAL_SD_ErrorCallback(hsd);
1398
        }
1399
      }
1400
 
1401
      /* Clear all the static flags */
1402
      __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1403
 
1404
      hsd->State = HAL_SD_STATE_READY;
1405
      if(((hsd->Context & SD_CONTEXT_READ_SINGLE_BLOCK) != RESET) || ((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != RESET))
1406
      {
1407
        HAL_SD_RxCpltCallback(hsd);
1408
      }
1409
      else
1410
      {
1411
        HAL_SD_TxCpltCallback(hsd);
1412
      }
1413
    }
1414
    else if((hsd->Context & SD_CONTEXT_DMA) != RESET)
1415
    {
1416
      if((hsd->Context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET)
1417
      {
1418
        errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1419
        if(errorstate != HAL_SD_ERROR_NONE)
1420
        {
1421
          hsd->ErrorCode |= errorstate;
1422
          HAL_SD_ErrorCallback(hsd);
1423
        }
1424
      }
1425
      if(((hsd->Context & SD_CONTEXT_READ_SINGLE_BLOCK) == RESET) && ((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == RESET))
1426
      {
1427
        /* Disable the DMA transfer for transmit request by setting the DMAEN bit
1428
        in the SD DCTRL register */
1429
        hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
1430
 
1431
        hsd->State = HAL_SD_STATE_READY;
1432
 
1433
        HAL_SD_TxCpltCallback(hsd);
1434
      }
1435
    }
1436
  }
1437
 
1438
  else if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_TXFIFOHE) != RESET)
1439
  {
1440
    __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_TXFIFOHE);
1441
 
1442
    SD_Write_IT(hsd);
1443
  }
1444
 
1445
  else if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_RXFIFOHF) != RESET)
1446
  {
1447
    __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_RXFIFOHF);
1448
 
1449
    SD_Read_IT(hsd);
1450
  }
1451
 
1452
  else if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR) != RESET)
1453
  {
1454
    /* Set Error code */
1455
    if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DCRCFAIL) != RESET)
1456
    {
1457
      hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
1458
    }
1459
    if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DTIMEOUT) != RESET)
1460
    {
1461
      hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1462
    }
1463
    if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_RXOVERR) != RESET)
1464
    {
1465
      hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
1466
    }
1467
    if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_TXUNDERR) != RESET)
1468
    {
1469
      hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
1470
    }
1471
    if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_STBITERR) != RESET)
1472
    {
1473
      hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1474
    }
1475
 
1476
    /* Clear All flags */
1477
    __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS | SDIO_FLAG_STBITERR);
1478
 
1479
    /* Disable all interrupts */
1480
    __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
1481
                             SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR |SDIO_IT_STBITERR);
1482
 
1483
    if((hsd->Context & SD_CONTEXT_DMA) != RESET)
1484
    {
1485
      /* Abort the SD DMA Streams */
1486
      if(hsd->hdmatx != NULL)
1487
      {
1488
        /* Set the DMA Tx abort callback */
1489
        hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
1490
        /* Abort DMA in IT mode */
1491
        if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
1492
        {
1493
          SD_DMATxAbort(hsd->hdmatx);
1494
        }
1495
      }
1496
      else if(hsd->hdmarx != NULL)
1497
      {
1498
        /* Set the DMA Rx abort callback */
1499
        hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
1500
        /* Abort DMA in IT mode */
1501
        if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
1502
        {
1503
          SD_DMARxAbort(hsd->hdmarx);
1504
        }
1505
      }
1506
      else
1507
      {
1508
        hsd->ErrorCode = HAL_SD_ERROR_NONE;
1509
        hsd->State = HAL_SD_STATE_READY;
1510
        HAL_SD_AbortCallback(hsd);
1511
      }
1512
    }
1513
    else if((hsd->Context & SD_CONTEXT_IT) != RESET)
1514
    {
1515
      /* Set the SD state to ready to be able to start again the process */
1516
      hsd->State = HAL_SD_STATE_READY;
1517
      HAL_SD_ErrorCallback(hsd);
1518
    }
1519
  }
1520
}
1521
 
1522
/**
1523
  * @brief return the SD state
1524
  * @param hsd: Pointer to sd handle
1525
  * @retval HAL state
1526
  */
1527
HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd)
1528
{
1529
  return hsd->State;
1530
}
1531
 
1532
/**
1533
* @brief  Return the SD error code
1534
* @param  hsd : Pointer to a SD_HandleTypeDef structure that contains
1535
  *              the configuration information.
1536
* @retval SD Error Code
1537
*/
1538
uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd)
1539
{
1540
  return hsd->ErrorCode;
1541
}
1542
 
1543
/**
1544
  * @brief Tx Transfer completed callbacks
1545
  * @param hsd: Pointer to SD handle
1546
  * @retval None
1547
  */
1548
 __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
1549
{
1550
  /* Prevent unused argument(s) compilation warning */
1551
  UNUSED(hsd);
1552
 
1553
  /* NOTE : This function should not be modified, when the callback is needed,
1554
            the HAL_SD_TxCpltCallback can be implemented in the user file
1555
   */
1556
}
1557
 
1558
/**
1559
  * @brief Rx Transfer completed callbacks
1560
  * @param hsd: Pointer SD handle
1561
  * @retval None
1562
  */
1563
__weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
1564
{
1565
  /* Prevent unused argument(s) compilation warning */
1566
  UNUSED(hsd);
1567
 
1568
  /* NOTE : This function should not be modified, when the callback is needed,
1569
            the HAL_SD_RxCpltCallback can be implemented in the user file
1570
   */
1571
}
1572
 
1573
/**
1574
  * @brief SD error callbacks
1575
  * @param hsd: Pointer SD handle
1576
  * @retval None
1577
  */
1578
__weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
1579
{
1580
  /* Prevent unused argument(s) compilation warning */
1581
  UNUSED(hsd);
1582
 
1583
  /* NOTE : This function should not be modified, when the callback is needed,
1584
            the HAL_SD_ErrorCallback can be implemented in the user file
1585
   */
1586
}
1587
 
1588
/**
1589
  * @brief SD Abort callbacks
1590
  * @param hsd: Pointer SD handle
1591
  * @retval None
1592
  */
1593
__weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
1594
{
1595
  /* Prevent unused argument(s) compilation warning */
1596
  UNUSED(hsd);
1597
 
1598
  /* NOTE : This function should not be modified, when the callback is needed,
1599
            the HAL_SD_ErrorCallback can be implemented in the user file
1600
   */
1601
}
1602
 
1603
 
1604
/**
1605
  * @}
1606
  */
1607
 
1608
/** @addtogroup SD_Exported_Functions_Group3
1609
 *  @brief   management functions
1610
 *
1611
@verbatim  
1612
  ==============================================================================
1613
                      ##### Peripheral Control functions #####
1614
  ==============================================================================  
1615
  [..]
1616
    This subsection provides a set of functions allowing to control the SD card
1617
    operations and get the related information
1618
 
1619
@endverbatim
1620
  * @{
1621
  */
1622
 
1623
/**
1624
  * @brief  Returns information the information of the card which are stored on
1625
  *         the CID register.
1626
  * @param  hsd: Pointer to SD handle
1627
  * @param  pCID: Pointer to a HAL_SD_CIDTypeDef structure that  
1628
  *         contains all CID register parameters
1629
  * @retval HAL status
1630
  */
1631
HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID)
1632
{
1633
  uint32_t tmp = 0U;
1634
 
1635
  /* Byte 0 */
1636
  tmp = (uint8_t)((hsd->CID[0U] & 0xFF000000U) >> 24U);
1637
  pCID->ManufacturerID = tmp;
1638
 
1639
  /* Byte 1 */
1640
  tmp = (uint8_t)((hsd->CID[0U] & 0x00FF0000U) >> 16U);
1641
  pCID->OEM_AppliID = tmp << 8U;
1642
 
1643
  /* Byte 2 */
1644
  tmp = (uint8_t)((hsd->CID[0U] & 0x000000FF00U) >> 8U);
1645
  pCID->OEM_AppliID |= tmp;
1646
 
1647
  /* Byte 3 */
1648
  tmp = (uint8_t)(hsd->CID[0U] & 0x000000FFU);
1649
  pCID->ProdName1 = tmp << 24U;
1650
 
1651
  /* Byte 4 */
1652
  tmp = (uint8_t)((hsd->CID[1U] & 0xFF000000U) >> 24U);
1653
  pCID->ProdName1 |= tmp << 16;
1654
 
1655
  /* Byte 5 */
1656
  tmp = (uint8_t)((hsd->CID[1U] & 0x00FF0000U) >> 16U);
1657
  pCID->ProdName1 |= tmp << 8U;
1658
 
1659
  /* Byte 6 */
1660
  tmp = (uint8_t)((hsd->CID[1U] & 0x0000FF00U) >> 8U);
1661
  pCID->ProdName1 |= tmp;
1662
 
1663
  /* Byte 7 */
1664
  tmp = (uint8_t)(hsd->CID[1U] & 0x000000FFU);
1665
  pCID->ProdName2 = tmp;
1666
 
1667
  /* Byte 8 */
1668
  tmp = (uint8_t)((hsd->CID[2U] & 0xFF000000U) >> 24U);
1669
  pCID->ProdRev = tmp;
1670
 
1671
  /* Byte 9 */
1672
  tmp = (uint8_t)((hsd->CID[2U] & 0x00FF0000U) >> 16U);
1673
  pCID->ProdSN = tmp << 24U;
1674
 
1675
  /* Byte 10 */
1676
  tmp = (uint8_t)((hsd->CID[2U] & 0x0000FF00U) >> 8U);
1677
  pCID->ProdSN |= tmp << 16U;
1678
 
1679
  /* Byte 11 */
1680
  tmp = (uint8_t)(hsd->CID[2U] & 0x000000FFU);
1681
  pCID->ProdSN |= tmp << 8U;
1682
 
1683
  /* Byte 12 */
1684
  tmp = (uint8_t)((hsd->CID[3U] & 0xFF000000U) >> 24U);
1685
  pCID->ProdSN |= tmp;
1686
 
1687
  /* Byte 13 */
1688
  tmp = (uint8_t)((hsd->CID[3U] & 0x00FF0000U) >> 16U);
1689
  pCID->Reserved1   |= (tmp & 0xF0U) >> 4U;
1690
  pCID->ManufactDate = (tmp & 0x0FU) << 8U;
1691
 
1692
  /* Byte 14 */
1693
  tmp = (uint8_t)((hsd->CID[3U] & 0x0000FF00U) >> 8U);
1694
  pCID->ManufactDate |= tmp;
1695
 
1696
  /* Byte 15 */
1697
  tmp = (uint8_t)(hsd->CID[3U] & 0x000000FFU);
1698
  pCID->CID_CRC   = (tmp & 0xFEU) >> 1U;
1699
  pCID->Reserved2 = 1U;
1700
 
1701
  return HAL_OK;
1702
}
1703
 
1704
/**
1705
  * @brief  Returns information the information of the card which are stored on
1706
  *         the CSD register.
1707
  * @param  hsd: Pointer to SD handle
1708
  * @param  pCSD: Pointer to a HAL_SD_CardCSDTypeDef structure that  
1709
  *         contains all CSD register parameters  
1710
  * @retval HAL status
1711
  */
1712
HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD)
1713
{
1714
  uint32_t tmp = 0U;
1715
 
1716
  /* Byte 0 */
1717
  tmp = (hsd->CSD[0U] & 0xFF000000U) >> 24U;
1718
  pCSD->CSDStruct      = (uint8_t)((tmp & 0xC0U) >> 6U);
1719
  pCSD->SysSpecVersion = (uint8_t)((tmp & 0x3CU) >> 2U);
1720
  pCSD->Reserved1      = tmp & 0x03U;
1721
 
1722
  /* Byte 1 */
1723
  tmp = (hsd->CSD[0U] & 0x00FF0000U) >> 16U;
1724
  pCSD->TAAC = (uint8_t)tmp;
1725
 
1726
  /* Byte 2 */
1727
  tmp = (hsd->CSD[0U] & 0x0000FF00U) >> 8U;
1728
  pCSD->NSAC = (uint8_t)tmp;
1729
 
1730
  /* Byte 3 */
1731
  tmp = hsd->CSD[0U] & 0x000000FFU;
1732
  pCSD->MaxBusClkFrec = (uint8_t)tmp;
1733
 
1734
  /* Byte 4 */
1735
  tmp = (hsd->CSD[1U] & 0xFF000000U) >> 24U;
1736
  pCSD->CardComdClasses = (uint16_t)(tmp << 4U);
1737
 
1738
  /* Byte 5 */
1739
  tmp = (hsd->CSD[1U] & 0x00FF0000U) >> 16U;
1740
  pCSD->CardComdClasses |= (uint16_t)((tmp & 0xF0U) >> 4U);
1741
  pCSD->RdBlockLen       = (uint8_t)(tmp & 0x0FU);
1742
 
1743
  /* Byte 6 */
1744
  tmp = (hsd->CSD[1U] & 0x0000FF00U) >> 8U;
1745
  pCSD->PartBlockRead   = (uint8_t)((tmp & 0x80U) >> 7U);
1746
  pCSD->WrBlockMisalign = (uint8_t)((tmp & 0x40U) >> 6U);
1747
  pCSD->RdBlockMisalign = (uint8_t)((tmp & 0x20U) >> 5U);
1748
  pCSD->DSRImpl         = (uint8_t)((tmp & 0x10U) >> 4U);
1749
  pCSD->Reserved2       = 0U; /*!< Reserved */
1750
 
1751
  if(hsd->SdCard.CardType == CARD_SDSC)
1752
  {
1753
    pCSD->DeviceSize = (tmp & 0x03U) << 10U;
1754
 
1755
    /* Byte 7 */
1756
    tmp = (uint8_t)(hsd->CSD[1U] & 0x000000FFU);
1757
    pCSD->DeviceSize |= (tmp) << 2U;
1758
 
1759
    /* Byte 8 */
1760
    tmp = (uint8_t)((hsd->CSD[2U] & 0xFF000000U) >> 24U);
1761
    pCSD->DeviceSize |= (tmp & 0xC0U) >> 6U;
1762
 
1763
    pCSD->MaxRdCurrentVDDMin = (tmp & 0x38U) >> 3U;
1764
    pCSD->MaxRdCurrentVDDMax = (tmp & 0x07U);
1765
 
1766
    /* Byte 9 */
1767
    tmp = (uint8_t)((hsd->CSD[2U] & 0x00FF0000U) >> 16U);
1768
    pCSD->MaxWrCurrentVDDMin = (tmp & 0xE0U) >> 5U;
1769
    pCSD->MaxWrCurrentVDDMax = (tmp & 0x1CU) >> 2U;
1770
    pCSD->DeviceSizeMul      = (tmp & 0x03U) << 1U;
1771
    /* Byte 10 */
1772
    tmp = (uint8_t)((hsd->CSD[2U] & 0x0000FF00U) >> 8U);
1773
    pCSD->DeviceSizeMul |= (tmp & 0x80U) >> 7U;
1774
 
1775
    hsd->SdCard.BlockNbr  = (pCSD->DeviceSize + 1U) ;
1776
    hsd->SdCard.BlockNbr *= (1U << (pCSD->DeviceSizeMul + 2U));
1777
    hsd->SdCard.BlockSize = 1U << (pCSD->RdBlockLen);
1778
 
1779
    hsd->SdCard.LogBlockNbr =  (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U);
1780
    hsd->SdCard.LogBlockSize = 512U;
1781
  }
1782
  else if(hsd->SdCard.CardType == CARD_SDHC_SDXC)
1783
  {
1784
    /* Byte 7 */
1785
    tmp = (uint8_t)(hsd->CSD[1U] & 0x000000FFU);
1786
    pCSD->DeviceSize = (tmp & 0x3FU) << 16U;
1787
 
1788
    /* Byte 8 */
1789
    tmp = (uint8_t)((hsd->CSD[2U] & 0xFF000000U) >> 24U);
1790
 
1791
    pCSD->DeviceSize |= (tmp << 8U);
1792
 
1793
    /* Byte 9 */
1794
    tmp = (uint8_t)((hsd->CSD[2U] & 0x00FF0000U) >> 16U);
1795
 
1796
    pCSD->DeviceSize |= (tmp);
1797
 
1798
    /* Byte 10 */
1799
    tmp = (uint8_t)((hsd->CSD[2U] & 0x0000FF00U) >> 8U);
1800
 
1801
    hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr = (((uint64_t)pCSD->DeviceSize + 1U) * 1024U);
1802
    hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize = 512U;
1803
  }
1804
  else
1805
  {
1806
    /* Clear all the static flags */
1807
    __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);  
1808
    hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
1809
    hsd->State = HAL_SD_STATE_READY;
1810
    return HAL_ERROR;
1811
  }
1812
 
1813
  pCSD->EraseGrSize = (tmp & 0x40U) >> 6U;
1814
  pCSD->EraseGrMul  = (tmp & 0x3FU) << 1U;
1815
 
1816
  /* Byte 11 */
1817
  tmp = (uint8_t)(hsd->CSD[2U] & 0x000000FFU);
1818
  pCSD->EraseGrMul     |= (tmp & 0x80U) >> 7U;
1819
  pCSD->WrProtectGrSize = (tmp & 0x7FU);
1820
 
1821
  /* Byte 12 */
1822
  tmp = (uint8_t)((hsd->CSD[3U] & 0xFF000000U) >> 24U);
1823
  pCSD->WrProtectGrEnable = (tmp & 0x80U) >> 7U;
1824
  pCSD->ManDeflECC        = (tmp & 0x60U) >> 5U;
1825
  pCSD->WrSpeedFact       = (tmp & 0x1CU) >> 2U;
1826
  pCSD->MaxWrBlockLen     = (tmp & 0x03U) << 2U;
1827
 
1828
  /* Byte 13 */
1829
  tmp = (uint8_t)((hsd->CSD[3U] & 0x00FF0000U) >> 16U);
1830
  pCSD->MaxWrBlockLen      |= (tmp & 0xC0U) >> 6U;
1831
  pCSD->WriteBlockPaPartial = (tmp & 0x20U) >> 5U;
1832
  pCSD->Reserved3           = 0U;
1833
  pCSD->ContentProtectAppli = (tmp & 0x01U);
1834
 
1835
  /* Byte 14 */
1836
  tmp = (uint8_t)((hsd->CSD[3U] & 0x0000FF00U) >> 8U);
1837
  pCSD->FileFormatGrouop = (tmp & 0x80U) >> 7U;
1838
  pCSD->CopyFlag         = (tmp & 0x40U) >> 6U;
1839
  pCSD->PermWrProtect    = (tmp & 0x20U) >> 5U;
1840
  pCSD->TempWrProtect    = (tmp & 0x10U) >> 4U;
1841
  pCSD->FileFormat       = (tmp & 0x0CU) >> 2U;
1842
  pCSD->ECC              = (tmp & 0x03U);
1843
 
1844
  /* Byte 15 */
1845
  tmp = (uint8_t)(hsd->CSD[3U] & 0x000000FFU);
1846
  pCSD->CSD_CRC   = (tmp & 0xFEU) >> 1U;
1847
  pCSD->Reserved4 = 1U;
1848
 
1849
  return HAL_OK;
1850
}
1851
 
1852
/**
1853
  * @brief  Gets the SD status info.
1854
  * @param  hsd: Pointer to SD handle      
1855
  * @param  pStatus: Pointer to the HAL_SD_CardStatusTypeDef structure that
1856
  *         will contain the SD card status information
1857
  * @retval HAL status
1858
  */
1859
HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
1860
{
1861
  uint32_t tmp = 0U;
1862
  uint32_t sd_status[16U];
1863
  uint32_t errorstate = HAL_SD_ERROR_NONE;
1864
 
1865
  errorstate = SD_SendSDStatus(hsd, sd_status);
1866
  if(errorstate != HAL_OK)
1867
  {
1868
    /* Clear all the static flags */
1869
    __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);  
1870
    hsd->ErrorCode |= errorstate;
1871
    hsd->State = HAL_SD_STATE_READY;
1872
    return HAL_ERROR;
1873
  }
1874
  else
1875
  {
1876
    /* Byte 0 */
1877
    tmp = (sd_status[0U] & 0xC0U) >> 6U;
1878
    pStatus->DataBusWidth = (uint8_t)tmp;
1879
 
1880
    /* Byte 0 */
1881
    tmp = (sd_status[0U] & 0x20U) >> 5U;
1882
    pStatus->SecuredMode = (uint8_t)tmp;
1883
 
1884
    /* Byte 2 */
1885
    tmp = (sd_status[0U] & 0x00FF0000U) >> 16U;
1886
    pStatus->CardType = (uint16_t)(tmp << 8U);
1887
 
1888
    /* Byte 3 */
1889
    tmp = (sd_status[0U] & 0xFF000000U) >> 24U;
1890
    pStatus->CardType |= (uint16_t)tmp;
1891
 
1892
    /* Byte 4 */
1893
    tmp = (sd_status[1U] & 0xFFU);
1894
    pStatus->ProtectedAreaSize = (uint32_t)(tmp << 24U);
1895
 
1896
    /* Byte 5 */
1897
    tmp = (sd_status[1U] & 0xFF00U) >> 8U;
1898
    pStatus->ProtectedAreaSize |= (uint32_t)(tmp << 16U);
1899
 
1900
    /* Byte 6 */
1901
    tmp = (sd_status[1U] & 0xFF0000U) >> 16U;
1902
    pStatus->ProtectedAreaSize |= (uint32_t)(tmp << 8U);
1903
 
1904
    /* Byte 7 */
1905
    tmp = (sd_status[1U] & 0xFF000000U) >> 24U;
1906
    pStatus->ProtectedAreaSize |= (uint32_t)tmp;
1907
 
1908
    /* Byte 8 */
1909
    tmp = (sd_status[2U] & 0xFFU);
1910
    pStatus->SpeedClass = (uint8_t)tmp;
1911
 
1912
    /* Byte 9 */
1913
    tmp = (sd_status[2U] & 0xFF00U) >> 8U;
1914
    pStatus->PerformanceMove = (uint8_t)tmp;
1915
 
1916
    /* Byte 10 */
1917
    tmp = (sd_status[2U] & 0xF00000U) >> 20U;
1918
    pStatus->AllocationUnitSize = (uint8_t)tmp;
1919
 
1920
    /* Byte 11 */
1921
    tmp = (sd_status[2U] & 0xFF000000U) >> 24U;
1922
    pStatus->EraseSize = (uint16_t)(tmp << 8U);
1923
 
1924
    /* Byte 12 */
1925
    tmp = (sd_status[3U] & 0xFFU);
1926
    pStatus->EraseSize |= (uint16_t)tmp;
1927
 
1928
    /* Byte 13 */
1929
    tmp = (sd_status[3U] & 0xFC00U) >> 10U;
1930
    pStatus->EraseTimeout = (uint8_t)tmp;
1931
 
1932
    /* Byte 13 */
1933
    tmp = (sd_status[3U] & 0x0300U) >> 8U;
1934
    pStatus->EraseOffset = (uint8_t)tmp;
1935
  }
1936
 
1937
  return HAL_OK;
1938
}
1939
 
1940
/**
1941
  * @brief  Gets the SD card info.
1942
  * @param  hsd: Pointer to SD handle      
1943
  * @param  pCardInfo: Pointer to the HAL_SD_CardInfoTypeDef structure that
1944
  *         will contain the SD card status information
1945
  * @retval HAL status
1946
  */
1947
HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
1948
{
1949
  pCardInfo->CardType     = (uint32_t)(hsd->SdCard.CardType);
1950
  pCardInfo->CardVersion  = (uint32_t)(hsd->SdCard.CardVersion);
1951
  pCardInfo->Class        = (uint32_t)(hsd->SdCard.Class);
1952
  pCardInfo->RelCardAdd   = (uint32_t)(hsd->SdCard.RelCardAdd);
1953
  pCardInfo->BlockNbr     = (uint32_t)(hsd->SdCard.BlockNbr);
1954
  pCardInfo->BlockSize    = (uint32_t)(hsd->SdCard.BlockSize);
1955
  pCardInfo->LogBlockNbr  = (uint32_t)(hsd->SdCard.LogBlockNbr);
1956
  pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize);
1957
 
1958
  return HAL_OK;
1959
}
1960
 
1961
/**
1962
  * @brief  Enables wide bus operation for the requested card if supported by
1963
  *         card.
1964
  * @param  hsd: Pointer to SD handle      
1965
  * @param  WideMode: Specifies the SD card wide bus mode
1966
  *          This parameter can be one of the following values:
1967
  *            @arg SDIO_BUS_WIDE_8B: 8-bit data transfer
1968
  *            @arg SDIO_BUS_WIDE_4B: 4-bit data transfer
1969
  *            @arg SDIO_BUS_WIDE_1B: 1-bit data transfer
1970
  * @retval HAL status
1971
  */
1972
HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
1973
{
1974
  SDIO_InitTypeDef Init;
1975
  uint32_t errorstate = HAL_SD_ERROR_NONE;
1976
 
1977
  /* Check the parameters */
1978
  assert_param(IS_SDIO_BUS_WIDE(WideMode));
1979
 
1980
  /* Chnage Satte */
1981
  hsd->State = HAL_SD_STATE_BUSY;
1982
 
1983
  if(hsd->SdCard.CardType != CARD_SECURED)
1984
  {
1985
    if(WideMode == SDIO_BUS_WIDE_8B)
1986
    {
1987
      hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
1988
    }
1989
    else if(WideMode == SDIO_BUS_WIDE_4B)
1990
    {
1991
      errorstate = SD_WideBus_Enable(hsd);
1992
 
1993
      hsd->ErrorCode |= errorstate;
1994
    }
1995
    else if(WideMode == SDIO_BUS_WIDE_1B)
1996
    {
1997
      errorstate = SD_WideBus_Disable(hsd);
1998
 
1999
      hsd->ErrorCode |= errorstate;
2000
    }
2001
    else
2002
    {
2003
      /* WideMode is not a valid argument*/
2004
      hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2005
    }
2006
  }  
2007
  else
2008
  {
2009
    /* MMC Card does not support this feature */
2010
    hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2011
  }
2012
 
2013
  if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2014
  {
2015
    /* Clear all the static flags */
2016
    __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2017
    hsd->State = HAL_SD_STATE_READY;
2018
    return HAL_ERROR;
2019
  }
2020
  else
2021
  {
2022
    /* Configure the SDIO peripheral */
2023
    Init.ClockEdge           = hsd->Init.ClockEdge;
2024
    Init.ClockBypass         = hsd->Init.ClockBypass;
2025
    Init.ClockPowerSave      = hsd->Init.ClockPowerSave;
2026
    Init.BusWide             = WideMode;
2027
    Init.HardwareFlowControl = hsd->Init.HardwareFlowControl;
2028
    Init.ClockDiv            = hsd->Init.ClockDiv;
2029
    SDIO_Init(hsd->Instance, Init);
2030
  }
2031
 
2032
  /* Change State */
2033
  hsd->State = HAL_SD_STATE_READY;
2034
 
2035
  return HAL_OK;
2036
}
2037
 
2038
 
2039
/**
2040
  * @brief  Gets the current sd card data state.
2041
  * @param  hsd: pointer to SD handle
2042
  * @retval Card state
2043
  */
2044
HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
2045
{
2046
  HAL_SD_CardStateTypeDef cardstate =  HAL_SD_CARD_TRANSFER;
2047
  uint32_t errorstate = HAL_SD_ERROR_NONE;
2048
  uint32_t resp1 = 0;
2049
 
2050
  errorstate = SD_SendStatus(hsd, &resp1);
2051
  if(errorstate != HAL_OK)
2052
  {
2053
    hsd->ErrorCode |= errorstate;
2054
  }
2055
 
2056
  cardstate = (HAL_SD_CardStateTypeDef)((resp1 >> 9U) & 0x0FU);
2057
 
2058
  return cardstate;
2059
}
2060
 
2061
/**
2062
  * @brief  Abort the current transfer and disable the SD.
2063
  * @param  hsd: pointer to a SD_HandleTypeDef structure that contains
2064
  *                the configuration information for SD module.
2065
  * @retval HAL status
2066
  */
2067
HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd)
2068
{
2069
  HAL_SD_CardStateTypeDef CardState;
2070
 
2071
  /* DIsable All interrupts */
2072
  __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
2073
                           SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
2074
 
2075
  /* Clear All flags */
2076
  __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2077
 
2078
  if((hsd->hdmatx != NULL) || (hsd->hdmarx != NULL))
2079
  {
2080
    /* Disable the SD DMA request */
2081
    hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
2082
 
2083
    /* Abort the SD DMA Tx Stream */
2084
    if(hsd->hdmatx != NULL)
2085
    {
2086
      HAL_DMA_Abort(hsd->hdmatx);
2087
    }
2088
    /* Abort the SD DMA Rx Stream */
2089
    if(hsd->hdmarx != NULL)
2090
    {
2091
      HAL_DMA_Abort(hsd->hdmarx);
2092
    }
2093
  }
2094
 
2095
  hsd->State = HAL_SD_STATE_READY;
2096
  CardState = HAL_SD_GetCardState(hsd);
2097
  if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2098
  {
2099
    hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2100
  }
2101
  if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2102
  {
2103
    return HAL_ERROR;
2104
  }
2105
  return HAL_OK;
2106
}
2107
 
2108
/**
2109
  * @brief  Abort the current transfer and disable the SD (IT mode).
2110
  * @param  hsd: pointer to a SD_HandleTypeDef structure that contains
2111
  *                the configuration information for SD module.
2112
  * @retval HAL status
2113
  */
2114
HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd)
2115
{
2116
  HAL_SD_CardStateTypeDef CardState;
2117
 
2118
  /* DIsable All interrupts */
2119
  __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
2120
                           SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
2121
 
2122
  /* Clear All flags */
2123
  __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2124
 
2125
  if((hsd->hdmatx != NULL) || (hsd->hdmarx != NULL))
2126
  {
2127
    /* Disable the SD DMA request */
2128
    hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
2129
 
2130
    /* Abort the SD DMA Tx Stream */
2131
    if(hsd->hdmatx != NULL)
2132
    {
2133
      hsd->hdmatx->XferAbortCallback =  SD_DMATxAbort;
2134
      if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
2135
      {
2136
        hsd->hdmatx = NULL;
2137
      }
2138
    }
2139
    /* Abort the SD DMA Rx Stream */
2140
    if(hsd->hdmarx != NULL)
2141
    {
2142
      hsd->hdmarx->XferAbortCallback =  SD_DMARxAbort;
2143
      if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
2144
      {
2145
        hsd->hdmarx = NULL;
2146
      }
2147
    }
2148
  }
2149
 
2150
  /* No transfer ongoing on both DMA channels*/
2151
  if((hsd->hdmatx == NULL) && (hsd->hdmarx == NULL))
2152
  {
2153
    CardState = HAL_SD_GetCardState(hsd);
2154
    hsd->State = HAL_SD_STATE_READY;
2155
    if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2156
    {
2157
      hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2158
    }
2159
    if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2160
    {
2161
      return HAL_ERROR;
2162
    }
2163
    else
2164
    {
2165
      HAL_SD_AbortCallback(hsd);
2166
    }
2167
  }
2168
 
2169
  return HAL_OK;
2170
}
2171
 
2172
/**
2173
  * @}
2174
  */
2175
 
2176
/**
2177
  * @}
2178
  */
2179
 
2180
/* Private function ----------------------------------------------------------*/  
2181
/** @addtogroup SD_Private_Functions
2182
  * @{
2183
  */
2184
 
2185
/**
2186
  * @brief  DMA SD transmit process complete callback
2187
  * @param  hdma: DMA handle
2188
  * @retval None
2189
  */
2190
static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma)    
2191
{
2192
  SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2193
 
2194
  /* Enable DATAEND Interrupt */
2195
  __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DATAEND));
2196
}
2197
 
2198
/**
2199
  * @brief  DMA SD receive process complete callback
2200
  * @param  hdma: DMA handle
2201
  * @retval None
2202
  */
2203
static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)  
2204
{
2205
  SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2206
  uint32_t errorstate = HAL_SD_ERROR_NONE;
2207
 
2208
  /* Send stop command in multiblock write */
2209
  if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA))
2210
  {
2211
    errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
2212
    if(errorstate != HAL_SD_ERROR_NONE)
2213
    {
2214
      hsd->ErrorCode |= errorstate;
2215
      HAL_SD_ErrorCallback(hsd);
2216
    }
2217
  }
2218
 
2219
  /* Disable the DMA transfer for transmit request by setting the DMAEN bit
2220
  in the SD DCTRL register */
2221
  hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
2222
 
2223
  /* Clear all the static flags */
2224
  __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2225
 
2226
  hsd->State = HAL_SD_STATE_READY;
2227
 
2228
  HAL_SD_RxCpltCallback(hsd);
2229
}
2230
 
2231
/**
2232
  * @brief  DMA SD communication error callback
2233
  * @param  hdma: DMA handle
2234
  * @retval None
2235
  */
2236
static void SD_DMAError(DMA_HandleTypeDef *hdma)  
2237
{
2238
  SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2239
  HAL_SD_CardStateTypeDef CardState;
2240
 
2241
  if((hsd->hdmarx->ErrorCode == HAL_DMA_ERROR_TE) || (hsd->hdmatx->ErrorCode == HAL_DMA_ERROR_TE))
2242
  {
2243
    /* Clear All flags */
2244
    __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2245
 
2246
    /* Disable All interrupts */
2247
    __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
2248
      SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
2249
 
2250
    hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2251
    CardState = HAL_SD_GetCardState(hsd);
2252
    if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2253
    {
2254
      hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2255
    }
2256
 
2257
    hsd->State= HAL_SD_STATE_READY;
2258
  }
2259
 
2260
  HAL_SD_ErrorCallback(hsd);
2261
}
2262
 
2263
/**
2264
  * @brief  DMA SD Tx Abort callback
2265
  * @param  hdma: DMA handle
2266
  * @retval None
2267
  */
2268
static void SD_DMATxAbort(DMA_HandleTypeDef *hdma)  
2269
{
2270
  SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2271
  HAL_SD_CardStateTypeDef CardState;
2272
 
2273
  if(hsd->hdmatx != NULL)
2274
  {
2275
    hsd->hdmatx = NULL;
2276
  }
2277
 
2278
  /* All DMA channels are aborted */
2279
  if(hsd->hdmarx == NULL)
2280
  {
2281
    CardState = HAL_SD_GetCardState(hsd);
2282
    hsd->ErrorCode = HAL_SD_ERROR_NONE;
2283
    hsd->State = HAL_SD_STATE_READY;
2284
    if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2285
    {
2286
      hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2287
 
2288
      if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2289
      {
2290
        HAL_SD_AbortCallback(hsd);
2291
      }
2292
      else
2293
      {
2294
        HAL_SD_ErrorCallback(hsd);
2295
      }
2296
    }
2297
  }
2298
}
2299
 
2300
/**
2301
  * @brief  DMA SD Rx Abort callback
2302
  * @param  hdma: DMA handle
2303
  * @retval None
2304
  */
2305
static void SD_DMARxAbort(DMA_HandleTypeDef *hdma)  
2306
{
2307
  SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2308
  HAL_SD_CardStateTypeDef CardState;
2309
 
2310
  if(hsd->hdmarx != NULL)
2311
  {
2312
    hsd->hdmarx = NULL;
2313
  }
2314
 
2315
  /* All DMA channels are aborted */
2316
  if(hsd->hdmatx == NULL)
2317
  {
2318
    CardState = HAL_SD_GetCardState(hsd);
2319
    hsd->ErrorCode = HAL_SD_ERROR_NONE;
2320
    hsd->State = HAL_SD_STATE_READY;
2321
    if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2322
    {
2323
      hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2324
 
2325
      if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2326
      {
2327
        HAL_SD_AbortCallback(hsd);
2328
      }
2329
      else
2330
      {
2331
        HAL_SD_ErrorCallback(hsd);
2332
      }
2333
    }
2334
  }
2335
}
2336
 
2337
 
2338
/**
2339
  * @brief  Initializes the sd card.
2340
  * @param  hsd: Pointer to SD handle
2341
  * @retval SD Card error state
2342
  */
2343
static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
2344
{
2345
  HAL_SD_CardCSDTypeDef CSD;
2346
  uint32_t errorstate = HAL_SD_ERROR_NONE;
2347
  uint16_t sd_rca = 1U;
2348
 
2349
  /* Check the power State */
2350
  if(SDIO_GetPowerState(hsd->Instance) == 0U)
2351
  {
2352
    /* Power off */
2353
    return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
2354
  }
2355
 
2356
  if(hsd->SdCard.CardType != CARD_SECURED)
2357
  {
2358
    /* Send CMD2 ALL_SEND_CID */
2359
    errorstate = SDMMC_CmdSendCID(hsd->Instance);
2360
    if(errorstate != HAL_SD_ERROR_NONE)
2361
    {
2362
      return errorstate;
2363
    }
2364
    else
2365
    {
2366
      /* Get Card identification number data */
2367
      hsd->CID[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2368
      hsd->CID[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2);
2369
      hsd->CID[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3);
2370
      hsd->CID[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4);
2371
    }
2372
  }
2373
 
2374
  if(hsd->SdCard.CardType != CARD_SECURED)
2375
  {
2376
    /* Send CMD3 SET_REL_ADDR with argument 0 */
2377
    /* SD Card publishes its RCA. */
2378
    errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
2379
    if(errorstate != HAL_SD_ERROR_NONE)
2380
    {
2381
      return errorstate;
2382
    }
2383
  }
2384
  if(hsd->SdCard.CardType != CARD_SECURED)
2385
  {
2386
    /* Get the SD card RCA */
2387
    hsd->SdCard.RelCardAdd = sd_rca;
2388
 
2389
    /* Send CMD9 SEND_CSD with argument as card's RCA */
2390
    errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2391
    if(errorstate != HAL_SD_ERROR_NONE)
2392
    {
2393
      return errorstate;
2394
    }
2395
    else
2396
    {
2397
      /* Get Card Specific Data */
2398
      hsd->CSD[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2399
      hsd->CSD[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2);
2400
      hsd->CSD[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3);
2401
      hsd->CSD[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4);
2402
    }
2403
  }
2404
 
2405
  /* Get the Card Class */
2406
  hsd->SdCard.Class = (SDIO_GetResponse(hsd->Instance, SDIO_RESP2) >> 20U);
2407
 
2408
  /* Get CSD parameters */
2409
  HAL_SD_GetCardCSD(hsd, &CSD);
2410
 
2411
  /* Select the Card */
2412
  errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U));
2413
  if(errorstate != HAL_SD_ERROR_NONE)
2414
  {
2415
    return errorstate;
2416
  }
2417
 
2418
  /* Configure SDIO peripheral interface */    
2419
  SDIO_Init(hsd->Instance, hsd->Init);
2420
 
2421
  /* All cards are initialized */
2422
  return HAL_SD_ERROR_NONE;
2423
}
2424
 
2425
/**
2426
  * @brief  Enquires cards about their operating voltage and configures clock
2427
  *         controls and stores SD information that will be needed in future
2428
  *         in the SD handle.
2429
  * @param  hsd: Pointer to SD handle
2430
  * @retval error state
2431
  */
2432
static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
2433
{
2434
  __IO uint32_t count = 0U;
2435
  uint32_t response = 0U, validvoltage = 0U;
2436
  uint32_t errorstate = HAL_SD_ERROR_NONE;
2437
 
2438
  /* CMD0: GO_IDLE_STATE */
2439
  errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2440
  if(errorstate != HAL_SD_ERROR_NONE)
2441
  {
2442
    return errorstate;
2443
  }
2444
 
2445
  /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
2446
  errorstate = SDMMC_CmdOperCond(hsd->Instance);
2447
  if(errorstate != HAL_SD_ERROR_NONE)
2448
  {
2449
    hsd->SdCard.CardVersion = CARD_V1_X;
2450
 
2451
    /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2452
    while(validvoltage == 0U)
2453
    {
2454
      if(count++ == SDMMC_MAX_VOLT_TRIAL)
2455
      {
2456
        return HAL_SD_ERROR_INVALID_VOLTRANGE;
2457
      }
2458
 
2459
      /* SEND CMD55 APP_CMD with RCA as 0 */
2460
      errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0U);
2461
      if(errorstate != HAL_SD_ERROR_NONE)
2462
      {
2463
        return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2464
      }
2465
 
2466
      /* Send CMD41 */
2467
      errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_STD_CAPACITY);
2468
      if(errorstate != HAL_SD_ERROR_NONE)
2469
      {
2470
        return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2471
      }
2472
 
2473
      /* Get command response */
2474
      response = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2475
 
2476
      /* Get operating voltage*/
2477
      validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
2478
    }
2479
    /* Card type is SDSC */
2480
    hsd->SdCard.CardType = CARD_SDSC;
2481
  }
2482
  else
2483
  {
2484
    hsd->SdCard.CardVersion = CARD_V2_X;
2485
 
2486
    /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2487
    while(validvoltage == 0U)
2488
    {
2489
      if(count++ == SDMMC_MAX_VOLT_TRIAL)
2490
      {
2491
        return HAL_SD_ERROR_INVALID_VOLTRANGE;
2492
      }
2493
 
2494
      /* SEND CMD55 APP_CMD with RCA as 0 */
2495
      errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0U);
2496
      if(errorstate != HAL_SD_ERROR_NONE)
2497
      {
2498
        return errorstate;
2499
      }
2500
 
2501
      /* Send CMD41 */
2502
      errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_HIGH_CAPACITY);
2503
      if(errorstate != HAL_SD_ERROR_NONE)
2504
      {
2505
        return errorstate;
2506
      }
2507
 
2508
      /* Get command response */
2509
      response = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2510
 
2511
      /* Get operating voltage*/
2512
      validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
2513
    }
2514
 
2515
    if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
2516
    {
2517
      hsd->SdCard.CardType = CARD_SDHC_SDXC;
2518
    }
2519
    else
2520
    {
2521
      hsd->SdCard.CardType = CARD_SDSC;
2522
    }
2523
  }
2524
 
2525
  return HAL_SD_ERROR_NONE;
2526
}
2527
 
2528
/**
2529
  * @brief  Turns the SDIO output signals off.
2530
  * @param  hsd: Pointer to SD handle
2531
  * @retval HAL status
2532
  */
2533
static HAL_StatusTypeDef SD_PowerOFF(SD_HandleTypeDef *hsd)
2534
{
2535
  /* Set Power State to OFF */
2536
  SDIO_PowerState_OFF(hsd->Instance);
2537
 
2538
  return HAL_OK;
2539
}
2540
 
2541
/**
2542
  * @brief  Send Status info command.
2543
  * @param  hsd: pointer to SD handle
2544
  * @param  pSDstatus: Pointer to the buffer that will contain the SD card status
2545
  *         SD Status register)
2546
  * @retval error state
2547
  */
2548
static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
2549
{
2550
  SDIO_DataInitTypeDef config;
2551
  uint32_t errorstate = HAL_SD_ERROR_NONE;
2552
  uint32_t tickstart = HAL_GetTick();
2553
  uint32_t count = 0U;
2554
 
2555
  /* Check SD response */
2556
  if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
2557
  {
2558
    return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
2559
  }
2560
 
2561
  /* Set block size for card if it is not equal to current block size for card */
2562
  errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
2563
  if(errorstate != HAL_SD_ERROR_NONE)
2564
  {
2565
    hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2566
    return errorstate;
2567
  }
2568
 
2569
  /* Send CMD55 */
2570
  errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2571
  if(errorstate != HAL_SD_ERROR_NONE)
2572
  {
2573
    hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2574
    return errorstate;
2575
  }
2576
 
2577
  /* Configure the SD DPSM (Data Path State Machine) */
2578
  config.DataTimeOut   = SDMMC_DATATIMEOUT;
2579
  config.DataLength    = 64U;
2580
  config.DataBlockSize = SDIO_DATABLOCK_SIZE_64B;
2581
  config.TransferDir   = SDIO_TRANSFER_DIR_TO_SDIO;
2582
  config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
2583
  config.DPSM          = SDIO_DPSM_ENABLE;
2584
  SDIO_ConfigData(hsd->Instance, &config);
2585
 
2586
  /* Send ACMD13 (SD_APP_STAUS)  with argument as card's RCA */
2587
  errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
2588
  if(errorstate != HAL_SD_ERROR_NONE)
2589
  {
2590
    hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2591
    return errorstate;
2592
  }
2593
 
2594
  /* Get status data */
2595
  while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND))
2596
  {
2597
    if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
2598
    {
2599
      for(count = 0U; count < 8U; count++)
2600
      {
2601
        *(pSDstatus + count) = SDIO_ReadFIFO(hsd->Instance);
2602
      }
2603
 
2604
      pSDstatus += 8U;
2605
    }
2606
 
2607
    if((HAL_GetTick() - tickstart) >=  SDMMC_DATATIMEOUT)
2608
    {
2609
      return HAL_SD_ERROR_TIMEOUT;
2610
    }
2611
  }
2612
 
2613
  if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
2614
  {
2615
    return HAL_SD_ERROR_DATA_TIMEOUT;
2616
  }
2617
  else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
2618
  {
2619
    return HAL_SD_ERROR_DATA_CRC_FAIL;
2620
  }
2621
  else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
2622
  {
2623
    return HAL_SD_ERROR_RX_OVERRUN;
2624
  }
2625
 
2626
  while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)))
2627
  {
2628
    *pSDstatus = SDIO_ReadFIFO(hsd->Instance);
2629
    pSDstatus++;
2630
 
2631
    if((HAL_GetTick() - tickstart) >=  SDMMC_DATATIMEOUT)
2632
    {
2633
      return HAL_SD_ERROR_TIMEOUT;
2634
    }
2635
  }
2636
 
2637
  /* Clear all the static status flags*/
2638
  __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2639
 
2640
  return HAL_SD_ERROR_NONE;
2641
}
2642
 
2643
/**
2644
  * @brief  Returns the current card's status.
2645
  * @param  hsd: Pointer to SD handle
2646
  * @param  pCardStatus: pointer to the buffer that will contain the SD card
2647
  *         status (Card Status register)  
2648
  * @retval error state
2649
  */
2650
static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
2651
{
2652
  uint32_t errorstate = HAL_SD_ERROR_NONE;
2653
 
2654
  if(pCardStatus == NULL)
2655
  {
2656
    return HAL_SD_ERROR_PARAM;
2657
  }
2658
 
2659
  /* Send Status command */
2660
  errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2661
  if(errorstate != HAL_OK)
2662
  {
2663
    return errorstate;
2664
  }
2665
 
2666
  /* Get SD card status */
2667
  *pCardStatus = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2668
 
2669
  return HAL_SD_ERROR_NONE;
2670
}
2671
 
2672
/**
2673
  * @brief  Enables the SDIO wide bus mode.
2674
  * @param  hsd: pointer to SD handle
2675
  * @retval error state
2676
  */
2677
static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd)
2678
{
2679
  uint32_t scr[2U] = {0U, 0U};
2680
  uint32_t errorstate = HAL_SD_ERROR_NONE;
2681
 
2682
  if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
2683
  {
2684
    return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
2685
  }
2686
 
2687
  /* Get SCR Register */
2688
  errorstate = SD_FindSCR(hsd, scr);
2689
  if(errorstate != HAL_OK)
2690
  {
2691
    return errorstate;
2692
  }
2693
 
2694
  /* If requested card supports wide bus operation */
2695
  if((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
2696
  {
2697
    /* Send CMD55 APP_CMD with argument as card's RCA.*/
2698
    errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2699
    if(errorstate != HAL_OK)
2700
    {
2701
      return errorstate;
2702
    }
2703
 
2704
    /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
2705
    errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U);
2706
    if(errorstate != HAL_OK)
2707
    {
2708
      return errorstate;
2709
    }
2710
 
2711
    return HAL_SD_ERROR_NONE;
2712
  }
2713
  else
2714
  {
2715
    return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
2716
  }
2717
}
2718
 
2719
/**
2720
  * @brief  Disables the SDIO wide bus mode.
2721
  * @param  hsd: Pointer to SD handle
2722
  * @retval error state
2723
  */
2724
static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd)
2725
{
2726
  uint32_t scr[2U] = {0U, 0U};
2727
  uint32_t errorstate = HAL_SD_ERROR_NONE;
2728
 
2729
  if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
2730
  {
2731
    return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
2732
  }
2733
 
2734
  /* Get SCR Register */
2735
  errorstate = SD_FindSCR(hsd, scr);
2736
  if(errorstate != HAL_OK)
2737
  {
2738
    return errorstate;
2739
  }
2740
 
2741
  /* If requested card supports 1 bit mode operation */
2742
  if((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO)
2743
  {
2744
    /* Send CMD55 APP_CMD with argument as card's RCA */
2745
    errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2746
    if(errorstate != HAL_OK)
2747
    {
2748
      return errorstate;
2749
    }
2750
 
2751
    /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
2752
    errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U);
2753
    if(errorstate != HAL_OK)
2754
    {
2755
      return errorstate;
2756
    }
2757
 
2758
    return HAL_SD_ERROR_NONE;
2759
  }
2760
  else
2761
  {
2762
    return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
2763
  }
2764
}
2765
 
2766
 
2767
/**
2768
  * @brief  Finds the SD card SCR register value.
2769
  * @param  hsd: Pointer to SD handle
2770
  * @param  pSCR: pointer to the buffer that will contain the SCR value  
2771
  * @retval error state
2772
  */
2773
static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
2774
{
2775
  SDIO_DataInitTypeDef config;
2776
  uint32_t errorstate = HAL_SD_ERROR_NONE;
2777
  uint32_t tickstart = HAL_GetTick();
2778
  uint32_t index = 0U;
2779
  uint32_t tempscr[2U] = {0U, 0U};
2780
 
2781
  /* Set Block Size To 8 Bytes */
2782
  errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U);
2783
  if(errorstate != HAL_OK)
2784
  {
2785
    return errorstate;
2786
  }
2787
 
2788
  /* Send CMD55 APP_CMD with argument as card's RCA */
2789
  errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16U));
2790
  if(errorstate != HAL_OK)
2791
  {
2792
    return errorstate;
2793
  }
2794
 
2795
  config.DataTimeOut   = SDMMC_DATATIMEOUT;
2796
  config.DataLength    = 8U;
2797
  config.DataBlockSize = SDIO_DATABLOCK_SIZE_8B;
2798
  config.TransferDir   = SDIO_TRANSFER_DIR_TO_SDIO;
2799
  config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
2800
  config.DPSM          = SDIO_DPSM_ENABLE;
2801
  SDIO_ConfigData(hsd->Instance, &config);
2802
 
2803
  /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
2804
  errorstate = SDMMC_CmdSendSCR(hsd->Instance);
2805
  if(errorstate != HAL_OK)
2806
  {
2807
    return errorstate;
2808
  }
2809
 
2810
  while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND))
2811
  {
2812
    if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL))
2813
    {
2814
      *(tempscr + index) = SDIO_ReadFIFO(hsd->Instance);
2815
      index++;
2816
    }
2817
 
2818
    if((HAL_GetTick() - tickstart) >=  SDMMC_DATATIMEOUT)
2819
    {
2820
      return HAL_SD_ERROR_TIMEOUT;
2821
    }
2822
  }
2823
 
2824
  if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
2825
  {
2826
    __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
2827
 
2828
    return HAL_SD_ERROR_DATA_TIMEOUT;
2829
  }
2830
  else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
2831
  {
2832
    __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
2833
 
2834
    return HAL_SD_ERROR_DATA_CRC_FAIL;
2835
  }
2836
  else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
2837
  {
2838
    __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
2839
 
2840
    return HAL_SD_ERROR_RX_OVERRUN;
2841
  }
2842
  else
2843
  {
2844
    /* No error flag set */
2845
    /* Clear all the static flags */
2846
    __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2847
 
2848
    *(pSCR + 1U) = ((tempscr[0U] & SDMMC_0TO7BITS) << 24U)  | ((tempscr[0U] & SDMMC_8TO15BITS) << 8U) |\
2849
      ((tempscr[0U] & SDMMC_16TO23BITS) >> 8U) | ((tempscr[0U] & SDMMC_24TO31BITS) >> 24U);
2850
 
2851
    *(pSCR) = ((tempscr[1U] & SDMMC_0TO7BITS) << 24U)  | ((tempscr[1U] & SDMMC_8TO15BITS) << 8U) |\
2852
      ((tempscr[1U] & SDMMC_16TO23BITS) >> 8U) | ((tempscr[1U] & SDMMC_24TO31BITS) >> 24U);
2853
  }
2854
 
2855
  return HAL_SD_ERROR_NONE;
2856
}
2857
 
2858
/**
2859
  * @brief  Wrap up reading in non-blocking mode.
2860
  * @param  hsd: pointer to a SD_HandleTypeDef structure that contains
2861
  *              the configuration information.
2862
  * @retval HAL status
2863
  */
2864
static HAL_StatusTypeDef SD_Read_IT(SD_HandleTypeDef *hsd)
2865
{
2866
  uint32_t count = 0U;
2867
  uint32_t* tmp;
2868
 
2869
  tmp = (uint32_t*)hsd->pRxBuffPtr;
2870
 
2871
  /* Read data from SDIO Rx FIFO */
2872
  for(count = 0U; count < 8U; count++)
2873
  {
2874
    *(tmp + count) = SDIO_ReadFIFO(hsd->Instance);
2875
  }
2876
 
2877
  hsd->pRxBuffPtr += 8U;
2878
 
2879
  return HAL_OK;
2880
}
2881
 
2882
/**
2883
  * @brief  Wrap up writing in non-blocking mode.
2884
  * @param  hsd: pointer to a SD_HandleTypeDef structure that contains
2885
  *              the configuration information.
2886
  * @retval HAL status
2887
  */
2888
static HAL_StatusTypeDef SD_Write_IT(SD_HandleTypeDef *hsd)
2889
{
2890
  uint32_t count = 0U;
2891
  uint32_t* tmp;
2892
 
2893
  tmp = (uint32_t*)hsd->pTxBuffPtr;
2894
 
2895
  /* Write data to SDIO Tx FIFO */
2896
  for(count = 0U; count < 8U; count++)
2897
  {
2898
    SDIO_WriteFIFO(hsd->Instance, (tmp + count));
2899
  }
2900
 
2901
  hsd->pTxBuffPtr += 8U;
2902
 
2903
  return HAL_OK;
2904
}
2905
 
2906
/**
2907
  * @}
2908
  */
2909
 
2910
#endif /* STM32F103xE || STM32F103xG */
2911
 
2912
#endif /* HAL_SD_MODULE_ENABLED */
2913
 
2914
/**
2915
  * @}
2916
  */
2917
 
2918
/**
2919
  * @}
2920
  */
2921
 
2922
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/