My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Sd2Card_sdio_stm32duino.cpp 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #define TRANSFER_CLOCK_DIV (uint8_t(SDIO_INIT_CLK_DIV) / 40)
  66. #ifndef USBD_OK
  67. #define USBD_OK 0
  68. #endif
  69. void go_to_transfer_speed() {
  70. SD_InitTypeDef Init;
  71. /* Default SDIO peripheral configuration for SD card initialization */
  72. Init.ClockEdge = hsd.Init.ClockEdge;
  73. Init.ClockBypass = hsd.Init.ClockBypass;
  74. Init.ClockPowerSave = hsd.Init.ClockPowerSave;
  75. Init.BusWide = hsd.Init.BusWide;
  76. Init.HardwareFlowControl = hsd.Init.HardwareFlowControl;
  77. Init.ClockDiv = TRANSFER_CLOCK_DIV;
  78. /* Initialize SDIO peripheral interface with default configuration */
  79. SDIO_Init(hsd.Instance, Init);
  80. }
  81. void SD_LowLevel_Init(void) {
  82. uint32_t tempreg;
  83. GPIO_InitTypeDef GPIO_InitStruct;
  84. __HAL_RCC_GPIOC_CLK_ENABLE(); //enable GPIO clocks
  85. __HAL_RCC_GPIOD_CLK_ENABLE(); //enable GPIO clocks
  86. GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_12; // D0 & SCK
  87. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  88. GPIO_InitStruct.Pull = 1; //GPIO_NOPULL;
  89. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  90. GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
  91. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  92. #if PINS_EXIST(SDIO_D1, SDIO_D2, SDIO_D3) // define D1-D3 only if have a four bit wide SDIO bus
  93. GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11; // D1-D3
  94. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  95. GPIO_InitStruct.Pull = 1; // GPIO_NOPULL;
  96. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  97. GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
  98. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  99. #endif
  100. // Configure PD.02 CMD line
  101. GPIO_InitStruct.Pin = GPIO_PIN_2;
  102. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  103. RCC->APB2RSTR &= ~RCC_APB2RSTR_SDIORST_Msk; // take SDIO out of reset
  104. RCC->APB2ENR |= RCC_APB2RSTR_SDIORST_Msk; // enable SDIO clock
  105. // Enable the DMA2 Clock
  106. //Initialize the SDIO (with initial <400Khz Clock)
  107. tempreg = 0; //Reset value
  108. tempreg |= SDIO_CLKCR_CLKEN; // Clock enabled
  109. tempreg |= (uint32_t)0x76; // Clock Divider. Clock = 48000 / (118 + 2) = 400Khz
  110. // Keep the rest at 0 => HW_Flow Disabled, Rising Clock Edge, Disable CLK ByPass, Bus Width = 0, Power save Disable
  111. SDIO->CLKCR = tempreg;
  112. // Power up the SDIO
  113. SDIO->POWER = 0x03;
  114. }
  115. void HAL_SD_MspInit(SD_HandleTypeDef *hsd) { // application specific init
  116. UNUSED(hsd); /* Prevent unused argument(s) compilation warning */
  117. __HAL_RCC_SDIO_CLK_ENABLE(); // turn on SDIO clock
  118. }
  119. constexpr uint8_t SD_RETRY_COUNT = TERN(SD_CHECK_AND_RETRY, 3, 1);
  120. bool SDIO_Init() {
  121. //init SDIO and get SD card info
  122. uint8_t retryCnt = SD_RETRY_COUNT;
  123. bool status;
  124. hsd.Instance = SDIO;
  125. hsd.State = (HAL_SD_StateTypeDef) 0; // HAL_SD_STATE_RESET
  126. SD_LowLevel_Init();
  127. uint8_t retry_Cnt = retryCnt;
  128. for (;;) {
  129. status = (bool) HAL_SD_Init(&hsd);
  130. if (!status) break;
  131. if (!--retry_Cnt) return false; // return failing status if retries are exhausted
  132. }
  133. go_to_transfer_speed();
  134. #if PINS_EXIST(SDIO_D1, SDIO_D2, SDIO_D3) // go to 4 bit wide mode if pins are defined
  135. retry_Cnt = retryCnt;
  136. for (;;) {
  137. if (!HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B)) break; // some cards are only 1 bit wide so a pass here is not required
  138. if (!--retry_Cnt) break;
  139. }
  140. if (!retry_Cnt) { // wide bus failed, go back to one bit wide mode
  141. hsd.State = (HAL_SD_StateTypeDef) 0; // HAL_SD_STATE_RESET
  142. SD_LowLevel_Init();
  143. retry_Cnt = retryCnt;
  144. for (;;) {
  145. status = (bool) HAL_SD_Init(&hsd);
  146. if (!status) break;
  147. if (!--retry_Cnt) return false; // return failing status if retries are exhausted
  148. }
  149. }
  150. #endif
  151. return true;
  152. }
  153. void init_SDIO_pins(void) {
  154. GPIO_InitTypeDef GPIO_InitStruct = {0};
  155. /**SDIO GPIO Configuration
  156. PC8 ------> SDIO_D0
  157. PC12 ------> SDIO_CK
  158. PD2 ------> SDIO_CMD
  159. */
  160. GPIO_InitStruct.Pin = GPIO_PIN_8;
  161. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  162. GPIO_InitStruct.Pull = GPIO_NOPULL;
  163. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  164. GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
  165. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  166. GPIO_InitStruct.Pin = GPIO_PIN_12;
  167. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  168. GPIO_InitStruct.Pull = GPIO_NOPULL;
  169. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  170. GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
  171. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  172. GPIO_InitStruct.Pin = GPIO_PIN_2;
  173. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  174. GPIO_InitStruct.Pull = GPIO_NOPULL;
  175. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  176. GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
  177. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  178. }
  179. //bool SDIO_init() { return (bool) (SD_SDIO_Init() ? 1 : 0);}
  180. //bool SDIO_Init_C() { return (bool) (SD_SDIO_Init() ? 1 : 0);}
  181. bool SDIO_ReadBlock(uint32_t block, uint8_t *dst) {
  182. hsd.Instance = SDIO;
  183. uint8_t retryCnt = SD_RETRY_COUNT;
  184. bool status;
  185. for (;;) {
  186. status = (bool) HAL_SD_ReadBlocks(&hsd, (uint8_t*)dst, block, 1, 1000); // read one 512 byte block with 500mS timeout
  187. status |= (bool) HAL_SD_GetCardState(&hsd); // make sure all is OK
  188. if (!status) break; // return passing status
  189. if (!--retryCnt) break; // return failing status if retries are exhausted
  190. }
  191. return status;
  192. /*
  193. return (bool) ((status_read | status_card) ? 1 : 0);
  194. if (SDIO_GetCardState() != SDIO_CARD_TRANSFER) return false;
  195. if (blockAddress >= SdCard.LogBlockNbr) return false;
  196. if ((0x03 & (uint32_t)data)) return false; // misaligned data
  197. if (SdCard.CardType != CARD_SDHC_SDXC) { blockAddress *= 512U; }
  198. if (!SDIO_CmdReadSingleBlock(blockAddress)) {
  199. SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS);
  200. dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
  201. return false;
  202. }
  203. while (!SDIO_GET_FLAG(SDIO_STA_DATAEND | SDIO_STA_TRX_ERROR_FLAGS)) {}
  204. dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
  205. if (SDIO->STA & SDIO_STA_RXDAVL) {
  206. while (SDIO->STA & SDIO_STA_RXDAVL) (void)SDIO->FIFO;
  207. SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
  208. return false;
  209. }
  210. if (SDIO_GET_FLAG(SDIO_STA_TRX_ERROR_FLAGS)) {
  211. SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
  212. return false;
  213. }
  214. SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
  215. */
  216. return true;
  217. }
  218. bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) {
  219. hsd.Instance = SDIO;
  220. uint8_t retryCnt = SD_RETRY_COUNT;
  221. bool status;
  222. for (;;) {
  223. status = (bool) HAL_SD_WriteBlocks(&hsd, (uint8_t*)src, block, 1, 500); // write one 512 byte block with 500mS timeout
  224. status |= (bool) HAL_SD_GetCardState(&hsd); // make sure all is OK
  225. if (!status) break; // return passing status
  226. if (!--retryCnt) break; // return failing status if retries are exhausted
  227. }
  228. return status;
  229. }
  230. #endif // !USBD_USE_CDC_COMPOSITE
  231. #endif // SDIO_SUPPORT