2020c.diff 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. diff --git a/STM32CubeMX/2020c/Src/fsmc.c b/STM32CubeMX/2020c/Src/fsmc.c
  2. index 03a1b12..1b01446 100644
  3. --- a/STM32CubeMX/2020c/Src/fsmc.c
  4. +++ b/STM32CubeMX/2020c/Src/fsmc.c
  5. @@ -50,12 +50,28 @@ void MX_FSMC_Init(void)
  6. hsram1.Init.AsynchronousWait = FSMC_ASYNCHRONOUS_WAIT_DISABLE;
  7. hsram1.Init.WriteBurst = FSMC_WRITE_BURST_DISABLE;
  8. /* Timing */
  9. +
  10. + // 1 clock to read the address, + 1 for synchroniser skew
  11. Timing.AddressSetupTime = 2;
  12. Timing.AddressHoldTime = 1;
  13. +
  14. + // Writes to device:
  15. + // 1 for synchroniser skew (dbx also delayed)
  16. + // 1 to skip hold time
  17. + // 1 to write data.
  18. +
  19. + // Reads from device:
  20. + // 3 for syncroniser
  21. + // 1 to write back to fsmc bus.
  22. Timing.DataSetupTime = 4;
  23. +
  24. + // Allow a clock for us to release signals
  25. + // Need to avoid both devices acting as outputs
  26. + // on the multiplexed lines at the same time.
  27. Timing.BusTurnAroundDuration = 1;
  28. - Timing.CLKDivision = 16;
  29. - Timing.DataLatency = 17;
  30. +
  31. + Timing.CLKDivision = 16; // Ignored for async
  32. + Timing.DataLatency = 17; // Ignored for async
  33. Timing.AccessMode = FSMC_ACCESS_MODE_A;
  34. /* ExtTiming */
  35. @@ -105,6 +121,10 @@ static void HAL_FSMC_MspInit(void){
  36. PE0 ------> FSMC_NBL0
  37. PE1 ------> FSMC_NBL1
  38. */
  39. +
  40. + // MM: GPIO_SPEED_FREQ_MEDIUM is rated up to 50MHz, which is fine as all the
  41. + // fsmc timings are > 1 (ie. so clock speed / 2 is around 50MHz).
  42. +
  43. /* GPIO_InitStruct */
  44. GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10
  45. |GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
  46. diff --git a/STM32CubeMX/2020c/Src/sdio.c b/STM32CubeMX/2020c/Src/sdio.c
  47. index f2a0b7c..a00c6a8 100644
  48. --- a/STM32CubeMX/2020c/Src/sdio.c
  49. +++ b/STM32CubeMX/2020c/Src/sdio.c
  50. @@ -40,6 +40,8 @@ void MX_SDIO_SD_Init(void)
  51. hsd.Init.BusWide = SDIO_BUS_WIDE_1B;
  52. hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
  53. hsd.Init.ClockDiv = 0;
  54. +
  55. + /*
  56. if (HAL_SD_Init(&hsd) != HAL_OK)
  57. {
  58. Error_Handler();
  59. @@ -47,8 +49,7 @@ void MX_SDIO_SD_Init(void)
  60. if (HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B) != HAL_OK)
  61. {
  62. Error_Handler();
  63. - }
  64. -
  65. + }*/
  66. }
  67. void HAL_SD_MspInit(SD_HandleTypeDef* sdHandle)
  68. diff --git a/STM32CubeMX/2020c/Src/spi.c b/STM32CubeMX/2020c/Src/spi.c
  69. index 902bdb2..4935bf0 100644
  70. --- a/STM32CubeMX/2020c/Src/spi.c
  71. +++ b/STM32CubeMX/2020c/Src/spi.c
  72. @@ -37,6 +37,8 @@ void MX_SPI1_Init(void)
  73. hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
  74. hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
  75. hspi1.Init.NSS = SPI_NSS_SOFT;
  76. +
  77. + // 13.5Mbaud FPGA device allows up to 25MHz write
  78. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  79. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  80. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  81. diff --git a/STM32CubeMX/2020c/Src/usbd_conf.c b/STM32CubeMX/2020c/Src/usbd_conf.c
  82. index eee1fd8..9567a95 100644
  83. --- a/STM32CubeMX/2020c/Src/usbd_conf.c
  84. +++ b/STM32CubeMX/2020c/Src/usbd_conf.c
  85. @@ -458,9 +458,12 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
  86. HAL_PCD_RegisterIsoOutIncpltCallback(&hpcd_USB_OTG_FS, PCD_ISOOUTIncompleteCallback);
  87. HAL_PCD_RegisterIsoInIncpltCallback(&hpcd_USB_OTG_FS, PCD_ISOINIncompleteCallback);
  88. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  89. +
  90. + // Combined RX + TX fifo of 0x140 4-byte words (1280 bytes)
  91. HAL_PCDEx_SetRxFiFo(&hpcd_USB_OTG_FS, 0x80);
  92. HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_FS, 0, 0x40);
  93. - HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_FS, 1, 0x80);
  94. + HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_FS, 1, 0x40);
  95. + HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_FS, 2, 0x40);
  96. }
  97. if (pdev->id == DEVICE_HS) {
  98. /* Link the driver to the stack. */
  99. @@ -497,9 +500,15 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
  100. HAL_PCD_RegisterIsoOutIncpltCallback(&hpcd_USB_OTG_HS, PCD_ISOOUTIncompleteCallback);
  101. HAL_PCD_RegisterIsoInIncpltCallback(&hpcd_USB_OTG_HS, PCD_ISOINIncompleteCallback);
  102. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  103. + // Combined RX + TX fifo of 0x400 4-byte words (4096 bytes)
  104. HAL_PCDEx_SetRxFiFo(&hpcd_USB_OTG_HS, 0x200);
  105. - HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 0, 0x80);
  106. - HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 1, 0x174);
  107. + HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 0, 0x40);
  108. +
  109. +// HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 1, 0x100);
  110. +// HOst requests 7 sectors, which is an odd number and doesn't fill the
  111. +// fifo, looks like it doesn't complete in this case !!!!
  112. + HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 1, 0x80); // 512 bytes
  113. + HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 2, 0x40);
  114. }
  115. return USBD_OK;
  116. }
  117. diff --git a/STM32CubeMX/2020c/Drivers/STM32F2xx_HAL_Driver/Inc/stm32f2xx_hal_sd.h b/STM32CubeMX/2020c/Drivers/STM32F2xx_HAL_Driver/Inc/stm32f2xx_hal_sd.h
  118. index a4317e4..7165538 100644
  119. --- a/STM32CubeMX/2020c/Drivers/STM32F2xx_HAL_Driver/Inc/stm32f2xx_hal_sd.h
  120. +++ b/STM32CubeMX/2020c/Drivers/STM32F2xx_HAL_Driver/Inc/stm32f2xx_hal_sd.h
  121. @@ -614,7 +614,8 @@ HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, ui
  122. HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks);
  123. /* Non-Blocking mode: DMA */
  124. HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks);
  125. -HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks);
  126. +HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t BlockAdd, uint32_t NumberOfBlocks);
  127. +HAL_StatusTypeDef HAL_SD_WriteBlocks_Data(SD_HandleTypeDef *hsd, uint8_t *pData);
  128. void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd);
  129. diff --git a/STM32CubeMX/2020c/Drivers/STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_sdmmc.h b/STM32CubeMX/2020c/Drivers/STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_sdmmc.h
  130. index 181b4b7..d71c37b 100644
  131. --- a/STM32CubeMX/2020c/Drivers/STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_sdmmc.h
  132. +++ b/STM32CubeMX/2020c/Drivers/STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_sdmmc.h
  133. @@ -1064,6 +1064,7 @@ uint32_t SDMMC_CmdReadSingleBlock(SDIO_TypeDef *SDIOx, uint32_t ReadAdd);
  134. uint32_t SDMMC_CmdReadMultiBlock(SDIO_TypeDef *SDIOx, uint32_t ReadAdd);
  135. uint32_t SDMMC_CmdWriteSingleBlock(SDIO_TypeDef *SDIOx, uint32_t WriteAdd);
  136. uint32_t SDMMC_CmdWriteMultiBlock(SDIO_TypeDef *SDIOx, uint32_t WriteAdd);
  137. +uint32_t SDMMC_CmdSetBlockCount(SDIO_TypeDef *SDIOx, uint32_t appCmdArg, uint32_t blockCount);
  138. uint32_t SDMMC_CmdEraseStartAdd(SDIO_TypeDef *SDIOx, uint32_t StartAdd);
  139. uint32_t SDMMC_CmdSDEraseStartAdd(SDIO_TypeDef *SDIOx, uint32_t StartAdd);
  140. uint32_t SDMMC_CmdEraseEndAdd(SDIO_TypeDef *SDIOx, uint32_t EndAdd);
  141. diff --git a/STM32CubeMX/2020c/Drivers/STM32F2xx_HAL_Driver/Src/stm32f2xx_hal_sd.c b/STM32CubeMX/2020c/Drivers/STM32F2xx_HAL_Driver/Src/stm32f2xx_hal_sd.c
  142. index 569c8b1..b10dd0e 100644
  143. --- a/STM32CubeMX/2020c/Drivers/STM32F2xx_HAL_Driver/Src/stm32f2xx_hal_sd.c
  144. +++ b/STM32CubeMX/2020c/Drivers/STM32F2xx_HAL_Driver/Src/stm32f2xx_hal_sd.c
  145. @@ -430,6 +430,10 @@ HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
  146. /* Enable SDIO Clock */
  147. __HAL_SD_ENABLE(hsd);
  148. + /* 1ms: required power up waiting time before starting the SD initialization
  149. + sequence */
  150. + HAL_Delay(1);
  151. +
  152. /* Identify card operating voltage */
  153. errorstate = SD_PowerON(hsd);
  154. if(errorstate != HAL_SD_ERROR_NONE)
  155. @@ -1227,22 +1231,21 @@ HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, u
  156. else
  157. {
  158. /* Enable SD DMA transfer */
  159. - __HAL_SD_DMA_ENABLE(hsd);
  160. + // MM disabled, as this fails on fast cards. __HAL_SD_DMA_ENABLE(hsd);
  161. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  162. {
  163. add *= 512U;
  164. - }
  165. -
  166. - /* Set Block Size for Card */
  167. - errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  168. - if(errorstate != HAL_SD_ERROR_NONE)
  169. - {
  170. - /* Clear all the static flags */
  171. - __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  172. - hsd->ErrorCode |= errorstate;
  173. - hsd->State = HAL_SD_STATE_READY;
  174. - return HAL_ERROR;
  175. + /* Set Block Size for Card */
  176. + errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  177. + if(errorstate != HAL_SD_ERROR_NONE)
  178. + {
  179. + /* Clear all the static flags */
  180. + __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  181. + hsd->ErrorCode |= errorstate;
  182. + hsd->State = HAL_SD_STATE_READY;
  183. + return HAL_ERROR;
  184. + }
  185. }
  186. /* Configure the SD DPSM (Data Path State Machine) */
  187. @@ -1252,6 +1255,11 @@ HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, u
  188. config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
  189. config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  190. config.DPSM = SDIO_DPSM_ENABLE;
  191. +
  192. + // We cannot enable DMA too early on UHS-I class 3 SD cards, or else the
  193. + // data is just discarded before the dpsm is started.
  194. + __HAL_SD_DMA_ENABLE();
  195. +
  196. (void)SDIO_ConfigData(hsd->Instance, &config);
  197. /* Read Blocks in DMA mode */
  198. @@ -1301,18 +1309,11 @@ HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, u
  199. * @param NumberOfBlocks: Number of blocks to write
  200. * @retval HAL status
  201. */
  202. -HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  203. +HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  204. {
  205. - SDIO_DataInitTypeDef config;
  206. uint32_t errorstate;
  207. uint32_t add = BlockAdd;
  208. - if(NULL == pData)
  209. - {
  210. - hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  211. - return HAL_ERROR;
  212. - }
  213. -
  214. if(hsd->State == HAL_SD_STATE_READY)
  215. {
  216. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  217. @@ -1323,15 +1324,29 @@ HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData,
  218. return HAL_ERROR;
  219. }
  220. - hsd->State = HAL_SD_STATE_BUSY;
  221. + if(NumberOfBlocks > 1U && hsd->SdCard.CardType == CARD_SDHC_SDXC)
  222. + {
  223. + /* MM: Prepare for write */
  224. + errorstate = SDMMC_CmdSetBlockCount(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd) << 16, NumberOfBlocks);
  225. + if(errorstate != HAL_SD_ERROR_NONE)
  226. + {
  227. + __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  228. + hsd->ErrorCode |= errorstate;
  229. + hsd->State = HAL_SD_STATE_READY;
  230. + return HAL_ERROR;
  231. + }
  232. + }
  233. +
  234. + // hsd->State = HAL_SD_STATE_BUSY;
  235. /* Initialize data control register */
  236. hsd->Instance->DCTRL = 0U;
  237. /* Enable SD Error interrupts */
  238. - __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR));
  239. + __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND));
  240. /* Set the DMA transfer complete callback */
  241. + // This callback now doesn't do anything - enabling DATAEND interrupt is set above to avoid race conditions
  242. hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt;
  243. /* Set the DMA error callback */
  244. @@ -1343,17 +1358,16 @@ HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData,
  245. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  246. {
  247. add *= 512U;
  248. - }
  249. -
  250. - /* Set Block Size for Card */
  251. - errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  252. - if(errorstate != HAL_SD_ERROR_NONE)
  253. - {
  254. - /* Clear all the static flags */
  255. - __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  256. - hsd->ErrorCode |= errorstate;
  257. - hsd->State = HAL_SD_STATE_READY;
  258. - return HAL_ERROR;
  259. + /* Set Block Size for Card */
  260. + errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  261. + if(errorstate != HAL_SD_ERROR_NONE)
  262. + {
  263. + /* Clear all the static flags */
  264. + __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  265. + hsd->ErrorCode |= errorstate;
  266. + hsd->State = HAL_SD_STATE_READY;
  267. + return HAL_ERROR;
  268. + }
  269. }
  270. /* Write Blocks in Polling mode */
  271. @@ -1381,11 +1395,55 @@ HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData,
  272. return HAL_ERROR;
  273. }
  274. - /* Enable SDIO DMA transfer */
  275. - __HAL_SD_DMA_ENABLE(hsd);
  276. + return HAL_OK;
  277. + }
  278. + else
  279. + {
  280. + return HAL_BUSY;
  281. + }
  282. +}
  283. +
  284. +/**
  285. + * @brief Writes block(s) to a specified address in a card. The Data transfer
  286. + * is managed by DMA mode.
  287. + * @note This API should be followed by a check on the card state through
  288. + * HAL_SD_GetCardState().
  289. + * @note You could also check the DMA transfer process through the SD Tx
  290. + * interrupt event.
  291. + * @param hsd: Pointer to SD handle
  292. + * @param pData: Pointer to the buffer that will contain the data to transmit
  293. + * @param BlockAdd: Block Address where data will be written
  294. + * @param NumberOfBlocks: Number of blocks to write
  295. + * @retval HAL status
  296. + */
  297. +HAL_StatusTypeDef HAL_SD_WriteBlocks_Data(SD_HandleTypeDef *hsd, uint8_t *pData)
  298. +{
  299. + SDIO_DataInitTypeDef config;
  300. +
  301. + if(hsd->State == HAL_SD_STATE_READY)
  302. + {
  303. + hsd->ErrorCode = HAL_SD_ERROR_NONE;
  304. +
  305. + hsd->State = HAL_SD_STATE_BUSY;
  306. +
  307. + /* Initialize data control register */
  308. + hsd->Instance->DCTRL = 0U;
  309. +
  310. + /* Enable SD Error interrupts */
  311. + __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND));
  312. +
  313. + /* Set the DMA transfer complete callback */
  314. + // This callback now doesn't do anything - enabling DATAEND interrupt is set above to avoid race conditions
  315. + hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt;
  316. +
  317. + /* Set the DMA error callback */
  318. + hsd->hdmatx->XferErrorCallback = SD_DMAError;
  319. +
  320. + /* Set the DMA Abort callback */
  321. + hsd->hdmatx->XferAbortCallback = NULL;
  322. /* Enable the DMA Channel */
  323. - if(HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
  324. + if(HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE)/4U) != HAL_OK)
  325. {
  326. __HAL_SD_DISABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR));
  327. __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  328. @@ -1398,11 +1456,16 @@ HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData,
  329. {
  330. /* Configure the SD DPSM (Data Path State Machine) */
  331. config.DataTimeOut = SDMMC_DATATIMEOUT;
  332. - config.DataLength = BLOCKSIZE * NumberOfBlocks;
  333. + config.DataLength = BLOCKSIZE;
  334. config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
  335. config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
  336. config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  337. config.DPSM = SDIO_DPSM_ENABLE;
  338. +
  339. + // We cannot enable DMA too early on UHS-I class 3 SD cards, or else the
  340. + // data is just discarded before the dpsm is started.
  341. + __HAL_SD_DMA_ENABLE();
  342. +
  343. (void)SDIO_ConfigData(hsd->Instance, &config);
  344. return HAL_OK;
  345. @@ -1588,16 +1651,8 @@ void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
  346. {
  347. if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
  348. {
  349. - errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  350. - if(errorstate != HAL_SD_ERROR_NONE)
  351. - {
  352. - hsd->ErrorCode |= errorstate;
  353. -#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  354. - hsd->ErrorCallback(hsd);
  355. -#else
  356. - HAL_SD_ErrorCallback(hsd);
  357. -#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  358. - }
  359. + __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
  360. + __HAL_SD_DMA_DISABLE(hsd);
  361. }
  362. if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == 0U))
  363. {
  364. @@ -2354,7 +2409,7 @@ HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd)
  365. hsd->Context = SD_CONTEXT_NONE;
  366. CardState = HAL_SD_GetCardState(hsd);
  367. - if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  368. + if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING) || (CardState == HAL_SD_CARD_PROGRAMMING))
  369. {
  370. hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
  371. }
  372. @@ -2460,10 +2515,13 @@ HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd)
  373. */
  374. static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  375. {
  376. - SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  377. + // SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  378. /* Enable DATAEND Interrupt */
  379. - __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DATAEND));
  380. + // __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DATAEND));
  381. + //WHAT IF IT ALREADY TRIGGERED ? Maybe it can't due to interrupt priorities ?
  382. + // Easier to just ignore it.
  383. + // __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DATAEND));
  384. }
  385. /**
  386. diff --git a/STM32CubeMX/2020c/Drivers/STM32F2xx_HAL_Driver/Src/stm32f2xx_ll_sdmmc.c b/STM32CubeMX/2020c/Drivers/STM32F2xx_HAL_Driver/Src/stm32f2xx_ll_sdmmc.c
  387. index b060eae..de39f9d 100644
  388. --- a/STM32CubeMX/2020c/Drivers/STM32F2xx_HAL_Driver/Src/stm32f2xx_ll_sdmmc.c
  389. +++ b/STM32CubeMX/2020c/Drivers/STM32F2xx_HAL_Driver/Src/stm32f2xx_ll_sdmmc.c
  390. @@ -606,6 +606,32 @@ uint32_t SDMMC_CmdWriteSingleBlock(SDIO_TypeDef *SDIOx, uint32_t WriteAdd)
  391. return errorstate;
  392. }
  393. +/**
  394. + * @brief Set the count of a multi-block write command
  395. + * @param SDIOx: Pointer to SDIO register base
  396. + * @retval HAL status
  397. + */
  398. +uint32_t SDMMC_CmdSetBlockCount(SDIO_TypeDef *SDIOx, uint32_t appCmdArg, uint32_t blockCount)
  399. +{
  400. + SDIO_CmdInitTypeDef sdmmc_cmdinit;
  401. + uint32_t errorstate;
  402. +
  403. + errorstate = SDMMC_CmdAppCommand(SDIOx, appCmdArg);
  404. + if(errorstate == HAL_SD_ERROR_NONE)
  405. + {
  406. + sdmmc_cmdinit.Argument = blockCount;
  407. + sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SET_BLOCK_COUNT;
  408. + sdmmc_cmdinit.Response = SDIO_RESPONSE_SHORT;
  409. + sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  410. + sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  411. + (void)SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  412. + errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_SET_BLOCK_COUNT, SDIO_CMDTIMEOUT);
  413. + }
  414. +
  415. + return errorstate;
  416. +}
  417. +
  418. +
  419. /**
  420. * @brief Send the Write Multi Block command and check the response
  421. * @param SDIOx: Pointer to SDIO register base