1 /*
2 * Copyright (c) 2018 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /**
8 * @file Synchronization demo using CMSIS RTOS V2 APIs.
9 */
10
11 #include <zephyr/kernel.h>
12 #include <cmsis_os2.h>
13
14 /* specify delay between greetings (in ms); compute equivalent in ticks */
15 #define TIMER_TICKS 50
16 #define MSGLEN 12
17 #define Q_LEN 1
18 #define INITIAL_DATA_VALUE 5
19
20 const osTimerAttr_t timer_attr = {
21 "myTimer", 0, NULL, 0U
22 };
23
24 osMessageQueueId_t message_id;
25
26 uint32_t data;
27
28 static char __aligned(4) sample_mem[sizeof(data) * Q_LEN];
29
30 static const osMessageQueueAttr_t mem_attrs = {
31 .name = "SampleMsgQ",
32 .attr_bits = 0,
33 .cb_mem = NULL,
34 .cb_size = 0,
35 .mq_mem = sample_mem,
36 .mq_size = sizeof(data) * Q_LEN,
37 };
38
read_msg_callback(void * arg)39 void read_msg_callback(void *arg)
40 {
41 uint32_t read_msg;
42 osStatus_t status;
43
44 status = osMessageQueueGet(message_id, (void *)&read_msg,
45 NULL, 0);
46 if (status == osOK) {
47 printk("Read from message queue: %d\n\n", read_msg);
48 } else {
49 printk("\n**Error reading message from message queue**\n");
50 }
51 }
52
send_msg_thread(void)53 int send_msg_thread(void)
54 {
55 osStatus_t status;
56
57 status = osMessageQueuePut(message_id, &data, 0, osWaitForever);
58 if (osMessageQueueGetCount(message_id) == 1 && status == osOK) {
59 printk("Wrote to message queue: %d\n", data);
60 return 0;
61 }
62 printk("\n**Error sending message to message queue**\n");
63 return 1;
64 }
65
main(void)66 int main(void)
67 {
68 osTimerId_t timer_id;
69 osStatus_t status;
70 uint32_t counter = 10U;
71
72 data = INITIAL_DATA_VALUE;
73
74 message_id = osMessageQueueNew(Q_LEN, sizeof(data), &mem_attrs);
75
76 status = osMessageQueuePut(message_id, &data, 0, osWaitForever);
77
78 if (osMessageQueueGetCount(message_id) == 1 && status == osOK) {
79 printk("Wrote to message queue: %d\n", data);
80 } else {
81 printk("\n**Error sending message to message queue**\n");
82 goto exit;
83 }
84
85 timer_id = osTimerNew(read_msg_callback, osTimerPeriodic, NULL,
86 &timer_attr);
87 osTimerStart(timer_id, TIMER_TICKS);
88
89 while (--counter) {
90 data++;
91 if (send_msg_thread()) {
92 /* Writing to message queue has failed */
93 break;
94 }
95 }
96 /* Let the last message be read */
97 osDelay(TIMER_TICKS);
98
99 osTimerStop(timer_id);
100 osTimerDelete(timer_id);
101
102 exit:
103 if (counter == 0U) {
104 printk("Sample execution successful\n");
105 } else {
106 printk("Error in execution! \n");
107 }
108 return 0;
109 }
110