| /* main.c - Synchronisation 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> |
| |
| #define SYS_LOG_LEVEL SYS_LOG_LEVEL_INFO |
| #include <misc/sys_log.h> |
| |
| /* |
| * 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(&nanoSemFiber, TICKS_UNLIMITED); |
| |
| /* say "hello" */ |
| SYS_LOG_INF("Hello World!"); |
| |
| /* wait a while, then let task have a turn */ |
| nano_fiber_timer_start(&timer, SLEEPTICKS); |
| nano_fiber_timer_test(&timer, TICKS_UNLIMITED); |
| 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" */ |
| SYS_LOG_INF("Hello World!"); |
| |
| /* wait a while, then let fiber have a turn */ |
| nano_task_timer_start(&timer, SLEEPTICKS); |
| nano_task_timer_test(&timer, TICKS_UNLIMITED); |
| nano_task_sem_give(&nanoSemFiber); |
| |
| /* now wait for fiber to let us have a turn */ |
| nano_task_sem_take(&nanoSemTask, TICKS_UNLIMITED); |
| } |
| } |
| |