2009-08-16 13:07:24 +00:00
|
|
|
/*
|
2015-01-11 13:56:55 +00:00
|
|
|
ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-05-11 06:08:17 +00:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-05-11 06:08:17 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-05-11 06:08:17 +00:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
|
|
|
/**
|
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) {
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
chSysLockFromISR();
|
2013-01-06 09:10:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void System::unlockFromIsr(void) {
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
chSysUnlockFromISR();
|
2013-01-06 09:10:58 +00:00
|
|
|
}
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
systime_t System::getTime(void) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
return chVTGetSystemTimeX();
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
return chVTIsSystemTimeWithinX(start, end);
|
2013-01-02 14:01:11 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
|
2014-11-22 17:56:36 +00:00
|
|
|
return chCoreGetStatusX();
|
2013-01-03 09:52:04 +00:00
|
|
|
}
|
|
|
|
|
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))
|
2013-06-16 12:21:30 +00:00
|
|
|
chVTDoResetI(&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
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
return 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) {
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
chSysHalt("invoked unimplemented method stop()");
|
2013-01-04 10:26:22 +00:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
"already referenced");
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
thread_ref = chThdGetSelfX();
|
|
|
|
chSchGoSleepS(CH_STATE_SUSPENDED);
|
2013-01-01 15:42:44 +00:00
|
|
|
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,
|
|
|
|
"already referenced");
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
thread_ref = chThdGetSelfX();
|
|
|
|
chSchGoSleepS(CH_STATE_SUSPENDED);
|
2013-01-01 15:42:44 +00:00
|
|
|
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
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
chSysLock();
|
2013-01-01 15:42:44 +00:00
|
|
|
|
|
|
|
chDbgAssert(thread_ref != NULL,
|
|
|
|
"not referenced");
|
|
|
|
|
|
|
|
if (thread_ref) {
|
2014-08-03 20:23:16 +00:00
|
|
|
thread_t *tp = thread_ref;
|
2013-01-01 15:42:44 +00:00
|
|
|
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,
|
|
|
|
"not referenced");
|
|
|
|
|
|
|
|
if (thread_ref) {
|
2014-08-03 20:23:16 +00:00
|
|
|
thread_t *tp = thread_ref;
|
2013-01-01 15:42:44 +00:00
|
|
|
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,
|
|
|
|
"not referenced");
|
|
|
|
|
2013-01-01 15:42:44 +00:00
|
|
|
chThdTerminate(thread_ref);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
#if CH_CFG_USE_WAITEXIT
|
2013-01-01 15:42:44 +00:00
|
|
|
msg_t ThreadReference::wait(void) {
|
|
|
|
|
|
|
|
chDbgAssert(thread_ref != NULL,
|
|
|
|
"not referenced");
|
|
|
|
|
|
|
|
msg_t msg = chThdWait(thread_ref);
|
|
|
|
thread_ref = NULL;
|
|
|
|
return msg;
|
|
|
|
}
|
2014-08-03 20:23:16 +00:00
|
|
|
#endif /* CH_CFG_USE_WAITEXIT */
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
#if CH_CFG_USE_MESSAGES
|
2013-01-01 15:42:44 +00:00
|
|
|
msg_t ThreadReference::sendMessage(msg_t msg) {
|
|
|
|
|
|
|
|
chDbgAssert(thread_ref != NULL,
|
|
|
|
"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,
|
|
|
|
"not referenced");
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
return 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,
|
|
|
|
"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,
|
|
|
|
"not referenced");
|
|
|
|
|
2013-01-03 16:17:42 +00:00
|
|
|
chMsgRelease(thread_ref, msg);
|
|
|
|
}
|
2014-08-03 20:23:16 +00:00
|
|
|
#endif /* CH_CFG_USE_MESSAGES */
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
#if CH_CFG_USE_EVENTS
|
2013-01-02 10:11:02 +00:00
|
|
|
void ThreadReference::signalEvents(eventmask_t mask) {
|
|
|
|
|
2013-01-04 10:26:22 +00:00
|
|
|
chDbgAssert(thread_ref != NULL,
|
|
|
|
"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,
|
|
|
|
"not referenced");
|
|
|
|
|
2013-01-02 10:11:02 +00:00
|
|
|
chEvtSignalI(thread_ref, mask);
|
|
|
|
}
|
2014-08-03 20:23:16 +00:00
|
|
|
#endif /* CH_CFG_USE_EVENTS */
|
2013-01-02 10:11:02 +00:00
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
#if CH_CFG_USE_DYNAMIC
|
|
|
|
#endif /* CH_CFG_USE_DYNAMIC */
|
2013-01-01 15:42:44 +00:00
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*
|
2013-05-11 10:17:48 +00:00
|
|
|
* chibios_rt::BaseThread *
|
2013-01-01 15:42:44 +00:00
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
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
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
return chThdShouldTerminateX();
|
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();
|
|
|
|
}
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
#if CH_CFG_USE_MESSAGES
|
2013-01-04 09:38:41 +00:00
|
|
|
ThreadReference BaseThread::waitMessage(void) {
|
|
|
|
|
|
|
|
ThreadReference tr(chMsgWait());
|
|
|
|
return tr;
|
|
|
|
}
|
2014-08-03 20:23:16 +00:00
|
|
|
#endif /* CH_CFG_USE_MESSAGES */
|
2013-01-04 09:38:41 +00:00
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
#if CH_CFG_USE_EVENTS
|
2013-01-02 10:11:02 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
#if CH_CFG_USE_EVENTS_TIMEOUT
|
2013-01-02 10:11:02 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-08-03 20:23:16 +00:00
|
|
|
#endif /* CH_CFG_USE_EVENTS_TIMEOUT */
|
2013-01-02 10:47:01 +00:00
|
|
|
|
|
|
|
void BaseThread::dispatchEvents(const evhandler_t handlers[],
|
|
|
|
eventmask_t mask) {
|
|
|
|
|
|
|
|
chEvtDispatch(handlers, mask);
|
|
|
|
}
|
2014-08-03 20:23:16 +00:00
|
|
|
#endif /* CH_CFG_USE_EVENTS */
|
2013-01-02 10:11:02 +00:00
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
#if CH_CFG_USE_MUTEXES
|
|
|
|
void BaseThread::unlockMutex(Mutex *mp) {
|
2013-01-02 14:01:11 +00:00
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
chMtxUnlock(&mp->mutex);
|
2013-01-02 14:01:11 +00:00
|
|
|
}
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
void BaseThread::unlockMutexS(Mutex *mp) {
|
2013-01-02 14:01:11 +00:00
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
chMtxUnlockS(&mp->mutex);
|
2013-01-02 14:01:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BaseThread::unlockAllMutexes(void) {
|
|
|
|
|
|
|
|
chMtxUnlockAll();
|
|
|
|
}
|
2014-08-03 20:23:16 +00:00
|
|
|
#endif /* CH_CFG_USE_MUTEXES */
|
2013-01-02 14:01:11 +00:00
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
#if CH_CFG_USE_SEMAPHORES
|
2009-08-16 13:07:24 +00:00
|
|
|
/*------------------------------------------------------------------------*
|
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
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
chSemObjectInit(&sem, n);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-09-10 18:36:23 +00:00
|
|
|
msg_t CounterSemaphore::wait(systime_t time) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
return chSemWaitTimeout(&sem, time);
|
|
|
|
}
|
|
|
|
|
2014-09-10 18:36:23 +00:00
|
|
|
msg_t CounterSemaphore::waitS(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);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*
|
|
|
|
* chibios_rt::BinarySemaphore *
|
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
BinarySemaphore::BinarySemaphore(bool taken) {
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
chBSemObjectInit(&bsem, taken);
|
2013-01-03 09:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg_t BinarySemaphore::wait(void) {
|
|
|
|
|
|
|
|
return chBSemWait(&bsem);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_t BinarySemaphore::waitS(void) {
|
|
|
|
|
|
|
|
return chBSemWaitS(&bsem);
|
|
|
|
}
|
|
|
|
|
2014-09-10 18:36:23 +00:00
|
|
|
msg_t BinarySemaphore::wait(systime_t time) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return chBSemWaitTimeout(&bsem, time);
|
|
|
|
}
|
|
|
|
|
2014-09-10 18:36:23 +00:00
|
|
|
msg_t BinarySemaphore::waitS(systime_t time) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return chBSemWaitTimeoutS(&bsem, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BinarySemaphore::reset(bool taken) {
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
chBSemReset(&bsem, taken);
|
2013-01-03 09:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BinarySemaphore::resetI(bool taken) {
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
chBSemResetI(&bsem, taken);
|
2013-01-03 09:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BinarySemaphore::signal(void) {
|
|
|
|
|
|
|
|
chBSemSignal(&bsem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BinarySemaphore::signalI(void) {
|
|
|
|
|
|
|
|
chBSemSignalI(&bsem);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BinarySemaphore::getStateI(void) {
|
|
|
|
|
|
|
|
return (bool)chBSemGetStateI(&bsem);
|
|
|
|
}
|
2014-08-03 20:23:16 +00:00
|
|
|
#endif /* CH_CFG_USE_SEMAPHORES */
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
#if CH_CFG_USE_MUTEXES
|
2009-08-16 13:07:24 +00:00
|
|
|
/*------------------------------------------------------------------------*
|
|
|
|
* chibios_rt::Mutex *
|
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
Mutex::Mutex(void) {
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
chMtxObjectInit(&mutex);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-08-26 11:51:53 +00:00
|
|
|
void Mutex::unlock(void) {
|
|
|
|
|
|
|
|
chMtxUnlock(&mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mutex::unlockS(void) {
|
|
|
|
|
|
|
|
chMtxLockS(&mutex);
|
|
|
|
}
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
#if CH_CFG_USE_CONDVARS
|
2009-08-16 13:07:24 +00:00
|
|
|
/*------------------------------------------------------------------------*
|
|
|
|
* chibios_rt::CondVar *
|
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
CondVar::CondVar(void) {
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
chCondObjectInit(&condvar);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
#if CH_CFG_USE_CONDVARS_TIMEOUT
|
2014-09-10 18:36:23 +00:00
|
|
|
msg_t CondVar::wait(systime_t time) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
return chCondWaitTimeout(&condvar, time);
|
|
|
|
}
|
2014-08-03 20:23:16 +00:00
|
|
|
#endif /* CH_CFG_USE_CONDVARS_TIMEOUT */
|
|
|
|
#endif /* CH_CFG_USE_CONDVARS */
|
|
|
|
#endif /* CH_CFG_USE_MUTEXES */
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
#if CH_CFG_USE_EVENTS
|
2009-08-16 13:07:24 +00:00
|
|
|
/*------------------------------------------------------------------------*
|
2013-01-02 10:47:01 +00:00
|
|
|
* chibios_rt::EvtListener *
|
2009-08-16 13:07:24 +00:00
|
|
|
*------------------------------------------------------------------------*/
|
2014-08-03 20:23:16 +00:00
|
|
|
eventflags_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
|
|
|
}
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
eventflags_t EvtListener::getAndClearFlagsI(void) {
|
2013-01-03 18:28:21 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
chEvtObjectInit(&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
|
|
|
}
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
void EvtSource::broadcastFlags(eventflags_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
|
|
|
}
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
void EvtSource::broadcastFlagsI(eventflags_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
|
|
|
}
|
2014-08-03 20:23:16 +00:00
|
|
|
#endif /* CH_CFG_USE_EVENTS */
|
2013-01-02 15:00:20 +00:00
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
#if CH_CFG_USE_QUEUES
|
2013-01-03 09:52:04 +00:00
|
|
|
/*------------------------------------------------------------------------*
|
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
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
chIQObjectInit(&iq, bp, size, infy, link);
|
2013-01-03 09:52:04 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-09-10 18:36:23 +00:00
|
|
|
msg_t InQueue::get(systime_t time) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return chIQGetTimeout(&iq, time);
|
|
|
|
}
|
|
|
|
|
2014-09-10 18:36:23 +00:00
|
|
|
size_t InQueue::read(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
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
chOQObjectInit(&oq, bp, size, onfy, link);
|
2013-01-03 09:52:04 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-09-10 18:36:23 +00:00
|
|
|
msg_t OutQueue::put(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);
|
|
|
|
}
|
|
|
|
|
2014-09-10 18:36:23 +00:00
|
|
|
size_t OutQueue::write(const uint8_t *bp, size_t n, systime_t time) {
|
2013-01-03 09:52:04 +00:00
|
|
|
|
|
|
|
return chOQWriteTimeout(&oq, bp, n, time);
|
|
|
|
}
|
2014-08-03 20:23:16 +00:00
|
|
|
#endif /* CH_CFG_USE_QUEUES */
|
2013-01-03 09:52:04 +00:00
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
#if CH_CFG_USE_MEMPOOLS
|
2013-01-02 18:42:39 +00:00
|
|
|
/*------------------------------------------------------------------------*
|
|
|
|
* chibios_rt::MemoryPool *
|
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
MemoryPool::MemoryPool(size_t size, memgetfunc_t provider) {
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
chPoolObjectInit(&pool, size, provider);
|
2013-01-02 18:42:39 +00:00
|
|
|
}
|
|
|
|
|
2013-01-06 09:10:58 +00:00
|
|
|
MemoryPool::MemoryPool(size_t size, memgetfunc_t provider, void* p, size_t n) {
|
|
|
|
|
2014-08-03 20:23:16 +00:00
|
|
|
chPoolObjectInit(&pool, size, provider);
|
2013-01-06 09:10:58 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-08-03 20:23:16 +00:00
|
|
|
#endif /* CH_CFG_USE_MEMPOOLS */
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|