zephyr/samples/microkernel/apps/hello_world/src/hello.c
Peter Mitsis 40eba989e2 semaphore: Simplify task_sem_take() API family
Changes the semaphore API so that the timeout parameter must be specified
when invoking task_sem_take() thereby obsoleting the following APIs:
	task_sem_take_wait()
	task_sem_take_wait_timeout()
	_task_sem_take()

Change-Id: I746d5c966a3b81ffe014333af51aa10ea8a63263
Signed-off-by: Peter Mitsis <peter.mitsis@windriver.com>
2016-02-05 20:25:05 -05:00

148 lines
3.5 KiB
C

/* hello.c - Hello World demo */
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <zephyr.h>
#if defined(CONFIG_STDOUT_CONSOLE)
#include <stdio.h>
#define PRINT printf
#else
#include <misc/printk.h>
#define PRINT printk
#endif
#ifdef CONFIG_MICROKERNEL
/*
* Microkernel version of hello world demo has two tasks that utilize
* semaphores and sleeps to take turns printing a greeting message at
* a controlled rate.
*/
/* specify delay between greetings (in ms); compute equivalent in ticks */
#define SLEEPTIME 500
#define SLEEPTICKS (SLEEPTIME * sys_clock_ticks_per_sec / 1000)
/*
*
* @param taskname task identification string
* @param mySem task's own semaphore
* @param otherSem other task's semaphore
*
*/
void helloLoop(const char *taskname, ksem_t mySem, ksem_t otherSem)
{
while (1) {
task_sem_take(mySem, TICKS_UNLIMITED);
/* say "hello" */
PRINT("%s: Hello World!\n", taskname);
/* wait a while, then let other task have a turn */
task_sleep(SLEEPTICKS);
task_sem_give(otherSem);
}
}
void taskA(void)
{
/* taskA gives its own semaphore, allowing it to say hello right away */
task_sem_give(TASKASEM);
/* invoke routine that allows task to ping-pong hello messages with taskB */
helloLoop(__func__, TASKASEM, TASKBSEM);
}
void taskB(void)
{
/* invoke routine that allows task to ping-pong hello messages with taskA */
helloLoop(__func__, TASKBSEM, TASKASEM);
}
#else /* CONFIG_NANOKERNEL */
/*
* Nanokernel version of hello world demo has a task and a fiber that utilize
* semaphores and timers to take turns printing a greeting message at
* a controlled rate.
*/
/* specify delay between greetings (in ms); compute equivalent in ticks */
#define SLEEPTIME 500
#define SLEEPTICKS (SLEEPTIME * sys_clock_ticks_per_sec / 1000)
#define STACKSIZE 2000
char __stack fiberStack[STACKSIZE];
struct nano_sem nanoSemTask;
struct nano_sem nanoSemFiber;
void fiberEntry(void)
{
struct nano_timer timer;
uint32_t data[2] = {0, 0};
nano_sem_init(&nanoSemFiber);
nano_timer_init(&timer, data);
while (1) {
/* wait for task to let us have a turn */
nano_fiber_sem_take_wait(&nanoSemFiber);
/* say "hello" */
PRINT("%s: Hello World!\n", __func__);
/* wait a while, then let task have a turn */
nano_fiber_timer_start(&timer, SLEEPTICKS);
nano_fiber_timer_wait(&timer);
nano_fiber_sem_give(&nanoSemTask);
}
}
void main(void)
{
struct nano_timer timer;
uint32_t data[2] = {0, 0};
task_fiber_start(&fiberStack[0], STACKSIZE,
(nano_fiber_entry_t) fiberEntry, 0, 0, 7, 0);
nano_sem_init(&nanoSemTask);
nano_timer_init(&timer, data);
while (1) {
/* say "hello" */
PRINT("%s: Hello World!\n", __func__);
/* wait a while, then let fiber have a turn */
nano_task_timer_start(&timer, SLEEPTICKS);
nano_task_timer_wait(&timer);
nano_task_sem_give(&nanoSemFiber);
/* now wait for fiber to let us have a turn */
nano_task_sem_take_wait(&nanoSemTask);
}
}
#endif /* CONFIG_MICROKERNEL || CONFIG_NANOKERNEL */