2013-09-01 08:19:24 +00:00
|
|
|
/*
|
|
|
|
Nil RTOS - Copyright (C) 2012 Giovanni Di Sirio.
|
|
|
|
|
|
|
|
This file is part of Nil RTOS.
|
|
|
|
|
|
|
|
Nil RTOS is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Nil RTOS is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file nil.h
|
|
|
|
* @brief Nil RTOS main header file.
|
|
|
|
* @details This header includes all the required kernel headers so it is the
|
|
|
|
* only header you usually need to include in your application.
|
|
|
|
*
|
|
|
|
* @addtogroup nil
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _NIL_H_
|
|
|
|
#define _NIL_H_
|
|
|
|
|
2013-09-05 14:54:24 +00:00
|
|
|
typedef struct nil_thread thread_t;
|
|
|
|
|
2013-09-01 08:19:24 +00:00
|
|
|
#include "nilconf.h"
|
|
|
|
#include "niltypes.h"
|
2013-09-05 14:54:24 +00:00
|
|
|
#include "nilcore.h"
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module constants. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Nil RTOS identification
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
#define _NIL_ /**< @brief Nil RTOS identification.*/
|
|
|
|
|
2013-09-03 13:50:15 +00:00
|
|
|
#define CH_KERNEL_VERSION "0.1.0alpha"
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-03 13:50:15 +00:00
|
|
|
#define CH_KERNEL_MAJOR 0
|
|
|
|
#define CH_KERNEL_MINOR 1
|
|
|
|
#define CH_KERNEL_PATCH 0
|
2013-09-01 08:19:24 +00:00
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
2013-09-03 13:21:10 +00:00
|
|
|
* @name Wakeup messages
|
2013-09-01 08:19:24 +00:00
|
|
|
* @{
|
|
|
|
*/
|
2013-09-03 13:21:10 +00:00
|
|
|
#define MSG_OK 0 /**< @brief Normal wakeup message. */
|
|
|
|
#define MSG_TIMEOUT -1 /**< @brief Wake-up caused by a timeout
|
2013-09-01 08:19:24 +00:00
|
|
|
condition. */
|
2013-09-03 13:21:10 +00:00
|
|
|
#define MSG_RESET -2 /**< @brief Wake-up caused by a reset
|
2013-09-01 08:19:24 +00:00
|
|
|
condition. */
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Special time constants
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @brief Zero time specification for some functions with a timeout
|
|
|
|
* specification.
|
|
|
|
* @note Not all functions accept @p TIME_IMMEDIATE as timeout parameter,
|
|
|
|
* see the specific function documentation.
|
|
|
|
*/
|
|
|
|
#define TIME_IMMEDIATE ((systime_t)-1)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Infinite time specification for all functions with a timeout
|
|
|
|
* specification.
|
|
|
|
*/
|
|
|
|
#define TIME_INFINITE ((systime_t)0)
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Thread state related macros
|
|
|
|
* @{
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#define NIL_STATE_READY 0 /**< @brief Thread ready or executing. */
|
|
|
|
#define NIL_STATE_SLEEPING 1 /**< @brief Thread sleeping. */
|
|
|
|
#define NIL_STATE_SUSP 2 /**< @brief Thread suspended. */
|
|
|
|
#define NIL_STATE_WTSEM 3 /**< @brief Thread waiting on semaphore.*/
|
2013-09-04 10:26:42 +00:00
|
|
|
#define NIL_STATE_WTOREVT 4 /**< @brief Thread waiting for events. */
|
2013-09-03 13:50:15 +00:00
|
|
|
#define NIL_THD_IS_READY(tr) ((tr)->state == NIL_STATE_READY)
|
|
|
|
#define NIL_THD_IS_SLEEPING(tr) ((tr)->state == NIL_STATE_SLEEPING)
|
|
|
|
#define NIL_THD_IS_SUSP(tr) ((tr)->state == NIL_STATE_SUSP)
|
|
|
|
#define NIL_THD_IS_WTSEM(tr) ((tr)->state == NIL_STATE_WTSEM)
|
2013-09-04 10:26:42 +00:00
|
|
|
#define NIL_THD_IS_WTOREVT(tr) ((tr)->state == NIL_STATE_WTOREVT)
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Events related macros
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @brief All events allowed mask.
|
|
|
|
*/
|
|
|
|
#define ALL_EVENTS ((eventmask_t)-1)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns an event mask from an event identifier.
|
|
|
|
*/
|
|
|
|
#define EVENT_MASK(eid) ((eventmask_t)(1 << (eid)))
|
2013-09-01 08:19:24 +00:00
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module pre-compile time settings. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Number of user threads in the application.
|
|
|
|
* @note This number is not inclusive of the idle thread which is
|
|
|
|
* implicitly handled.
|
|
|
|
*/
|
|
|
|
#if !defined(NIL_CFG_NUM_THREADS) || defined(__DOXYGEN__)
|
|
|
|
#define NIL_CFG_NUM_THREADS 2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2013-09-11 12:24:04 +00:00
|
|
|
* @brief System time counter resolution.
|
|
|
|
* @note Allowed values are 16 or 32 bits.
|
|
|
|
*/
|
|
|
|
#if !defined(NIL_CFG_ST_RESOLUTION) || defined(__DOXYGEN__)
|
|
|
|
#define NIL_CFG_ST_RESOLUTION 32
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief System tick frequency.
|
|
|
|
* @note This value together with the @p NIL_CFG_ST_RESOLUTION
|
|
|
|
* option defines the maximum amount of time allowed for
|
|
|
|
* timeouts.
|
2013-09-01 08:19:24 +00:00
|
|
|
*/
|
2013-09-04 10:26:42 +00:00
|
|
|
#if !defined(NIL_CFG_ST_FREQUENCY) || defined(__DOXYGEN__)
|
|
|
|
#define NIL_CFG_ST_FREQUENCY 100
|
2013-09-01 08:19:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Time delta constant for the tick-less mode.
|
|
|
|
* @note If this value is zero then the system uses the classic
|
|
|
|
* periodic tick. This value represents the minimum number
|
|
|
|
* of ticks that is safe to specify in a timeout directive.
|
|
|
|
* The value one is not valid, timeouts are rounded up to
|
|
|
|
* this value.
|
|
|
|
*/
|
2013-09-11 12:00:01 +00:00
|
|
|
#if !defined(NIL_CFG_ST_TIMEDELTA) || defined(__DOXYGEN__)
|
|
|
|
#define NIL_CFG_ST_TIMEDELTA 0
|
2013-09-01 08:19:24 +00:00
|
|
|
#endif
|
|
|
|
|
2013-09-04 10:26:42 +00:00
|
|
|
/**
|
|
|
|
* @brief Events Flags APIs.
|
|
|
|
* @details If enabled then the event flags APIs are included in the kernel.
|
|
|
|
*
|
|
|
|
* @note The default is @p TRUE.
|
|
|
|
*/
|
|
|
|
#if !defined(NIL_CFG_USE_EVENTS) || defined(__DOXYGEN__)
|
|
|
|
#define NIL_CFG_USE_EVENTS TRUE
|
|
|
|
#endif
|
|
|
|
|
2013-09-01 08:19:24 +00:00
|
|
|
/**
|
|
|
|
* @brief System assertions.
|
|
|
|
*/
|
|
|
|
#if !defined(NIL_CFG_ENABLE_ASSERTS) || defined(__DOXYGEN__)
|
|
|
|
#define NIL_CFG_ENABLE_ASSERTS FALSE
|
|
|
|
#endif
|
|
|
|
|
2013-09-05 12:33:42 +00:00
|
|
|
/**
|
|
|
|
* @brief Stack check.
|
|
|
|
*/
|
|
|
|
#if !defined(NIL_CFG_ENABLE_STACK_CHECK) || defined(__DOXYGEN__)
|
|
|
|
#define NIL_CFG_ENABLE_STACK_CHECK FALSE
|
|
|
|
#endif
|
|
|
|
|
2013-09-01 08:19:24 +00:00
|
|
|
/**
|
|
|
|
* @brief Threads descriptor structure extension.
|
|
|
|
* @details User fields added to the end of the @p thread_t structure.
|
|
|
|
*/
|
|
|
|
#if !defined(NIL_CFG_THREAD_EXT_FIELDS) || defined(__DOXYGEN__)
|
|
|
|
#define NIL_CFG_THREAD_EXT_FIELDS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Derived constants and error checks. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
#if NIL_CFG_NUM_THREADS < 1
|
|
|
|
#error "at least one thread must be defined"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if NIL_CFG_NUM_THREADS > 12
|
|
|
|
#error "Nil is not recommended for thread-intensive applications, consider" \
|
|
|
|
"ChibiOS/RT instead"
|
|
|
|
#endif
|
|
|
|
|
2013-09-11 12:24:04 +00:00
|
|
|
#if (NIL_CFG_ST_RESOLUTION != 16) && (NIL_CFG_ST_RESOLUTION != 32)
|
|
|
|
#error "invalid NIL_CFG_ST_RESOLUTION specified, must be 16 or 32"
|
|
|
|
#endif
|
|
|
|
|
2013-09-04 10:26:42 +00:00
|
|
|
#if NIL_CFG_ST_FREQUENCY <= 0
|
2013-09-11 12:24:04 +00:00
|
|
|
#error "invalid NIL_CFG_ST_FREQUENCY specified, must be greated than zero"
|
2013-09-01 08:19:24 +00:00
|
|
|
#endif
|
|
|
|
|
2013-09-11 12:00:01 +00:00
|
|
|
#if (NIL_CFG_ST_TIMEDELTA < 0) || (NIL_CFG_ST_TIMEDELTA == 1)
|
2013-09-11 12:24:04 +00:00
|
|
|
#error "invalid NIL_CFG_ST_TIMEDELTA specified, must " \
|
|
|
|
"be zero or greater than one"
|
2013-09-01 08:19:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if NIL_CFG_ENABLE_ASSERTS
|
2013-09-05 14:54:24 +00:00
|
|
|
#define NIL_DBG_ENABLED TRUE
|
|
|
|
#else
|
|
|
|
#define NIL_DBG_ENABLED FALSE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** Boundaries of the idle thread boundaries, only required if stack checking
|
|
|
|
is enabled.*/
|
|
|
|
#if NIL_CFG_ENABLE_STACK_CHECK
|
|
|
|
extern stkalign_t __main_thread_stack_base__, __main_thread_stack_end__;
|
|
|
|
|
|
|
|
#define THD_IDLE_BASE (&__main_thread_stack_base__)
|
|
|
|
#define THD_IDLE_END (&__main_thread_stack_end__)
|
2013-09-01 08:19:24 +00:00
|
|
|
#else
|
2013-09-05 14:54:24 +00:00
|
|
|
#define THD_IDLE_BASE NULL
|
|
|
|
#define THD_IDLE_END NULL
|
2013-09-01 08:19:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module data structures and types. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Type of internal context structure.
|
|
|
|
*/
|
|
|
|
typedef struct port_intctx intctx_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Type of a structure representing a counting semaphore.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
volatile cnt_t cnt; /**< @brief Semaphore counter. */
|
|
|
|
} semaphore_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Thread function.
|
|
|
|
*/
|
|
|
|
typedef void (*tfunc_t)(void *);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Type of a structure representing a thread static configuration.
|
|
|
|
*/
|
|
|
|
typedef struct nil_thread_cfg thread_config_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Type of a structure representing a thread.
|
|
|
|
*/
|
|
|
|
typedef struct nil_thread thread_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Structure representing a thread static configuration.
|
|
|
|
*/
|
|
|
|
struct nil_thread_cfg {
|
2013-09-05 14:54:24 +00:00
|
|
|
stkalign_t *wbase; /**< @brief Thread working area base. */
|
|
|
|
stkalign_t *wend; /**< @brief Thread working area end. */
|
2013-09-01 08:19:24 +00:00
|
|
|
const char *namep; /**< @brief Thread name, for debugging. */
|
|
|
|
tfunc_t funcp; /**< @brief Thread function. */
|
|
|
|
void *arg; /**< @brief Thread function argument. */
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Type of a thread reference.
|
|
|
|
*/
|
2013-09-04 08:19:38 +00:00
|
|
|
typedef thread_t * thread_reference_t;
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Structure representing a thread.
|
|
|
|
*/
|
|
|
|
struct nil_thread {
|
|
|
|
intctx_t *ctxp; /**< @brief Pointer to internal context. */
|
|
|
|
tstate_t state; /**< @brief Thread state. */
|
|
|
|
/* Note, the following union contains a pointer while the thread is in a
|
2013-09-05 12:33:42 +00:00
|
|
|
sleeping state (!NIL_THD_IS_READY()) else contains the wake-up message.*/
|
2013-09-01 08:19:24 +00:00
|
|
|
union {
|
|
|
|
msg_t msg; /**< @brief Wake-up message. */
|
|
|
|
void *p; /**< @brief Generic pointer. */
|
2013-09-04 08:19:38 +00:00
|
|
|
thread_reference_t *trp; /**< @brief Pointer to thread reference. */
|
2013-09-01 08:19:24 +00:00
|
|
|
semaphore_t *semp; /**< @brief Pointer to semaphore. */
|
2013-09-04 10:26:42 +00:00
|
|
|
#if NIL_CFG_USE_EVENTS
|
|
|
|
eventmask_t ewmask; /**< @brief Enabled events mask. */
|
|
|
|
#endif
|
2013-09-01 08:19:24 +00:00
|
|
|
} u1;
|
|
|
|
volatile systime_t timeout;/**< @brief Timeout counter, zero
|
|
|
|
if disabled. */
|
2013-09-04 10:26:42 +00:00
|
|
|
#if NIL_CFG_USE_EVENTS
|
|
|
|
eventmask_t epmask; /**< @brief Pending events mask. */
|
2013-09-05 14:54:24 +00:00
|
|
|
#endif
|
|
|
|
#if NIL_CFG_ENABLE_STACK_CHECK || defined(__DOXYGEN__)
|
|
|
|
stkalign_t *stklim;/**< @brief Thread stack boundary. */
|
2013-09-04 10:26:42 +00:00
|
|
|
#endif
|
2013-09-01 08:19:24 +00:00
|
|
|
/* Optional extra fields.*/
|
|
|
|
NIL_CFG_THREAD_EXT_FIELDS
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief System data structure.
|
|
|
|
* @note This structure contain all the data areas used by the OS except
|
|
|
|
* stacks.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
/**
|
|
|
|
* @brief Pointer to the running thread.
|
|
|
|
*/
|
2013-09-05 12:33:42 +00:00
|
|
|
thread_t *current;
|
2013-09-01 08:19:24 +00:00
|
|
|
/**
|
|
|
|
* @brief Pointer to the next thread to be executed.
|
|
|
|
* @note This pointer must point at the same thread pointed by @p currp
|
|
|
|
* or to an higher priority thread if a switch is required.
|
|
|
|
*/
|
2013-09-05 12:33:42 +00:00
|
|
|
thread_t *next;
|
2013-09-11 12:00:01 +00:00
|
|
|
#if NIL_CFG_ST_TIMEDELTA == 0 || defined(__DOXYGEN__)
|
2013-09-01 08:19:24 +00:00
|
|
|
/**
|
|
|
|
* @brief System time.
|
|
|
|
*/
|
2013-09-04 10:26:42 +00:00
|
|
|
systime_t systime;
|
2013-09-01 08:19:24 +00:00
|
|
|
#endif
|
2013-09-11 12:00:01 +00:00
|
|
|
#if NIL_CFG_ST_TIMEDELTA > 0 || defined(__DOXYGEN__)
|
2013-09-01 08:19:24 +00:00
|
|
|
/**
|
|
|
|
* @brief System time of the last tick event.
|
|
|
|
*/
|
2013-09-04 10:26:42 +00:00
|
|
|
systime_t lasttime;
|
2013-09-01 08:19:24 +00:00
|
|
|
/**
|
|
|
|
* @brief Time of the next scheduled tick event.
|
|
|
|
*/
|
2013-09-04 10:26:42 +00:00
|
|
|
systime_t nexttime;
|
2013-09-01 08:19:24 +00:00
|
|
|
#endif
|
|
|
|
/**
|
|
|
|
* @brief Thread structures for all the defined threads.
|
|
|
|
*/
|
2013-09-04 10:26:42 +00:00
|
|
|
thread_t threads[NIL_CFG_NUM_THREADS + 1];
|
2013-09-05 14:54:24 +00:00
|
|
|
#if NIL_DBG_ENABLED || defined(__DOXYGEN__)
|
2013-09-01 08:19:24 +00:00
|
|
|
/**
|
|
|
|
* @brief Panic message.
|
|
|
|
* @note This field is only present if some debug options have been
|
|
|
|
* activated.
|
|
|
|
*/
|
2013-09-04 10:26:42 +00:00
|
|
|
const char *dbg_panic_msg;
|
2013-09-01 08:19:24 +00:00
|
|
|
#endif
|
|
|
|
} nil_system_t;
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module macros. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Threads tables definition macros
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @brief Start of user threads table.
|
|
|
|
*/
|
2013-09-03 13:35:40 +00:00
|
|
|
#define THD_TABLE_BEGIN \
|
2013-09-01 08:19:24 +00:00
|
|
|
const thread_config_t nil_thd_configs[NIL_CFG_NUM_THREADS + 1] = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Entry of user threads table
|
|
|
|
*/
|
2013-09-05 14:54:24 +00:00
|
|
|
#define THD_TABLE_ENTRY(wap, name, funcp, arg) \
|
|
|
|
{wap, (wap) + sizeof (wap), name, funcp, arg},
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief End of user threads table.
|
|
|
|
*/
|
2013-09-03 13:35:40 +00:00
|
|
|
#define THD_TABLE_END \
|
2013-09-05 14:54:24 +00:00
|
|
|
{THD_IDLE_BASE, THD_IDLE_END, "idle", 0, NULL} \
|
2013-09-01 08:19:24 +00:00
|
|
|
};
|
2013-09-03 13:21:10 +00:00
|
|
|
/** @} */
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-03 13:21:10 +00:00
|
|
|
/**
|
|
|
|
* @name Working Areas and Alignment
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @brief Enforces a correct alignment for a stack area size value.
|
|
|
|
*
|
|
|
|
* @param[in] n the stack size to be aligned to the next stack
|
|
|
|
* alignment boundary
|
|
|
|
* @return The aligned stack size.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
|
|
|
#define THD_ALIGN_STACK_SIZE(n) \
|
|
|
|
((((n) - 1) | (sizeof(stkalign_t) - 1)) + 1)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Calculates the total Working Area size.
|
|
|
|
*
|
|
|
|
* @param[in] n the stack size to be assigned to the thread
|
|
|
|
* @return The total used memory in bytes.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
|
|
|
#define THD_WORKING_AREA_SIZE(n) \
|
|
|
|
THD_ALIGN_STACK_SIZE(sizeof(thread_t) + PORT_WA_SIZE(n))
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Static working area allocation.
|
|
|
|
* @details This macro is used to allocate a static thread working area
|
|
|
|
* aligned as both position and size.
|
|
|
|
*
|
|
|
|
* @param[in] s the name to be assigned to the stack array
|
|
|
|
* @param[in] n the stack size to be assigned to the thread
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
|
|
|
#define THD_WORKING_AREA(s, n) \
|
|
|
|
stkalign_t s[THD_WORKING_AREA_SIZE(n) / sizeof(stkalign_t)]
|
2013-09-01 08:19:24 +00:00
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
2013-09-03 13:21:10 +00:00
|
|
|
* @name Threads abstraction macros
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @brief Thread declaration macro.
|
|
|
|
* @note Thread declarations should be performed using this macro because
|
|
|
|
* the port layer could define optimizations for thread functions.
|
|
|
|
*/
|
|
|
|
#define THD_FUNCTION(tname, arg) PORT_THD_FUNCTION(tname, arg)
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name ISRs abstraction macros
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @brief IRQ handler enter code.
|
|
|
|
* @note Usually IRQ handlers functions are also declared naked.
|
|
|
|
* @note On some architectures this macro can be empty.
|
|
|
|
*
|
|
|
|
* @special
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#define CH_IRQ_PROLOGUE() PORT_IRQ_PROLOGUE()
|
2013-09-03 13:21:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief IRQ handler exit code.
|
|
|
|
* @note Usually IRQ handlers function are also declared naked.
|
|
|
|
*
|
|
|
|
* @special
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#define CH_IRQ_EPILOGUE() PORT_IRQ_EPILOGUE()
|
2013-09-03 13:21:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Standard normal IRQ handler declaration.
|
|
|
|
* @note @p id can be a function name or a vector number depending on the
|
|
|
|
* port implementation.
|
|
|
|
*
|
|
|
|
* @special
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#define CH_IRQ_HANDLER(id) PORT_IRQ_HANDLER(id)
|
2013-09-03 13:21:10 +00:00
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Fast ISRs abstraction macros
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @brief Standard fast IRQ handler declaration.
|
|
|
|
* @note @p id can be a function name or a vector number depending on the
|
|
|
|
* port implementation.
|
|
|
|
* @note Not all architectures support fast interrupts.
|
|
|
|
*
|
|
|
|
* @special
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#define CH_FAST_IRQ_HANDLER(id) PORT_FAST_IRQ_HANDLER(id)
|
2013-09-03 13:21:10 +00:00
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Time conversion utilities
|
2013-09-01 08:19:24 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
/**
|
2013-09-03 13:21:10 +00:00
|
|
|
* @brief Seconds to system ticks.
|
|
|
|
* @details Converts from seconds to system ticks number.
|
|
|
|
* @note The result is rounded upward to the next tick boundary.
|
|
|
|
*
|
|
|
|
* @param[in] sec number of seconds
|
|
|
|
* @return The number of ticks.
|
|
|
|
*
|
|
|
|
* @api
|
2013-09-01 08:19:24 +00:00
|
|
|
*/
|
2013-09-03 13:21:10 +00:00
|
|
|
#define S2ST(sec) \
|
2013-09-04 10:26:42 +00:00
|
|
|
((systime_t)((sec) * NIL_CFG_ST_FREQUENCY))
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-03 13:21:10 +00:00
|
|
|
/**
|
|
|
|
* @brief Milliseconds to system ticks.
|
|
|
|
* @details Converts from milliseconds to system ticks number.
|
|
|
|
* @note The result is rounded upward to the next tick boundary.
|
|
|
|
*
|
|
|
|
* @param[in] msec number of milliseconds
|
|
|
|
* @return The number of ticks.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
|
|
|
#define MS2ST(msec) \
|
2013-09-04 10:26:42 +00:00
|
|
|
((systime_t)(((((uint32_t)(msec)) * \
|
|
|
|
((uint32_t)NIL_CFG_ST_FREQUENCY) - 1UL) / 1000UL) + 1UL))
|
2013-09-03 13:21:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Microseconds to system ticks.
|
|
|
|
* @details Converts from microseconds to system ticks number.
|
|
|
|
* @note The result is rounded upward to the next tick boundary.
|
|
|
|
*
|
|
|
|
* @param[in] usec number of microseconds
|
|
|
|
* @return The number of ticks.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
|
|
|
#define US2ST(usec) \
|
2013-09-04 10:26:42 +00:00
|
|
|
((systime_t)(((((uint32_t)(usec)) * \
|
|
|
|
((uint32_t)NIL_CFG_ST_FREQUENCY) - 1UL) / 1000000UL) + 1UL))
|
2013-09-03 13:21:10 +00:00
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Macro Functions
|
|
|
|
* @{
|
|
|
|
*/
|
2013-09-01 08:19:24 +00:00
|
|
|
/**
|
|
|
|
* @brief Enters the kernel lock mode.
|
|
|
|
*
|
|
|
|
* @special
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#define chSysDisable() port_disable()
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Enters the kernel lock mode.
|
|
|
|
*
|
|
|
|
* @special
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#define chSysEnable() port_enable()
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Enters the kernel lock mode.
|
|
|
|
*
|
|
|
|
* @special
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#define chSysLock() port_lock()
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Leaves the kernel lock mode.
|
|
|
|
*
|
|
|
|
* @special
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#define chSysUnlock() port_unlock()
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Enters the kernel lock mode from within an interrupt handler.
|
|
|
|
* @note This API may do nothing on some architectures, it is required
|
|
|
|
* because on ports that support preemptable interrupt handlers
|
|
|
|
* it is required to raise the interrupt mask to the same level of
|
|
|
|
* the system mutual exclusion zone.<br>
|
|
|
|
* It is good practice to invoke this API before invoking any I-class
|
|
|
|
* syscall from an interrupt handler.
|
|
|
|
* @note This API must be invoked exclusively from interrupt handlers.
|
|
|
|
*
|
|
|
|
* @special
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#define chSysLockFromISR() port_lock_from_isr()
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Leaves the kernel lock mode from within an interrupt handler.
|
|
|
|
*
|
|
|
|
* @note This API may do nothing on some architectures, it is required
|
|
|
|
* because on ports that support preemptable interrupt handlers
|
|
|
|
* it is required to raise the interrupt mask to the same level of
|
|
|
|
* the system mutual exclusion zone.<br>
|
|
|
|
* It is good practice to invoke this API after invoking any I-class
|
|
|
|
* syscall from an interrupt handler.
|
|
|
|
* @note This API must be invoked exclusively from interrupt handlers.
|
|
|
|
*
|
|
|
|
* @special
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#define chSysUnlockFromISR() port_unlock_from_isr()
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-05 10:34:09 +00:00
|
|
|
/**
|
|
|
|
* @brief Evaluates if a reschedule is required.
|
|
|
|
*
|
|
|
|
* @retval true if there is a thread that must go in running state
|
|
|
|
* immediately.
|
|
|
|
* @retval false if preemption is not required.
|
|
|
|
*
|
|
|
|
* @iclass
|
|
|
|
*/
|
|
|
|
#define chSchIsRescRequiredI() ((bool)(nil.current != nil.next))
|
|
|
|
|
2013-09-01 08:19:24 +00:00
|
|
|
/**
|
|
|
|
* @brief Delays the invoking thread for the specified number of seconds.
|
|
|
|
* @note The specified time is rounded up to a value allowed by the real
|
|
|
|
* system clock.
|
|
|
|
* @note The maximum specified value is implementation dependent.
|
|
|
|
*
|
|
|
|
* @param[in] sec time in seconds, must be different from zero
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#define chThdSleepSeconds(sec) chThdSleep(S2ST(sec))
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Delays the invoking thread for the specified number of
|
|
|
|
* milliseconds.
|
|
|
|
* @note The specified time is rounded up to a value allowed by the real
|
|
|
|
* system clock.
|
|
|
|
* @note The maximum specified value is implementation dependent.
|
|
|
|
*
|
|
|
|
* @param[in] msec time in milliseconds, must be different from zero
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#define chThdSleepMilliseconds(msec) chThdSleep(MS2ST(msec))
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Delays the invoking thread for the specified number of
|
|
|
|
* microseconds.
|
|
|
|
* @note The specified time is rounded up to a value allowed by the real
|
|
|
|
* system clock.
|
|
|
|
* @note The maximum specified value is implementation dependent.
|
|
|
|
*
|
|
|
|
* @param[in] usec time in microseconds, must be different from zero
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#define chThdSleepMicroseconds(usec) chThdSleep(US2ST(usec))
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Suspends the invoking thread for the specified time.
|
|
|
|
*
|
|
|
|
* @param[in] timeout the delay in system ticks
|
|
|
|
*
|
|
|
|
* @sclass
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#define chThdSleepS(timeout) chSchGoSleepTimeoutS(NIL_STATE_SLEEPING, timeout)
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Suspends the invoking thread until the system time arrives to the
|
|
|
|
* specified value.
|
|
|
|
*
|
|
|
|
* @param[in] time absolute system time
|
|
|
|
*
|
|
|
|
* @sclass
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#define chThdSleepUntilS(time) \
|
2013-09-04 10:26:42 +00:00
|
|
|
chSchGoSleepTimeoutS(NIL_STATE_SLEEPING, (time) - chVTGetSystemTimeX())
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initializes a semaphore with the specified counter value.
|
|
|
|
*
|
|
|
|
* @param[out] sp pointer to a @p semaphore_t structure
|
|
|
|
* @param[in] n initial value of the semaphore counter. Must be
|
|
|
|
* non-negative.
|
|
|
|
*
|
|
|
|
* @init
|
|
|
|
*/
|
2013-09-04 08:19:38 +00:00
|
|
|
#define chSemObjectInit(sp, n) ((sp)->cnt = n)
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Performs a wait operation on a semaphore.
|
|
|
|
*
|
|
|
|
* @param[in] sp pointer to a @p semaphore_t structure
|
|
|
|
* @return A message specifying how the invoking thread has been
|
|
|
|
* released from the semaphore.
|
2013-09-03 13:50:15 +00:00
|
|
|
* @retval CH_MSG_OK if the thread has not stopped on the semaphore or the
|
2013-09-01 08:19:24 +00:00
|
|
|
* semaphore has been signaled.
|
2013-09-03 13:50:15 +00:00
|
|
|
* @retval CH_MSG_RST if the semaphore has been reset using @p chSemReset().
|
2013-09-01 08:19:24 +00:00
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#define chSemWait(sp) chSemWaitTimeout(sp, TIME_INFINITE)
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Performs a wait operation on a semaphore.
|
|
|
|
*
|
|
|
|
* @param[in] sp pointer to a @p semaphore_t structure
|
|
|
|
* @return A message specifying how the invoking thread has been
|
|
|
|
* released from the semaphore.
|
2013-09-03 13:50:15 +00:00
|
|
|
* @retval CH_MSG_OK if the thread has not stopped on the semaphore or the
|
2013-09-01 08:19:24 +00:00
|
|
|
* semaphore has been signaled.
|
2013-09-03 13:50:15 +00:00
|
|
|
* @retval CH_MSG_RST if the semaphore has been reset using @p chSemReset().
|
2013-09-01 08:19:24 +00:00
|
|
|
*
|
|
|
|
* @sclass
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#define chSemWaitS(sp) chSemWaitTimeoutS(sp, TIME_INFINITE)
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-04 08:19:38 +00:00
|
|
|
/**
|
|
|
|
* @brief Returns the semaphore counter current value.
|
|
|
|
*
|
|
|
|
* @iclass
|
|
|
|
*/
|
|
|
|
#define chSemGetCounterI(sp) ((sp)->cnt)
|
|
|
|
|
2013-09-01 08:19:24 +00:00
|
|
|
/**
|
|
|
|
* @brief Current system time.
|
2013-09-03 13:50:15 +00:00
|
|
|
* @details Returns the number of system ticks since the @p chSysInit()
|
2013-09-01 08:19:24 +00:00
|
|
|
* invocation.
|
|
|
|
* @note The counter can reach its maximum and then restart from zero.
|
2013-09-04 10:26:42 +00:00
|
|
|
* @note This function can be called from any context but its atomicity
|
|
|
|
* is not guaranteed on architectures whose word size is less than
|
|
|
|
* @systime_t size.
|
2013-09-01 08:19:24 +00:00
|
|
|
*
|
|
|
|
* @return The system time in ticks.
|
|
|
|
*
|
2013-09-04 10:26:42 +00:00
|
|
|
* @xclass
|
2013-09-01 08:19:24 +00:00
|
|
|
*/
|
2013-09-11 12:00:01 +00:00
|
|
|
#if NIL_CFG_ST_TIMEDELTA == 0 || defined(__DOXYGEN__)
|
2013-09-04 10:26:42 +00:00
|
|
|
#define chVTGetSystemTimeX() (nil.systime)
|
2013-09-01 08:19:24 +00:00
|
|
|
#else
|
2013-09-04 10:26:42 +00:00
|
|
|
#define chVTGetSystemTimeX() port_timer_get_time()
|
2013-09-01 08:19:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Condition assertion.
|
2013-09-03 13:21:10 +00:00
|
|
|
* @details If the condition check fails then the kernel panics with a
|
|
|
|
* message and halts.
|
2013-09-01 08:19:24 +00:00
|
|
|
* @note The condition is tested only if the @p NIL_CFG_ENABLE_ASSERTS
|
2013-09-24 13:00:46 +00:00
|
|
|
* switch is specified in @p nilconf.h else the macro does nothing.
|
2013-09-01 08:19:24 +00:00
|
|
|
* @note The remark string is not currently used except for putting a
|
|
|
|
* comment in the code about the assertion.
|
|
|
|
*
|
|
|
|
* @param[in] c the condition to be verified to be true
|
|
|
|
* @param[in] r a remark string
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
#if !defined(chDbgAssert)
|
2013-09-24 13:00:46 +00:00
|
|
|
#define chDbgAssert(c, r) do { \
|
2013-09-23 19:16:06 +00:00
|
|
|
if (NIL_CFG_ENABLE_ASSERTS && !(c)) \
|
|
|
|
chSysHalt(__func__); \
|
2013-09-24 13:00:46 +00:00
|
|
|
} while (0)
|
2013-09-03 13:50:15 +00:00
|
|
|
#endif /* !defined(chDbgAssert) */
|
2013-09-01 08:19:24 +00:00
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* External declarations. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
#if !defined(__DOXYGEN__)
|
|
|
|
extern nil_system_t nil;
|
|
|
|
extern const thread_config_t nil_thd_configs[NIL_CFG_NUM_THREADS + 1];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2013-09-03 13:50:15 +00:00
|
|
|
void chSysInit(void);
|
|
|
|
void chSysHalt(const char *reason);
|
|
|
|
void chSysTimerHandlerI(void);
|
2013-09-04 10:26:42 +00:00
|
|
|
syssts_t chSysGetStatusAndLockX(void);
|
|
|
|
void chSysRestoreStatusX(syssts_t sts);
|
2013-09-05 12:33:42 +00:00
|
|
|
thread_t *chSchReadyI(thread_t *tp, msg_t msg);
|
2013-09-03 13:50:15 +00:00
|
|
|
void chSchRescheduleS(void);
|
2013-09-05 10:34:09 +00:00
|
|
|
msg_t chSchGoSleepTimeoutS(tstate_t newstate, systime_t timeout);
|
2013-09-04 08:19:38 +00:00
|
|
|
msg_t chThdSuspendTimeoutS(thread_reference_t *trp, systime_t timeout);
|
|
|
|
void chThdResumeI(thread_reference_t *trp, msg_t msg);
|
2013-09-03 13:50:15 +00:00
|
|
|
void chThdSleep(systime_t time);
|
|
|
|
void chThdSleepUntil(systime_t time);
|
2013-09-04 10:26:42 +00:00
|
|
|
bool chVTIsTimeWithinX(systime_t time, systime_t start, systime_t end);
|
2013-09-03 13:50:15 +00:00
|
|
|
msg_t chSemWaitTimeout(semaphore_t *sp, systime_t time);
|
|
|
|
msg_t chSemWaitTimeoutS(semaphore_t *sp, systime_t time);
|
|
|
|
void chSemSignal(semaphore_t *sp);
|
|
|
|
void chSemSignalI(semaphore_t *sp);
|
|
|
|
void chSemReset(semaphore_t *sp, cnt_t n);
|
|
|
|
void chSemResetI(semaphore_t *sp, cnt_t n);
|
2013-09-04 10:26:42 +00:00
|
|
|
void chEvtSignal(thread_t *tp, eventmask_t mask);
|
|
|
|
void chEvtSignalI(thread_t *tp, eventmask_t mask);
|
|
|
|
eventmask_t chEvtWaitAnyTimeout(eventmask_t mask, systime_t timeout);
|
2013-09-01 08:19:24 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* _NIL_H_ */
|
|
|
|
|
|
|
|
/** @} */
|