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 * @file ch.cpp 00028 * @brief C++ wrapper code. 00029 * @addtogroup cpp_library 00030 * @{ 00031 */ 00032 00033 #include "ch.hpp" 00034 00035 namespace chibios_rt { 00036 00037 /*------------------------------------------------------------------------* 00038 * chibios_rt::System * 00039 *------------------------------------------------------------------------*/ 00040 void System::Init(void) { 00041 00042 chSysInit(); 00043 } 00044 00045 void System::Lock(void) { 00046 00047 chSysLock(); 00048 } 00049 00050 void System::Unlock(void) { 00051 00052 chSysUnlock(); 00053 } 00054 00055 systime_t System::GetTime(void) { 00056 00057 return chTimeNow(); 00058 } 00059 00060 /*------------------------------------------------------------------------* 00061 * chibios_rt::Timer * 00062 *------------------------------------------------------------------------*/ 00063 void Timer::Set(systime_t time, vtfunc_t vtfunc, void *par) { 00064 00065 chVTSetI(&timer, time, vtfunc, par); 00066 } 00067 00068 void Timer::Reset() { 00069 00070 chVTResetI(&timer); 00071 } 00072 00073 bool Timer::IsArmed(void) { 00074 00075 return chVTIsArmedI(&timer); 00076 } 00077 00078 /*------------------------------------------------------------------------* 00079 * chibios_rt::BaseThread * 00080 *------------------------------------------------------------------------*/ 00081 static msg_t thdstart(void *arg) { 00082 00083 return ((BaseThread *)arg)->Main(); 00084 } 00085 00086 BaseThread::BaseThread(void *workspace, size_t wsize, tprio_t prio) { 00087 00088 thread_ref = chThdCreateStatic(workspace, wsize, prio, thdstart, this); 00089 } 00090 00091 void BaseThread::Exit(msg_t msg) { 00092 00093 chThdExit(msg); 00094 } 00095 00096 #if CH_USE_WAITEXIT 00097 msg_t BaseThread::Wait(void) { 00098 00099 return chThdWait(thread_ref); 00100 } 00101 #endif /* CH_USE_WAITEXIT */ 00102 00103 void BaseThread::SetPriority(tprio_t newprio) { 00104 00105 chThdSetPriority(newprio); 00106 } 00107 00108 void BaseThread::Resume(void) { 00109 00110 chThdResume(thread_ref); 00111 } 00112 00113 void BaseThread::Terminate(void) { 00114 00115 chThdTerminate(thread_ref); 00116 } 00117 00118 void BaseThread::Sleep(systime_t n) { 00119 00120 chThdSleep(n); 00121 } 00122 00123 void BaseThread::SleepUntil(systime_t time) { 00124 00125 chThdSleepUntil(time); 00126 } 00127 00128 #if CH_USE_MESSAGES 00129 msg_t BaseThread::SendMessage(::Thread* tp, msg_t msg) { 00130 00131 return chMsgSend(tp, msg); 00132 } 00133 00134 msg_t BaseThread::SendMessage(msg_t msg) { 00135 00136 return chMsgSend(thread_ref, msg); 00137 } 00138 00139 msg_t BaseThread::WaitMessage(void) { 00140 00141 return chMsgWait(); 00142 } 00143 00144 msg_t BaseThread::GetMessage(void) { 00145 00146 return chMsgGet(); 00147 } 00148 00149 void BaseThread::ReleaseMessage(msg_t msg) { 00150 00151 chMsgRelease(msg); 00152 } 00153 00154 bool BaseThread::IsPendingMessage(void) { 00155 00156 return chMsgIsPendingI(currp); 00157 } 00158 #endif /* CH_USE_MESSAGES */ 00159 00160 msg_t BaseThread::Main(void) { 00161 00162 return 0; 00163 } 00164 00165 #if CH_USE_SEMAPHORES 00166 /*------------------------------------------------------------------------* 00167 * chibios_rt::Semaphore * 00168 *------------------------------------------------------------------------*/ 00169 Semaphore::Semaphore(cnt_t n) { 00170 00171 chSemInit(&sem, n); 00172 } 00173 00174 void Semaphore::Reset(cnt_t n) { 00175 00176 chSemReset(&sem, n); 00177 } 00178 00179 msg_t Semaphore::Wait(void) { 00180 00181 return chSemWait(&sem); 00182 } 00183 00184 msg_t Semaphore::WaitTimeout(systime_t time) { 00185 00186 return chSemWaitTimeout(&sem, time); 00187 } 00188 00189 void Semaphore::Signal(void) { 00190 00191 chSemSignal(&sem); 00192 } 00193 00194 #if CH_USE_SEMSW 00195 msg_t Semaphore::SignalWait(Semaphore *ssem, Semaphore *wsem) { 00196 00197 return chSemSignalWait(&ssem->sem, &wsem->sem); 00198 } 00199 #endif /* CH_USE_SEMSW */ 00200 #endif /* CH_USE_SEMAPHORES */ 00201 00202 #if CH_USE_MUTEXES 00203 /*------------------------------------------------------------------------* 00204 * chibios_rt::Mutex * 00205 *------------------------------------------------------------------------*/ 00206 Mutex::Mutex(void) { 00207 00208 chMtxInit(&mutex); 00209 } 00210 00211 bool Mutex::TryLock(void) { 00212 00213 return chMtxTryLock(&mutex); 00214 } 00215 00216 void Mutex::Lock(void) { 00217 00218 chMtxLock(&mutex); 00219 } 00220 00221 void Mutex::Unlock(void) { 00222 00223 chMtxUnlock(); 00224 } 00225 00226 void UnlockAll(void) { 00227 00228 chMtxUnlockAll(); 00229 } 00230 00231 #if CH_USE_CONDVARS 00232 /*------------------------------------------------------------------------* 00233 * chibios_rt::CondVar * 00234 *------------------------------------------------------------------------*/ 00235 CondVar::CondVar(void) { 00236 00237 chCondInit(&condvar); 00238 } 00239 00240 void CondVar::Signal(void) { 00241 00242 chCondSignal(&condvar); 00243 } 00244 00245 void CondVar::Broadcast(void) { 00246 00247 chCondBroadcast(&condvar); 00248 } 00249 00250 msg_t CondVar::Wait(void) { 00251 00252 return chCondWait(&condvar); 00253 } 00254 00255 #if CH_USE_CONDVARS_TIMEOUT 00256 msg_t CondVar::WaitTimeout(systime_t time) { 00257 00258 return chCondWaitTimeout(&condvar, time); 00259 } 00260 #endif /* CH_USE_CONDVARS_TIMEOUT */ 00261 #endif /* CH_USE_CONDVARS */ 00262 #endif /* CH_USE_MUTEXES */ 00263 00264 #if CH_USE_EVENTS 00265 /*------------------------------------------------------------------------* 00266 * chibios_rt::Event * 00267 *------------------------------------------------------------------------*/ 00268 Event::Event(void) { 00269 00270 chEvtInit(&event); 00271 } 00272 00273 void Event::Register(EventListener *elp, eventid_t eid) { 00274 00275 chEvtRegister(&event,elp, eid); 00276 } 00277 00278 void Event::RegisterMask(EventListener *elp, eventmask_t emask) { 00279 00280 chEvtRegisterMask(&event,elp, emask); 00281 } 00282 00283 void Event::Unregister(EventListener *elp) { 00284 00285 chEvtUnregister(&event, elp); 00286 } 00287 00288 void Event::Broadcast(void) { 00289 00290 chEvtBroadcast(&event); 00291 } 00292 00293 eventmask_t Event::Clear(eventmask_t mask) { 00294 00295 return chEvtClear(mask); 00296 } 00297 00298 eventmask_t Event::Pend(eventmask_t mask) { 00299 00300 return chEvtPend(mask); 00301 } 00302 00303 void Event::Dispatch(const evhandler_t handlers[], eventmask_t mask) { 00304 00305 chEvtDispatch(handlers, mask); 00306 } 00307 00308 eventmask_t Event::WaitOne(eventmask_t ewmask) { 00309 00310 return chEvtWaitOne(ewmask); 00311 } 00312 00313 eventmask_t Event::WaitAny(eventmask_t ewmask) { 00314 00315 return chEvtWaitAny(ewmask); 00316 } 00317 00318 eventmask_t Event::WaitAll(eventmask_t ewmask) { 00319 00320 return chEvtWaitAll(ewmask); 00321 } 00322 00323 #if CH_USE_EVENTS_TIMEOUT 00324 eventmask_t Event::WaitOneTimeout(eventmask_t ewmask, systime_t time) { 00325 00326 return chEvtWaitOneTimeout(ewmask, time); 00327 } 00328 00329 eventmask_t Event::WaitAnyTimeout(eventmask_t ewmask, systime_t time) { 00330 00331 return chEvtWaitAnyTimeout(ewmask, time); 00332 } 00333 00334 eventmask_t Event::WaitAllTimeout(eventmask_t ewmask, systime_t time) { 00335 00336 return chEvtWaitAllTimeout(ewmask, time); 00337 } 00338 #endif /* CH_USE_EVENTS_TIMEOUT */ 00339 #endif /* CH_USE_EVENTS */ 00340 } 00341 00342 /** @} */