[STM32F0] STM32通过FSMC读写CPLD

[复制链接]
653|1
 楼主| 范德萨发额 发表于 2021-8-6 11:04 | 显示全部楼层 |阅读模式

STM32通过FSMC读写CPLD的程序,CPLD挂在STM32的地址线和数据线上,将CPLD看做片外RAM的方式来进行读写,在我做的板子上CPLD挂在第四个区,因此基地址是0x6c000000,通过FSMC来进行读写,程序较为简单,具体的地方在函数中都有注释,仅供参考。



 楼主| 范德萨发额 发表于 2021-8-6 12:17 | 显示全部楼层
  1. phClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
  2. *******************************************************************************/
  3. #include "STM32Lib//stm32f10x.h"
  4. #include "hal.h"
  5. //使用第一块存储区,使用第四块,定义基地址
  6. #define Bank1_SRAM4_ADDR    ((uint32_t)0x6c000000)     
  7. /*******************************************************************************
  8. 名称:CPLD_Init(void)
  9. 功能:配置FSMC寄存器
  10. 参数:无
  11. 时间:2011.1.15
  12. 版本:1.0
  13. 注意:实际CPLD只用了8根地址线和8根数据线
  14.           按照模式A-SRAM/PSRAM(CRAM)OE翻转模式配置读写时序时序图在STM32技术手册P332
  15.           可以按照实际连接配置地址线数据线
  16. *******************************************************************************/
  17. void CPLD_Init(void)
  18. {
  19.        
  20.   FSMC_NORSRAMInitTypeDef  FSMC_NORSRAMInitStructure;
  21.   FSMC_NORSRAMTimingInitTypeDef  p;
  22.   GPIO_InitTypeDef GPIO_InitStructure;
  23.   
  24.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOG | RCC_APB2Periph_GPIOE |
  25.                          RCC_APB2Periph_GPIOF, ENABLE);
  26.   
  27. /*-- GPIO Configuration ------------------------------------------------------*/
  28.   /*!< SRAM Data lines configuration */
  29.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_8 | GPIO_Pin_9 |
  30.                                 GPIO_Pin_10 | GPIO_Pin_14 | GPIO_Pin_15;
  31.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  32.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  33.   GPIO_Init(GPIOD, &GPIO_InitStructure);
  34.   
  35.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
  36.                                 GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |
  37.                                 GPIO_Pin_15;
  38.   GPIO_Init(GPIOE, &GPIO_InitStructure);
  39.   
  40.   /*!< SRAM Address lines configuration */
  41.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |
  42.                                 GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_12 | GPIO_Pin_13 |
  43.                                 GPIO_Pin_14 | GPIO_Pin_15;
  44.   GPIO_Init(GPIOF, &GPIO_InitStructure);
  45.   
  46.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |
  47.                                 GPIO_Pin_4 | GPIO_Pin_5;
  48.   GPIO_Init(GPIOG, &GPIO_InitStructure);
  49.   
  50.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
  51.   GPIO_Init(GPIOD, &GPIO_InitStructure);
  52.    
  53.   /*!< NOE and NWE configuration */  
  54.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 |GPIO_Pin_5;
  55.   GPIO_Init(GPIOD, &GPIO_InitStructure);
  56.   
  57.   /*!< NE4 configuration */
  58.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  59.   GPIO_Init(GPIOG, &GPIO_InitStructure);
  60.   
  61.   /*!< NBL0, NBL1 configuration 有些芯片上需要进行高低字节使能,对于CPLD不需要*/
  62. //  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  63. //  GPIO_Init(GPIOE, &GPIO_InitStructure);
  64.   
  65. /*-- FSMC Configuration ------------------------------------------------------*/
  66.   p.FSMC_AddressSetupTime = 0;
  67.   p.FSMC_AddressHoldTime = 0;
  68.   p.FSMC_DataSetupTime = 1;
  69.   p.FSMC_BusTurnAroundDuration = 0;
  70.   p.FSMC_CLKDivision = 0;
  71.   p.FSMC_DataLatency = 0;
  72.   p.FSMC_AccessMode = FSMC_AccessMode_A;
  73.   FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM4;
  74.   FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
  75.   FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
  76.   FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b;
  77.   FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
  78.   FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;  
  79.   FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
  80.   FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
  81.   FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
  82.   FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
  83.   FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
  84.   FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
  85.   FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
  86.   FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
  87.   FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
  88.   FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
  89.   /*!< Enable FSMC Bank1_SRAM Bank */
  90.   FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM4, ENABLE);  
  91. }
  92. /*******************************************************************************
  93. 名称:CPLD_Write
  94. 功能:CPLD写时序
  95. 参数:uint8_t pBuffer-写入的数据 uint32_t WriteAddr-写入的地址
  96. 时间:2011.1.15
  97. 版本:1.0
  98. 注意:在硬件设计中使用了八根地址线和数据线,因此以八位的数据写入
  99. *******************************************************************************/
  100. void CPLD_Write(uint8_t pBuffer, uint32_t WriteAddr)
  101. {
  102.     *(uint32_t *) (Bank1_SRAM4_ADDR + WriteAddr) = pBuffer;  
  103. }
  104. /*******************************************************************************
  105. 名称:uint8_t SRAM_Read(uint32_t ReadAddr)
  106. 功能:CPLD读
  107. 参数:uint32_t ReadAddr需要读取的地址,返回读取的值
  108. 时间:2011.1.15
  109. 版本:1.0
  110. 注意:在硬件设计中使用了八根地址线和数据线,因此以八位的数据写入
  111. *******************************************************************************/
  112. uint8_t SRAM_Read(uint32_t ReadAddr)
  113. {
  114.         uint8_t pBuffer;
  115.         pBuffer = *(__IO uint32_t*) (Bank1_SRAM4_ADDR + ReadAddr);
  116.         return pBuffer;
  117. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

71

主题

1022

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部