avruart
Data Structures | Macros | Enumerations | Functions | Variables
uart.h File Reference
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <avr/io.h>
#include <util/atomic.h>
#include <util/setbaud.h>

Go to the source code of this file.

Data Structures

struct  DirBuff
 Presenting a circular buffer. More...
 
struct  CBuffer
 This holds the circular buffers. More...
 
struct  UARTcfg
 A struct to configure the UART. More...
 

Macros

#define F_CPU   16000000UL /* 16MHz ┬Ác */
 
#define BAUD   9600L
 
#define UARTAVR_VERSION_MAJOR   0
 Version major.
 
#define UARTAVR_VERSION_MINOR   3
 Version minor.
 
#define UARTAVR_VERSION_PATCH   0
 Version patch.
 
#define CR   "\n\r"
 Define the line ending you need. This one will work with minicom on linux. More...
 
#define CR_PRINTF   "\r"
 If set this sign well be send whenever puts_printf_UART() sees a newline character.
 
#define BUFFSIZE   64
 The buffer size for the RX and TX buffers.
 

Enumerations

enum  DIR_BUFFS { RX_BUFF, TX_BUFF }
 Identifier for direction buffer. More...
 

Functions

void cb_init (void)
 Initializes the circular buffer structure. More...
 
void get_direction_buffer (enum DIR_BUFFS dir, struct DirBuff **dbuff)
 Get a direction buffer struct from CBuffer. More...
 
uint8_t cb_pop (char *c, enum DIR_BUFFS dir)
 Get one byte from the circular buffer. More...
 
uint8_t cp_push (char c, enum DIR_BUFFS dir)
 Put one byte in the circular buffer. More...
 
void init_uart_cfg (struct UARTcfg *cfg)
 Init a cfg struct with the default values. More...
 
void init_UART (const struct UARTcfg *cfg)
 Initialize the UART on the microcontroller. More...
 
void put_UART (const char c)
 Send a single character. More...
 
void puts_UART (const char *s)
 Write a string to the UART buffer. More...
 
uint8_t get_UART (char *s)
 Retrieve one char from the buffer. More...
 
uint8_t gets_UART (char *s)
 Get all data from the circular buffer. More...
 
int puts_printf_UART (char c, FILE *stream)
 The FILE stream method for uartavr_stdout. More...
 

Variables

struct CBuffer cb
 

Detailed Description

Author
Christian Rapp
Date
2016
Precondition
This library was written and tested fo the Atmege328P microcontroller. You may need to change the registers if you want to port this to a different AVR.

uartavr is a simple interrupt driven UART implementation for Atmel AVRs. This library uses a circular buffer data structure to store data that should be send or was received. In general the user does not have to interact directly with this buffer as there are some convenience methods already available.

In order to use this implementation you have to use sei which enables interrupts by setting the global interrupt mask.

Macro Definition Documentation

§ CR

#define CR   "\n\r"

Define the line ending you need. This one will work with minicom on linux.

This will automatically appended to all strings that you want to send using puts_UART()

Enumeration Type Documentation

§ DIR_BUFFS

enum DIR_BUFFS

Identifier for direction buffer.

Enumerator
RX_BUFF 

RX Buffer identifier

TX_BUFF 

TX Buffer identifier

Function Documentation

§ cb_init()

void cb_init ( void  )

Initializes the circular buffer structure.

Warning
Do not call this function yourself. The init_UART() function takes care of this.

§ cb_pop()

uint8_t cb_pop ( char *  c,
enum DIR_BUFFS  dir 
)

Get one byte from the circular buffer.

Parameters
cPointer to char variable
dirGet the byte from the TX or RX buffer
Returns
0 If the byte has been retrieved, 1 if the buffer is empty

§ cp_push()

uint8_t cp_push ( char  c,
enum DIR_BUFFS  dir 
)

Put one byte in the circular buffer.

Parameters
cA char variable
dirPut the byte on the TX or RX buffer
Returns
0 If the byte has written successfully, 1 if the buffer is full

§ get_direction_buffer()

void get_direction_buffer ( enum DIR_BUFFS  dir,
struct DirBuff **  dbuff 
)

Get a direction buffer struct from CBuffer.

Parameters
dirRX or TX
dbuff** to a DirBuff struct

§ get_UART()

uint8_t get_UART ( char *  s)

Retrieve one char from the buffer.

Parameters
sPointer to a char variable
Returns
0 If character was retrieved, 1 otherwise

§ gets_UART()

uint8_t gets_UART ( char *  s)

Get all data from the circular buffer.

Parameters
sPointer an array which this function uses to store the data from cb
Returns
0 on success or 1 if something went wrong
Warning
Make sure your char array has enough room for the data in the circular buffer plus \0 as termination character. You can get the numbers of items in the buffer with DirBuff::items

§ init_UART()

void init_UART ( const struct UARTcfg cfg)

Initialize the UART on the microcontroller.

Parameters
cfgPointer to a UARTcfg struct with the configuration for the UART

Some configuration options are currently fixed. The UART will always be setup in 8N1 mode. This may be changed in the future.

This method also initializes a circular buffer. So there is no need to call cb_init() yourself.

§ init_uart_cfg()

void init_uart_cfg ( struct UARTcfg cfg)

Init a cfg struct with the default values.

Parameters
cfg

§ put_UART()

void put_UART ( const char  c)

Send a single character.

Parameters
cThe character to send

§ puts_printf_UART()

int puts_printf_UART ( char  c,
FILE *  stream 
)

The FILE stream method for uartavr_stdout.

Parameters
cThe character to write into the circular buffer
stream
Returns
Always 0 currently

init_UART() will bind uartavr_stdout FILE stream to stdout. This way everything that will be written to stdout (e.g. with printf) will effectively be written in the circular buffer of uartavr. You need to link your application against printf_ftl or printf_min to make this work.

See also
printf_examples.c in examples subfolder

§ puts_UART()

void puts_UART ( const char *  s)

Write a string to the UART buffer.

Parameters
sThe string you want to send

The macro CR will automatically appended to your string so you do not have to worry about this.

Variable Documentation

§ cb

struct CBuffer cb

Global instance of the circular buffer