×
单片机 > 单片机程序设计 > 详情

STM32单片机用FSMC接口控制SRAM

发布时间:2020-05-21 发布时间:
|

环境:

主机:WIN7

开发环境:MDK4.72

MCU:STM32F103ZE


说明:

sram型号:IS62WV51216BLL
连接方式:FSMC
大小:1M字节.512K * 16


源代码:

inf_sram.h


  1. /********************************************************************* 

  2. *                         sram接口层头文件 

  3. *                       (c)copyright 2013,jdh 

  4. *                         All Right Reserved 

  5. *文件名:inf_sram.h 

  6. *程序员:jdh 

  7. *修改日期:2013/10/10 

  8. *         2013/10/11 

  9. **********************************************************************/  

  10.   

  11. /********************************************************************* 

  12. *                               说明 

  13. *sram型号:IS62WV51216BLL 

  14. *连接方式:FSMC 

  15. *大小:1M字节.512K * 16 

  16. **********************************************************************/  

  17.   

  18. #ifndef _INF_SRAM_H_  

  19. #define _INF_SRAM_H_  

  20.   

  21. /********************************************************************* 

  22. *                           头文件 

  23. **********************************************************************/  

  24.   

  25. #include "stm32f10x.h"  

  26. #include "stm32f10x_fsmc.h"  

  27.   

  28. /********************************************************************* 

  29. *                           宏定义 

  30. **********************************************************************/  

  31.   

  32. /********************************************************************* 

  33. *                           SRAM2的BANK1起始地址 

  34. **********************************************************************/  

  35.   

  36. #define Bank1_SRAM2_ADDR    ((uint32_t)0x64000000)  

  37.   

  38. /********************************************************************* 

  39. *                           函数 

  40. **********************************************************************/  

  41.   

  42. /********************************************************************* 

  43. *                           初始化sram 

  44. **********************************************************************/  

  45.   

  46. void inf_init_sram(void);  

  47.   

  48. /********************************************************************* 

  49. *                           写入数据包 

  50. *输入:pBuffer:数据指针 

  51. *     WriteAddr:写入数据地址 

  52. *     NumHalfwordToWrite:数据长度 

  53. *返回:无 

  54. **********************************************************************/  

  55.   

  56. void FSMC_SRAM_WriteBuffer(uint16_t* pBuffer,uint32_t WriteAddr,uint32_t NumHalfwordToWrite);  

  57.   

  58. /********************************************************************* 

  59. *                           读取数据包 

  60. *输入:pBuffer:存放数据的指针 

  61. *     ReadAddr:读取数据地址 

  62. *     NumHalfwordToRead:读取数据长度,单位半字,即2字节 

  63. *返回:无 

  64. **********************************************************************/  

  65.   

  66. void FSMC_SRAM_ReadBuffer(uint16_t* pBuffer, uint32_t ReadAddr, uint32_t NumHalfwordToRead);  

  67.   

  68. /********************************************************************* 

  69. *                           写入半字数据 

  70. *输入:WriteAddr:写入数据地址 

  71. *     data:数据 

  72. *返回:无 

  73. **********************************************************************/  

  74.   

  75. void FSMC_SRAM_WriteHalfWord(uint32_t WriteAddr, uint16_t data);  

  76.   

  77. /********************************************************************* 

  78. *                           读取半字数据 

  79. *输入:ReadAddr:读取数据地址 

  80. *返回:读取的数据 

  81. **********************************************************************/  

  82.   

  83. uint16_t FSMC_SRAM_ReadHalfWord(uint32_t ReadAddr);  

  84.   

  85. #endif  

  86.   

  87.   


inf_sram.c



  1. /********************************************************************* 

  2. *                         sram接口层头文件 

  3. *                       (c)copyright 2013,jdh 

  4. *                         All Right Reserved 

  5. *文件名:inf_sram.c 

  6. *程序员:jdh 

  7. *修改日期:2013/10/10 

  8. *         2013/10/11 

  9. **********************************************************************/  

  10.   

  11. /********************************************************************* 

  12. *                               说明 

  13. *sram型号:IS62WV51216BLL 

  14. *连接方式:FSMC 

  15. *大小:1M字节.512K * 16 

  16. **********************************************************************/  

  17.   

  18. /********************************************************************* 

  19. *                           头文件 

  20. **********************************************************************/  

  21.   

  22. #include "inf_sram.h"  

  23.   

  24. /********************************************************************* 

  25. *                           函数 

  26. **********************************************************************/  

  27.   

  28. /********************************************************************* 

  29. *                           初始化sram 

  30. **********************************************************************/  

  31.   

  32. void inf_init_sram(void)  

  33. {  

  34.     GPIO_InitTypeDef GPIO_InitStructure;   

  35.     FSMC_NORSRAMTimingInitTypeDef p;  

  36.     FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;  

  37.       

  38.     //开启FSMC时钟  

  39.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC,ENABLE);  

  40.     //RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC | RCC_AHBPeriph_SRAM,ENABLE);  

  41.     //开启FSMC相关的IO时钟  

  42.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |   

  43.                            RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG |   

  44.                            RCC_APB2Periph_AFIO, ENABLE);  

  45.     //FSMC相关的IO配置  

  46.     //数据线  

  47.     //PD口  

  48.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_8 |   

  49.                                   GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_14 |   

  50.                                   GPIO_Pin_15;  

  51.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  

  52.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  

  53.     GPIO_Init(GPIOD, &GPIO_InitStructure);   

  54.     //PE口  

  55.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 |   

  56.                                   GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 |   

  57.                                   GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;  

  58.     GPIO_Init(GPIOE, &GPIO_InitStructure);  

  59.       

  60.     //地址线  

  61.     //PF口  

  62.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 |   

  63.                                   GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 |   

  64.                                   GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |   

  65.                                   GPIO_Pin_15;  

  66.     GPIO_Init(GPIOF, &GPIO_InitStructure);  

  67.     //PG口  

  68.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 |   

  69.                                   GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;  

  70.     GPIO_Init(GPIOG, &GPIO_InitStructure);  

  71.     //PD口  

  72.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;   

  73.     GPIO_Init(GPIOD, &GPIO_InitStructure);  

  74.       

  75.     //NOE和NWE配置   

  76.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;  

  77.     GPIO_Init(GPIOD, &GPIO_InitStructure);  

  78.   

  79.     //NE2配置  

  80.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;   

  81.     GPIO_Init(GPIOG, &GPIO_InitStructure);  

  82.   

  83.     //NBL0, NBL1配置  

  84.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;   

  85.     GPIO_Init(GPIOE, &GPIO_InitStructure);   

  86.   

  87.     //FSMC配置  

  88.     //地址建立时间  

  89.     p.FSMC_AddressSetupTime = 0;  

  90.     //地址保持时间  

  91.     p.FSMC_AddressHoldTime = 0;  

  92.     //数据建立时间  

  93.     p.FSMC_DataSetupTime = 5;  

  94.     //总线恢复时间  

  95.     p.FSMC_BusTurnAroundDuration = 0;  

  96.     //时钟分频因子  

  97.     p.FSMC_CLKDivision = 0;  

  98.     //数据产生时间  

  99.     p.FSMC_DataLatency = 0;  

  100.     //控制器时序  

  101.     p.FSMC_AccessMode = FSMC_AccessMode_A;  

  102.       

  103.     //总线参数配置  

  104.     //使用FSMC的Bank1的字块2  

  105.     FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM2;  

  106.     //禁止地址数据线复用  

  107.     FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;  

  108.     //存储类型为sram  

  109.     FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;  

  110.     //存储器位宽16位  

  111.     FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;  

  112.     //关闭突发模式访问  

  113.     FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;  

  114.     //使能突发访问模式后才有效,等待信号极性  

  115.     FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;  

  116.     //使能突发访问模式后才有效,非对齐成组模式  

  117.     FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;  

  118.     //使能突发访问模式后才有效,配置等待时序  

  119.     FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;  

  120.     //使能写操作  

  121.     FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;  

  122.     //使能突发访问模式后才有效,关闭等待信号  

  123.     FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;  

  124.     //扩展模式使能  

  125.     FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;  

  126.     //成组写使能位  

  127.     FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;  

  128.     //读操作时序操作  

  129.     FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;  

  130.     //写操作时序参数  

  131.     FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;  

  132.     //初始化FSMC总线  

  133.     FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);  

  134.     //使能FSMC Bank1_SRAM Bank  

  135.     FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, ENABLE);    

  136. }  

  137.   

  138. /********************************************************************* 

  139. *                           写入数据包 

  140. *输入:pBuffer:数据指针 

  141. *     WriteAddr:写入数据地址 

  142. *     NumHalfwordToWrite:数据长度,单位半字,即2字节 

  143. *返回:无 

  144. **********************************************************************/  

  145.   

  146. void FSMC_SRAM_WriteBuffer(uint16_t* pBuffer,uint32_t WriteAddr,uint32_t NumHalfwordToWrite)  

  147. {  

  148.     for(; NumHalfwordToWrite != 0; NumHalfwordToWrite--)  

  149.     {  

  150.         *(u16 *) (Bank1_SRAM2_ADDR + WriteAddr) = *pBuffer++;  

  151.         WriteAddr += 2;  

  152.     }     

  153. }  

  154.   

  155. /********************************************************************* 

  156. *                           读取数据包 

  157. *输入:pBuffer:存放数据的指针 

  158. *     ReadAddr:读取数据地址 

  159. *     NumHalfwordToRead:读取数据长度,单位半字,即2字节 

  160. *返回:无 

  161. **********************************************************************/  

  162.   

  163. void FSMC_SRAM_ReadBuffer(uint16_t* pBuffer, uint32_t ReadAddr, uint32_t NumHalfwordToRead)  

  164. {  

  165.     for(; NumHalfwordToRead != 0; NumHalfwordToRead--)  

  166.     {  

  167.         *pBuffer++ = *(vu16*) (Bank1_SRAM2_ADDR + ReadAddr);   

  168.         ReadAddr += 2;  

  169.     }    

  170. }  

  171.   

  172. /********************************************************************* 

  173. *                           写入半字数据 

  174. *输入:WriteAddr:写入数据地址 

  175. *     data:数据 

  176. *返回:无 

  177. **********************************************************************/  

  178.   

  179. void FSMC_SRAM_WriteHalfWord(uint32_t WriteAddr, uint16_t data)  

  180. {  

  181.     *(u16 *)(Bank1_SRAM2_ADDR + WriteAddr) = data;  

  182. }  

  183.   

  184. /********************************************************************* 

  185. *                           读取半字数据 

  186. *输入:ReadAddr:读取数据地址 

  187. *返回:读取的数据 

  188. **********************************************************************/  

  189.   

  190. uint16_t FSMC_SRAM_ReadHalfWord(uint32_t ReadAddr)  

  191. {  

  192.   return (*(vu16 *)((Bank1_SRAM2_ADDR + ReadAddr)));  

  193. }  

  194.    

  195.   

  196.   

  197.   

  198.   


测试代码:


main.c


  1. /********************************************************************* 

  2. *                        无线定位基站程序 

  3. *                             主文件 

  4. *                       (c)copyright 2013,jdh 

  5. *                         All Right Reserved 

  6. *文件名:main.c 

  7. *程序员:jdh 

  8. **********************************************************************/  

  9.   

  10. /********************************************************************* 

  11. *                           头文件 

  12. **********************************************************************/  

  13.   

  14. #include "public.h"  

  15.   

  16. uint8_t test_sram[100] __attribute__((at(Bank1_SRAM2_ADDR)));  

  17.   

  18. /********************************************************************* 

  19. *                           函数 

  20. **********************************************************************/  

  21.   

  22. int main(void)  

  23. {  

  24.     uint8_t i = 0;  

  25.     uint16_t buf1[3] = {1,2,3};  

  26.     uint16_t buf2[10] = {0};  

  27.       

  28.     //初始化设备  

  29.     init_device();  

  30.   

  31. #ifndef DEBUG     

  32.     //打开内部看门狗  

  33.     inf_enable_iwdg();  

  34. #endif  

  35.       

  36.     //sram测试代码  

  37.     //FSMC_SRAM_WriteHalfWord(0,1);  

  38.     //FSMC_SRAM_WriteHalfWord(2,2);  

  39.     //FSMC_SRAM_WriteHalfWord(4,3);  

  40.     //Test_Data = FSMC_SRAM_ReadHalfWord(2);  

  41.     //FSMC_SRAM_WriteBuffer(buf1,0,3);  

  42.     //FSMC_SRAM_ReadBuffer(buf2,0,3);  

  43.     test_sram[0] = 5;  

  44.     test_sram[1] = 2;  

  45.     test_sram[2] = 3;  

  46.     FSMC_SRAM_ReadBuffer(buf2,0,10);  

  47.     __nop();  

  48.   

  49.     //状态机执行  

  50.     while (1)  

  51.     {  

  52.         for (i = 0;i 

  53.         {  

  54.             //inf_set_led(i + 1,LED_ON);  

  55.             inf_delay_ms(10);  

  56.             //inf_set_led(i + 1,LED_OFF);  

  57.             inf_delay_ms(10);  

  58.         }  

  59.     }  

  60. }  

  61.   

  62. #ifdef  USE_FULL_ASSERT  

  63. /** 

  64.   * @brief  Reports the name of the source file and the source line number 

  65.   *         where the assert_param error has occurred. 

  66.   * @param  file: pointer to the source file name 

  67.   * @param  line: assert_param error line source number 

  68.   * @retval None 

  69.   */  

  70. void assert_failed(uint8_t* file, uint32_t line)  

  71. {   

  72.   /* User can add his own implementation to report the file name and line number, 

  73.      ex: printf("Wrong parameters value: file %s on line %d ", file, line) */  

  74.   

  75.   /* Infinite loop */  

  76.   while (1)  

  77.   {  

  78.   }  

  79. }  

  80. #endif  



关键字:STM32  单片机  FSMC接口  控制SRAM 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
AVR熔丝位操作时的要点和需要注意的相关事项