My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sdio.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../platforms.h"
  23. #ifdef HAL_STM32
  24. #include "../../inc/MarlinConfig.h"
  25. #if ENABLED(SDIO_SUPPORT)
  26. #include "sdio.h"
  27. #include <stdint.h>
  28. #include <stdbool.h>
  29. #if defined(STM32F103xE) || defined(STM32F103xG)
  30. #include <stm32f1xx_hal_rcc_ex.h>
  31. #include <stm32f1xx_hal_sd.h>
  32. #elif defined(STM32F4xx)
  33. #include <stm32f4xx_hal_rcc.h>
  34. #include <stm32f4xx_hal_dma.h>
  35. #include <stm32f4xx_hal_gpio.h>
  36. #include <stm32f4xx_hal_sd.h>
  37. #elif defined(STM32F7xx)
  38. #include <stm32f7xx_hal_rcc.h>
  39. #include <stm32f7xx_hal_dma.h>
  40. #include <stm32f7xx_hal_gpio.h>
  41. #include <stm32f7xx_hal_sd.h>
  42. #elif defined(STM32H7xx)
  43. #define SDIO_FOR_STM32H7
  44. #include <stm32h7xx_hal_rcc.h>
  45. #include <stm32h7xx_hal_dma.h>
  46. #include <stm32h7xx_hal_gpio.h>
  47. #include <stm32h7xx_hal_sd.h>
  48. #else
  49. #error "SDIO is only supported with STM32F103xE, STM32F103xG, STM32F4xx, STM32F7xx, and STM32H7xx."
  50. #endif
  51. // SDIO Max Clock (naming from STM Manual, don't change)
  52. #define SDIOCLK 48000000
  53. // Target Clock, configurable. Default is 18MHz, from STM32F1
  54. #ifndef SDIO_CLOCK
  55. #define SDIO_CLOCK 18000000 // 18 MHz
  56. #endif
  57. SD_HandleTypeDef hsd; // SDIO structure
  58. static uint32_t clock_to_divider(uint32_t clk) {
  59. #ifdef SDIO_FOR_STM32H7
  60. // SDMMC_CK frequency = sdmmc_ker_ck / [2 * CLKDIV].
  61. uint32_t sdmmc_clk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SDMMC);
  62. return sdmmc_clk / (2U * SDIO_CLOCK) + (sdmmc_clk % (2U * SDIO_CLOCK) != 0);
  63. #else
  64. // limit the SDIO master clock to 8/3 of PCLK2. See STM32 Manuals
  65. // Also limited to no more than 48Mhz (SDIOCLK).
  66. const uint32_t pclk2 = HAL_RCC_GetPCLK2Freq();
  67. clk = min(clk, (uint32_t)(pclk2 * 8 / 3));
  68. clk = min(clk, (uint32_t)SDIOCLK);
  69. // Round up divider, so we don't run the card over the speed supported,
  70. // and subtract by 2, because STM32 will add 2, as written in the manual:
  71. // SDIO_CK frequency = SDIOCLK / [CLKDIV + 2]
  72. return pclk2 / clk + (pclk2 % clk != 0) - 2;
  73. #endif
  74. }
  75. // Start the SDIO clock
  76. void HAL_SD_MspInit(SD_HandleTypeDef *hsd) {
  77. UNUSED(hsd);
  78. #ifdef SDIO_FOR_STM32H7
  79. pinmap_pinout(PC_12, PinMap_SD);
  80. pinmap_pinout(PD_2, PinMap_SD);
  81. pinmap_pinout(PC_8, PinMap_SD);
  82. #if PINS_EXIST(SDIO_D1, SDIO_D2, SDIO_D3) // Define D1-D3 only for 4-bit wide SDIO bus
  83. pinmap_pinout(PC_9, PinMap_SD);
  84. pinmap_pinout(PC_10, PinMap_SD);
  85. pinmap_pinout(PC_11, PinMap_SD);
  86. #endif
  87. __HAL_RCC_SDMMC1_CLK_ENABLE();
  88. HAL_NVIC_EnableIRQ(SDMMC1_IRQn);
  89. #else
  90. __HAL_RCC_SDIO_CLK_ENABLE();
  91. #endif
  92. }
  93. #ifdef SDIO_FOR_STM32H7
  94. #define SD_TIMEOUT 1000 // ms
  95. extern "C" void SDMMC1_IRQHandler(void) { HAL_SD_IRQHandler(&hsd); }
  96. uint8_t waitingRxCplt = 0, waitingTxCplt = 0;
  97. void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsdio) { waitingTxCplt = 0; }
  98. void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsdio) { waitingRxCplt = 0; }
  99. void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd) {
  100. __HAL_RCC_SDMMC1_FORCE_RESET(); delay(10);
  101. __HAL_RCC_SDMMC1_RELEASE_RESET(); delay(10);
  102. }
  103. bool SDIO_Init() {
  104. HAL_StatusTypeDef sd_state = HAL_OK;
  105. if (hsd.Instance == SDMMC1) HAL_SD_DeInit(&hsd);
  106. // HAL SD initialization
  107. hsd.Instance = SDMMC1;
  108. hsd.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
  109. hsd.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
  110. hsd.Init.BusWide = SDMMC_BUS_WIDE_1B;
  111. hsd.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
  112. hsd.Init.ClockDiv = clock_to_divider(SDIO_CLOCK);
  113. sd_state = HAL_SD_Init(&hsd);
  114. #if PINS_EXIST(SDIO_D1, SDIO_D2, SDIO_D3)
  115. if (sd_state == HAL_OK)
  116. sd_state = HAL_SD_ConfigWideBusOperation(&hsd, SDMMC_BUS_WIDE_4B);
  117. #endif
  118. return (sd_state == HAL_OK);
  119. }
  120. #else // !SDIO_FOR_STM32H7
  121. #define SD_TIMEOUT 500 // ms
  122. // SDIO retries, configurable. Default is 3, from STM32F1
  123. #ifndef SDIO_READ_RETRIES
  124. #define SDIO_READ_RETRIES 3
  125. #endif
  126. // F4 supports one DMA for RX and another for TX, but Marlin will never
  127. // do read and write at same time, so we use the same DMA for both.
  128. DMA_HandleTypeDef hdma_sdio;
  129. #ifdef STM32F1xx
  130. #define DMA_IRQ_HANDLER DMA2_Channel4_5_IRQHandler
  131. #elif defined(STM32F4xx)
  132. #define DMA_IRQ_HANDLER DMA2_Stream3_IRQHandler
  133. #else
  134. #error "Unknown STM32 architecture."
  135. #endif
  136. extern "C" void SDIO_IRQHandler(void) { HAL_SD_IRQHandler(&hsd); }
  137. extern "C" void DMA_IRQ_HANDLER(void) { HAL_DMA_IRQHandler(&hdma_sdio); }
  138. /*
  139. SDIO_INIT_CLK_DIV is 118
  140. SDIO clock frequency is 48MHz / (TRANSFER_CLOCK_DIV + 2)
  141. SDIO init clock frequency should not exceed 400kHz = 48MHz / (118 + 2)
  142. Default TRANSFER_CLOCK_DIV is 2 (118 / 40)
  143. Default SDIO clock frequency is 48MHz / (2 + 2) = 12 MHz
  144. This might be too fast for stable SDIO operations
  145. MKS Robin SDIO seems stable with BusWide 1bit and ClockDiv 8 (i.e., 4.8MHz SDIO clock frequency)
  146. More testing is required as there are clearly some 4bit init problems.
  147. */
  148. void go_to_transfer_speed() {
  149. /* Default SDIO peripheral configuration for SD card initialization */
  150. hsd.Init.ClockEdge = hsd.Init.ClockEdge;
  151. hsd.Init.ClockBypass = hsd.Init.ClockBypass;
  152. hsd.Init.ClockPowerSave = hsd.Init.ClockPowerSave;
  153. hsd.Init.BusWide = hsd.Init.BusWide;
  154. hsd.Init.HardwareFlowControl = hsd.Init.HardwareFlowControl;
  155. hsd.Init.ClockDiv = clock_to_divider(SDIO_CLOCK);
  156. /* Initialize SDIO peripheral interface with default configuration */
  157. SDIO_Init(hsd.Instance, hsd.Init);
  158. }
  159. void SD_LowLevel_Init() {
  160. uint32_t tempreg;
  161. // Enable GPIO clocks
  162. __HAL_RCC_GPIOC_CLK_ENABLE();
  163. __HAL_RCC_GPIOD_CLK_ENABLE();
  164. GPIO_InitTypeDef GPIO_InitStruct;
  165. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  166. GPIO_InitStruct.Pull = 1; // GPIO_NOPULL
  167. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  168. #if DISABLED(STM32F1xx)
  169. GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
  170. #endif
  171. GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_12; // D0 & SCK
  172. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  173. #if PINS_EXIST(SDIO_D1, SDIO_D2, SDIO_D3) // define D1-D3 only if have a four bit wide SDIO bus
  174. GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11; // D1-D3
  175. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  176. #endif
  177. // Configure PD.02 CMD line
  178. GPIO_InitStruct.Pin = GPIO_PIN_2;
  179. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  180. // Setup DMA
  181. #ifdef STM32F1xx
  182. hdma_sdio.Init.Mode = DMA_NORMAL;
  183. hdma_sdio.Instance = DMA2_Channel4;
  184. HAL_NVIC_EnableIRQ(DMA2_Channel4_5_IRQn);
  185. #elif defined(STM32F4xx)
  186. hdma_sdio.Init.Mode = DMA_PFCTRL;
  187. hdma_sdio.Instance = DMA2_Stream3;
  188. hdma_sdio.Init.Channel = DMA_CHANNEL_4;
  189. hdma_sdio.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
  190. hdma_sdio.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  191. hdma_sdio.Init.MemBurst = DMA_MBURST_INC4;
  192. hdma_sdio.Init.PeriphBurst = DMA_PBURST_INC4;
  193. HAL_NVIC_EnableIRQ(DMA2_Stream3_IRQn);
  194. #endif
  195. HAL_NVIC_EnableIRQ(SDIO_IRQn);
  196. hdma_sdio.Init.PeriphInc = DMA_PINC_DISABLE;
  197. hdma_sdio.Init.MemInc = DMA_MINC_ENABLE;
  198. hdma_sdio.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
  199. hdma_sdio.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
  200. hdma_sdio.Init.Priority = DMA_PRIORITY_LOW;
  201. __HAL_LINKDMA(&hsd, hdmarx, hdma_sdio);
  202. __HAL_LINKDMA(&hsd, hdmatx, hdma_sdio);
  203. #ifdef STM32F1xx
  204. __HAL_RCC_SDIO_CLK_ENABLE();
  205. __HAL_RCC_DMA2_CLK_ENABLE();
  206. #else
  207. __HAL_RCC_SDIO_FORCE_RESET(); delay(2);
  208. __HAL_RCC_SDIO_RELEASE_RESET(); delay(2);
  209. __HAL_RCC_SDIO_CLK_ENABLE();
  210. __HAL_RCC_DMA2_FORCE_RESET(); delay(2);
  211. __HAL_RCC_DMA2_RELEASE_RESET(); delay(2);
  212. __HAL_RCC_DMA2_CLK_ENABLE();
  213. #endif
  214. // Initialize the SDIO (with initial <400Khz Clock)
  215. tempreg = 0 // Reset value
  216. | SDIO_CLKCR_CLKEN // Clock enabled
  217. | SDIO_INIT_CLK_DIV; // Clock Divider. Clock = 48000 / (118 + 2) = 400Khz
  218. // Keep the rest at 0 => HW_Flow Disabled, Rising Clock Edge, Disable CLK ByPass, Bus Width = 0, Power save Disable
  219. SDIO->CLKCR = tempreg;
  220. // Power up the SDIO
  221. SDIO_PowerState_ON(SDIO);
  222. hsd.Instance = SDIO;
  223. }
  224. bool SDIO_Init() {
  225. uint8_t retryCnt = SDIO_READ_RETRIES;
  226. bool status;
  227. hsd.Instance = SDIO;
  228. hsd.State = HAL_SD_STATE_RESET;
  229. SD_LowLevel_Init();
  230. uint8_t retry_Cnt = retryCnt;
  231. for (;;) {
  232. hal.watchdog_refresh();
  233. status = (bool) HAL_SD_Init(&hsd);
  234. if (!status) break;
  235. if (!--retry_Cnt) return false; // return failing status if retries are exhausted
  236. }
  237. go_to_transfer_speed();
  238. #if PINS_EXIST(SDIO_D1, SDIO_D2, SDIO_D3) // go to 4 bit wide mode if pins are defined
  239. retry_Cnt = retryCnt;
  240. for (;;) {
  241. hal.watchdog_refresh();
  242. if (!HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B)) break; // some cards are only 1 bit wide so a pass here is not required
  243. if (!--retry_Cnt) break;
  244. }
  245. if (!retry_Cnt) { // wide bus failed, go back to one bit wide mode
  246. hsd.State = (HAL_SD_StateTypeDef) 0; // HAL_SD_STATE_RESET
  247. SD_LowLevel_Init();
  248. retry_Cnt = retryCnt;
  249. for (;;) {
  250. hal.watchdog_refresh();
  251. status = (bool) HAL_SD_Init(&hsd);
  252. if (!status) break;
  253. if (!--retry_Cnt) return false; // return failing status if retries are exhausted
  254. }
  255. go_to_transfer_speed();
  256. }
  257. #endif
  258. return true;
  259. }
  260. /**
  261. * @brief Read or Write a block
  262. * @details Read or Write a block with SDIO
  263. *
  264. * @param block The block index
  265. * @param src The data buffer source for a write
  266. * @param dst The data buffer destination for a read
  267. *
  268. * @return true on success
  269. */
  270. static bool SDIO_ReadWriteBlock_DMA(uint32_t block, const uint8_t *src, uint8_t *dst) {
  271. if (HAL_SD_GetCardState(&hsd) != HAL_SD_CARD_TRANSFER) return false;
  272. hal.watchdog_refresh();
  273. HAL_StatusTypeDef ret;
  274. if (src) {
  275. hdma_sdio.Init.Direction = DMA_MEMORY_TO_PERIPH;
  276. HAL_DMA_Init(&hdma_sdio);
  277. ret = HAL_SD_WriteBlocks_DMA(&hsd, (uint8_t*)src, block, 1);
  278. }
  279. else {
  280. hdma_sdio.Init.Direction = DMA_PERIPH_TO_MEMORY;
  281. HAL_DMA_Init(&hdma_sdio);
  282. ret = HAL_SD_ReadBlocks_DMA(&hsd, (uint8_t*)dst, block, 1);
  283. }
  284. if (ret != HAL_OK) {
  285. HAL_DMA_Abort_IT(&hdma_sdio);
  286. HAL_DMA_DeInit(&hdma_sdio);
  287. return false;
  288. }
  289. millis_t timeout = millis() + SD_TIMEOUT;
  290. // Wait the transfer
  291. while (hsd.State != HAL_SD_STATE_READY) {
  292. if (ELAPSED(millis(), timeout)) {
  293. HAL_DMA_Abort_IT(&hdma_sdio);
  294. HAL_DMA_DeInit(&hdma_sdio);
  295. return false;
  296. }
  297. }
  298. while (__HAL_DMA_GET_FLAG(&hdma_sdio, __HAL_DMA_GET_TC_FLAG_INDEX(&hdma_sdio)) != 0
  299. || __HAL_DMA_GET_FLAG(&hdma_sdio, __HAL_DMA_GET_TE_FLAG_INDEX(&hdma_sdio)) != 0) { /* nada */ }
  300. HAL_DMA_Abort_IT(&hdma_sdio);
  301. HAL_DMA_DeInit(&hdma_sdio);
  302. timeout = millis() + SD_TIMEOUT;
  303. while (HAL_SD_GetCardState(&hsd) != HAL_SD_CARD_TRANSFER) if (ELAPSED(millis(), timeout)) return false;
  304. return true;
  305. }
  306. #endif // !SDIO_FOR_STM32H7
  307. /**
  308. * @brief Read a block
  309. * @details Read a block from media with SDIO
  310. *
  311. * @param block The block index
  312. * @param src The block buffer
  313. *
  314. * @return true on success
  315. */
  316. bool SDIO_ReadBlock(uint32_t block, uint8_t *dst) {
  317. #ifdef SDIO_FOR_STM32H7
  318. uint32_t timeout = HAL_GetTick() + SD_TIMEOUT;
  319. while (HAL_SD_GetCardState(&hsd) != HAL_SD_CARD_TRANSFER)
  320. if (HAL_GetTick() >= timeout) return false;
  321. waitingRxCplt = 1;
  322. if (HAL_SD_ReadBlocks_DMA(&hsd, (uint8_t*)dst, block, 1) != HAL_OK)
  323. return false;
  324. timeout = HAL_GetTick() + SD_TIMEOUT;
  325. while (waitingRxCplt)
  326. if (HAL_GetTick() >= timeout) return false;
  327. return true;
  328. #else
  329. uint8_t retries = SDIO_READ_RETRIES;
  330. while (retries--) if (SDIO_ReadWriteBlock_DMA(block, nullptr, dst)) return true;
  331. return false;
  332. #endif
  333. }
  334. /**
  335. * @brief Write a block
  336. * @details Write a block to media with SDIO
  337. *
  338. * @param block The block index
  339. * @param src The block data
  340. *
  341. * @return true on success
  342. */
  343. bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) {
  344. #ifdef SDIO_FOR_STM32H7
  345. uint32_t timeout = HAL_GetTick() + SD_TIMEOUT;
  346. while (HAL_SD_GetCardState(&hsd) != HAL_SD_CARD_TRANSFER)
  347. if (HAL_GetTick() >= timeout) return false;
  348. waitingTxCplt = 1;
  349. if (HAL_SD_WriteBlocks_DMA(&hsd, (uint8_t*)src, block, 1) != HAL_OK)
  350. return false;
  351. timeout = HAL_GetTick() + SD_TIMEOUT;
  352. while (waitingTxCplt)
  353. if (HAL_GetTick() >= timeout) return false;
  354. return true;
  355. #else
  356. uint8_t retries = SDIO_READ_RETRIES;
  357. while (retries--) if (SDIO_ReadWriteBlock_DMA(block, src, nullptr)) return true;
  358. return false;
  359. #endif
  360. }
  361. bool SDIO_IsReady() {
  362. return hsd.State == HAL_SD_STATE_READY;
  363. }
  364. uint32_t SDIO_GetCardSize() {
  365. return (uint32_t)(hsd.SdCard.BlockNbr) * (hsd.SdCard.BlockSize);
  366. }
  367. #endif // SDIO_SUPPORT
  368. #endif // HAL_STM32