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,
|
2013-02-02 10:58:09 +00:00
|
|
|
2011,2012,2013 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-10-09 10:17:11 +00:00
|
|
|
* @file ch.cpp
|
|
|
|
* @brief C++ wrapper code.
|
|
|
|
*
|
2009-08-30 07:17:22 +00:00
|
|
|
* @addtogroup cpp_library
|
2009-08-16 13:07:24 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2009-12-08 17:37:49 +00:00
|
|
|
#include "ch.hpp"
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
namespace chibios_rt {
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*
|
|
|
|
* chibios_rt::System *
|
|
|
|
*------------------------------------------------------------------------*/
|
2013-01-01 15:42:44 +00:00
|
|
|
void System::init(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chSysInit();
|
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
void System::lock(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chSysLock();
|
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
void System::unlock(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
2013-01-06 09:10:58 +00:00
|
|
|
void System::lockFromIsr(void) {
|
|
|
|
|
|
|
|
chSysLockFromIsr();
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::unlockFromIsr(void) {
|
|
|
|
|
|
|
|
chSysUnlockFromIsr();
|
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
systime_t System::getTime(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
return chTimeNow();
|
|
|
|
}
|
|
|
|
|
2013-01-03 09:52:04 +00:00
|
|
|
bool System::isTimeWithin(systime_t start, systime_t end) {
|
2013-01-02 14:01:11 +00:00
|
|
|
|
|
|
|
return (bool)chTimeIsWithin(start, end);
|
|
|
|
}
|
|
|
|
|
2013-01-03 09:52:04 +00:00
|
|
|
/*------------------------------------------------------------------------*
|
|
|
|
* chibios_rt::Core *
|
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
void *Core::alloc(size_t size) {
|
|
|
|
|
|
|
|
return chCoreAlloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *Core::allocI(size_t size) {
|
|
|
|
|
|
|
|
return chCoreAllocI(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Core::getStatus(void) {
|
|
|
|
|
|
|
|
return chCoreStatus();
|
|
|
|
}
|
|
|
|
|
2009-08-16 13:07:24 +00:00
|
|
|
/*------------------------------------------------------------------------*
|
|
|
|
* chibios_rt::Timer *
|
|
|
|
*------------------------------------------------------------------------*/
|
2013-01-01 15:42:44 +00:00
|
|
|
void Timer::setI(systime_t time, vtfunc_t vtfunc, void *par) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
chVTSetI(&timer_ref, time, vtfunc, par);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
void Timer::resetI() {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
if (chVTIsArmedI(&timer_ref))
|
|
|
|
chVTResetI(&timer_ref);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
bool Timer::isArmedI(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
return (bool)chVTIsArmedI(&timer_ref);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*
|
2013-01-01 15:42:44 +00:00
|
|
|
* chibios_rt::ThreadReference *
|
2009-08-16 13:07:24 +00:00
|
|
|
*------------------------------------------------------------------------*/
|
2013-01-04 10:26:22 +00:00
|
|
|
|
|
|
|
void ThreadReference::stop(void) {
|
|
|
|
|
|
|
|
chDbgPanic("invoked unimplemented method stop()");
|
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
msg_t ThreadReference::suspend(void) {
|
|
|
|
msg_t msg;
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
chSysLock();
|
|
|
|
|
|
|
|
chDbgAssert(thread_ref != NULL,
|
|
|
|
"ThreadReference, #1",
|
|
|
|
"already referenced");
|
|
|
|
|
|
|
|
thread_ref = chThdSelf();
|
|
|
|
chSchGoSleepS(THD_STATE_SUSPENDED);
|
|
|
|
msg = thread_ref->p_u.rdymsg;
|
|
|
|
|
|
|
|
chSysUnlock();
|
|
|
|
return msg;
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
msg_t ThreadReference::suspendS(void) {
|
|
|
|
|
|
|
|
chDbgAssert(thread_ref == NULL,
|
|
|
|
"ThreadReference, #2",
|
|
|
|
"already referenced");
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
thread_ref = chThdSelf();
|
|
|
|
chSchGoSleepS(THD_STATE_SUSPENDED);
|
|
|
|
return thread_ref->p_u.rdymsg;
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
void ThreadReference::resume(msg_t msg) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
chSysLock()
|
|
|
|
|
|
|
|
chDbgAssert(thread_ref != NULL,
|
|
|
|
"ThreadReference, #3",
|
|
|
|
"not referenced");
|
|
|
|
|
|
|
|
if (thread_ref) {
|
|
|
|
Thread *tp = thread_ref;
|
|
|
|
thread_ref = NULL;
|
|
|
|
chSchWakeupS(tp, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
chSysUnlock();
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
void ThreadReference::resumeI(msg_t msg) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
chDbgAssert(thread_ref != NULL,
|
|
|
|
"ThreadReference, #4",
|
|
|
|
"not referenced");
|
|
|
|
|
|
|
|
if (thread_ref) {
|
|
|
|
Thread *tp = thread_ref;
|
|
|
|
thread_ref = NULL;
|
|
|
|
tp->p_msg = msg;
|
|
|
|
chSchReadyI(tp);
|
|
|
|
}
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
void ThreadReference::requestTerminate(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
chDbgAssert(thread_ref != NULL,
|
|
|
|
"ThreadReference, #5",
|
|
|
|
"not referenced");
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
chThdTerminate(thread_ref);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-03 09:52:04 +00:00
|
|
|
#if CH_USE_WAITEXIT
|
2013-01-01 15:42:44 +00:00
|
|
|
msg_t ThreadReference::wait(void) {
|
|
|
|
|
|
|
|
chDbgAssert(thread_ref != NULL,
|
2013-01-04 10:26:22 +00:00
|
|
|
"ThreadReference, #6",
|
2013-01-01 15:42:44 +00:00
|
|
|
"not referenced");
|
|
|
|
|
|
|
|
msg_t msg = chThdWait(thread_ref);
|
|
|
|
thread_ref = NULL;
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
#endif /* CH_USE_WAITEXIT */
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-03 09:52:04 +00:00
|
|
|
#if CH_USE_MESSAGES
|
2013-01-01 15:42:44 +00:00
|
|
|
msg_t ThreadReference::sendMessage(msg_t msg) {
|
|
|
|
|
|
|
|
chDbgAssert(thread_ref != NULL,
|
2013-01-04 10:26:22 +00:00
|
|
|
"ThreadReference, #7",
|
2013-01-01 15:42:44 +00:00
|
|
|
"not referenced");
|
|
|
|
|
|
|
|
return chMsgSend(thread_ref, msg);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
bool ThreadReference::isPendingMessage(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
chDbgAssert(thread_ref != NULL,
|
|
|
|
"ThreadReference, #7",
|
|
|
|
"not referenced");
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
return (bool)chMsgIsPendingI(thread_ref);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
2013-01-03 16:17:42 +00:00
|
|
|
|
|
|
|
msg_t ThreadReference::getMessage(void) {
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
chDbgAssert(thread_ref != NULL,
|
|
|
|
"ThreadReference, #8",
|
|
|
|
"not referenced");
|
|
|
|
|
2013-01-03 16:17:42 +00:00
|
|
|
return chMsgGet(thread_ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadReference::releaseMessage(msg_t msg) {
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
chDbgAssert(thread_ref != NULL,
|
|
|
|
"ThreadReference, #9",
|
|
|
|
"not referenced");
|
|
|
|
|
2013-01-03 16:17:42 +00:00
|
|
|
chMsgRelease(thread_ref, msg);
|
|
|
|
}
|
2013-01-01 15:42:44 +00:00
|
|
|
#endif /* CH_USE_MESSAGES */
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-02 10:11:02 +00:00
|
|
|
#if CH_USE_EVENTS
|
|
|
|
void ThreadReference::signalEvents(eventmask_t mask) {
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
chDbgAssert(thread_ref != NULL,
|
|
|
|
"ThreadReference, #10",
|
|
|
|
"not referenced");
|
|
|
|
|
2013-01-02 10:11:02 +00:00
|
|
|
chEvtSignal(thread_ref, mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadReference::signalEventsI(eventmask_t mask) {
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
chDbgAssert(thread_ref != NULL,
|
|
|
|
"ThreadReference, #11",
|
|
|
|
"not referenced");
|
|
|
|
|
2013-01-02 10:11:02 +00:00
|
|
|
chEvtSignalI(thread_ref, mask);
|
|
|
|
}
|
|
|
|
#endif /* CH_USE_EVENTS */
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
#if CH_USE_DYNAMIC
|
|
|
|
#endif /* CH_USE_DYNAMIC */
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*
|
|
|
|
* chibios_rt::BaseThread *
|
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
BaseThread::BaseThread() : ThreadReference(NULL) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-01-03 16:17:42 +00:00
|
|
|
msg_t BaseThread::main(void) {
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadReference BaseThread::start(tprio_t prio) {
|
|
|
|
|
|
|
|
(void)prio;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
};
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
msg_t _thd_start(void *arg) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-03 16:17:42 +00:00
|
|
|
return ((BaseThread *)arg)->main();
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 10:11:02 +00:00
|
|
|
void BaseThread::setName(const char *tname) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-02 10:11:02 +00:00
|
|
|
chRegSetThreadName(tname);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
tprio_t BaseThread::setPriority(tprio_t newprio) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
return chThdSetPriority(newprio);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 10:11:02 +00:00
|
|
|
void BaseThread::exit(msg_t msg) {
|
|
|
|
|
|
|
|
chThdExit(msg);
|
|
|
|
}
|
|
|
|
|
2013-01-02 14:01:11 +00:00
|
|
|
void BaseThread::exitS(msg_t msg) {
|
|
|
|
|
|
|
|
chThdExitS(msg);
|
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
bool BaseThread::shouldTerminate(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
return (bool)chThdShouldTerminate();
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
void BaseThread::sleep(systime_t interval){
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
chThdSleep(interval);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
void BaseThread::sleepUntil(systime_t time) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
chThdSleepUntil(time);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 14:01:11 +00:00
|
|
|
void BaseThread::yield(void) {
|
|
|
|
|
|
|
|
chThdYield();
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:38:41 +00:00
|
|
|
#if CH_USE_MESSAGES
|
|
|
|
ThreadReference BaseThread::waitMessage(void) {
|
|
|
|
|
|
|
|
ThreadReference tr(chMsgWait());
|
|
|
|
return tr;
|
|
|
|
}
|
|
|
|
#endif /* CH_USE_MESSAGES */
|
|
|
|
|
2013-01-02 10:11:02 +00:00
|
|
|
#if CH_USE_EVENTS
|
|
|
|
eventmask_t BaseThread::getAndClearEvents(eventmask_t mask) {
|
|
|
|
|
|
|
|
return chEvtGetAndClearEvents(mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
eventmask_t BaseThread::addEvents(eventmask_t mask) {
|
|
|
|
|
|
|
|
return chEvtAddEvents(mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
eventmask_t BaseThread::waitOneEvent(eventmask_t ewmask) {
|
|
|
|
|
|
|
|
return chEvtWaitOne(ewmask);
|
|
|
|
}
|
|
|
|
|
|
|
|
eventmask_t BaseThread::waitAnyEvent(eventmask_t ewmask) {
|
|
|
|
|
|
|
|
return chEvtWaitAny(ewmask);
|
|
|
|
}
|
|
|
|
|
|
|
|
eventmask_t BaseThread::waitAllEvents(eventmask_t ewmask) {
|
|
|
|
|
|
|
|
return chEvtWaitAll(ewmask);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CH_USE_EVENTS_TIMEOUT
|
|
|
|
eventmask_t BaseThread::waitOneEventTimeout(eventmask_t ewmask,
|
|
|
|
systime_t time) {
|
|
|
|
|
|
|
|
return chEvtWaitOneTimeout(ewmask, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
eventmask_t BaseThread::waitAnyEventTimeout(eventmask_t ewmask,
|
|
|
|
systime_t time) {
|
|
|
|
|
|
|
|
return chEvtWaitAnyTimeout(ewmask, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
eventmask_t BaseThread::waitAllEventsTimeout(eventmask_t ewmask,
|
|
|
|
systime_t time) {
|
|
|
|
|
|
|
|
return chEvtWaitAllTimeout(ewmask, time);
|
|
|
|
}
|
|
|
|
#endif /* CH_USE_EVENTS_TIMEOUT */
|
2013-01-02 10:47:01 +00:00
|
|
|
|
|
|
|
void BaseThread::dispatchEvents(const evhandler_t handlers[],
|
|
|
|
eventmask_t mask) {
|
|
|
|
|
|
|
|
chEvtDispatch(handlers, mask);
|
|
|
|
}
|
2013-01-02 10:11:02 +00:00
|
|
|
#endif /* CH_USE_EVENTS */
|
|
|
|
|
2013-01-02 14:01:11 +00:00
|
|
|
#if CH_USE_MUTEXES
|
|
|
|
void BaseThread::unlockMutex(void) {
|
|
|
|
|
|
|
|
chMtxUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseThread::unlockMutexS(void) {
|
|
|
|
|
|
|
|
chMtxUnlockS();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseThread::unlockAllMutexes(void) {
|
|
|
|
|
|
|
|
chMtxUnlockAll();
|
|
|
|
}
|
|
|
|
#endif /* CH_USE_MUTEXES */
|
|
|
|
|
2009-08-16 13:07:24 +00:00
|
|
|
#if CH_USE_SEMAPHORES
|
|
|
|
/*------------------------------------------------------------------------*
|
2013-01-04 10:26:22 +00:00
|
|
|
* chibios_rt::CounterSemaphore *
|
2009-08-16 13:07:24 +00:00
|
|
|
*------------------------------------------------------------------------*/
|
2013-01-04 10:26:22 +00:00
|
|
|
CounterSemaphore::CounterSemaphore(cnt_t n) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chSemInit(&sem, n);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
void CounterSemaphore::reset(cnt_t n) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chSemReset(&sem, n);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
void CounterSemaphore::resetI(cnt_t n) {
|
2013-01-02 13:30:07 +00:00
|
|
|
|
|
|
|
chSemResetI(&sem, n);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
msg_t CounterSemaphore::wait(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
return chSemWait(&sem);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
msg_t CounterSemaphore::waitS(void) {
|
2013-01-02 13:30:07 +00:00
|
|
|
|
|
|
|
return chSemWaitS(&sem);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
msg_t CounterSemaphore::waitTimeout(systime_t time) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
return chSemWaitTimeout(&sem, time);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
msg_t CounterSemaphore::waitTimeoutS(systime_t time) {
|
2013-01-02 13:30:07 +00:00
|
|
|
|
|
|
|
return chSemWaitTimeoutS(&sem, time);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
void CounterSemaphore::signal(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chSemSignal(&sem);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
void CounterSemaphore::signalI(void) {
|
2013-01-02 13:30:07 +00:00
|
|
|
|
|
|
|
chSemSignalI(&sem);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
void CounterSemaphore::addCounterI(cnt_t n) {
|
2013-01-02 13:30:07 +00:00
|
|
|
|
|
|
|
chSemAddCounterI(&sem, n);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
cnt_t CounterSemaphore::getCounterI(void) {
|
2013-01-02 13:30:07 +00:00
|
|
|
|
|
|
|
return chSemGetCounterI(&sem);
|
|
|
|
}
|
|
|
|
|
2009-08-16 13:07:24 +00:00
|
|
|
#if CH_USE_SEMSW
|
2013-01-04 10:26:22 +00:00
|
|
|
msg_t CounterSemaphore::signalWait(CounterSemaphore *ssem,
|
|
|
|
CounterSemaphore *wsem) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
return chSemSignalWait(&ssem->sem, &wsem->sem);
|
|
|
|
}
|
|
|
|
#endif /* CH_USE_SEMSW */
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*
|
|
|
|
* chibios_rt::BinarySemaphore *
|
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
BinarySemaphore::BinarySemaphore(bool taken) {
|
|
|
|
|
|
|
|
chBSemInit(&bsem, (bool_t)taken);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_t BinarySemaphore::wait(void) {
|
|
|
|
|
|
|
|
return chBSemWait(&bsem);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_t BinarySemaphore::waitS(void) {
|
|
|
|
|
|
|
|
return chBSemWaitS(&bsem);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_t BinarySemaphore::waitTimeout(systime_t time) {
|
|
|
|
|
|
|
|
return chBSemWaitTimeout(&bsem, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_t BinarySemaphore::waitTimeoutS(systime_t time) {
|
|
|
|
|
|
|
|
return chBSemWaitTimeoutS(&bsem, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BinarySemaphore::reset(bool taken) {
|
|
|
|
|
|
|
|
chBSemReset(&bsem, (bool_t)taken);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BinarySemaphore::resetI(bool taken) {
|
|
|
|
|
|
|
|
chBSemResetI(&bsem, (bool_t)taken);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BinarySemaphore::signal(void) {
|
|
|
|
|
|
|
|
chBSemSignal(&bsem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BinarySemaphore::signalI(void) {
|
|
|
|
|
|
|
|
chBSemSignalI(&bsem);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BinarySemaphore::getStateI(void) {
|
|
|
|
|
|
|
|
return (bool)chBSemGetStateI(&bsem);
|
|
|
|
}
|
2009-08-16 13:07:24 +00:00
|
|
|
#endif /* CH_USE_SEMAPHORES */
|
|
|
|
|
|
|
|
#if CH_USE_MUTEXES
|
|
|
|
/*------------------------------------------------------------------------*
|
|
|
|
* chibios_rt::Mutex *
|
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
Mutex::Mutex(void) {
|
|
|
|
|
|
|
|
chMtxInit(&mutex);
|
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
bool Mutex::tryLock(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
return chMtxTryLock(&mutex);
|
|
|
|
}
|
|
|
|
|
2013-01-02 14:01:11 +00:00
|
|
|
bool Mutex::tryLockS(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-02 14:01:11 +00:00
|
|
|
return chMtxTryLockS(&mutex);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 14:01:11 +00:00
|
|
|
void Mutex::lock(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-02 14:01:11 +00:00
|
|
|
chMtxLock(&mutex);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 14:01:11 +00:00
|
|
|
void Mutex::lockS(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-02 14:01:11 +00:00
|
|
|
chMtxLockS(&mutex);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if CH_USE_CONDVARS
|
|
|
|
/*------------------------------------------------------------------------*
|
|
|
|
* chibios_rt::CondVar *
|
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
CondVar::CondVar(void) {
|
|
|
|
|
|
|
|
chCondInit(&condvar);
|
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
void CondVar::signal(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chCondSignal(&condvar);
|
|
|
|
}
|
|
|
|
|
2013-01-02 14:01:11 +00:00
|
|
|
void CondVar::signalI(void) {
|
|
|
|
|
|
|
|
chCondSignalI(&condvar);
|
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
void CondVar::broadcast(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chCondBroadcast(&condvar);
|
|
|
|
}
|
|
|
|
|
2013-01-02 14:01:11 +00:00
|
|
|
void CondVar::broadcastI(void) {
|
|
|
|
|
|
|
|
chCondBroadcastI(&condvar);
|
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
msg_t CondVar::wait(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
return chCondWait(&condvar);
|
|
|
|
}
|
|
|
|
|
2013-01-02 14:01:11 +00:00
|
|
|
msg_t CondVar::waitS(void) {
|
|
|
|
|
|
|
|
return chCondWaitS(&condvar);
|
|
|
|
}
|
|
|
|
|
2009-08-16 13:07:24 +00:00
|
|
|
#if CH_USE_CONDVARS_TIMEOUT
|
2013-01-01 15:42:44 +00:00
|
|
|
msg_t CondVar::waitTimeout(systime_t time) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
return chCondWaitTimeout(&condvar, time);
|
|
|
|
}
|
|
|
|
#endif /* CH_USE_CONDVARS_TIMEOUT */
|
|
|
|
#endif /* CH_USE_CONDVARS */
|
|
|
|
#endif /* CH_USE_MUTEXES */
|
|
|
|
|
|
|
|
#if CH_USE_EVENTS
|
|
|
|
/*------------------------------------------------------------------------*
|
2013-01-02 10:47:01 +00:00
|
|
|
* chibios_rt::EvtListener *
|
2009-08-16 13:07:24 +00:00
|
|
|
*------------------------------------------------------------------------*/
|
2013-01-02 10:47:01 +00:00
|
|
|
flagsmask_t EvtListener::getAndClearFlags(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-02 10:11:02 +00:00
|
|
|
return chEvtGetAndClearFlags(&ev_listener);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-03 18:28:21 +00:00
|
|
|
flagsmask_t EvtListener::getAndClearFlagsI(void) {
|
|
|
|
|
|
|
|
return chEvtGetAndClearFlagsI(&ev_listener);
|
|
|
|
}
|
|
|
|
|
2013-01-02 10:11:02 +00:00
|
|
|
/*------------------------------------------------------------------------*
|
2013-01-02 10:47:01 +00:00
|
|
|
* chibios_rt::EvtSource *
|
2013-01-02 10:11:02 +00:00
|
|
|
*------------------------------------------------------------------------*/
|
2013-01-02 10:47:01 +00:00
|
|
|
EvtSource::EvtSource(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-02 10:47:01 +00:00
|
|
|
chEvtInit(&ev_source);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 10:47:01 +00:00
|
|
|
void EvtSource::registerOne(chibios_rt::EvtListener *elp,
|
2013-01-02 10:11:02 +00:00
|
|
|
eventid_t eid) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-02 10:11:02 +00:00
|
|
|
chEvtRegister(&ev_source, &elp->ev_listener, eid);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 10:47:01 +00:00
|
|
|
void EvtSource::registerMask(chibios_rt::EvtListener *elp,
|
2013-01-02 10:11:02 +00:00
|
|
|
eventmask_t emask) {
|
2013-01-02 08:32:03 +00:00
|
|
|
|
2013-01-02 10:11:02 +00:00
|
|
|
chEvtRegisterMask(&ev_source, &elp->ev_listener, emask);
|
2013-01-02 08:32:03 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 10:47:01 +00:00
|
|
|
void EvtSource::unregister(chibios_rt::EvtListener *elp) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-02 10:11:02 +00:00
|
|
|
chEvtUnregister(&ev_source, &elp->ev_listener);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 10:47:01 +00:00
|
|
|
void EvtSource::broadcastFlags(flagsmask_t flags) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-01-02 10:11:02 +00:00
|
|
|
chEvtBroadcastFlags(&ev_source, flags);
|
2013-01-01 09:37:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 10:47:01 +00:00
|
|
|
void EvtSource::broadcastFlagsI(flagsmask_t flags) {
|
2013-01-01 09:37:24 +00:00
|
|
|
|
2013-01-02 10:11:02 +00:00
|
|
|
chEvtBroadcastFlagsI(&ev_source, flags);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
#endif /* CH_USE_EVENTS */
|
2013-01-02 15:00:20 +00:00
|
|
|
|
2013-01-03 09:52:04 +00:00
|
|
|
#if CH_USE_QUEUES
|
|
|
|
/*------------------------------------------------------------------------*
|
2013-01-04 10:26:22 +00:00
|
|
|
* chibios_rt::InQueue *
|
2013-01-03 09:52:04 +00:00
|
|
|
*------------------------------------------------------------------------*/
|
2013-01-04 10:26:22 +00:00
|
|
|
InQueue::InQueue(uint8_t *bp, size_t size, qnotify_t infy, void *link) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
chIQInit(&iq, bp, size, infy, link);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
size_t InQueue::getFullI(void) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return chIQGetFullI(&iq);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
size_t InQueue::getEmptyI(void) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return chIQGetEmptyI(&iq);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
bool InQueue::isEmptyI(void) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return (bool)chIQIsEmptyI(&iq);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
bool InQueue::isFullI(void) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return (bool)chIQIsFullI(&iq);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
void InQueue::resetI(void) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
chIQResetI(&iq);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
msg_t InQueue::putI(uint8_t b) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return chIQPutI(&iq, b);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
msg_t InQueue::get() {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return chIQGet(&iq);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
msg_t InQueue::getTimeout(systime_t time) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return chIQGetTimeout(&iq, time);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
size_t InQueue::readTimeout(uint8_t *bp, size_t n, systime_t time) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return chIQReadTimeout(&iq, bp, n, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*
|
2013-01-04 10:26:22 +00:00
|
|
|
* chibios_rt::OutQueue *
|
2013-01-03 09:52:04 +00:00
|
|
|
*------------------------------------------------------------------------*/
|
2013-01-04 10:26:22 +00:00
|
|
|
OutQueue::OutQueue(uint8_t *bp, size_t size, qnotify_t onfy, void *link) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
chOQInit(&oq, bp, size, onfy, link);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
size_t OutQueue::getFullI(void) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return chOQGetFullI(&oq);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
size_t OutQueue::getEmptyI(void) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return chOQGetEmptyI(&oq);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
bool OutQueue::isEmptyI(void) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return (bool)chOQIsEmptyI(&oq);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
bool OutQueue::isFullI(void) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return (bool)chOQIsFullI(&oq);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
void OutQueue::resetI(void) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
chOQResetI(&oq);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
msg_t OutQueue::put(uint8_t b) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return chOQPut(&oq, b);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
msg_t OutQueue::putTimeout(uint8_t b, systime_t time) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return chOQPutTimeout(&oq, b, time);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
msg_t OutQueue::getI(void) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return chOQGetI(&oq);
|
|
|
|
}
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
size_t OutQueue::writeTimeout(const uint8_t *bp, size_t n,
|
|
|
|
systime_t time) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return chOQWriteTimeout(&oq, bp, n, time);
|
|
|
|
}
|
|
|
|
#endif /* CH_USE_QUEUES */
|
|
|
|
|
|
|
|
#if CH_USE_MAILBOXES
|
2013-01-02 15:00:20 +00:00
|
|
|
/*------------------------------------------------------------------------*
|
|
|
|
* chibios_rt::Mailbox *
|
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
Mailbox::Mailbox(msg_t *buf, cnt_t n) {
|
|
|
|
|
|
|
|
chMBInit(&mb, buf, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mailbox::reset(void) {
|
|
|
|
|
|
|
|
chMBReset(&mb);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_t Mailbox::post(msg_t msg, systime_t time) {
|
|
|
|
|
|
|
|
return chMBPost(&mb, msg, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_t Mailbox::postS(msg_t msg, systime_t time) {
|
|
|
|
|
|
|
|
return chMBPostS(&mb, msg, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_t Mailbox::postI(msg_t msg) {
|
|
|
|
|
|
|
|
return chMBPostI(&mb, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_t Mailbox::postAhead(msg_t msg, systime_t time) {
|
|
|
|
|
|
|
|
return chMBPostAhead(&mb, msg, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_t Mailbox::postAheadS(msg_t msg, systime_t time) {
|
|
|
|
|
|
|
|
return chMBPostAheadS(&mb, msg, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_t Mailbox::postAheadI(msg_t msg) {
|
|
|
|
|
|
|
|
return chMBPostAheadI(&mb, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_t Mailbox::fetch(msg_t *msgp, systime_t time) {
|
|
|
|
|
|
|
|
return chMBFetch(&mb, msgp, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_t Mailbox::fetchS(msg_t *msgp, systime_t time) {
|
|
|
|
|
|
|
|
return chMBFetchS(&mb, msgp, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_t Mailbox::fetchI(msg_t *msgp) {
|
|
|
|
|
|
|
|
return chMBFetchI(&mb, msgp);
|
|
|
|
}
|
|
|
|
#endif /* CH_USE_MAILBOXES */
|
2013-01-02 18:42:39 +00:00
|
|
|
|
|
|
|
#if CH_USE_MEMPOOLS
|
|
|
|
/*------------------------------------------------------------------------*
|
|
|
|
* chibios_rt::MemoryPool *
|
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
MemoryPool::MemoryPool(size_t size, memgetfunc_t provider) {
|
|
|
|
|
|
|
|
chPoolInit(&pool, size, provider);
|
|
|
|
}
|
|
|
|
|
2013-01-06 09:10:58 +00:00
|
|
|
MemoryPool::MemoryPool(size_t size, memgetfunc_t provider, void* p, size_t n) {
|
|
|
|
|
|
|
|
chPoolInit(&pool, size, provider);
|
|
|
|
chPoolLoadArray(&pool, p, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-02 18:42:39 +00:00
|
|
|
void MemoryPool::loadArray(void *p, size_t n) {
|
|
|
|
|
|
|
|
chPoolLoadArray(&pool, p, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *MemoryPool::allocI(void) {
|
|
|
|
|
2013-01-06 09:10:58 +00:00
|
|
|
return chPoolAllocI(&pool);
|
2013-01-02 18:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *MemoryPool::alloc(void) {
|
|
|
|
|
2013-01-06 09:10:58 +00:00
|
|
|
return chPoolAlloc(&pool);
|
2013-01-02 18:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryPool::free(void *objp) {
|
|
|
|
|
|
|
|
chPoolFree(&pool, objp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryPool::freeI(void *objp) {
|
|
|
|
|
|
|
|
chPoolFreeI(&pool, objp);
|
|
|
|
}
|
|
|
|
#endif /* CH_USE_MEMPOOLS */
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|