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

Source file with implementation of polling-based UART functions and macros. More...

#include "uart.h"
Include dependency graph for uart.c:

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.
UART_Data uart_scanchar (char *data)
 Non-blocking check for received UART data with error handling.
char uart_getchar (UART_Data *status)
 Blocking receive single character via UART.
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.
UART_Error uart_error_flags (void)
 Check and clear UART receive error flags.

Detailed Description

Source file with implementation of polling-based UART functions and macros.

This file contains the definitions of function implementations for polling-based UART communication on AVR-Mega-Series microcontrollers. Supports configurable baud rates, frame formats, optional echo, and stdio integration (printf/scanf).

Author
g.raf
Date
2025-12-07
Version
1.0 Release
Note
This file is part of a larger project and subject to the license specified in the repository. For updates and the complete revision history, see the GitHub repository.
Important
Interrupts are NOT implemented. Define UART_RXCIE/UART_TXCIE/UART_UDRIE macros to disable polling functions for custom interrupt handling.
See also
uart.h for declarations, configuration macros, and related information.
uart_enums.h for UART status and error enumerations.
https://github.com/0x007e/hal-avr-mega "AVR ATmega GitHub Repository"

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: