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 9.9KB

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