ChibiOS/RT Architecture - Reference Manual - Guides |
00001 /* 00002 ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio. 00003 00004 This file is part of ChibiOS/RT. 00005 00006 ChibiOS/RT is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 ChibiOS/RT is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 00019 --- 00020 00021 A special exception to the GPL can be applied should you wish to distribute 00022 a combined work that includes ChibiOS/RT, without being obliged to provide 00023 the source code for any proprietary components. See the file exception.txt 00024 for full details of how and when the exception can be applied. 00025 */ 00026 00027 /** 00028 * @file chvt.h 00029 * @brief Time macros and structures. 00030 * 00031 * @addtogroup time 00032 * @{ 00033 */ 00034 00035 #ifndef _CHVT_H_ 00036 #define _CHVT_H_ 00037 00038 /** 00039 * @brief Time conversion utility. 00040 * @details Converts from seconds to system ticks number. 00041 * @note The result is rounded upward to the next tick boundary. 00042 */ 00043 #define S2ST(sec) ((systime_t)((sec) * CH_FREQUENCY)) 00044 00045 /** 00046 * @brief Time conversion utility. 00047 * @details Converts from milliseconds to system ticks number. 00048 * @note The result is rounded upward to the next tick boundary. 00049 */ 00050 #define MS2ST(msec) ((systime_t)(((((msec) - 1L) * CH_FREQUENCY) / 1000L) + 1L)) 00051 00052 /** 00053 * @brief Time conversion utility. 00054 * @details Converts from microseconds to system ticks number. 00055 * @note The result is rounded upward to the next tick boundary. 00056 */ 00057 #define US2ST(usec) ((systime_t)(((((usec) - 1L) * CH_FREQUENCY) / 1000000L) + 1L)) 00058 00059 /** 00060 * @brief Virtual Timer callback function. 00061 */ 00062 typedef void (*vtfunc_t)(void *); 00063 00064 /** 00065 * @brief Virtual Timer structure type. 00066 */ 00067 typedef struct VirtualTimer VirtualTimer; 00068 00069 /** 00070 * @extends DeltaList 00071 * 00072 * @brief Virtual Timer descriptor structure. 00073 */ 00074 struct VirtualTimer { 00075 VirtualTimer *vt_next; /**< @brief Next timer in the delta 00076 list. */ 00077 VirtualTimer *vt_prev; /**< @brief Previous timer in the delta 00078 list. */ 00079 systime_t vt_time; /**< @brief Time delta before timeout. */ 00080 vtfunc_t vt_func; /**< @brief Timer callback function 00081 pointer. */ 00082 void *vt_par; /**< @brief Timer callback function 00083 parameter. */ 00084 }; 00085 00086 /** 00087 * @brief Virtual timers list header. 00088 * @note The delta list is implemented as a double link bidirectional list 00089 * in order to make the unlink time constant, the reset of a virtual 00090 * timer is often used in the code. 00091 */ 00092 typedef struct { 00093 VirtualTimer *vt_next; /**< @brief Next timer in the delta 00094 list. */ 00095 VirtualTimer *vt_prev; /**< @brief Last timer in the delta 00096 list. */ 00097 systime_t vt_time; /**< @brief Must be initialized to -1. */ 00098 volatile systime_t vt_systime; /**< @brief System Time counter. */ 00099 } VTList; 00100 00101 extern VTList vtlist; 00102 00103 /** 00104 * @brief Virtual timers ticker. 00105 */ 00106 #define chVTDoTickI() { \ 00107 vtlist.vt_systime++; \ 00108 if (&vtlist != (VTList *)vtlist.vt_next) { \ 00109 VirtualTimer *vtp; \ 00110 \ 00111 --vtlist.vt_next->vt_time; \ 00112 while (!(vtp = vtlist.vt_next)->vt_time) { \ 00113 vtfunc_t fn = vtp->vt_func; \ 00114 vtp->vt_func = (vtfunc_t)NULL; \ 00115 vtp->vt_next->vt_prev = (void *)&vtlist; \ 00116 (&vtlist)->vt_next = vtp->vt_next; \ 00117 fn(vtp->vt_par); \ 00118 } \ 00119 } \ 00120 } 00121 00122 /* 00123 * Virtual Timers APIs. 00124 */ 00125 #ifdef __cplusplus 00126 extern "C" { 00127 #endif 00128 void vt_init(void); 00129 void chVTSetI(VirtualTimer *vtp, systime_t time, vtfunc_t vtfunc, void *par); 00130 void chVTResetI(VirtualTimer *vtp); 00131 bool_t chTimeIsWithin(systime_t start, systime_t end); 00132 #ifdef __cplusplus 00133 } 00134 #endif 00135 00136 /** 00137 * @brief Returns TRUE if the speciified timer is armed. 00138 */ 00139 #define chVTIsArmedI(vtp) ((vtp)->vt_func != NULL) 00140 00141 /** 00142 * @brief Current system time. 00143 * @details Returns the number of system ticks since the @p chSysInit() 00144 * invocation. 00145 * @note The counter can reach its maximum and then restart from zero. 00146 * @note This function is designed to work with the @p chThdSleepUntil(). 00147 * 00148 * @return The system time in ticks.r 00149 */ 00150 #define chTimeNow() (vtlist.vt_systime) 00151 00152 #endif /* _CHVT_H_ */ 00153 00154 /** @} */