419 lines
		
	
	
		
			8.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			419 lines
		
	
	
		
			8.2 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 "rtc_hw.h"
 | |
| #if 0
 | |
| 
 | |
| #include "encoding.h"
 | |
| #include "bits.h"
 | |
| 
 | |
| #ifdef __riscv_hard_float
 | |
| # define SOFT_FLOAT_CONTEXT_SIZE 0
 | |
| #else
 | |
| # define SOFT_FLOAT_CONTEXT_SIZE (8 * 32)
 | |
| #endif
 | |
| #define HLS_SIZE 64
 | |
| #define INTEGER_CONTEXT_SIZE (32 * REGBYTES)//REGBYTES
 | |
| 
 | |
| 
 | |
| #define MACHINE_STACK_TOP() ({ \
 | |
|   register uintptr_t sp asm ("sp"); \
 | |
|   (void*)((sp + RISCV_PGSIZE) & -RISCV_PGSIZE); })
 | |
| 
 | |
| 
 | |
| #define MENTRY_FRAME_SIZE (INTEGER_CONTEXT_SIZE + SOFT_FLOAT_CONTEXT_SIZE \
 | |
|                            + HLS_SIZE)
 | |
| 
 | |
| 
 | |
| void IRAM_ATTR enter_user_mode(void (*fn)(uintptr_t), uintptr_t stack)
 | |
| {
 | |
|   uintptr_t mstatus = read_csr(mstatus);
 | |
|   mstatus = INSERT_FIELD(mstatus, MSTATUS_MPP, PRV_U);
 | |
|   mstatus = INSERT_FIELD(mstatus, MSTATUS_MPIE, 0);
 | |
|   write_csr(mstatus, mstatus);
 | |
|   write_csr(mscratch, MACHINE_STACK_TOP() - MENTRY_FRAME_SIZE);
 | |
|   write_csr(mepc, fn);
 | |
|   asm volatile ("mv a0, %0; mv sp, %0; mret" : : "r" (stack));
 | |
|   __builtin_unreachable();
 | |
| }
 | |
| 
 | |
| void IRAM_ATTR app()
 | |
| {
 | |
|    while(1)
 | |
|    {};
 | |
| }
 | |
| #endif
 | |
| 
 | |
| #define write_csr(reg, val) ({ \
 | |
|   if (__builtin_constant_p(val) && (unsigned long)(val) < 32) \
 | |
|     asm volatile ("csrw " #reg ", %0" :: "i"(val)); \
 | |
|   else \
 | |
|     asm volatile ("csrw " #reg ", %0" :: "r"(val)); })
 | |
| 
 | |
| #ifdef __riscv_hard_float
 | |
| # define SOFT_FLOAT_CONTEXT_SIZE 0
 | |
| #else
 | |
| # define SOFT_FLOAT_CONTEXT_SIZE (8 * 32)
 | |
| #endif
 | |
| #define HLS_SIZE 64
 | |
| #define INTEGER_CONTEXT_SIZE (32 * REGBYTES)//REGBYTES
 | |
| 
 | |
| 
 | |
| #define MACHINE_STACK_TOP() ({ \
 | |
|     register uintptr_t sp asm ("sp"); \
 | |
|     (void*)((sp + RISCV_PGSIZE) & -RISCV_PGSIZE); })
 | |
| 
 | |
| 
 | |
| #define MENTRY_FRAME_SIZE (INTEGER_CONTEXT_SIZE + SOFT_FLOAT_CONTEXT_SIZE \
 | |
|     						 + HLS_SIZE)
 | |
| 
 | |
| 
 | |
| #define SYS_write 64
 | |
| #define SYS_exit 93
 | |
| #define SYS_timer 1234
 | |
| 
 | |
| /* Fires a syscall */
 | |
| long syscall(long num, long arg0, long arg1, long arg2)
 | |
| {
 | |
|     register long a7 asm("a7") = num;
 | |
|     register long a0 asm("a0") = arg0;
 | |
|     register long a1 asm("a1") = arg1;
 | |
|     register long a2 asm("a2") = arg2;
 | |
|     asm volatile ("ecall":"+r"(a0) : "r"(a1), "r"(a2), "r"(a7));
 | |
|     return a0;
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| void  enter_user_mode(void (*fn)(uintptr_t), uintptr_t stack)
 | |
| {
 | |
|     uintptr_t mstatus = read_csr(mstatus);
 | |
| //    uintptr_t pmpcfg0 = read_csr(pmpcfg0);
 | |
| //    uintptr_t pmpaddr0 = read_csr(pmpaddr0);
 | |
| //    uintptr_t pmpaddr1 = read_csr(pmpaddr1);
 | |
| 
 | |
| //    pmpcfg0 = INSERT_FIELD(pmpcfg0, 0xff00, 0xf);
 | |
| 
 | |
| 
 | |
|     mstatus = INSERT_FIELD(mstatus, MSTATUS_MPP, PRV_U);
 | |
|     mstatus = INSERT_FIELD(mstatus, MSTATUS_MPIE, 0);
 | |
|     write_csr(mstatus, mstatus);
 | |
| 
 | |
|     write_csr(pmpcfg0, 0xf);//write_csr(pmpcfg0, 0x1f0f00);  //0x8f00
 | |
|     write_csr(pmpaddr0, (0x10000200>>2));//write_csr(pmpaddr0, 0x3ff6400);//0x3fff800
 | |
|     //write_csr(pmpaddr1, 0x3ffffff);//write_csr(pmpaddr1, 0x3ffffff);//0x3fff401
 | |
|     //write_csr(pmpaddr2, 0x440000ff);//0x3fff401
 | |
| 
 | |
| 
 | |
|     write_csr(mscratch, MACHINE_STACK_TOP() - MENTRY_FRAME_SIZE);
 | |
|     write_csr(mepc, fn);
 | |
|     asm volatile ("mv a0, %0; mv sp, %0; mret" : : "r" (stack));
 | |
|     __builtin_unreachable();
 | |
| }
 | |
| 
 | |
| size_t iott_strlen( const char *s )
 | |
| {
 | |
|     uint32_t r = 0;
 | |
|     while( *s++ ) r++;
 | |
|     return r;
 | |
| }
 | |
| 
 | |
| 
 | |
| /* Prints a string with a syscall  */
 | |
| static void printstr(const char* s)
 | |
| {
 | |
|     syscall(SYS_write, 1, (long) s, iott_strlen(s));
 | |
| }
 | |
| 
 | |
| void  app()//IRAM_ATTR
 | |
| {
 | |
|     unsigned int i=0, j=0;
 | |
|     while(1)
 | |
|     {
 | |
|        if (i%1000==0)
 | |
|           printstr("hello world!!!!.\n");
 | |
|        if (j%2000==0)
 | |
|           printstr("hello !!!.\n");
 | |
|        if (i == 6666666)
 | |
|        {
 | |
|            printstr("read IIs base addr!.\n");
 | |
|            j = ((volatile uint32_t)*((volatile uint32_t*)(0x44000000)));
 | |
|        }
 | |
|        i++;
 | |
|        j++;
 | |
|    };
 | |
| }
 | |
| 
 | |
| 
 | |
| void boot_loader()  // IRAM_ATTR
 | |
| {
 | |
|  // extern char trap_entry;
 | |
|  // write_csr(utvec, &trap_entry);
 | |
|  // write_csr(uscratch, 0);
 | |
|  // write_csr(uie, 0);
 | |
| 
 | |
|   enter_user_mode(app, (uintptr_t)0x0fffd000);// 0x0ffe8800
 | |
| }
 | |
| 
 | |
| void user_task_1(){
 | |
|     while(1)
 | |
|     {
 | |
|         printstr("user_task_1....\n");
 | |
| 
 | |
|         os_delay(400);
 | |
|         printstr("user_task_1...!!!.\n");
 | |
|     }
 | |
| 
 | |
|     for(;;) {
 | |
|     //boot_loader();
 | |
|        printstr("user_task_1....\n");
 | |
|        os_delay(4000);
 | |
|     }
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| void user_task_2()
 | |
| {
 | |
|     int i =0;
 | |
|     while(1)
 | |
|     {
 | |
|         printstr("user_task_2....\n");
 | |
|         os_delay(1000);
 | |
|         printstr("user_task_2...!!!\n");
 | |
|         *((volatile uint32_t*)(0x10000100)) = 0x12345678;
 | |
| 
 | |
|         if(i>10)
 | |
|         {
 | |
|             printstr("NOW ERROR TRAP !!!!\n");
 | |
|             *((volatile uint32_t*)(0x10000300)) = 0x87654321;
 | |
|         }
 | |
|         i++;
 | |
|     }
 | |
| 
 | |
|     for(;;) {
 | |
| 
 | |
|     //boot_loader();
 | |
| 
 | |
|     printstr("user_task_1....\n");
 | |
| 
 | |
|     os_delay(4000);
 | |
|     }
 | |
| }
 | |
| 
 | |
| 
 | |
| void user_task_3(){
 | |
|     while(1)
 | |
|     {
 | |
|         printstr("user_task_3....\n");
 | |
|         os_delay(10);
 | |
|         printstr("user_task_3...ttttttt.\n");
 | |
|     }
 | |
| 
 | |
|     for(;;) {
 | |
|        printstr("user_task_1....\n");
 | |
| 
 | |
|       os_delay(4000);
 | |
|     }
 | |
| }
 | |
| 
 | |
| 
 | |
| 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;
 | |
| }
 | |
| 
 | |
| 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;
 | |
| }
 | |
| 
 | |
| 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");
 | |
|     }
 | |
| 
 | |
|         handle = os_create_task(user_task_2, NULL, 9);
 | |
| 
 | |
| 
 | |
|     //create the tasks;
 | |
|     if(handle != NULL) {
 | |
|         iot_printf("task 2 init successfully...\n");
 | |
|     }
 | |
| 
 | |
|         handle = os_create_task(user_task_3, NULL, 9);
 | |
| 
 | |
| 
 | |
|     //create the tasks;
 | |
|     if(handle != NULL) {
 | |
|         iot_printf("task 3 init successfully...\n");
 | |
|     }
 | |
| 
 | |
|     return 0;
 | |
| }
 | |
| 
 | |
| void 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");
 | |
|       printstr("user mode task 1 init successfully...\n");
 | |
|     }
 | |
| 
 | |
|     handle = os_create_task(user_task_2, NULL, 9);
 | |
| 
 | |
|     //create the tasks;
 | |
|     if(handle != NULL) {
 | |
|         printstr("task 2 init successfully...\n");
 | |
|     }
 | |
| 
 | |
|         handle = os_create_task(user_task_3, NULL, 9);
 | |
| 
 | |
| 
 | |
|     //create the tasks;
 | |
|     if(handle != NULL) {
 | |
|         printstr("task 3 init successfully...\n");
 | |
|     }
 | |
| 
 | |
|     iot__module_start();
 | |
| 
 | |
|     return;
 | |
| }
 | |
| 
 | |
| 
 | |
| int32_t iot__module_init(void)
 | |
| {
 | |
|     //platform intialization;
 | |
|     iot__platform_init();
 | |
| 
 | |
|     //create all the tasks;
 | |
|     iot__task_init();
 | |
| 
 | |
|     iot_printf("starting...\n");
 | |
| 
 | |
|     return 0;
 | |
| }
 | |
| 
 | |
| int main(void)  //IRAM_ATTR
 | |
| {
 | |
|     unsigned int j;
 | |
| 
 | |
|     //platform intialization;
 | |
|     iot__platform_init();
 | |
| 
 | |
|     iot_printf("entering user mode...\n");
 | |
| 
 | |
|     j = ((volatile uint32_t)*((volatile uint32_t*)(0x01500200)));
 | |
| 
 | |
|     iot_printf("Test a machine addr 0x%x!!!  \n",j);
 | |
| 
 | |
|     iot_printf("--------ADD intr in Machine mode---------\r\n");
 | |
|     rtc_tmr_int_en(0, 0);
 | |
|     rtc_tmr_cnt_clr(0);
 | |
| 
 | |
|     rtc_tmr_set_value(0, 0xffff);
 | |
|     rtc_tmr_mode_set(0,1);
 | |
|     rtc_tmr_en(0, 1);
 | |
|     rtc_tmr_int_init(0);
 | |
|     rtc_tmr_int_en(0, 1);
 | |
| 
 | |
|     enter_user_mode(task_init, (uintptr_t)0x0611b000);// 0x0ffe8800
 | |
|     return 0;
 | |
| 
 | |
|     //module init;
 | |
|     iot__module_init();
 | |
| 
 | |
|     //module start;
 | |
|     iot__module_start();
 | |
| 
 | |
|     return 0;
 | |
| 
 | |
|     //uint8_t buf[32];
 | |
|     uint32_t addr1 = 0xffd9000;
 | |
|     //uint32_t addr2 = 0x10003000;
 | |
|     //uint32_t i;
 | |
| 
 | |
|     dbg_uart_init();
 | |
| 
 | |
|     iot_printf("hello world.\n");
 | |
| 
 | |
|     boot_loader();
 | |
| 
 | |
|     while(1)
 | |
|     {};
 | |
| 
 | |
|     dbg_uart_init();
 | |
| 
 | |
|    // iot_mem_read(buf, addr1, 32);
 | |
|     //iot_mem_write(buf, addr2, 32);
 | |
| 
 | |
|    // for(i = 0; i < 32; i++){
 | |
|   //     iot_printf("0x%08x:0x%02x\r\n", addr1+i, *(uint8_t*)(addr1+i));
 | |
|   //      iot_printf("0x%08x:0x%02x\r\n", addr2+i, *(uint8_t*)(addr2+i));
 | |
|   //  }
 | |
|     while(1) {
 | |
|         iot_printf("hello world. 0x%x\n",*((uint32_t *)addr1));
 | |
|     }
 | |
|     while(1);
 | |
|     return 0;
 | |
| }
 | |
| 
 | |
| 
 |