1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include "py/gc.h"
#include "py/mphal.h"
#include "py/runtime.h"
#include "softtimer.h"
#ifdef MICROPY_SOFT_TIMER_TICKS_MS
extern __IO uint32_t MICROPY_SOFT_TIMER_TICKS_MS;
volatile uint32_t soft_timer_next;
static inline uint32_t soft_timer_get_ms(void) {
return MICROPY_SOFT_TIMER_TICKS_MS;
}
static void soft_timer_schedule_at_ms(uint32_t ticks_ms) {
uint32_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
uint32_t uw_tick = MICROPY_SOFT_TIMER_TICKS_MS;
if (soft_timer_ticks_diff(ticks_ms, uw_tick) <= 0) {
soft_timer_next = uw_tick + 1;
} else {
soft_timer_next = ticks_ms;
}
MICROPY_END_ATOMIC_SECTION(atomic_state);
}
#endif
// Pointer to the pairheap of soft timer objects.
// This may contain bss/data pointers as well as GC-heap pointers,
// and is explicitly GC traced by soft_timer_gc_mark_all().
static soft_timer_entry_t *soft_timer_heap;
static int soft_timer_lt(mp_pairheap_t *n1, mp_pairheap_t *n2) {
soft_timer_entry_t *e1 = (soft_timer_entry_t *)n1;
soft_timer_entry_t *e2 = (soft_timer_entry_t *)n2;
return soft_timer_ticks_diff(e1->expiry_ms, e2->expiry_ms) < 0;
}
void soft_timer_deinit(void) {
// Pop off all the nodes which are allocated on the GC-heap.
MICROPY_PY_PENDSV_ENTER;
soft_timer_entry_t *heap_from = soft_timer_heap;
soft_timer_entry_t *heap_to = (soft_timer_entry_t *)mp_pairheap_new(soft_timer_lt);
while (heap_from != NULL) {
soft_timer_entry_t *entry = (soft_timer_entry_t *)mp_pairheap_peek(soft_timer_lt, &heap_from->pairheap);
heap_from = (soft_timer_entry_t *)mp_pairheap_pop(soft_timer_lt, &heap_from->pairheap);
if (!(entry->flags & SOFT_TIMER_FLAG_GC_ALLOCATED)) {
heap_to = (soft_timer_entry_t *)mp_pairheap_push(soft_timer_lt, &heap_to->pairheap, &entry->pairheap);
}
}
soft_timer_heap = heap_to;
MICROPY_PY_PENDSV_EXIT;
}
// Must be executed at IRQ_PRI_PENDSV
void soft_timer_handler(void) {
uint32_t ticks_ms = soft_timer_get_ms();
soft_timer_entry_t *heap = soft_timer_heap;
while (heap != NULL && soft_timer_ticks_diff(heap->expiry_ms, ticks_ms) <= 0) {
soft_timer_entry_t *entry = heap;
heap = (soft_timer_entry_t *)mp_pairheap_pop(soft_timer_lt, &heap->pairheap);
if (entry->flags & SOFT_TIMER_FLAG_PY_CALLBACK) {
mp_sched_schedule(entry->py_callback, MP_OBJ_FROM_PTR(entry));
} else if (entry->c_callback) {
entry->c_callback(entry);
}
if (entry->mode == SOFT_TIMER_MODE_PERIODIC) {
entry->expiry_ms += entry->delta_ms;
heap = (soft_timer_entry_t *)mp_pairheap_push(soft_timer_lt, &heap->pairheap, &entry->pairheap);
}
}
soft_timer_heap = heap;
// Schedule the port's timer to call us back at the correct time.
if (heap != NULL) {
soft_timer_schedule_at_ms(heap->expiry_ms);
}
}
void soft_timer_gc_mark_all(void) {
// Mark all soft timer nodes that are allocated on the GC-heap.
// To avoid deep C recursion, pop and recreate the pairheap as nodes are marked.
MICROPY_PY_PENDSV_ENTER;
soft_timer_entry_t *heap_from = soft_timer_heap;
soft_timer_entry_t *heap_to = (soft_timer_entry_t *)mp_pairheap_new(soft_timer_lt);
while (heap_from != NULL) {
soft_timer_entry_t *entry = (soft_timer_entry_t *)mp_pairheap_peek(soft_timer_lt, &heap_from->pairheap);
heap_from = (soft_timer_entry_t *)mp_pairheap_pop(soft_timer_lt, &heap_from->pairheap);
if (entry->flags & SOFT_TIMER_FLAG_GC_ALLOCATED) {
gc_collect_root((void **)&entry, 1);
}
heap_to = (soft_timer_entry_t *)mp_pairheap_push(soft_timer_lt, &heap_to->pairheap, &entry->pairheap);
}
soft_timer_heap = heap_to;
MICROPY_PY_PENDSV_EXIT;
}
void soft_timer_static_init(soft_timer_entry_t *entry, uint16_t mode, uint32_t delta_ms, void (*cb)(soft_timer_entry_t *)) {
mp_pairheap_init_node(soft_timer_lt, &entry->pairheap);
entry->flags = 0;
entry->mode = mode;
entry->delta_ms = delta_ms;
entry->c_callback = cb;
}
void soft_timer_insert(soft_timer_entry_t *entry, uint32_t initial_delta_ms) {
mp_pairheap_init_node(soft_timer_lt, &entry->pairheap);
entry->expiry_ms = soft_timer_get_ms() + initial_delta_ms;
MICROPY_PY_PENDSV_ENTER;
soft_timer_heap = (soft_timer_entry_t *)mp_pairheap_push(soft_timer_lt, &soft_timer_heap->pairheap, &entry->pairheap);
if (entry == soft_timer_heap) {
// This new timer became the earliest one so schedule a callback.
soft_timer_schedule_at_ms(entry->expiry_ms);
}
MICROPY_PY_PENDSV_EXIT;
}
void soft_timer_remove(soft_timer_entry_t *entry) {
MICROPY_PY_PENDSV_ENTER;
soft_timer_heap = (soft_timer_entry_t *)mp_pairheap_delete(soft_timer_lt, &soft_timer_heap->pairheap, &entry->pairheap);
MICROPY_PY_PENDSV_EXIT;
}
|