My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Sd2Card_sdio_stm32duino.cpp 11KB

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