revF.diff 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_ll_sdmmc.h b/STM32CubeMX/revF/Drivers/STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_sdmmc.h
  99. index c966c906..9d709100 100644
  100. --- a/STM32CubeMX/revF/Drivers/STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_sdmmc.h
  101. +++ b/STM32CubeMX/revF/Drivers/STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_sdmmc.h
  102. @@ -1074,6 +1074,7 @@ uint32_t SDMMC_CmdReadSingleBlock(SDIO_TypeDef *SDIOx, uint32_t ReadAdd);
  103. uint32_t SDMMC_CmdReadMultiBlock(SDIO_TypeDef *SDIOx, uint32_t ReadAdd);
  104. uint32_t SDMMC_CmdWriteSingleBlock(SDIO_TypeDef *SDIOx, uint32_t WriteAdd);
  105. uint32_t SDMMC_CmdWriteMultiBlock(SDIO_TypeDef *SDIOx, uint32_t WriteAdd);
  106. +uint32_t SDMMC_CmdSetBlockCount(SDIO_TypeDef *SDIOx, uint32_t appCmdArg, uint32_t blockCount);
  107. uint32_t SDMMC_CmdEraseStartAdd(SDIO_TypeDef *SDIOx, uint32_t StartAdd);
  108. uint32_t SDMMC_CmdSDEraseStartAdd(SDIO_TypeDef *SDIOx, uint32_t StartAdd);
  109. uint32_t SDMMC_CmdEraseEndAdd(SDIO_TypeDef *SDIOx, uint32_t EndAdd);
  110. 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
  111. index 4f23a455..614b6dce 100644
  112. --- a/STM32CubeMX/revF/Drivers/STM32F2xx_HAL_Driver/Src/stm32f2xx_ll_sdmmc.c
  113. +++ b/STM32CubeMX/revF/Drivers/STM32F2xx_HAL_Driver/Src/stm32f2xx_ll_sdmmc.c
  114. @@ -606,6 +606,31 @@ uint32_t SDMMC_CmdWriteSingleBlock(SDIO_TypeDef *SDIOx, uint32_t WriteAdd)
  115. return errorstate;
  116. }
  117. +/**
  118. + * @brief Set the count of a multi-block write command
  119. + * @param SDIOx: Pointer to SDIO register base
  120. + * @retval HAL status
  121. + */
  122. +uint32_t SDMMC_CmdSetBlockCount(SDIO_TypeDef *SDIOx, uint32_t appCmdArg, uint32_t blockCount)
  123. +{
  124. + SDIO_CmdInitTypeDef sdmmc_cmdinit;
  125. + uint32_t errorstate;
  126. +
  127. + errorstate = SDMMC_CmdAppCommand(SDIOx, appCmdArg);
  128. + if(errorstate == HAL_SD_ERROR_NONE)
  129. + {
  130. + sdmmc_cmdinit.Argument = blockCount;
  131. + sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SET_BLOCK_COUNT;
  132. + sdmmc_cmdinit.Response = SDIO_RESPONSE_SHORT;
  133. + sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  134. + sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  135. + (void)SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  136. + errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_SET_BLOCK_COUNT, SDIO_CMDTIMEOUT);
  137. + }
  138. +
  139. + return errorstate;
  140. +}
  141. +
  142. /**
  143. * @brief Send the Write Multi Block command and check the response
  144. * @param SDIOx: Pointer to SDIO register base
  145. 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
  146. index d2a88d75..1a09028f 100644
  147. --- a/STM32CubeMX/revF/Drivers/STM32F2xx_HAL_Driver/Src/stm32f2xx_hal_sd.c
  148. +++ b/STM32CubeMX/revF/Drivers/STM32F2xx_HAL_Driver/Src/stm32f2xx_hal_sd.c
  149. @@ -430,6 +430,10 @@ HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
  150. /* Enable SDIO Clock */
  151. __HAL_SD_ENABLE(hsd);
  152. + /* 1ms: required power up waiting time before starting the SD initialization
  153. + sequence */
  154. + HAL_Delay(1);
  155. +
  156. /* Identify card operating voltage */
  157. errorstate = SD_PowerON(hsd);
  158. if(errorstate != HAL_SD_ERROR_NONE)
  159. @@ -1247,22 +1251,22 @@ HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, u
  160. else
  161. {
  162. /* Enable SD DMA transfer */
  163. - __HAL_SD_DMA_ENABLE(hsd);
  164. + // MM disabled, as this fails on fast cards. __HAL_SD_DMA_ENABLE(hsd);
  165. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  166. {
  167. add *= 512U;
  168. - }
  169. - /* Set Block Size for Card */
  170. - errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  171. - if(errorstate != HAL_SD_ERROR_NONE)
  172. - {
  173. - /* Clear all the static flags */
  174. - __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  175. - hsd->ErrorCode |= errorstate;
  176. - hsd->State = HAL_SD_STATE_READY;
  177. - return HAL_ERROR;
  178. + /* Set Block Size for Card */
  179. + errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  180. + if(errorstate != HAL_SD_ERROR_NONE)
  181. + {
  182. + /* Clear all the static flags */
  183. + __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  184. + hsd->ErrorCode |= errorstate;
  185. + hsd->State = HAL_SD_STATE_READY;
  186. + return HAL_ERROR;
  187. + }
  188. }
  189. /* Configure the SD DPSM (Data Path State Machine) */
  190. @@ -1272,6 +1276,11 @@ HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, u
  191. config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
  192. config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  193. config.DPSM = SDIO_DPSM_ENABLE;
  194. +
  195. + // We cannot enable DMA too early on UHS-I class 3 SD cards, or else the
  196. + // data is just discarded before the dpsm is started.
  197. + __HAL_SD_DMA_ENABLE();
  198. +
  199. (void)SDIO_ConfigData(hsd->Instance, &config);
  200. /* Read Blocks in DMA mode */
  201. @@ -1343,6 +1352,19 @@ HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData,
  202. return HAL_ERROR;
  203. }
  204. + if(NumberOfBlocks > 1U && hsd->SdCard.CardType == CARD_SDHC_SDXC)
  205. + {
  206. + /* MM: Prepare for write */
  207. + errorstate = SDMMC_CmdSetBlockCount(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd) << 16, NumberOfBlocks);
  208. + if(errorstate != HAL_SD_ERROR_NONE)
  209. + {
  210. + __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  211. + hsd->ErrorCode |= errorstate;
  212. + hsd->State = HAL_SD_STATE_READY;
  213. + return HAL_ERROR;
  214. + }
  215. + }
  216. +
  217. hsd->State = HAL_SD_STATE_BUSY;
  218. /* Initialize data control register */
  219. @@ -1367,17 +1389,17 @@ HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData,
  220. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  221. {
  222. add *= 512U;
  223. - }
  224. - /* Set Block Size for Card */
  225. - errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  226. - if(errorstate != HAL_SD_ERROR_NONE)
  227. - {
  228. - /* Clear all the static flags */
  229. - __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  230. - hsd->ErrorCode |= errorstate;
  231. - hsd->State = HAL_SD_STATE_READY;
  232. - return HAL_ERROR;
  233. + /* Set Block Size for Card */
  234. + errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  235. + if(errorstate != HAL_SD_ERROR_NONE)
  236. + {
  237. + /* Clear all the static flags */
  238. + __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  239. + hsd->ErrorCode |= errorstate;
  240. + hsd->State = HAL_SD_STATE_READY;
  241. + return HAL_ERROR;
  242. + }
  243. }
  244. /* Write Blocks in Polling mode */
  245. @@ -1406,7 +1428,7 @@ HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData,
  246. }
  247. /* Enable SDIO DMA transfer */
  248. - __HAL_SD_DMA_ENABLE(hsd);
  249. + // MM disabled, as this fails on fast cards. __HAL_SD_DMA_ENABLE(hsd);
  250. /* Enable the DMA Channel */
  251. if(HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
  252. @@ -1431,6 +1453,11 @@ HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData,
  253. config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
  254. config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  255. config.DPSM = SDIO_DPSM_ENABLE;
  256. +
  257. + // We cannot enable DMA too early on UHS-I class 3 SD cards, or else the
  258. + // data is just discarded before the dpsm is started.
  259. + __HAL_SD_DMA_ENABLE();
  260. +
  261. (void)SDIO_ConfigData(hsd->Instance, &config);
  262. return HAL_OK;
  263. @@ -1632,6 +1659,10 @@ void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
  264. HAL_SD_ErrorCallback(hsd);
  265. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  266. }
  267. + __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
  268. +
  269. + hsd->State = HAL_SD_STATE_READY;
  270. + hsd->Context = SD_CONTEXT_NONE;
  271. }
  272. if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == 0U))
  273. {