Implements a driver for USART0, which is connected to the host.
More...
#include "usart.h"
#include "yarf.h"
#include "fastio.h"
#include <stdint.h>
#include <stdio.h>
#include <math.h>
#include <avr/interrupt.h>
#include <util/setbaud.h>
Go to the source code of this file.
Defines |
#define | BAUD HOST_BAUD_RATE |
| The baud rate, which must be defined before loading the <util/setbaud.h> helper macros.
|
#define | ERROR_CHARACTER ((char)0xFE) |
| A character which can be inserted into the input buffer to signal the occurrence of an error while receiving a character.
|
Functions |
static uint8_t | buf_index_next (uint8_t index) |
| Returns the successor index of the given index in the input buffer.
|
static uint8_t | buf_index_prev (uint8_t index) |
| Returns the predecessor index of the given index in the input buffer.
|
static bool | buf_full (void) |
| Returns whether the input buffer is full.
|
static bool | buf_empty (void) |
| Returns whether the input buffer is empty.
|
static void | usart0_init (void) |
| Initializes the USART0 hardware.
|
static int | usart0_putchar (char c, FILE *stream) |
| Sends a character to the USART hardware buffer and waits until it has been transmitted.
|
static int | usart0_getchar (FILE *stream) |
| Returns the first unread character from the USART.
|
static void | read_character (void) |
| Reads a character from the USART hardware buffer and appends it to the input buffer.
|
| ISR (USART0_RX_vect, ISR_BLOCK) |
| Interrupt service routine which is called whenever a new character has arrived in the USART hardware buffer.
|
void | usart_init (void) |
| Initializes the usart component.
|
void | usart0_try_read_character (void) |
| If a character is waiting in the USART hardware buffer, this function reads that character and places it into the input buffer.
|
Variables |
static volatile char | rx_buffer [USART0_RECEIVE_BUFFER_SIZE] |
| Array for buffering incoming characters.
|
static volatile uint8_t | rxbuf_head |
| The index of the head of the input buffer.
|
static volatile uint8_t | rxbuf_tail |
| The index of the tail of the input buffer.
|
FILE | usart0_stream = FDEV_SETUP_STREAM(usart0_putchar, usart0_getchar, _FDEV_SETUP_RW) |
| Stream which can be used to read from and write to the USART.
|
Detailed Description
Implements a driver for USART0, which is connected to the host.
- Author:
- Pieter Agten (pieter.agten@gmail.com)
- Date:
- 18 sep 2011
Definition in file usart.c.
Define Documentation
#define BAUD HOST_BAUD_RATE |
The baud rate, which must be defined before loading the <util/setbaud.h> helper macros.
- See also:
- config.h
Definition at line 46 of file usart.c.
#define ERROR_CHARACTER ((char)0xFE) |
A character which can be inserted into the input buffer to signal the occurrence of an error while receiving a character.
This can be any non-ASCII character.
Definition at line 54 of file usart.c.
Function Documentation
static bool buf_empty |
( |
void |
| ) |
[inline, static] |
Returns whether the input buffer is empty.
- Returns:
- whether the input buffer is empty
Definition at line 122 of file usart.c.
static bool buf_full |
( |
void |
| ) |
[inline, static] |
Returns whether the input buffer is full.
- Returns:
- whether the input buffer is full
Definition at line 111 of file usart.c.
static uint8_t buf_index_next |
( |
uint8_t |
index | ) |
[inline, static] |
Returns the successor index of the given index in the input buffer.
- Parameters:
-
index | the index of which to return the successor |
- Returns:
- the successor index of the given index in the input buffer
Definition at line 80 of file usart.c.
static uint8_t buf_index_prev |
( |
uint8_t |
index | ) |
[inline, static] |
Returns the predecessor index of the given index in the input buffer.
- Parameters:
-
index | the index of which to return the predecessor |
- Returns:
- the predecessor index of the given index in the input buffer
Definition at line 96 of file usart.c.
static void read_character |
( |
void |
| ) |
[inline, static] |
Reads a character from the USART hardware buffer and appends it to the input buffer.
If the hardware buffer is full or an error occurred while reading or receiving the character, a special ERROR_CHARACTER is written to the input buffer at the position of the last character previously received.
Definition at line 223 of file usart.c.
static int usart0_getchar |
( |
FILE * |
stream | ) |
[static] |
Returns the first unread character from the USART.
- Parameters:
-
stream | a reference to the stream to which the character will be written |
- Returns:
- the first unread character from the USART or _FDEV_ERR if there was a problem reading the character
The actual reception is line buffered, and one character is returned from the input buffer at each invokation of this function. If the buffer is empty, this function will wait for a new character to arrive. If there was a problem reading the character to be returned, the function returns the _FDEV_ERR error character.
Definition at line 198 of file usart.c.
static int usart0_putchar |
( |
char |
c, |
|
|
FILE * |
stream |
|
) |
| [static] |
Sends a character to the USART hardware buffer and waits until it has been transmitted.
- Parameters:
-
c | the character to transmit |
stream | a reference to the underlying stream object from which the character is sent |
- Returns:
- 0 on success and a nonzero value if the character could not be transmitted
Definition at line 166 of file usart.c.
void usart0_try_read_character |
( |
void |
| ) |
|
If a character is waiting in the USART hardware buffer, this function reads that character and places it into the input buffer.
This function can be called from within long interrupt service routines that have higher priority than the USART0_RX interrupt, in order to prevent the hardware buffer from overrunning.
Definition at line 262 of file usart.c.
Variable Documentation
volatile char rx_buffer[USART0_RECEIVE_BUFFER_SIZE] [static] |
Array for buffering incoming characters.
This array is used as a circular buffer.
Definition at line 61 of file usart.c.
The index of the head of the input buffer.
This is where new characters will be inserted.
Definition at line 66 of file usart.c.
The index of the tail of the input buffer.
This is where the next character will be fetched from, when reading from the input.
Definition at line 71 of file usart.c.