STM32F4-DISCO 学习之 FSMC 驱动SRAM [模拟]

/ 0评 / 0

因为手边实在没有SRAM,要不就做板子,这样也太浪费时间和精力,何况我最终应该不会用到外部SRAM的,所以只好借助逻辑分析仪纸上谈兵了.

首先因为IO不足的问题,所以只能用MUX模式,而在MUX模式下,如果写外部储存器结构是SRAM,就不能进行NE片选,比较奇怪.常规SRAM接法应该是这样的:

	/*
		PE0 = NBL0 - CH13
		PE1 = NBL1 - CH14
		PD14 = DA0 - CH4
		PD15 = DA1 - CH5
		PD0 = DA2 - CH6
		PD1 = DA3 - CH7
		PE7 = DA4 - CH8
		PE8 = DA5 - CH9
		PE9 = DA6 - CH10
		PE10 = DA7 - CH11
		PE11 = DA8
		PE12 = DA9
		PE13 = DA10
		PE14 = DA11
		PE15 = DA12 - CH15
		PD8 = DA13
		PD9 = DA14
		PD10 = DA15
		PD11 = A16
		PD12 = A17
		PD13 = A18 - CH12
		PD4 = NOE - CH3
		PD5 = NWE - CH2
		PD7 = NE1 - CH1
		PB7 = NL - CH0
	*/

其中NL接573锁存器,因为公16位需要锁存的,锁存前为数据,锁存后的输出为地址.因为地址是不能读的,所以不能在锁存后.而573是低电平锁存,所以在573之前还要加一个非门,或者用其他逻辑[其他我还真不知道],内存初始化就简单多了,与之前一样是一堆复用,我们这次要用逻辑分析仪观察,所以时间不能设置太短.初始化OK后可以直接通过指针访问,编译器指定等等,非常方便哦.

void FSMC_SRAM_Init(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
    FSMC_NORSRAMInitTypeDef  FSMC_NORSRAMInitStructure;
    FSMC_NORSRAMTimingInitTypeDef  readWriteTiming;
    /*
    	PE0 = NBL0 - CH13
    	PE1 = NBL1 - CH14
    	PD14 = DA0 - CH4
    	PD15 = DA1 - CH5
    	PD0 = DA2 - CH6
    	PD1 = DA3 - CH7
    	PE7 = DA4 - CH8
    	PE8 = DA5 - CH9
    	PE9 = DA6 - CH10
    	PE10 = DA7 - CH11
    	PE11 = DA8
    	PE12 = DA9
    	PE13 = DA10
    	PE14 = DA11
    	PE15 = DA12 - CH15
    	PD8 = DA13
    	PD9 = DA14
    	PD10 = DA15
    	PD11 = A16
    	PD12 = A17
    	PD13 = A18 - CH12
    	PD4 = NOE - CH3
    	PD5 = NWE - CH2
    	PD7 = NE1 - CH1
    	PB7 = NL - CH0
    */
    /* PE7 - PE15 */
    /* PD8 - PD15 */
    /* PD0 - PD1 */
    /* PD5 - NWE,PD4 - NOE,PD7 - NE1,PB7 - NL */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE, ENABLE); //使能PB,PD,PE时钟
    RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE); //使能FSMC时钟
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 | 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;//PD0,1,4,5,8~15 AF OUT
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用输出
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
    GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | 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_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用输出
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
    GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用输出
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
    GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_FSMC); //PD0,AF12
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_FSMC); //PD1,AF12
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource4, GPIO_AF_FSMC);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_FSMC);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource7, GPIO_AF_FSMC);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_FSMC);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_FSMC);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource10, GPIO_AF_FSMC);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource11, GPIO_AF_FSMC);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_FSMC);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_FSMC);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_FSMC);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_FSMC); //PD15,AF12
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource0, GPIO_AF_FSMC); //PE7,AF12
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource1, GPIO_AF_FSMC);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_FSMC); //PE7,AF12
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource8, GPIO_AF_FSMC);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_FSMC);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource10, GPIO_AF_FSMC);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_FSMC);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource12, GPIO_AF_FSMC);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_FSMC);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_FSMC);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource15, GPIO_AF_FSMC); //PE15,AF12
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_FSMC);
    readWriteTiming.FSMC_AddressSetupTime = 0x03;
    readWriteTiming.FSMC_AddressHoldTime = 0x03;
    readWriteTiming.FSMC_DataSetupTime = 0x03;
    readWriteTiming.FSMC_BusTurnAroundDuration = 0x03;
    readWriteTiming.FSMC_CLKDivision = 0x03;
    readWriteTiming.FSMC_DataLatency = 0x03;
    readWriteTiming.FSMC_AccessMode = FSMC_AccessMode_A;	 //模式A
    FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;
    FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Enable;
    FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_NOR; // FSMC_MemoryType_SRAM;  //SRAM
    FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;//存储器数据宽度为16bit
    FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable; // FSMC_BurstAccessMode_Disable;
    FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
    FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
    FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
    FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
    FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;	//存储器写使能
    FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
    FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable; // 读写使用相同的时序
    FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
    FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &readWriteTiming;
    FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &readWriteTiming; //读写同样时序
    FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);  //初始化FSMC配置
    FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);  // 使能BANK1区域3
}

为了验证时序,我们在主程序,就做了这么一个设置:

while (1)
{
    *(volatile uint8_t *)(((uint32_t)(0x60000000))) = 0xAA;
    *(volatile uint8_t *)(((uint32_t)(0x60000004))) = 0xA4;
    *(volatile uint8_t *)(((uint32_t)(0x60000008))) = 0xA8;
    *(volatile uint8_t *)(((uint32_t)(0x60000010))) = 0x1A;
    *(volatile uint8_t *)(((uint32_t)(0x60000020))) = 0x2A;
    *(volatile uint8_t *)(((uint32_t)(0x60000040))) = 0x4A;
    *(volatile uint8_t *)(((uint32_t)(0x60000003))) = 0xA3;
    *(volatile uint8_t *)(((uint32_t)(0x60000009))) = 0xA9;
    *(volatile uint8_t *)(((uint32_t)(0x60000011))) = 0xBB;
    *(volatile uint8_t *)(((uint32_t)(0x60000025))) = 0xA6;
    *(volatile uint8_t *)(((uint32_t)(0x60000027))) = 0xA7;
    *(volatile uint8_t *)(((uint32_t)(0x60000044))) = 0x95;
    *(volatile uint8_t *)(((uint32_t)(0x6007FFFF))) = 0x65;
    *(volatile uint8_t *)(((uint32_t)(0x60080000))) = 0x75;
    temp = *(volatile uint8_t *)(((uint32_t)(0x60000035)));
    temp = *(volatile uint8_t *)(((uint32_t)(0x60000071)));
    temp = *(volatile uint8_t *)(((uint32_t)(0x60000064)));
    for(temp = 0; temp < 5; temp++)
    {
    }
    *(volatile uint16_t *)(((uint32_t)(0x60000000))) = 0xAABB;
    *(volatile uint16_t *)(((uint32_t)(0x60000004))) = 0xA44A;
    *(volatile uint16_t *)(((uint32_t)(0x60000008))) = 0xA88A;
    *(volatile uint16_t *)(((uint32_t)(0x60000010))) = 0x1AA1;
    *(volatile uint16_t *)(((uint32_t)(0x60000020))) = 0x2AA2;
    *(volatile uint16_t *)(((uint32_t)(0x60000040))) = 0x4AA4;
    *(volatile uint16_t *)(((uint32_t)(0x60000003))) = 0xA3A3;
    *(volatile uint16_t *)(((uint32_t)(0x60000009))) = 0xA99A;
    *(volatile uint16_t *)(((uint32_t)(0x60000011))) = 0xBBCC;
    *(volatile uint16_t *)(((uint32_t)(0x60000025))) = 0xA6FF;
    *(volatile uint16_t *)(((uint32_t)(0x60000027))) = 0xA7EE;
    *(volatile uint16_t *)(((uint32_t)(0x60000044))) = 0x95DD;
    *(volatile uint16_t *)(((uint32_t)(0x6007FFFF))) = 0x65AA;
    *(volatile uint16_t *)(((uint32_t)(0x60080000))) = 0x75BB;
    dtemp = *(volatile uint16_t *)(((uint32_t)(0x60000035)));
    dtemp = *(volatile uint16_t *)(((uint32_t)(0x60000071)));
    dtemp = *(volatile uint16_t *)(((uint32_t)(0x60000064)));
    for(temp = 0; temp < 5; temp++)
    {
    }

 通过抓去波形,看到两部分,第一部分是按Byte访问,第二部分是按Word访问(程序中可见):

QQ截图20150910135755

明确要知道当CS[NE]为低的时候,SRAM才有效,当NL为低的时候,因为有非门,所以573的LE是高,输出保持不变.573的真值表如下:

OE
LE
D
Q
L
H
H
H
L
H
L
L
L
L
X
Q0
H
X
X
Z

如图:

QQ截图20150910140201

接着等待写入数据:

QQ截图20150910140314

访问了一个奇数地址0x60000003(如果是0x0x60000002,那么实际访问写入地址是0x01,但是NBL0是低,NBL1是高):

QQ截图20150910140708

读取时候NOE拉低:

QQ截图20150910150931

读取双字节时候,能自动控制NBL.

QQ截图20150910151134

参考代码打包:

并口驱动SRAM

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注