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.c 00029 * @brief Time and Virtual Timers related code. 00030 * 00031 * @addtogroup time 00032 * @details Time and Virtual Timers related APIs and services. 00033 * @{ 00034 */ 00035 00036 #include "ch.h" 00037 00038 /** 00039 * @brief Virtual timers delta list header. 00040 */ 00041 VTList vtlist; 00042 00043 /** 00044 * @brief Virtual Timers initialization. 00045 * @note Internal use only. 00046 */ 00047 void vt_init(void) { 00048 00049 vtlist.vt_next = vtlist.vt_prev = (void *)&vtlist; 00050 vtlist.vt_time = (systime_t)-1; 00051 vtlist.vt_systime = 0; 00052 } 00053 00054 /** 00055 * @brief Enables a virtual timer. 00056 * @note The associated function is invoked by an interrupt handler within 00057 * the I-Locked state, see @ref system_states. 00058 * 00059 * @param[out] vtp the @p VirtualTimer structure pointer 00060 * @param[in] time the number of time ticks, the value @p TIME_INFINITE 00061 * is notallowed. The value @p TIME_IMMEDIATE is allowed 00062 * but interpreted as a normal time specification not as 00063 * an immediate timeout specification. 00064 * @param[in] vtfunc the timer callback function. After invoking the 00065 * callback the timer is disabled and the structure can 00066 * be disposed or reused. 00067 * @param[in] par a parameter that will be passed to the callback 00068 * function 00069 */ 00070 void chVTSetI(VirtualTimer *vtp, systime_t time, vtfunc_t vtfunc, void *par) { 00071 VirtualTimer *p; 00072 00073 chDbgCheck((vtp != NULL) && (vtfunc != NULL) && (time != TIME_INFINITE), 00074 "chVTSetI"); 00075 00076 vtp->vt_par = par; 00077 vtp->vt_func = vtfunc; 00078 p = vtlist.vt_next; 00079 while (p->vt_time < time) { 00080 time -= p->vt_time; 00081 p = p->vt_next; 00082 } 00083 00084 vtp->vt_prev = (vtp->vt_next = p)->vt_prev; 00085 vtp->vt_prev->vt_next = p->vt_prev = vtp; 00086 vtp->vt_time = time; 00087 if (p != (void *)&vtlist) 00088 p->vt_time -= time; 00089 } 00090 00091 /** 00092 * @brief Disables a Virtual Timer. 00093 * @note The timer MUST be active when this function is invoked. 00094 * 00095 * @param[in] vtp the @p VirtualTimer structure pointer 00096 */ 00097 void chVTResetI(VirtualTimer *vtp) { 00098 00099 chDbgCheck(vtp != NULL, "chVTResetI"); 00100 chDbgAssert(vtp->vt_func != NULL, 00101 "chVTResetI(), #1", 00102 "timer not set or already triggered"); 00103 00104 if (vtp->vt_next != (void *)&vtlist) 00105 vtp->vt_next->vt_time += vtp->vt_time; 00106 vtp->vt_prev->vt_next = vtp->vt_next; 00107 vtp->vt_next->vt_prev = vtp->vt_prev; 00108 vtp->vt_func = (vtfunc_t)NULL; 00109 } 00110 00111 /** 00112 * @brief Checks if the current system time is within the specified time 00113 * window. 00114 * @note When start==end then the function returns always true because the 00115 * whole time range is specified. 00116 * 00117 * @param[in] start the start of the time window (inclusive) 00118 * @param[in] end the end of the time window (non inclusive) 00119 * @retval TRUE current time within the specified time window. 00120 * @retval FALSE current time not within the specified time window. 00121 */ 00122 bool_t chTimeIsWithin(systime_t start, systime_t end) { 00123 00124 systime_t time = chTimeNow(); 00125 return end > start ? (time >= start) && (time < end) : 00126 (time >= start) || (time < end); 00127 } 00128 00129 /** @} */