/* Standard includes. */ #include #include #include /* FreeRTOS kernel includes. */ #include "FreeRTOS.h" #include "task.h" /* LVGL includes */ #include "lvgl.h" #include "fdb_app.h" #include "ff.h" #include "elog.h" #include "audio_scene.h" /* peripheral drivers */ #include "fr30xx.h" #include "driver_flash.h" #include "IC_W25Qxx.h" #include "app_config.h" #include "app_task.h" #include "app_at.h" #include "app_hw.h" #include "app_rpmsg.h" #include "fr_device_pmu_io.h" #include "fr_device_button.h" #include "ext_flash.h" #include "ext_flash_program.h" #include "host.h" #include "driver_pmu_iwdt.h" #include "fr_device_rtc.h" #include "fr_device_vbat.h" #include "driver_nv3047_rgb.h" #include "system_dwt.h" /* hardware handlers */ SD_HandleTypeDef sdio_handle; /* file system */ static FATFS fs; #if ENABLE_RTOS_MONITOR == 1 /* FreeRTOS running status monitor task */ static TaskHandle_t monitor_task_handle; volatile unsigned int CPU_RunTime; static uint8_t CPU_RunInfo[2048]; #endif void sys_wdt_init(void) { iwdt_Init_t iwdt_handle; iwdt_handle.iwdt_int_Enable = WDT_INT_DISABLE; iwdt_handle.iwdt_Timeout = 0xFFF; iwdt_handle.iwdt_Count = 32000 * 60; // 32K, timeout 3s iwdt_init(iwdt_handle); iwdt_Enable(); } #if ENABLE_RTOS_MONITOR == 1 static void monitor_task(void *arg) { vTaskDelay(5000); /* enable sleep */ system_prevent_sleep_clear(SYSTEM_PREVENT_SLEEP_TYPE_DISABLE); sys_wdt_init(); while(1) { vTaskDelay(30000); iwdt_Refresh(); rtc_running(); //battery detect adc_vbat_start_detect(); // memset(CPU_RunInfo,0,2048); // vTaskList((char *)&CPU_RunInfo); // printf("---------------------------------------------\r\n"); // printf("name state priority stack seq\r\n"); // printf("%s", CPU_RunInfo); // printf("---------------------------------------------\r\n"); // // memset(CPU_RunInfo,0,400); // vTaskGetRunTimeStats((char *)&CPU_RunInfo); // printf("name counter usage\r\n"); // printf("%s", CPU_RunInfo); // printf("---------------------------------------------\r\n"); } } #endif void spi_flash_cs_set(void) { #if(SPI_FLASH_GPIO_SEL==1) gpio_write_pin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET); #elif(SPI_FLASH_GPIO_SEL==2) gpio_write_pin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET); #endif } void spi_flash_cs_clear(void) { #if(SPI_FLASH_GPIO_SEL==1) gpio_write_pin(GPIOC, GPIO_PIN_9, GPIO_PIN_CLEAR); #elif(SPI_FLASH_GPIO_SEL==2) gpio_write_pin(GPIOB, GPIO_PIN_1, GPIO_PIN_CLEAR); #endif } void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName ) { ( void ) pcTaskName; ( void ) pxTask; log_i("%s", pcTaskName); assert( 0 ); } void vApplicationTickHook(void) { //gui tick counter add here! lv_tick_inc(1); #if ENABLE_RTOS_MONITOR == 1 CPU_RunTime++; #endif } __RAM_CODE bool user_deep_sleep_check(void) { return host_before_sleep_check(); } __RAM_CODE void user_entry_before_sleep(void) { //fputc('S', NULL); /* wait for UART TX done */ system_delay_us(20); hw_gpio_save(); ool_write(PMU_REG_PMU_GATE_M, ool_read(PMU_REG_PMU_GATE_M) | 0x40); } void wdt_rst_start(void) { iwdt_Init_t iwdt_handle; iwdt_handle.iwdt_int_Enable = WDT_INT_DISABLE; iwdt_handle.iwdt_Timeout = 0xFFF; iwdt_handle.iwdt_Count = 32000 * 3; // 32K, timeout 3s iwdt_init(iwdt_handle); iwdt_Enable(); } __RAM_CODE void user_entry_after_sleep(void) { /* configure all interrupt priority to 2 */ *(volatile uint32_t *)0xE000E400 = 0x40404040; *(volatile uint32_t *)0xE000E404 = 0x40404040; *(volatile uint32_t *)0xE000E408 = 0x40404040; *(volatile uint32_t *)0xE000E40C = 0x40404040; *(volatile uint32_t *)0xE000E410 = 0x40404040; *(volatile uint32_t *)0xE000E414 = 0x40404040; *(volatile uint32_t *)0xE000E418 = 0x40404040; *(volatile uint32_t *)0xE000E41C = 0x40404040; *(volatile uint32_t *)0xE000E420 = 0x40404040; *(volatile uint32_t *)0xE000E424 = 0x40404040; *(volatile uint32_t *)0xE000E428 = 0x40404040; *(volatile uint32_t *)0xE000E42C = 0x40404040; *(volatile uint32_t *)0xE000E430 = 0x40404040; *(volatile uint32_t *)0xE000E434 = 0x40404040; *(volatile uint32_t *)0xE000E438 = 0x40404040; *(volatile uint32_t *)0xE000E43C = 0x40404040; *(volatile uint32_t *)0xE000E440 = 0x40404040; hw_gpio_restore(); ool_write(PMU_REG_PMU_GATE_M, ool_read(PMU_REG_PMU_GATE_M) & (~0x40)); hw_clock_init(); host_hci_reinit(); hw_xip_flash_init(true); hw_log_init(true); #if (BOARD_SEL != BOARD_EVB_FR3092E_RGB) hw_display_init(true); hw_touchpad_init(true); hw_external_flash_init(true); #endif #if ENABLE_PSRAM hw_psram_init(true); #endif #if DSP_ROM_CODE_XIP hw_dsp_xip_flash_init(true); #endif NVIC_SetPriority(PMU_IRQn, 4); NVIC_EnableIRQ(PMU_IRQn); //fputc('W', NULL); app_rpmsg_recover(); } int main( void ) { GPIO_InitTypeDef gpio_config; UART_HandleTypeDef dsp_uart_handle; uint32_t error; uint32_t rand_num; size_t size; /* reserve 1s to program data with jlink when sleep or spll is enabled. */ system_delay_us(1000000); pmu_init(); // /* power on DSP flash */ // ool_write(0xfc, ool_read(0xfc) | 0x01); /* power on codec */ ool_write(0xc3, 0x27); #if 0 /* set DSP DLDO to 1.3v */ ool_write(PMU_REG_DSP_DLDO_CTRL, 0x06); /* short DSP LDO and APP LDO, set APP DLDO to 1.3v */ ool_write(PMU_REG_APP_DLDO_CTRL, 0x86); #else /* set SYSTEM BUCK to 1.35v, set DSP LDO and APP LDO to bypass mode */ ool_write(PMU_REG_SYSBUCK_CTRL_0, 0x04); ool_write(PMU_REG_DSP_DLDO_CTRL, ool_read(PMU_REG_DSP_DLDO_CTRL)| 0x80); ool_write(PMU_REG_APP_DLDO_CTRL, ool_read(PMU_REG_APP_DLDO_CTRL)| 0x40); #endif /* initial system clock and XIP flash */ hw_clock_init(); hw_xip_flash_init(false); /* try to handshake with PC to program external flash */ ext_flash_program(); system_dwt_init(); /* reinit external flash */ // hw_external_flash_init(false); /* configure all interrupt priority to 2 */ *(volatile uint32_t *)0xE000E400 = 0x40404040; *(volatile uint32_t *)0xE000E404 = 0x40404040; *(volatile uint32_t *)0xE000E408 = 0x40404040; *(volatile uint32_t *)0xE000E40C = 0x40404040; *(volatile uint32_t *)0xE000E410 = 0x40404040; *(volatile uint32_t *)0xE000E414 = 0x40404040; *(volatile uint32_t *)0xE000E418 = 0x40404040; *(volatile uint32_t *)0xE000E41C = 0x40404040; *(volatile uint32_t *)0xE000E420 = 0x40404040; *(volatile uint32_t *)0xE000E424 = 0x40404040; *(volatile uint32_t *)0xE000E428 = 0x40404040; *(volatile uint32_t *)0xE000E42C = 0x40404040; *(volatile uint32_t *)0xE000E430 = 0x40404040; *(volatile uint32_t *)0xE000E434 = 0x40404040; *(volatile uint32_t *)0xE000E438 = 0x40404040; *(volatile uint32_t *)0xE000E43C = 0x40404040; *(volatile uint32_t *)0xE000E440 = 0x40404040; // pmu_reg_write(0xe4, (pmu_reg_read(0xe4)&0xF0)|0x03); //3.3V IO pmu_reg_write(0xe6, (pmu_reg_read(0xe6)&0xF0)|0x0F); //1.8V IO 将1.8V io电压抬到最高 // /* initialize uart for DSP UART */ // /* ========================================================== */ // /* ========= Uart LOG configuration ========= */ // /* ========================================================== */ // /* configure PA4 and PA5 to UART1 function */ // gpio_config.Pin = GPIO_PIN_4 | GPIO_PIN_5; // gpio_config.Mode = GPIO_MODE_AF_PP; // gpio_config.Pull = GPIO_PULLUP; // gpio_config.Alternate = GPIO_FUNCTION_1; // gpio_init(GPIOA, &gpio_config); // /* UART1: used for DSP Log */ // __SYSTEM_UART1_CLK_ENABLE(); // dsp_uart_handle.UARTx = UART1; // dsp_uart_handle.Init.BaudRate = 3000000; // dsp_uart_handle.Init.DataLength = UART_DATA_LENGTH_8BIT; // dsp_uart_handle.Init.StopBits = UART_STOPBITS_1; // dsp_uart_handle.Init.Parity = UART_PARITY_NONE; // dsp_uart_handle.Init.FIFO_Mode = UART_FIFO_ENABLE; // dsp_uart_handle.TxCpltCallback = NULL; // dsp_uart_handle.RxCpltCallback = NULL; // uart_init(&dsp_uart_handle); /* initialize uart for log and AT command */ app_at_init(); /* initialize EasyLogger */ elog_init(); /* set EasyLogger log format */ elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_LVL); elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_LVL); elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL); elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL); elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_LVL); elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_LVL); // elog_set_text_color_enabled(true); /* start EasyLogger */ elog_start(); log_a("Hello ELOG_LVL_ASSERT!"); log_e("Hello ELOG_LVL_ERROR!"); log_w("Hello ELOG_LVL_WARN!"); log_i("Hello ELOG_LVL_INFO!"); log_d("Hello ELOG_LVL_DEBUG!"); log_v("Hello ELOG_LVL_VERBOSE!"); #if DSP_ROM_CODE_XIP hw_dsp_xip_flash_init(false); #endif /* flashdb is used to manage user enviroments stored in internal flash (XIP flash) */ flashdb_init(); /* get random seed*/ size = flashdb_get(FDB_KEY_USER_RANDOM_SEED, (void *)&rand_num, 4); printf("flashdb get random seed :%d\r\n",size); if(size == 0){ __SYSTEM_TRNG_CLK_ENABLE(); trng_init(); trng_read_rand_num((uint8_t *)&rand_num,4); flashdb_set(FDB_KEY_USER_RANDOM_SEED,(uint8_t *)&rand_num,4); __SYSTEM_TRNG_CLK_DISABLE(); } printf("flash db get rand num: %x\r\n",rand_num); /* * initialize modules step by step * 1. app_task * 2. rpmsg * 3. btdm stack * 4. lvgl * 5. others..... */ #if ENABLE_RTOS_MONITOR == 1 xTaskCreate(monitor_task, "monitor", MONITOR_TASK_STACK_SIZE, NULL, tskIDLE_PRIORITY+1, &monitor_task_handle); #endif audio_scene_init(AUDIO_SCENE_TASK_STACK_SIZE, AUDIO_SCENE_TASK_PRIORITY); app_task_init(); /* * enable pull up of all 3.3v IO, these configuration will be latched by set * BIT6 of PMU_REG_PMU_GATE_M regsiter. used to avoid electric leakage */ SYSTEM->PortA_PullSelect = 0x0000ff9f; SYSTEM->PortB_PullSelect = 0x00000fff; SYSTEM->PortC_PullSelect = 0x00000000; SYSTEM->PortD_PullSelect = 0x0000ffff; SYSTEM->PortA_PullEN = 0x00007fff; SYSTEM->PortB_PullEN = 0x00000fff; SYSTEM->PortC_PullEN = 0x00000000; SYSTEM->PortD_PullEN = 0x0000ffff; SYSTEM->QspiPadConfig.QSPI_PullSelect = 0x00000000; SYSTEM->QspiPadConfig.QSPI_PullEN = 0xffffffff; SYSTEM->OspiPadConfig.OSPI_PullSelect = 0x00000000; SYSTEM->OspiPadConfig.OSPI_PullEN = 0xffffffff; /* IO33 always on, IO18 off in sleep mode */ ool_write(0x63, 0x08); __SYSTEM_GPIOB_CLK_ENABLE(); gpio_config.Pin = GPIO_PIN_12; gpio_config.Mode = GPIO_MODE_OUTPUT_PP; gpio_config.Pull = GPIO_PULLDOWN; gpio_config.Alternate = GPIO_FUNCTION_0; gpio_init(GPIOB, &gpio_config); gpio_write_pin(GPIOB, GPIO_PIN_12, GPIO_PIN_CLEAR); // motor en: default output low __SYSTEM_GPIOA_CLK_ENABLE(); gpio_config.Pin = GPIO_PIN_5; gpio_config.Mode = GPIO_MODE_OUTPUT_PP; gpio_config.Pull = GPIO_PULLUP; gpio_config.Alternate = GPIO_FUNCTION_0; gpio_init(GPIOA, &gpio_config); gpio_write_pin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // HR sensor power en: default output high /* Start the scheduler itself. */ vTaskStartScheduler(); return 0; } #if 1 void dma0_irq(void) { #if (BOARD_SEL == BOARD_EVB_FR3092E_RGB) rgb_display_dma_irq(); #else IC_W25Qxx_DMA_Interrupt(); #endif } #endif //void dma1_irq(void) //{ // printf("dma1_irq\r\n"); // IC_W25Qxx_DMA_Interrupt(); // // //} void spimx8_0_irq(void) { IC_W25Qxx_Spi_Interrupt(); }