porklib: Simple library for programming Arduino in C
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
avr-lib/lib/uart.h

69 lines
1.4 KiB

10 years ago
#pragma once
//
// Utilities for UART communication.
//
// First, init uart with desired baud rate using uart_init(baud).
// Then enable interrupts you want with uart_isr_XXX().
//
10 years ago
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdbool.h>
#include <stdint.h>
#include "stream.h"
// Shared UART stream object
// Can be used with functions from stream.h once UART is initialized
extern STREAM* uart;
10 years ago
/** Init UART for given baudrate */
void _uart_init_do(uint16_t ubrr); // internal, needed for the macro.
10 years ago
#define uart_init(baud) _uart_init_do(F_CPU / 16 / (baud) - 1)
/** Check if there's a byte in the RX register */
#define uart_rx_ready() (0 != (UCSR0A & (1 << RXC0)))
/** Check if transmission of everything is done */
#define uart_tx_ready() (0 != (UCSR0A & (1 << UDRE0)))
// Enable UART interrupts
10 years ago
/** Enable or disable RX ISR */
void uart_isr_rx(bool enable);
/** Enable or disable TX ISR (1 byte is sent) */
void uart_isr_tx(bool enable);
/** Enable or disable DRE ISR (all is sent) */
void uart_isr_dre(bool enable);
// Basic IO
/** Receive one byte over UART */
uint8_t uart_rx();
10 years ago
/** Send byte over UART */
#define uart_putc(data) uart_tx((data))
10 years ago
void uart_tx(uint8_t data);
/** Clear receive buffer */
void uart_flush();
// Strings
10 years ago
/** Send string over UART */
void uart_puts(const char* str);
/** Send progmem string over UART */
void uart_puts_P(const char* str);