Browse Source

Better defaults, compatibility for SDIO + STM32 (#20570)

Victor Oliveira 4 years ago
parent
commit
2ce9fa4b9c
No account linked to committer's email address

+ 33
- 27
Marlin/src/HAL/STM32/Sd2Card_sdio_stm32duino.cpp View File

88
 
88
 
89
     MKS Robin board seems to have stable SDIO with BusWide 1bit and ClockDiv 8 i.e. 4.8MHz SDIO clock frequency
89
     MKS Robin board seems to have stable SDIO with BusWide 1bit and ClockDiv 8 i.e. 4.8MHz SDIO clock frequency
90
     Additional testing is required as there are clearly some 4bit initialization problems
90
     Additional testing is required as there are clearly some 4bit initialization problems
91
-
92
-    Add -DTRANSFER_CLOCK_DIV=8 to build parameters to improve SDIO stability
93
   */
91
   */
94
 
92
 
95
-  #ifndef TRANSFER_CLOCK_DIV
96
-    #define TRANSFER_CLOCK_DIV (uint8_t(SDIO_INIT_CLK_DIV) / 40)
97
-  #endif
98
-
99
   #ifndef USBD_OK
93
   #ifndef USBD_OK
100
     #define USBD_OK 0
94
     #define USBD_OK 0
101
   #endif
95
   #endif
102
 
96
 
97
+  // Target Clock, configurable. Default is 18MHz, from STM32F1
98
+  #ifndef SDIO_CLOCK
99
+    #define SDIO_CLOCK                         18000000       /* 18 MHz */
100
+  #endif
101
+
102
+  // SDIO retries, configurable. Default is 3, from STM32F1
103
+  #ifndef SDIO_READ_RETRIES
104
+    #define SDIO_READ_RETRIES                  3
105
+  #endif
106
+
107
+  // SDIO Max Clock (naming from STM Manual, don't change)
108
+  #define SDIOCLK 48000000
109
+
110
+  static uint32_t clock_to_divider(uint32_t clk) {
111
+    // limit the SDIO master clock to 8/3 of PCLK2. See STM32 Manuals
112
+    // Also limited to no more than 48Mhz (SDIOCLK).
113
+    const uint32_t pclk2 = HAL_RCC_GetPCLK2Freq();
114
+    clk = min(clk, (uint32_t)(pclk2 * 8 / 3));
115
+    clk = min(clk, (uint32_t)SDIOCLK);
116
+    // Round up divider, so we don't run the card over the speed supported,
117
+    // and subtract by 2, because STM32 will add 2, as written in the manual:
118
+    // SDIO_CK frequency = SDIOCLK / [CLKDIV + 2]
119
+    return pclk2 / clk + (pclk2 % clk != 0) - 2;
120
+  }
121
+
103
   void go_to_transfer_speed() {
122
   void go_to_transfer_speed() {
104
     SD_InitTypeDef Init;
123
     SD_InitTypeDef Init;
105
 
124
 
109
     Init.ClockPowerSave      = hsd.Init.ClockPowerSave;
128
     Init.ClockPowerSave      = hsd.Init.ClockPowerSave;
110
     Init.BusWide             = hsd.Init.BusWide;
129
     Init.BusWide             = hsd.Init.BusWide;
111
     Init.HardwareFlowControl = hsd.Init.HardwareFlowControl;
130
     Init.HardwareFlowControl = hsd.Init.HardwareFlowControl;
112
-    Init.ClockDiv            = TRANSFER_CLOCK_DIV;
131
+    Init.ClockDiv            = clock_to_divider(SDIO_CLOCK);
113
 
132
 
114
     /* Initialize SDIO peripheral interface with default configuration */
133
     /* Initialize SDIO peripheral interface with default configuration */
115
     SDIO_Init(hsd.Instance, Init);
134
     SDIO_Init(hsd.Instance, Init);
155
     //Initialize the SDIO (with initial <400Khz Clock)
174
     //Initialize the SDIO (with initial <400Khz Clock)
156
     tempreg = 0;  //Reset value
175
     tempreg = 0;  //Reset value
157
     tempreg |= SDIO_CLKCR_CLKEN;  // Clock enabled
176
     tempreg |= SDIO_CLKCR_CLKEN;  // Clock enabled
158
-    tempreg |= (uint32_t)0x76;    // Clock Divider. Clock = 48000 / (118 + 2) = 400Khz
177
+    tempreg |= SDIO_INIT_CLK_DIV; // Clock Divider. Clock = 48000 / (118 + 2) = 400Khz
159
     // Keep the rest at 0 => HW_Flow Disabled, Rising Clock Edge, Disable CLK ByPass, Bus Width = 0, Power save Disable
178
     // Keep the rest at 0 => HW_Flow Disabled, Rising Clock Edge, Disable CLK ByPass, Bus Width = 0, Power save Disable
160
     SDIO->CLKCR = tempreg;
179
     SDIO->CLKCR = tempreg;
161
 
180
 
162
     // Power up the SDIO
181
     // Power up the SDIO
163
-    SDIO->POWER = 0x03;
182
+    SDIO_PowerState_ON(SDIO);
164
   }
183
   }
165
 
184
 
166
   void HAL_SD_MspInit(SD_HandleTypeDef *hsd) { // application specific init
185
   void HAL_SD_MspInit(SD_HandleTypeDef *hsd) { // application specific init
167
-    UNUSED(hsd);   /* Prevent unused argument(s) compilation warning */
186
+    UNUSED(hsd);   // Prevent unused argument(s) compilation warning
168
     __HAL_RCC_SDIO_CLK_ENABLE();  // turn on SDIO clock
187
     __HAL_RCC_SDIO_CLK_ENABLE();  // turn on SDIO clock
169
   }
188
   }
170
 
189
 
171
-  constexpr uint8_t SD_RETRY_COUNT = TERN(SD_CHECK_AND_RETRY, 3, 1);
172
-
173
   bool SDIO_Init() {
190
   bool SDIO_Init() {
174
-    //init SDIO and get SD card info
175
-
176
-    uint8_t retryCnt = SD_RETRY_COUNT;
191
+    uint8_t retryCnt = SDIO_READ_RETRIES;
177
 
192
 
178
     bool status;
193
     bool status;
179
     hsd.Instance = SDIO;
194
     hsd.Instance = SDIO;
180
-    hsd.State = (HAL_SD_StateTypeDef) 0;  // HAL_SD_STATE_RESET
181
-
182
-    /*
183
-    hsd.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
184
-    hsd.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
185
-    hsd.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
186
-    hsd.Init.BusWide = SDIO_BUS_WIDE_1B;
187
-    hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
188
-    hsd.Init.ClockDiv = 8;
189
-    */
195
+    hsd.State = HAL_SD_STATE_RESET;
190
 
196
 
191
     SD_LowLevel_Init();
197
     SD_LowLevel_Init();
192
 
198
 
258
 
264
 
259
   bool SDIO_ReadBlock(uint32_t block, uint8_t *dst) {
265
   bool SDIO_ReadBlock(uint32_t block, uint8_t *dst) {
260
     hsd.Instance = SDIO;
266
     hsd.Instance = SDIO;
261
-    uint8_t retryCnt = SD_RETRY_COUNT;
267
+    uint8_t retryCnt = SDIO_READ_RETRIES;
262
 
268
 
263
     bool status;
269
     bool status;
264
     for (;;) {
270
     for (;;) {
307
 
313
 
308
   bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) {
314
   bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) {
309
     hsd.Instance = SDIO;
315
     hsd.Instance = SDIO;
310
-    uint8_t retryCnt = SD_RETRY_COUNT;
316
+    uint8_t retryCnt = SDIO_READ_RETRIES;
311
     bool status;
317
     bool status;
312
     for (;;) {
318
     for (;;) {
313
       status = (bool) HAL_SD_WriteBlocks(&hsd, (uint8_t*)src, block, 1, 500);  // write one 512 byte block with 500mS timeout
319
       status = (bool) HAL_SD_WriteBlocks(&hsd, (uint8_t*)src, block, 1, 500);  // write one 512 byte block with 500mS timeout

+ 2
- 0
Marlin/src/pins/stm32f1/pins_MKS_ROBIN.h View File

165
 #define SPI_DEVICE                             2
165
 #define SPI_DEVICE                             2
166
 
166
 
167
 #define SDIO_SUPPORT
167
 #define SDIO_SUPPORT
168
+#define SDIO_CLOCK                       4500000
169
+#define SDIO_READ_RETRIES                     16
168
 #if ENABLED(SDIO_SUPPORT)
170
 #if ENABLED(SDIO_SUPPORT)
169
   #define SCK_PIN                           PB13  // SPI2
171
   #define SCK_PIN                           PB13  // SPI2
170
   #define MISO_PIN                          PB14  // SPI2
172
   #define MISO_PIN                          PB14  // SPI2

+ 1
- 0
Marlin/src/pins/stm32f4/pins_LERDGE_K.h View File

142
 // SD support
142
 // SD support
143
 //
143
 //
144
 #define SDIO_SUPPORT
144
 #define SDIO_SUPPORT
145
+#define SDIO_CLOCK                      4800000
145
 
146
 
146
 //
147
 //
147
 // Misc. Functions
148
 // Misc. Functions

+ 1
- 0
Marlin/src/pins/stm32f4/pins_LERDGE_S.h View File

156
 // SD support
156
 // SD support
157
 //
157
 //
158
 #define SDIO_SUPPORT
158
 #define SDIO_SUPPORT
159
+#define SDIO_CLOCK                       4800000
159
 
160
 
160
 #define SCK_PIN                             PC12  //confirmed working
161
 #define SCK_PIN                             PC12  //confirmed working
161
 #define MISO_PIN                            PC8   //confirmed working
162
 #define MISO_PIN                            PC8   //confirmed working

+ 1
- 0
Marlin/src/pins/stm32f4/pins_LERDGE_X.h View File

127
 //
127
 //
128
 #define SDIO_SUPPORT
128
 #define SDIO_SUPPORT
129
 #define SD_DETECT_PIN                       PA8
129
 #define SD_DETECT_PIN                       PA8
130
+#define SDIO_CLOCK                      4800000
130
 
131
 
131
 //
132
 //
132
 // LCD / Controller
133
 // LCD / Controller

+ 4
- 4
platformio.ini View File

973
 board_build.encrypt  = Yes
973
 board_build.encrypt  = Yes
974
 board_build.firmware = Robin.bin
974
 board_build.firmware = Robin.bin
975
 build_flags          = ${common_stm32.build_flags}
975
 build_flags          = ${common_stm32.build_flags}
976
-  -DENABLE_HWSERIAL3 -DTRANSFER_CLOCK_DIV=8 -DTIMER_SERIAL=TIM5
976
+  -DENABLE_HWSERIAL3 -DTIMER_SERIAL=TIM5
977
 build_unflags        = ${common_stm32.build_unflags}
977
 build_unflags        = ${common_stm32.build_unflags}
978
  -DUSBCON -DUSBD_USE_CDC
978
  -DUSBCON -DUSBD_USE_CDC
979
 extra_scripts        = ${common.extra_scripts}
979
 extra_scripts        = ${common.extra_scripts}
1149
 [env:flsun_hispeedv1]
1149
 [env:flsun_hispeedv1]
1150
 platform             = ${common_stm32.platform}
1150
 platform             = ${common_stm32.platform}
1151
 extends              = common_stm32
1151
 extends              = common_stm32
1152
-build_flags          = ${common_stm32.build_flags} -DMCU_STM32F103VE -DSS_TIMER=4 -DENABLE_HWSERIAL3 -DTRANSFER_CLOCK_DIV=8
1152
+build_flags          = ${common_stm32.build_flags} -DMCU_STM32F103VE -DSS_TIMER=4 -DENABLE_HWSERIAL3
1153
 board                = genericSTM32F103VE
1153
 board                = genericSTM32F103VE
1154
 board_build.core     = stm32
1154
 board_build.core     = stm32
1155
 board_build.variant  = MARLIN_F103Vx
1155
 board_build.variant  = MARLIN_F103Vx
1321
 build_flags         = ${common_stm32.build_flags}
1321
 build_flags         = ${common_stm32.build_flags}
1322
   -DSTM32F4 -DSTM32F4xx -DTARGET_STM32F4
1322
   -DSTM32F4 -DSTM32F4xx -DTARGET_STM32F4
1323
   -DDISABLE_GENERIC_SERIALUSB -DARDUINO_ARCH_STM32 -DARDUINO_LERDGE
1323
   -DDISABLE_GENERIC_SERIALUSB -DARDUINO_ARCH_STM32 -DARDUINO_LERDGE
1324
-  -DTRANSFER_CLOCK_DIV=8 -DHAL_SRAM_MODULE_ENABLED
1324
+  -DHAL_SRAM_MODULE_ENABLED
1325
 build_unflags       = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC -DUSBD_VID=0x0483
1325
 build_unflags       = ${common_stm32.build_unflags} -DUSBCON -DUSBD_USE_CDC -DUSBD_VID=0x0483
1326
 
1326
 
1327
 #
1327
 #
1369
 [env:mks_robin_nano35_stm32]
1369
 [env:mks_robin_nano35_stm32]
1370
 platform             = ${common_stm32.platform}
1370
 platform             = ${common_stm32.platform}
1371
 extends              = common_stm32
1371
 extends              = common_stm32
1372
-build_flags          = ${common_stm32.build_flags} -DMCU_STM32F103VE -DSS_TIMER=4 -DENABLE_HWSERIAL3 -DTRANSFER_CLOCK_DIV=8
1372
+build_flags          = ${common_stm32.build_flags} -DMCU_STM32F103VE -DSS_TIMER=4 -DENABLE_HWSERIAL3
1373
 board                = genericSTM32F103VE
1373
 board                = genericSTM32F103VE
1374
 board_build.core     = stm32
1374
 board_build.core     = stm32
1375
 board_build.variant  = MARLIN_F103Vx
1375
 board_build.variant  = MARLIN_F103Vx

Loading…
Cancel
Save