|
@@ -27,248 +27,263 @@
|
27
|
27
|
#include <stdint.h>
|
28
|
28
|
#include <stdbool.h>
|
29
|
29
|
|
30
|
|
-//#include "SdMscDriver.h"
|
31
|
|
-
|
32
|
|
-//#include "usbd_msc_bot.h"
|
33
|
|
-//#include "usbd_msc_scsi.h"
|
34
|
|
-//#include "usbd_msc_composite.h"
|
35
|
|
-//#include "usbd_msc_cdc_composite.h"
|
36
|
|
-
|
37
|
|
-//#include "usbd_msc_data.h"
|
38
|
|
-
|
39
|
|
-#if defined(STM32F103xE) || defined(STM32F103xG)
|
40
|
|
- #include <stm32f1xx_hal_rcc_ex.h>
|
41
|
|
- #include <stm32f1xx_hal_sd.h>
|
42
|
|
-#elif defined(STM32F4xx)
|
43
|
|
- #include <stm32f4xx_hal_rcc.h>
|
44
|
|
- #include <stm32f4xx_hal_dma.h>
|
45
|
|
- #include <stm32f4xx_hal_gpio.h>
|
46
|
|
- #include <stm32f4xx_hal_sd.h>
|
47
|
|
-#elif defined(STM32F7xx)
|
48
|
|
- #include <stm32f7xx_hal_rcc.h>
|
49
|
|
- #include <stm32f7xx_hal_dma.h>
|
50
|
|
- #include <stm32f7xx_hal_gpio.h>
|
51
|
|
- #include <stm32f7xx_hal_sd.h>
|
52
|
|
-#else
|
|
30
|
+#if NONE(STM32F103xE, STM32F103xG, STM32F4xx, STM32F7xx)
|
53
|
31
|
#error "ERROR - Only STM32F103xE, STM32F103xG, STM32F4xx or STM32F7xx CPUs supported"
|
54
|
32
|
#endif
|
55
|
33
|
|
56
|
|
-SD_HandleTypeDef hsd; // create SDIO structure
|
|
34
|
+#ifdef USBD_USE_CDC_COMPOSITE
|
57
|
35
|
|
58
|
|
-#define TRANSFER_CLOCK_DIV ((uint8_t)SDIO_INIT_CLK_DIV/40)
|
|
36
|
+ // use USB drivers
|
59
|
37
|
|
60
|
|
-#ifndef USBD_OK
|
61
|
|
- #define USBD_OK 0
|
62
|
|
-#endif
|
|
38
|
+ extern "C" { int8_t SD_MSC_Read(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
|
|
39
|
+ int8_t SD_MSC_Write(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
|
|
40
|
+ extern SD_HandleTypeDef hsd;
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ bool SDIO_Init() {
|
|
44
|
+ if (hsd.State == HAL_SD_STATE_READY) return 1; // return passing status
|
|
45
|
+ return 0; // return failing status
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ bool SDIO_ReadBlock(uint32_t block, uint8_t *src) {
|
|
49
|
+ int8_t status = SD_MSC_Read(0, (uint8_t*)src, block, 1); // read one 512 byte block
|
|
50
|
+ return (bool) status;
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) {
|
|
54
|
+ int8_t status = SD_MSC_Write(0, (uint8_t*)src, block, 1); // write one 512 byte block
|
|
55
|
+ return (bool) status;
|
|
56
|
+ }
|
63
|
57
|
|
64
|
|
-void go_to_transfer_speed() {
|
|
58
|
+#else // !USBD_USE_CDC_COMPOSITE
|
|
59
|
+
|
|
60
|
+ // use local drivers
|
|
61
|
+
|
|
62
|
+ #if defined(STM32F103xE) || defined(STM32F103xG)
|
|
63
|
+ #include <stm32f1xx_hal_rcc_ex.h>
|
|
64
|
+ #include <stm32f1xx_hal_sd.h>
|
|
65
|
+ #elif defined(STM32F4xx)
|
|
66
|
+ #include <stm32f4xx_hal_rcc.h>
|
|
67
|
+ #include <stm32f4xx_hal_dma.h>
|
|
68
|
+ #include <stm32f4xx_hal_gpio.h>
|
|
69
|
+ #include <stm32f4xx_hal_sd.h>
|
|
70
|
+ #elif defined(STM32F7xx)
|
|
71
|
+ #include <stm32f7xx_hal_rcc.h>
|
|
72
|
+ #include <stm32f7xx_hal_dma.h>
|
|
73
|
+ #include <stm32f7xx_hal_gpio.h>
|
|
74
|
+ #include <stm32f7xx_hal_sd.h>
|
|
75
|
+ #else
|
|
76
|
+ #error "ERROR - Only STM32F103xE, STM32F103xG, STM32F4xx or STM32F7xx CPUs supported"
|
|
77
|
+ #endif
|
65
|
78
|
|
66
|
|
- SD_InitTypeDef Init;
|
|
79
|
+ SD_HandleTypeDef hsd; // create SDIO structure
|
67
|
80
|
|
68
|
|
- /* Default SDIO peripheral configuration for SD card initialization */
|
69
|
|
- Init.ClockEdge = hsd.Init.ClockEdge;
|
70
|
|
- Init.ClockBypass = hsd.Init.ClockBypass;
|
71
|
|
- Init.ClockPowerSave = hsd.Init.ClockPowerSave;
|
72
|
|
- Init.BusWide = hsd.Init.BusWide;
|
73
|
|
- Init.HardwareFlowControl = hsd.Init.HardwareFlowControl;
|
74
|
|
- Init.ClockDiv = TRANSFER_CLOCK_DIV;
|
|
81
|
+ #define TRANSFER_CLOCK_DIV (uint8_t(SDIO_INIT_CLK_DIV) / 40)
|
75
|
82
|
|
76
|
|
- /* Initialize SDIO peripheral interface with default configuration */
|
77
|
|
- SDIO_Init(hsd.Instance, Init);
|
78
|
|
-}
|
|
83
|
+ #ifndef USBD_OK
|
|
84
|
+ #define USBD_OK 0
|
|
85
|
+ #endif
|
79
|
86
|
|
80
|
|
-void SD_LowLevel_Init(void) {
|
|
87
|
+ void go_to_transfer_speed() {
|
|
88
|
+ SD_InitTypeDef Init;
|
81
|
89
|
|
82
|
|
- uint32_t tempreg;
|
|
90
|
+ /* Default SDIO peripheral configuration for SD card initialization */
|
|
91
|
+ Init.ClockEdge = hsd.Init.ClockEdge;
|
|
92
|
+ Init.ClockBypass = hsd.Init.ClockBypass;
|
|
93
|
+ Init.ClockPowerSave = hsd.Init.ClockPowerSave;
|
|
94
|
+ Init.BusWide = hsd.Init.BusWide;
|
|
95
|
+ Init.HardwareFlowControl = hsd.Init.HardwareFlowControl;
|
|
96
|
+ Init.ClockDiv = TRANSFER_CLOCK_DIV;
|
83
|
97
|
|
84
|
|
- GPIO_InitTypeDef GPIO_InitStruct;
|
|
98
|
+ /* Initialize SDIO peripheral interface with default configuration */
|
|
99
|
+ SDIO_Init(hsd.Instance, Init);
|
|
100
|
+ }
|
85
|
101
|
|
86
|
|
- __HAL_RCC_GPIOC_CLK_ENABLE(); //enable GPIO clocks
|
87
|
|
- __HAL_RCC_GPIOD_CLK_ENABLE(); //enable GPIO clocks
|
|
102
|
+ void SD_LowLevel_Init(void) {
|
|
103
|
+ uint32_t tempreg;
|
88
|
104
|
|
89
|
|
- GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_12; // D0 & SCK
|
90
|
|
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
91
|
|
- GPIO_InitStruct.Pull = 1; //GPIO_NOPULL;
|
92
|
|
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
93
|
|
- GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
|
94
|
|
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
105
|
+ GPIO_InitTypeDef GPIO_InitStruct;
|
95
|
106
|
|
96
|
|
- #if defined(SDIO_D1_PIN) && defined(SDIO_D2_PIN) && defined(SDIO_D3_PIN) // define D1-D3 only if have a four bit wide SDIO bus
|
97
|
|
- GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11; // D1-D3
|
|
107
|
+ __HAL_RCC_GPIOC_CLK_ENABLE(); //enable GPIO clocks
|
|
108
|
+ __HAL_RCC_GPIOD_CLK_ENABLE(); //enable GPIO clocks
|
|
109
|
+
|
|
110
|
+ GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_12; // D0 & SCK
|
98
|
111
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
99
|
112
|
GPIO_InitStruct.Pull = 1; //GPIO_NOPULL;
|
100
|
113
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
101
|
114
|
GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
|
102
|
115
|
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
103
|
|
- #endif
|
104
|
116
|
|
105
|
|
- // Configure PD.02 CMD line
|
106
|
|
- GPIO_InitStruct.Pin = GPIO_PIN_2;
|
107
|
|
- HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
|
117
|
+ #if PINS_EXIST(SDIO_D1, SDIO_D2, SDIO_D3) // define D1-D3 only if have a four bit wide SDIO bus
|
|
118
|
+ GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11; // D1-D3
|
|
119
|
+ GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
120
|
+ GPIO_InitStruct.Pull = 1; // GPIO_NOPULL;
|
|
121
|
+ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
122
|
+ GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
|
|
123
|
+ HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
124
|
+ #endif
|
|
125
|
+
|
|
126
|
+ // Configure PD.02 CMD line
|
|
127
|
+ GPIO_InitStruct.Pin = GPIO_PIN_2;
|
|
128
|
+ HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
|
129
|
+
|
|
130
|
+ RCC->APB2RSTR &= ~RCC_APB2RSTR_SDIORST_Msk; // take SDIO out of reset
|
|
131
|
+ RCC->APB2ENR |= RCC_APB2RSTR_SDIORST_Msk; // enable SDIO clock
|
|
132
|
+
|
|
133
|
+ // Enable the DMA2 Clock
|
|
134
|
+
|
|
135
|
+ //Initialize the SDIO (with initial <400Khz Clock)
|
|
136
|
+ tempreg = 0; //Reset value
|
|
137
|
+ tempreg |= SDIO_CLKCR_CLKEN; // Clock enabled
|
|
138
|
+ tempreg |= (uint32_t)0x76; // Clock Divider. Clock = 48000 / (118 + 2) = 400Khz
|
|
139
|
+ // Keep the rest at 0 => HW_Flow Disabled, Rising Clock Edge, Disable CLK ByPass, Bus Width = 0, Power save Disable
|
|
140
|
+ SDIO->CLKCR = tempreg;
|
|
141
|
+
|
|
142
|
+ // Power up the SDIO
|
|
143
|
+ SDIO->POWER = 0x03;
|
|
144
|
+ }
|
108
|
145
|
|
109
|
|
- RCC->APB2RSTR &= ~RCC_APB2RSTR_SDIORST_Msk; // take SDIO out of reset
|
110
|
|
- RCC->APB2ENR |= RCC_APB2RSTR_SDIORST_Msk; // enable SDIO clock
|
|
146
|
+ void HAL_SD_MspInit(SD_HandleTypeDef *hsd) { // application specific init
|
|
147
|
+ UNUSED(hsd); /* Prevent unused argument(s) compilation warning */
|
|
148
|
+ __HAL_RCC_SDIO_CLK_ENABLE(); // turn on SDIO clock
|
|
149
|
+ }
|
111
|
150
|
|
112
|
|
- // Enable the DMA2 Clock
|
|
151
|
+ constexpr uint8_t SD_RETRY_COUNT = 1 + 2 * ENABLED(SD_CHECK_AND_RETRY);
|
113
|
152
|
|
114
|
|
- //Initialize the SDIO (with initial <400Khz Clock)
|
115
|
|
- tempreg = 0; //Reset value
|
116
|
|
- tempreg |= SDIO_CLKCR_CLKEN; //Clock is enabled
|
117
|
|
- tempreg |= (uint32_t)0x76; //Clock Divider. Clock = 48000/(118+2) = 400Khz
|
118
|
|
- //Keep the rest at 0 => HW_Flow Disabled, Rising Clock Edge, Disable CLK ByPass, Bus Width = 0, Power save Disable
|
119
|
|
- SDIO->CLKCR = tempreg;
|
|
153
|
+ bool SDIO_Init() {
|
|
154
|
+ //init SDIO and get SD card info
|
120
|
155
|
|
121
|
|
- //Power up the SDIO
|
122
|
|
- SDIO->POWER = 0x03;
|
123
|
|
-}
|
|
156
|
+ uint8_t retryCnt = SD_RETRY_COUNT;
|
124
|
157
|
|
|
158
|
+ bool status;
|
|
159
|
+ hsd.Instance = SDIO;
|
|
160
|
+ hsd.State = (HAL_SD_StateTypeDef) 0; // HAL_SD_STATE_RESET
|
|
161
|
+ SD_LowLevel_Init();
|
125
|
162
|
|
126
|
|
-void HAL_SD_MspInit(SD_HandleTypeDef *hsd) { // application specific init
|
127
|
|
- UNUSED(hsd); /* Prevent unused argument(s) compilation warning */
|
128
|
|
- __HAL_RCC_SDIO_CLK_ENABLE(); // turn on SDIO clock
|
129
|
|
-}
|
|
163
|
+ uint8_t retry_Cnt = retryCnt;
|
|
164
|
+ for (;;) {
|
|
165
|
+ status = (bool) HAL_SD_Init(&hsd);
|
|
166
|
+ if (!status) break;
|
|
167
|
+ if (!--retry_Cnt) return false; // return failing status if retries are exhausted
|
|
168
|
+ }
|
130
|
169
|
|
131
|
|
-constexpr uint8_t SD_RETRY_COUNT = (1
|
132
|
|
- #if ENABLED(SD_CHECK_AND_RETRY)
|
133
|
|
- + 2
|
134
|
|
- #endif
|
135
|
|
-);
|
|
170
|
+ go_to_transfer_speed();
|
136
|
171
|
|
137
|
|
-bool SDIO_Init() {
|
138
|
|
- //init SDIO and get SD card info
|
|
172
|
+ #if PINS_EXIST(SDIO_D1, SDIO_D2, SDIO_D3) // go to 4 bit wide mode if pins are defined
|
|
173
|
+ retry_Cnt = retryCnt;
|
|
174
|
+ for (;;) {
|
|
175
|
+ if (!HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B)) break; // some cards are only 1 bit wide so a pass here is not required
|
|
176
|
+ if (!--retry_Cnt) break;
|
|
177
|
+ }
|
|
178
|
+ if (!retry_Cnt) { // wide bus failed, go back to one bit wide mode
|
|
179
|
+ hsd.State = (HAL_SD_StateTypeDef) 0; // HAL_SD_STATE_RESET
|
|
180
|
+ SD_LowLevel_Init();
|
|
181
|
+ retry_Cnt = retryCnt;
|
|
182
|
+ for (;;) {
|
|
183
|
+ status = (bool) HAL_SD_Init(&hsd);
|
|
184
|
+ if (!status) break;
|
|
185
|
+ if (!--retry_Cnt) return false; // return failing status if retries are exhausted
|
|
186
|
+ }
|
|
187
|
+ }
|
|
188
|
+ #endif
|
|
189
|
+
|
|
190
|
+ return true;
|
|
191
|
+ }
|
139
|
192
|
|
140
|
|
- uint8_t retryCnt = SD_RETRY_COUNT;
|
|
193
|
+ void init_SDIO_pins(void) {
|
|
194
|
+ GPIO_InitTypeDef GPIO_InitStruct = {0};
|
141
|
195
|
|
142
|
|
- bool status;
|
143
|
|
- hsd.Instance = SDIO;
|
144
|
|
- hsd.State = (HAL_SD_StateTypeDef) 0; // HAL_SD_STATE_RESET
|
145
|
|
- SD_LowLevel_Init();
|
|
196
|
+ /**SDIO GPIO Configuration
|
|
197
|
+ PC8 ------> SDIO_D0
|
|
198
|
+ PC12 ------> SDIO_CK
|
|
199
|
+ PD2 ------> SDIO_CMD
|
|
200
|
+ */
|
|
201
|
+ GPIO_InitStruct.Pin = GPIO_PIN_8;
|
|
202
|
+ GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
203
|
+ GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
204
|
+ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
|
205
|
+ GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
|
|
206
|
+ HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
146
|
207
|
|
147
|
|
- uint8_t retry_Cnt = retryCnt;
|
148
|
|
- for (;;) {
|
149
|
|
- status = (bool) HAL_SD_Init(&hsd);
|
150
|
|
- if (!status) break;
|
151
|
|
- if (!--retry_Cnt) return false; // return failing status if retries are exhausted
|
|
208
|
+ GPIO_InitStruct.Pin = GPIO_PIN_12;
|
|
209
|
+ GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
210
|
+ GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
211
|
+ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
|
212
|
+ GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
|
|
213
|
+ HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
214
|
+
|
|
215
|
+ GPIO_InitStruct.Pin = GPIO_PIN_2;
|
|
216
|
+ GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
217
|
+ GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
218
|
+ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
|
219
|
+ GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
|
|
220
|
+ HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
152
|
221
|
}
|
153
|
222
|
|
154
|
|
- go_to_transfer_speed();
|
|
223
|
+ //bool SDIO_init() { return (bool) (SD_SDIO_Init() ? 1 : 0);}
|
|
224
|
+ //bool SDIO_Init_C() { return (bool) (SD_SDIO_Init() ? 1 : 0);}
|
|
225
|
+
|
|
226
|
+ bool SDIO_ReadBlock(uint32_t block, uint8_t *dst) {
|
|
227
|
+ hsd.Instance = SDIO;
|
|
228
|
+ uint8_t retryCnt = SD_RETRY_COUNT;
|
155
|
229
|
|
156
|
|
- #if defined(SDIO_D1_PIN) && defined(SDIO_D2_PIN) && defined(SDIO_D3_PIN) // go to 4 bit wide mode if pins are defined
|
157
|
|
- retry_Cnt = retryCnt;
|
|
230
|
+ bool status;
|
158
|
231
|
for (;;) {
|
159
|
|
- if (!HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B)) break; // some cards are only 1 bit wide so a pass here is not required
|
160
|
|
- if (!--retry_Cnt) break;
|
|
232
|
+ status = (bool) HAL_SD_ReadBlocks(&hsd, (uint8_t*)dst, block, 1, 1000); // read one 512 byte block with 500mS timeout
|
|
233
|
+ status |= (bool) HAL_SD_GetCardState(&hsd); // make sure all is OK
|
|
234
|
+ if (!status) break; // return passing status
|
|
235
|
+ if (!--retryCnt) break; // return failing status if retries are exhausted
|
161
|
236
|
}
|
162
|
|
- if (!retry_Cnt) { // wide bus failed, go back to one bit wide mode
|
163
|
|
- hsd.State = (HAL_SD_StateTypeDef) 0; // HAL_SD_STATE_RESET
|
164
|
|
- SD_LowLevel_Init();
|
165
|
|
- retry_Cnt = retryCnt;
|
166
|
|
- for (;;) {
|
167
|
|
- status = (bool) HAL_SD_Init(&hsd);
|
168
|
|
- if (!status) break;
|
169
|
|
- if (!--retry_Cnt) return false; // return failing status if retries are exhausted
|
170
|
|
- }
|
171
|
|
- }
|
172
|
|
- #endif
|
|
237
|
+ return status;
|
173
|
238
|
|
174
|
|
- return true;
|
175
|
|
-}
|
176
|
|
-
|
177
|
|
-void init_SDIO_pins(void) {
|
178
|
|
- GPIO_InitTypeDef GPIO_InitStruct = {0};
|
179
|
|
-
|
180
|
|
- /**SDIO GPIO Configuration
|
181
|
|
- PC8 ------> SDIO_D0
|
182
|
|
- PC12 ------> SDIO_CK
|
183
|
|
- PD2 ------> SDIO_CMD
|
184
|
|
- */
|
185
|
|
- GPIO_InitStruct.Pin = GPIO_PIN_8;
|
186
|
|
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
187
|
|
- GPIO_InitStruct.Pull = GPIO_NOPULL;
|
188
|
|
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
189
|
|
- GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
|
190
|
|
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
191
|
|
-
|
192
|
|
- GPIO_InitStruct.Pin = GPIO_PIN_12;
|
193
|
|
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
194
|
|
- GPIO_InitStruct.Pull = GPIO_NOPULL;
|
195
|
|
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
196
|
|
- GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
|
197
|
|
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
198
|
|
-
|
199
|
|
- GPIO_InitStruct.Pin = GPIO_PIN_2;
|
200
|
|
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
201
|
|
- GPIO_InitStruct.Pull = GPIO_NOPULL;
|
202
|
|
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
203
|
|
- GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
|
204
|
|
- HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
205
|
|
-}
|
206
|
|
-
|
207
|
|
-//bool SDIO_init() { return (bool) (SD_SDIO_Init() ? 1 : 0);}
|
208
|
|
-//bool SDIO_Init_C() { return (bool) (SD_SDIO_Init() ? 1 : 0);}
|
209
|
|
-
|
210
|
|
-bool SDIO_ReadBlock(uint32_t block, uint8_t *dst) {
|
211
|
|
- bool status;
|
212
|
|
-
|
213
|
|
- hsd.Instance = SDIO;
|
214
|
|
-
|
215
|
|
- uint8_t retryCnt = SD_RETRY_COUNT;
|
216
|
|
-
|
217
|
|
- for (;;) {
|
218
|
|
- bool status = (bool) HAL_SD_ReadBlocks(&hsd, (uint8_t*)dst, block, 1, 1000); // read one 512 byte block with 500mS timeout
|
219
|
|
- status |= (bool) HAL_SD_GetCardState(&hsd); // make sure all is OK
|
220
|
|
- if (!status) return false; // return passing status
|
221
|
|
- if (!--retryCnt) return true; // return failing status if retries are exhausted
|
222
|
|
- }
|
|
239
|
+ /*
|
|
240
|
+ return (bool) ((status_read | status_card) ? 1 : 0);
|
223
|
241
|
|
224
|
|
- /*
|
225
|
|
- return (bool) ((status_read | status_card) ? 1 : 0);
|
|
242
|
+ if (SDIO_GetCardState() != SDIO_CARD_TRANSFER) return false;
|
|
243
|
+ if (blockAddress >= SdCard.LogBlockNbr) return false;
|
|
244
|
+ if ((0x03 & (uint32_t)data)) return false; // misaligned data
|
226
|
245
|
|
227
|
|
- if (SDIO_GetCardState() != SDIO_CARD_TRANSFER) return false;
|
228
|
|
- if (blockAddress >= SdCard.LogBlockNbr) return false;
|
229
|
|
- if ((0x03 & (uint32_t)data)) return false; // misaligned data
|
|
246
|
+ if (SdCard.CardType != CARD_SDHC_SDXC) { blockAddress *= 512U; }
|
230
|
247
|
|
231
|
|
- if (SdCard.CardType != CARD_SDHC_SDXC) { blockAddress *= 512U; }
|
|
248
|
+ if (!SDIO_CmdReadSingleBlock(blockAddress)) {
|
|
249
|
+ SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS);
|
|
250
|
+ dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
|
|
251
|
+ return false;
|
|
252
|
+ }
|
232
|
253
|
|
233
|
|
- if (!SDIO_CmdReadSingleBlock(blockAddress)) {
|
234
|
|
- SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS);
|
235
|
|
- dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
|
236
|
|
- return false;
|
237
|
|
- }
|
|
254
|
+ while (!SDIO_GET_FLAG(SDIO_STA_DATAEND | SDIO_STA_TRX_ERROR_FLAGS)) {}
|
238
|
255
|
|
239
|
|
- while (!SDIO_GET_FLAG(SDIO_STA_DATAEND | SDIO_STA_TRX_ERROR_FLAGS)) {}
|
|
256
|
+ dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
|
240
|
257
|
|
241
|
|
- dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
|
|
258
|
+ if (SDIO->STA & SDIO_STA_RXDAVL) {
|
|
259
|
+ while (SDIO->STA & SDIO_STA_RXDAVL) (void)SDIO->FIFO;
|
|
260
|
+ SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
|
|
261
|
+ return false;
|
|
262
|
+ }
|
242
|
263
|
|
243
|
|
- if (SDIO->STA & SDIO_STA_RXDAVL) {
|
244
|
|
- while (SDIO->STA & SDIO_STA_RXDAVL) (void)SDIO->FIFO;
|
|
264
|
+ if (SDIO_GET_FLAG(SDIO_STA_TRX_ERROR_FLAGS)) {
|
|
265
|
+ SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
|
|
266
|
+ return false;
|
|
267
|
+ }
|
245
|
268
|
SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
|
246
|
|
- return false;
|
247
|
|
- }
|
|
269
|
+ */
|
248
|
270
|
|
249
|
|
- if (SDIO_GET_FLAG(SDIO_STA_TRX_ERROR_FLAGS)) {
|
250
|
|
- SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
|
251
|
|
- return false;
|
|
271
|
+ return true;
|
252
|
272
|
}
|
253
|
|
- SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
|
254
|
|
- */
|
255
|
|
-
|
256
|
|
- return true;
|
257
|
|
-}
|
258
|
|
-
|
259
|
|
-bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) {
|
260
|
|
- bool status;
|
261
|
273
|
|
262
|
|
- hsd.Instance = SDIO;
|
|
274
|
+ bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) {
|
|
275
|
+ hsd.Instance = SDIO;
|
|
276
|
+ uint8_t retryCnt = SD_RETRY_COUNT;
|
263
|
277
|
|
264
|
|
- uint8_t retryCnt = SD_RETRY_COUNT;
|
265
|
|
-
|
266
|
|
- for (;;) {
|
267
|
|
- status = (bool) HAL_SD_WriteBlocks(&hsd, (uint8_t*)src, block, 1, 500); // write one 512 byte block with 500mS timeout
|
268
|
|
- status |= (bool) HAL_SD_GetCardState(&hsd); // make sure all is OK
|
269
|
|
- if (!status) return (bool) status; // return passing status
|
270
|
|
- if (!--retryCnt) return (bool) status; // return failing status if retries are exhausted
|
|
278
|
+ bool status;
|
|
279
|
+ for (;;) {
|
|
280
|
+ status = (bool) HAL_SD_WriteBlocks(&hsd, (uint8_t*)src, block, 1, 500); // write one 512 byte block with 500mS timeout
|
|
281
|
+ status |= (bool) HAL_SD_GetCardState(&hsd); // make sure all is OK
|
|
282
|
+ if (!status) break; // return passing status
|
|
283
|
+ if (!--retryCnt) break; // return failing status if retries are exhausted
|
|
284
|
+ }
|
|
285
|
+ return status;
|
271
|
286
|
}
|
272
|
|
-}
|
273
|
287
|
|
|
288
|
+#endif // !USBD_USE_CDC_COMPOSITE
|
274
|
289
|
#endif // SDIO_SUPPORT
|