Subversion Repositories canSerial

Rev

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

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