drv_sram.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-12-04 Leo first version
  9. */
  10. #include <board.h>
  11. #include <rtthread.h>
  12. #include "drv_sram.h"
  13. #ifdef BSP_USING_SRAM
  14. #define DRV_DEBUG
  15. #define LOG_TAG "drv.sram"
  16. #include <drv_log.h>
  17. uint16_t RT_TxBuffer[RT_BUFFER_SIZE];
  18. uint16_t RT_RxBuffer[RT_BUFFER_SIZE];
  19. uint32_t WriteReadStatus = 0, Index = 0;
  20. #ifdef RT_USING_MEMHEAP_AS_HEAP
  21. static struct rt_memheap system_heap;
  22. #endif
  23. static int rt_hw_sram_Init(void)
  24. {
  25. int result = RT_EOK;
  26. XMC_Bank1_Type *XMC;
  27. XMC_NORSRAMInitType XMC_NORSRAMInitStructure;
  28. XMC_NORSRAMTimingInitType p;
  29. /* Init XMC pin */
  30. at32_msp_xmc_init(XMC);
  31. /*-- FSMC Configuration ------------------------------------------------------*/
  32. p.XMC_AdrOpTime = 0x04;
  33. p.XMC_AdrHoldTime = 0x04;
  34. p.XMC_DataOpTime = 0x0a;
  35. p.XMC_IntervalBetweenOP = 0x0;
  36. p.XMC_CLKPsc = 0x0;
  37. p.XMC_DataStableTime = 0x0;
  38. p.XMC_Mode = XMC_Mode_A;
  39. XMC_NORSRAMInitStructure.XMC_Bank = XMC_Bank1_NORSRAM3;
  40. XMC_NORSRAMInitStructure.XMC_DataAdrMux = XMC_DataAdrMux_Disable;
  41. XMC_NORSRAMInitStructure.XMC_Dev = XMC_Dev_SRAM;
  42. XMC_NORSRAMInitStructure.XMC_BusType = XMC_BusType_16b;
  43. XMC_NORSRAMInitStructure.XMC_EnableBurstMode = XMC_BurstMode_Disable;
  44. XMC_NORSRAMInitStructure.XMC_EnableAsynWait = XMC_AsynWait_Disable;
  45. XMC_NORSRAMInitStructure.XMC_WaitSignalLv = XMC_WaitSignalLv_Low;
  46. XMC_NORSRAMInitStructure.XMC_EnableBurstModeSplit = XMC_BurstModeSplit_Disable;
  47. XMC_NORSRAMInitStructure.XMC_WaitSignalConfig = XMC_WaitSignalConfig_BeforeWaitState;
  48. XMC_NORSRAMInitStructure.XMC_EnableWrite = XMC_WriteOperation_Enable;
  49. XMC_NORSRAMInitStructure.XMC_EnableWaitSignal = XMC_WaitSignal_Disable;
  50. XMC_NORSRAMInitStructure.XMC_EnableWriteTiming = XMC_WriteTiming_Disable;
  51. XMC_NORSRAMInitStructure.XMC_WriteBurstSyn = XMC_WriteBurstSyn_Disable;
  52. XMC_NORSRAMInitStructure.XMC_RWTimingStruct = &p;
  53. XMC_NORSRAMInitStructure.XMC_WTimingStruct = &p;
  54. XMC_NORSRAMInit(&XMC_NORSRAMInitStructure);
  55. /*!< Enable FSMC Bank1_SRAM Bank */
  56. XMC_NORSRAMCmd(XMC_Bank1_NORSRAM3, ENABLE);
  57. #ifdef RT_USING_MEMHEAP_AS_HEAP
  58. /* If RT_USING_MEMHEAP_AS_HEAP is enabled, SRAM is initialized to the heap */
  59. rt_memheap_init(&system_heap, "sram", (void *)EXT_SRAM_BEGIN, SRAM_LENGTH);
  60. #endif
  61. return result;
  62. }
  63. INIT_BOARD_EXPORT(rt_hw_sram_Init);
  64. #ifdef DRV_DEBUG
  65. #ifdef FINSH_USING_MSH
  66. /**
  67. * @brief Writes a Half-word buffer to the FSMC SRAM memory.
  68. * @param pBuffer : pointer to buffer.
  69. * @param WriteAddr : SRAM memory internal address from which the data will be
  70. * written.
  71. * @param NumHalfwordToWrite : number of half-words to write.
  72. * @retval None
  73. */
  74. static void SRAM_WriteBuffer(uint16_t* pBuffer, uint32_t WriteAddr, uint32_t NumHalfwordToWrite)
  75. {
  76. for(; NumHalfwordToWrite != 0; NumHalfwordToWrite--) /*!< while there is data to write */
  77. {
  78. /*!< Transfer data to the memory */
  79. *(uint16_t *) (EXT_SRAM_BEGIN + WriteAddr) = *pBuffer++;
  80. /*!< Increment the address*/
  81. WriteAddr += 2;
  82. }
  83. }
  84. /**
  85. * @brief Reads a block of data from the FSMC SRAM memory.
  86. * @param pBuffer : pointer to the buffer that receives the data read from the
  87. * SRAM memory.
  88. * @param ReadAddr : SRAM memory internal address to read from.
  89. * @param NumHalfwordToRead : number of half-words to read.
  90. * @retval None
  91. */
  92. static void SRAM_ReadBuffer(uint16_t* pBuffer, uint32_t ReadAddr, uint32_t NumHalfwordToRead)
  93. {
  94. for(; NumHalfwordToRead != 0; NumHalfwordToRead--) /*!< while there is data to read */
  95. {
  96. /*!< Read a half-word from the memory */
  97. *pBuffer++ = *(__IO uint16_t*) (EXT_SRAM_BEGIN + ReadAddr);
  98. /*!< Increment the address*/
  99. ReadAddr += 2;
  100. }
  101. }
  102. /**
  103. * @brief Fill the global buffer
  104. * @param pBuffer: pointer on the Buffer to fill
  105. * @param BufferSize: size of the buffer to fill
  106. * @param Offset: first value to fill on the Buffer
  107. */
  108. static void Fill_Buffer(uint16_t *pBuffer, uint16_t BufferLenght, uint32_t Offset)
  109. {
  110. uint16_t IndexTmp = 0;
  111. /* Put in global buffer same values */
  112. for (IndexTmp = 0; IndexTmp < BufferLenght; IndexTmp++ )
  113. {
  114. pBuffer[IndexTmp] = IndexTmp + Offset;
  115. }
  116. }
  117. int sram_test(void)
  118. {
  119. /* Write data to XMC SRAM memory */
  120. /* Fill the buffer to send */
  121. Fill_Buffer(RT_TxBuffer, RT_BUFFER_SIZE, 0x3212);
  122. SRAM_WriteBuffer(RT_TxBuffer, RT_WRITE_READ_ADDR, RT_BUFFER_SIZE);
  123. /* Read data from XMC SRAM memory */
  124. SRAM_ReadBuffer(RT_RxBuffer, RT_WRITE_READ_ADDR, RT_BUFFER_SIZE);
  125. /* Read back SRAM memory and check content correctness */
  126. for (Index = 0x00; (Index < RT_BUFFER_SIZE) && (WriteReadStatus == 0); Index++)
  127. {
  128. if (RT_RxBuffer[Index] != RT_TxBuffer[Index])
  129. {
  130. WriteReadStatus = Index + 1;
  131. }
  132. }
  133. if(WriteReadStatus == 0)
  134. {
  135. LOG_D("SRAM test success!");
  136. }
  137. else
  138. {
  139. LOG_E("SRAM test failed!");
  140. }
  141. return RT_EOK;
  142. }
  143. MSH_CMD_EXPORT(sram_test, sram test)
  144. #endif /* FINSH_USING_MSH */
  145. #endif /* DRV_DEBUG */
  146. #endif /* BSP_USING_SRAM */