Serial Interface

This library provides AVR asynchronous serial interface usage functions.

Data Types

  • usart
    Data type to hold USART inteface control, status and data register addresses. usart typed variables should be initialized at the beginning of the program code. For initializations, there is a macro-functions USART, whose only parameter is the index of interface (0 or 1).
  • usart_databits
    Data bits count enumeration data type. Options:
    • USART_DATABITS_5 - 5 data bits.
    • USART_DATABITS_6 - 6 data bits.
    • USART_DATABITS_7 - 7 data bits.
    • USART_DATABITS_8 - 8 data bits.
    • USART_DATABITS_9 - 9 data bits.
  • usart_stopbits
    Stop bits count enumeration data type. Options:
    • USART_STOPBITS_ONE - One stop bit.
    • USART_STOPBITS_TWO - Two stop bits.
  • usart_parity
    Parity mode enumeration data type. Options:
    • USART_PARITY_NONE - Disabled.
    • USART_PARITY_EVEN - Even parity.
    • USART_PARITY_ODD - Odd parity.

Functions

  • USART_BAUDRATE_ASYNC(baud)

Macro function to calculate USART baud rate register value in asynchronous mode. Parameters:

  • baud - Desired baud rate.
  • Returns baud rate register value.
  • void usart_init_async(usart port, usart_databits data_bits, usart_stopbits stop_bits, usart_parity parity, usart_baudrate baudrate)

Initializes asynchronous USART. Parameters:

  • port - USART interface variable.
  • data_bits - Data bits.
  • stop_bits - Stop bits.
  • parity - Parity mode.
  • baudrate - Baud rate register value (can be calculated with USART_BAUDRATE_ASYNC macro function).
  • void usart_send_char(usart port, char symbol)

Blocking character transmission. Functions wait until transmit buffer empties before writing a character to the buffer. Parameters:

  • port - USART interface variable.
  • symbol - Character to be sent.
  • void usart_send_string(usart port, char *text)

Blocking string transmission. Parameters:

  • port - USART interface variable.
  • text - Pointer to strin (char array). String has to end with null character.
  • bool usart_has_data(usart port)

Checks for data in receive buffer. Parameters:

  • port - USART interface variable.
  • Returns true when there is a character in receive buffer, and false when not.
  • char usart_read_char(usart port)

Reads a character from receive buffer. Before reading user must check if there actually is a received character. Parameters:

  • port - USART interface variable.
  • Returns character.
  • bool usart_try_read_char(usart port, char *symbol)

Reads a character from receive buffer if there is any. Parameters:

  • port - USART interface variable.
  • symbol - Pointer to character variable. If there is a character in receive buffer, it is written to the pointed variable.
  • Returns true when there was a character in receive buffer, and false when not.

Example

USART interface is configured to use 8 data bits, one stop bit, 9600 bps baud rate and no parity mode. Program sends a string, waits until some character is received and then reads it out.

#include <homelab/usart.h>
 
// Use USART interface 0.
usart port = USART(0);
 
int main(void)
{	
	char c;
 
	// Interface initialization.
	usart_init_async(port,
		USART_DATABITS_8,
		USART_STOPBITS_ONE,
		USART_PARITY_NONE,
		USART_BAUDRATE_ASYNC(9600));
 
	// String sending.
	usart_send_string(port, "Hello\n");
 
	// Waiting for incoming data.
	while (!usart_has_data(port)) {}
 
	// Read out the received character.
	c = usart_read_char(port);
}
en/software/homelab/library/usart.txt · Last modified: 2018/12/27 12:53 (external edit)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0