| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- /*******************************************************************************
- * File Name : main.c
- * Author : Sim
- * Date First Issued : 12/28/2022
- * Description : application entry
- ********************************************************************************
- * History:
- * 12/28/2022: V0.1
- *******************************************************************************/
- /* Includes ------------------------------------------------------------------*/
- #include <stdio.h>
- #include <string.h>
- #include <intrinsics.h>
- #include "mpu_demo.h"
- #include "sfud.h"
- #include "amt630hv160_lib.h"
- #include "update.h"
- #include "secure_funcs.h"
- #include "fs/ff.h"
- #include "fs/diskio.h"
- #include "libhsm.h"
- #include "mailbox_message.h"
- #include "access_module.h"
- #if ADC_DEMO
- extern void adc_demo(void);
- #endif
- #ifdef MEDIA_UPDATE_SUPPORT
- extern void MediaUpdateReceiveDemo(void);
- #endif
- extern void StartCpu(void);
- extern void can_ecu_demo(void);
- extern void can_host_demo(void);
- #if RTC_DEMO
- extern void rtc_sleep_demo(void);
- extern void rtc_wakeup_demo(void);
- #endif
- void SystemInit(void)
- {
- /* disable systick */
- *(volatile u32*)SysTick_BASE = 0;
- #ifndef DEBUG
- NVIC_DeInit();
- #endif
- }
- static void prvSetupHardware( void )
- {
- /* Set the Vector Table base address at 0x20100000 */
- NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_6);
- __set_BASEPRI( 0 ); //ENABLE GLOBAL IRQ
- /* Configure the regions in the MPU that are common to all tasks. */
- prvSetupMPU();
- /* Init sema. */
- sema_init();
- /* Init Delay Timer. */
- TIMER_InitForDelay();
- /* Init Debug Uart */
- UART_Init(PRINT_UART, 115200, 0);
- /* Config the Clock */
- SYSCLK_Init();
- printf("sys_pll=%d\n", CLK_GetPLLFreq(SYSCTRL->SYSPLL_CFG));
- printf("cpu_pll=%d\n", CLK_GetPLLFreq(SYSCTRL->CPUPLL_CFG));
- printf("vpu_pll=%d\n", CLK_GetPLLFreq(SYSCTRL->VPUPLL_CFG));
- printf("ddr_pll=%d\n", CLK_GetPLLFreq(SYSCTRL->DDRPLL_CFG));
- printf("mfc_pll=%d\n", CLK_GetPLLFreq(SYSCTRL->MFCPLL_CFG));
- printf("ahb_pll=%d\n", CLK_GetPLLFreq(SYSCTRL->AHBPLL_CFG));
- printf("gpu_pll=%d\n", CLK_GetPLLFreq(SYSCTRL->GPUPLL_CFG));
- #ifdef _RTC
- RTC_SendCmd(RTC_Clk32KSel, 1);
- //rtc_time_init();
- #endif
- #ifdef _SPI0
- SPI0_SetBusClk(SPI0_FREQ);
- SPI0_SelectPad();
- #endif
- #ifdef _SDMMC0
- SDMMC0_SetBusClk(SDMMC0_FREQ);
- SDMMC0_SelectPad();
- #endif
- #ifdef _SDMMC1
- SDMMC1_SetBusClk(SDMMC1_FREQ);
- SDMMC1_SelectPad();
- #endif
- #ifdef _CANFD0
- CANFD0_SetBusClk(CANFD0_FREQ);
- #endif
- #ifdef _CANFD1
- CANFD1_SetBusClk(CANFD1_FREQ);
- #endif
- #ifndef SYSTICK_TIMER
- /* Configure HCLK clock as SysTick clock source. */
- SysTick_CLKSourceConfig( SysTick_CLKSource_CORECLK );
- #endif
- #ifdef _DMA
- DMA_Init();
- #endif
- #ifdef _SPI0
- dwspi_init();
- #endif
- #ifdef _I2C0
- i2c_dw_init(I2C_ID0);
- #endif
- #ifdef _I2C1
- i2c_dw_init(I2C_ID1);
- #endif
- #ifdef _MAILBOX
- mailbox_msg_init();
- #endif
- #ifdef _MCU_WDT
- WDT_Init();
- #endif
- #ifdef _ADC
- adc_hardware_init();
- #endif
- #ifdef _MCU_GPIO
- GPIO_IrqRegister();
- #endif
- #ifdef _RTC
- RTC_WakeupDetect();
- #if RTC_DEMO
- rtc_wakeup_demo();
- #endif
- #endif
- }
- int LoadFastboot(void)
- {
- header_offset_t *hoff = GetHeaderOff();
- u8 *buf = (u8*)MCU_IRAM_BASE;
- if ((hoff->fboffset == FASTBOOTA_OFFSET
- || hoff->fboffset == FASTBOOTB_OFFSET)
- && hoff->fbsize <= FASTBOOT_MAX_SIZE) {
- UPDATE_ReadData(hoff->fboffset, buf, hoff->fbsize);
- if (xcrc32((void*)buf, hoff->fbsize, 0xffffffff, HARD_CALC_CRC) != hoff->fbcrc) {
- TRACE_ERROR("Load fastboot checksum fail!\n");
- return -1;
- }
- return 0;
- }
- return -1;
- }
- int main(void)
- {
- #ifdef DEBUG
- debug();
- //disable interrupt
- NVIC_DeInit();
- #endif
- //1.8V低电压复位
- //LPR_TH 00:<1.45V 01:<1.41V 10:<1.38V 11:<1.35V
- SYSCTRL->ANA_REG[0] &= ~7;
- /*LPR_TH*/ /*LPR_EN*/
- SYSCTRL->ANA_REG[0] |= (0x3 << 1) | 1;
- //disable ecc error reset
- MCU_SYSCTRL->V6_RST_CTL &= ~(1UL << 31);
- prvSetupHardware();
- printf("amthv160 app %s %s\n", __DATE__, __TIME__);
- #if DEVICE_TYPE_SELECT != EMMC_FLASH
- #ifdef SPI0_QSPI_MODE
- sfud_flash *sflash;
- sfud_err result;
- u8 status;
- u8 qe_wsta_cmd = 0;
- u8 qe_sta_bit = 0;
- #endif
- if (sfud_init()) {
- printf("sfud_init fail!\n");
- while(1);
- }
- #ifdef SPI0_QSPI_MODE
- sflash = sfud_get_device(0);
- switch (sflash->chip.mf_id) {
- case SFUD_MF_ID_WINBOND:
- case SFUD_MF_ID_GIGADEVICE:
- qe_wsta_cmd = 0x31;
- qe_sta_bit = 1;
- break;
- default:
- break;
- }
- if(qe_wsta_cmd | qe_sta_bit) {
- //Write QE
- result = sfud_read_status(sflash, &status, qe_wsta_cmd | (1 << 2));
- if (result != SFUD_SUCCESS) {
- printf("sfud read QE status register fail\n");
- while(1);
- }
- if (!(status & (1 << qe_sta_bit))) {
- status |= (1 << qe_sta_bit);
- sfud_write_status(sflash, false, status, qe_wsta_cmd);
- if (result != SFUD_SUCCESS) {
- printf("sfud write QE status register fail\n");
- while(1);
- }
- }
- }
- sfud_qspi_fast_read_enable(sflash, 4);
- #endif
- #else
- if (EMMC_Init()) {
- printf("EMMC_Init fail!\n");
- while(1);
- }
- EMMC_SetBusWidth(4);
- #endif
- ReadHeaderOff();
- secure_module_init();
- #if CAN_DEMO
- RTC_CanStbOut(0, 0);
- RTC_CanStbOut(1, 0);
- #else
- UPDATE_ReadImageHeader();
- ReadSysInfo();
- StartCpu();
- #if ADC_DEMO
- adc_demo();
- #endif
- #endif
- #if RTC_DEMO
- rtc_sleep_demo();
- #endif
- for (;;) {
- #if CAN_DEMO
- #if CAN_DEMO == CAN_ECU_DEMO
- can_ecu_demo();
- #elif CAN_DEMO == CAN_HOST_DEMO
- can_host_demo();
- #endif
- #else
- RebootCpu();
- #ifdef MEDIA_UPDATE_SUPPORT
- MediaUpdateReceiveDemo();
- #endif
- #ifdef CPU_ACCESS_MODULE_SUPPORT
- DetectCpuAccessModule();
- #endif
- #if defined(_UART0)
- if(PRINT_UART != UART0)
- uart_demo(UART_ID0);
- #endif
- #if defined(_UART1)
- if(PRINT_UART != UART1)
- uart_demo(UART_ID1);
- #endif
- #endif
- }
- }
- #ifdef DEBUG
- /* Keep the linker happy. */
- void assert_failed( unsigned char* pcFile, unsigned long ulLine )
- {
- for( ;; )
- {
- }
- }
- #endif
- void HardFault_Handler(void)
- {
- u32 pc;
- u32 sp = 0;
- //xPSR, pc, lr, r12, r3, r2, r1, r0
- asm("mov %0, sp" : : "r"(sp));
- pc = *(volatile u32*)(sp + 0x20);
- printf("hardfault addr 0x%x\n", pc);
- while (1);
- }
- void vApplicationMallocFailedHook( void )
- {
- /* If configUSE_MALLOC_FAILED_HOOK is set to 1 then this function will
- be called automatically if a call to pvPortMalloc() fails. pvPortMalloc()
- is called automatically when a task, queue or semaphore is created. */
- for( ;; );
- }
- /*-----------------------------------------------------------*/
|