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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 "../../inc/MarlinConfig.h"
  23. #if ENABLED(SDIO_SUPPORT) && !defined(STM32GENERIC)
  24. #include <stdint.h>
  25. #include <stdbool.h>
  26. #if NONE(STM32F103xE, STM32F103xG, STM32F4xx, STM32F7xx)
  27. #error "ERROR - Only STM32F103xE, STM32F103xG, STM32F4xx or STM32F7xx CPUs supported"
  28. #endif
  29. #ifdef USBD_USE_CDC_COMPOSITE
  30. // use USB drivers
  31. extern "C" { int8_t SD_MSC_Read(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
  32. int8_t SD_MSC_Write(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
  33. extern SD_HandleTypeDef hsd;
  34. }
  35. bool SDIO_Init() {
  36. return hsd.State == HAL_SD_STATE_READY; // return pass/fail status
  37. }
  38. bool SDIO_ReadBlock(uint32_t block, uint8_t *src) {
  39. int8_t status = SD_MSC_Read(0, (uint8_t*)src, block, 1); // read one 512 byte block
  40. return (bool) status;
  41. }
  42. bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) {
  43. int8_t status = SD_MSC_Write(0, (uint8_t*)src, block, 1); // write one 512 byte block
  44. return (bool) status;
  45. }
  46. #else // !USBD_USE_CDC_COMPOSITE
  47. // use local drivers
  48. #if defined(STM32F103xE) || defined(STM32F103xG)
  49. #include <stm32f1xx_hal_rcc_ex.h>
  50. #include <stm32f1xx_hal_sd.h>
  51. #elif defined(STM32F4xx)
  52. #include <stm32f4xx_hal_rcc.h>
  53. #include <stm32f4xx_hal_dma.h>
  54. #include <stm32f4xx_hal_gpio.h>
  55. #include <stm32f4xx_hal_sd.h>
  56. #elif defined(STM32F7xx)
  57. #include <stm32f7xx_hal_rcc.h>
  58. #include <stm32f7xx_hal_dma.h>
  59. #include <stm32f7xx_hal_gpio.h>
  60. #include <stm32f7xx_hal_sd.h>
  61. #else
  62. #error "ERROR - Only STM32F103xE, STM32F103xG, STM32F4xx or STM32F7xx CPUs supported"
  63. #endif
  64. SD_HandleTypeDef hsd; // create SDIO structure
  65. /*
  66. SDIO_INIT_CLK_DIV is 118
  67. SDIO clock frequency is 48MHz / (TRANSFER_CLOCK_DIV + 2)
  68. SDIO init clock frequency should not exceed 400KHz = 48MHz / (118 + 2)
  69. Default TRANSFER_CLOCK_DIV is 2 (118 / 40)
  70. Default SDIO clock frequency is 48MHz / (2 + 2) = 12 MHz
  71. This might be too fast for stable SDIO operations
  72. MKS Robin board seems to have stable SDIO with BusWide 1bit and ClockDiv 8 i.e. 4.8MHz SDIO clock frequency
  73. Additional testing is required as there are clearly some 4bit initialization problems
  74. Add -DTRANSFER_CLOCK_DIV=8 to build parameters to improve SDIO stability
  75. */
  76. #ifndef TRANSFER_CLOCK_DIV
  77. #define TRANSFER_CLOCK_DIV (uint8_t(SDIO_INIT_CLK_DIV) / 40)
  78. #endif
  79. #ifndef USBD_OK
  80. #define USBD_OK 0
  81. #endif
  82. void go_to_transfer_speed() {
  83. SD_InitTypeDef Init;
  84. /* Default SDIO peripheral configuration for SD card initialization */
  85. Init.ClockEdge = hsd.Init.ClockEdge;
  86. Init.ClockBypass = hsd.Init.ClockBypass;
  87. Init.ClockPowerSave = hsd.Init.ClockPowerSave;
  88. Init.BusWide = hsd.Init.BusWide;
  89. Init.HardwareFlowControl = hsd.Init.HardwareFlowControl;
  90. Init.ClockDiv = TRANSFER_CLOCK_DIV;
  91. /* Initialize SDIO peripheral interface with default configuration */
  92. SDIO_Init(hsd.Instance, Init);
  93. }
  94. void SD_LowLevel_Init(void) {
  95. uint32_t tempreg;
  96. __HAL_RCC_SDIO_CLK_ENABLE();
  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. #if DISABLED(STM32F1xx)
  116. // TODO: use __HAL_RCC_SDIO_RELEASE_RESET() and __HAL_RCC_SDIO_CLK_ENABLE();
  117. RCC->APB2RSTR &= ~RCC_APB2RSTR_SDIORST_Msk; // take SDIO out of reset
  118. RCC->APB2ENR |= RCC_APB2RSTR_SDIORST_Msk; // enable SDIO clock
  119. // Enable the DMA2 Clock
  120. #endif
  121. //Initialize the SDIO (with initial <400Khz Clock)
  122. tempreg = 0; //Reset value
  123. tempreg |= SDIO_CLKCR_CLKEN; // Clock enabled
  124. tempreg |= (uint32_t)0x76; // Clock Divider. Clock = 48000 / (118 + 2) = 400Khz
  125. // Keep the rest at 0 => HW_Flow Disabled, Rising Clock Edge, Disable CLK ByPass, Bus Width = 0, Power save Disable
  126. SDIO->CLKCR = tempreg;
  127. // Power up the SDIO
  128. SDIO->POWER = 0x03;
  129. }
  130. void HAL_SD_MspInit(SD_HandleTypeDef *hsd) { // application specific init
  131. UNUSED(hsd); /* Prevent unused argument(s) compilation warning */
  132. __HAL_RCC_SDIO_CLK_ENABLE(); // turn on SDIO clock
  133. }
  134. constexpr uint8_t SD_RETRY_COUNT = TERN(SD_CHECK_AND_RETRY, 3, 1);
  135. bool SDIO_Init() {
  136. //init SDIO and get SD card info
  137. uint8_t retryCnt = SD_RETRY_COUNT;
  138. bool status;
  139. hsd.Instance = SDIO;
  140. hsd.State = (HAL_SD_StateTypeDef) 0; // HAL_SD_STATE_RESET
  141. /*
  142. hsd.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
  143. hsd.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
  144. hsd.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
  145. hsd.Init.BusWide = SDIO_BUS_WIDE_1B;
  146. hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
  147. hsd.Init.ClockDiv = 8;
  148. */
  149. SD_LowLevel_Init();
  150. uint8_t retry_Cnt = retryCnt;
  151. for (;;) {
  152. TERN_(USE_WATCHDOG, HAL_watchdog_refresh());
  153. status = (bool) HAL_SD_Init(&hsd);
  154. if (!status) break;
  155. if (!--retry_Cnt) return false; // return failing status if retries are exhausted
  156. }
  157. go_to_transfer_speed();
  158. #if PINS_EXIST(SDIO_D1, SDIO_D2, SDIO_D3) // go to 4 bit wide mode if pins are defined
  159. retry_Cnt = retryCnt;
  160. for (;;) {
  161. TERN_(USE_WATCHDOG, HAL_watchdog_refresh());
  162. if (!HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B)) break; // some cards are only 1 bit wide so a pass here is not required
  163. if (!--retry_Cnt) break;
  164. }
  165. if (!retry_Cnt) { // wide bus failed, go back to one bit wide mode
  166. hsd.State = (HAL_SD_StateTypeDef) 0; // HAL_SD_STATE_RESET
  167. SD_LowLevel_Init();
  168. retry_Cnt = retryCnt;
  169. for (;;) {
  170. TERN_(USE_WATCHDOG, HAL_watchdog_refresh());
  171. status = (bool) HAL_SD_Init(&hsd);
  172. if (!status) break;
  173. if (!--retry_Cnt) return false; // return failing status if retries are exhausted
  174. }
  175. }
  176. #endif
  177. return true;
  178. }
  179. /*
  180. void init_SDIO_pins(void) {
  181. GPIO_InitTypeDef GPIO_InitStruct = {0};
  182. // SDIO GPIO Configuration
  183. // PC8 ------> SDIO_D0
  184. // PC12 ------> SDIO_CK
  185. // PD2 ------> SDIO_CMD
  186. GPIO_InitStruct.Pin = GPIO_PIN_8;
  187. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  188. GPIO_InitStruct.Pull = GPIO_NOPULL;
  189. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  190. GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
  191. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  192. GPIO_InitStruct.Pin = GPIO_PIN_12;
  193. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  194. GPIO_InitStruct.Pull = GPIO_NOPULL;
  195. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  196. GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
  197. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  198. GPIO_InitStruct.Pin = GPIO_PIN_2;
  199. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  200. GPIO_InitStruct.Pull = GPIO_NOPULL;
  201. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  202. GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
  203. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  204. }
  205. */
  206. //bool SDIO_init() { return (bool) (SD_SDIO_Init() ? 1 : 0);}
  207. //bool SDIO_Init_C() { return (bool) (SD_SDIO_Init() ? 1 : 0);}
  208. bool SDIO_ReadBlock(uint32_t block, uint8_t *dst) {
  209. hsd.Instance = SDIO;
  210. uint8_t retryCnt = SD_RETRY_COUNT;
  211. bool status;
  212. for (;;) {
  213. TERN_(USE_WATCHDOG, HAL_watchdog_refresh());
  214. status = (bool) HAL_SD_ReadBlocks(&hsd, (uint8_t*)dst, block, 1, 1000); // read one 512 byte block with 500mS timeout
  215. status |= (bool) HAL_SD_GetCardState(&hsd); // make sure all is OK
  216. if (!status) break; // return passing status
  217. if (!--retryCnt) break; // return failing status if retries are exhausted
  218. }
  219. return status;
  220. /*
  221. return (bool) ((status_read | status_card) ? 1 : 0);
  222. if (SDIO_GetCardState() != SDIO_CARD_TRANSFER) return false;
  223. if (blockAddress >= SdCard.LogBlockNbr) return false;
  224. if ((0x03 & (uint32_t)data)) return false; // misaligned data
  225. if (SdCard.CardType != CARD_SDHC_SDXC) { blockAddress *= 512U; }
  226. if (!SDIO_CmdReadSingleBlock(blockAddress)) {
  227. SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS);
  228. dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
  229. return false;
  230. }
  231. while (!SDIO_GET_FLAG(SDIO_STA_DATAEND | SDIO_STA_TRX_ERROR_FLAGS)) {}
  232. dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
  233. if (SDIO->STA & SDIO_STA_RXDAVL) {
  234. while (SDIO->STA & SDIO_STA_RXDAVL) (void)SDIO->FIFO;
  235. SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
  236. return false;
  237. }
  238. if (SDIO_GET_FLAG(SDIO_STA_TRX_ERROR_FLAGS)) {
  239. SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
  240. return false;
  241. }
  242. SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
  243. */
  244. return true;
  245. }
  246. bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) {
  247. hsd.Instance = SDIO;
  248. uint8_t retryCnt = SD_RETRY_COUNT;
  249. bool status;
  250. for (;;) {
  251. status = (bool) HAL_SD_WriteBlocks(&hsd, (uint8_t*)src, block, 1, 500); // write one 512 byte block with 500mS timeout
  252. status |= (bool) HAL_SD_GetCardState(&hsd); // make sure all is OK
  253. if (!status) break; // return passing status
  254. if (!--retryCnt) break; // return failing status if retries are exhausted
  255. }
  256. return status;
  257. }
  258. #endif // !USBD_USE_CDC_COMPOSITE
  259. #endif // SDIO_SUPPORT