2009-08-16 13:07:24 +00:00
|
|
|
/*
|
2011-03-18 18:38:08 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
2014-07-26 09:06:28 +00:00
|
|
|
2011,2012,2013,2014 Giovanni Di Sirio.
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT 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.
|
|
|
|
|
|
|
|
ChibiOS/RT 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-02-27 08:54:22 +00:00
|
|
|
* @file chsys.h
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief System related macros and structures.
|
|
|
|
*
|
2009-08-30 06:59:43 +00:00
|
|
|
* @addtogroup system
|
2009-08-16 13:07:24 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2010-02-27 08:54:22 +00:00
|
|
|
#ifndef _CHSYS_H_
|
|
|
|
#define _CHSYS_H_
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-07-19 14:51:35 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module constants. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module pre-compile time settings. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Derived constants and error checks. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module data structures and types. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module macros. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2011-08-23 10:09:08 +00:00
|
|
|
/**
|
2013-07-19 14:51:35 +00:00
|
|
|
* @name ISRs abstraction macros
|
2011-08-23 10:09:08 +00:00
|
|
|
*/
|
2010-08-20 07:15:55 +00:00
|
|
|
/**
|
2013-07-19 14:51:35 +00:00
|
|
|
* @brief IRQ handler enter code.
|
|
|
|
* @note Usually IRQ handlers functions are also declared naked.
|
|
|
|
* @note On some architectures this macro can be empty.
|
2010-08-20 07:15:55 +00:00
|
|
|
*
|
2013-07-19 14:51:35 +00:00
|
|
|
* @special
|
|
|
|
*/
|
|
|
|
#define CH_IRQ_PROLOGUE() \
|
|
|
|
PORT_IRQ_PROLOGUE(); \
|
2013-07-30 09:19:07 +00:00
|
|
|
_stats_increase_irq(); \
|
2013-08-10 10:17:45 +00:00
|
|
|
_dbg_check_enter_isr()
|
2013-07-19 14:51:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief IRQ handler exit code.
|
|
|
|
* @note Usually IRQ handlers function are also declared naked.
|
|
|
|
* @note This macro usually performs the final reschedule by using
|
|
|
|
* @p chSchIsPreemptionRequired() and @p chSchDoReschedule().
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
2013-07-19 14:51:35 +00:00
|
|
|
* @special
|
2010-08-20 07:15:55 +00:00
|
|
|
*/
|
2013-07-19 14:51:35 +00:00
|
|
|
#define CH_IRQ_EPILOGUE() \
|
2013-08-10 10:17:45 +00:00
|
|
|
_dbg_check_leave_isr(); \
|
2013-07-29 13:28:35 +00:00
|
|
|
PORT_IRQ_EPILOGUE()
|
2013-07-19 14:51:35 +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
|
|
|
|
*/
|
|
|
|
#define CH_IRQ_HANDLER(id) PORT_IRQ_HANDLER(id)
|
|
|
|
/** @} */
|
2010-08-20 07:15:55 +00:00
|
|
|
|
2009-08-16 13:07:24 +00:00
|
|
|
/**
|
2013-07-19 14:51:35 +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.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @special
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 14:51:35 +00:00
|
|
|
#define CH_FAST_IRQ_HANDLER(id) PORT_FAST_IRQ_HANDLER(id)
|
|
|
|
/** @} */
|
|
|
|
|
2013-07-29 14:31:13 +00:00
|
|
|
/**
|
|
|
|
* @name Time conversion utilities for the realtime counter
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @brief Seconds to realtime counter.
|
|
|
|
* @details Converts from seconds to realtime counter cycles.
|
2014-01-20 09:38:35 +00:00
|
|
|
* @note The macro assumes that @p freq >= @p 1.
|
2013-07-29 14:31:13 +00:00
|
|
|
*
|
2014-01-20 09:38:35 +00:00
|
|
|
* @param[in] freq clock frequency, in Hz, of the realtime counter
|
2013-07-29 14:31:13 +00:00
|
|
|
* @param[in] sec number of seconds
|
|
|
|
* @return The number of cycles.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2014-01-20 09:38:35 +00:00
|
|
|
#define S2RTC(freq, sec) ((freq) * (sec))
|
2013-07-29 14:31:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Milliseconds to realtime counter.
|
|
|
|
* @details Converts from milliseconds to realtime counter cycles.
|
2014-01-20 09:38:35 +00:00
|
|
|
* @note The result is rounded upward to the next millisecond boundary.
|
|
|
|
* @note The macro assumes that @p freq >= @p 1000.
|
2013-07-29 14:31:13 +00:00
|
|
|
*
|
2014-01-20 09:38:35 +00:00
|
|
|
* @param[in] freq clock frequency, in Hz, of the realtime counter
|
2013-07-29 14:31:13 +00:00
|
|
|
* @param[in] msec number of milliseconds
|
|
|
|
* @return The number of cycles.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2014-01-20 09:38:35 +00:00
|
|
|
#define MS2RTC(freq, msec) (rtcnt_t)((((freq) + 999UL) / 1000UL) * (msec))
|
2013-07-29 14:31:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Microseconds to realtime counter.
|
|
|
|
* @details Converts from microseconds to realtime counter cycles.
|
2014-01-20 09:38:35 +00:00
|
|
|
* @note The result is rounded upward to the next microsecond boundary.
|
|
|
|
* @note The macro assumes that @p freq >= @p 1000000.
|
2013-07-29 14:31:13 +00:00
|
|
|
*
|
2014-01-20 09:38:35 +00:00
|
|
|
* @param[in] freq clock frequency, in Hz, of the realtime counter
|
2013-07-29 14:31:13 +00:00
|
|
|
* @param[in] usec number of microseconds
|
|
|
|
* @return The number of cycles.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2014-01-20 09:38:35 +00:00
|
|
|
#define US2RTC(freq, usec) (rtcnt_t)((((freq) + 999999UL) / 1000000UL) * (usec))
|
2013-07-29 14:31:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Realtime counter cycles to seconds.
|
|
|
|
* @details Converts from realtime counter cycles number to seconds.
|
2013-11-29 13:24:08 +00:00
|
|
|
* @note The result is rounded up to the next second boundary.
|
2014-01-20 09:38:35 +00:00
|
|
|
* @note The macro assumes that @p freq >= @p 1.
|
2013-07-29 14:31:13 +00:00
|
|
|
*
|
2014-01-20 09:38:35 +00:00
|
|
|
* @param[in] freq clock frequency, in Hz, of the realtime counter
|
2013-07-29 14:31:13 +00:00
|
|
|
* @param[in] n number of cycles
|
|
|
|
* @return The number of seconds.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2014-01-20 09:38:35 +00:00
|
|
|
#define RTC2S(freq, n) ((((n) - 1UL) / (freq)) + 1UL)
|
2013-07-29 14:31:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Realtime counter cycles to milliseconds.
|
|
|
|
* @details Converts from realtime counter cycles number to milliseconds.
|
2013-11-29 13:24:08 +00:00
|
|
|
* @note The result is rounded up to the next millisecond boundary.
|
2014-01-20 09:38:35 +00:00
|
|
|
* @note The macro assumes that @p freq >= @p 1000.
|
2013-07-29 14:31:13 +00:00
|
|
|
*
|
2014-01-20 09:38:35 +00:00
|
|
|
* @param[in] freq clock frequency, in Hz, of the realtime counter
|
2013-07-29 14:31:13 +00:00
|
|
|
* @param[in] n number of cycles
|
|
|
|
* @return The number of milliseconds.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2014-01-20 09:38:35 +00:00
|
|
|
#define RTC2MS(freq, n) ((((n) - 1UL) / ((freq) / 1000UL)) + 1UL)
|
2013-07-29 14:31:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Realtime counter cycles to microseconds.
|
|
|
|
* @details Converts from realtime counter cycles number to microseconds.
|
2013-11-29 13:24:08 +00:00
|
|
|
* @note The result is rounded up to the next microsecond boundary.
|
2014-01-20 09:38:35 +00:00
|
|
|
* @note The macro assumes that @p freq >= @p 1000000.
|
2013-07-29 14:31:13 +00:00
|
|
|
*
|
2014-01-20 09:38:35 +00:00
|
|
|
* @param[in] freq clock frequency, in Hz, of the realtime counter
|
2013-07-29 14:31:13 +00:00
|
|
|
* @param[in] n number of cycles
|
|
|
|
* @return The number of microseconds.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2014-01-20 09:38:35 +00:00
|
|
|
#define RTC2US(freq, n) ((((n) - 1UL) / ((freq) / 1000000UL)) + 1UL)
|
2013-07-29 14:31:13 +00:00
|
|
|
/** @} */
|
|
|
|
|
2013-07-29 13:28:35 +00:00
|
|
|
/**
|
|
|
|
* @brief Returns the current value of the system real time counter.
|
2013-07-29 15:13:52 +00:00
|
|
|
* @note This function is only available if the port layer supports the
|
2013-09-03 09:51:32 +00:00
|
|
|
* option @p PORT_SUPPORTS_RT.
|
2013-07-29 13:28:35 +00:00
|
|
|
*
|
|
|
|
* @return The value of the system realtime counter of
|
2013-07-29 15:13:52 +00:00
|
|
|
* type rtcnt_t.
|
2013-07-29 13:28:35 +00:00
|
|
|
*
|
2013-07-29 14:31:13 +00:00
|
|
|
* @xclass
|
2013-07-29 13:28:35 +00:00
|
|
|
*/
|
2013-09-03 09:51:32 +00:00
|
|
|
#if PORT_SUPPORTS_RT || defined(__DOXYGEN__)
|
2013-07-29 13:28:35 +00:00
|
|
|
#define chSysGetRealtimeCounterX() (rtcnt_t)port_rt_get_counter_value()
|
|
|
|
#endif
|
|
|
|
|
2013-07-30 14:23:37 +00:00
|
|
|
/**
|
|
|
|
* @brief Performs a context switch.
|
|
|
|
* @note Not a user function, it is meant to be invoked by the scheduler
|
|
|
|
* itself or from within the port layer.
|
|
|
|
*
|
|
|
|
* @param[in] ntp the thread to be switched in
|
|
|
|
* @param[in] otp the thread to be switched out
|
|
|
|
*
|
|
|
|
* @special
|
|
|
|
*/
|
|
|
|
#define chSysSwitch(ntp, otp) { \
|
|
|
|
\
|
2013-08-10 10:17:45 +00:00
|
|
|
_dbg_trace(otp); \
|
2013-07-31 12:26:02 +00:00
|
|
|
_stats_ctxswc(ntp, otp); \
|
2013-07-30 14:23:37 +00:00
|
|
|
CH_CFG_CONTEXT_SWITCH_HOOK(ntp, otp); \
|
|
|
|
port_switch(ntp, otp); \
|
|
|
|
}
|
|
|
|
|
2013-07-19 14:51:35 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* External declarations. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
void chSysInit(void);
|
2013-08-17 11:52:50 +00:00
|
|
|
void chSysHalt(const char *reason);
|
2013-07-19 14:51:35 +00:00
|
|
|
void chSysTimerHandlerI(void);
|
2013-08-26 13:07:19 +00:00
|
|
|
syssts_t chSysGetStatusAndLockX(void);
|
2013-08-26 14:08:25 +00:00
|
|
|
void chSysRestoreStatusX(syssts_t sts);
|
2013-09-03 09:51:32 +00:00
|
|
|
#if PORT_SUPPORTS_RT
|
2013-07-29 15:13:52 +00:00
|
|
|
bool chSysIsCounterWithinX(rtcnt_t cnt, rtcnt_t start, rtcnt_t end);
|
|
|
|
void chSysPolledDelayX(rtcnt_t cycles);
|
|
|
|
#endif
|
2013-07-19 14:51:35 +00:00
|
|
|
#ifdef __cplusplus
|
2010-08-21 08:38:14 +00:00
|
|
|
}
|
|
|
|
#endif
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-07-19 14:51:35 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module inline functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2009-08-16 13:07:24 +00:00
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Raises the system interrupt priority mask to the maximum level.
|
2009-08-16 13:07:24 +00:00
|
|
|
* @details All the maskable interrupt sources are disabled regardless their
|
2010-02-06 12:31:09 +00:00
|
|
|
* hardware priority.
|
|
|
|
* @note Do not invoke this API from within a kernel lock.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @special
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 14:51:35 +00:00
|
|
|
static inline void chSysDisable(void) {
|
|
|
|
|
|
|
|
port_disable();
|
2013-08-10 10:17:45 +00:00
|
|
|
_dbg_check_disable();
|
2011-08-11 17:51:37 +00:00
|
|
|
}
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Raises the system interrupt priority mask to system level.
|
2009-08-16 13:07:24 +00:00
|
|
|
* @details The interrupt sources that should not be able to preempt the kernel
|
2010-02-06 12:31:09 +00:00
|
|
|
* are disabled, interrupt sources with higher priority are still
|
|
|
|
* enabled.
|
|
|
|
* @note Do not invoke this API from within a kernel lock.
|
|
|
|
* @note This API is no replacement for @p chSysLock(), the @p chSysLock()
|
|
|
|
* could do more than just disable the interrupts.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @special
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 14:51:35 +00:00
|
|
|
static inline void chSysSuspend(void) {
|
|
|
|
|
|
|
|
port_suspend();
|
2013-08-10 10:17:45 +00:00
|
|
|
_dbg_check_suspend();
|
2011-08-11 17:51:37 +00:00
|
|
|
}
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Lowers the system interrupt priority mask to user level.
|
2009-08-16 13:07:24 +00:00
|
|
|
* @details All the interrupt sources are enabled.
|
2010-02-06 12:31:09 +00:00
|
|
|
* @note Do not invoke this API from within a kernel lock.
|
|
|
|
* @note This API is no replacement for @p chSysUnlock(), the
|
|
|
|
* @p chSysUnlock() could do more than just enable the interrupts.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @special
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 14:51:35 +00:00
|
|
|
static inline void chSysEnable(void) {
|
|
|
|
|
2013-08-10 10:17:45 +00:00
|
|
|
_dbg_check_enable();
|
2013-07-19 14:51:35 +00:00
|
|
|
port_enable();
|
2011-08-11 17:51:37 +00:00
|
|
|
}
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Enters the kernel lock mode.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @special
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 14:51:35 +00:00
|
|
|
static inline void chSysLock(void) {
|
|
|
|
|
|
|
|
port_lock();
|
2013-07-30 09:19:07 +00:00
|
|
|
_stats_start_measure_crit_thd();
|
2013-08-10 10:17:45 +00:00
|
|
|
_dbg_check_lock();
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Leaves the kernel lock mode.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @special
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 14:51:35 +00:00
|
|
|
static inline void chSysUnlock(void) {
|
|
|
|
|
2013-08-10 10:17:45 +00:00
|
|
|
_dbg_check_unlock();
|
2013-07-30 09:19:07 +00:00
|
|
|
_stats_stop_measure_crit_thd();
|
2013-11-12 11:05:57 +00:00
|
|
|
|
|
|
|
/* The following condition can be triggered by the use of i-class functions
|
|
|
|
in a critical section not followed by a chSchResceduleS(), this means
|
|
|
|
that the current thread has a lower priority than the next thread in
|
|
|
|
the ready list.*/
|
|
|
|
chDbgAssert(ch.rlist.r_current->p_prio >= ch.rlist.r_queue.p_next->p_prio,
|
|
|
|
"priority violation, missing reschedule");
|
|
|
|
|
2013-07-19 14:51:35 +00:00
|
|
|
port_unlock();
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +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.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @special
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-24 14:54:26 +00:00
|
|
|
static inline void chSysLockFromISR(void) {
|
2013-07-19 14:51:35 +00:00
|
|
|
|
|
|
|
port_lock_from_isr();
|
2013-07-30 09:19:07 +00:00
|
|
|
_stats_start_measure_crit_isr();
|
2013-08-10 10:17:45 +00:00
|
|
|
_dbg_check_lock_from_isr();
|
2011-08-11 17:51:37 +00:00
|
|
|
}
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Leaves the kernel lock mode from within an interrupt handler.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2010-02-06 12:31:09 +00:00
|
|
|
* @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.
|
2010-09-21 10:22:06 +00:00
|
|
|
* @note This API must be invoked exclusively from interrupt handlers.
|
|
|
|
*
|
|
|
|
* @special
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-24 14:54:26 +00:00
|
|
|
static inline void chSysUnlockFromISR(void) {
|
2010-04-26 12:34:10 +00:00
|
|
|
|
2013-08-10 10:17:45 +00:00
|
|
|
_dbg_check_unlock_from_isr();
|
2013-07-30 09:19:07 +00:00
|
|
|
_stats_stop_measure_crit_isr();
|
2013-07-19 14:51:35 +00:00
|
|
|
port_unlock_from_isr();
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2010-02-27 08:54:22 +00:00
|
|
|
#endif /* _CHSYS_H_ */
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/** @} */
|