My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Sd2Card_sdio_stm32duino.cpp 9.6KB

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