記錄自己學習的過程,歡迎交流

直接生成之後是無法直接使用。
添加函數,配置了圖形相關設定後該函數會自動生成。
#define SDRAM_MODEREG_BURST_LENGTH_1 ((uint16_t)0x0000)
#define SDRAM_MODEREG_BURST_LENGTH_2 ((uint16_t)0x0001)
#define SDRAM_MODEREG_BURST_LENGTH_4 ((uint16_t)0x0002)
#define SDRAM_MODEREG_BURST_LENGTH_8 ((uint16_t)0x0004)
#define SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL ((uint16_t)0x0000)
#define SDRAM_MODEREG_BURST_TYPE_INTERLEAVED ((uint16_t)0x0008)
#define SDRAM_MODEREG_CAS_LATENCY_2 ((uint16_t)0x0020)
#define SDRAM_MODEREG_CAS_LATENCY_3 ((uint16_t)0x0030)
#define SDRAM_MODEREG_OPERATING_MODE_STANDARD ((uint16_t)0x0000)
#define SDRAM_MODEREG_WRITEBURST_MODE_PROGRAMMED ((uint16_t)0x0000)
#define SDRAM_MODEREG_WRITEBURST_MODE_SINGLE ((uint16_t)0x0200)
void MX_SDRAM_InitEx(void)
{
__IO uint32_t tmpmrd = 0;
FMC_SDRAM_CommandTypeDef Command;
/* Step 1: Configure a clock configuration enable command */
Command.CommandMode = FMC_SDRAM_CMD_CLK_ENABLE;
Command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
Command.AutoRefreshNumber = 1;
Command.ModeRegisterDefinition = 0;
/* Send the command */
HAL_SDRAM_SendCommand(&hsdram1, &Command, 0x1000);
/* Step 2: Insert 100 us minimum delay */
/* Inserted delay is equal to 1 ms due to systick time base unit (ms) */
HAL_Delay(1);
/* Step 3: Configure a PALL (precharge all) command */
Command.CommandMode = FMC_SDRAM_CMD_PALL;
Command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
Command.AutoRefreshNumber = 1;
Command.ModeRegisterDefinition = 0;
/* Send the command */
HAL_SDRAM_SendCommand(&hsdram1, &Command, 0x1000);
/* Step 4: Configure an Auto Refresh command */
Command.CommandMode = FMC_SDRAM_CMD_AUTOREFRESH_MODE;
Command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
Command.AutoRefreshNumber = 8;
Command.ModeRegisterDefinition = 0;
/* Send the command */
HAL_SDRAM_SendCommand(&hsdram1, &Command, 0x1000);
/* Step 5: Program the external memory mode register */
tmpmrd = (uint32_t)SDRAM_MODEREG_BURST_LENGTH_1 |\
SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL |\
SDRAM_MODEREG_CAS_LATENCY_3 |\
SDRAM_MODEREG_OPERATING_MODE_STANDARD |\
SDRAM_MODEREG_WRITEBURST_MODE_SINGLE;
Command.CommandMode = FMC_SDRAM_CMD_LOAD_MODE;
Command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
Command.AutoRefreshNumber = 1;
Command.ModeRegisterDefinition = tmpmrd;
/* Send the command */
HAL_SDRAM_SendCommand(&hsdram1, &Command, 0x1000);
/* Step 6: Set the refresh rate counter */
/* Set the device refresh rate */
HAL_SDRAM_ProgramRefreshRate(&hsdram1, 683);
}
直接采用他人例程,使用的SDRAM_DEVICE_ADDR 0xD0000000是采用STM32中SDRAM Bank 2的位址
///*絕對定位方式通路SDRAM,這種方式必須定義成全局變量*/
#define SDRAM_DEVICE_ADDR 0xD0000000
uint8_t testValue __attribute__((at(SDRAM_DEVICE_ADDR)));
void test_sdram(void)
{
/*指針方式通路SDRAM*/
{
uint32_t temp;
printf("\r\n指針方式通路SDRAM\r\n");
/*向SDRAM寫入8位資料*/
*( uint8_t*) (SDRAM_DEVICE_ADDR ) = (uint8_t)0xAA;
printf("\r\n指針通路SDRAM,寫入資料0xAA \r\n");
/*從SDRAM讀取資料*/
temp = *( uint8_t*) (SDRAM_DEVICE_ADDR );
printf("讀取資料:0x%X \r\n",temp);
/*寫/讀 16位資料*/
*( uint16_t*) (SDRAM_DEVICE_ADDR+10 ) = (uint16_t)0xBBBB;
printf("指針通路SDRAM,寫入資料0xBBBB \r\n");
temp = *( uint16_t*) (SDRAM_DEVICE_ADDR+10 );
printf("讀取資料:0x%X \r\n",temp);
/*寫/讀 32位資料*/
*( uint32_t*) (SDRAM_DEVICE_ADDR+20 ) = (uint32_t)0xCCCCCCCC;
printf("指針通路SDRAM,寫入資料0xCCCCCCCC \r\n");
temp = *( uint32_t*) (SDRAM_DEVICE_ADDR+20 );
printf("讀取資料:0x%X \r\n",temp);
}
/*絕對定位方式通路SDRAM,這種方式必須定義成全局變量*/
{
testValue = 0xDD;
printf("\r\n絕對定位通路SDRAM,寫入資料0xDD,讀出資料0x%X,變量位址為%X\r\n",testValue,(uint32_t )&testValue);
}
}