Run Once Thread

From Nutwiki
Revision as of 12:20, 2 August 2010 by AdrianPyka (Talk) (Test Environment)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Test Environment

Hardware Comments Nut/OS
4.8.3
Nut/OS
4.8.7
Ethernut 1.3 H OK
Binaries
Compiler: AVR-GCC 4.3.2
OK
Binaries
Compiler: AVR-GCC 4.3.2
Ethernut 2.1 B OK
Binaries
Compiler: AVR-GCC 4.3.2
OK
Binaries
Compiler: AVR-GCC 4.3.2
Ethernut 3.0 E OK
Binaries
Compiler: ARM-GCC 4.3.3
OK
Binaries
Compiler: ARM-GCC 4.3.3

This sample should run on all platforms. I actually used:

Nut/OS: Version 4.1.0

Hardware: Ethernut 2.1B with ATmega2561.

Compiler: avr-gcc (GCC) 4.1.2 (WinAVR 20070525)

Description

This sample demonstrates how to terminate a running thread.

When started, the program enters an endless loop, which continuously creates a thread and then sleeps for 1.5 seconds. As soon as the main loop is sleeping, the new thread starts, sleeps for 1 second and then terminates.


Source Code

<source lang="c">

  1. include <stdio.h>
  2. include <io.h>
  1. include <dev/board.h>
  1. include <sys/thread.h>
  2. include <sys/timer.h>

/*

* Temporary thread.
*/

THREAD(RunOnce, arg) {

   puts("Started");
   NutSleep(1000);
   puts("Terminating\n");
   NutThreadExit();
   for(;;); /* Avoids compiler warning. */

}

/*

* Application entry. 
*/

int main(void) {

   u_long baud = 115200;
   /* Enable standard output. */
   NutRegisterDevice(&DEV_UART, 0, 0);
   freopen(DEV_UART_NAME, "w", stdout);
   _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
   puts("\nRun Once Thread Test");


   /* Endless loop. */
   for (;;) {
       puts("Creating");
       NutThreadCreate("once", RunOnce, 0, 256);
       NutSleep(1500);
   }
   return 0;  /* Avoids compiler warning. */

} </source>

Building the Binaries

  1. If not done, create a sample directory with the Configurator.
  2. In the sample directory create a new directory named runonce.
  3. In the new directory create a new file named runonce.c. Use a plain text editor.
  4. Copy the source above into the new file. Use the clipboard.
  5. Save the new file.
  6. Create another file named Makefile in the same directory. Again use your text editor.
  7. Copy the sample Makefile given below.
  8. Replace any leading spaces with a tab character. This is important.
  9. Save the Makefile.
  10. Open a command line window and navigate to the newly created directory.
  11. On Windows PCs, make sure that your environment is properly set (example is given below).
  12. On the command line enter make

Among others, the files runonce.bin, runonce.hex and runonce.elf will be created. Use the proper method, depending on your hardware, to upload one of these to the target board.

Contents of the Makefile (spaces in front of -rm must be replaced by a tab character):

PROJ = runonce
include ../Makedefs
SRCS =  $(PROJ).c
OBJS =  $(SRCS:.c=.o)
LIBS =  $(LIBDIR)/nutinit.o -lnutcrt -lnutos -lnutdev -lnutarch $(ADDLIBS)
TARG =  $(PROJ).hex
all: $(OBJS) $(TARG) $(ITARG) $(DTARG)
include ../Makerules
clean:
       -rm -f $(OBJS)
       -rm -f $(TARG) $(ITARG) $(DTARG)
       -rm -f $(PROJ).eep
       -rm -f $(PROJ).obj
       -rm -f $(PROJ).map
       -rm -f $(SRCS:.c=.lst)
       -rm -f $(SRCS:.c=.bak)
       -rm -f $(SRCS:.c=.i)

On Windows use the following command shows how to setup your environment. This is just an example. The actual paths on your computer will be different, if you installed Nut/OS or WinAVR in another location.

SET PATH=C:\ethernut-4.4.0\nut\tools\win32;C:\WinAVR\bin;C:\WinAVR\utils\bin;%PATH%

The expected output when running make is similar to

avr-gcc -c -mmcu=atmega2561 -Os -fno-delete-null-pointer-checks 
 -Wall -Wstrict-prototypes -Wa,-ahlms=runonce.lst -DETHERNUT2
 -D__HARVARD_ARCH__ -DATMega2561 -I../../nutbld-enut21b-256gcc/include
 -I../../nut/include  runonce.c -o runonce.o
avr-gcc runonce.o -mmcu=atmega2561 -Wl,--defsym=main=0,-Map=runonce.map,
 --cref -L../../nutbld-enut21b-256gcc/lib -Wl,--start-group
 ../../nutbld-enut21b-256gcc/lib/nutinit.o -lnutcrt -lnutos -lnutdev 
 -lnutarch  -Wl,--end-group -o runonce.elf
avr-objcopy -R .eeprom -O ihex runonce.elf runonce.hex
avr-objcopy -O binary runonce.elf runonce.bin

Expected Program Output

On stdout (default UART at 115.2 kBaud, 8n1) the following output should be seen:

Run Once Thread Test
Creating
Started
Terminating

Creating
Started
Terminating

Creating
Started
Terminating

Creating
Started

See also