|
@@ -28,319 +28,296 @@
|
28
|
28
|
#include <stdint.h>
|
29
|
29
|
#include <stdbool.h>
|
30
|
30
|
|
31
|
|
-#if NONE(STM32F103xE, STM32F103xG, STM32F4xx, STM32F7xx)
|
32
|
|
- #error "ERROR - Only STM32F103xE, STM32F103xG, STM32F4xx or STM32F7xx CPUs supported"
|
|
31
|
+// use local drivers
|
|
32
|
+#if defined(STM32F103xE) || defined(STM32F103xG)
|
|
33
|
+ #include <stm32f1xx_hal_rcc_ex.h>
|
|
34
|
+ #include <stm32f1xx_hal_sd.h>
|
|
35
|
+#elif defined(STM32F4xx)
|
|
36
|
+ #include <stm32f4xx_hal_rcc.h>
|
|
37
|
+ #include <stm32f4xx_hal_dma.h>
|
|
38
|
+ #include <stm32f4xx_hal_gpio.h>
|
|
39
|
+ #include <stm32f4xx_hal_sd.h>
|
|
40
|
+#elif defined(STM32F7xx)
|
|
41
|
+ #include <stm32f7xx_hal_rcc.h>
|
|
42
|
+ #include <stm32f7xx_hal_dma.h>
|
|
43
|
+ #include <stm32f7xx_hal_gpio.h>
|
|
44
|
+ #include <stm32f7xx_hal_sd.h>
|
|
45
|
+#else
|
|
46
|
+ #error "SDIO only supported with STM32F103xE, STM32F103xG, STM32F4xx, or STM32F7xx."
|
33
|
47
|
#endif
|
34
|
48
|
|
35
|
|
-#if HAS_SD_HOST_DRIVE
|
36
|
|
-
|
37
|
|
- // use USB drivers
|
38
|
|
-
|
39
|
|
- extern "C" {
|
40
|
|
- int8_t SD_MSC_Read(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
|
41
|
|
- int8_t SD_MSC_Write(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
|
42
|
|
- extern SD_HandleTypeDef hsd;
|
43
|
|
- }
|
|
49
|
+// Fixed
|
|
50
|
+#define SDIO_D0_PIN PC8
|
|
51
|
+#define SDIO_D1_PIN PC9
|
|
52
|
+#define SDIO_D2_PIN PC10
|
|
53
|
+#define SDIO_D3_PIN PC11
|
|
54
|
+#define SDIO_CK_PIN PC12
|
|
55
|
+#define SDIO_CMD_PIN PD2
|
|
56
|
+
|
|
57
|
+SD_HandleTypeDef hsd; // create SDIO structure
|
|
58
|
+// F4 supports one DMA for RX and another for TX, but Marlin will never
|
|
59
|
+// do read and write at same time, so we use the same DMA for both.
|
|
60
|
+DMA_HandleTypeDef hdma_sdio;
|
|
61
|
+
|
|
62
|
+/*
|
|
63
|
+ SDIO_INIT_CLK_DIV is 118
|
|
64
|
+ SDIO clock frequency is 48MHz / (TRANSFER_CLOCK_DIV + 2)
|
|
65
|
+ SDIO init clock frequency should not exceed 400KHz = 48MHz / (118 + 2)
|
|
66
|
+
|
|
67
|
+ Default TRANSFER_CLOCK_DIV is 2 (118 / 40)
|
|
68
|
+ Default SDIO clock frequency is 48MHz / (2 + 2) = 12 MHz
|
|
69
|
+ This might be too fast for stable SDIO operations
|
|
70
|
+
|
|
71
|
+ MKS Robin board seems to have stable SDIO with BusWide 1bit and ClockDiv 8 i.e. 4.8MHz SDIO clock frequency
|
|
72
|
+ Additional testing is required as there are clearly some 4bit initialization problems
|
|
73
|
+*/
|
|
74
|
+
|
|
75
|
+#ifndef USBD_OK
|
|
76
|
+ #define USBD_OK 0
|
|
77
|
+#endif
|
44
|
78
|
|
45
|
|
- bool SDIO_Init() {
|
46
|
|
- return hsd.State == HAL_SD_STATE_READY; // return pass/fail status
|
47
|
|
- }
|
|
79
|
+// Target Clock, configurable. Default is 18MHz, from STM32F1
|
|
80
|
+#ifndef SDIO_CLOCK
|
|
81
|
+ #define SDIO_CLOCK 18000000 // 18 MHz
|
|
82
|
+#endif
|
48
|
83
|
|
49
|
|
- bool SDIO_ReadBlock(uint32_t block, uint8_t *src) {
|
50
|
|
- int8_t status = SD_MSC_Read(0, (uint8_t*)src, block, 1); // read one 512 byte block
|
51
|
|
- return (bool) status;
|
52
|
|
- }
|
|
84
|
+// SDIO retries, configurable. Default is 3, from STM32F1
|
|
85
|
+#ifndef SDIO_READ_RETRIES
|
|
86
|
+ #define SDIO_READ_RETRIES 3
|
|
87
|
+#endif
|
53
|
88
|
|
54
|
|
- bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) {
|
55
|
|
- int8_t status = SD_MSC_Write(0, (uint8_t*)src, block, 1); // write one 512 byte block
|
56
|
|
- return (bool) status;
|
57
|
|
- }
|
|
89
|
+// SDIO Max Clock (naming from STM Manual, don't change)
|
|
90
|
+#define SDIOCLK 48000000
|
|
91
|
+
|
|
92
|
+static uint32_t clock_to_divider(uint32_t clk) {
|
|
93
|
+ // limit the SDIO master clock to 8/3 of PCLK2. See STM32 Manuals
|
|
94
|
+ // Also limited to no more than 48Mhz (SDIOCLK).
|
|
95
|
+ const uint32_t pclk2 = HAL_RCC_GetPCLK2Freq();
|
|
96
|
+ clk = min(clk, (uint32_t)(pclk2 * 8 / 3));
|
|
97
|
+ clk = min(clk, (uint32_t)SDIOCLK);
|
|
98
|
+ // Round up divider, so we don't run the card over the speed supported,
|
|
99
|
+ // and subtract by 2, because STM32 will add 2, as written in the manual:
|
|
100
|
+ // SDIO_CK frequency = SDIOCLK / [CLKDIV + 2]
|
|
101
|
+ return pclk2 / clk + (pclk2 % clk != 0) - 2;
|
|
102
|
+}
|
|
103
|
+
|
|
104
|
+void go_to_transfer_speed() {
|
|
105
|
+ /* Default SDIO peripheral configuration for SD card initialization */
|
|
106
|
+ hsd.Init.ClockEdge = hsd.Init.ClockEdge;
|
|
107
|
+ hsd.Init.ClockBypass = hsd.Init.ClockBypass;
|
|
108
|
+ hsd.Init.ClockPowerSave = hsd.Init.ClockPowerSave;
|
|
109
|
+ hsd.Init.BusWide = hsd.Init.BusWide;
|
|
110
|
+ hsd.Init.HardwareFlowControl = hsd.Init.HardwareFlowControl;
|
|
111
|
+ hsd.Init.ClockDiv = clock_to_divider(SDIO_CLOCK);
|
|
112
|
+
|
|
113
|
+ /* Initialize SDIO peripheral interface with default configuration */
|
|
114
|
+ SDIO_Init(hsd.Instance, hsd.Init);
|
|
115
|
+}
|
|
116
|
+
|
|
117
|
+void SD_LowLevel_Init(void) {
|
|
118
|
+ uint32_t tempreg;
|
|
119
|
+
|
|
120
|
+ __HAL_RCC_GPIOC_CLK_ENABLE(); //enable GPIO clocks
|
|
121
|
+ __HAL_RCC_GPIOD_CLK_ENABLE(); //enable GPIO clocks
|
|
122
|
+
|
|
123
|
+ GPIO_InitTypeDef GPIO_InitStruct;
|
|
124
|
+
|
|
125
|
+ GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
126
|
+ GPIO_InitStruct.Pull = 1; //GPIO_NOPULL;
|
|
127
|
+ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
128
|
+
|
|
129
|
+ #if DISABLED(STM32F1xx)
|
|
130
|
+ GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
|
|
131
|
+ #endif
|
58
|
132
|
|
59
|
|
-#else // !USBD_USE_CDC_COMPOSITE
|
|
133
|
+ GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_12; // D0 & SCK
|
|
134
|
+ HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
60
|
135
|
|
61
|
|
- // use local drivers
|
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"
|
|
136
|
+ #if PINS_EXIST(SDIO_D1, SDIO_D2, SDIO_D3) // define D1-D3 only if have a four bit wide SDIO bus
|
|
137
|
+ GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11; // D1-D3
|
|
138
|
+ HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
77
|
139
|
#endif
|
78
|
140
|
|
79
|
|
- // Fixed
|
80
|
|
- #define SDIO_D0_PIN PC8
|
81
|
|
- #define SDIO_D1_PIN PC9
|
82
|
|
- #define SDIO_D2_PIN PC10
|
83
|
|
- #define SDIO_D3_PIN PC11
|
84
|
|
- #define SDIO_CK_PIN PC12
|
85
|
|
- #define SDIO_CMD_PIN PD2
|
86
|
|
-
|
87
|
|
- SD_HandleTypeDef hsd; // create SDIO structure
|
88
|
|
- // F4 supports one DMA for RX and another for TX, but Marlin will never
|
89
|
|
- // do read and write at same time, so we use the same DMA for both.
|
90
|
|
- DMA_HandleTypeDef hdma_sdio;
|
91
|
|
-
|
92
|
|
- /*
|
93
|
|
- SDIO_INIT_CLK_DIV is 118
|
94
|
|
- SDIO clock frequency is 48MHz / (TRANSFER_CLOCK_DIV + 2)
|
95
|
|
- SDIO init clock frequency should not exceed 400KHz = 48MHz / (118 + 2)
|
96
|
|
-
|
97
|
|
- Default TRANSFER_CLOCK_DIV is 2 (118 / 40)
|
98
|
|
- Default SDIO clock frequency is 48MHz / (2 + 2) = 12 MHz
|
99
|
|
- This might be too fast for stable SDIO operations
|
100
|
|
-
|
101
|
|
- MKS Robin board seems to have stable SDIO with BusWide 1bit and ClockDiv 8 i.e. 4.8MHz SDIO clock frequency
|
102
|
|
- Additional testing is required as there are clearly some 4bit initialization problems
|
103
|
|
- */
|
104
|
|
-
|
105
|
|
- #ifndef USBD_OK
|
106
|
|
- #define USBD_OK 0
|
107
|
|
- #endif
|
|
141
|
+ // Configure PD.02 CMD line
|
|
142
|
+ GPIO_InitStruct.Pin = GPIO_PIN_2;
|
|
143
|
+ HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
108
|
144
|
|
109
|
|
- // Target Clock, configurable. Default is 18MHz, from STM32F1
|
110
|
|
- #ifndef SDIO_CLOCK
|
111
|
|
- #define SDIO_CLOCK 18000000 // 18 MHz
|
|
145
|
+ // Setup DMA
|
|
146
|
+ #if defined(STM32F1xx)
|
|
147
|
+ hdma_sdio.Init.Mode = DMA_NORMAL;
|
|
148
|
+ hdma_sdio.Instance = DMA2_Channel4;
|
|
149
|
+ HAL_NVIC_EnableIRQ(DMA2_Channel4_5_IRQn);
|
|
150
|
+ #elif defined(STM32F4xx)
|
|
151
|
+ hdma_sdio.Init.Mode = DMA_PFCTRL;
|
|
152
|
+ hdma_sdio.Instance = DMA2_Stream3;
|
|
153
|
+ hdma_sdio.Init.Channel = DMA_CHANNEL_4;
|
|
154
|
+ hdma_sdio.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
|
|
155
|
+ hdma_sdio.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
|
|
156
|
+ hdma_sdio.Init.MemBurst = DMA_MBURST_INC4;
|
|
157
|
+ hdma_sdio.Init.PeriphBurst = DMA_PBURST_INC4;
|
|
158
|
+ HAL_NVIC_EnableIRQ(DMA2_Stream3_IRQn);
|
112
|
159
|
#endif
|
|
160
|
+ HAL_NVIC_EnableIRQ(SDIO_IRQn);
|
|
161
|
+ hdma_sdio.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
162
|
+ hdma_sdio.Init.MemInc = DMA_MINC_ENABLE;
|
|
163
|
+ hdma_sdio.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
|
|
164
|
+ hdma_sdio.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
|
|
165
|
+ hdma_sdio.Init.Priority = DMA_PRIORITY_LOW;
|
|
166
|
+ __HAL_LINKDMA(&hsd, hdmarx, hdma_sdio);
|
|
167
|
+ __HAL_LINKDMA(&hsd, hdmatx, hdma_sdio);
|
113
|
168
|
|
114
|
|
- // SDIO retries, configurable. Default is 3, from STM32F1
|
115
|
|
- #ifndef SDIO_READ_RETRIES
|
116
|
|
- #define SDIO_READ_RETRIES 3
|
|
169
|
+ #if defined(STM32F1xx)
|
|
170
|
+ __HAL_RCC_SDIO_CLK_ENABLE();
|
|
171
|
+ __HAL_RCC_DMA2_CLK_ENABLE();
|
|
172
|
+ #else
|
|
173
|
+ __HAL_RCC_SDIO_FORCE_RESET();
|
|
174
|
+ delay(2);
|
|
175
|
+ __HAL_RCC_SDIO_RELEASE_RESET();
|
|
176
|
+ delay(2);
|
|
177
|
+ __HAL_RCC_SDIO_CLK_ENABLE();
|
|
178
|
+
|
|
179
|
+ __HAL_RCC_DMA2_FORCE_RESET();
|
|
180
|
+ delay(2);
|
|
181
|
+ __HAL_RCC_DMA2_RELEASE_RESET();
|
|
182
|
+ delay(2);
|
|
183
|
+ __HAL_RCC_DMA2_CLK_ENABLE();
|
117
|
184
|
#endif
|
118
|
185
|
|
119
|
|
- // SDIO Max Clock (naming from STM Manual, don't change)
|
120
|
|
- #define SDIOCLK 48000000
|
121
|
|
-
|
122
|
|
- static uint32_t clock_to_divider(uint32_t clk) {
|
123
|
|
- // limit the SDIO master clock to 8/3 of PCLK2. See STM32 Manuals
|
124
|
|
- // Also limited to no more than 48Mhz (SDIOCLK).
|
125
|
|
- const uint32_t pclk2 = HAL_RCC_GetPCLK2Freq();
|
126
|
|
- clk = min(clk, (uint32_t)(pclk2 * 8 / 3));
|
127
|
|
- clk = min(clk, (uint32_t)SDIOCLK);
|
128
|
|
- // Round up divider, so we don't run the card over the speed supported,
|
129
|
|
- // and subtract by 2, because STM32 will add 2, as written in the manual:
|
130
|
|
- // SDIO_CK frequency = SDIOCLK / [CLKDIV + 2]
|
131
|
|
- return pclk2 / clk + (pclk2 % clk != 0) - 2;
|
132
|
|
- }
|
133
|
|
-
|
134
|
|
- void go_to_transfer_speed() {
|
135
|
|
- /* Default SDIO peripheral configuration for SD card initialization */
|
136
|
|
- hsd.Init.ClockEdge = hsd.Init.ClockEdge;
|
137
|
|
- hsd.Init.ClockBypass = hsd.Init.ClockBypass;
|
138
|
|
- hsd.Init.ClockPowerSave = hsd.Init.ClockPowerSave;
|
139
|
|
- hsd.Init.BusWide = hsd.Init.BusWide;
|
140
|
|
- hsd.Init.HardwareFlowControl = hsd.Init.HardwareFlowControl;
|
141
|
|
- hsd.Init.ClockDiv = clock_to_divider(SDIO_CLOCK);
|
142
|
|
-
|
143
|
|
- /* Initialize SDIO peripheral interface with default configuration */
|
144
|
|
- SDIO_Init(hsd.Instance, hsd.Init);
|
145
|
|
- }
|
146
|
|
-
|
147
|
|
- void SD_LowLevel_Init(void) {
|
148
|
|
- uint32_t tempreg;
|
|
186
|
+ //Initialize the SDIO (with initial <400Khz Clock)
|
|
187
|
+ tempreg = 0; //Reset value
|
|
188
|
+ tempreg |= SDIO_CLKCR_CLKEN; // Clock enabled
|
|
189
|
+ tempreg |= SDIO_INIT_CLK_DIV; // Clock Divider. Clock = 48000 / (118 + 2) = 400Khz
|
|
190
|
+ // Keep the rest at 0 => HW_Flow Disabled, Rising Clock Edge, Disable CLK ByPass, Bus Width = 0, Power save Disable
|
|
191
|
+ SDIO->CLKCR = tempreg;
|
149
|
192
|
|
150
|
|
- __HAL_RCC_GPIOC_CLK_ENABLE(); //enable GPIO clocks
|
151
|
|
- __HAL_RCC_GPIOD_CLK_ENABLE(); //enable GPIO clocks
|
|
193
|
+ // Power up the SDIO
|
|
194
|
+ SDIO_PowerState_ON(SDIO);
|
|
195
|
+ hsd.Instance = SDIO;
|
|
196
|
+}
|
152
|
197
|
|
153
|
|
- GPIO_InitTypeDef GPIO_InitStruct;
|
|
198
|
+void HAL_SD_MspInit(SD_HandleTypeDef *hsd) { // application specific init
|
|
199
|
+ UNUSED(hsd); // Prevent unused argument(s) compilation warning
|
|
200
|
+ __HAL_RCC_SDIO_CLK_ENABLE(); // turn on SDIO clock
|
|
201
|
+}
|
154
|
202
|
|
155
|
|
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
156
|
|
- GPIO_InitStruct.Pull = 1; //GPIO_NOPULL;
|
157
|
|
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
203
|
+bool SDIO_Init() {
|
|
204
|
+ uint8_t retryCnt = SDIO_READ_RETRIES;
|
158
|
205
|
|
159
|
|
- #if DISABLED(STM32F1xx)
|
160
|
|
- GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
|
161
|
|
- #endif
|
|
206
|
+ bool status;
|
|
207
|
+ hsd.Instance = SDIO;
|
|
208
|
+ hsd.State = HAL_SD_STATE_RESET;
|
162
|
209
|
|
163
|
|
- GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_12; // D0 & SCK
|
164
|
|
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
210
|
+ SD_LowLevel_Init();
|
165
|
211
|
|
166
|
|
- #if PINS_EXIST(SDIO_D1, SDIO_D2, SDIO_D3) // define D1-D3 only if have a four bit wide SDIO bus
|
167
|
|
- GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11; // D1-D3
|
168
|
|
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
169
|
|
- #endif
|
170
|
|
-
|
171
|
|
- // Configure PD.02 CMD line
|
172
|
|
- GPIO_InitStruct.Pin = GPIO_PIN_2;
|
173
|
|
- HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
174
|
|
-
|
175
|
|
- // Setup DMA
|
176
|
|
- #if defined(STM32F1xx)
|
177
|
|
- hdma_sdio.Init.Mode = DMA_NORMAL;
|
178
|
|
- hdma_sdio.Instance = DMA2_Channel4;
|
179
|
|
- HAL_NVIC_EnableIRQ(DMA2_Channel4_5_IRQn);
|
180
|
|
- #elif defined(STM32F4xx)
|
181
|
|
- hdma_sdio.Init.Mode = DMA_PFCTRL;
|
182
|
|
- hdma_sdio.Instance = DMA2_Stream3;
|
183
|
|
- hdma_sdio.Init.Channel = DMA_CHANNEL_4;
|
184
|
|
- hdma_sdio.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
|
185
|
|
- hdma_sdio.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
|
186
|
|
- hdma_sdio.Init.MemBurst = DMA_MBURST_INC4;
|
187
|
|
- hdma_sdio.Init.PeriphBurst = DMA_PBURST_INC4;
|
188
|
|
- HAL_NVIC_EnableIRQ(DMA2_Stream3_IRQn);
|
189
|
|
- #endif
|
190
|
|
- HAL_NVIC_EnableIRQ(SDIO_IRQn);
|
191
|
|
- hdma_sdio.Init.PeriphInc = DMA_PINC_DISABLE;
|
192
|
|
- hdma_sdio.Init.MemInc = DMA_MINC_ENABLE;
|
193
|
|
- hdma_sdio.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
|
194
|
|
- hdma_sdio.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
|
195
|
|
- hdma_sdio.Init.Priority = DMA_PRIORITY_LOW;
|
196
|
|
- __HAL_LINKDMA(&hsd, hdmarx, hdma_sdio);
|
197
|
|
- __HAL_LINKDMA(&hsd, hdmatx, hdma_sdio);
|
198
|
|
-
|
199
|
|
- #if defined(STM32F1xx)
|
200
|
|
- __HAL_RCC_SDIO_CLK_ENABLE();
|
201
|
|
- __HAL_RCC_DMA2_CLK_ENABLE();
|
202
|
|
- #else
|
203
|
|
- __HAL_RCC_SDIO_FORCE_RESET();
|
204
|
|
- delay(2);
|
205
|
|
- __HAL_RCC_SDIO_RELEASE_RESET();
|
206
|
|
- delay(2);
|
207
|
|
- __HAL_RCC_SDIO_CLK_ENABLE();
|
208
|
|
-
|
209
|
|
- __HAL_RCC_DMA2_FORCE_RESET();
|
210
|
|
- delay(2);
|
211
|
|
- __HAL_RCC_DMA2_RELEASE_RESET();
|
212
|
|
- delay(2);
|
213
|
|
- __HAL_RCC_DMA2_CLK_ENABLE();
|
214
|
|
- #endif
|
215
|
|
-
|
216
|
|
- //Initialize the SDIO (with initial <400Khz Clock)
|
217
|
|
- tempreg = 0; //Reset value
|
218
|
|
- tempreg |= SDIO_CLKCR_CLKEN; // Clock enabled
|
219
|
|
- tempreg |= SDIO_INIT_CLK_DIV; // Clock Divider. Clock = 48000 / (118 + 2) = 400Khz
|
220
|
|
- // Keep the rest at 0 => HW_Flow Disabled, Rising Clock Edge, Disable CLK ByPass, Bus Width = 0, Power save Disable
|
221
|
|
- SDIO->CLKCR = tempreg;
|
222
|
|
-
|
223
|
|
- // Power up the SDIO
|
224
|
|
- SDIO_PowerState_ON(SDIO);
|
225
|
|
- hsd.Instance = SDIO;
|
226
|
|
- }
|
227
|
|
-
|
228
|
|
- void HAL_SD_MspInit(SD_HandleTypeDef *hsd) { // application specific init
|
229
|
|
- UNUSED(hsd); // Prevent unused argument(s) compilation warning
|
230
|
|
- __HAL_RCC_SDIO_CLK_ENABLE(); // turn on SDIO clock
|
|
212
|
+ uint8_t retry_Cnt = retryCnt;
|
|
213
|
+ for (;;) {
|
|
214
|
+ TERN_(USE_WATCHDOG, HAL_watchdog_refresh());
|
|
215
|
+ status = (bool) HAL_SD_Init(&hsd);
|
|
216
|
+ if (!status) break;
|
|
217
|
+ if (!--retry_Cnt) return false; // return failing status if retries are exhausted
|
231
|
218
|
}
|
232
|
219
|
|
233
|
|
- bool SDIO_Init() {
|
234
|
|
- uint8_t retryCnt = SDIO_READ_RETRIES;
|
235
|
|
-
|
236
|
|
- bool status;
|
237
|
|
- hsd.Instance = SDIO;
|
238
|
|
- hsd.State = HAL_SD_STATE_RESET;
|
|
220
|
+ go_to_transfer_speed();
|
239
|
221
|
|
240
|
|
- SD_LowLevel_Init();
|
241
|
|
-
|
242
|
|
- uint8_t retry_Cnt = retryCnt;
|
|
222
|
+ #if PINS_EXIST(SDIO_D1, SDIO_D2, SDIO_D3) // go to 4 bit wide mode if pins are defined
|
|
223
|
+ retry_Cnt = retryCnt;
|
243
|
224
|
for (;;) {
|
244
|
225
|
TERN_(USE_WATCHDOG, HAL_watchdog_refresh());
|
245
|
|
- status = (bool) HAL_SD_Init(&hsd);
|
246
|
|
- if (!status) break;
|
247
|
|
- if (!--retry_Cnt) return false; // return failing status if retries are exhausted
|
|
226
|
+ if (!HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B)) break; // some cards are only 1 bit wide so a pass here is not required
|
|
227
|
+ if (!--retry_Cnt) break;
|
248
|
228
|
}
|
249
|
|
-
|
250
|
|
- go_to_transfer_speed();
|
251
|
|
-
|
252
|
|
- #if PINS_EXIST(SDIO_D1, SDIO_D2, SDIO_D3) // go to 4 bit wide mode if pins are defined
|
|
229
|
+ if (!retry_Cnt) { // wide bus failed, go back to one bit wide mode
|
|
230
|
+ hsd.State = (HAL_SD_StateTypeDef) 0; // HAL_SD_STATE_RESET
|
|
231
|
+ SD_LowLevel_Init();
|
253
|
232
|
retry_Cnt = retryCnt;
|
254
|
233
|
for (;;) {
|
255
|
234
|
TERN_(USE_WATCHDOG, HAL_watchdog_refresh());
|
256
|
|
- if (!HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B)) break; // some cards are only 1 bit wide so a pass here is not required
|
257
|
|
- if (!--retry_Cnt) break;
|
|
235
|
+ status = (bool) HAL_SD_Init(&hsd);
|
|
236
|
+ if (!status) break;
|
|
237
|
+ if (!--retry_Cnt) return false; // return failing status if retries are exhausted
|
258
|
238
|
}
|
259
|
|
- if (!retry_Cnt) { // wide bus failed, go back to one bit wide mode
|
260
|
|
- hsd.State = (HAL_SD_StateTypeDef) 0; // HAL_SD_STATE_RESET
|
261
|
|
- SD_LowLevel_Init();
|
262
|
|
- retry_Cnt = retryCnt;
|
263
|
|
- for (;;) {
|
264
|
|
- TERN_(USE_WATCHDOG, HAL_watchdog_refresh());
|
265
|
|
- status = (bool) HAL_SD_Init(&hsd);
|
266
|
|
- if (!status) break;
|
267
|
|
- if (!--retry_Cnt) return false; // return failing status if retries are exhausted
|
268
|
|
- }
|
269
|
|
- go_to_transfer_speed();
|
270
|
|
- }
|
271
|
|
- #endif
|
|
239
|
+ go_to_transfer_speed();
|
|
240
|
+ }
|
|
241
|
+ #endif
|
272
|
242
|
|
273
|
|
- return true;
|
274
|
|
- }
|
|
243
|
+ return true;
|
|
244
|
+}
|
275
|
245
|
|
276
|
|
- static bool SDIO_ReadWriteBlock_DMA(uint32_t block, const uint8_t *src, uint8_t *dst) {
|
277
|
|
- if (HAL_SD_GetCardState(&hsd) != HAL_SD_CARD_TRANSFER) return false;
|
|
246
|
+static bool SDIO_ReadWriteBlock_DMA(uint32_t block, const uint8_t *src, uint8_t *dst) {
|
|
247
|
+ if (HAL_SD_GetCardState(&hsd) != HAL_SD_CARD_TRANSFER) return false;
|
278
|
248
|
|
279
|
|
- TERN_(USE_WATCHDOG, HAL_watchdog_refresh());
|
|
249
|
+ TERN_(USE_WATCHDOG, HAL_watchdog_refresh());
|
280
|
250
|
|
281
|
|
- HAL_StatusTypeDef ret;
|
282
|
|
- if (src) {
|
283
|
|
- hdma_sdio.Init.Direction = DMA_MEMORY_TO_PERIPH;
|
284
|
|
- HAL_DMA_Init(&hdma_sdio);
|
285
|
|
- ret = HAL_SD_WriteBlocks_DMA(&hsd, (uint8_t *)src, block, 1);
|
286
|
|
- }
|
287
|
|
- else {
|
288
|
|
- hdma_sdio.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
289
|
|
- HAL_DMA_Init(&hdma_sdio);
|
290
|
|
- ret = HAL_SD_ReadBlocks_DMA(&hsd, (uint8_t *)dst, block, 1);
|
291
|
|
- }
|
|
251
|
+ HAL_StatusTypeDef ret;
|
|
252
|
+ if (src) {
|
|
253
|
+ hdma_sdio.Init.Direction = DMA_MEMORY_TO_PERIPH;
|
|
254
|
+ HAL_DMA_Init(&hdma_sdio);
|
|
255
|
+ ret = HAL_SD_WriteBlocks_DMA(&hsd, (uint8_t *)src, block, 1);
|
|
256
|
+ }
|
|
257
|
+ else {
|
|
258
|
+ hdma_sdio.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
|
259
|
+ HAL_DMA_Init(&hdma_sdio);
|
|
260
|
+ ret = HAL_SD_ReadBlocks_DMA(&hsd, (uint8_t *)dst, block, 1);
|
|
261
|
+ }
|
292
|
262
|
|
293
|
|
- if (ret != HAL_OK) {
|
|
263
|
+ if (ret != HAL_OK) {
|
|
264
|
+ HAL_DMA_Abort_IT(&hdma_sdio);
|
|
265
|
+ HAL_DMA_DeInit(&hdma_sdio);
|
|
266
|
+ return false;
|
|
267
|
+ }
|
|
268
|
+
|
|
269
|
+ millis_t timeout = millis() + 500;
|
|
270
|
+ // Wait the transfer
|
|
271
|
+ while (hsd.State != HAL_SD_STATE_READY) {
|
|
272
|
+ if (ELAPSED(millis(), timeout)) {
|
294
|
273
|
HAL_DMA_Abort_IT(&hdma_sdio);
|
295
|
274
|
HAL_DMA_DeInit(&hdma_sdio);
|
296
|
275
|
return false;
|
297
|
276
|
}
|
|
277
|
+ }
|
298
|
278
|
|
299
|
|
- millis_t timeout = millis() + 500;
|
300
|
|
- // Wait the transfer
|
301
|
|
- while (hsd.State != HAL_SD_STATE_READY) {
|
302
|
|
- if (ELAPSED(millis(), timeout)) {
|
303
|
|
- HAL_DMA_Abort_IT(&hdma_sdio);
|
304
|
|
- HAL_DMA_DeInit(&hdma_sdio);
|
305
|
|
- return false;
|
306
|
|
- }
|
307
|
|
- }
|
|
279
|
+ while (__HAL_DMA_GET_FLAG(&hdma_sdio, __HAL_DMA_GET_TC_FLAG_INDEX(&hdma_sdio)) != 0
|
|
280
|
+ || __HAL_DMA_GET_FLAG(&hdma_sdio, __HAL_DMA_GET_TE_FLAG_INDEX(&hdma_sdio)) != 0) { /* nada */ }
|
308
|
281
|
|
309
|
|
- while (__HAL_DMA_GET_FLAG(&hdma_sdio, __HAL_DMA_GET_TC_FLAG_INDEX(&hdma_sdio)) != 0
|
310
|
|
- || __HAL_DMA_GET_FLAG(&hdma_sdio, __HAL_DMA_GET_TE_FLAG_INDEX(&hdma_sdio)) != 0) { /* nada */ }
|
|
282
|
+ HAL_DMA_Abort_IT(&hdma_sdio);
|
|
283
|
+ HAL_DMA_DeInit(&hdma_sdio);
|
311
|
284
|
|
312
|
|
- HAL_DMA_Abort_IT(&hdma_sdio);
|
313
|
|
- HAL_DMA_DeInit(&hdma_sdio);
|
|
285
|
+ timeout = millis() + 500;
|
|
286
|
+ while (HAL_SD_GetCardState(&hsd) != HAL_SD_CARD_TRANSFER) if (ELAPSED(millis(), timeout)) return false;
|
314
|
287
|
|
315
|
|
- timeout = millis() + 500;
|
316
|
|
- while (HAL_SD_GetCardState(&hsd) != HAL_SD_CARD_TRANSFER) if (ELAPSED(millis(), timeout)) return false;
|
|
288
|
+ return true;
|
|
289
|
+}
|
317
|
290
|
|
318
|
|
- return true;
|
319
|
|
- }
|
|
291
|
+bool SDIO_ReadBlock(uint32_t block, uint8_t *dst) {
|
|
292
|
+ uint8_t retries = SDIO_READ_RETRIES;
|
|
293
|
+ while (retries--) if (SDIO_ReadWriteBlock_DMA(block, NULL, dst)) return true;
|
|
294
|
+ return false;
|
|
295
|
+}
|
320
|
296
|
|
321
|
|
- bool SDIO_ReadBlock(uint32_t block, uint8_t *dst) {
|
322
|
|
- uint8_t retries = SDIO_READ_RETRIES;
|
323
|
|
- while (retries--) if (SDIO_ReadWriteBlock_DMA(block, NULL, dst)) return true;
|
324
|
|
- return false;
|
325
|
|
- }
|
|
297
|
+bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) {
|
|
298
|
+ uint8_t retries = SDIO_READ_RETRIES;
|
|
299
|
+ while (retries--) if (SDIO_ReadWriteBlock_DMA(block, src, NULL)) return true;
|
|
300
|
+ return false;
|
|
301
|
+}
|
326
|
302
|
|
327
|
|
- bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) {
|
328
|
|
- uint8_t retries = SDIO_READ_RETRIES;
|
329
|
|
- while (retries--) if (SDIO_ReadWriteBlock_DMA(block, src, NULL)) return true;
|
330
|
|
- return false;
|
331
|
|
- }
|
|
303
|
+bool SDIO_IsReady() {
|
|
304
|
+ return hsd.State == HAL_SD_STATE_READY;
|
|
305
|
+}
|
332
|
306
|
|
333
|
|
- #if defined(STM32F1xx)
|
334
|
|
- #define DMA_IRQ_HANDLER DMA2_Channel4_5_IRQHandler
|
335
|
|
- #elif defined(STM32F4xx)
|
336
|
|
- #define DMA_IRQ_HANDLER DMA2_Stream3_IRQHandler
|
337
|
|
- #else
|
338
|
|
- #error "Unknown STM32 architecture."
|
339
|
|
- #endif
|
|
307
|
+uint32_t SDIO_GetCardSize() {
|
|
308
|
+ return (uint32_t)(hsd.SdCard.BlockNbr) * (hsd.SdCard.BlockSize);
|
|
309
|
+}
|
|
310
|
+
|
|
311
|
+#if defined(STM32F1xx)
|
|
312
|
+ #define DMA_IRQ_HANDLER DMA2_Channel4_5_IRQHandler
|
|
313
|
+#elif defined(STM32F4xx)
|
|
314
|
+ #define DMA_IRQ_HANDLER DMA2_Stream3_IRQHandler
|
|
315
|
+#else
|
|
316
|
+ #error "Unknown STM32 architecture."
|
|
317
|
+#endif
|
340
|
318
|
|
341
|
|
- extern "C" void SDIO_IRQHandler(void) { HAL_SD_IRQHandler(&hsd); }
|
342
|
|
- extern "C" void DMA_IRQ_HANDLER(void) { HAL_DMA_IRQHandler(&hdma_sdio); }
|
|
319
|
+extern "C" void SDIO_IRQHandler(void) { HAL_SD_IRQHandler(&hsd); }
|
|
320
|
+extern "C" void DMA_IRQ_HANDLER(void) { HAL_DMA_IRQHandler(&hdma_sdio); }
|
343
|
321
|
|
344
|
|
-#endif // !USBD_USE_CDC_COMPOSITE
|
345
|
322
|
#endif // SDIO_SUPPORT
|
346
|
323
|
#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1
|