stm322xg_eval_sram.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. ******************************************************************************
  3. * @file stm322xg_eval_sram.c
  4. * @author MCD Application Team
  5. * @brief This file includes the SRAM driver for the IS61WV102416BLL-10MLI memory
  6. * device mounted on STM322xG-EVAL evaluation board.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. * 1. Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ******************************************************************************
  35. */
  36. /* File Info : -----------------------------------------------------------------
  37. User NOTES
  38. 1. How To use this driver:
  39. --------------------------
  40. - This driver is used to drive the IS61WV102416BLL-10MLI SRAM external memory mounted
  41. on STM322xG-EVAL evaluation board.
  42. - This driver does not need a specific component driver for the SRAM device
  43. to be included with.
  44. 2. Driver description:
  45. ---------------------
  46. + Initialization steps:
  47. o Initialize the SRAM external memory using the BSP_SRAM_Init() function. This
  48. function includes the MSP layer hardware resources initialization and the
  49. FSMC controller configuration to interface with the external SRAM memory.
  50. + SRAM read/write operations
  51. o SRAM external memory can be accessed with read/write operations once it is
  52. initialized.
  53. Read/write operation can be performed with AHB access using the functions
  54. BSP_SRAM_ReadData()/BSP_SRAM_WriteData(), or by DMA transfer using the functions
  55. BSP_SRAM_ReadData_DMA()/BSP_SRAM_WriteData_DMA().
  56. o The AHB access is performed with 16-bit width transaction, the DMA transfer
  57. configuration is fixed at single (no burst) halfword transfer
  58. (see the SRAM_MspInit() static function).
  59. o User can implement his own functions for read/write access with his desired
  60. configurations.
  61. o If interrupt mode is used for DMA transfer, the function BSP_SRAM_DMA_IRQHandler()
  62. is called in IRQ handler file, to serve the generated interrupt once the DMA
  63. transfer is complete.
  64. ------------------------------------------------------------------------------*/
  65. /* Includes ------------------------------------------------------------------*/
  66. #include "stm322xg_eval_sram.h"
  67. /** @addtogroup BSP
  68. * @{
  69. */
  70. /** @addtogroup STM322xG_EVAL
  71. * @{
  72. */
  73. /** @defgroup STM322xG_EVAL_SRAM STM322xG EVAL SRAM
  74. * @{
  75. */
  76. /** @defgroup STM322xG_EVAL_SRAM_Private_Variables STM322xG EVAL SRAM Private Variables
  77. * @{
  78. */
  79. static SRAM_HandleTypeDef sramHandle;
  80. static FSMC_NORSRAM_TimingTypeDef Timing;
  81. /**
  82. * @}
  83. */
  84. /** @defgroup STM322xG_EVAL_SRAM_Private_Functions STM322xG EVAL SRAM Private Functions
  85. * @{
  86. */
  87. /**
  88. * @brief Initializes the SRAM device.
  89. * @retval SRAM status
  90. */
  91. uint8_t BSP_SRAM_Init(void)
  92. {
  93. sramHandle.Instance = FSMC_NORSRAM_DEVICE;
  94. sramHandle.Extended = FSMC_NORSRAM_EXTENDED_DEVICE;
  95. /* SRAM device configuration */
  96. Timing.AddressSetupTime = 2;
  97. Timing.AddressHoldTime = 1;
  98. Timing.DataSetupTime = 2;
  99. Timing.BusTurnAroundDuration = 1;
  100. Timing.CLKDivision = 2;
  101. Timing.DataLatency = 2;
  102. Timing.AccessMode = FSMC_ACCESS_MODE_A;
  103. sramHandle.Init.NSBank = FSMC_NORSRAM_BANK2;
  104. sramHandle.Init.DataAddressMux = FSMC_DATA_ADDRESS_MUX_DISABLE;
  105. sramHandle.Init.MemoryType = FSMC_MEMORY_TYPE_SRAM;
  106. sramHandle.Init.MemoryDataWidth = SRAM_MEMORY_WIDTH;
  107. sramHandle.Init.BurstAccessMode = SRAM_BURSTACCESS;
  108. sramHandle.Init.WaitSignalPolarity = FSMC_WAIT_SIGNAL_POLARITY_LOW;
  109. sramHandle.Init.WrapMode = FSMC_WRAP_MODE_DISABLE;
  110. sramHandle.Init.WaitSignalActive = FSMC_WAIT_TIMING_BEFORE_WS;
  111. sramHandle.Init.WriteOperation = FSMC_WRITE_OPERATION_ENABLE;
  112. sramHandle.Init.WaitSignal = FSMC_WAIT_SIGNAL_DISABLE;
  113. sramHandle.Init.ExtendedMode = FSMC_EXTENDED_MODE_DISABLE;
  114. sramHandle.Init.AsynchronousWait = FSMC_ASYNCHRONOUS_WAIT_DISABLE;
  115. sramHandle.Init.WriteBurst = SRAM_WRITEBURST;
  116. /* SRAM controller initialization */
  117. BSP_SRAM_MspInit();
  118. if(HAL_SRAM_Init(&sramHandle, &Timing, &Timing) != HAL_OK)
  119. {
  120. return SRAM_ERROR;
  121. }
  122. else
  123. {
  124. return SRAM_OK;
  125. }
  126. }
  127. /**
  128. * @brief Reads an amount of data from the SRAM device in polling mode.
  129. * @param uwStartAddress : Read start address
  130. * @param pData: Pointer to data to be read
  131. * @param uwDataSize: Size of read data from the memory
  132. * @retval SRAM status
  133. */
  134. uint8_t BSP_SRAM_ReadData(uint32_t uwStartAddress, uint16_t *pData, uint32_t uwDataSize)
  135. {
  136. if(HAL_SRAM_Read_16b(&sramHandle, (uint32_t *)uwStartAddress, pData, uwDataSize) != HAL_OK)
  137. {
  138. return SRAM_ERROR;
  139. }
  140. else
  141. {
  142. return SRAM_OK;
  143. }
  144. }
  145. /**
  146. * @brief Reads an amount of data from the SRAM device in DMA mode.
  147. * @param uwStartAddress : Read start address
  148. * @param pData: Pointer to data to be read
  149. * @param uwDataSize: Size of read data from the memory
  150. * @retval SRAM status
  151. */
  152. uint8_t BSP_SRAM_ReadData_DMA(uint32_t uwStartAddress, uint16_t *pData, uint32_t uwDataSize)
  153. {
  154. if(HAL_SRAM_Read_DMA(&sramHandle, (uint32_t *)uwStartAddress, (uint32_t *)pData, uwDataSize) != HAL_OK)
  155. {
  156. return SRAM_ERROR;
  157. }
  158. else
  159. {
  160. return SRAM_OK;
  161. }
  162. }
  163. /**
  164. * @brief Writes an amount of data from the SRAM device in polling mode.
  165. * @param uwStartAddress: Write start address
  166. * @param pData: Pointer to data to be written
  167. * @param uwDataSize: Size of written data from the memory
  168. * @retval SRAM status
  169. */
  170. uint8_t BSP_SRAM_WriteData(uint32_t uwStartAddress, uint16_t *pData, uint32_t uwDataSize)
  171. {
  172. if(HAL_SRAM_Write_16b(&sramHandle, (uint32_t *)uwStartAddress, pData, uwDataSize) != HAL_OK)
  173. {
  174. return SRAM_ERROR;
  175. }
  176. else
  177. {
  178. return SRAM_OK;
  179. }
  180. }
  181. /**
  182. * @brief Writes an amount of data from the SRAM device in DMA mode.
  183. * @param uwStartAddress: Write start address
  184. * @param pData: Pointer to data to be written
  185. * @param uwDataSize: Size of written data from the memory
  186. * @retval SRAM status
  187. */
  188. uint8_t BSP_SRAM_WriteData_DMA(uint32_t uwStartAddress, uint16_t *pData, uint32_t uwDataSize)
  189. {
  190. if(HAL_SRAM_Write_DMA(&sramHandle, (uint32_t *)uwStartAddress, (uint32_t *)pData, uwDataSize) != HAL_OK)
  191. {
  192. return SRAM_ERROR;
  193. }
  194. else
  195. {
  196. return SRAM_OK;
  197. }
  198. }
  199. /**
  200. * @brief Handles SRAM DMA transfer interrupt request.
  201. */
  202. void BSP_SRAM_DMA_IRQHandler(void)
  203. {
  204. HAL_DMA_IRQHandler(sramHandle.hdma);
  205. }
  206. /**
  207. * @brief Initializes SRAM MSP.
  208. */
  209. __weak void BSP_SRAM_MspInit(void)
  210. {
  211. static DMA_HandleTypeDef dmaHandle;
  212. GPIO_InitTypeDef GPIO_Init_Structure;
  213. SRAM_HandleTypeDef *hsram = &sramHandle;
  214. /* Enable FSMC clock */
  215. __HAL_RCC_FSMC_CLK_ENABLE();
  216. /* Enable chosen DMAx clock */
  217. __SRAM_DMAx_CLK_ENABLE();
  218. /* Enable GPIOs clock */
  219. __HAL_RCC_GPIOD_CLK_ENABLE();
  220. __HAL_RCC_GPIOE_CLK_ENABLE();
  221. __HAL_RCC_GPIOF_CLK_ENABLE();
  222. __HAL_RCC_GPIOG_CLK_ENABLE();
  223. /* Common GPIO configuration */
  224. GPIO_Init_Structure.Mode = GPIO_MODE_AF_PP;
  225. GPIO_Init_Structure.Pull = GPIO_PULLUP;
  226. GPIO_Init_Structure.Speed = GPIO_SPEED_HIGH;
  227. GPIO_Init_Structure.Alternate = GPIO_AF12_FSMC;
  228. /* GPIOD configuration */
  229. GPIO_Init_Structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_8 |\
  230. GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 |\
  231. GPIO_PIN_14 | GPIO_PIN_15;
  232. HAL_GPIO_Init(GPIOD, &GPIO_Init_Structure);
  233. /* GPIOE configuration */
  234. GPIO_Init_Structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3| GPIO_PIN_4 | GPIO_PIN_7 |\
  235. GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 |\
  236. GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
  237. HAL_GPIO_Init(GPIOE, &GPIO_Init_Structure);
  238. /* GPIOF configuration */
  239. GPIO_Init_Structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2| GPIO_PIN_3 | GPIO_PIN_4 |\
  240. GPIO_PIN_5 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
  241. HAL_GPIO_Init(GPIOF, &GPIO_Init_Structure);
  242. /* GPIOG configuration */
  243. GPIO_Init_Structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2| GPIO_PIN_3 | GPIO_PIN_4 |\
  244. GPIO_PIN_5 | GPIO_PIN_9;
  245. HAL_GPIO_Init(GPIOG, &GPIO_Init_Structure);
  246. /* Configure common DMA parameters */
  247. dmaHandle.Init.Channel = SRAM_DMAx_CHANNEL;
  248. dmaHandle.Init.Direction = DMA_MEMORY_TO_MEMORY;
  249. dmaHandle.Init.PeriphInc = DMA_PINC_ENABLE;
  250. dmaHandle.Init.MemInc = DMA_MINC_ENABLE;
  251. dmaHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
  252. dmaHandle.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
  253. dmaHandle.Init.Mode = DMA_NORMAL;
  254. dmaHandle.Init.Priority = DMA_PRIORITY_HIGH;
  255. dmaHandle.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  256. dmaHandle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  257. dmaHandle.Init.MemBurst = DMA_MBURST_INC8;
  258. dmaHandle.Init.PeriphBurst = DMA_PBURST_INC8;
  259. dmaHandle.Instance = SRAM_DMAx_STREAM;
  260. /* Associate the DMA handle */
  261. __HAL_LINKDMA(hsram, hdma, dmaHandle);
  262. /* Deinitialize the stream for new transfer */
  263. HAL_DMA_DeInit(&dmaHandle);
  264. /* Configure the DMA stream */
  265. HAL_DMA_Init(&dmaHandle);
  266. /* NVIC configuration for DMA transfer complete interrupt */
  267. HAL_NVIC_SetPriority(SRAM_DMAx_IRQn, 0x0F, 0);
  268. HAL_NVIC_EnableIRQ(SRAM_DMAx_IRQn);
  269. }
  270. /**
  271. * @}
  272. */
  273. /**
  274. * @}
  275. */
  276. /**
  277. * @}
  278. */
  279. /**
  280. * @}
  281. */
  282. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/