Lazuli
Functions | Variables
usart.c File Reference

ATmega328p USART driver. More...

#include <stdint.h>
#include <stdio.h>
#include <Lazuli/common.h>
#include <Lazuli/config.h>
#include <Lazuli/serial.h>
#include <Lazuli/sys/arch/AVR/usart.h>
#include <Lazuli/sys/arch/arch.h>
#include <Lazuli/sys/compiler.h>
#include <Lazuli/sys/kernel.h>

Go to the source code of this file.

Functions

int putchar (int c)
 Transmit a single character on the serial line. More...
 
int puts (const char *s)
 Transmit the NUL-terminated string s followed by a trailing newline on the serial line, without any formatting. More...
 
static enum Lz_SerialEnableFlags GetEnablingStatus (void)
 Retrieve the enabling status of the serial line. More...
 
static enum Lz_SerialStopBits GetStopBits (void)
 Retrieve the number of stop bits currently used by the serial line. More...
 
static enum Lz_SerialParityBit GetParityBit (void)
 Retrieve the kind of parity currently used by the serial line. More...
 
static enum Lz_SerialSize GetSize (void)
 Retrieve the character size currently used by the serial line. More...
 
static void SetEnablingStatus (const enum Lz_SerialEnableFlags flags)
 Set the enabling/disabling of Tx/Rx of the serial line. More...
 
static void SetStopBits1 (void)
 Set USART to use 1 stop bit.
 
static void SetStopBits2 (void)
 Set USART to use 2 stop bits.
 
static void SetStopBits (const enum Lz_SerialStopBits stopBits)
 Set the number of stop bits of the serial line. More...
 
static void SetParityBitNone (void)
 Set no parity bit.
 
static void SetParityBitEven (void)
 Set even parity bit.
 
static void SetParityBitOdd (void)
 Set odd parity bit.
 
static void SetParityBit (const enum Lz_SerialParityBit parityBit)
 Set the kind of parity bit of the serial line. More...
 
static void SetSize5 (void)
 Set character size to 5 bits.
 
static void SetSize6 (void)
 Set character size to 6 bits.
 
static void SetSize7 (void)
 Set character size to 7 bits.
 
static void SetSize8 (void)
 Set character size to 8 bits.
 
static void SetSize (const enum Lz_SerialSize size)
 Set the character size of the serial line. More...
 
static void SetSpeed (const enum Lz_SerialSpeed speed)
 Set the baud rate of the serial line. More...
 
void Arch_GetSerialConfiguration (Lz_SerialConfiguration *const configuration)
 Retrieve the current configuration of the serial line. More...
 
void Arch_SetSerialConfiguration (const Lz_SerialConfiguration *const configuration)
 Configure the seria line according to the parameter. More...
 
void Arch_InitSerial (void)
 Initialize serial line with default configuration at system startup. More...
 

Variables

static Usart *const usart = (Usart*)&UCSR0A
 A constant pointer to the memory mapped Usart structure.
 
static enum Lz_SerialSpeed currentSerialSpeed
 The current serial baud rate. More...
 
static void(*const setStopBits [])(void)
 Jump table to functions setting the number of stop bits of the serial line. More...
 
static void(*const setParityBit [])(void)
 Jump table to functions setting the kind of parity of the serial line. More...
 
static void(*const setSize [])(void)
 Jump table to functions setting character sizes of the serial line. More...
 
static const uint16_t serialSpeedRegisterValue []
 Jump table containing the setting values of UBRR0L and UBRR0H registers for baud rates defined in Lz_SerialSpeed. More...
 

Detailed Description

ATmega328p USART driver.

Describes the driver implementation for the USART of the ATMega328p.

Definition in file usart.c.

Function Documentation

◆ putchar()

int putchar ( int  c)

Transmit a single character on the serial line.

Parameters
cThe character to transmit, that will be cast to an unsigned char.
Returns
The actual value of the transmitted character, resulting from the cast, as an int.

Definition at line 40 of file usart.c.

◆ puts()

int puts ( const char *  s)

Transmit the NUL-terminated string s followed by a trailing newline on the serial line, without any formatting.

Parameters
sThe string to transmit.
Returns
A non-negative number on success, otherwise EOF on error.

Definition at line 52 of file usart.c.

◆ GetEnablingStatus()

static enum Lz_SerialEnableFlags GetEnablingStatus ( void  )
static

Retrieve the enabling status of the serial line.

Returns
An Lz_SerialEnableFlags containing the enabling status.

Definition at line 79 of file usart.c.

◆ GetStopBits()

static enum Lz_SerialStopBits GetStopBits ( void  )
static

Retrieve the number of stop bits currently used by the serial line.

Returns
An Lz_SerialStopBits containing the number of stop bits.

Definition at line 100 of file usart.c.

◆ GetParityBit()

static enum Lz_SerialParityBit GetParityBit ( void  )
static

Retrieve the kind of parity currently used by the serial line.

Returns
An Lz_SerialParityBit containing the kind of parity.

Definition at line 115 of file usart.c.

◆ GetSize()

static enum Lz_SerialSize GetSize ( void  )
static

Retrieve the character size currently used by the serial line.

Returns
An Lz_SerialSize containing the character size.

Definition at line 138 of file usart.c.

◆ SetEnablingStatus()

static void SetEnablingStatus ( const enum Lz_SerialEnableFlags  flags)
static

Set the enabling/disabling of Tx/Rx of the serial line.

Parameters
flagsAn Lz_SerialEnableFlags containing enable flags.

Definition at line 165 of file usart.c.

◆ SetStopBits()

static void SetStopBits ( const enum Lz_SerialStopBits  stopBits)
static

Set the number of stop bits of the serial line.

Parameters
stopBitsAn Lz_SerialStopBits defining the number of stop bits.

Definition at line 224 of file usart.c.

◆ SetParityBit()

static void SetParityBit ( const enum Lz_SerialParityBit  parityBit)
static

Set the kind of parity bit of the serial line.

Parameters
parityBitAn Lz_SerialParityBit defining the kind of parity.

Definition at line 294 of file usart.c.

◆ SetSize()

static void SetSize ( const enum Lz_SerialSize  size)
static

Set the character size of the serial line.

Parameters
sizeAn Lz_SerialSize defining the character size.

Definition at line 378 of file usart.c.

◆ SetSpeed()

static void SetSpeed ( const enum Lz_SerialSpeed  speed)
static

Set the baud rate of the serial line.

Parameters
speedAn Lz_SerialSpeed value defining the baud rate.

Definition at line 421 of file usart.c.

◆ Arch_GetSerialConfiguration()

void Arch_GetSerialConfiguration ( Lz_SerialConfiguration *const  configuration)

Retrieve the current configuration of the serial line.

Parameters
configurationA pointer to an allocated Lz_SerialConfiguration used to store the configuration.

Definition at line 441 of file usart.c.

◆ Arch_SetSerialConfiguration()

void Arch_SetSerialConfiguration ( const Lz_SerialConfiguration *const  configuration)

Configure the seria line according to the parameter.

Parameters
configurationA pointer to an existing Lz_SerialConfiguration containg the full configuration to set up the serial line.

Definition at line 451 of file usart.c.

◆ Arch_InitSerial()

void Arch_InitSerial ( void  )

Initialize serial line with default configuration at system startup.

< member: enableFlags

< member: stopBits

< member: parityBits

< member: size

< member: speed

Definition at line 480 of file usart.c.

Variable Documentation

◆ currentSerialSpeed

enum Lz_SerialSpeed currentSerialSpeed
static

The current serial baud rate.

This is used to save the current setting of the serial baud rate. On AVR, the setting of the speed isn't always accurate once calculated, so we use this variable to store it and be able to retrieve it easily.

Definition at line 37 of file usart.c.

◆ setStopBits

void(* const setStopBits[])(void)
static
Initial value:
= {
}
static void SetStopBits1(void)
Set USART to use 1 stop bit.
Definition: usart.c:184
static void SetStopBits2(void)
Set USART to use 2 stop bits.
Definition: usart.c:193

Jump table to functions setting the number of stop bits of the serial line.

Warning
This table must be ordered by entry values of enum Lz_SerialStopBits.

Definition at line 205 of file usart.c.

◆ setParityBit

void(* const setParityBit[])(void)
static
Initial value:
= {
}
static void SetParityBitEven(void)
Set even parity bit.
Definition: usart.c:252
static void SetParityBitNone(void)
Set no parity bit.
Definition: usart.c:243
static void SetParityBitOdd(void)
Set odd parity bit.
Definition: usart.c:262

Jump table to functions setting the kind of parity of the serial line.

Warning
This table must be ordered by entry values of enum Lz_SerialParityBit.

Definition at line 274 of file usart.c.

◆ setSize

void(* const setSize[])(void)
static
Initial value:
= {
}
static void SetSize5(void)
Set character size to 5 bits.
Definition: usart.c:313
static void SetSize7(void)
Set character size to 7 bits.
Definition: usart.c:334
static void SetSize6(void)
Set character size to 6 bits.
Definition: usart.c:323
static void SetSize8(void)
Set character size to 8 bits.
Definition: usart.c:345

Jump table to functions setting character sizes of the serial line.

Warning
This table must be ordered by entry values of enum Lz_SerialSize.

Definition at line 357 of file usart.c.

◆ serialSpeedRegisterValue

const uint16_t serialSpeedRegisterValue[]
static
Initial value:
= {
(uint16_t)416,
(uint16_t)207,
(uint16_t)103,
(uint16_t)51
}
unsigned int uint16_t
Represents a unsigned integer type with width of exactly 16 bits.
Definition: stdint.h:94

Jump table containing the setting values of UBRR0L and UBRR0H registers for baud rates defined in Lz_SerialSpeed.

Warning
This table must be ordered by entry values of enum Lz_SerialSpeed.

Definition at line 400 of file usart.c.