mcu_ab568x/userboot240328/app/platform/bsp/bsp_sys.c
2025-05-30 18:03:10 +08:00

221 lines
6.1 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "include.h"
xcfg_cb_t xcfg_cb;
sys_cb_t sys_cb AT(.buf.bsp.sys_cb);
volatile u32 ticks_50ms;
uint8_t cfg_spiflash_speed_up_en = SPIFLASH_SPEED_UP_EN; //SPI FLASH提速。部份FLASH不支持提速
extern u32 __comm_start, __comm_end, __comm_size, __sram_size;
extern u32 __bss_start, __bss_end, __bss_size;
static u8 heap_func[HEAP_FUNC_SIZE] AT(.heap.func);
//timer tick interrupt(1ms)
AT(.com_text.timer)
void usr_tmr1ms_isr(void)
{
}
//timer tick interrupt(5ms)
AT(.com_text.timer)
void usr_tmr5ms_isr(void)
{
tmr5ms_cnt++;
//1s timer process
if ((tmr5ms_cnt % 200) == 0) {
msg_enqueue(MSG_SYS_1S);
tmr5ms_cnt = 0;
sys_cb.lpwr_warning_cnt++;
}
}
//UART0打印信息输出GPIO选择UART0默认G1(PA7)
void uart0_mapping_sel(void)
{
//等待uart0发送完成
if(UART0CON & BIT(0)) {
while (!(UART0CON & BIT(8)));
}
GPIOBPU &= ~(BIT(2) | BIT(3) | BIT(4));
FUNCMCON0 = (0xf << 12) | (0xf << 8); //clear uart0 mapping
#if (UART0_PRINTF_SEL == PRINTF_PB3)
GPIOBDE |= BIT(3);
GPIOBPU |= BIT(3);
GPIOBDIR |= BIT(3);
GPIOBFEN |= BIT(3);
FUNCMCON0 = URX0MAP_TX | UTX0MAP_PB3; //RX0 Map To TX0, TX0 Map to G1
#elif (UART0_PRINTF_SEL == PRINTF_PA1)
GPIOBDE |= BIT(1);
GPIOAPU |= BIT(1);
GPIOADIR |= BIT(1);
GPIOAFEN |= BIT(1);
GPIOADRV |= BIT(1);
FUNCMCON0 = URX0MAP_TX | UTX0MAP_PA1; //RX0 Map To TX0, TX0 Map to G2
#elif (UART0_PRINTF_SEL == PRINTF_PA3)
GPIOBDE |= BIT(3);
GPIOAPU |= BIT(3);
GPIOADIR |= BIT(3);
GPIOAFEN |= BIT(3);
GPIOADRV |= BIT(3);
FUNCMCON0 = URX0MAP_TX | UTX0MAP_PA3; //RX0 Map To TX0, TX0 Map to G3
#elif (UART0_PRINTF_SEL == PRINTF_PE3)
GPIOEDE |= BIT(3);
GPIOEPU |= BIT(3);
GPIOEDIR |= BIT(3);
GPIOEFEN |= BIT(3);
FUNCMCON0 = URX0MAP_TX | UTX0MAP_PE3; //RX0 Map To TX0, TX0 Map to G4
#elif (UART0_PRINTF_SEL == PRINTF_PE5)
GPIOEDE |= BIT(5);
GPIOEPU |= BIT(5);
GPIOEDIR |= BIT(5);
GPIOEFEN |= BIT(5);
FUNCMCON0 = URX0MAP_TX | UTX0MAP_PE5; //RX0 Map To TX0, TX0 Map to G5
#elif (UART0_PRINTF_SEL == PRINTF_PE7)
GPIOEDE |= BIT(7);
GPIOEPU |= BIT(7);
GPIOEDIR |= BIT(7);
GPIOEFEN |= BIT(7);
FUNCMCON0 = URX0MAP_TX | UTX0MAP_PE7; //RX0 Map To TX0, TX0 Map to G6
#elif (UART0_PRINTF_SEL == PRINTF_PF5)
GPIOFDE |= BIT(5);
GPIOFPU |= BIT(5);
GPIOFDIR |= BIT(5);
GPIOFFEN |= BIT(5);
FUNCMCON0 = URX0MAP_TX | UTX0MAP_PF5; //RX0 Map To TX0, TX0 Map to G7
#endif
}
AT(.text.bsp.sys.init)
bool rtc_init(void)
{
u32 temp;
printf("RTCCON0: %x, RTCCNT: %x\n", RTCCON0, RTCCNT);
temp = RTCCON0;
temp |= BIT(9) | BIT(8); //rtc clk sel x26m div
temp &= ~BIT(6); //enable clk32k_rtc
temp &= ~BIT(2); //disable osc_32k out
RTCCON0 = temp;
RTCCON2 = 16249; //26m / 16 / 100 - 1
return true;
}
AT(.rodata.vol)
const u8 maxvol_tbl[2] = {16, 32};
//开user timer前初始化的内容
AT(.text.bsp.sys.init)
static void bsp_var_init(void)
{
memset(&sys_cb, 0, sizeof(sys_cb));
func_heap_init(heap_func, HEAP_FUNC_SIZE);
sys_cb.loudspeaker_mute = 1;
sys_cb.ms_ticks = tick_get();
xcfg_cb.vol_max = maxvol_tbl[xcfg_cb.vol_max];
sys_cb.hfp2sys_mul = (xcfg_cb.vol_max + 2) / 16;
// if (SYS_INIT_VOLUME > xcfg_cb.vol_max) {
// SYS_INIT_VOLUME = xcfg_cb.vol_max;
// }
// if (WARNING_VOLUME > xcfg_cb.vol_max) {
// WARNING_VOLUME = xcfg_cb.vol_max;
// }
sys_cb.sleep_time = -1L;
sys_cb.pwroff_time = -1L;
sys_cb.guioff_delay = GUI_OFF_DELAY_TIME;
if (xcfg_cb.sys_sleep_time != 0) {
sys_cb.sleep_time = (u32)xcfg_cb.sys_sleep_time * 10; //100ms为单位
}
sys_cb.sleep_delay = -1L;
sys_cb.pwroff_delay = -1L;
sys_cb.sleep_en = 0;
sys_cb.lpwr_warning_times = LPWR_WARING_TIMES;
if ((!xcfg_cb.ft_osc_cap_en) || (xcfg_cb.osci_cap == 0 && xcfg_cb.osco_cap == 0)) { //没有过产测时使用自定义OSC电容
xcfg_cb.osci_cap = xcfg_cb.uosci_cap;
xcfg_cb.osco_cap = xcfg_cb.uosco_cap;
}
// port_var_init();
msg_queue_init();
}
AT(.text.bsp.sys.init)
static void bsp_io_init(void)
{
GPIOADE = BIT(7); //UART
GPIOBDE = 0;
GPIOEDE = 0;
GPIOFDE = 0;
GPIOGDE = 0x3F; //MCP FLASH
uart0_mapping_sel(); //调试UART IO选择或关闭
}
void bsp_get_xosc_xcfg(u8 *osci_cap, u8 *osco_cap, u8 *both_cap)
{
*osci_cap = xcfg_cb.osci_cap;
*osco_cap = xcfg_cb.osco_cap;
*both_cap = xcfg_cb.osc_both_cap;
}
//void print_comm_info(void)
//{
// printf("-------------------sram info----------------------\n");
// printf("sram total = 0x%X, used = 0x%X, remain = 0x%X\n", (u32)&__sram_size, (u32)&__comm_size+(u32)&__bss_size, (u32)&__sram_size - (u32)&__comm_size-(u32)&__bss_size);
// printf("comm: 0x%X ~ 0x%X size: 0x%X\n", (u32)&__comm_start, (u32)&__comm_end, (u32)&__comm_size);
// printf("bss : 0x%X ~ 0x%X size: 0x%X\n", (u32)&__bss_start, (u32)&__bss_end, (u32)&__bss_size);
// printf("--------------------------------------------------\n");
//}
AT(.text.bsp.sys.init)
void bsp_sys_init(void)
{
/// config
if (!xcfg_init(&xcfg_cb, sizeof(xcfg_cb))) { //获取配置参数
printf("xcfg init error\n");
}
// print_comm_info();
// io init
bsp_io_init();
// var init
bsp_var_init();
// power init
pmu_init(BUCK_MODE_EN*xcfg_cb.buck_mode_en);
// clock init
sys_clk_set(SYS_CLK_SEL);
// // peripheral init
// param_init(sys_cb.rtc_first_pwron);
rtc_init();
//晶振配置
xosc_init();
/// enable user timer for display & dac
sys_set_tmr_enable(1, 1);
bt_init();
#if BT_BACKSTAGE_EN
func_bt_init();
#endif
#if GUI_SELECT
tft_init();
#endif
}