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.

Sd2Card_sdio_stm32duino.cpp 10KB

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