Skip to content

Commit 611bbca

Browse files
committed
Refactoring of ets_timer.c
Switch from FreeRTOS queue to task notification. Removed unknown/unused code.
1 parent 1ebbece commit 611bbca

File tree

1 file changed

+15
-85
lines changed

1 file changed

+15
-85
lines changed

open_esplibs/libmain/ets_timer.c

Lines changed: 15 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
* from the real FRC2 timer ISR handler and calls former ISR handler.
1313
* So, timer callbacks are called from the task context rather than an interrupt.
1414
*
15+
* Modifications from the original reverese engineered version:
16+
* - FreeRTOS queue is replaced with Task notifications.
17+
* - Removed unknown queue lenght monitoring and parameters allocation.
18+
* - Removed unused debug variables
19+
* - xTaskGenericCreate is replaced with xTaskCreate
20+
*
1521
* This timer should be used with coution together with other tasks. As the
1622
* timer callback is executed within timer task context, access to data that
1723
* other tasks accessing should be protected.
@@ -55,25 +61,7 @@ typedef struct ets_timer_st {
5561
*/
5662
static ets_timer_t* timer_list = 0;
5763

58-
/**
59-
* Those debug variables are set but never used.
60-
*/
61-
static ets_timer_func_t *debug_timerfn;
62-
static ets_timer_t *debug_timer;
63-
64-
/**
65-
* Timer queue
66-
*/
67-
static QueueHandle_t queue;
68-
69-
/**
70-
* Unknown stuff
71-
* Some counters
72-
*/
73-
static uint8_t queue_len = 0;
74-
static uint8_t buf_param_index = 0;
75-
static uint64_t _unknown_buf[4];
76-
64+
static TaskHandle_t task_handle = NULL;
7765

7866
void sdk_ets_timer_setfn(ets_timer_t *timer, ets_timer_func_t *func, void *parg)
7967
{
@@ -87,7 +75,7 @@ void sdk_ets_timer_setfn(ets_timer_t *timer, ets_timer_func_t *func, void *parg)
8775
/**
8876
* .Lfunc004
8977
*/
90-
static void set_alarm_value(uint32_t value)
78+
static inline void set_alarm_value(uint32_t value)
9179
{
9280
TIMER_FRC2.ALARM = value;
9381
}
@@ -247,15 +235,12 @@ void sdk_ets_timer_disarm(ets_timer_t *timer)
247235
* Check the list of pending timers for expired ones and process them.
248236
* This function is not called from the interrupt regardless of its name.
249237
*/
250-
void sdk_ets_timer_handler_isr()
238+
void IRAM sdk_ets_timer_handler_isr()
251239
{
252240
vPortEnterCritical();
253241
int32_t ticks = TIMER_FRC2.COUNT;
254242
while (timer_list) {
255243
if (((int32_t)timer_list->fire_ticks - ticks) < 1) {
256-
debug_timerfn = timer_list->callback;
257-
debug_timer = timer_list;
258-
259244
ets_timer_t *timer = timer_list;
260245
timer_list = timer->next;
261246
timer->next = ETS_TIMER_NOT_ARMED;
@@ -281,67 +266,19 @@ void sdk_ets_timer_handler_isr()
281266
vPortExitCritical();
282267
}
283268

284-
285-
/**
286-
* .Lfunc001
287-
*
288-
* Mysterious function.
289-
* It seems like it keeps track of the queue size and returns 0 if
290-
* the queue gets longer than 5.
291-
* Also it seems like it returns some buffers that are used in the queue.
292-
* But those buffers in the queue are not used at all.
293-
* If anybody knows what is this all about please leave a comment.
294-
*/
295-
static void* IRAM func001()
296-
{
297-
uint8_t *p = (uint8_t*)_unknown_buf;
298-
299-
queue_len++;
300-
if (queue_len < 5) {
301-
p += (buf_param_index*8);
302-
if (buf_param_index + 1 < 4) {
303-
buf_param_index++;
304-
} else {
305-
buf_param_index = 0;
306-
}
307-
return p;
308-
} else {
309-
queue_len--;
310-
return 0;
311-
}
312-
}
313-
314269
/**
315270
* .Lfunc002
316271
*/
317272
static void IRAM frc2_isr()
318273
{
319-
void *p = func001();
320-
321-
if (!p) {
322-
printf("TIMQ_NUL\n");
323-
return;
324-
}
325-
326274
BaseType_t task_woken = 0;
327275

328-
BaseType_t result = xQueueGenericSendFromISR(queue, p, &task_woken, 0);
276+
BaseType_t result = xTaskNotifyFromISR(task_handle, 0, eNoAction, &task_woken);
329277
if (result != pdTRUE) {
330278
printf("TIMQ_FL:%d!!", (uint32_t)result);
331279
}
332-
if (task_woken) {
333-
vTaskSwitchContext();
334-
}
335-
}
336280

337-
/**
338-
* .Lfunc003
339-
*/
340-
static void func003()
341-
{
342-
vPortEnterCritical();
343-
queue_len--;
344-
vPortExitCritical();
281+
portEND_SWITCHING_ISR(task_woken);
345282
}
346283

347284
/**
@@ -351,11 +288,9 @@ static void func003()
351288
*/
352289
static void timer_task(void* param)
353290
{
354-
uint32_t *local0;
355291
while (true) {
356-
if (xQueueGenericReceive(queue, &local0, 0xffffffff, 0) == 1) {
292+
if (xTaskNotifyWait(0, 0, NULL, portMAX_DELAY) == pdTRUE) {
357293
sdk_ets_timer_handler_isr();
358-
func003();
359294
}
360295
}
361296
}
@@ -366,17 +301,12 @@ void sdk_ets_timer_init()
366301

367302
_xt_isr_attach(INUM_TIMER_FRC2, frc2_isr);
368303

369-
queue = xQueueGenericCreate(/*length=*/4, /*item size=*/4,
370-
/*queu type: base*/0);
371-
372-
TaskHandle_t handle = 0;
373-
374304
/* Original code calls xTaskGenericCreate:
375-
* xTaskGenericCreate(timer_task, "rtc_timer_task", 200, 0, 12, &handle,
305+
* xTaskGenericCreate(task_handle, "rtc_timer_task", 200, 0, 12, &handle,
376306
* NULL, NULL);
377307
*/
378-
xTaskCreate(timer_task, "rtc_timer_task", 200, 0, 12, &handle);
379-
printf("frc2_timer_task_hdl:%x, prio:%d, stack:%d\n", (uint32_t)handle, 12, 200);
308+
xTaskCreate(timer_task, "rtc_timer_task", 200, 0, 12, &task_handle);
309+
printf("frc2_timer_task_hdl:%p, prio:%d, stack:%d\n", task_handle, 12, 200);
380310

381311
TIMER_FRC2.ALARM = 0;
382312
TIMER_FRC2.CTRL = VAL2FIELD(TIMER_CTRL_CLKDIV, TIMER_CLKDIV_16)

0 commit comments

Comments
 (0)