2018-07-20 18:08:24 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-10-24 23:08:21 +08:00
|
|
|
#include <kernel.h>
|
2018-07-20 18:08:24 +08:00
|
|
|
#include <cmsis_os.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create and Initialize Message queue.
|
|
|
|
*/
|
|
|
|
osMessageQId osMessageCreate(const osMessageQDef_t *queue_def,
|
|
|
|
osThreadId thread_id)
|
|
|
|
{
|
|
|
|
if (queue_def == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:57:13 +08:00
|
|
|
if (k_is_in_isr()) {
|
2018-07-20 18:08:24 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_msgq_init(queue_def->msgq, queue_def->pool,
|
|
|
|
queue_def->item_sz, queue_def->queue_sz);
|
|
|
|
return (osMessageQId)(queue_def);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Put a message to a Queue.
|
|
|
|
*/
|
|
|
|
osStatus osMessagePut(osMessageQId queue_id, uint32_t info, uint32_t millisec)
|
|
|
|
{
|
|
|
|
osMessageQDef_t *queue_def = (osMessageQDef_t *)queue_id;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (queue_def == NULL) {
|
|
|
|
return osErrorParameter;
|
|
|
|
}
|
|
|
|
|
2019-03-27 09:57:45 +08:00
|
|
|
if (millisec == 0U) {
|
2018-07-20 18:08:24 +08:00
|
|
|
retval = k_msgq_put(queue_def->msgq, (void *)&info, K_NO_WAIT);
|
|
|
|
} else if (millisec == osWaitForever) {
|
|
|
|
retval = k_msgq_put(queue_def->msgq, (void *)&info, K_FOREVER);
|
|
|
|
} else {
|
2020-05-05 04:39:17 +08:00
|
|
|
retval = k_msgq_put(queue_def->msgq, (void *)&info,
|
|
|
|
K_MSEC(millisec));
|
2018-07-20 18:08:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (retval == 0) {
|
|
|
|
return osOK;
|
2018-08-17 12:16:49 +08:00
|
|
|
} else if (retval == -EAGAIN) {
|
|
|
|
return osErrorTimeoutResource;
|
2018-07-20 18:08:24 +08:00
|
|
|
} else {
|
|
|
|
return osErrorResource;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get a message or Wait for a Message from a Queue.
|
|
|
|
*/
|
|
|
|
osEvent osMessageGet(osMessageQId queue_id, uint32_t millisec)
|
|
|
|
{
|
|
|
|
osMessageQDef_t *queue_def = (osMessageQDef_t *)queue_id;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t info;
|
2018-08-17 12:16:49 +08:00
|
|
|
osEvent evt = {0};
|
2018-07-20 18:08:24 +08:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (queue_def == NULL) {
|
|
|
|
evt.status = osErrorParameter;
|
|
|
|
return evt;
|
|
|
|
}
|
|
|
|
|
2019-03-27 09:57:45 +08:00
|
|
|
if (millisec == 0U) {
|
2018-07-20 18:08:24 +08:00
|
|
|
retval = k_msgq_get(queue_def->msgq, &info, K_NO_WAIT);
|
|
|
|
} else if (millisec == osWaitForever) {
|
|
|
|
retval = k_msgq_get(queue_def->msgq, &info, K_FOREVER);
|
|
|
|
} else {
|
2020-05-05 04:39:17 +08:00
|
|
|
retval = k_msgq_get(queue_def->msgq, &info, K_MSEC(millisec));
|
2018-07-20 18:08:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (retval == 0) {
|
|
|
|
evt.status = osEventMessage;
|
2018-08-17 12:16:49 +08:00
|
|
|
evt.value.v = info;
|
2018-07-20 18:08:24 +08:00
|
|
|
} else if (retval == -EAGAIN) {
|
|
|
|
evt.status = osEventTimeout;
|
|
|
|
} else if (retval == -ENOMSG) {
|
|
|
|
evt.status = osOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
evt.def.message_id = queue_id;
|
|
|
|
|
|
|
|
return evt;
|
|
|
|
}
|