samples: cmsis_rtos_v2: Demo on msgq and timers synchronization

The sample application demonstrates usage of threads, message queue
and timer APIs in CMSIS RTOS V2.

Signed-off-by: Spoorthi K <spoorthi.k@intel.com>
This commit is contained in:
Spoorthi K 2018-12-21 10:34:54 +05:30 committed by Carles Cufí
commit b1e912bdb3
5 changed files with 201 additions and 0 deletions

View file

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.8.2)
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(cpp_synchronization)
target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/include/cmsis_rtos_v2)
target_sources(app PRIVATE src/main.c)

View file

@ -0,0 +1,65 @@
.. _cmsis_rtos_v2-sync_sample:
Synchronization using CMSI RTOS V2 APIs
#######################################
Overview
********
The sample project illustrates usage of timers and message queues using
CMSIS RTOS V2 APIs.
The main thread creates a preemptive thread which writes message to message queue
and on timer expiry, message is read by main thread.
Building and Running Project
****************************
This project outputs to the console. It can be built and executed
on QEMU as follows:
.. zephyr-app-commands::
:zephyr-app: samples/philosophers
:host-os: unix
:board: qemu_x86
:goals: run
:compact:
Sample Output
=============
.. code-block:: console
Wrote to message queue: 5
Read from message queue: 5
Wrote to message queue: 6
Read from message queue: 6
Wrote to message queue: 7
Read from message queue: 7
Wrote to message queue: 8
Read from message queue: 8
Wrote to message queue: 9
Read from message queue: 9
Wrote to message queue: 10
Read from message queue: 10
Wrote to message queue: 11
Read from message queue: 11
Wrote to message queue: 12
Read from message queue: 12
Wrote to message queue: 13
Read from message queue: 13
Wrote to message queue: 14
Read from message queue: 14
Wrote to message queue: 15
Read from message queue: 15
Sample execution successful

View file

@ -0,0 +1,13 @@
CONFIG_STDOUT_CONSOLE=n
CONFIG_SYS_CLOCK_TICKS_PER_SEC=100
CONFIG_ASSERT=y
CONFIG_ASSERT_LEVEL=2
CONFIG_CMSIS_RTOS_V2=y
CONFIG_NUM_PREEMPT_PRIORITIES=56
CONFIG_HEAP_MEM_POOL_SIZE=256
CONFIG_THREAD_NAME=y
CONFIG_THREAD_STACK_INFO=y
CONFIG_THREAD_MONITOR=y
CONFIG_INIT_STACKS=y
CONFIG_POLL=y
CONFIG_SCHED_SCALABLE=y

View file

@ -0,0 +1,15 @@
sample:
name: CMSIS_RTOS_V2 Syncronization
tests:
test:
tags: cmsis_rtos_v2_syncronization
min_ram: 32
min_flash: 34
harness: console
harness_config:
type: multi_line
ordered: true
regex:
- "Wrote to message queue: (.*)"
- "Read from message queue: (.*)"
- "Sample execution successful"

View file

@ -0,0 +1,102 @@
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file Synchronization demo using CMSIS RTOS V2 APIs.
*/
#include <zephyr.h>
#include <cmsis_os2.h>
/* specify delay between greetings (in ms); compute equivalent in ticks */
#define TIMER_TICKS 50
#define MSGLEN 12
#define Q_LEN 1
#define INITIAL_DATA_VALUE 5
const osTimerAttr_t timer_attr = {
"myTimer", 0, NULL, 0U
};
osMessageQueueId_t message_id;
u32_t data;
static char __aligned(4) sample_mem[sizeof(data) * Q_LEN];
static const osMessageQueueAttr_t mem_attrs = {
.name = "SampleMsgQ",
.attr_bits = 0,
.cb_mem = NULL,
.cb_size = 0,
.mq_mem = sample_mem,
.mq_size = sizeof(data) * Q_LEN,
};
void read_msg_callback(void *arg)
{
u32_t read_msg;
osStatus_t status;
status = osMessageQueueGet(message_id, (void *)&read_msg,
NULL, 0);
if (status == osOK) {
printk("Read from message queue: %d\n\n", read_msg);
} else {
printk("\n**Error reading message from message queue**\n");
}
}
void send_msg_thread(void)
{
osStatus_t status;
status = osMessageQueuePut(message_id, &data, 0, osWaitForever);
if (osMessageQueueGetCount(message_id) == 1 && status == osOK) {
printk("Wrote to message queue: %d\n", data);
} else {
printk("\n**Error sending message to message queue**\n");
}
}
void main(void)
{
osTimerId_t timer_id;
osStatus_t status;
u32_t counter = 10;
data = INITIAL_DATA_VALUE;
message_id = osMessageQueueNew(Q_LEN, sizeof(data), &mem_attrs);
status = osMessageQueuePut(message_id, &data, 0, osWaitForever);
if (osMessageQueueGetCount(message_id) == 1 && status == osOK) {
printk("Wrote to message queue: %d\n", data);
} else {
printk("\n**Error sending message to message queue**\n");
}
timer_id = osTimerNew(read_msg_callback, osTimerPeriodic, NULL,
&timer_attr);
osTimerStart(timer_id, TIMER_TICKS);
while (--counter) {
data++;
send_msg_thread();
}
/* Let the last message be read */
osDelay(TIMER_TICKS);
osTimerStop(timer_id);
osTimerDelete(timer_id);
if (counter == 0) {
printk("Sample execution successful\n");
} else {
printk("Error in execution! \n");
}
}