STM32 FSMC操作SRAM实例

 

#include "fsmc_sram.h"
#include <stdio.h>

static FSMC_NORSRAMInitTypeDef  FSMC_NORSRAMInitStructure;  /* 在函数内定义会出现莫名奇妙的错误 */
 
/*******************************************************************************
* Function Name  : FSMC_SRAM_Init
* Description    : Configures the FSMC and GPIOs to interface with the SRAM memory.
*       This function must be called before any write/read operation
*       on the SRAM.
* Input          : None
* Output         : None
* Return         : None
* Attention   : None
*******************************************************************************/
void FSMC_SRAM_Init(void)
{
 //FSMC_NORSRAMInitTypeDef  FSMC_NORSRAMInitStructure;
   FSMC_NORSRAMTimingInitTypeDef  p;
   GPIO_InitTypeDef GPIO_InitStructure;

 /*使能FSMC总线时钟*/
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
     
 /*使能FSMC总线使用的GPIO组时钟*/ 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF |
                         RCC_APB2Periph_GPIOG | RCC_APB2Periph_AFIO , ENABLE);

    /*FSMC数据线FSMC_D[0:15]初始化,推挽复用输出*/
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_8 | GPIO_Pin_9 |
                                  GPIO_Pin_10 | GPIO_Pin_14 | GPIO_Pin_15;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_Init(GPIOD, &GPIO_InitStructure);
 
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
                                  GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |
                                  GPIO_Pin_15;
   GPIO_Init(GPIOE, &GPIO_InitStructure);
 
    /*FSMC地址线FSMC_A[0:17]初始化,推挽复用输出*/
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |
                                  GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_12 | GPIO_Pin_13 |
                                  GPIO_Pin_14 | GPIO_Pin_15;
   GPIO_Init(GPIOF, &GPIO_InitStructure);
 
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |
                                  GPIO_Pin_4 | GPIO_Pin_5;
   GPIO_Init(GPIOG, &GPIO_InitStructure);
 
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
   GPIO_Init(GPIOD, &GPIO_InitStructure);
  
   /*FSMC NOE和NWE初试化,推挽复用输出*/ 
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 |GPIO_Pin_5;
   GPIO_Init(GPIOD, &GPIO_InitStructure);
 
   /*FSMC NE3初试化,推挽复用输出*/ 
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
   GPIO_Init(GPIOG, &GPIO_InitStructure);
 
   /*FSMC NBL0和NBL1初试化,推挽复用输出*/ 
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
   GPIO_Init(GPIOE, &GPIO_InitStructure);
 
/*--------------FSMC 总线 存储器参数配置------------------------------*/
   p.FSMC_AddressSetupTime = 0;           //地址建立时间   
   p.FSMC_AddressHoldTime = 0;            //地址保持时间 
   p.FSMC_DataSetupTime = 2;              //数据建立时间
   p.FSMC_BusTurnAroundDuration = 0;      //总线恢复时间
   p.FSMC_CLKDivision = 0;                // 时钟分频因子
   p.FSMC_DataLatency = 0;              //数据产生时间
   p.FSMC_AccessMode =  FSMC_AccessMode_A; //FSMC NOR控制器时序
   
/*--------------FSMC 总线 参数配置------------------------------*/
   FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM3;                  //使用了FSMC的BANK1的子板块3            
   FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;//禁止地址数据线复用
   FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;           //存储器类型为SRAM
   FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;  //存储器数据宽度为16位
   FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable; //关闭突发模式访问
    //等待信号优先级,只有在使能突发访问模式才有效
 FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
    //关闭Wrapped burst access mode,只有在使能突发访问模式才有效
   FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;        
    //等待信号设置,只有在使能突发访问模式才有效
   FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
   FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;     //使能这个BANK的写操作
    //使能/关闭等待信息设置,只在使能突发访问模式才有效
   FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;    
   FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable; //关闭Extend Mode
   FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;     //关闭Write Burst Mode  
   FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;               //读操作时序参数
   FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;                   //写操作时序参数

   FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);

/*--------------使能BANK1的子板块3------------------------------*/
   FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM3, ENABLE); 
}


/*******************************************************************************
* Function Name  : FSMC_SRAM_Test
* Description    : test SRAM
* Input          : None
* Output         : None
* Return         : None
* Attention   : None
*******************************************************************************/
void FSMC_SRAM_Test(void)
{
 __IO uint32_t addr;
 
 printf("SRAM TEST....\r\n");

 for( addr = 0x68000000; addr < 0x68080000; addr += 4 )
 {
     *(uint32_t *)addr = addr;
 }
 
 for( addr = 0x68000000; addr < 0x68080000; addr += 4 )
 {
  if (*(uint32_t *)addr != addr)
  {
   break;
  }
 }
   
 if( addr >= 0x68080000 )
 {
     printf("SRAM TEST OK....\r\n");
 }
 else
 {
  printf("SRAM error. Address = 0x%08X, Read = 0x%08X, Expected = 0x%08X \r\n", addr, *(uint32_t *)addr, addr);
 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值