stm32f429标准库sdram例程
时间: 2023-07-06 08:33:49 浏览: 170
STM32F429—SDRAM例子
5星 · 资源好评率100%
以下是基于STM32F429标准库的SDRAM例程:
```c
#include "stm32f4xx.h"
#include "stm32f429i_discovery_sdram.h"
#define SDRAM_TIMEOUT ((uint32_t)0xFFFF)
int main(void)
{
/* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG and AFIO clocks */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_GPIOG, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* Initialize SDRAM */
SDRAM_Init();
/* Write data to SDRAM */
SDRAM_WriteData(0xC0000000, 0x12345678);
/* Read data from SDRAM */
uint32_t data = SDRAM_ReadData(0xC0000000);
while (1);
}
void SDRAM_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable FMC clock */
RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FMC, ENABLE);
/* Configure SDNE1 and SDCKE1 pins */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_FMC);
/* Configure A0-A3 pins */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource3, GPIO_AF_FMC);
/* Configure D0-D3 pins */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_15 | GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource15, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource0, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource1, GPIO_AF_FMC);
/* Configure D4-D11 pins */
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_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource7, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource8, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource9, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource10, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource11, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource12, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource13, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource14, GPIO_AF_FMC);
/* Configure D12-D15 pins */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource0, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource1, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource2, GPIO_AF_FMC);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource3, GPIO_AF_FMC);
/* SDRAM initialization sequence */
FMC_SDRAMTimingInitTypeDef SDRAM_Timing_InitStructure;
FMC_SDRAMInitTypeDef SDRAM_InitStructure;
SDRAM_Timing_InitStructure.FMC_LoadToActiveDelay = 2;
SDRAM_Timing_InitStructure.FMC_ExitSelfRefreshDelay = 7;
SDRAM_Timing_InitStructure.FMC_SelfRefreshTime = 4;
SDRAM_Timing_InitStructure.FMC_RowCycleDelay = 7;
SDRAM_Timing_InitStructure.FMC_WriteRecoveryTime = 2;
SDRAM_Timing_InitStructure.FMC_RPDelay = 2;
SDRAM_Timing_InitStructure.FMC_RCDDelay = 2;
SDRAM_InitStructure.FMC_Bank = FMC_Bank2_SDRAM;
SDRAM_InitStructure.FMC_ColumnBitsNumber = FMC_ColumnBits_Number_8b;
SDRAM_InitStructure.FMC_RowBitsNumber = FMC_RowBits_Number_12b;
SDRAM_InitStructure.FMC_SDMemoryDataWidth = FMC_SDMemory_Width_16b;
SDRAM_InitStructure.FMC_InternalBankNumber = FMC_InternalBank_Number_4;
SDRAM_InitStructure.FMC_CASLatency = FMC_CAS_Latency_2;
SDRAM_InitStructure.FMC_WriteProtection = FMC_Write_Protection_Disable;
SDRAM_InitStructure.FMC_SDClockPeriod = FMC_SDClock_Period_2;
SDRAM_InitStructure.FMC_ReadBurst = FMC_Read_Burst_Disable;
SDRAM_InitStructure.FMC_ReadPipeDelay = FMC_ReadPipe_Delay_1;
SDRAM_InitStructure.FMC_SDRAMTimingStruct = &SDRAM_Timing_InitStructure;
FMC_SDRAMInit(&SDRAM_InitStructure);
/* SDRAM bank enable */
FMC_SDRAMCmdConfigTypeDef SDRAM_Cmd_InitStructure;
SDRAM_Cmd_InitStructure.FMC_CommandMode = FMC_Command_Mode_CLK_Enabled;
SDRAM_Cmd_InitStructure.FMC_CommandTarget = FMC_Command_Target_bank2;
SDRAM_Cmd_InitStructure.FMC_AutoRefreshNumber = 1;
SDRAM_Cmd_InitStructure.FMC_ModeRegisterDefinition = 0;
FMC_SDRAMCmdConfig(&SDRAM_Cmd_InitStructure);
/* Wait for SDRAM initialization */
uint32_t timeout = SDRAM_TIMEOUT;
while (FMC_GetFlagStatus(FMC_Bank2_SDRAM, FMC_FLAG_BUSY) != RESET)
{
if (timeout-- == 0)
{
/* SDRAM initialization error */
while (1);
}
}
}
void SDRAM_WriteData(uint32_t addr, uint32_t data)
{
/* Write data to SDRAM */
*(uint32_t *)addr = data;
}
uint32_t SDRAM_ReadData(uint32_t addr)
{
/* Read data from SDRAM */
return *(uint32_t *)addr;
}
```
这个例程用到了SDRAM外设,需要连接到MCU的外部引脚。具体的SDRAM引脚分配可以在STM32F429I Discovery板手册中找到。在本例程中,SDRAM连接到了MCU的Bank2,使用16位数据总线,12位行地址和8位列地址。在初始化SDRAM时,需要设置SDRAM的时序参数。在写入和读取数据时,需要将SDRAM地址转换为指针,并使用指针进行数据传输。
阅读全文