2018-07-18 12:39:20 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <kernel_structs.h>
|
|
|
|
#include <cmsis_os.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create and Initialize mail queue.
|
|
|
|
*/
|
|
|
|
osMailQId osMailCreate(const osMailQDef_t *queue_def, osThreadId thread_id)
|
|
|
|
{
|
|
|
|
if (queue_def == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_is_in_isr()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_mbox_init(queue_def->mbox);
|
|
|
|
return (osMailQId)(queue_def);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Allocate a memory block from a mail.
|
|
|
|
*/
|
|
|
|
void *osMailAlloc(osMailQId queue_id, uint32_t millisec)
|
|
|
|
{
|
|
|
|
osMailQDef_t *queue_def = (osMailQDef_t *)queue_id;
|
|
|
|
char *ptr;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (queue_def == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (millisec == 0) {
|
|
|
|
retval = k_mem_slab_alloc(
|
|
|
|
(struct k_mem_slab *)(queue_def->pool),
|
|
|
|
(void **)&ptr, K_NO_WAIT);
|
|
|
|
} else if (millisec == osWaitForever) {
|
|
|
|
retval = k_mem_slab_alloc(
|
|
|
|
(struct k_mem_slab *)(queue_def->pool),
|
|
|
|
(void **)&ptr, K_FOREVER);
|
|
|
|
} else {
|
|
|
|
retval = k_mem_slab_alloc(
|
|
|
|
(struct k_mem_slab *)(queue_def->pool),
|
|
|
|
(void **)&ptr, millisec);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval == 0) {
|
|
|
|
return ptr;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Allocate a memory block from a mail and set memory block to zero.
|
|
|
|
*/
|
|
|
|
void *osMailCAlloc(osMailQId queue_id, uint32_t millisec)
|
|
|
|
{
|
|
|
|
osMailQDef_t *queue_def = (osMailQDef_t *)queue_id;
|
|
|
|
char *ptr;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (queue_def == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (millisec == 0) {
|
|
|
|
retval = k_mem_slab_alloc(
|
|
|
|
(struct k_mem_slab *)(queue_def->pool),
|
|
|
|
(void **)&ptr, K_NO_WAIT);
|
|
|
|
} else if (millisec == osWaitForever) {
|
|
|
|
retval = k_mem_slab_alloc(
|
|
|
|
(struct k_mem_slab *)(queue_def->pool),
|
|
|
|
(void **)&ptr, K_FOREVER);
|
|
|
|
} else {
|
|
|
|
retval = k_mem_slab_alloc(
|
|
|
|
(struct k_mem_slab *)(queue_def->pool),
|
|
|
|
(void **)&ptr, millisec);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval == 0) {
|
2018-09-12 10:09:03 +08:00
|
|
|
(void)memset(ptr, 0, queue_def->item_sz);
|
2018-07-18 12:39:20 +08:00
|
|
|
return ptr;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Put a mail to a queue.
|
|
|
|
*/
|
|
|
|
osStatus osMailPut(osMailQId queue_id, void *mail)
|
|
|
|
{
|
|
|
|
osMailQDef_t *queue_def = (osMailQDef_t *)queue_id;
|
|
|
|
struct k_mbox_msg mmsg;
|
|
|
|
|
|
|
|
if (queue_def == NULL) {
|
|
|
|
return osErrorParameter;
|
|
|
|
}
|
|
|
|
|
2018-09-10 16:58:42 +08:00
|
|
|
if (mail == NULL) {
|
|
|
|
return osErrorValue;
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:09:03 +08:00
|
|
|
(void)memset(&mmsg, 0, sizeof(mmsg));
|
2018-07-18 12:39:20 +08:00
|
|
|
mmsg.tx_data = mail;
|
|
|
|
mmsg.rx_source_thread = K_ANY;
|
|
|
|
mmsg.tx_target_thread = K_ANY;
|
|
|
|
|
2018-09-10 16:58:42 +08:00
|
|
|
k_mbox_async_put(queue_def->mbox, &mmsg, NULL);
|
|
|
|
return osOK;
|
2018-07-18 12:39:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get a mail from a queue.
|
|
|
|
*/
|
|
|
|
osEvent osMailGet(osMailQId queue_id, uint32_t millisec)
|
|
|
|
{
|
|
|
|
osMailQDef_t *queue_def = (osMailQDef_t *)queue_id;
|
|
|
|
struct k_mbox_msg mmsg;
|
2018-08-17 12:16:49 +08:00
|
|
|
osEvent evt = {0};
|
2018-07-18 12:39:20 +08:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (queue_def == NULL) {
|
|
|
|
evt.status = osErrorParameter;
|
|
|
|
return evt;
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:09:03 +08:00
|
|
|
(void)memset(&mmsg, 0, sizeof(mmsg));
|
2018-07-18 12:39:20 +08:00
|
|
|
mmsg.rx_source_thread = K_ANY;
|
|
|
|
mmsg.tx_target_thread = K_ANY;
|
|
|
|
|
|
|
|
if (millisec == 0) {
|
|
|
|
retval = k_mbox_get(queue_def->mbox, &mmsg, NULL, K_NO_WAIT);
|
|
|
|
} else if (millisec == osWaitForever) {
|
|
|
|
retval = k_mbox_get(queue_def->mbox, &mmsg, NULL, K_FOREVER);
|
|
|
|
} else {
|
|
|
|
retval = k_mbox_get(queue_def->mbox, &mmsg, NULL, millisec);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval == 0) {
|
|
|
|
evt.status = osEventMail;
|
2018-08-17 12:16:49 +08:00
|
|
|
evt.value.p = mmsg.tx_data;
|
2018-07-18 12:39:20 +08:00
|
|
|
} else if (retval == -EAGAIN) {
|
|
|
|
evt.status = osEventTimeout;
|
|
|
|
} else if (retval == -ENOMSG) {
|
|
|
|
evt.status = osOK;
|
|
|
|
} else {
|
|
|
|
evt.status = osErrorValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
evt.def.mail_id = queue_id;
|
|
|
|
|
|
|
|
return evt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Free a memory block from a mail.
|
|
|
|
*/
|
|
|
|
osStatus osMailFree(osMailQId queue_id, void *mail)
|
|
|
|
{
|
|
|
|
osMailQDef_t *queue_def = (osMailQDef_t *)queue_id;
|
|
|
|
|
|
|
|
k_mem_slab_free((struct k_mem_slab *)(queue_def->pool), (void *) &mail);
|
|
|
|
|
|
|
|
return osOK;
|
|
|
|
}
|