revF.diff 17 KB

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