Import Mbed OS hard-float snapshot

This commit is contained in:
Beslan
2026-06-01 20:15:04 +03:00
commit d3738e2f89
16278 changed files with 10628036 additions and 0 deletions

View File

@@ -0,0 +1,403 @@
/*
* Copyright (c) 2014-2015 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string.h>
#include "ns_types.h"
#include "ns_list.h"
#include "eventOS_event.h"
#include "eventOS_scheduler.h"
#include "timer_sys.h"
#include "nsdynmemLIB.h"
#include "ns_timer.h"
#include "event.h"
#include "platform/arm_hal_interrupt.h"
typedef struct arm_core_tasklet {
int8_t id; /**< Event handler Tasklet ID */
void (*func_ptr)(arm_event_s *);
ns_list_link_t link;
} arm_core_tasklet_t;
static NS_LIST_DEFINE(arm_core_tasklet_list, arm_core_tasklet_t, link);
static NS_LIST_DEFINE(event_queue_active, arm_event_storage_t, link);
static NS_LIST_DEFINE(free_event_entry, arm_event_storage_t, link);
// Statically allocate initial pool of events.
#define STARTUP_EVENT_POOL_SIZE 10
static arm_event_storage_t startup_event_pool[STARTUP_EVENT_POOL_SIZE];
/** Curr_tasklet tell to core and platform which task_let is active, Core Update this automatic when switch Tasklet. */
int8_t curr_tasklet = 0;
static arm_core_tasklet_t *tasklet_dynamically_allocate(void);
static arm_event_storage_t *event_dynamically_allocate(void);
static arm_event_storage_t *event_core_get(void);
static void event_core_write(arm_event_storage_t *event);
static arm_core_tasklet_t *event_tasklet_handler_get(uint8_t tasklet_id)
{
ns_list_foreach(arm_core_tasklet_t, cur, &arm_core_tasklet_list) {
if (cur->id == tasklet_id) {
return cur;
}
}
return NULL;
}
bool event_tasklet_handler_id_valid(uint8_t tasklet_id)
{
return event_tasklet_handler_get(tasklet_id);
}
// XXX this can return 0, but 0 seems to mean "none" elsewhere? Or at least
// curr_tasklet is reset to 0 in various places.
static int8_t tasklet_get_free_id(void)
{
/*(Note use of uint8_t to avoid overflow if we reach 0x7F)*/
for (uint8_t i = 0; i <= INT8_MAX; i++) {
if (!event_tasklet_handler_get(i)) {
return i;
}
}
return -1;
}
int8_t eventOS_event_handler_create(void (*handler_func_ptr)(arm_event_s *), uint8_t init_event_type)
{
arm_event_storage_t *event_tmp;
// XXX Do we really want to prevent multiple tasklets with same function?
ns_list_foreach(arm_core_tasklet_t, cur, &arm_core_tasklet_list) {
if (cur->func_ptr == handler_func_ptr) {
return -1;
}
}
//Allocate new
arm_core_tasklet_t *new = tasklet_dynamically_allocate();
if (!new) {
return -2;
}
event_tmp = event_core_get();
if (!event_tmp) {
ns_dyn_mem_free(new);
return -2;
}
//Fill in tasklet; add to list
new->id = tasklet_get_free_id();
new->func_ptr = handler_func_ptr;
ns_list_add_to_end(&arm_core_tasklet_list, new);
//Queue "init" event for the new task
event_tmp->data.receiver = new->id;
event_tmp->data.sender = 0;
event_tmp->data.event_type = init_event_type;
event_tmp->data.event_data = 0;
event_core_write(event_tmp);
return new->id;
}
int8_t eventOS_event_send(const arm_event_t *event)
{
if (event_tasklet_handler_get(event->receiver)) {
arm_event_storage_t *event_tmp = event_core_get();
if (event_tmp) {
event_tmp->data = *event;
event_core_write(event_tmp);
return 0;
}
}
return -1;
}
void eventOS_event_send_user_allocated(arm_event_storage_t *event)
{
event->allocator = ARM_LIB_EVENT_USER;
event_core_write(event);
}
void eventOS_event_send_timer_allocated(arm_event_storage_t *event)
{
event->allocator = ARM_LIB_EVENT_TIMER;
event_core_write(event);
}
void eventOS_event_cancel_critical(arm_event_storage_t *event)
{
ns_list_remove(&event_queue_active, event);
}
static arm_event_storage_t *event_dynamically_allocate(void)
{
arm_event_storage_t *event = ns_dyn_mem_temporary_alloc(sizeof(arm_event_storage_t));
if (event) {
event->allocator = ARM_LIB_EVENT_DYNAMIC;
}
return event;
}
static arm_core_tasklet_t *tasklet_dynamically_allocate(void)
{
return ns_dyn_mem_alloc(sizeof(arm_core_tasklet_t));
}
arm_event_storage_t *event_core_get(void)
{
arm_event_storage_t *event;
platform_enter_critical();
event = ns_list_get_first(&free_event_entry);
if (event) {
ns_list_remove(&free_event_entry, event);
} else {
event = event_dynamically_allocate();
}
if (event) {
event->data.data_ptr = NULL;
event->data.priority = ARM_LIB_LOW_PRIORITY_EVENT;
}
platform_exit_critical();
return event;
}
void event_core_free_push(arm_event_storage_t *free)
{
switch (free->allocator) {
case ARM_LIB_EVENT_STARTUP_POOL:
free->state = ARM_LIB_EVENT_UNQUEUED;
platform_enter_critical();
ns_list_add_to_start(&free_event_entry, free);
platform_exit_critical();
break;
case ARM_LIB_EVENT_DYNAMIC:
// Free all dynamically allocated events.
// No need to set state to UNQUEUED - it's being freed.
ns_dyn_mem_free(free);
break;
case ARM_LIB_EVENT_TIMER:
// Hand it back to the timer system
free->state = ARM_LIB_EVENT_UNQUEUED;
timer_sys_event_free(free);
break;
case ARM_LIB_EVENT_USER:
// No need set state to UNQUEUED - we forget about it.
default:
break;
}
}
static arm_event_storage_t *event_core_read(void)
{
platform_enter_critical();
arm_event_storage_t *event = ns_list_get_first(&event_queue_active);
if (event) {
event->state = ARM_LIB_EVENT_RUNNING;
ns_list_remove(&event_queue_active, event);
}
platform_exit_critical();
return event;
}
void event_core_write(arm_event_storage_t *event)
{
platform_enter_critical();
bool added = false;
ns_list_foreach(arm_event_storage_t, event_tmp, &event_queue_active) {
// note enum ordering means we're checking if event_tmp is LOWER priority than event
if (event_tmp->data.priority > event->data.priority) {
ns_list_add_before(&event_queue_active, event_tmp, event);
added = true;
break;
}
}
if (!added) {
ns_list_add_to_end(&event_queue_active, event);
}
event->state = ARM_LIB_EVENT_QUEUED;
/* Wake From Idle */
platform_exit_critical();
eventOS_scheduler_signal();
}
// Requires lock to be held
arm_event_storage_t *eventOS_event_find_by_id_critical(uint8_t tasklet_id, uint8_t event_id)
{
ns_list_foreach(arm_event_storage_t, cur, &event_queue_active) {
if (cur->data.receiver == tasklet_id && cur->data.event_id == event_id) {
return cur;
}
}
return NULL;
}
/**
*
* \brief Initialize Nanostack Core.
*
* Function Initialize Nanostack Core, Socket Interface,Buffer memory and Send Init event to all Tasklett which are Defined.
*
*/
void eventOS_scheduler_init(void)
{
/* Reset Event List variables */
ns_list_init(&free_event_entry);
ns_list_init(&event_queue_active);
ns_list_init(&arm_core_tasklet_list);
//Add first 10 entries to "free" list
for (unsigned i = 0; i < (sizeof(startup_event_pool) / sizeof(startup_event_pool[0])); i++) {
startup_event_pool[i].allocator = ARM_LIB_EVENT_STARTUP_POOL;
ns_list_add_to_start(&free_event_entry, &startup_event_pool[i]);
}
/* Init Generic timer module */
timer_sys_init(); //initialize timer
/* Set Tasklett switcher to Idle */
curr_tasklet = 0;
}
int8_t eventOS_scheduler_get_active_tasklet(void)
{
return curr_tasklet;
}
void eventOS_scheduler_set_active_tasklet(int8_t tasklet)
{
curr_tasklet = tasklet;
}
int eventOS_scheduler_timer_stop(void)
{
timer_sys_disable();
if (ns_timer_sleep() != 0) {
return 1;
}
return 0;
}
int eventOS_scheduler_timer_synch_after_sleep(uint32_t sleep_ticks)
{
//Update MS to 10ms ticks
sleep_ticks /= 10;
sleep_ticks++;
system_timer_tick_update(sleep_ticks);
if (timer_sys_wakeup() == 0) {
return 0;
}
return -1;
}
/**
*
* \brief Infinite Event Read Loop.
*
* Function Read and handle Cores Event and switch/enable tasklet which are event receiver. WhenEvent queue is empty it goes to sleep
*
*/
bool eventOS_scheduler_dispatch_event(void)
{
curr_tasklet = 0;
arm_event_storage_t *cur_event = event_core_read();
if (!cur_event) {
return false;
}
curr_tasklet = cur_event->data.receiver;
arm_core_tasklet_t *tasklet = event_tasklet_handler_get(curr_tasklet);
/* Do not bother with check for NULL - tasklets cannot be deleted,
* and user-facing API eventOS_event_send() has already checked the tasklet
* exists, so there is no possible issue there.
*
* For eventOS_event_send_user_allocated(), it would be a non-recoverable
* error to not deliver the message - we have to have a receiver to pass
* ownership to. If the lookup fails, let it crash. We want the send call
* itself to return void to simplify logic.
*/
/* Tasklet Scheduler Call */
tasklet->func_ptr(&cur_event->data);
event_core_free_push(cur_event);
/* Set Current Tasklet to Idle state */
curr_tasklet = 0;
return true;
}
void eventOS_scheduler_run_until_idle(void)
{
while (eventOS_scheduler_dispatch_event());
}
/**
*
* \brief Infinite Event Read Loop.
*
* Function Read and handle Cores Event and switch/enable tasklet which are event receiver. WhenEvent queue is empty it goes to sleep
*
*/
NS_NORETURN void eventOS_scheduler_run(void)
{
while (1) {
if (!eventOS_scheduler_dispatch_event()) {
eventOS_scheduler_idle();
}
}
}
void eventOS_cancel(arm_event_storage_t *event)
{
if (!event) {
return;
}
platform_enter_critical();
/*
* Notify timer of cancellation.
*/
if (event->allocator == ARM_LIB_EVENT_TIMER) {
timer_sys_event_cancel_critical(event);
}
/*
* Remove event from the list,
* Only queued can be removed, unqued are either timers or stale pointers
* RUNNING cannot be removed, we are currenly "in" that event.
*/
if (event->state == ARM_LIB_EVENT_QUEUED) {
eventOS_event_cancel_critical(event);
}
/*
* Push back to "free" state
*/
if (event->state != ARM_LIB_EVENT_RUNNING) {
event_core_free_push(event);
}
platform_exit_critical();
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (c) 2017 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NS_EVENT_H_
#define NS_EVENT_H_
#ifdef __cplusplus
extern "C" {
#endif
bool event_tasklet_handler_id_valid(uint8_t tasklet_id);
void eventOS_event_send_timer_allocated(arm_event_storage_t *event);
// This requires lock to be held
arm_event_storage_t *eventOS_event_find_by_id_critical(uint8_t tasklet_id, uint8_t event_id);
#ifdef __cplusplus
}
#endif
#endif /*NS_EVENT_H_*/

View File

@@ -0,0 +1,118 @@
/*
* Copyright (c) 2014-2015 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "eventOS_event.h"
#include "eventOS_event_timer.h"
#include "nsdynmemLIB.h"
#include "ns_list.h"
#include "timer_sys.h"
#define STARTUP_EVENT 0
#define TIMER_EVENT 1
// Timeout structure, already typedefed to timeout_t
struct timeout_entry_t {
void (*callback)(void *);
void *arg;
arm_event_storage_t *event;
};
static int8_t timeout_tasklet_id = -1;
static void timeout_tasklet(arm_event_s *event)
{
if (TIMER_EVENT != event->event_type) {
return;
}
timeout_t *t = event->data_ptr;
arm_event_storage_t *storage = t->event;
sys_timer_struct_s *timer = NS_CONTAINER_OF(storage, sys_timer_struct_s, event);
t->callback(t->arg);
// Check if this was periodic timer
if (timer->period == 0) {
ns_dyn_mem_free(event->data_ptr);
}
}
static timeout_t *eventOS_timeout_at_(void (*callback)(void *), void *arg, uint32_t at, uint32_t period)
{
arm_event_storage_t *storage;
timeout_t *timeout = ns_dyn_mem_alloc(sizeof(timeout_t));
if (!timeout) {
return NULL;
}
timeout->callback = callback;
timeout->arg = arg;
// Start timeout taskled if it is not running
if (-1 == timeout_tasklet_id) {
timeout_tasklet_id = eventOS_event_handler_create(timeout_tasklet, STARTUP_EVENT);
if (timeout_tasklet_id < 0) {
timeout_tasklet_id = -1;
goto FAIL;
}
}
arm_event_t event = {
.receiver = timeout_tasklet_id,
.sender = timeout_tasklet_id,
.event_type = TIMER_EVENT,
.event_id = TIMER_EVENT,
.data_ptr = timeout
};
if (period) {
storage = eventOS_event_timer_request_every(&event, period);
} else {
storage = eventOS_event_timer_request_at(&event, at);
}
timeout->event = storage;
if (storage) {
return timeout;
}
FAIL:
ns_dyn_mem_free(timeout);
return NULL;
}
timeout_t *eventOS_timeout_ms(void (*callback)(void *), uint32_t ms, void *arg)
{
return eventOS_timeout_at_(callback, arg, eventOS_event_timer_ms_to_ticks(ms) + eventOS_event_timer_ticks(), 0);
}
timeout_t *eventOS_timeout_every_ms(void (*callback)(void *), uint32_t every, void *arg)
{
return eventOS_timeout_at_(callback, arg, 0, eventOS_event_timer_ms_to_ticks(every));
}
void eventOS_timeout_cancel(timeout_t *t)
{
if (!t) {
return;
}
eventOS_cancel(t->event);
// Defer the freeing until returning from the callback
if (t->event->state != ARM_LIB_EVENT_RUNNING) {
ns_dyn_mem_free(t);
}
}

View File

@@ -0,0 +1,391 @@
/*
* Copyright (c) 2014-2015 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ns_types.h"
#include "ns_list.h"
#include "ns_timer.h"
#include "eventOS_callback_timer.h"
#include "platform/arm_hal_interrupt.h"
#include "platform/arm_hal_timer.h"
#include "nsdynmemLIB.h"
#ifndef NS_EXCLUDE_HIGHRES_TIMER
typedef enum ns_timer_state_e {
NS_TIMER_ACTIVE = 0, // Will run on the next HAL interrupt
NS_TIMER_HOLD, // Will run on a later HAL interrupt
NS_TIMER_RUN_INTERRUPT, // Running on the interrupt we're currently handling
NS_TIMER_STOP // Timer not scheduled ("start" not called since last callback)
} ns_timer_state_e;
typedef struct ns_timer_struct {
int8_t ns_timer_id;
ns_timer_state_e timer_state;
uint16_t slots;
uint16_t remaining_slots;
void (*interrupt_handler)(int8_t, uint16_t);
ns_list_link_t link;
} ns_timer_struct;
static NS_LIST_DEFINE(ns_timer_list, ns_timer_struct, link);
#define NS_TIMER_RUNNING 1
static uint8_t ns_timer_state = 0;
#ifdef ATMEGA256RFR2
#define COMPENSATION 3
#define COMPENSATION_TUNE 1
#else
#define COMPENSATION 0
#define COMPENSATION_TUNE 0
#endif
static void ns_timer_interrupt_handler(void);
static ns_timer_struct *ns_timer_get_pointer_to_timer_struct(int8_t timer_id);
static bool ns_timer_initialized = 0;
int8_t eventOS_callback_timer_register(void (*timer_interrupt_handler)(int8_t, uint16_t))
{
int8_t retval = -1;
if (!ns_timer_initialized) {
/*Set interrupt handler in HAL driver*/
platform_timer_set_cb(ns_timer_interrupt_handler);
ns_timer_initialized = 1;
}
/*Find first free timer ID in timer list*/
/*(Note use of uint8_t to avoid overflow if we reach 0x7F)*/
for (uint8_t i = 0; i <= INT8_MAX; i++) {
if (!ns_timer_get_pointer_to_timer_struct(i)) {
retval = i;
break;
}
}
if (retval == -1) {
return -1;
}
ns_timer_struct *new_timer = ns_dyn_mem_alloc(sizeof(ns_timer_struct));
if (!new_timer) {
return -1;
}
/*Initialise new timer*/
new_timer->ns_timer_id = retval;
new_timer->timer_state = NS_TIMER_STOP;
new_timer->remaining_slots = 0;
new_timer->interrupt_handler = timer_interrupt_handler;
// Critical section sufficient as long as list can't be reordered from
// interrupt, otherwise will need to cover whole routine
platform_enter_critical();
ns_list_add_to_end(&ns_timer_list, new_timer);
platform_exit_critical();
/*Return timer ID*/
return retval;
}
int8_t eventOS_callback_timer_unregister(int8_t ns_timer_id)
{
ns_timer_struct *current_timer;
current_timer = ns_timer_get_pointer_to_timer_struct(ns_timer_id);
if (!current_timer) {
return -1;
}
// Critical section sufficient as long as list can't be reordered from
// interrupt, otherwise will need to cover whole routine
platform_enter_critical();
ns_list_remove(&ns_timer_list, current_timer);
platform_exit_critical();
ns_dyn_mem_free(current_timer);
return 0;
}
static int8_t ns_timer_start_pl_timer(uint16_t pl_timer_start_slots)
{
/*Don't start timer with 0 slots*/
if (!pl_timer_start_slots) {
pl_timer_start_slots = 1;
}
/*Start HAL timer*/
platform_timer_start(pl_timer_start_slots);
/*Set HAL timer state to running*/
ns_timer_state |= NS_TIMER_RUNNING;
return 0;
}
int8_t ns_timer_sleep(void)
{
int8_t ret_val = -1;
if (ns_timer_state & NS_TIMER_RUNNING) {
/*Start HAL timer*/
platform_timer_disable();
/*Set HAL timer state to running*/
ns_timer_state &= ~NS_TIMER_RUNNING;
ret_val = 0;
}
return ret_val;
}
static int8_t ns_timer_get_next_running_to(void)
{
uint8_t hold_count = 0;
ns_timer_struct *first_timer = NULL;
/*Find hold-labelled timer with the least remaining slots*/
ns_list_foreach(ns_timer_struct, current_timer, &ns_timer_list) {
if (current_timer->timer_state == NS_TIMER_HOLD) {
if (!first_timer || current_timer->remaining_slots < first_timer->remaining_slots) {
first_timer = current_timer;
}
/*For optimisation, count the found timers*/
hold_count++;
}
}
if (!first_timer) {
return 0;
}
/*If hold-labelled timer found, set it active and start the HAL driver*/
hold_count--;
first_timer->timer_state = NS_TIMER_ACTIVE;
/*Compensate time spent in timer function*/
if (first_timer->remaining_slots > COMPENSATION) {
first_timer->remaining_slots -= COMPENSATION;
}
/*Start HAL timer*/
ns_timer_start_pl_timer(first_timer->remaining_slots);
/*Update other hold-labelled timers*/
ns_list_foreach(ns_timer_struct, current_timer, &ns_timer_list) {
if (hold_count == 0) { // early termination optimisation
break;
}
if (current_timer->timer_state == NS_TIMER_HOLD) {
if (current_timer->remaining_slots == first_timer->remaining_slots) {
current_timer->timer_state = NS_TIMER_ACTIVE;
} else {
current_timer->remaining_slots -= first_timer->remaining_slots;
/*Compensate time spent in timer function*/
if (current_timer->remaining_slots > COMPENSATION) {
current_timer->remaining_slots -= COMPENSATION;
}
}
hold_count--;
}
}
return 0;
}
static ns_timer_struct *ns_timer_get_pointer_to_timer_struct(int8_t timer_id)
{
/*Find timer with the given ID*/
ns_list_foreach(ns_timer_struct, current_timer, &ns_timer_list) {
if (current_timer->ns_timer_id == timer_id) {
return current_timer;
}
}
return NULL;
}
int8_t eventOS_callback_timer_start(int8_t ns_timer_id, uint16_t slots)
{
int8_t ret_val = 0;
uint16_t pl_timer_remaining_slots;
ns_timer_struct *timer;
platform_enter_critical();
/*Find timer to be activated*/
timer = ns_timer_get_pointer_to_timer_struct(ns_timer_id);
if (!timer) {
ret_val = -1;
goto exit;
}
// XXX this assumes the timer currently isn't running?
// Is event.c relying on this restarting HAL timer after ns_timer_sleep()?
/*If any timers are active*/
if (ns_timer_state & NS_TIMER_RUNNING) {
/*Get remaining slots of the currently activated timeout*/
pl_timer_remaining_slots = platform_timer_get_remaining_slots();
/*New timeout is shorter than currently enabled timeout*/
if (pl_timer_remaining_slots > slots) {
/*Start HAL timer*/
ns_timer_start_pl_timer(slots - 0);
ns_list_foreach(ns_timer_struct, current_timer, &ns_timer_list) {
/*Switch active timers to hold*/
if (current_timer->timer_state == NS_TIMER_ACTIVE) {
current_timer->timer_state = NS_TIMER_HOLD;
current_timer->remaining_slots = 0;
}
/*Update hold-labelled timers*/
if (current_timer->timer_state == NS_TIMER_HOLD) {
current_timer->remaining_slots += (pl_timer_remaining_slots - slots);
/*Compensate time spent in timer function*/
if (current_timer->remaining_slots > (COMPENSATION - COMPENSATION_TUNE)) {
current_timer->remaining_slots -= (COMPENSATION - COMPENSATION_TUNE);
}
}
}
/*Mark active and start the timer*/
timer->timer_state = NS_TIMER_ACTIVE;
timer->slots = slots;
timer->remaining_slots = slots;
}
/*New timeout is longer than currently enabled timeout*/
else if (pl_timer_remaining_slots < slots) {
/*Mark hold and update remaining slots*/
timer->timer_state = NS_TIMER_HOLD;
timer->slots = slots;
timer->remaining_slots = (slots - pl_timer_remaining_slots);
}
/*New timeout is equal to currently enabled timeout*/
else {
/*Mark it active and it will be handled in next interrupt*/
timer->timer_state = NS_TIMER_ACTIVE;
timer->slots = slots;
timer->remaining_slots = slots;
}
} else {
/*No timers running*/
timer->timer_state = NS_TIMER_HOLD;
timer->slots = slots;
timer->remaining_slots = slots;
/*Start next timeout*/
ns_timer_get_next_running_to();
}
exit:
platform_exit_critical();
return ret_val;
}
static void ns_timer_interrupt_handler(void)
{
uint8_t i = 0;
platform_enter_critical();
/*Clear timer running state*/
ns_timer_state &= ~NS_TIMER_RUNNING;
/*Mark active timers as NS_TIMER_RUN_INTERRUPT, interrupt functions are called at the end of this function*/
ns_list_foreach(ns_timer_struct, current_timer, &ns_timer_list) {
if (current_timer->timer_state == NS_TIMER_ACTIVE) {
current_timer->timer_state = NS_TIMER_RUN_INTERRUPT;
/*For optimisation, count the found timers*/
i++;
}
}
/*Start next timeout*/
ns_timer_get_next_running_to();
/*Call interrupt functions*/
ns_list_foreach(ns_timer_struct, current_timer, &ns_timer_list) {
if (i == 0) {
break;
}
if (current_timer->timer_state == NS_TIMER_RUN_INTERRUPT) {
current_timer->timer_state = NS_TIMER_STOP;
current_timer->interrupt_handler(current_timer->ns_timer_id, current_timer->slots);
i--;
}
}
platform_exit_critical();
}
int8_t eventOS_callback_timer_stop(int8_t ns_timer_id)
{
uint16_t pl_timer_remaining_slots;
bool active_timer_found = false;
ns_timer_struct *current_timer;
ns_timer_struct *first_timer = NULL;
int8_t retval = -1;
platform_enter_critical();
/*Find timer with given timer ID*/
current_timer = ns_timer_get_pointer_to_timer_struct(ns_timer_id);
if (!current_timer) {
goto exit;
}
retval = 0;
/*Check if already stopped*/
if (current_timer->timer_state == NS_TIMER_STOP) {
goto exit;
}
current_timer->timer_state = NS_TIMER_STOP;
current_timer->remaining_slots = 0;
/*Check if some timer is already active*/
ns_list_foreach(ns_timer_struct, curr_timer, &ns_timer_list) {
if (curr_timer->timer_state == NS_TIMER_ACTIVE) {
active_timer_found = true;
break;
}
}
/*If no active timers found, start one*/
if (!active_timer_found) {
pl_timer_remaining_slots = platform_timer_get_remaining_slots();
/*Find hold-labelled timer with the least remaining slots*/
ns_list_foreach(ns_timer_struct, cur_timer, &ns_timer_list) {
if (cur_timer->timer_state == NS_TIMER_HOLD) {
cur_timer->remaining_slots += pl_timer_remaining_slots;
if (!first_timer || cur_timer->remaining_slots < first_timer->remaining_slots) {
first_timer = cur_timer;
}
}
}
/*If hold-labelled timer found, set it active and start the HAL driver*/
if (first_timer) {
first_timer->timer_state = NS_TIMER_ACTIVE;
/*Start HAL timer*/
ns_timer_start_pl_timer(first_timer->remaining_slots);
/*If some of the other hold-labelled timers have the same remaining slots as the timer_tmp, mark them active*/
ns_list_foreach(ns_timer_struct, cur_timer, &ns_timer_list) {
if (cur_timer->timer_state == NS_TIMER_HOLD) {
if (cur_timer->remaining_slots == first_timer->remaining_slots) {
cur_timer->timer_state = NS_TIMER_ACTIVE;
} else {
cur_timer->remaining_slots -= first_timer->remaining_slots;
}
}
}
}
}
exit:
platform_exit_critical();
return retval;
}
#endif // NS_EXCLUDE_HIGHRES_TIMER

View File

@@ -0,0 +1,35 @@
/*
* Copyright (c) 2014-2015 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NS_TIMER_H_
#define NS_TIMER_H_
#include "platform/eventloop_config.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifndef NS_EXCLUDE_HIGHRES_TIMER
extern int8_t ns_timer_sleep(void);
#else
#define ns_timer_sleep() ((int8_t) 0)
#endif
#ifdef __cplusplus
}
#endif
#endif /*NS_TIMER_H_*/

View File

@@ -0,0 +1,373 @@
/*
* Copyright (c) 2014-2015 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ns_types.h"
#include "ns_list.h"
#include "timer_sys.h"
#include "platform/arm_hal_interrupt.h"
#include "platform/arm_hal_timer.h"
#include "nsdynmemLIB.h"
#include "eventOS_event.h"
#include "eventOS_event_timer.h"
#include "event.h"
#include "eventOS_callback_timer.h"
#include "ns_timer.h"
#ifndef ST_MAX
#define ST_MAX 6
#endif
static sys_timer_struct_s startup_sys_timer_pool[ST_MAX];
#define TIMER_SLOTS_PER_MS 20
NS_STATIC_ASSERT(1000 % EVENTOS_EVENT_TIMER_HZ == 0, "Need whole number of ms per tick")
#define TIMER_SYS_TICK_PERIOD (1000 / EVENTOS_EVENT_TIMER_HZ) // milliseconds
// timer_sys_ticks must be read in critical section to guarantee
// atomicity on 16-bit platforms
static volatile uint32_t timer_sys_ticks;
static NS_LIST_DEFINE(system_timer_free, sys_timer_struct_s, event.link);
static NS_LIST_DEFINE(system_timer_list, sys_timer_struct_s, event.link);
static sys_timer_struct_s *sys_timer_dynamically_allocate(void);
static void timer_sys_interrupt(void);
static void timer_sys_add(sys_timer_struct_s *timer);
#ifndef NS_EVENTLOOP_USE_TICK_TIMER
static int8_t platform_tick_timer_start(uint32_t period_ms);
/* Implement platform tick timer using eventOS timer */
// platform tick timer callback function
static void (*tick_timer_callback)(void);
static int8_t tick_timer_id = -1; // eventOS timer id for tick timer
// EventOS timer callback function
static void tick_timer_eventOS_callback(int8_t timer_id, uint16_t slots)
{
// Not interested in timer id or slots
(void)slots;
// Call the tick timer callback
if (tick_timer_callback != NULL && timer_id == tick_timer_id) {
platform_tick_timer_start(TIMER_SYS_TICK_PERIOD);
tick_timer_callback();
}
}
static int8_t platform_tick_timer_register(void (*tick_timer_cb)(void))
{
tick_timer_callback = tick_timer_cb;
tick_timer_id = eventOS_callback_timer_register(tick_timer_eventOS_callback);
return tick_timer_id;
}
static int8_t platform_tick_timer_start(uint32_t period_ms)
{
return eventOS_callback_timer_start(tick_timer_id, TIMER_SLOTS_PER_MS * period_ms);
}
static int8_t platform_tick_timer_stop(void)
{
return eventOS_callback_timer_stop(tick_timer_id);
}
#endif // !NS_EVENTLOOP_USE_TICK_TIMER
/*
* Initializes timers and starts system timer
*/
void timer_sys_init(void)
{
for (uint8_t i = 0; i < ST_MAX; i++) {
ns_list_add_to_start(&system_timer_free, &startup_sys_timer_pool[i]);
}
platform_tick_timer_register(timer_sys_interrupt);
platform_tick_timer_start(TIMER_SYS_TICK_PERIOD);
}
/*-------------------SYSTEM TIMER FUNCTIONS--------------------------*/
void timer_sys_disable(void)
{
platform_tick_timer_stop();
}
/*
* Starts ticking system timer interrupts every 10ms
*/
int8_t timer_sys_wakeup(void)
{
return platform_tick_timer_start(TIMER_SYS_TICK_PERIOD);
}
static void timer_sys_interrupt(void)
{
system_timer_tick_update(1);
}
/* * * * * * * * * */
static sys_timer_struct_s *sys_timer_dynamically_allocate(void)
{
return ns_dyn_mem_alloc(sizeof(sys_timer_struct_s));
}
static sys_timer_struct_s *timer_struct_get(void)
{
sys_timer_struct_s *timer;
platform_enter_critical();
timer = ns_list_get_first(&system_timer_free);
if (timer) {
ns_list_remove(&system_timer_free, timer);
} else {
timer = sys_timer_dynamically_allocate();
}
platform_exit_critical();
return timer;
}
void timer_sys_event_free(arm_event_storage_t *event)
{
platform_enter_critical();
sys_timer_struct_s *timer = NS_CONTAINER_OF(event, sys_timer_struct_s, event);
if (timer->period == 0) {
// Non-periodic - return to free list
ns_list_add_to_start(&system_timer_free, timer);
} else {
// Periodic - check due time of next launch
timer->launch_time += timer->period;
if (TICKS_BEFORE_OR_AT(timer->launch_time, timer_sys_ticks)) {
// next event is overdue - queue event now
eventOS_event_send_timer_allocated(&timer->event);
} else {
// add back to timer queue for the future
timer_sys_add(timer);
}
}
platform_exit_critical();
}
void timer_sys_event_cancel_critical(struct arm_event_storage *event)
{
sys_timer_struct_s *timer = NS_CONTAINER_OF(event, sys_timer_struct_s, event);
timer->period = 0;
// If its unqueued it is on my timer list, otherwise it is in event-loop.
if (event->state == ARM_LIB_EVENT_UNQUEUED) {
ns_list_remove(&system_timer_list, timer);
}
}
uint32_t eventOS_event_timer_ticks(void)
{
uint32_t ret_val;
// Enter/exit critical is a bit clunky, but necessary on 16-bit platforms,
// which won't be able to do an atomic 32-bit read.
platform_enter_critical();
ret_val = timer_sys_ticks;
platform_exit_critical();
return ret_val;
}
/* Called internally with lock held */
static void timer_sys_add(sys_timer_struct_s *timer)
{
uint32_t at = timer->launch_time;
// Find first timer scheduled to run after us, and insert before it.
// (This means timers scheduled for same time run in order of request)
ns_list_foreach(sys_timer_struct_s, t, &system_timer_list) {
if (TICKS_BEFORE(at, t->launch_time)) {
ns_list_add_before(&system_timer_list, t, timer);
return;
}
}
// Didn't insert before another timer, so must be last.
ns_list_add_to_end(&system_timer_list, timer);
}
/* Called internally with lock held */
static arm_event_storage_t *eventOS_event_timer_request_at_(const arm_event_t *event, uint32_t at, uint32_t period)
{
// Because we use user-allocated events, they must get delivered to avoid
// a leak. Previously this call queued timers for invalid tasks, then they
// would go undelivered. Now it returns an error.
if (!event_tasklet_handler_id_valid(event->receiver)) {
return NULL;
}
sys_timer_struct_s *timer = timer_struct_get();
if (!timer) {
return NULL;
}
timer->event.data = *event;
timer->event.allocator = ARM_LIB_EVENT_TIMER;
timer->event.state = ARM_LIB_EVENT_UNQUEUED;
timer->launch_time = at;
timer->period = period;
if (TICKS_BEFORE_OR_AT(at, timer_sys_ticks)) {
eventOS_event_send_timer_allocated(&timer->event);
} else {
timer_sys_add(timer);
}
return &timer->event;
}
arm_event_storage_t *eventOS_event_timer_request_at(const arm_event_t *event, uint32_t at)
{
platform_enter_critical();
arm_event_storage_t *ret = eventOS_event_timer_request_at_(event, at, 0);
platform_exit_critical();
return ret;
}
arm_event_storage_t *eventOS_event_timer_request_in(const arm_event_t *event, int32_t in)
{
platform_enter_critical();
arm_event_storage_t *ret = eventOS_event_timer_request_at_(event, timer_sys_ticks + in, 0);
platform_exit_critical();
return ret;
}
arm_event_storage_t *eventOS_event_timer_request_every(const arm_event_t *event, int32_t period)
{
if (period <= 0) {
return NULL;
}
platform_enter_critical();
arm_event_storage_t *ret = eventOS_event_timer_request_at_(event, timer_sys_ticks + period, period);
platform_exit_critical();
return ret;
}
int8_t eventOS_event_timer_request(uint8_t event_id, uint8_t event_type, int8_t tasklet_id, uint32_t time)
{
const arm_event_t event = {
.event_id = event_id,
.event_type = event_type,
.receiver = tasklet_id,
.sender = 0,
.data_ptr = NULL,
.event_data = 0,
.priority = ARM_LIB_MED_PRIORITY_EVENT,
};
// Legacy time behaviour preserved
// Note that someone wanting 20ms gets 2 ticks, thanks to this test. 30ms would be 4 ticks.
// And why shouldn't they be able to get a 1-tick callback?
if (time > 2 * TIMER_SYS_TICK_PERIOD) {
time /= TIMER_SYS_TICK_PERIOD;
// XXX Why this? Someone wanting 50ms shouldn't get 6 ticks. Round to nearest, maybe?
time++;
} else {
time = 2;
}
platform_enter_critical();
arm_event_storage_t *ret = eventOS_event_timer_request_at_(&event, timer_sys_ticks + time, 0);
platform_exit_critical();
return ret ? 0 : -1;
}
int8_t eventOS_event_timer_cancel(uint8_t event_id, int8_t tasklet_id)
{
platform_enter_critical();
/* First check pending timers */
ns_list_foreach(sys_timer_struct_s, cur, &system_timer_list) {
if (cur->event.data.receiver == tasklet_id && cur->event.data.event_id == event_id) {
eventOS_cancel(&cur->event);
goto done;
}
}
/* No pending timer, so check for already-pending event */
arm_event_storage_t *event = eventOS_event_find_by_id_critical(tasklet_id, event_id);
if (event && event->allocator == ARM_LIB_EVENT_TIMER) {
eventOS_cancel(event);
goto done;
}
/* No match found */
platform_exit_critical();
return -1;
done:
platform_exit_critical();
return 0;
}
uint32_t eventOS_event_timer_shortest_active_timer(void)
{
uint32_t ret_val = 0;
platform_enter_critical();
sys_timer_struct_s *first = ns_list_get_first(&system_timer_list);
if (first == NULL) {
// Weird API has 0 for "no events"
ret_val = 0;
} else if (TICKS_BEFORE_OR_AT(first->launch_time, timer_sys_ticks)) {
// Which means an immediate/overdue event has to be 1
ret_val = 1;
} else {
ret_val = first->launch_time - timer_sys_ticks;
}
platform_exit_critical();
return eventOS_event_timer_ticks_to_ms(ret_val);
}
void system_timer_tick_update(uint32_t ticks)
{
platform_enter_critical();
//Keep runtime time
timer_sys_ticks += ticks;
ns_list_foreach_safe(sys_timer_struct_s, cur, &system_timer_list) {
if (TICKS_BEFORE_OR_AT(cur->launch_time, timer_sys_ticks)) {
// Unthread from our list
ns_list_remove(&system_timer_list, cur);
// Make it an event (can't fail - no allocation)
// event system will call our timer_sys_event_free on event delivery.
eventOS_event_send_timer_allocated(&cur->event);
} else {
// List is ordered, so as soon as we see a later event, we're done.
break;
}
}
platform_exit_critical();
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright (c) 2014-2015 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _PL_NANO_TIMER_SYS_H_
#define _PL_NANO_TIMER_SYS_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "eventOS_event.h"
/* We borrow base event storage, including its list link, and add a time field */
typedef struct sys_timer_struct_s {
arm_event_storage_t event;
uint32_t launch_time; // tick value
uint32_t period;
} sys_timer_struct_s;
/**
* Initialize system timer
* */
extern void timer_sys_init(void);
extern uint32_t timer_get_runtime_ticks(void);
int8_t timer_sys_wakeup(void);
void timer_sys_disable(void);
void timer_sys_event_free(struct arm_event_storage *event);
// This require lock to be held
void timer_sys_event_cancel_critical(struct arm_event_storage *event);
/**
* System Timer update and synch after sleep
*
* \param ticks Time in 10 ms resolution
*
* \return none
*
* */
void system_timer_tick_update(uint32_t ticks);
#ifdef __cplusplus
}
#endif
#endif /*_PL_NANO_TIMER_SYS_H_*/