你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32单片机用FSMC接口控制SRAM

[复制链接]
STMCU小助手 发布时间:2021-8-6 13:07
环境:
主机:WIN7
开发环境:MDK4.72
MCU:STM32F103ZE


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


源代码:
inf_sram.h
  1. <font face="微软雅黑" size="3">/*********************************************************************
  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. *sram型号:IS62WV51216BLL
  13. *连接方式:FSMC
  14. *大小:1M字节.512K * 16
  15. **********************************************************************/

  16. #ifndef _INF_SRAM_H_
  17. #define _INF_SRAM_H_

  18. /*********************************************************************
  19. *                                                        头文件
  20. **********************************************************************/

  21. #include "stm32f10x.h"
  22. #include "stm32f10x_fsmc.h"

  23. /*********************************************************************
  24. *                                                        宏定义
  25. **********************************************************************/

  26. /*********************************************************************
  27. *                                                        SRAM2的BANK1起始地址
  28. **********************************************************************/

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

  30. /*********************************************************************
  31. *                                                        函数
  32. **********************************************************************/

  33. /*********************************************************************
  34. *                                                        初始化sram
  35. **********************************************************************/

  36. void inf_init_sram(void);

  37. /*********************************************************************
  38. *                                                        写入数据包
  39. *输入:pBuffer:数据指针
  40. *     WriteAddr:写入数据地址
  41. *     NumHalfwordToWrite:数据长度
  42. *返回:无
  43. **********************************************************************/

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

  45. /*********************************************************************
  46. *                                                        读取数据包
  47. *输入:pBuffer:存放数据的指针
  48. *     ReadAddr:读取数据地址
  49. *     NumHalfwordToRead:读取数据长度,单位半字,即2字节
  50. *返回:无
  51. **********************************************************************/

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

  53. /*********************************************************************
  54. *                                                        写入半字数据
  55. *输入:WriteAddr:写入数据地址
  56. *     data:数据
  57. *返回:无
  58. **********************************************************************/

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

  60. /*********************************************************************
  61. *                                                        读取半字数据
  62. *输入:ReadAddr:读取数据地址
  63. *返回:读取的数据
  64. **********************************************************************/

  65. uint16_t FSMC_SRAM_ReadHalfWord(uint32_t ReadAddr);

  66. #endif
  67. </font>
复制代码
inf_sram.c
  1. <font face="微软雅黑" size="3">/*********************************************************************
  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. *sram型号:IS62WV51216BLL
  13. *连接方式:FSMC
  14. *大小:1M字节.512K * 16
  15. **********************************************************************/

  16. /*********************************************************************
  17. *                                                        头文件
  18. **********************************************************************/

  19. #include "inf_sram.h"

  20. /*********************************************************************
  21. *                                                        函数
  22. **********************************************************************/

  23. /*********************************************************************
  24. *                                                        初始化sram
  25. **********************************************************************/

  26. void inf_init_sram(void)
  27. {
  28.         GPIO_InitTypeDef GPIO_InitStructure;
  29.         FSMC_NORSRAMTimingInitTypeDef p;
  30.         FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
  31.        
  32.         //开启FSMC时钟
  33.         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC,ENABLE);
  34.         //RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC | RCC_AHBPeriph_SRAM,ENABLE);
  35.         //开启FSMC相关的IO时钟
  36.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | \
  37.                                                    RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG | \
  38.                                                    RCC_APB2Periph_AFIO, ENABLE);
  39.         //FSMC相关的IO配置
  40.         //数据线
  41.         //PD口
  42.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_8 | \
  43.                                                                   GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_14 | \
  44.                                                                   GPIO_Pin_15;
  45.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  46.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  47.         GPIO_Init(GPIOD, &GPIO_InitStructure);
  48.         //PE口
  49.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | \
  50.                                                                   GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | \
  51.                                                                   GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  52.         GPIO_Init(GPIOE, &GPIO_InitStructure);
  53.        
  54.         //地址线
  55.         //PF口
  56.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | \
  57.                                                                   GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | \
  58.                                                                   GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | \
  59.                                                                   GPIO_Pin_15;
  60.         GPIO_Init(GPIOF, &GPIO_InitStructure);
  61.         //PG口
  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_Init(GPIOG, &GPIO_InitStructure);
  65.         //PD口
  66.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
  67.         GPIO_Init(GPIOD, &GPIO_InitStructure);
  68.        
  69.         //NOE和NWE配置
  70.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
  71.         GPIO_Init(GPIOD, &GPIO_InitStructure);

  72.         //NE2配置
  73.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  74.         GPIO_Init(GPIOG, &GPIO_InitStructure);

  75.         //NBL0, NBL1配置
  76.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  77.         GPIO_Init(GPIOE, &GPIO_InitStructure);

  78.         //FSMC配置
  79.         //地址建立时间
  80.         p.FSMC_AddressSetupTime = 0;
  81.         //地址保持时间
  82.         p.FSMC_AddressHoldTime = 0;
  83.         //数据建立时间
  84.         p.FSMC_DataSetupTime = 5;
  85.         //总线恢复时间
  86.         p.FSMC_BusTurnAroundDuration = 0;
  87.         //时钟分频因子
  88.         p.FSMC_CLKDivision = 0;
  89.         //数据产生时间
  90.         p.FSMC_DataLatency = 0;
  91.         //控制器时序
  92.         p.FSMC_AccessMode = FSMC_AccessMode_A;
  93.        
  94.         //总线参数配置
  95.         //使用FSMC的Bank1的字块2
  96.         FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM2;
  97.         //禁止地址数据线复用
  98.         FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
  99.         //存储类型为sram
  100.         FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
  101.         //存储器位宽16位
  102.         FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
  103.         //关闭突发模式访问
  104.         FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
  105.         //使能突发访问模式后才有效,等待信号极性
  106.         FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
  107.         //使能突发访问模式后才有效,非对齐成组模式
  108.         FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
  109.         //使能突发访问模式后才有效,配置等待时序
  110.         FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
  111.         //使能写操作
  112.         FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
  113.         //使能突发访问模式后才有效,关闭等待信号
  114.         FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
  115.         //扩展模式使能
  116.         FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
  117.         //成组写使能位
  118.         FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
  119.         //读操作时序操作
  120.         FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
  121.         //写操作时序参数
  122.         FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
  123.         //初始化FSMC总线
  124.         FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
  125.         //使能FSMC Bank1_SRAM Bank
  126.         FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, ENABLE);  
  127. }

  128. /*********************************************************************
  129. *                                                        写入数据包
  130. *输入:pBuffer:数据指针
  131. *     WriteAddr:写入数据地址
  132. *     NumHalfwordToWrite:数据长度,单位半字,即2字节
  133. *返回:无
  134. **********************************************************************/

  135. void FSMC_SRAM_WriteBuffer(uint16_t* pBuffer,uint32_t WriteAddr,uint32_t NumHalfwordToWrite)
  136. {
  137.         for(; NumHalfwordToWrite != 0; NumHalfwordToWrite--)
  138.         {
  139.                 *(u16 *) (Bank1_SRAM2_ADDR + WriteAddr) = *pBuffer++;
  140.                 WriteAddr += 2;
  141.         }   
  142. }

  143. /*********************************************************************
  144. *                                                        读取数据包
  145. *输入:pBuffer:存放数据的指针
  146. *     ReadAddr:读取数据地址
  147. *     NumHalfwordToRead:读取数据长度,单位半字,即2字节
  148. *返回:无
  149. **********************************************************************/

  150. void FSMC_SRAM_ReadBuffer(uint16_t* pBuffer, uint32_t ReadAddr, uint32_t NumHalfwordToRead)
  151. {
  152.         for(; NumHalfwordToRead != 0; NumHalfwordToRead--)
  153.         {
  154.                 *pBuffer++ = *(vu16*) (Bank1_SRAM2_ADDR + ReadAddr);
  155.                 ReadAddr += 2;
  156.         }  
  157. }

  158. /*********************************************************************
  159. *                                                        写入半字数据
  160. *输入:WriteAddr:写入数据地址
  161. *     data:数据
  162. *返回:无
  163. **********************************************************************/

  164. void FSMC_SRAM_WriteHalfWord(uint32_t WriteAddr, uint16_t data)
  165. {
  166.         *(u16 *)(Bank1_SRAM2_ADDR + WriteAddr) = data;
  167. }

  168. /*********************************************************************
  169. *                                                        读取半字数据
  170. *输入:ReadAddr:读取数据地址
  171. *返回:读取的数据
  172. **********************************************************************/

  173. uint16_t FSMC_SRAM_ReadHalfWord(uint32_t ReadAddr)
  174. {
  175.   return (*(vu16 *)((Bank1_SRAM2_ADDR + ReadAddr)));
  176. }
  177. </font>
复制代码
测试代码:
main.c
  1. <font face="微软雅黑" size="3">/*********************************************************************
  2. *                                             无线定位基站程序
  3. *                                                          主文件
  4. *                                                (c)copyright 2013,jdh
  5. *                                                  All Right Reserved
  6. *文件名:main.c
  7. *程序员:jdh
  8. **********************************************************************/

  9. /*********************************************************************
  10. *                                                        头文件
  11. **********************************************************************/

  12. #include "public.h"

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

  14. /*********************************************************************
  15. *                                                        函数
  16. **********************************************************************/

  17. int main(void)
  18. {
  19.         uint8_t i = 0;
  20.         uint16_t buf1[3] = {1,2,3};
  21.         uint16_t buf2[10] = {0};
  22.        
  23.         //初始化设备
  24.         init_device();

  25. #ifndef DEBUG       
  26.         //打开内部看门狗
  27.         inf_enable_iwdg();
  28. #endif
  29.        
  30.         //sram测试代码
  31.         //FSMC_SRAM_WriteHalfWord(0,1);
  32.         //FSMC_SRAM_WriteHalfWord(2,2);
  33.         //FSMC_SRAM_WriteHalfWord(4,3);
  34.         //Test_Data = FSMC_SRAM_ReadHalfWord(2);
  35.         //FSMC_SRAM_WriteBuffer(buf1,0,3);
  36.         //FSMC_SRAM_ReadBuffer(buf2,0,3);
  37.         test_sram[0] = 5;
  38.         test_sram[1] = 2;
  39.         test_sram[2] = 3;
  40.         FSMC_SRAM_ReadBuffer(buf2,0,10);
  41.         __nop();

  42.         //状态机执行
  43.         while (1)
  44.         {
  45.                 for (i = 0;i < 6;i++)
  46.                 {
  47.                         //inf_set_led(i + 1,LED_ON);
  48.                         inf_delay_ms(10);
  49.                         //inf_set_led(i + 1,LED_OFF);
  50.                         inf_delay_ms(10);
  51.                 }
  52.         }
  53. }

  54. #ifdef  USE_FULL_ASSERT
  55. /**
  56.   * @brief  Reports the name of the source file and the source line number
  57.   *         where the assert_param error has occurred.
  58.   * @param  file: pointer to the source file name
  59.   * @param  line: assert_param error line source number
  60.   * @retval None
  61.   */
  62. void assert_failed(uint8_t* file, uint32_t line)
  63. {
  64.   /* User can add his own implementation to report the file name and line number,
  65.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  66.   /* Infinite loop */
  67.   while (1)
  68.   {
  69.   }
  70. }
  71. #endif</font>
复制代码


收藏 评论0 发布时间:2021-8-6 13:07

举报

0个回答

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版