425 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			425 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
|  | 
 | ||
|  | #include "os_types.h"
 | ||
|  | #include "os_task.h"
 | ||
|  | #include "os_mem.h"
 | ||
|  | 
 | ||
|  | #include "dbg_io.h"
 | ||
|  | #include "iot_diag.h"
 | ||
|  | #include "iot_io.h"
 | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | //#include <stdlib.h>
 | ||
|  | #include "iot_system_api.h"
 | ||
|  | #include "iot_config.h"
 | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | //#include "iot_gptmr.h"
 | ||
|  | #include "iot_system.h"
 | ||
|  | //#include "os_mem.h"
 | ||
|  | //#include "cpu.h"
 | ||
|  | //#include "os_task.h"
 | ||
|  | 
 | ||
|  | #include "platform.h"
 | ||
|  | #include "encoding.h"
 | ||
|  | 
 | ||
|  | //#include "sec_glb.h"
 | ||
|  | //#include "ahb.h"
 | ||
|  | //#include "intc.h"
 | ||
|  | 
 | ||
|  | #include "bits.h"
 | ||
|  | 
 | ||
|  | #include "os_utils.h"
 | ||
|  | #include "iot_gpio_api.h"
 | ||
|  | #include "pmm_hw.h"
 | ||
|  | 
 | ||
|  | //#define NONSECURE
 | ||
|  | 
 | ||
|  | //#define HIBERNATE
 | ||
|  | //#define PUSH_BUTTON
 | ||
|  | //#define DEEP_SLEEP
 | ||
|  | #define SOFT_RESET
 | ||
|  | 
 | ||
|  | 
 | ||
|  | //#define FORCE
 | ||
|  | //#define IO_WKUP
 | ||
|  | 
 | ||
|  | static int32_t iot__platform_init() | ||
|  | { | ||
|  |     /*platform intialization*/ | ||
|  |     platform_init(); | ||
|  | 
 | ||
|  |     //resource initializations;
 | ||
|  | //    system_clock_init();
 | ||
|  | 
 | ||
|  | //    system_uart_init();
 | ||
|  | 
 | ||
|  |     dbg_uart_init(); | ||
|  | 
 | ||
|  |    // dbg_uart_stage1_init();
 | ||
|  | 
 | ||
|  |   //  iot_led_init();
 | ||
|  | 
 | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | 
 | ||
|  | void user_task_1() | ||
|  | { | ||
|  | 
 | ||
|  | // power off bbl
 | ||
|  | #ifdef NONSECURE
 | ||
|  |     power_off_bbl(); | ||
|  | #endif
 | ||
|  | 
 | ||
|  | //PUSH_BUTTON test//////////////////////
 | ||
|  | #ifdef PUSH_BUTTON
 | ||
|  |     uint8_t    wakeup_from_pb_flag = 0; // push_button flag
 | ||
|  | 
 | ||
|  |     if (wake_up_from_pb_check()) { | ||
|  |         wakeup_from_pb_flag = 1; | ||
|  |     } | ||
|  | 
 | ||
|  |     if(wakeup_from_pb_flag) {                     // wake up from push_button
 | ||
|  |         iot_printf("--------------------wake up from push button mode ------------------\n"); | ||
|  |     }//else {
 | ||
|  |         os_delay(2000); | ||
|  |         iot_printf("--------------------config to enter push button mode ------------------\n"); | ||
|  |         #ifdef FORCE
 | ||
|  |         iot_printf("force enter into push_button low power mode\n"); | ||
|  |         os_delay(400); | ||
|  |         enter_push_button_mode(1); | ||
|  |         #else
 | ||
|  |         iot_printf("auto enter into push_button low power mode\n"); | ||
|  |         os_delay(400); | ||
|  |         enter_push_button_mode(0); | ||
|  |         #endif
 | ||
|  |    // }
 | ||
|  | #endif
 | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | //HIBERNATE test////////////////////////
 | ||
|  | #ifdef HIBERNATE
 | ||
|  |     uint32_t rdata[3] = {0}; | ||
|  |     volatile uint8_t error = 0; | ||
|  |     volatile uint8_t i; | ||
|  |     uint8_t    wakeup_from_hiber_flag = 0; // hibernate flag
 | ||
|  |     uint32_t check_data[100]={0}; | ||
|  |     uint32_t pattern[2] = {0x66888866,0x66888866}; | ||
|  | 
 | ||
|  |     if (wake_up_from_hibernate_check()){ | ||
|  |         wakeup_from_hiber_flag = 1; | ||
|  |     } | ||
|  | 
 | ||
|  |     if(wakeup_from_hiber_flag) {           // wake up from hibernate
 | ||
|  |         iot_printf("--------------------wake up from hibernate mode ------------------\n"); | ||
|  |         //check saved pattern
 | ||
|  |         low_power_mode_read_data(hibernate,rdata,2); | ||
|  |         read_hiber_rtc_value(&rdata[2]); | ||
|  |         iot_printf("hibernate mode : check saved pattern ana0 = %x, ana1 = %x,rtc = %x\n", rdata[0],rdata[1],rdata[2]); | ||
|  |         if((rdata[0] != 0x66888866) || (rdata[1] != 0x66888866) || (rdata[2] != 0x66888866)) { | ||
|  |             error++; | ||
|  |         } | ||
|  | 
 | ||
|  |         //check pmm mem can read and write
 | ||
|  |         for(i=0; i<100; i++) {                   // access pmm mem
 | ||
|  |             *(check_data+i) = i; | ||
|  |         } | ||
|  |         write_pmm_16k_ram(0,check_data,100); | ||
|  | 
 | ||
|  |         for(i=0; i<100; i++) {                   // access pmm mem
 | ||
|  |             *(check_data+i) = 0; | ||
|  |         } | ||
|  | 
 | ||
|  |         read_pmm_16k_ram(0,check_data,100); | ||
|  | 
 | ||
|  |         for(i=0; i<100; i++) { | ||
|  |             if(check_data[i] != i) error++; | ||
|  |         } | ||
|  | 
 | ||
|  |         //check if dtop register back to default value
 | ||
|  |         rdata[0] =  *(volatile uint32_t *)(0x03280000); | ||
|  |         iot_printf("hibernate mode : check dtop reg 0x03280000 = %x\n", rdata[0]); | ||
|  |         if (0x00080000 != rdata[0]){ | ||
|  |             error++; | ||
|  |         } | ||
|  | 
 | ||
|  |         //check if pmm register back to default value
 | ||
|  |         rdata[0] =  *(volatile uint32_t *)(0x05c0e8a0); | ||
|  |         iot_printf("hibernate mode : check pmm reg 0x05c0e8a0 = %x\n", rdata[0]); | ||
|  |         if (0 != rdata[0]){ | ||
|  |             error++; | ||
|  |         } | ||
|  | 
 | ||
|  |         if(error == 0) | ||
|  |             iot_printf("wake up from hibernate mode and access pmm 16k ram successfully !\n"); | ||
|  |         else | ||
|  |             iot_printf("wake up from hibernate mode and access pmm 16k ram failed !\n"); | ||
|  |     }//else {
 | ||
|  |         os_delay(2000); | ||
|  |         iot_printf("--------------------config to enter hibernate mode ------------------\n"); | ||
|  |         //change dtop register value for test
 | ||
|  |         *(volatile uint32_t *)(0x03280000) = 0x6860; //this reg default value = 0x80000
 | ||
|  |         rdata[0] =  *(volatile uint32_t *)(0x03280000); | ||
|  |         iot_printf("hibernate mode : write dtop reg 0x03280000 = %x\n", rdata[0]); | ||
|  | 
 | ||
|  |         //change pmm register value for test
 | ||
|  |         *(volatile uint32_t *)(0x05c0e8a0) = 0xAA55; //this reg default value = 0x0
 | ||
|  |         rdata[0] =  *(volatile uint32_t *)(0x05c0e8a0); | ||
|  |         iot_printf("hibernate mode : write pmm reg 0x05c0e8a0 = %x\n", rdata[0]); | ||
|  | 
 | ||
|  |         // save hiber pattern to pmm ana0 ana1 and clk domain
 | ||
|  |         low_power_mode_save_data(hibernate,pattern,2); | ||
|  |         save_hiber_rtc_value(pattern[0]); | ||
|  | 
 | ||
|  |         #ifdef FORCE
 | ||
|  |             #ifdef IO_WKUP
 | ||
|  |             iot_printf("force enter into hibernate low power mode and wait for IO wakeup\n"); | ||
|  |             os_delay(400); | ||
|  |             enter_hibernate_mode(io,0,1); | ||
|  |             #else
 | ||
|  |             iot_printf("force enter into hibernate low power mode and wait for time out wakeup\n"); | ||
|  |             os_delay(400); | ||
|  |             //enter_hibernate_mode(timer,10000,1);
 | ||
|  |             enter_low_power_mode(hibernate,80000,1); | ||
|  |             #endif
 | ||
|  |         #else
 | ||
|  |             #ifdef IO_WKUP
 | ||
|  |             iot_printf("auto enter into hibernate low power mode and wait for IO wakeup\n"); | ||
|  |             os_delay(400); | ||
|  |             enter_hibernate_mode(io,0,0); | ||
|  |             #else
 | ||
|  |             iot_printf("auto enter into hibernate low power mode and wait for time out wakeup\n"); | ||
|  |             os_delay(400); | ||
|  |             //enter_hibernate_mode(timer,5000,0);
 | ||
|  |             enter_low_power_mode(hibernate,160000,0); | ||
|  |             #endif
 | ||
|  |         #endif
 | ||
|  |    // }
 | ||
|  | #endif
 | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | //DEEP_SLEEP test////////////////////////
 | ||
|  | #ifdef DEEP_SLEEP
 | ||
|  |     uint32_t rdata = 0; | ||
|  |     volatile uint8_t error = 0; | ||
|  |     volatile uint8_t i; | ||
|  |     uint8_t    wakeup_from_deep_slp_flag = 0; // deep_sleep flag
 | ||
|  |     uint32_t pattern = 0x66888866; | ||
|  |     uint32_t check_data[100]={0}; | ||
|  | 
 | ||
|  |     if (wake_up_from_deep_slp_check()) { | ||
|  |         wakeup_from_deep_slp_flag = 1; | ||
|  |     } | ||
|  | 
 | ||
|  |     if (wakeup_from_deep_slp_flag){ | ||
|  |         iot_printf("--------------------wake up from deep sleep mode ------------------\n"); | ||
|  |         //check saved pattern
 | ||
|  |         read_pmm_2k_ram(0,&rdata,1); | ||
|  |         iot_printf("deep sleep mode : check saved pattern = %x\n", rdata); | ||
|  |         if(0x66888866 != rdata) { | ||
|  |             error++; | ||
|  |         } | ||
|  | 
 | ||
|  |         //check saved data in pmm mem 16k
 | ||
|  |         for(i=0; i<100; i++) { | ||
|  |             *(check_data+i) = 0; | ||
|  |         } | ||
|  | 
 | ||
|  |         read_pmm_16k_ram(0,check_data,100); | ||
|  | 
 | ||
|  |         for(i=0; i<100; i++) { | ||
|  |             if(check_data[i] != i) error++; | ||
|  |         } | ||
|  | 
 | ||
|  |         //check if dtop register back to default value
 | ||
|  |         rdata =  *(volatile uint32_t *)(0x03280000); | ||
|  |         iot_printf("deep sleep mode : check 0x03280000 = %x\n", rdata); | ||
|  |         if (0x00080000 != rdata){ | ||
|  |             error++; | ||
|  |         } | ||
|  | 
 | ||
|  |         if(error == 0) { | ||
|  |             iot_printf("wake up from deep sleep mode and access pmm 16k ram successfully !\n"); | ||
|  | 
 | ||
|  |         }else | ||
|  |             iot_printf("wake up from deep sleep mode and access pmm 16k ram failed !\n"); | ||
|  | 
 | ||
|  |     }//else {
 | ||
|  |         os_delay(2000); | ||
|  |         iot_printf("--------------------config to enter deep sleep mode ------------------\n"); | ||
|  |         //save data to pmm 16k ram
 | ||
|  |         for(i=0; i<100; i++) {                   // access pmm mem
 | ||
|  |             *(check_data+i) = i; | ||
|  |         } | ||
|  |         write_pmm_16k_ram(0,check_data,100); | ||
|  | 
 | ||
|  |         //change dtop register value for test
 | ||
|  |          *(volatile uint32_t *)(0x03280000) = 0x6860; //this reg default value = 0x80000
 | ||
|  |          rdata =  *(volatile uint32_t *)(0x03280000); | ||
|  |          iot_printf("deep sleep mode : write 0x03280000 = %x\n", rdata); | ||
|  | 
 | ||
|  |          // save deep sleep pattern to pmm 2k mem
 | ||
|  |          //write_pmm_2k_ram(0,&pattern,1);
 | ||
|  |          low_power_mode_save_data(deep_sleep,&pattern,1); | ||
|  | 
 | ||
|  |         #ifdef FORCE
 | ||
|  |             #ifdef IO_WKUP
 | ||
|  |             iot_printf("force enter into deep sleep low power mode and wait for IO wakeup\n"); | ||
|  |             os_delay(400); | ||
|  |             enter_deep_sleep_mode(io,0,0,1); | ||
|  |             #else
 | ||
|  |             iot_printf("force enter into deep sleep low power mode and wait for time out wakeup\n"); | ||
|  |             os_delay(400); | ||
|  |             //enter_deep_sleep_mode(timer,10000,0,1);
 | ||
|  |             enter_low_power_mode(deep_sleep,80000,1); | ||
|  |             #endif
 | ||
|  |         #else
 | ||
|  |             #ifdef IO_WKUP
 | ||
|  |             iot_printf("auto enter into deep sleep low power mode and wait for IO wakeup\n"); | ||
|  |             os_delay(400); | ||
|  |             enter_deep_sleep_mode(io,1,0,0); | ||
|  |             #else
 | ||
|  |             iot_printf("auto enter into deep sleep low power mode and wait for time out wakeup\n"); | ||
|  |             os_delay(400); | ||
|  |             //enter_deep_sleep_mode(timer,5000,0,0);
 | ||
|  |             enter_low_power_mode(deep_sleep,160000,0); | ||
|  |             #endif
 | ||
|  |         #endif
 | ||
|  |  //   }
 | ||
|  | #endif
 | ||
|  | 
 | ||
|  | 
 | ||
|  |     //soft reset test////////////////////////
 | ||
|  | #ifdef SOFT_RESET
 | ||
|  |         uint32_t rdata = 0; | ||
|  |         volatile uint8_t error = 0; | ||
|  |         volatile uint8_t i; | ||
|  |         uint8_t    wakeup_from_soft_reset_flag = 0; // deep_sleep flag
 | ||
|  |         uint32_t pattern = 0x66888866; | ||
|  |         uint32_t check_data[100]={0}; | ||
|  | 
 | ||
|  |         if (wake_up_from_soft_reset_check()) { | ||
|  |             wakeup_from_soft_reset_flag = 1; | ||
|  |         } | ||
|  | 
 | ||
|  |         if (wakeup_from_soft_reset_flag){ | ||
|  |             iot_printf("--------------------wake up from dtop soft reset mode ------------------\n"); | ||
|  | 
 | ||
|  |             //check saved data in pmm mem 16k
 | ||
|  |             for(i=0; i<100; i++) { | ||
|  |                 *(check_data+i) = 0; | ||
|  |             } | ||
|  | 
 | ||
|  |             read_pmm_16k_ram(0,check_data,100); | ||
|  | 
 | ||
|  |             for(i=0; i<100; i++) { | ||
|  |                 if(check_data[i] != i) error++; | ||
|  |             } | ||
|  | 
 | ||
|  |             //check if dtop register back to default value
 | ||
|  |             rdata =  *(volatile uint32_t *)(0x03280000); | ||
|  |             iot_printf("soft reset mode : check 0x03280000 = %x\n", rdata); | ||
|  |             if (0x00080000 != rdata){ | ||
|  |                 error++; | ||
|  |             } | ||
|  | 
 | ||
|  |             if(error == 0) { | ||
|  |                 iot_printf("wake up from soft reset mode and access pmm 16k ram successfully !\n"); | ||
|  | 
 | ||
|  |             }else | ||
|  |                 iot_printf("wake up from soft reset mode and access pmm 16k ram failed !\n"); | ||
|  | 
 | ||
|  |         } | ||
|  | 
 | ||
|  |             os_delay(2000); | ||
|  |             iot_printf("--------------------config to trigger dtop soft reset ------------------\n"); | ||
|  |             //save data to pmm 16k ram
 | ||
|  |             for(i=0; i<100; i++) {                     // access pmm mem
 | ||
|  |                 *(check_data+i) = i; | ||
|  |             } | ||
|  |             write_pmm_16k_ram(0,check_data,100); | ||
|  | 
 | ||
|  |             //change dtop register value for test
 | ||
|  |              *(volatile uint32_t *)(0x03280000) = 0x6860; //this reg default value = 0x80000
 | ||
|  |              rdata =  *(volatile uint32_t *)(0x03280000); | ||
|  |              iot_printf("soft reset mode : write 0x03280000 = %x\n", rdata); | ||
|  | 
 | ||
|  |              // save soft reset pattern to pmm 2k mem
 | ||
|  |              write_pmm_2k_ram(0,&pattern,1); | ||
|  | 
 | ||
|  |             iot_printf("trigger dtop soft reset\n"); | ||
|  |             os_delay(400); | ||
|  |             trigger_dtop_soft_reset(); | ||
|  | #endif
 | ||
|  | 
 | ||
|  |     while(1) | ||
|  |     { | ||
|  |         iot_printf("user_task_1....\n"); | ||
|  | 
 | ||
|  |         os_delay(400); | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | int32_t iot__task_init() | ||
|  | { | ||
|  |     os_task_h handle; | ||
|  | 
 | ||
|  |     handle = os_create_task(user_task_1, NULL, 9); | ||
|  | 
 | ||
|  |     //create the tasks;
 | ||
|  |     if(handle != NULL) { | ||
|  |         iot_printf("task 1 init successfully...\n"); | ||
|  |     } | ||
|  | 
 | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | 
 | ||
|  | int32_t iot__module_init(void) | ||
|  | { | ||
|  |     //platform intialization;
 | ||
|  |     iot__platform_init(); | ||
|  | 
 | ||
|  |     //create all the tasks;
 | ||
|  |     iot__task_init(); | ||
|  | 
 | ||
|  |     iot_printf("starting...\n"); | ||
|  | 
 | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | int32_t iot__task_start() | ||
|  | { | ||
|  |     //start the tasks;
 | ||
|  |     os_start_kernel(); | ||
|  | 
 | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | int32_t iot__module_start(void) | ||
|  | { | ||
|  |     int32_t res = 0; | ||
|  | 
 | ||
|  |     res = iot__task_start(); | ||
|  | 
 | ||
|  |     return res; | ||
|  | } | ||
|  | 
 | ||
|  | 
 | ||
|  | int main(void) | ||
|  | { | ||
|  |     //module init;
 | ||
|  |     iot__module_init(); | ||
|  | 
 | ||
|  |     //module start;
 | ||
|  |     iot__module_start(); | ||
|  | 
 | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 |