2021.diff 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. diff --git a/STM32CubeMX/2021/Src/fmc.c b/STM32CubeMX/2021/Src/fmc.c
  2. index dae179a..995fd15 100644
  3. --- a/STM32CubeMX/2021/Src/fmc.c
  4. +++ b/STM32CubeMX/2021/Src/fmc.c
  5. @@ -52,12 +52,28 @@ void MX_FMC_Init(void)
  6. hsram1.Init.WriteFifo = FMC_WRITE_FIFO_ENABLE;
  7. hsram1.Init.PageSize = FMC_PAGE_SIZE_NONE;
  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 = FMC_ACCESS_MODE_A;
  34. /* ExtTiming */
  35. @@ -107,6 +123,10 @@ static void HAL_FMC_MspInit(void){
  36. PE0 ------> FMC_NBL0
  37. PE1 ------> FMC_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/2021/Src/sdio.c b/STM32CubeMX/2021/Src/sdio.c
  47. index 01e3895..33fbae1 100644
  48. --- a/STM32CubeMX/2021/Src/sdio.c
  49. +++ b/STM32CubeMX/2021/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_ENABLE;
  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/2021/Src/spi.c b/STM32CubeMX/2021/Src/spi.c
  69. index 2f9fbfb..aa786dd 100644
  70. --- a/STM32CubeMX/2021/Src/spi.c
  71. +++ b/STM32CubeMX/2021/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. + // 22.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/2021/Src/usbd_conf.c b/STM32CubeMX/2021/Src/usbd_conf.c
  82. index 5b10126..a2c4047 100644
  83. --- a/STM32CubeMX/2021/Src/usbd_conf.c
  84. +++ b/STM32CubeMX/2021/Src/usbd_conf.c
  85. @@ -466,9 +466,11 @@ 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. + // Combined RX + TX fifo of 0x140 4-byte words (1280 bytes)
  90. HAL_PCDEx_SetRxFiFo(&hpcd_USB_OTG_FS, 0x80);
  91. HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_FS, 0, 0x40);
  92. - HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_FS, 1, 0x80);
  93. + HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_FS, 1, 0x40);
  94. + HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_FS, 2, 0x40);
  95. }
  96. if (pdev->id == DEVICE_HS) {
  97. /* Link the driver to the stack. */
  98. @@ -506,9 +508,15 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
  99. HAL_PCD_RegisterIsoOutIncpltCallback(&hpcd_USB_OTG_HS, PCD_ISOOUTIncompleteCallback);
  100. HAL_PCD_RegisterIsoInIncpltCallback(&hpcd_USB_OTG_HS, PCD_ISOINIncompleteCallback);
  101. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  102. + // Combined RX + TX fifo of 0x400 4-byte words (4096 bytes)
  103. HAL_PCDEx_SetRxFiFo(&hpcd_USB_OTG_HS, 0x200);
  104. - HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 0, 0x80);
  105. - HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 1, 0x174);
  106. + HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 0, 0x40);
  107. +
  108. +// HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 1, 0x100);
  109. +// HOst requests 7 sectors, which is an odd number and doesn't fill the
  110. +// fifo, looks like it doesn't complete in this case !!!!
  111. + HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 1, 0x80); // 512 bytes
  112. + HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_HS, 2, 0x40);
  113. }
  114. return USBD_OK;
  115. }
  116. diff --git a/STM32CubeMX/2021/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sd.c b/STM32CubeMX/2021/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sd.c
  117. index d2a88d75..1a09028f 100644
  118. --- a/STM32CubeMX/2021/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sd.c
  119. +++ b/STM32CubeMX/2021/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sd.c
  120. @@ -430,6 +430,10 @@ HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
  121. /* Enable SDIO Clock */
  122. __HAL_SD_ENABLE(hsd);
  123. + /* 1ms: required power up waiting time before starting the SD initialization
  124. + sequence */
  125. + HAL_Delay(1);
  126. +
  127. /* Identify card operating voltage */
  128. errorstate = SD_PowerON(hsd);
  129. if(errorstate != HAL_SD_ERROR_NONE)
  130. @@ -1247,22 +1251,22 @@ HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, u
  131. else
  132. {
  133. /* Enable SD DMA transfer */
  134. - __HAL_SD_DMA_ENABLE(hsd);
  135. + // MM disabled, as this fails on fast cards. __HAL_SD_DMA_ENABLE(hsd);
  136. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  137. {
  138. add *= 512U;
  139. - }
  140. - /* Set Block Size for Card */
  141. - errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  142. - if(errorstate != HAL_SD_ERROR_NONE)
  143. - {
  144. - /* Clear all the static flags */
  145. - __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  146. - hsd->ErrorCode |= errorstate;
  147. - hsd->State = HAL_SD_STATE_READY;
  148. - return HAL_ERROR;
  149. + /* Set Block Size for Card */
  150. + errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  151. + if(errorstate != HAL_SD_ERROR_NONE)
  152. + {
  153. + /* Clear all the static flags */
  154. + __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  155. + hsd->ErrorCode |= errorstate;
  156. + hsd->State = HAL_SD_STATE_READY;
  157. + return HAL_ERROR;
  158. + }
  159. }
  160. /* Configure the SD DPSM (Data Path State Machine) */
  161. @@ -1272,6 +1276,11 @@ HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, u
  162. config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
  163. config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  164. config.DPSM = SDIO_DPSM_ENABLE;
  165. +
  166. + // We cannot enable DMA too early on UHS-I class 3 SD cards, or else the
  167. + // data is just discarded before the dpsm is started.
  168. + __HAL_SD_DMA_ENABLE();
  169. +
  170. (void)SDIO_ConfigData(hsd->Instance, &config);
  171. /* Read Blocks in DMA mode */
  172. @@ -1343,6 +1352,19 @@ HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData,
  173. return HAL_ERROR;
  174. }
  175. + if(NumberOfBlocks > 1U && hsd->SdCard.CardType == CARD_SDHC_SDXC)
  176. + {
  177. + /* MM: Prepare for write */
  178. + errorstate = SDMMC_CmdSetBlockCount(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd) << 16, NumberOfBlocks);
  179. + if(errorstate != HAL_SD_ERROR_NONE)
  180. + {
  181. + __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  182. + hsd->ErrorCode |= errorstate;
  183. + hsd->State = HAL_SD_STATE_READY;
  184. + return HAL_ERROR;
  185. + }
  186. + }
  187. +
  188. hsd->State = HAL_SD_STATE_BUSY;
  189. /* Initialize data control register */
  190. @@ -1367,17 +1389,17 @@ HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData,
  191. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  192. {
  193. add *= 512U;
  194. - }
  195. - /* Set Block Size for Card */
  196. - errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  197. - if(errorstate != HAL_SD_ERROR_NONE)
  198. - {
  199. - /* Clear all the static flags */
  200. - __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  201. - hsd->ErrorCode |= errorstate;
  202. - hsd->State = HAL_SD_STATE_READY;
  203. - return HAL_ERROR;
  204. + /* Set Block Size for Card */
  205. + errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  206. + if(errorstate != HAL_SD_ERROR_NONE)
  207. + {
  208. + /* Clear all the static flags */
  209. + __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  210. + hsd->ErrorCode |= errorstate;
  211. + hsd->State = HAL_SD_STATE_READY;
  212. + return HAL_ERROR;
  213. + }
  214. }
  215. /* Write Blocks in Polling mode */
  216. @@ -1406,7 +1428,7 @@ HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData,
  217. }
  218. /* Enable SDIO DMA transfer */
  219. - __HAL_SD_DMA_ENABLE(hsd);
  220. + // MM disabled, as this fails on fast cards. __HAL_SD_DMA_ENABLE(hsd);
  221. /* Enable the DMA Channel */
  222. if(HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
  223. @@ -1431,6 +1453,11 @@ HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData,
  224. config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
  225. config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
  226. config.DPSM = SDIO_DPSM_ENABLE;
  227. +
  228. + // We cannot enable DMA too early on UHS-I class 3 SD cards, or else the
  229. + // data is just discarded before the dpsm is started.
  230. + __HAL_SD_DMA_ENABLE();
  231. +
  232. (void)SDIO_ConfigData(hsd->Instance, &config);
  233. return HAL_OK;
  234. @@ -1632,6 +1659,10 @@ void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
  235. HAL_SD_ErrorCallback(hsd);
  236. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  237. }
  238. + __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
  239. +
  240. + hsd->State = HAL_SD_STATE_READY;
  241. + hsd->Context = SD_CONTEXT_NONE;
  242. }
  243. if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == 0U))
  244. {
  245. diff --git a/STM32CubeMX/2021/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_sdmmc.h b/STM32CubeMX/2021/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_sdmmc.h
  246. index c966c906..9d709100 100644
  247. --- a/STM32CubeMX/2021/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_sdmmc.h
  248. +++ b/STM32CubeMX/2021/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_sdmmc.h
  249. @@ -1074,6 +1074,7 @@ uint32_t SDMMC_CmdReadSingleBlock(SDIO_TypeDef *SDIOx, uint32_t ReadAdd);
  250. uint32_t SDMMC_CmdReadMultiBlock(SDIO_TypeDef *SDIOx, uint32_t ReadAdd);
  251. uint32_t SDMMC_CmdWriteSingleBlock(SDIO_TypeDef *SDIOx, uint32_t WriteAdd);
  252. uint32_t SDMMC_CmdWriteMultiBlock(SDIO_TypeDef *SDIOx, uint32_t WriteAdd);
  253. +uint32_t SDMMC_CmdSetBlockCount(SDIO_TypeDef *SDIOx, uint32_t appCmdArg, uint32_t blockCount);
  254. uint32_t SDMMC_CmdEraseStartAdd(SDIO_TypeDef *SDIOx, uint32_t StartAdd);
  255. uint32_t SDMMC_CmdSDEraseStartAdd(SDIO_TypeDef *SDIOx, uint32_t StartAdd);
  256. uint32_t SDMMC_CmdEraseEndAdd(SDIO_TypeDef *SDIOx, uint32_t EndAdd);
  257. diff --git a/STM32CubeMX/2021/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_sdmmc.c b/STM32CubeMX/2021/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_sdmmc.c
  258. index 4f23a455..614b6dce 100644
  259. --- a/STM32CubeMX/2021/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_sdmmc.c
  260. +++ b/STM32CubeMX/2021/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_sdmmc.c
  261. @@ -606,6 +606,31 @@ uint32_t SDMMC_CmdWriteSingleBlock(SDIO_TypeDef *SDIOx, uint32_t WriteAdd)
  262. return errorstate;
  263. }
  264. +/**
  265. + * @brief Set the count of a multi-block write command
  266. + * @param SDIOx: Pointer to SDIO register base
  267. + * @retval HAL status
  268. + */
  269. +uint32_t SDMMC_CmdSetBlockCount(SDIO_TypeDef *SDIOx, uint32_t appCmdArg, uint32_t blockCount)
  270. +{
  271. + SDIO_CmdInitTypeDef sdmmc_cmdinit;
  272. + uint32_t errorstate;
  273. +
  274. + errorstate = SDMMC_CmdAppCommand(SDIOx, appCmdArg);
  275. + if(errorstate == HAL_SD_ERROR_NONE)
  276. + {
  277. + sdmmc_cmdinit.Argument = blockCount;
  278. + sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SET_BLOCK_COUNT;
  279. + sdmmc_cmdinit.Response = SDIO_RESPONSE_SHORT;
  280. + sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  281. + sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  282. + (void)SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  283. + errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_SET_BLOCK_COUNT, SDIO_CMDTIMEOUT);
  284. + }
  285. +
  286. + return errorstate;
  287. +}
  288. +
  289. /**
  290. * @brief Send the Write Multi Block command and check the response
  291. * @param SDIOx: Pointer to SDIO register base