2026-06-26 00:57:59 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @file esc_exmc_bus.cpp
|
|
|
|
|
|
* @brief LAN9252 ESC 的 EXMC 16-bit 并行总线驱动实现。
|
2026-07-16 23:00:52 +08:00
|
|
|
|
*
|
|
|
|
|
|
* 使用 GigaDevice gd32e50x_exmc 标准库。
|
|
|
|
|
|
* 注意:DFP 只把 FMC 注册为 CMSIS 组件,EXMC 未进组件列表;
|
|
|
|
|
|
* 源文件放在 RTE/Device/... 并由 cproject group device_exmc 本地编译。
|
2026-06-26 00:57:59 +08:00
|
|
|
|
*/
|
|
|
|
|
|
#include "comm/esc_exmc_bus.h"
|
|
|
|
|
|
#include "sys/config.h"
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
#include "gd32e50x_rcu.h"
|
|
|
|
|
|
#include "gd32e50x_gpio.h"
|
2026-07-16 23:00:52 +08:00
|
|
|
|
#include "gd32e50x_exmc.h" /* RTE/Device/GD32E508VE 或 -I 同目录 */
|
2026-06-26 00:57:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-16 23:00:52 +08:00
|
|
|
|
/* 内存映射访问宏:基址 + 偏移(按 16-bit 半字对齐,LAN9252 HBI 地址左移 1 位)。 */
|
2026-06-26 00:57:59 +08:00
|
|
|
|
#define ESC_EXMC_REG16(offset) (*(volatile uint16_t*)(CONFIG_ESC_EXMC_BASE_ADDR + (offset)))
|
|
|
|
|
|
|
|
|
|
|
|
void EscExmcBus::init()
|
|
|
|
|
|
{
|
|
|
|
|
|
rcu_periph_clock_enable(RCU_GPIOD);
|
|
|
|
|
|
rcu_periph_clock_enable(RCU_GPIOE);
|
|
|
|
|
|
rcu_periph_clock_enable(RCU_AF);
|
|
|
|
|
|
rcu_periph_clock_enable(RCU_EXMC);
|
|
|
|
|
|
|
|
|
|
|
|
/* 配置 16-bit 数据线 D0-D15 为 AF 推挽 */
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_D0_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_D0_PIN);
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_D1_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_D1_PIN);
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_D2_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_D2_PIN);
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_D3_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_D3_PIN);
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_D4_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_D4_PIN);
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_D5_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_D5_PIN);
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_D6_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_D6_PIN);
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_D7_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_D7_PIN);
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_D8_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_D8_PIN);
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_D9_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_D9_PIN);
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_D10_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_D10_PIN);
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_D11_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_D11_PIN);
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_D12_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_D12_PIN);
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_D13_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_D13_PIN);
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_D14_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_D14_PIN);
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_D15_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_D15_PIN);
|
|
|
|
|
|
|
|
|
|
|
|
/* 控制线:A16 / NOE / NWE / NE1 */
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_A16_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_A16_PIN);
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_NOE_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_NOE_PIN);
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_NWE_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_NWE_PIN);
|
|
|
|
|
|
gpio_init(CONFIG_EXMC_NE1_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, CONFIG_EXMC_NE1_PIN);
|
|
|
|
|
|
|
2026-07-16 23:00:52 +08:00
|
|
|
|
/*
|
|
|
|
|
|
* EXMC NOR/SRAM Region1:16-bit、非复用、异步写。
|
|
|
|
|
|
* 时序(AHB=180MHz,1 HCLK ≈ 5.56ns):
|
|
|
|
|
|
* LAN9252 HBI 最小读/写周期约 100ns → data_setup ≥ 18 HCLK。
|
|
|
|
|
|
* asyn_* 字段单位为 HCLK 计数(库文档范围 address_setup 1..16,data_setup 2..256)。
|
|
|
|
|
|
*/
|
2026-06-26 00:57:59 +08:00
|
|
|
|
exmc_norsram_parameter_struct norsram_init;
|
2026-07-16 23:00:52 +08:00
|
|
|
|
exmc_norsram_timing_parameter_struct rw_timing;
|
|
|
|
|
|
exmc_norsram_timing_parameter_struct wr_timing;
|
|
|
|
|
|
|
|
|
|
|
|
norsram_init.read_write_timing = &rw_timing;
|
|
|
|
|
|
norsram_init.write_timing = &wr_timing;
|
2026-06-26 00:57:59 +08:00
|
|
|
|
exmc_norsram_struct_para_init(&norsram_init);
|
2026-07-16 23:00:52 +08:00
|
|
|
|
|
|
|
|
|
|
norsram_init.norsram_region = CONFIG_ESC_EXMC_BANK_NOR_SRAM_REGION1;
|
|
|
|
|
|
norsram_init.write_mode = EXMC_ASYN_WRITE;
|
|
|
|
|
|
norsram_init.extended_mode = DISABLE;
|
|
|
|
|
|
norsram_init.asyn_wait = DISABLE;
|
|
|
|
|
|
norsram_init.nwait_signal = DISABLE;
|
|
|
|
|
|
norsram_init.memory_write = ENABLE;
|
|
|
|
|
|
norsram_init.nwait_config = EXMC_NWAIT_CONFIG_BEFORE;
|
|
|
|
|
|
norsram_init.wrap_burst_mode = DISABLE;
|
|
|
|
|
|
norsram_init.nwait_polarity = EXMC_NWAIT_POLARITY_LOW;
|
|
|
|
|
|
norsram_init.burst_mode = DISABLE;
|
|
|
|
|
|
norsram_init.databus_width = EXMC_NOR_DATABUS_WIDTH_16B;
|
|
|
|
|
|
norsram_init.memory_type = EXMC_MEMORY_TYPE_SRAM;
|
|
|
|
|
|
norsram_init.address_data_mux = DISABLE;
|
|
|
|
|
|
|
|
|
|
|
|
/* 读/写共用时序(非 extended mode) */
|
|
|
|
|
|
rw_timing.asyn_access_mode = EXMC_ACCESS_MODE_A;
|
|
|
|
|
|
rw_timing.syn_data_latency = EXMC_DATALAT_2_CLK;
|
|
|
|
|
|
rw_timing.syn_clk_division = EXMC_SYN_CLOCK_RATIO_DISABLE;
|
|
|
|
|
|
rw_timing.bus_latency = 1U;
|
|
|
|
|
|
rw_timing.asyn_address_setuptime = 2U; /* ~11ns */
|
|
|
|
|
|
rw_timing.asyn_address_holdtime = 2U; /* min 2 */
|
|
|
|
|
|
rw_timing.asyn_data_setuptime = 20U; /* ~111ns ≥ 100ns HBI */
|
|
|
|
|
|
|
|
|
|
|
|
wr_timing = rw_timing;
|
|
|
|
|
|
|
2026-06-26 00:57:59 +08:00
|
|
|
|
exmc_norsram_init(&norsram_init);
|
|
|
|
|
|
exmc_norsram_enable(CONFIG_ESC_EXMC_BANK_NOR_SRAM_REGION1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-29 01:51:02 +08:00
|
|
|
|
__attribute__((section(".ramfunc"), noinline))
|
2026-06-26 00:57:59 +08:00
|
|
|
|
uint16_t EscExmcBus::read16(uint32_t offset)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ESC_EXMC_REG16(offset);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-29 01:51:02 +08:00
|
|
|
|
__attribute__((section(".ramfunc"), noinline))
|
2026-06-26 00:57:59 +08:00
|
|
|
|
void EscExmcBus::write16(uint32_t offset, uint16_t data)
|
|
|
|
|
|
{
|
|
|
|
|
|
ESC_EXMC_REG16(offset) = data;
|
|
|
|
|
|
}
|