Hello World!

From NutWiki

Jump to: navigation, search

Contents

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
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

#include <dev/board.h>
#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;
}

Details

#include <dev/board.h>

Includes a header file, which contains board specific definitions. This helps to make the program portable on all supported target platforms.

#include <stdio.h>

Includes another header file. You need this if your code calls standard input and output functions.

int main(void)

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.

NutRegisterDevice(&DEV_DEBUG, 0, 0);
freopen(DEV_DEBUG_NAME, "w", stdout);

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.

unsigned long baud = 115200;
 
_ioctl(_fileno(stdout), UART_SETSPEED, &baud);

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.

printf("Hello World!");

Puts out the phrase "Hello World!" to standard output.

for(;;);
return 0;

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

TeraTerm serial port setup
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.


Personal tools