My U[S]ART implementation

My USART software is based on the ATMEL application note AVR306 : Using the AVR® UART in C.

The original source code of the application note was changed to support receiver error and to support  some of the capabilities of the avr UART interface databit (5..8 ), stop bit (1-2) and parity check (none, odd and even).

The sources could be build using WinAVR-20090313 and AVRStudio 4.16.SP2 and the application runs on atmega328P and atmega88 (they use the internal 8 MHz oscillator).  With minor changes it's possible to use the software on every Atmega and Attiny microcontroller 

The test board  used is the Atmel STK500. PD0-PD1 pins of the STK500 are connected to the RX-TX pin of  the serial interface. The HyperTeminal, on Windows, or  screen application on Linux, was used to communicate by the serial line of the PC with the atmega328P[88]. 

The application contains a simple shell, that was added to give an easy way to send/receive commands to the atmega. The “commands“ could be interpreted like command line “applications” with input parameters (the standard argc argcv[] C language arguments). 

New commands could be added into the terminal application filling the ptCommandTbl command table. An entry in the command table is defined by the structure tCommandEntry. The structure is defined as follow:

typedef struct


  char * pcCmdName  ;                  

  char * pcCmdHelpMesg;                  

  void (* proc)(char * args);   

} tCommandEntry;

where :

pcCmdName  is the “name” of the command. It will be use by the  “shell” parser to identify the command.

pcCmdHelpMesg is the usage/help message. It will be print by the command help or if the command will contain invalid parameters.

proc is the execution procedure of the command. 

The  tCommandEntry entry, by default, contains only two “commands”: the help and the example command. The former is used to display the help message for a specific command or for all the commands defined in the command table. The latter is an example that shows how to add new commands in the table.

The example command explains how to insert a command and the facilities that could be used to parser command line parameters. To parser  the commands parameters, the source code gives two facilities:  the getopt function and the iCreateCommandLineArgs function. 

The former is a public domain implementation of the getopt POSIX function while the latter is used to parse a string and to obtain the argv and argc arguments. 

These functions give the illusion that the text, sent over the serial line, has been passed to a command line application. The functions are very useful. You can design and code new command functions on your cross-compiler OS . These functions could be used on every C compiler


(to be continued…)

The index page