This sample demonstrates the usage of Nut/OS timer functions.
In addition it demonstrates how to enable system debug output. You need the debug version of the Nut/OS libraries. To create them, you must enable the RTOS Kernel - OS Debug option in the Configurator. Then use the Build Menu in the Configurator to generate the build tree again and build Nut/OS.
Note, that the debug version consumes much more memory than the original version. If in doubt, check the map file.
#include <cfg/os.h>
#ifdef NUTDEBUG
#include <sys/osdebug.h>
#endif
#include <stdio.h>
#include <io.h>
#include <cfg/arch.h>
#include <dev/debug.h>
#ifdef MCU_GBA
#define DEV_DEBUG_NAME "con"
#else
#define DEV_DEBUG_NAME "uart0"
#endif
#define DEV_DEBUG devDebug0
#include <sys/thread.h>
#include <sys/timer.h>
#include <sys/event.h>
#include <sys/heap.h>
void TimerCallback(HANDLE timer, void *arg)
{
NutEventPostAsync(arg);
}
THREAD(TimerEvent1, arg)
{
printf(" I1");
NutThreadSetPriority(4);
for (;;) {
if (NutEventWait(arg, 12500))
printf(" T1");
else
printf(" E1");
}
}
THREAD(TimerEvent2, arg)
{
printf(" I2");
NutThreadSetPriority(8);
for (;;) {
if (NutEventWait(arg, 12500))
printf(" T2");
else
printf(" E2");
}
}
THREAD(TimerEvent3, arg)
{
printf(" I3");
NutThreadSetPriority(16);
for (;;) {
if (NutEventWait(arg, 12500))
printf(" T3");
else
printf(" E3");
}
}
THREAD(TimerEvent4, arg)
{
printf(" I4");
NutThreadSetPriority(32);
for (;;) {
if (NutEventWait(arg, 12500))
printf(" T4");
else
printf(" E4");
}
}
THREAD(Sleeper1, arg)
{
NutThreadSetPriority(128);
for (;;) {
if (NutHeapAvailable() > 500)
printf("\n%u free ", NutHeapAvailable());
else
puts("Memory low");
NutSleep(500);
}
}
THREAD(Sleeper2, arg)
{
NutThreadSetPriority(129);
for (;;) {
NutSleep(500);
printf(" S2");
}
}
THREAD(Sleeper3, arg)
{
NutThreadSetPriority(130);
for (;;) {
NutSleep(500);
printf(" S3");
}
}
THREAD(Sleeper4, arg)
{
NutThreadSetPriority(131);
for (;;) {
NutSleep(500);
printf(" S4");
}
}
int main(void)
{
int seq;
u_long baud = 115200;
u_long sleep_ms = 2000;
u_long timer_ms = 125;
u_long cpu_crystal;
int one_shot;
HANDLE timer1, timer2, timer3, timer4;
HANDLE event1 = 0, event2 = 0, event3 = 0, event4 = 0;
NutRegisterDevice(&DEV_DEBUG, 0, 0);
freopen(DEV_DEBUG_NAME, "w", stdout);
_ioctl(_fileno(stdout), UART_SETSPEED, &baud);
#ifdef NUTDEBUG
NutTraceHeap(stdout, 1);
NutTraceOs(stdout, 1);
#endif
NutThreadSetPriority(8);
cpu_crystal = NutGetCpuClock();
puts("\n*******************************************************************************");
printf("Timer sample running on %u.%04u MHz CPU\n",
(int) (cpu_crystal / 1000000UL), (int) ((cpu_crystal - (cpu_crystal / 1000000UL) * 1000000UL) / 100)
);
NutThreadCreate("tmr1", TimerEvent1, &event1, 256);
NutThreadCreate("tmr2", TimerEvent2, &event2, 256);
NutThreadCreate("tmr3", TimerEvent3, &event3, 256);
NutThreadCreate("tmr4", TimerEvent4, &event4, 256);
NutThreadCreate("slpr1", Sleeper1, 0, 256);
NutThreadCreate("slpr2", Sleeper2, 0, 256);
NutThreadCreate("slpr3", Sleeper3, 0, 256);
NutThreadCreate("slpr4", Sleeper4, 0, 256);
for (seq = 0;; seq++) {
if (seq & 1)
one_shot = TM_ONESHOT;
else
one_shot = 0;
printf("\nStart %s t1 ", one_shot ? "oneshot" : "periodic");
timer1 = NutTimerStart(timer_ms, TimerCallback, &event1, one_shot);
printf("\nStart %s t2 ", one_shot ? "oneshot" : "periodic");
timer2 = NutTimerStart(timer_ms, TimerCallback, &event2, one_shot);
printf("\nStart %s t3 ", one_shot ? "oneshot" : "periodic");
timer3 = NutTimerStart(timer_ms, TimerCallback, &event3, one_shot);
printf("\nStart %s t4 ", one_shot ? "oneshot" : "periodic");
timer4 = NutTimerStart(timer_ms, TimerCallback, &event4, one_shot);
if ((sleep_ms += 1000) > 30000)
sleep_ms = 1000;
printf("\nSleeping %u seconds ", (int) (sleep_ms / 1000UL));
NutSleep(sleep_ms);
if (one_shot == 0) {
printf("\nStop timers ");
NutTimerStop(timer1);
NutTimerStop(timer2);
NutTimerStop(timer3);
NutTimerStop(timer4);
}
printf("\n%u bytes free\n", NutHeapAvailable());
}
}