You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
19 KiB
Groff
161 lines
19 KiB
Groff
4 years ago
|
.TH "Using the standard IO facilities" 3 "24 Jun 2019" "Version 2.0.0" "avr-libc" \" -*- nroff -*-
|
||
|
.ad l
|
||
|
.nh
|
||
|
.SH NAME
|
||
|
Using the standard IO facilities \- This project illustrates how to use the standard IO facilities (stdio) provided by this library. It assumes a basic knowledge of how the stdio subsystem is used in standard C applications, and concentrates on the differences in this library's implementation that mainly result from the differences of the microcontroller environment, compared to a hosted environment of a standard computer.
|
||
|
.PP
|
||
|
This demo is meant to supplement the \fBdocumentation\fP, not to replace it.
|
||
|
.SH "Hardware setup"
|
||
|
.PP
|
||
|
The demo is set up in a way so it can be run on the ATmega16 that ships with the STK500 development kit. The UART port needs to be connected to the RS-232 'spare' port by a jumper cable that connects PD0 to RxD and PD1 to TxD. The RS-232 channel is set up as standard input (\fCstdin\fP) and standard output (\fCstdout\fP), respectively.
|
||
|
.PP
|
||
|
In order to have a different device available for a standard error channel (\fCstderr\fP), an industry-standard LCD display with an HD44780-compatible LCD controller has been chosen. This display needs to be connected to port A of the STK500 in the following way:
|
||
|
.PP
|
||
|
\fBPort\fP\fBHeader\fP\fBFunction\fP A0 1 LCD D4 A1 2 LCD D5 A2 3 LCD D6 A3 4 LCD D7 A4 5 LCD R/~W A5 6 LCD E A6 7 LCD RS A7 8 unused GND9 GND VCC10Vcc
|
||
|
.PP
|
||
|
Wiring of the STK500Wiring of the STK500
|
||
|
.PP
|
||
|
The LCD controller is used in 4-bit mode, including polling the 'busy' flag so the R/~W line from the LCD controller needs to be connected. Note that the LCD controller has yet another supply pin that is used to adjust the LCD's contrast (V5). Typically, that pin connects to a potentiometer between Vcc and GND. Often, it might work to just connect that pin to GND, while leaving it unconnected usually yields an unreadable display.
|
||
|
.PP
|
||
|
Port A has been chosen as 7 pins are needed to connect the LCD, yet all other ports are already partially in use: port B has the pins for in-system programming (ISP), port C has the ports for JTAG (can be used for debugging), and port D is used for the UART connection.
|
||
|
.SH "Functional overview"
|
||
|
.PP
|
||
|
The project consists of the following files:
|
||
|
.PP
|
||
|
.IP "\(bu" 2
|
||
|
\fCstdiodemo.c\fP This is the main example file.
|
||
|
.IP "\(bu" 2
|
||
|
\fC\fBdefines.h\fP\fP Contains some global defines, like the LCD wiring
|
||
|
.IP "\(bu" 2
|
||
|
\fChd44780.c\fP Implementation of an HD44780 LCD display driver
|
||
|
.IP "\(bu" 2
|
||
|
\fC\fBhd44780.h\fP\fP Interface declarations for the HD44780 driver
|
||
|
.IP "\(bu" 2
|
||
|
\fClcd.c\fP Implementation of LCD character IO on top of the HD44780 driver
|
||
|
.IP "\(bu" 2
|
||
|
\fC\fBlcd.h\fP\fP Interface declarations for the LCD driver
|
||
|
.IP "\(bu" 2
|
||
|
\fCuart.c\fP Implementation of a character IO driver for the internal UART
|
||
|
.IP "\(bu" 2
|
||
|
\fC\fBuart.h\fP\fP Interface declarations for the UART driver
|
||
|
.PP
|
||
|
.SH "A code walkthrough"
|
||
|
.PP
|
||
|
.SS "stdiodemo.c"
|
||
|
As usual, include files go first. While conventionally, system header files (those in angular brackets \fC<\fP ... \fC>\fP) go before application-specific header files (in double quotes), \fC\fBdefines.h\fP\fP comes as the first header file here. The main reason is that this file defines the value of \fCF_CPU\fP which needs to be known before including \fC<utils/delay.h>\fP.
|
||
|
.PP
|
||
|
The function \fCioinit()\fP summarizes all hardware initialization tasks. As this function is declared to be module-internal only (\fCstatic\fP), the compiler will notice its simplicity, and with a reasonable optimization level in effect, it will inline that function. That needs to be kept in mind when debugging, because the inlining might cause the debugger to 'jump around wildly' at a first glance when single-stepping.
|
||
|
.PP
|
||
|
The definitions of \fCuart_str\fP and \fClcd_str\fP set up two stdio streams. The initialization is done using the \fC\fBFDEV_SETUP_STREAM()\fP\fP initializer template macro, so a static object can be constructed that can be used for IO purposes. This initializer macro takes three arguments, two function macros to connect the corresponding output and input functions, respectively, the third one describes the intent of the stream (read, write, or both). Those functions that are not required by the specified intent (like the input function for \fClcd_str\fP which is specified to only perform output operations) can be given as \fCNULL\fP.
|
||
|
.PP
|
||
|
The stream \fCuart_str\fP corresponds to input and output operations performed over the RS-232 connection to a terminal (e.g. from/to a PC running a terminal program), while the \fClcd_str\fP stream provides a method to display character data on the LCD text display.
|
||
|
.PP
|
||
|
The function \fCdelay_1s()\fP suspends program execution for approximately one second. This is done using the \fC\fB_delay_ms()\fP\fP function from \fC<\fButil/delay.h\fP>\fP which in turn needs the \fCF_CPU\fP macro in order to adjust the cycle counts. As the \fC\fB_delay_ms()\fP\fP function has a limited range of allowable argument values (depending on \fCF_CPU\fP), a value of 10 ms has been chosen as the base delay which would be safe for CPU frequencies of up to about 26 MHz. This function is then called 100 times to accomodate for the actual one-second delay.
|
||
|
.PP
|
||
|
In a practical application, long delays like this one were better be handled by a hardware timer, so the main CPU would be free for other tasks while waiting, or could be put on sleep.
|
||
|
.PP
|
||
|
At the beginning of \fCmain()\fP, after initializing the peripheral devices, the default stdio streams \fCstdin\fP, \fCstdout\fP, and \fCstderr\fP are set up by using the existing static \fCFILE\fP stream objects. While this is not mandatory, the availability of \fCstdin\fP and \fCstdout\fP allows to use the shorthand functions (e.g. \fC\fBprintf()\fP\fP instead of \fC\fBfprintf()\fP\fP), and \fCstderr\fP can mnemonically be referred to when sending out diagnostic messages.
|
||
|
.PP
|
||
|
Just for demonstration purposes, \fCstdin\fP and \fCstdout\fP are connected to a stream that will perform UART IO, while \fCstderr\fP is arranged to output its data to the LCD text display.
|
||
|
.PP
|
||
|
Finally, a main loop follows that accepts simple 'commands' entered via the RS-232 connection, and performs a few simple actions based on the commands.
|
||
|
.PP
|
||
|
First, a prompt is sent out using \fC\fBprintf_P()\fP\fP (which takes a \fBprogram space string\fP). The string is read into an internal buffer as one line of input, using \fC\fBfgets()\fP\fP. While it would be also possible to use \fC\fBgets()\fP\fP (which implicitly reads from \fCstdin\fP), \fC\fBgets()\fP\fP has no control that the user's input does not overflow the input buffer provided so it should never be used at all.
|
||
|
.PP
|
||
|
If \fC\fBfgets()\fP\fP fails to read anything, the main loop is left. Of course, normally the main loop of a microcontroller application is supposed to never finish, but again, for demonstrational purposes, this explains the error handling of stdio. \fC\fBfgets()\fP\fP will return NULL in case of an input error or end-of-file condition on input. Both these conditions are in the domain of the function that is used to establish the stream, \fCuart_putchar()\fP in this case. In short, this function returns EOF in case of a serial line 'break' condition (extended start condition) has been recognized on the serial line. Common PC terminal programs allow to assert this condition as some kind of out-of-band signalling on an RS-232 connection.
|
||
|
.PP
|
||
|
When leaving the main loop, a goodbye message is sent to standard error output (i.e. to the LCD), followed by three dots in one-second spacing, followed by a sequence that will clear the LCD. Finally, \fCmain()\fP will be terminated, and the library will add an infinite loop, so only a CPU reset will be able to restart the application.
|
||
|
.PP
|
||
|
There are three 'commands' recognized, each determined by the first letter of the line entered (converted to lower case):
|
||
|
.PP
|
||
|
.IP "\(bu" 2
|
||
|
The 'q' (quit) command has the same effect of leaving the main loop.
|
||
|
.IP "\(bu" 2
|
||
|
The 'l' (LCD) command takes its second argument, and sends it to the LCD.
|
||
|
.IP "\(bu" 2
|
||
|
The 'u' (UART) command takes its second argument, and sends it back to the UART connection.
|
||
|
.PP
|
||
|
.PP
|
||
|
Command recognition is done using \fC\fBsscanf()\fP\fP where the first format in the format string just skips over the command itself (as the assignment suppression modifier \fC*\fP is given).
|
||
|
.SS "defines.h"
|
||
|
This file just contains a few peripheral definitions.
|
||
|
.PP
|
||
|
The \fCF_CPU\fP macro defines the CPU clock frequency, to be used in delay loops, as well as in the UART baud rate calculation.
|
||
|
.PP
|
||
|
The macro \fCUART_BAUD\fP defines the RS-232 baud rate. Depending on the actual CPU frequency, only a limited range of baud rates can be supported.
|
||
|
.PP
|
||
|
The remaining macros customize the IO port and pins used for the HD44780 LCD driver. Each definition consists of a letter naming the port this pin is attached to, and a respective bit number. For accessing the data lines, only the first data line gets its own macro (line D4 on the HD44780, lines D0 through D3 are not used in 4-bit mode), all other data lines are expected to be in ascending order next to D4.
|
||
|
.SS "hd44780.h"
|
||
|
This file describes the public interface of the low-level LCD driver that interfaces to the HD44780 LCD controller. Public functions are available to initialize the controller into 4-bit mode, to wait for the controller's busy bit to be clear, and to read or write one byte from or to the controller.
|
||
|
.PP
|
||
|
As there are two different forms of controller IO, one to send a command or receive the controller status (RS signal clear), and one to send or receive data to/from the controller's SRAM (RS asserted), macros are provided that build on the mentioned function primitives.
|
||
|
.PP
|
||
|
Finally, macros are provided for all the controller commands to allow them to be used symbolically. The HD44780 datasheet explains these basic functions of the controller in more detail.
|
||
|
.SS "hd44780.c"
|
||
|
This is the implementation of the low-level HD44780 LCD controller driver.
|
||
|
.PP
|
||
|
On top, a few preprocessor glueing tricks are used to establish symbolic access to the hardware port pins the LCD controller is attached to, based on the application's definitions made in \fBdefines.h\fP.
|
||
|
.PP
|
||
|
The \fChd44780_pulse_e()\fP function asserts a short pulse to the controller's E (enable) pin. Since reading back the data asserted by the LCD controller needs to be performed while E is active, this function reads and returns the input data if the parameter \fCreadback\fP is true. When called with a compile-time constant parameter that is false, the compiler will completely eliminate the unused readback operation, as well as the return value as part of its optimizations.
|
||
|
.PP
|
||
|
As the controller is used in 4-bit interface mode, all byte IO to/from the controller needs to be handled as two nibble IOs. The functions \fChd44780_outnibble()\fP and \fChd44780_innibble()\fP implement this. They do not belong to the public interface, so they are declared static.
|
||
|
.PP
|
||
|
Building upon these, the public functions \fChd44780_outbyte()\fP and \fChd44780_inbyte()\fP transfer one byte to/from the controller.
|
||
|
.PP
|
||
|
The function \fChd44780_wait_ready()\fP waits for the controller to become ready, by continuously polling the controller's status (which is read by performing a byte read with the RS signal cleard), and examining the BUSY flag within the status byte. This function needs to be called before performing any controller IO.
|
||
|
.PP
|
||
|
Finally, \fChd44780_init()\fP initializes the LCD controller into 4-bit mode, based on the initialization sequence mandated by the datasheet. As the BUSY flag cannot be examined yet at this point, this is the only part of this code where timed delays are used. While the controller can perform a power-on reset when certain constraints on the power supply rise time are met, always calling the software initialization routine at startup ensures the controller will be in a known state. This function also puts the interface into 4-bit mode (which would not be done automatically after a power-on reset).
|
||
|
.SS "lcd.h"
|
||
|
This function declares the public interface of the higher-level (character IO) LCD driver.
|
||
|
.SS "lcd.c"
|
||
|
The implementation of the higher-level LCD driver. This driver builds on top of the HD44780 low-level LCD controller driver, and offers a character IO interface suitable for direct use by the standard IO facilities. Where the low-level HD44780 driver deals with setting up controller SRAM addresses, writing data to the controller's SRAM, and controlling display functions like clearing the display, or moving the cursor, this high-level driver allows to just write a character to the LCD, in the assumption this will somehow show up on the display.
|
||
|
.PP
|
||
|
Control characters can be handled at this level, and used to perform specific actions on the LCD. Currently, there is only one control character that is being dealt with: a newline character (\fC\\n\fP) is taken as an indication to clear the display and set the cursor into its initial position upon reception of the next character, so a 'new line' of text can be displayed. Therefore, a received newline character is remembered until more characters have been sent by the application, and will only then cause the display to be cleared before continuing. This provides a convenient abstraction where full lines of text can be sent to the driver, and will remain visible at the LCD until the next line is to be displayed.
|
||
|
.PP
|
||
|
Further control characters could be implemented, e. g. using a set of escape sequences. That way, it would be possible to implement self-scrolling display lines etc.
|
||
|
.PP
|
||
|
The public function \fClcd_init()\fP first calls the initialization entry point of the lower-level HD44780 driver, and then sets up the LCD in a way we'd like to (display cleared, non-blinking cursor enabled, SRAM addresses are increasing so characters will be written left to right).
|
||
|
.PP
|
||
|
The public function \fClcd_putchar()\fP takes arguments that make it suitable for being passed as a \fCput()\fP function pointer to the stdio stream initialization functions and macros (\fC\fBfdevopen()\fP\fP, \fC\fBFDEV_SETUP_STREAM()\fP\fP etc.). Thus, it takes two arguments, the character to display itself, and a reference to the underlying stream object, and it is expected to return 0 upon success.
|
||
|
.PP
|
||
|
This function remembers the last unprocessed newline character seen in the function-local static variable \fCnl_seen\fP. If a newline character is encountered, it will simply set this variable to a true value, and return to the caller. As soon as the first non-newline character is to be displayed with \fCnl_seen\fP still true, the LCD controller is told to clear the display, put the cursor home, and restart at SRAM address 0. All other characters are sent to the display.
|
||
|
.PP
|
||
|
The single static function-internal variable \fCnl_seen\fP works for this purpose. If multiple LCDs should be controlled using the same set of driver functions, that would not work anymore, as a way is needed to distinguish between the various displays. This is where the second parameter can be used, the reference to the stream itself: instead of keeping the state inside a private variable of the function, it can be kept inside a private object that is attached to the stream itself. A reference to that private object can be attached to the stream (e.g. inside the function \fClcd_init()\fP that then also needs to be passed a reference to the stream) using \fC\fBfdev_set_udata()\fP\fP, and can be accessed inside \fClcd_putchar()\fP using \fBfdev_get_udata()\fP.
|
||
|
.SS "uart.h"
|
||
|
Public interface definition for the RS-232 UART driver, much like in \fBlcd.h\fP except there is now also a character input function available.
|
||
|
.PP
|
||
|
As the RS-232 input is line-buffered in this example, the macro \fCRX_BUFSIZE\fP determines the size of that buffer.
|
||
|
.SS "uart.c"
|
||
|
This implements an stdio-compatible RS-232 driver using an AVR's standard UART (or USART in asynchronous operation mode). Both, character output as well as character input operations are implemented. Character output takes care of converting the internal newline \fC\\n\fP into its external representation carriage return/line feed (\fC\\r\\n\fP).
|
||
|
.PP
|
||
|
Character input is organized as a line-buffered operation that allows to minimally edit the current line until it is 'sent' to the application when either a carriage return (\fC\\r\fP) or newline (\fC\\n\fP) character is received from the terminal. The line editing functions implemented are:
|
||
|
.PP
|
||
|
.IP "\(bu" 2
|
||
|
\fC\\b\fP (back space) or \fC\\177\fP (delete) deletes the previous character
|
||
|
.IP "\(bu" 2
|
||
|
^u (control-U, ASCII NAK) deletes the entire input buffer
|
||
|
.IP "\(bu" 2
|
||
|
^w (control-W, ASCII ETB) deletes the previous input word, delimited by white space
|
||
|
.IP "\(bu" 2
|
||
|
^r (control-R, ASCII DC2) sends a \fC\\r\fP, then reprints the buffer (refresh)
|
||
|
.IP "\(bu" 2
|
||
|
\fC\\t\fP (tabulator) will be replaced by a single space
|
||
|
.PP
|
||
|
.PP
|
||
|
The function \fCuart_init()\fP takes care of all hardware initialization that is required to put the UART into a mode with 8 data bits, no parity, one stop bit (commonly referred to as 8N1) at the baud rate configured in \fBdefines.h\fP. At low CPU clock frequencies, the \fCU2X\fP bit in the UART is set, reducing the oversampling from 16x to 8x, which allows for a 9600 Bd rate to be achieved with tolerable error using the default 1 MHz RC oscillator.
|
||
|
.PP
|
||
|
The public function \fCuart_putchar()\fP again has suitable arguments for direct use by the stdio stream interface. It performs the \fC\\n\fP into \fC\\r\\n\fP translation by recursively calling itself when it sees a \fC\\n\fP character. Just for demonstration purposes, the \fC\\a\fP (audible bell, ASCII BEL) character is implemented by sending a string to \fCstderr\fP, so it will be displayed on the LCD.
|
||
|
.PP
|
||
|
The public function \fCuart_getchar()\fP implements the line editor. If there are characters available in the line buffer (variable \fCrxp\fP is not \fCNULL\fP), the next character will be returned from the buffer without any UART interaction.
|
||
|
.PP
|
||
|
If there are no characters inside the line buffer, the input loop will be entered. Characters will be read from the UART, and processed accordingly. If the UART signalled a framing error (\fCFE\fP bit set), typically caused by the terminal sending a \fIline break\fP condition (start condition held much longer than one character period), the function will return an end-of-file condition using \fC_FDEV_EOF\fP. If there was a data overrun condition on input (\fCDOR\fP bit set), an error condition will be returned as \fC_FDEV_ERR\fP.
|
||
|
.PP
|
||
|
Line editing characters are handled inside the loop, potentially modifying the buffer status. If characters are attempted to be entered beyond the size of the line buffer, their reception is refused, and a \fC\\a\fP character is sent to the terminal. If a \fC\\r\fP or \fC\\n\fP character is seen, the variable \fCrxp\fP (receive pointer) is set to the beginning of the buffer, the loop is left, and the first character of the buffer will be returned to the application. (If no other characters have been entered, this will just be the newline character, and the buffer is marked as being exhausted immediately again.)
|
||
|
.SH "The source code"
|
||
|
.PP
|
||
|
.PP
|
||
|
|
||
|
.SH "Author"
|
||
|
.PP
|
||
|
Generated automatically by Doxygen for avr-libc from the source code.
|