Hello World!

From Nutwiki
Jump to: navigation, search

Test Environments

Hardware Comments Nut/OS
4.6.3
Nut/OS
4.6.4
Nut/OS
4.7.4
Nut/OS
4.8.0
Nut/OS
4.8.7
Ethernut 1.3 H OK OK
Binaries
OK
Binaries
OK
Binaries
OK
Binaries
Compiler: AVR-GCC 4.3.2
Ethernut 2.1 B OK OK
Binaries
OK
Binaries
OK
Binaries
OK
Binaries
Compiler: AVR-GCC 4.3.2
Ethernut 3.0 E OK OK
Binaries
OK
Binaries
OK
Binaries
OK
Binaries
Compiler: ARM-GCC 4.3.3
EIR 1.0 C Set jumper JP1
to DEBUG mode.
OK
OK
Binaries
OK
Binaries
OK
Binaries
Compiler: ARM-GCC 4.2.2 ; AVR-GCC 4.3.0

Description

This very basic sample program puts out the phrase "Hello World" to the serial port.

Source Code

<source lang="c">

  1. include <dev/board.h>
  2. include <stdio.h>

int main(void) {

   unsigned long baud = 115200;

   NutRegisterDevice(&DEV_DEBUG, 0, 0);
   freopen(DEV_DEBUG_NAME, "w", stdout);
   _ioctl(_fileno(stdout), UART_SETSPEED, &baud);

   printf("Hello World!");

   for(;;);

   return 0;

} </source>

Details

<source lang="c">#include <dev/board.h></source> Includes a header file, which contains board specific definitions. This helps to make the program portable on all supported target platforms.

<source lang="c">#include <stdio.h></source> Includes another header file. You need this if your code calls standard input and output functions.

<source lang="c">int main(void)</source> Declares a C function named main. More advanced applications will have a large number of functions, but they always must declare a main function, which will be executed on startup.

<source lang="c"> NutRegisterDevice(&DEV_DEBUG, 0, 0); freopen(DEV_DEBUG_NAME, "w", stdout); </source> Nut/OS doesn't provide a default device for standard output. If an application wants to write to the standard output via printf for example, it must assign a registered driver to the stdout stream. Furthermore, a device drivers must be registered before it can be used.

The first line registers a driver for debug output and the second line assigns this driver to the stdout stream.

DEV_DEBUG and DEV_DEBUG_NAME are defined in the header file dev/board.h and are available for all target boards. On the AVR this is the first USART, while DBGU is used on AT91 boards. On the Gameboy however, the LCD is used.

One specific feature of DEV_DEBUG is, that it is a polling device. That means, that its driver is quite simple, easy to implement and you can even use it for printing data in interrupt routines.

In most case it is write-only. You cannot use it for user input. Trying to do so will simply crash the system. If you need to read user input, then try

<source lang="c"> NutRegisterDevice(&DEV_CONSOLE, 0, 0); freopen(DEV_CONSOLE_NAME, "w", stdout); </source>

However, this is available on later Nut/OS releases only and may use a different hardware interface. Furthermore it is interrupt driven, you cannot use it within interrupt routines. However, if you enabled NUT_DEV_DEBUG_READ in the Configurator and if the device supports input, then DEV_CONSOLE will become equal to DEV_DEBUG for most target boards.

Whatever serial port device you may use to say "Hello", the remaining code works with all of them.

<source lang="c"> unsigned long baud = 115200;

_ioctl(_fileno(stdout), UART_SETSPEED, &baud); </source>

A serial port like USART or DBGU needs to be configured, specifically its transfer rate, also known as baud rate. While most embedded systems can provide much higher rates, 115,200 bits per second is still the upper limit on most desktop computers.

The _ioctl() function is typically used in C programs to configure device specific settings. It expects a file handle, not a stream. Thus the function _fileno() is called to retrieve the file handle of the stdout stream.

Also note, that you can't directly pass configuration constanst to _ioctl(). Instead you must assign the value to a variable and use a pointer to this variable for the function argument.

<source lang="c">printf("Hello World!");</source> Puts out the phrase "Hello World!" to standard output.

<source lang="c"> for(;;); return 0; </source>

Once started, Nut/OS applications will never return. If they do, the result is unknown, so we put an endless loop before the return statement to avoid this.

Although never reached, the return 0 is sometimes necessary to get rid of a compiler warning. However, other compilers may spit out a warning about unreachable code.

Output

File:Teraterm-serial-port-setup.png
TeraTerm serial port setup

On most target boards the output is sent to the first serial port and can be viewed by connecting a terminal emulator like TeraTerm on Windows or miniterm on Linux.

Hello World!

See also

External Links

Hello world program A computer program that prints out "Hello world!".

C preprocessor A separate program invoked by the compiler as the first part of translation.

Standard streams The three standard I/O connections are called standard input, standard output and standard error.

NutRegisterDevice Register a specific device which will be used by a Nut/OS application.

Ethernut RS-232 Primer explains how to connect serial communication ports.

ioctl The most common use of ioctls is to control hardware devices.

File descriptor An abstract key for accessing a file or a device.


Template:Languages