From 0cb217046da34cd9b66322592ba6df087fdd4f15 Mon Sep 17 00:00:00 2001 From: susisstrolch Date: Mon, 23 Nov 2015 20:17:52 +0100 Subject: [PATCH] simple handler for system_os_task/system_os_post we wrap both functions in much easier ones (register_usr_task, post_usr_task). register_usr_task requires an os_task_t parameter (the task itself) and returns a task number (0...MAXUSRTASKS) which is later on used by "post_usr_task". register_usr_task is self-initializing. no static arrays - malloc only... --- esp-link/task.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ esp-link/task.h | 20 +++++++++++++ serial/uart.c | 11 +++---- 3 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 esp-link/task.c create mode 100644 esp-link/task.h diff --git a/esp-link/task.c b/esp-link/task.c new file mode 100644 index 0000000..879a694 --- /dev/null +++ b/esp-link/task.c @@ -0,0 +1,78 @@ +/* + * task.c + * + * Copyright 2015 Susi's Strolch + * + * For license information see projects "License.txt" + * + * Not sure if it's save to use ICACHE_FLASH_ATTR, so we're running from RAM + */ + +#undef USRTASK_DBG + +#include "esp8266.h" +#include + +#define MAXUSRTASKS 8 + +#ifdef USRTASK_DBG +#define DBG_USRTASK(format, ...) os_printf(format, ## __VA_ARGS__) +#else +#define DBG_USRTASK(format, ...) do { } while(0) +#endif + +LOCAL os_event_t *_task_queue = NULL; // system_os_task queue +LOCAL os_task_t *usr_task_queue = NULL; // user task queue + +// it seems save to run the usr_event_handler from RAM, so no ICACHE_FLASH_ATTR here... + +LOCAL void usr_event_handler(os_event_t *e) +{ + DBG_USRTASK("usr_event_handler: event %p (sig=%d, par=%p)\n", e, (int)e->sig, (void *)e->par); + if (usr_task_queue[e->sig] == NULL || e->sig < 0 || e->sig >= MAXUSRTASKS) { + os_printf("usr_event_handler: task %d %s\n", (int)e->sig, + usr_task_queue[e->sig] == NULL ? "not registered" : "out of range"); + return; + } + (usr_task_queue[e->sig])(e); +} + +LOCAL void init_usr_task() { + if (_task_queue == NULL) + _task_queue = (os_event_t *)os_zalloc(sizeof(os_event_t) * _task_queueLen); + + if (usr_task_queue == NULL) + usr_task_queue = (os_task_t *)os_zalloc(sizeof(os_task_t) * MAXUSRTASKS); + + system_os_task(usr_event_handler, _taskPrio, _task_queue, _task_queueLen); +} + +// public functions +bool post_usr_task(uint8_t task, os_param_t par) +{ + return system_os_post(_taskPrio, task, par); +} + +uint8_t register_usr_task (os_task_t event) +{ + int task; + + DBG_USRTASK("register_usr_task: %p\n", event); + if (_task_queue == NULL || usr_task_queue == NULL) + init_usr_task(); + + for (task = 0; task < MAXUSRTASKS; task++) { + if (usr_task_queue[task] == event) + return task; // task already registered - bail out... + } + + for (task = 0; task < MAXUSRTASKS; task++) { + if (usr_task_queue[task] == NULL) { + DBG_USRTASK("register_usr_task: assign task #%d\n", task); + usr_task_queue[task] = event; + break; + } + } + return task; +} + diff --git a/esp-link/task.h b/esp-link/task.h new file mode 100644 index 0000000..2dfd5d8 --- /dev/null +++ b/esp-link/task.h @@ -0,0 +1,20 @@ +/* + * task.h + * + * Copyright 2015 Susi's Strolch + * + * For license information see projects "License.txt" + * + * + */ + +#ifndef USRTASK_H +#define USRTASK_H + +#define _taskPrio 1 +#define _task_queueLen 64 + +uint8_t register_usr_task (os_task_t event); +bool post_usr_task(uint8_t task, os_param_t par); + +#endif diff --git a/serial/uart.c b/serial/uart.c index 8e61f7e..f7f07fa 100644 --- a/serial/uart.c +++ b/serial/uart.c @@ -18,6 +18,7 @@ * Heavily modified and enhanced by Thorsten von Eicken in 2015 */ #include "esp8266.h" +#include "task.h" #include "uart.h" #ifdef UART_DBG @@ -26,14 +27,10 @@ #define DBG_UART(format, ...) do { } while(0) #endif -#define recvTaskPrio 1 -#define recvTaskQueueLen 64 +LOCAL uint8_t uart_recvTaskNum; // UartDev is defined and initialized in rom code. extern UartDevice UartDev; - -os_event_t recvTaskQueue[recvTaskQueueLen]; - #define MAX_CB 4 static UartRecv_cb uart_recv_cb[4]; @@ -208,7 +205,7 @@ uart0_rx_intr_handler(void *para) { //DBG_UART("stat:%02X",*(uint8 *)UART_INT_ENA(uart_no)); ETS_UART_INTR_DISABLE(); - system_os_post(recvTaskPrio, 0, 0); + post_usr_task(uart_recvTaskNum, 0); } } @@ -284,7 +281,7 @@ uart_init(UartBautRate uart0_br, UartBautRate uart1_br) // install uart1 putc callback os_install_putc1((void *)uart0_write_char); - system_os_task(uart_recvTask, recvTaskPrio, recvTaskQueue, recvTaskQueueLen); + uart_recvTaskNum = register_usr_task(uart_recvTask); } void ICACHE_FLASH_ATTR