Subversion Repositories LedShow

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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