2015-12-21 21:04:11 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "osapi.h"
|
|
|
|
#include "os_type.h"
|
|
|
|
#include "ets_sys.h"
|
2016-02-06 08:40:34 +00:00
|
|
|
#include <esp_sdk_ver.h>
|
2015-12-21 21:04:11 +00:00
|
|
|
#include "etshal.h"
|
2015-12-22 14:27:35 +00:00
|
|
|
#include "user_interface.h"
|
2016-06-01 17:26:49 +01:00
|
|
|
#include "ets_alt_task.h"
|
2015-12-21 21:04:11 +00:00
|
|
|
|
|
|
|
// Use standard ets_task or alternative impl
|
|
|
|
#define USE_ETS_TASK 0
|
|
|
|
|
|
|
|
#define MP_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
|
|
|
|
|
|
|
|
struct task_entry {
|
|
|
|
os_event_t *queue;
|
|
|
|
os_task_t task;
|
|
|
|
uint8_t qlen;
|
|
|
|
uint8_t prio;
|
|
|
|
int8_t i_get;
|
|
|
|
int8_t i_put;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void (*idle_cb)(void *);
|
|
|
|
static void *idle_arg;
|
|
|
|
|
2016-02-06 08:40:34 +00:00
|
|
|
#if ESP_SDK_VERSION >= 010500
|
2020-02-27 04:36:53 +00:00
|
|
|
#define FIRST_PRIO 0
|
2016-02-06 08:40:34 +00:00
|
|
|
#else
|
2020-02-27 04:36:53 +00:00
|
|
|
#define FIRST_PRIO 0x14
|
2016-02-06 08:40:34 +00:00
|
|
|
#endif
|
2015-12-21 21:04:11 +00:00
|
|
|
#define LAST_PRIO 0x20
|
|
|
|
#define PRIO2ID(prio) ((prio) - FIRST_PRIO)
|
|
|
|
|
|
|
|
volatile struct task_entry emu_tasks[PRIO2ID(LAST_PRIO) + 1];
|
|
|
|
|
|
|
|
static inline int prio2id(uint8_t prio) {
|
|
|
|
int id = PRIO2ID(prio);
|
|
|
|
if (id < 0 || id >= MP_ARRAY_SIZE(emu_tasks)) {
|
|
|
|
printf("task prio out of range: %d\n", prio);
|
2020-02-27 04:36:53 +00:00
|
|
|
while (1) {
|
|
|
|
;
|
|
|
|
}
|
2015-12-21 21:04:11 +00:00
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2016-04-02 23:04:01 +01:00
|
|
|
#if DEBUG
|
2015-12-21 21:04:11 +00:00
|
|
|
void dump_task(int prio, volatile struct task_entry *t) {
|
|
|
|
printf("q for task %d: queue: %p, get ptr: %d, put ptr: %d, qlen: %d\n",
|
|
|
|
prio, t->queue, t->i_get, t->i_put, t->qlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dump_tasks(void) {
|
|
|
|
for (int i = 0; i < MP_ARRAY_SIZE(emu_tasks); i++) {
|
|
|
|
if (emu_tasks[i].qlen) {
|
|
|
|
dump_task(i + FIRST_PRIO, &emu_tasks[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("====\n");
|
|
|
|
}
|
2016-04-02 23:04:01 +01:00
|
|
|
#endif
|
2015-12-21 21:04:11 +00:00
|
|
|
|
|
|
|
bool ets_task(os_task_t task, uint8 prio, os_event_t *queue, uint8 qlen) {
|
|
|
|
static unsigned cnt;
|
|
|
|
printf("#%d ets_task(%p, %d, %p, %d)\n", cnt++, task, prio, queue, qlen);
|
2020-02-27 04:36:53 +00:00
|
|
|
#if USE_ETS_TASK
|
2015-12-21 21:04:11 +00:00
|
|
|
return _ets_task(task, prio, queue, qlen);
|
2020-02-27 04:36:53 +00:00
|
|
|
#else
|
2015-12-21 21:04:11 +00:00
|
|
|
int id = prio2id(prio);
|
|
|
|
emu_tasks[id].task = task;
|
|
|
|
emu_tasks[id].queue = queue;
|
|
|
|
emu_tasks[id].qlen = qlen;
|
|
|
|
emu_tasks[id].i_get = 0;
|
|
|
|
emu_tasks[id].i_put = 0;
|
|
|
|
return true;
|
2020-02-27 04:36:53 +00:00
|
|
|
#endif
|
2015-12-21 21:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ets_post(uint8 prio, os_signal_t sig, os_param_t param) {
|
|
|
|
// static unsigned cnt; printf("#%d ets_post(%d, %x, %x)\n", cnt++, prio, sig, param);
|
2020-02-27 04:36:53 +00:00
|
|
|
#if USE_ETS_TASK
|
2015-12-21 21:04:11 +00:00
|
|
|
return _ets_post(prio, sig, param);
|
2020-02-27 04:36:53 +00:00
|
|
|
#else
|
2015-12-21 21:04:11 +00:00
|
|
|
ets_intr_lock();
|
|
|
|
|
|
|
|
const int id = prio2id(prio);
|
|
|
|
os_event_t *q = emu_tasks[id].queue;
|
|
|
|
if (emu_tasks[id].i_put == -1) {
|
|
|
|
// queue is full
|
|
|
|
printf("ets_post: task %d queue full\n", prio);
|
2016-09-18 21:01:58 +01:00
|
|
|
return 1;
|
2015-12-21 21:04:11 +00:00
|
|
|
}
|
|
|
|
q = &q[emu_tasks[id].i_put++];
|
|
|
|
q->sig = sig;
|
|
|
|
q->par = param;
|
|
|
|
if (emu_tasks[id].i_put == emu_tasks[id].qlen) {
|
|
|
|
emu_tasks[id].i_put = 0;
|
|
|
|
}
|
|
|
|
if (emu_tasks[id].i_put == emu_tasks[id].i_get) {
|
|
|
|
// queue got full
|
|
|
|
emu_tasks[id].i_put = -1;
|
|
|
|
}
|
|
|
|
//printf("after ets_post: "); dump_task(prio, &emu_tasks[id]);
|
|
|
|
//dump_tasks();
|
|
|
|
|
|
|
|
ets_intr_unlock();
|
|
|
|
|
2016-09-18 21:01:58 +01:00
|
|
|
return 0;
|
2020-02-27 04:36:53 +00:00
|
|
|
#endif
|
2015-12-21 21:04:11 +00:00
|
|
|
}
|
|
|
|
|
2016-06-01 17:26:49 +01:00
|
|
|
int ets_loop_iter_disable = 0;
|
2018-07-03 06:31:10 +01:00
|
|
|
int ets_loop_dont_feed_sw_wdt = 0;
|
2016-06-01 17:26:49 +01:00
|
|
|
|
2016-09-20 05:28:17 +01:00
|
|
|
// to implement a 64-bit wide microsecond counter
|
2019-05-22 06:05:03 +01:00
|
|
|
uint32_t system_time_low_word = 0;
|
2016-09-20 05:28:17 +01:00
|
|
|
uint32_t system_time_high_word = 0;
|
|
|
|
|
2019-05-22 06:05:03 +01:00
|
|
|
void system_time_update(void) {
|
|
|
|
// Handle overflow of system microsecond counter
|
2017-05-12 19:37:49 +01:00
|
|
|
ets_intr_lock();
|
2016-09-20 05:28:17 +01:00
|
|
|
uint32_t system_time_cur = system_get_time();
|
2019-05-22 06:05:03 +01:00
|
|
|
if (system_time_cur < system_time_low_word) {
|
2016-09-20 05:28:17 +01:00
|
|
|
system_time_high_word += 1; // record overflow of low 32-bits
|
|
|
|
}
|
2019-05-22 06:05:03 +01:00
|
|
|
system_time_low_word = system_time_cur;
|
2017-05-12 19:37:49 +01:00
|
|
|
ets_intr_unlock();
|
2019-05-22 06:05:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ets_loop_iter(void) {
|
|
|
|
if (ets_loop_iter_disable) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update 64-bit microsecond counter
|
|
|
|
system_time_update();
|
2016-09-20 05:28:17 +01:00
|
|
|
|
2018-07-03 06:31:10 +01:00
|
|
|
// 6 words before pend_flag_noise_check is a variable that is used by
|
|
|
|
// the software WDT. A 1.6 second period timer will increment this
|
|
|
|
// variable and if it gets to 2 then the SW WDT will trigger a reset.
|
|
|
|
extern uint32_t pend_flag_noise_check;
|
|
|
|
uint32_t *sw_wdt = &pend_flag_noise_check - 6;
|
|
|
|
|
2015-12-21 21:04:11 +00:00
|
|
|
//static unsigned cnt;
|
|
|
|
bool progress = false;
|
|
|
|
for (volatile struct task_entry *t = emu_tasks; t < &emu_tasks[MP_ARRAY_SIZE(emu_tasks)]; t++) {
|
2018-07-03 06:31:10 +01:00
|
|
|
if (!ets_loop_dont_feed_sw_wdt) {
|
|
|
|
system_soft_wdt_feed();
|
|
|
|
}
|
2015-12-21 21:04:11 +00:00
|
|
|
ets_intr_lock();
|
|
|
|
//printf("etc_loop_iter: "); dump_task(t - emu_tasks + FIRST_PRIO, t);
|
|
|
|
if (t->i_get != t->i_put) {
|
|
|
|
progress = true;
|
|
|
|
//printf("#%d Calling task %d(%p) (%x, %x)\n", cnt++,
|
|
|
|
// t - emu_tasks + FIRST_PRIO, t->task, t->queue[t->i_get].sig, t->queue[t->i_get].par);
|
2016-01-03 10:48:29 +00:00
|
|
|
int idx = t->i_get;
|
2015-12-21 21:04:11 +00:00
|
|
|
if (t->i_put == -1) {
|
|
|
|
t->i_put = t->i_get;
|
|
|
|
}
|
|
|
|
if (++t->i_get == t->qlen) {
|
|
|
|
t->i_get = 0;
|
|
|
|
}
|
2016-01-03 10:48:29 +00:00
|
|
|
//ets_intr_unlock();
|
2018-07-03 06:31:10 +01:00
|
|
|
uint32_t old_sw_wdt = *sw_wdt;
|
2016-01-03 10:48:29 +00:00
|
|
|
t->task(&t->queue[idx]);
|
2018-07-03 06:31:10 +01:00
|
|
|
if (ets_loop_dont_feed_sw_wdt) {
|
|
|
|
// Restore previous SW WDT counter, in case task fed/cleared it
|
|
|
|
*sw_wdt = old_sw_wdt;
|
|
|
|
}
|
2016-01-03 10:48:29 +00:00
|
|
|
//ets_intr_lock();
|
|
|
|
//printf("Done calling task %d\n", t - emu_tasks + FIRST_PRIO);
|
2015-12-21 21:04:11 +00:00
|
|
|
}
|
|
|
|
ets_intr_unlock();
|
|
|
|
}
|
2018-11-28 00:54:50 +00:00
|
|
|
|
|
|
|
if (!progress && idle_cb) {
|
|
|
|
idle_cb(idle_arg);
|
|
|
|
}
|
|
|
|
|
2015-12-21 21:04:11 +00:00
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if SDK_BELOW_1_1_1
|
|
|
|
void my_timer_isr(void *arg) {
|
|
|
|
// uart0_write_char('+');
|
|
|
|
ets_post(0x1f, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timer init func is in ROM, and calls ets_task by relative addr directly in ROM
|
|
|
|
// so, we have to re-init task using our handler
|
|
|
|
void ets_timer_init() {
|
|
|
|
printf("ets_timer_init\n");
|
|
|
|
// _ets_timer_init();
|
|
|
|
ets_isr_attach(10, my_timer_isr, NULL);
|
|
|
|
SET_PERI_REG_MASK(0x3FF00004, 4);
|
|
|
|
ETS_INTR_ENABLE(10);
|
2020-02-27 04:36:53 +00:00
|
|
|
ets_task((os_task_t)0x40002E3C, 0x1f, (os_event_t *)0x3FFFDDC0, 4);
|
2015-12-21 21:04:11 +00:00
|
|
|
|
|
|
|
WRITE_PERI_REG(PERIPHS_TIMER_BASEDDR + 0x30, 0);
|
|
|
|
WRITE_PERI_REG(PERIPHS_TIMER_BASEDDR + 0x28, 0x88);
|
|
|
|
WRITE_PERI_REG(PERIPHS_TIMER_BASEDDR + 0x30, 0);
|
|
|
|
printf("Installed timer ISR\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool ets_run(void) {
|
2020-02-27 04:36:53 +00:00
|
|
|
#if USE_ETS_TASK
|
2015-12-21 21:04:11 +00:00
|
|
|
#if SDK_BELOW_1_1_1
|
|
|
|
ets_isr_attach(10, my_timer_isr, NULL);
|
|
|
|
#endif
|
|
|
|
_ets_run();
|
2020-02-27 04:36:53 +00:00
|
|
|
#else
|
2015-12-21 21:04:11 +00:00
|
|
|
// ets_timer_init();
|
2020-02-27 04:36:53 +00:00
|
|
|
*(char *)0x3FFFC6FC = 0;
|
2015-12-21 21:04:11 +00:00
|
|
|
ets_intr_lock();
|
|
|
|
printf("ets_alt_task: ets_run\n");
|
2020-02-27 04:36:53 +00:00
|
|
|
#if DEBUG
|
2015-12-21 21:04:11 +00:00
|
|
|
dump_tasks();
|
2020-02-27 04:36:53 +00:00
|
|
|
#endif
|
2015-12-21 21:04:11 +00:00
|
|
|
ets_intr_unlock();
|
|
|
|
while (1) {
|
|
|
|
if (!ets_loop_iter()) {
|
|
|
|
//printf("idle\n");
|
|
|
|
ets_intr_lock();
|
|
|
|
if (idle_cb) {
|
|
|
|
idle_cb(idle_arg);
|
|
|
|
}
|
2020-02-27 04:36:53 +00:00
|
|
|
asm ("waiti 0");
|
2015-12-21 21:04:11 +00:00
|
|
|
ets_intr_unlock();
|
|
|
|
}
|
|
|
|
}
|
2020-02-27 04:36:53 +00:00
|
|
|
#endif
|
2015-12-21 21:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ets_set_idle_cb(void (*handler)(void *), void *arg) {
|
|
|
|
//printf("ets_set_idle_cb(%p, %p)\n", handler, arg);
|
|
|
|
idle_cb = handler;
|
|
|
|
idle_arg = arg;
|
|
|
|
}
|