1 /*
2 * Copyright (c) 2018 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <cmsis_os.h>
9
10 /**
11 * @brief Create and Initialize Message queue.
12 */
osMessageCreate(const osMessageQDef_t * queue_def,osThreadId thread_id)13 osMessageQId osMessageCreate(const osMessageQDef_t *queue_def,
14 osThreadId thread_id)
15 {
16 if (queue_def == NULL) {
17 return NULL;
18 }
19
20 if (k_is_in_isr()) {
21 return NULL;
22 }
23
24 k_msgq_init(queue_def->msgq, queue_def->pool,
25 queue_def->item_sz, queue_def->queue_sz);
26 return (osMessageQId)(queue_def);
27 }
28
29 /**
30 * @brief Put a message to a Queue.
31 */
osMessagePut(osMessageQId queue_id,uint32_t info,uint32_t millisec)32 osStatus osMessagePut(osMessageQId queue_id, uint32_t info, uint32_t millisec)
33 {
34 osMessageQDef_t *queue_def = (osMessageQDef_t *)queue_id;
35 int retval;
36
37 if (queue_def == NULL) {
38 return osErrorParameter;
39 }
40
41 if (millisec == 0U) {
42 retval = k_msgq_put(queue_def->msgq, (void *)&info, K_NO_WAIT);
43 } else if (millisec == osWaitForever) {
44 retval = k_msgq_put(queue_def->msgq, (void *)&info, K_FOREVER);
45 } else {
46 retval = k_msgq_put(queue_def->msgq, (void *)&info,
47 K_MSEC(millisec));
48 }
49
50 if (retval == 0) {
51 return osOK;
52 } else if (retval == -EAGAIN) {
53 return osErrorTimeoutResource;
54 } else {
55 return osErrorResource;
56 }
57 }
58
59 /**
60 * @brief Get a message or Wait for a Message from a Queue.
61 */
osMessageGet(osMessageQId queue_id,uint32_t millisec)62 osEvent osMessageGet(osMessageQId queue_id, uint32_t millisec)
63 {
64 osMessageQDef_t *queue_def = (osMessageQDef_t *)queue_id;
65 uint32_t info;
66 osEvent evt = {0};
67 int retval;
68
69 if (queue_def == NULL) {
70 evt.status = osErrorParameter;
71 return evt;
72 }
73
74 if (millisec == 0U) {
75 retval = k_msgq_get(queue_def->msgq, &info, K_NO_WAIT);
76 } else if (millisec == osWaitForever) {
77 retval = k_msgq_get(queue_def->msgq, &info, K_FOREVER);
78 } else {
79 retval = k_msgq_get(queue_def->msgq, &info, K_MSEC(millisec));
80 }
81
82 if (retval == 0) {
83 evt.status = osEventMessage;
84 evt.value.v = info;
85 } else if (retval == -EAGAIN) {
86 evt.status = osEventTimeout;
87 } else if (retval == -ENOMSG) {
88 evt.status = osOK;
89 }
90
91 evt.def.message_id = queue_id;
92
93 return evt;
94 }
95