ASCII Serial Com
Serial communication library between computers, microcontrollers, FPGAs, etc. Uses only ASCII. Not the most efficient protocol, but meant to be easy to read
Loading...
Searching...
No Matches
avr_uart.h
Go to the documentation of this file.
1#ifndef AVR_UART_H
2#define AVR_UART_H
3
4/** \file */
5
6#include <avr/io.h>
7#include <stdint.h>
8
9/** \brief Initialize USART
10 *
11 * 8 bit, 1 stop bit, no parity bit
12 *
13 * \param uart_no: number of UART, like 0 or 1
14 *
15 * \param ubrr: 12 bits of uint16_t: should be clock / 16 / baud - 1
16 *
17 * \param rxIntEnable: 1 bit enable RX interrupt
18 *
19 */
20#define UART_Init(uart_no, ubrr, rxIntEnable) \
21 _UART_Init(uart_no, ubrr, rxIntEnable)
22
23/**
24 * Second level of macro makes sure parameters are expanded before subbing into
25 * string concatenation
26 */
27#define _UART_Init(uart_no, ubrr, rxIntEnable) \
28 UBRR##uart_no##H = (uint8_t)(ubrr >> 8) & 0xFF; \
29 UBRR##uart_no##L = (uint8_t)(ubrr)&0xFF; \
30 UCSR##uart_no##A = 0; \
31 UCSR##uart_no##C = (3 << UCSZ00); \
32 UCSR##uart_no##B = (1 << RXEN0) | (1 << TXEN0) | (rxIntEnable << RXCIE0);
33
34// Enable/disable USART_UDRE_vect interrupt that triggers when USART (transmit)
35// data register is empty i.e. ready for more data
36#define USART_enable_udre_interrupt(uart_no) \
37 UCSR##uart_no##B |= (1 << UDRIE##uart_no)
38#define USART_disable_udre_interrupt(uart_no) \
39 UCSR##uart_no##B &= ~(1 << UDRIE##uart_no)
40
41/** \brief Define the ISR for a USART to push rx bytes to a circular buffer
42 *
43 * Defines the interrupt handler for the given USART. The interrupt handler
44 * will push all rx bytes to the back of the circular buffer the user provides.
45 *
46 * ## Notes
47 *
48 * **DON'T USE A SEMICOLON AFTER THIS MACRO.**
49 *
50 * **Use atomic operations to remove data from the front of the circular
51 * buffer** like `CM_ATOMIC_BLOCK() {}`
52 *
53 * **Make sure to setup the USART with rx interrupt enabled:**
54 * `USART0_Init(<ubrr>,1)`
55 *
56 * **Make sure to turn enable the global interrupt flag:** `sei();`
57 *
58 * ## Parameters
59 *
60 * isr_name: USART_RX_vect for 328
61 *
62 * data_register: UDR0 for 328
63 *
64 * circular_buffer: a pointer to a circular_buffer_uint8 that you want received
65 * bytes pushed_back on.
66 *
67 */
68#define def_usart_isr_push_rx_to_circ_buf(isr_name, data_register, \
69 circular_buffer) \
70 ISR(USART_RX_vect) { circular_buffer_push_back_uint8(circular_buffer, UDR0); }
71
72#endif