My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Sd2Card_sdio_stm32duino.cpp 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. //#include "SdMscDriver.h"
  27. //#include "usbd_msc_bot.h"
  28. //#include "usbd_msc_scsi.h"
  29. //#include "usbd_msc_composite.h"
  30. //#include "usbd_msc_cdc_composite.h"
  31. //#include "usbd_msc_data.h"
  32. #if defined(STM32F103xE) || defined(STM32F103xG)
  33. #include <stm32f1xx_hal_rcc_ex.h>
  34. #include <stm32f1xx_hal_sd.h>
  35. #elif defined(STM32F4xx)
  36. #include <stm32f4xx_hal_rcc.h>
  37. #include <stm32f4xx_hal_dma.h>
  38. #include <stm32f4xx_hal_gpio.h>
  39. #include <stm32f4xx_hal_sd.h>
  40. #elif defined(STM32F7xx)
  41. #include <stm32f7xx_hal_rcc.h>
  42. #include <stm32f7xx_hal_dma.h>
  43. #include <stm32f7xx_hal_gpio.h>
  44. #include <stm32f7xx_hal_sd.h>
  45. #else
  46. #error "ERROR - Only STM32F103xE, STM32F103xG, STM32F4xx or STM32F7xx CPUs supported"
  47. #endif
  48. SD_HandleTypeDef hsd; // create SDIO structure
  49. #define TRANSFER_CLOCK_DIV ((uint8_t)SDIO_INIT_CLK_DIV/40)
  50. #ifndef USBD_OK
  51. #define USBD_OK 0
  52. #endif
  53. void go_to_transfer_speed() {
  54. SD_InitTypeDef Init;
  55. /* Default SDIO peripheral configuration for SD card initialization */
  56. Init.ClockEdge = hsd.Init.ClockEdge;
  57. Init.ClockBypass = hsd.Init.ClockBypass;
  58. Init.ClockPowerSave = hsd.Init.ClockPowerSave;
  59. Init.BusWide = hsd.Init.BusWide;
  60. Init.HardwareFlowControl = hsd.Init.HardwareFlowControl;
  61. Init.ClockDiv = TRANSFER_CLOCK_DIV;
  62. /* Initialize SDIO peripheral interface with default configuration */
  63. SDIO_Init(hsd.Instance, Init);
  64. }
  65. void SD_LowLevel_Init(void) {
  66. uint32_t tempreg;
  67. GPIO_InitTypeDef GPIO_InitStruct;
  68. __HAL_RCC_GPIOC_CLK_ENABLE(); //enable GPIO clocks
  69. __HAL_RCC_GPIOD_CLK_ENABLE(); //enable GPIO clocks
  70. GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_12; // D0 & SCK
  71. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  72. GPIO_InitStruct.Pull = 1; //GPIO_NOPULL;
  73. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  74. GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
  75. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  76. #if defined(SDIO_D1_PIN) && defined(SDIO_D2_PIN) && defined(SDIO_D3_PIN) // define D1-D3 only if have a four bit wide SDIO bus
  77. GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11; // D1-D3
  78. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  79. GPIO_InitStruct.Pull = 1; //GPIO_NOPULL;
  80. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  81. GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
  82. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  83. #endif
  84. // Configure PD.02 CMD line
  85. GPIO_InitStruct.Pin = GPIO_PIN_2;
  86. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  87. RCC->APB2RSTR &= ~RCC_APB2RSTR_SDIORST_Msk; // take SDIO out of reset
  88. RCC->APB2ENR |= RCC_APB2RSTR_SDIORST_Msk; // enable SDIO clock
  89. // Enable the DMA2 Clock
  90. //Initialize the SDIO (with initial <400Khz Clock)
  91. tempreg = 0; //Reset value
  92. tempreg |= SDIO_CLKCR_CLKEN; //Clock is enabled
  93. tempreg |= (uint32_t)0x76; //Clock Divider. Clock = 48000/(118+2) = 400Khz
  94. //Keep the rest at 0 => HW_Flow Disabled, Rising Clock Edge, Disable CLK ByPass, Bus Width = 0, Power save Disable
  95. SDIO->CLKCR = tempreg;
  96. //Power up the SDIO
  97. SDIO->POWER = 0x03;
  98. }
  99. void HAL_SD_MspInit(SD_HandleTypeDef *hsd) { // application specific init
  100. UNUSED(hsd); /* Prevent unused argument(s) compilation warning */
  101. __HAL_RCC_SDIO_CLK_ENABLE(); // turn on SDIO clock
  102. }
  103. constexpr uint8_t SD_RETRY_COUNT = (1
  104. #if ENABLED(SD_CHECK_AND_RETRY)
  105. + 2
  106. #endif
  107. );
  108. bool SDIO_Init() {
  109. //init SDIO and get SD card info
  110. uint8_t retryCnt = SD_RETRY_COUNT;
  111. bool status;
  112. hsd.Instance = SDIO;
  113. hsd.State = (HAL_SD_StateTypeDef) 0; // HAL_SD_STATE_RESET
  114. SD_LowLevel_Init();
  115. uint8_t retry_Cnt = retryCnt;
  116. for (;;) {
  117. status = (bool) HAL_SD_Init(&hsd);
  118. if (!status) break;
  119. if (!--retry_Cnt) return false; // return failing status if retries are exhausted
  120. }
  121. go_to_transfer_speed();
  122. #if defined(SDIO_D1_PIN) && defined(SDIO_D2_PIN) && defined(SDIO_D3_PIN) // go to 4 bit wide mode if pins are defined
  123. retry_Cnt = retryCnt;
  124. for (;;) {
  125. if (!HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B)) break; // some cards are only 1 bit wide so a pass here is not required
  126. if (!--retry_Cnt) break;
  127. }
  128. if (!retry_Cnt) { // wide bus failed, go back to one bit wide mode
  129. hsd.State = (HAL_SD_StateTypeDef) 0; // HAL_SD_STATE_RESET
  130. SD_LowLevel_Init();
  131. retry_Cnt = retryCnt;
  132. for (;;) {
  133. status = (bool) HAL_SD_Init(&hsd);
  134. if (!status) break;
  135. if (!--retry_Cnt) return false; // return failing status if retries are exhausted
  136. }
  137. }
  138. #endif
  139. return true;
  140. }
  141. void init_SDIO_pins(void) {
  142. GPIO_InitTypeDef GPIO_InitStruct = {0};
  143. /**SDIO GPIO Configuration
  144. PC8 ------> SDIO_D0
  145. PC12 ------> SDIO_CK
  146. PD2 ------> SDIO_CMD
  147. */
  148. GPIO_InitStruct.Pin = GPIO_PIN_8;
  149. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  150. GPIO_InitStruct.Pull = GPIO_NOPULL;
  151. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  152. GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
  153. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  154. GPIO_InitStruct.Pin = GPIO_PIN_12;
  155. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  156. GPIO_InitStruct.Pull = GPIO_NOPULL;
  157. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  158. GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
  159. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  160. GPIO_InitStruct.Pin = GPIO_PIN_2;
  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(GPIOD, &GPIO_InitStruct);
  166. }
  167. //bool SDIO_init() { return (bool) (SD_SDIO_Init() ? 1 : 0);}
  168. //bool SDIO_Init_C() { return (bool) (SD_SDIO_Init() ? 1 : 0);}
  169. bool SDIO_ReadBlock(uint32_t block, uint8_t *dst) {
  170. bool status;
  171. hsd.Instance = SDIO;
  172. uint8_t retryCnt = SD_RETRY_COUNT;
  173. for (;;) {
  174. bool status = (bool) HAL_SD_ReadBlocks(&hsd, (uint8_t*)dst, block, 1, 1000); // read one 512 byte block with 500mS timeout
  175. status |= (bool) HAL_SD_GetCardState(&hsd); // make sure all is OK
  176. if (!status) return false; // return passing status
  177. if (!--retryCnt) return true; // return failing status if retries are exhausted
  178. }
  179. /*
  180. return (bool) ((status_read | status_card) ? 1 : 0);
  181. if (SDIO_GetCardState() != SDIO_CARD_TRANSFER) return false;
  182. if (blockAddress >= SdCard.LogBlockNbr) return false;
  183. if ((0x03 & (uint32_t)data)) return false; // misaligned data
  184. if (SdCard.CardType != CARD_SDHC_SDXC) { blockAddress *= 512U; }
  185. if (!SDIO_CmdReadSingleBlock(blockAddress)) {
  186. SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS);
  187. dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
  188. return false;
  189. }
  190. while (!SDIO_GET_FLAG(SDIO_STA_DATAEND | SDIO_STA_TRX_ERROR_FLAGS)) {}
  191. dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
  192. if (SDIO->STA & SDIO_STA_RXDAVL) {
  193. while (SDIO->STA & SDIO_STA_RXDAVL) (void)SDIO->FIFO;
  194. SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
  195. return false;
  196. }
  197. if (SDIO_GET_FLAG(SDIO_STA_TRX_ERROR_FLAGS)) {
  198. SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
  199. return false;
  200. }
  201. SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
  202. */
  203. return true;
  204. }
  205. bool SDIO_WriteBlock(uint32_t block, const uint8_t *src) {
  206. bool status;
  207. hsd.Instance = SDIO;
  208. uint8_t retryCnt = SD_RETRY_COUNT;
  209. for (;;) {
  210. status = (bool) HAL_SD_WriteBlocks(&hsd, (uint8_t*)src, block, 1, 500); // write one 512 byte block with 500mS timeout
  211. status |= (bool) HAL_SD_GetCardState(&hsd); // make sure all is OK
  212. if (!status) return (bool) status; // return passing status
  213. if (!--retryCnt) return (bool) status; // return failing status if retries are exhausted
  214. }
  215. }
  216. #endif // SDIO_SUPPORT