This sample demonstrates the usage of the ATmega on-chip UART. Note, that we don't do any error checking, because without this UART we can't tell the user our problem.
We use floating points. Make sure to link with nutlibcrtf.
#include <cfg/crt.h>
#include <string.h>
#include <stdio.h>
#include <io.h>
#include <dev/uartavr.h>
#include <sys/timer.h>
static char *banner = "\nNut/OS UART Sample\n";
static prog_char presskey_P[] = "Press any key...";
static prog_char pgm_ptr[] = "\nHello stranger!\n";
static char inbuf[128];
int main(void)
{
int got;
int i;
char *cp;
u_long baud = 115200;
FILE *uart;
#ifdef STDIO_FLOATING_POINT
float dval = 0.0;
#endif
NutRegisterDevice(&devUart0, 0, 0);
uart = fopen("uart0", "r+");
_ioctl(_fileno(uart), UART_SETSPEED, &baud);
_write(_fileno(uart), banner, strlen(banner));
{
_write_P(_fileno(uart), presskey_P, sizeof(presskey_P));
}
_write(_fileno(uart), 0, 0);
got = _read(_fileno(uart), inbuf, sizeof(inbuf));
_write(_fileno(uart), inbuf, got);
for (i = 0;; i++) {
fputs("\nEnter your name: ", uart);
fflush(uart);
fgets(inbuf, sizeof(inbuf), uart);
cp = strchr(inbuf, '\n');
if (cp)
*cp = 0;
if (inbuf[0])
fprintf(uart, "\nHello %s!\n", inbuf);
else {
fputs_P(pgm_ptr, uart);
}
#ifdef STDIO_FLOATING_POINT
dval += 1.0125;
fprintf(uart, "FP %f\n", dval);
#endif
}
}