Added messages to CMSIS RTOS.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@7382 35acf78f-673a-0410-8e92-d51de3d6d3f4
This commit is contained in:
gdisirio 2014-10-07 12:17:30 +00:00
parent dad8b482d9
commit 9edf99296c
2 changed files with 179 additions and 58 deletions

View File

@ -225,7 +225,7 @@ osTimerId osTimerCreate (const osTimerDef_t *timer_def,
osStatus osTimerStart(osTimerId timer_id, uint32_t millisec) {
if (millisec == 0)
return osErrorParameter;
return osErrorValue;
timer_id->millisec = millisec;
chVTSet(&timer_id->vt, millisec, timer_cb, timer_id);
@ -290,15 +290,12 @@ int32_t osSignalClear(osThreadId thread_id, int32_t signals) {
osEvent osSignalWait(int32_t signals, uint32_t millisec) {
osEvent event;
systime_t timeout = millisec == osWaitForever ? TIME_INFINITE :
(systime_t)millisec;
if (signals == 0)
event.value.signals = (uint32_t)chEvtWaitAnyTimeout((eventmask_t)signals,
timeout);
(systime_t)millisec);
else
event.value.signals = (uint32_t)chEvtWaitAllTimeout((eventmask_t)signals,
timeout);
(systime_t)millisec);
/* Type of event.*/
if (event.value.signals == 0)
@ -329,9 +326,8 @@ osSemaphoreId osSemaphoreCreate (const osSemaphoreDef_t *semaphore_def,
*/
int32_t osSemaphoreWait(osSemaphoreId semaphore_id, uint32_t millisec) {
systime_t timeout = millisec == osWaitForever ? TIME_INFINITE :
(systime_t)millisec;
msg_t msg = chSemWaitTimeout((semaphore_t *)semaphore_id, timeout);
msg_t msg = chSemWaitTimeout((semaphore_t *)semaphore_id,
(systime_t)millisec);
switch (msg) {
case MSG_OK:
return osOK;
@ -385,9 +381,8 @@ osMutexId osMutexCreate (const osMutexDef_t *mutex_def) {
*/
osStatus osMutexWait(osMutexId mutex_id, uint32_t millisec) {
systime_t timeout = millisec == osWaitForever ? TIME_INFINITE :
(systime_t)millisec;
msg_t msg = chBSemWaitTimeout((binary_semaphore_t *)mutex_id, timeout);
msg_t msg = chBSemWaitTimeout((binary_semaphore_t *)mutex_id,
(systime_t)millisec);
switch (msg) {
case MSG_OK:
return osOK;
@ -471,4 +466,85 @@ osStatus osPoolFree (osPoolId pool_id, void *block) {
return osOK;
}
/**
* @brief Create a message queue.
* @note The queue is not really created because it is allocated statically,
* this function just re-initializes it.
*/
osMessageQId osMessageCreate(const osMessageQDef_t *queue_def,
osThreadId thread_id) {
/* Ignoring this parameter for now.*/
(void)thread_id;
if (queue_def->item_sz > sizeof (msg_t))
return NULL;
chMBObjectInit(queue_def->mailbox,
queue_def->items,
(size_t)queue_def->queue_sz);
return osOK;
}
/**
* @brief Put a message in the queue.
*/
osStatus osMessagePut(osMessageQId queue_id,
uint32_t info,
uint32_t millisec) {
msg_t msg;
if (port_is_isr_context()) {
/* Waiting makes no sense in ISRs so any value except "immediate"
makes no sense.*/
if (millisec != 0)
return osErrorValue;
chSysLockFromISR();
msg = chMBPostI((mailbox_t *)queue_id, (msg_t)info);
chSysUnlockFromISR();
}
else
msg = chMBPost((mailbox_t *)queue_id, (msg_t)info, (systime_t)millisec);
return msg == MSG_OK ? osOK : osEventTimeout;
}
/**
* @brief Get a message from the queue.
*/
osEvent osMessageGet(osMessageQId queue_id,
uint32_t millisec) {
msg_t msg;
osEvent event;
event.def.message_id = queue_id;
if (port_is_isr_context()) {
/* Waiting makes no sense in ISRs so any value except "immediate"
makes no sense.*/
if (millisec != 0) {
event.status = osErrorValue;
return event;
}
chSysLockFromISR();
msg = chMBFetchI((mailbox_t *)queue_id, (msg_t*)&event.value.v);
chSysUnlockFromISR();
}
else {
msg = chMBFetch((mailbox_t *)queue_id,
(msg_t*)&event.value.v,
(systime_t)millisec);
}
/* Returned event type.*/
event.status = msg == MSG_OK ? osEventMessage : osEventTimeout;
return event;
}
/** @} */

View File

@ -62,8 +62,8 @@
#define osFeature_MainThread 1
#define osFeature_Pool 1
#define osFeature_MailQ 0
#define osFeature_MessageQ 0
#define osFeature_Signals 31
#define osFeature_MessageQ 1
#define osFeature_Signals 24
#define osFeature_Semaphore ((1U << 31) - 1U)
#define osFeature_Wait 0
#define osFeature_SysTick 1
@ -216,6 +216,11 @@ typedef semaphore_t *osSemaphoreId;
*/
typedef memory_pool_t *osPoolId;
/**
* @brief Type of pointer to message queue control block.
*/
typedef struct mailbox *osMessageQId;
/**
* @brief Type of an event.
*/
@ -272,6 +277,16 @@ typedef struct os_pool_def {
void *items;
} osPoolDef_t;
/**
* @brief Type of a message queue definition block.
*/
typedef struct os_messageQ_def {
uint32_t queue_sz;
uint32_t item_sz;
mailbox_t *mailbox;
void *items;
} osMessageQDef_t;
/*===========================================================================*/
/* Module macros. */
/*===========================================================================*/
@ -374,6 +389,29 @@ const osPoolDef_t os_pool_def_##name = { \
*/
#define osPool(name) &os_pool_def_##name
/**
* @brief Define a Message Queue.
*/
#if defined (osObjectsExternal)
#define osMessageQDef(name, queue_sz, type) \
extern const osMessageQDef_t os_messageQ_def_##name
#else
#define osMessageQDef(name, queue_sz, type) \
static const msg_t os_messageQ_buf_##name[queue_sz]; \
static mailbox_t os_messageQ_obj_##name; \
const osMessageQDef_t os_messageQ_def_##name = { \
(queue_sz), \
sizeof (type) \
(void *)&os_messageQ_obj_##name, \
(void *)&os_messageQ_buf_##name[0] \
}
#endif
/**
* @brief Access a Message Queue definition.
*/
#define osMessageQ(name) &os_messageQ_def_##name
/*===========================================================================*/
/* External declarations. */
/*===========================================================================*/
@ -411,6 +449,13 @@ extern "C" {
void *osPoolAlloc(osPoolId pool_id);
void *osPoolCAlloc(osPoolId pool_id);
osStatus osPoolFree(osPoolId pool_id, void *block);
osMessageQId osMessageCreate(const osMessageQDef_t *queue_def,
osThreadId thread_id);
osStatus osMessagePut(osMessageQId queue_id,
uint32_t info,
uint32_t millisec);
osEvent osMessageGet(osMessageQId queue_id,
uint32_t millisec);
#ifdef __cplusplus
}
#endif