本帖最后由 TL-LED 于 2023-9-23 10:22 编辑
这篇来测试下开发板外扩SRAM存储器的读写。
一、外扩SRAM硬件电路
1.1、外扩SRAM电路图部分
1.2、SRAM存储器的地址范围
SRAM 接在STM32的FMC总线接口,看下手册中FMC外扩存储器的框图
SRAM存储器使用了FMC_NE1接口,对应的存储器地址如下
存储地址范围:0X6000 0000 ~ 0x6FFF FFFF
1.3、存储器容量
看下开发板使用的这款芯片的存储空间大小
从芯片手册上看,存储空间是8Mbit,512K*16位的存储空间。
二、测试程序
测试程序对sram芯片1MB范围内数据的读写测试,存储地址存取每次增加4000。
2.1、sram.c
#include "sram/sram.h"
#include "usart/usart.h"
#define BUFFER_SIZE ((uint32_t)0x8000)
#define WRITE_READ_ADDR ((uint32_t)0x0800)
#define WRITING_OFFSET ((uint32_t)0xC20F)
SRAM_HandleTypeDef psramHandle;
FMC_NORSRAM_TimingTypeDef Timing;
uint8_t aTxBuffer[BUFFER_SIZE];
uint8_t aRxBuffer[BUFFER_SIZE];
__IO uint32_t uwWriteReadStatus = 0;
uint32_t uwIndex = 0;
//uint32_t sram_test_buffer[200000] __attribute__((at(SRAM_BANK_ADDR)));
static void Error_Handler(void)
{
printf("error\r\n");
while (1)
{
}
}
void sram_Init(void)
{
/* SRAM device configuration */
psramHandle.Instance = FMC_NORSRAM_DEVICE;
psramHandle.Extended = FMC_NORSRAM_EXTENDED_DEVICE;
/* Timing configuration derived from system clock (up to 200Mhz)
for 100Mhz as SRAM clock frequency */
Timing.AddressSetupTime = 9;
Timing.AddressHoldTime = 2;
Timing.DataSetupTime = 6;
Timing.BusTurnAroundDuration = 1;
Timing.CLKDivision = 2;
Timing.DataLatency = 2;
Timing.AccessMode = FMC_ACCESS_MODE_A;
psramHandle.Init.NSBank = FMC_NORSRAM_BANK1;
psramHandle.Init.DataAddressMux = FMC_DATA_ADDRESS_MUX_DISABLE;
psramHandle.Init.MemoryType = FMC_MEMORY_TYPE_SRAM;
psramHandle.Init.MemoryDataWidth = SRAM_MEMORY_WIDTH;
psramHandle.Init.BurstAccessMode = FMC_BURST_ACCESS_MODE_DISABLE;
psramHandle.Init.WaitSignalPolarity = FMC_WAIT_SIGNAL_POLARITY_LOW;
psramHandle.Init.WaitSignalActive = FMC_WAIT_TIMING_BEFORE_WS;
psramHandle.Init.WriteOperation = FMC_WRITE_OPERATION_ENABLE;
psramHandle.Init.WaitSignal = FMC_WAIT_SIGNAL_DISABLE;
psramHandle.Init.ExtendedMode = FMC_EXTENDED_MODE_DISABLE;
psramHandle.Init.AsynchronousWait = FMC_ASYNCHRONOUS_WAIT_DISABLE;
psramHandle.Init.WriteBurst = FMC_WRITE_BURST_DISABLE;
psramHandle.Init.ContinuousClock = FMC_CONTINUOUS_CLOCK_SYNC_ONLY;
/* Initialize the SRAM controller */
if(HAL_SRAM_Init(&psramHandle, &Timing, &Timing) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
}
void sram_test(void)
{
uint32_t i = 0;
uint8_t temp = 0;
printf("ext memoory test\r\n");
printf("\r\nwrite data:\r\n");
temp=0;
for (i = 0; i < 1024000; i += 4000)
{
aTxBuffer[0]=temp;
temp++;
HAL_SRAM_Write_8b(&psramHandle, (uint32_t *)(SRAM_BANK_ADDR + i), aTxBuffer, 1);
printf(" %x",aTxBuffer[0]);
}
printf("\r\nread data:\r\n");
temp=0;
for (i = 0; i < 1024000; i += 4000)
{
HAL_SRAM_Read_8b(&psramHandle, (uint32_t *)(SRAM_BANK_ADDR + i), aRxBuffer, 1);
printf(" %x",aRxBuffer[0]);
aRxBuffer[0]=0;
}
}
2.2、sram.h
#ifndef _SRAM_H
#define _SRAM_H
#include "stm32f7xx_hal.h"
#define SRAM_BANK_ADDR ((uint32_t)0x60000000)
/* #define SRAM_MEMORY_WIDTH FMC_NORSRAM_MEM_BUS_WIDTH_8 */
#define SRAM_MEMORY_WIDTH FMC_NORSRAM_MEM_BUS_WIDTH_16
#define SRAM_TIMEOUT ((uint32_t)0xFFFF)
void sram_Init(void);
void sram_test(void);
#endif
2.3、main.c
int main(void)
{
MPU_Config(); /* Configure the MPU attributes */
CPU_CACHE_Enable(); /* Enable the CPU Cache */
HAL_Init();
SystemClock_Config(); /* Configure the system clock to 216 MHz */
delay_init(216);
led_init();
usart_init(115200);
sram_Init();
sram_test();
LED5(0);
LED6(1);
while (1)
{
LED5_TOGGLE();
LED6_TOGGLE();
HAL_Delay(100);
}
}
三、程序运行
串口输出: