blob: 9e56987432c4a01682fc92b332bdc543b2e10232 [file] [log] [blame]
Paul Sokolovsky97c26282015-12-21 23:04:11 +02001#include <stdio.h>
2#include "osapi.h"
3#include "os_type.h"
4#include "ets_sys.h"
5#include "etshal.h"
6
7// Use standard ets_task or alternative impl
8#define USE_ETS_TASK 0
9
10#define MP_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
11
12struct task_entry {
13 os_event_t *queue;
14 os_task_t task;
15 uint8_t qlen;
16 uint8_t prio;
17 int8_t i_get;
18 int8_t i_put;
19};
20
21static void (*idle_cb)(void *);
22static void *idle_arg;
23
24#define FIRST_PRIO 0x14
25#define LAST_PRIO 0x20
26#define PRIO2ID(prio) ((prio) - FIRST_PRIO)
27
28volatile struct task_entry emu_tasks[PRIO2ID(LAST_PRIO) + 1];
29
30static inline int prio2id(uint8_t prio) {
31 int id = PRIO2ID(prio);
32 if (id < 0 || id >= MP_ARRAY_SIZE(emu_tasks)) {
33 printf("task prio out of range: %d\n", prio);
34 while (1);
35 }
36 return id;
37}
38
39void dump_task(int prio, volatile struct task_entry *t) {
40 printf("q for task %d: queue: %p, get ptr: %d, put ptr: %d, qlen: %d\n",
41 prio, t->queue, t->i_get, t->i_put, t->qlen);
42}
43
44void dump_tasks(void) {
45 for (int i = 0; i < MP_ARRAY_SIZE(emu_tasks); i++) {
46 if (emu_tasks[i].qlen) {
47 dump_task(i + FIRST_PRIO, &emu_tasks[i]);
48 }
49 }
50 printf("====\n");
51}
52
53bool ets_task(os_task_t task, uint8 prio, os_event_t *queue, uint8 qlen) {
54 static unsigned cnt;
55 printf("#%d ets_task(%p, %d, %p, %d)\n", cnt++, task, prio, queue, qlen);
56#if USE_ETS_TASK
57 return _ets_task(task, prio, queue, qlen);
58#else
59 int id = prio2id(prio);
60 emu_tasks[id].task = task;
61 emu_tasks[id].queue = queue;
62 emu_tasks[id].qlen = qlen;
63 emu_tasks[id].i_get = 0;
64 emu_tasks[id].i_put = 0;
65 return true;
66#endif
67}
68
69bool ets_post(uint8 prio, os_signal_t sig, os_param_t param) {
70// static unsigned cnt; printf("#%d ets_post(%d, %x, %x)\n", cnt++, prio, sig, param);
71#if USE_ETS_TASK
72 return _ets_post(prio, sig, param);
73#else
74 ets_intr_lock();
75
76 const int id = prio2id(prio);
77 os_event_t *q = emu_tasks[id].queue;
78 if (emu_tasks[id].i_put == -1) {
79 // queue is full
80 printf("ets_post: task %d queue full\n", prio);
81 return false;
82 }
83 q = &q[emu_tasks[id].i_put++];
84 q->sig = sig;
85 q->par = param;
86 if (emu_tasks[id].i_put == emu_tasks[id].qlen) {
87 emu_tasks[id].i_put = 0;
88 }
89 if (emu_tasks[id].i_put == emu_tasks[id].i_get) {
90 // queue got full
91 emu_tasks[id].i_put = -1;
92 }
93 //printf("after ets_post: "); dump_task(prio, &emu_tasks[id]);
94 //dump_tasks();
95
96 ets_intr_unlock();
97
98 return true;
99#endif
100}
101
102bool ets_loop_iter(void) {
103 //static unsigned cnt;
104 bool progress = false;
105 for (volatile struct task_entry *t = emu_tasks; t < &emu_tasks[MP_ARRAY_SIZE(emu_tasks)]; t++) {
106 ets_intr_lock();
107 //printf("etc_loop_iter: "); dump_task(t - emu_tasks + FIRST_PRIO, t);
108 if (t->i_get != t->i_put) {
109 progress = true;
110 //printf("#%d Calling task %d(%p) (%x, %x)\n", cnt++,
111 // t - emu_tasks + FIRST_PRIO, t->task, t->queue[t->i_get].sig, t->queue[t->i_get].par);
112 //ets_intr_unlock();
113 t->task(&t->queue[t->i_get]);
114 //ets_intr_lock();
115 //printf("Done calling task %d\n", t - emu_tasks + FIRST_PRIO);
116 if (t->i_put == -1) {
117 t->i_put = t->i_get;
118 }
119 if (++t->i_get == t->qlen) {
120 t->i_get = 0;
121 }
122 }
123 ets_intr_unlock();
124 }
125 return progress;
126}
127
128#if SDK_BELOW_1_1_1
129void my_timer_isr(void *arg) {
130// uart0_write_char('+');
131 ets_post(0x1f, 0, 0);
132}
133
134// Timer init func is in ROM, and calls ets_task by relative addr directly in ROM
135// so, we have to re-init task using our handler
136void ets_timer_init() {
137 printf("ets_timer_init\n");
138// _ets_timer_init();
139 ets_isr_attach(10, my_timer_isr, NULL);
140 SET_PERI_REG_MASK(0x3FF00004, 4);
141 ETS_INTR_ENABLE(10);
142 ets_task((os_task_t)0x40002E3C, 0x1f, (os_event_t*)0x3FFFDDC0, 4);
143
144 WRITE_PERI_REG(PERIPHS_TIMER_BASEDDR + 0x30, 0);
145 WRITE_PERI_REG(PERIPHS_TIMER_BASEDDR + 0x28, 0x88);
146 WRITE_PERI_REG(PERIPHS_TIMER_BASEDDR + 0x30, 0);
147 printf("Installed timer ISR\n");
148}
149#endif
150
151bool ets_run(void) {
152#if USE_ETS_TASK
153 #if SDK_BELOW_1_1_1
154 ets_isr_attach(10, my_timer_isr, NULL);
155 #endif
156 _ets_run();
157#else
158// ets_timer_init();
159 *(char*)0x3FFFC6FC = 0;
160 ets_intr_lock();
161 printf("ets_alt_task: ets_run\n");
162 dump_tasks();
163 ets_intr_unlock();
164 while (1) {
165 if (!ets_loop_iter()) {
166 //printf("idle\n");
167 ets_intr_lock();
168 if (idle_cb) {
169 idle_cb(idle_arg);
170 }
171 asm("waiti 0");
172 ets_intr_unlock();
173 }
174 }
175#endif
176}
177
178void ets_set_idle_cb(void (*handler)(void *), void *arg) {
179 //printf("ets_set_idle_cb(%p, %p)\n", handler, arg);
180 idle_cb = handler;
181 idle_arg = arg;
182}