#include <board.h>
#ifdef BSP_USING_SRAM
#include <drv_common.h>
#include <rtthread.h>
#include <rtdevice.h>
#define DBG_TAG "drv_exsram"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
#define RAM_HEAP_EX_START (0x68000000)
#define RAM_HEAP_EX_SIZE (1024 * 1024)
#define RAM_HEAP_EX_END (RAM_HEAP_EX_START + RAM_HEAP_EX_SIZE)
/* 注意:SRAM 句柄不能为局部变量 */
SRAM_HandleTypeDef SRAM_Handler; // SRAM句柄
#ifdef RT_USING_MEMHEAP_AS_HEAP
static struct rt_memheap system_heap;
#endif
/**
* @brief SRAM 初始化
* @retval None
*/
static int system_sram_init(void)
{
int result = RT_EOK;
FSMC_NORSRAM_TimingTypeDef FSMC_ReadWriteTim;
/*----------------------------------------- 使能时钟 -----------------------------------------*/
__HAL_RCC_GPIOD_CLK_ENABLE(); // 使能GPIOD时钟
__HAL_RCC_GPIOE_CLK_ENABLE(); // 使能GPIOE时钟
__HAL_RCC_GPIOF_CLK_ENABLE(); // 使能GPIOF时钟
__HAL_RCC_GPIOG_CLK_ENABLE(); // 使能GPIOG时钟
/*----------------------------------------- 配置 SRAM -----------------------------------------*/
SRAM_Handler.Instance = FSMC_NORSRAM_DEVICE;
SRAM_Handler.Extended = FSMC_NORSRAM_EXTENDED_DEVICE;
SRAM_Handler.Init.NSBank = FSMC_NORSRAM_BANK3; // 使用NE3
SRAM_Handler.Init.DataAddressMux = FSMC_DATA_ADDRESS_MUX_DISABLE; // 地址/数据线不复用
SRAM_Handler.Init.MemoryType = FSMC_MEMORY_TYPE_SRAM; // SRAM
SRAM_Handler.Init.MemoryDataWidth = FSMC_NORSRAM_MEM_BUS_WIDTH_16; // 16位数据宽度
SRAM_Handler.Init.BurstAccessMode = FSMC_BURST_ACCESS_MODE_DISABLE; // 是否使能突发访问,仅对同步突发存储器有效,此处未用到
SRAM_Handler.Init.WaitSignalPolarity = FSMC_WAIT_SIGNAL_POLARITY_LOW; // 等待信号的极性,仅在突发模式访问下有用
SRAM_Handler.Init.WaitSignalActive = FSMC_WAIT_TIMING_BEFORE_WS; // 存储器是在等待周期之前的一个时钟周期还是等待周期期间使能NWAIT
SRAM_Handler.Init.WriteOperation = FSMC_WRITE_OPERATION_ENABLE; // 存储器写使能
SRAM_Handler.Init.WaitSignal = FSMC_WAIT_SIGNAL_DISABLE; // 等待使能位,此处未用到
SRAM_Handler.Init.ExtendedMode = FSMC_EXTENDED_MODE_DISABLE; // 读写使用相同的时序
SRAM_Handler.Init.AsynchronousWait = FSMC_ASYNCHRONOUS_WAIT_DISABLE; // 是否使能同步传输模式下的等待信号,此处未用到
SRAM_Handler.Init.WriteBurst = FSMC_WRITE_BURST_DISABLE; // 禁止突发写
/*----------------------------------------- FSMC 读写时序控制 -----------------------------------------*/
FSMC_ReadWriteTim.AddressSetupTime = 0x00; // 地址建立时间(ADDSET)为1个HCLK 1/72M = 13.8ns
FSMC_ReadWriteTim.AddressHoldTime = 0x00; // 地址保持时间(ADDHLD)模式A未用到
FSMC_ReadWriteTim.DataSetupTime = 0x03; // 数据保存时间为3个HCLK = 4*13.8 = 55ns
FSMC_ReadWriteTim.BusTurnAroundDuration = 0X00;
FSMC_ReadWriteTim.CLKDivision = 0X00;
FSMC_ReadWriteTim.DataLatency = 0X00;
FSMC_ReadWriteTim.AccessMode = FSMC_ACCESS_MODE_A; // 模式A
if (HAL_SRAM_Init(&SRAM_Handler, &FSMC_ReadWriteTim, &FSMC_ReadWriteTim) != HAL_OK)
{
LOG_E("SDRAM init failed!");
result = -RT_ERROR;
}
else
{
#ifdef RT_USING_MEMHEAP_AS_HEAP
/* If RT_USING_MEMHEAP_AS_HEAP is enabled, SDRAM is initialized to the heap */
rt_memheap_init(&system_heap, "sdram", (void *)RAM_HEAP_EX_START, RAM_HEAP_EX_SIZE);
#else
rt_system_heap_init((void *)RAM_HEAP_EX_START, (void *)RAM_HEAP_EX_END);
#endif
}
return result;
}
INIT_BOARD_EXPORT(system_sram_init);
#endif /* BSP_USING_SRAM */
标签:HAL,ReadWriteTim,SRAM,FSMC,STM32,Init,Handler,HEAP
From: https://www.cnblogs.com/jzcn/p/17388482.html