WXK
2024-09-18 05e2e954bd127de378a9d1dfbb0ed95d725aad63
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright (c) 2020-2021 Shanghai Panchip Microelectronics Co.,Ltd.
 *
 * SPDX-License-Identifier: Apache-2.0
 */
 
#include "signal_slot_manager.h"
#include "uart_dfu.h"
#include "usb_dfu.h"
#include "flash_manager.h"
#include "prf_ota.h"
#include "boot_config.c"
 
static void on_image_load_enter(void)
{
    fm_image_move(FLASH_AREA_BACK_UP_START, FLASH_AREA_IMAGE_START);
}
 
void sys_clock_init(void)
{
    /* Unlock protected registers */
    SYS_UnlockReg();
    
    ANA->LP_FSYN_LDO |= 0X1;
 
    CLK_XthStartupConfig();
    CLK->XTH_CTRL |= CLK_XTHCTL_XTH_EN_Msk;
    CLK_WaitClockReady(CLK_SYS_SRCSEL_XTH);
 
    CLK_HCLKConfig(0);
    CLK_SYSCLKConfig(CLK_DPLL_REF_CLKSEL_XTH,CLK_DPLL_OUT_48M); 
    CLK_RefClkSrcConfig(CLK_SYS_SRCSEL_DPLL);
    
    SYS_LockReg();
}
 
static void jump_to_app(void)
{
    void (*app_main)();
    
    uint32_t msp = *(volatile uint32_t*)(FLASH_AREA_IMAGE_START + sizeof(image_header_t));
    uint32_t addr = *(uint32_t*)(FLASH_AREA_IMAGE_START + sizeof(image_header_t) + 4);
    app_main = (void(*)())(uint32_t*)addr;
    
    FLCTL->X_FL_REMAP_ADDR = FLASH_AREA_IMAGE_START + sizeof(image_header_t); /* fmc remap */
    
    __set_MSP(msp);
    
    app_main();
}
 
int main(void)
{    
    sys_clock_init();
    
    /* when checking backup image is valid, the on_image_load_enter function will be handled */
    ss_connect(0, sig_back_up_is_completed_image, on_image_load_enter);
    
    #if BOOT_FROM_UART
    /* when detecting key1 down, the on_uart_dfu_enter function will be handled */
    ss_connect(1, sig_key1_push_down, on_uart_dfu_enter);
    #endif
    
    #if BOOT_FROM_USB
    /* when dectecting key2 down, the on_usb_dfu_enter function will be handled */
    ss_connect(2, sig_key2_push_down, on_usb_dfu_enter);
    #endif
    
    #if BOOT_FROM_PRF_OTA
    /* when receive a ota start packet, the on_prf_ota_enter function will be handled */
    ss_connect(3, sig_ota_start_received, on_prf_ota_enter);
    #endif
    
    /* handle all of events related signal fuction*/
    ss_events_handle(); 
    
    /* recovery gpio status that you used to trigger signal */
    sig_hardware_recovery();
    
    jump_to_app();
    
    return 0;  
}