1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #include <k_api.h>
6 #include "hal_trace.h"
7 #include "hal_gpio.h"
8 #include "hal_sleep.h"
9 #include "hal_timer.h"
10 #include "hwtimer_list.h"
11 #include "board.h"
12 #include "watchdog.h"
13 
14 #ifdef AOS_COMP_OSAL_POSIX
15 #include "posix/pthread.h"
16 extern pthread_key_list_t pthread_key_list_head;
17 #endif
18 
19 #if RHINO_CONFIG_USER_HOOK
20 extern const k_mm_region_t g_mm_region[];
21 extern int g_region_num;
mem_in_heap(uint32_t addr)22 static int mem_in_heap(uint32_t addr)
23 {
24     int i;
25     for(i = 0; i < g_region_num; i++){
26         if(addr > (uint32_t)g_mm_region[i].start && addr < (uint32_t)g_mm_region[i].start + g_mm_region[i].len){
27             return 1;
28 	}
29     }
30     return 0;
31 }
32 
krhino_tick_hook(void)33 void krhino_tick_hook(void)
34 {
35     static unsigned int idx = 0;
36     if((idx % (RHINO_CONFIG_TICKS_PER_SECOND / 2) ) == 0) {
37         watchdog_feeddog();
38     }
39     idx ++;
40 }
41 
42 HWTIMER_ID krhino_sleep_timer;
43 
krhino_sleep(void)44 void krhino_sleep(void)
45 {
46     hal_sleep_enter_sleep();
47 }
48 
49 static int idle_sleep = 1; // swd will set it
krhino_idle_hook_onoff(int onoff)50 void krhino_idle_hook_onoff(int onoff)
51 {
52     idle_sleep = onoff;
53 }
krhino_idle_hook(void)54 void krhino_idle_hook(void)
55 {
56     uint32_t suspend_time;
57     enum E_HWTIMER_T ret;
58     uint32_t sleep_ms;
59 
60     if (idle_sleep == 0)
61         return ;
62 
63     if ((hal_sleep_light_sleep() == HAL_SLEEP_STATUS_DEEP)) {
64         tick_t tick = krhino_next_sleep_ticks_get();
65         sleep_ms = krhino_ticks_to_ms(tick);
66         if (sleep_ms > 60*1000)
67             sleep_ms = 60*1000;
68         ret = hwtimer_start(krhino_sleep_timer, MS_TO_HWTICKS(sleep_ms));
69         if (ret == E_HWTIMER_OK) {
70             soc_systick_stop();
71             suspend_time = hal_sys_timer_get();
72             krhino_sleep();
73             hwtimer_stop(krhino_sleep_timer);
74             suspend_time = TICKS_TO_MS(hal_sys_timer_get() - suspend_time);
75             soc_systick_start();
76             tick_list_update(krhino_ms_to_ticks(suspend_time));
77         }
78     }
79 }
80 
aos_trace_crash_notify()81 void aos_trace_crash_notify()
82 {
83     abort();
84 }
85 
krhino_idle_pre_hook(void)86 void krhino_idle_pre_hook(void)
87 {
88     hal_trace_crash_dump_register(HAL_TRACE_CRASH_DUMP_MODULE_SYS, aos_trace_crash_notify);
89     krhino_sleep_timer = hwtimer_alloc(NULL, NULL);
90     ASSERT(krhino_sleep_timer, "IdleTask: Failed to alloc sleep timer");
91 }
92 
krhino_start_hook(void)93 void krhino_start_hook(void)
94 {
95 }
96 
krhino_task_create_hook(ktask_t * task)97 void krhino_task_create_hook(ktask_t *task)
98 {
99 }
100 
krhino_task_del_hook(ktask_t * task,res_free_t * arg)101 void krhino_task_del_hook(ktask_t *task, res_free_t *arg)
102 {
103     /*free task->task_sem_obj*/
104     void * task_sem = task->task_sem_obj;
105     g_sched_lock[cpu_cur_get()]++;
106     if(task_sem) {
107         krhino_task_sem_del(task);
108         if(mem_in_heap((uint32_t)task_sem)){
109             aos_free(task_sem);
110         }
111         task->task_sem_obj = NULL;
112     }
113     g_sched_lock[cpu_cur_get()]--;
114 
115     return;
116 }
117 
118 
krhino_init_hook(void)119 void krhino_init_hook(void)
120 {
121 }
122 
krhino_task_switch_hook(ktask_t * orgin,ktask_t * dest)123 void krhino_task_switch_hook(ktask_t *orgin, ktask_t *dest)
124 {
125 }
126 
krhino_mm_alloc_hook(void * mem,size_t size)127 void krhino_mm_alloc_hook(void *mem, size_t size)
128 {
129 }
130 
krhino_task_abort_hook(ktask_t * task)131 void krhino_task_abort_hook(ktask_t *task)
132 {
133 }
134 #endif
135