uart 1.0
AVR mega series uart library
Loading...
Searching...
No Matches
uart.h File Reference

Header file with declarations and macros for hardware UART. More...

#include <stdio.h>
#include <avr/io.h>
#include <util/setbaud.h>
#include "../../common/enums/UART_enums.h"
Include dependency graph for uart.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define F_CPU   12000000UL
 System clock frequency definition.
#define UART_BAUDRATE   9600UL
 Default UART communication baud rate.
#define BAUD   UART_BAUDRATE
#define UART_DATASIZE   8
 Number of data bits per frame (5-8).
#define UART_PARITY   0
 Parity bit configuration.
#define UART_STOPBITS   1
 Number of stop bits per frame (1-2).
#define UART_RXC_ECHO
 Enables local echo of received characters.
#define UART_HANDSHAKE   0
 Flow control / handshaking mode.
#define UART_HANDSHAKE_XON   0x11
 XON character (transmit when ready to receive).
#define UART_HANDSHAKE_XOFF   0x13
 XOFF character (transmit when not ready to receive).
#define UART_STDMODE   1
 Standard I/O integration mode (printf/scanf).

Functions

void uart_init (void)
 Initialize the UART hardware interface with configured parameters.
void uart_disable (void)
 Disable the UART hardware interface and reset configuration.
char uart_putchar (char data)
 Transmit a single character via UART (blocking).
int uart_printf (char data, FILE *stream)
 UART printf stream handler for stdout redirection.
char uart_getchar (UART_Data *status)
 Blocking receive single character via UART.
UART_Data uart_scanchar (char *data)
 Non-blocking check for received UART data with error handling.
UART_Error uart_error_flags (void)
 Check and clear UART receive error flags.
int uart_scanf (FILE *stream)
 UART scanf stream handler for stdin redirection.
void uart_clear (void)
 Clear UART input stream errors and discard pending character.

Detailed Description

Header file with declarations and macros for hardware UART.

This file provides function prototypes, type definitions, and constants for hardware-based UART communication on AVR0 microcontrollers.

Author
g.raf
Date
2025-09-27
Version
1.0 Release
Note
This file mostly becomes part of larger projects and subject to the license specified in the repository. For updates and the complete revision history, see the GitHub repository.
See also
https://github.com/0x007e/hal-avr-mega "AVR ATmega GitHub Repository"

Macro Definition Documentation

◆ BAUD

#define BAUD   UART_BAUDRATE

◆ F_CPU

#define F_CPU   12000000UL

System clock frequency definition.

This macro defines the operating frequency of the microcontroller's clock in Hertz. It is used for timing calculations. The value should match the actual hardware clock frequency to ensure correct timing behavior.

◆ UART_BAUDRATE

#define UART_BAUDRATE   9600UL

Default UART communication baud rate.

Sets the default serial communication speed to 9600 baud. Common values are 9600, 19200, 38400, 57600, 115200.

Note
Override this macro before including uart.h for different communication speeds.

◆ UART_DATASIZE

#define UART_DATASIZE   8

Number of data bits per frame (5-8).

Configures the character length transmitted/received: Valid values: 5, 6, 7, 8, 9H, 9L (default: 8).

  • 5 = 5 Bits
  • 6 = 6 Bits
  • 7 = 7 Bits
  • 8 = 8 Bits
Note
8 data bits (8N1) is the most common serial configuration.

◆ UART_HANDSHAKE

#define UART_HANDSHAKE   0

Flow control / handshaking mode.

Configures flow control between communicating systems:

  • 0 = Disabled (default)
  • 1 = Software flow control (XON/XOFF)
  • 2 = Hardware flow control (RTS/CTS)
Note
Enables reliable data transfer when receiver buffer overflows.

◆ UART_HANDSHAKE_XOFF

#define UART_HANDSHAKE_XOFF   0x13

XOFF character (transmit when not ready to receive).

◆ UART_HANDSHAKE_XON

#define UART_HANDSHAKE_XON   0x11

XON character (transmit when ready to receive).

◆ UART_PARITY

#define UART_PARITY   0

Parity bit configuration.

Selects parity checking for error detection:

  • 0 = no parity, default
  • 1 = even parity
  • 2 = odd parity
Note
Parity adds one bit to each frame for basic error checking.

◆ UART_RXC_ECHO

#define UART_RXC_ECHO

Enables local echo of received characters.

When defined, each received character is automatically transmitted back through TX (echo effect). Useful for terminal applications.

Note
Disabled automatically if UART_TXCIE or UART_UDRIE interrupts are enabled.

◆ UART_STDMODE

#define UART_STDMODE   1

Standard I/O integration mode (printf/scanf).

Enables avr-libc stdio functions over UART:

  • 0 = None
  • 1 = printf + scanf (default)
  • 2 = printf only
  • 3 = scanf only
Note
Requires fdevopen() setup in application code.

◆ UART_STOPBITS

#define UART_STOPBITS   1

Number of stop bits per frame (1-2).

Configures stop bits signaling frame end:

  • 1 = 1 Bit (default)
  • 2 = 2 Bits
Note
Stop bits must be idle (high) level.

Function Documentation

◆ uart_clear()

void uart_clear ( void )

Clear UART input stream errors and discard pending character.

Calls clearerr(stdin) and getchar() to reset stream state and discard any buffered input. Used to recover from scanf() failures.

◆ uart_disable()

void uart_disable ( void )

Disable the UART hardware interface and reset configuration.

This function completely disables the USART peripheral by clearing TXEN/RXEN bits and all interrupt enables. Call before reconfiguring UART or entering power-save modes.

◆ uart_error_flags()

UART_Error uart_error_flags ( void )

Check and clear UART receive error flags.

Returns
UART_Error code: UART_None, UART_Frame, UART_Overrun, or UART_Parity.

Reads RXDATAH error bits (FERR, BUFOVF, PERR) and clears by reading RXDATAL. Returns first detected error or UART_None if no errors.

Here is the caller graph for this function:

◆ uart_getchar()

char uart_getchar ( UART_Data * status)

Blocking receive single character via UART.

Parameters
[out]statusPointer to receive UART_Data status (UART_Received/UART_Fault).
Returns
Received character byte.

Loops calling uart_scanchar() until data available or error occurs. Status indicates if data valid (UART_Received) or error (UART_Fault).

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uart_init()

void uart_init ( void )

Initialize the UART hardware interface with configured parameters.

This function configures the USART peripheral for polling-based operation:

  • Sets hardware handshake pins (RTS/CTS) if UART_HANDSHAKE==2
  • Calculates and applies baud rate using setbaud.h
  • Configures frame format: data bits, parity, stop bits
  • Enables TX/RX with optional RXC echo and stdio stream assignment

All configuration is derived from uart.h preprocessor macros.

Note
Call this function once during system initialization before using UART functions.
stdio streams (stdout/stdin) are assigned only when UART_STDMODE > 0 and no interrupts defined.

◆ uart_printf()

int uart_printf ( char data,
FILE * stream )

UART printf stream handler for stdout redirection.

Parameters
dataCharacter to transmit.
streamFILE stream pointer (unused).
Returns
Result of uart_putchar().

Internal callback used by avr-libc fdevopen() for printf() redirection. Only compiled when UART_STDMODE == 1 or 2 (write support).

Here is the call graph for this function:

◆ uart_putchar()

char uart_putchar ( char data)

Transmit a single character via UART (blocking).

Parameters
dataCharacter byte to transmit (0-255).
Returns
Always returns 0 (success indicator for stdio compatibility).

Polling implementation waits for DREIF (Data Register Empty) flag before writing to UDR register. Blocks until transmission completes.

Note
Only available when no TX interrupts defined (UART_TXCIE/UART_UDRIE).
Here is the caller graph for this function:

◆ uart_scanchar()

UART_Data uart_scanchar ( char * data)

Non-blocking check for received UART data with error handling.

Parameters
[out]dataPointer to store received byte (valid only if UART_Received returned).
Returns
UART_Data status: UART_Empty, UART_Received, or UART_Fault.

Checks RXCIF flag and validates frame using uart_error_flags(). Handles XON/XOFF software handshake if enabled. Echoes received data if UART_RXC_ECHO defined.

Note
Does NOT block. Returns immediately with status.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ uart_scanf()

int uart_scanf ( FILE * stream)

UART scanf stream handler for stdin redirection.

Parameters
streamFILE stream pointer (unused).
Returns
Received character as int (for stdio compatibility).

Internal callback used by avr-libc fdevopen() for scanf() redirection. Only compiled when UART_STDMODE == 1 or 3 (read support).

Here is the call graph for this function: