APM:Libraries
Classes | Macros | Typedefs | Functions | Variables
usart.h File Reference
#include <hal_types.h>
#include "ring_buffer.h"
Include dependency graph for usart.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  usart_state
 
struct  usart_dev
 

Macros

#define USART_RX_BUF_SIZE   512
 
#define USART_TX_BUF_SIZE   1024
 
#define USART_F_RXNE   0x20
 
#define USART_F_TXE   0x80
 
#define USART_F_ORE   0x8
 
#define USART_MASK_IDLEIE   0x10
 
#define USART_MASK_RXNEIE   0x20
 
#define USART_MASK_TCEIE   0x40
 
#define USART_MASK_TXEIE   0x80
 
#define USART_MASK_PEIE   0x100
 
#define USART_MASK2_LBDIE   0x40
 
#define USART_MASK3_CTSIE   0x400
 
#define USART_MASK3_EIE   0x1
 
#define UART_Mode_Rx   (0x0004)
 
#define UART_Mode_Tx   (0x0008)
 
#define UART_HardwareFlowControl_None   (0x0000)
 
#define UART_HardwareFlowControl_RTS   (0x0100)
 
#define UART_HardwareFlowControl_CTS   (0x0200)
 
#define UART_HardwareFlowControl_RTS_CTS   (0x0300)
 
#define UART_Word_8b   ((uint16_t)0x0000)
 
#define UART_Word_9b   ((uint16_t)0x1000)
 

Typedefs

typedef void(* usart_cb) ()
 
typedef struct usart_state usart_state
 
typedef struct usart_dev usart_dev
 

Functions

void usart_init (const usart_dev *dev)
 Initialize a serial port. More...
 
void usart_setup (const usart_dev *dev, uint32_t baudRate, uint16_t wordLength, uint16_t stopBits, uint16_t parity, uint16_t mode, uint16_t hardwareFlowControl)
 Configure a serial port's baud rate. More...
 
static void usart_enable (const usart_dev *dev)
 Enable a serial port. More...
 
static uint8_t usart_is_used (const usart_dev *dev)
 
static void usart_disable (const usart_dev *dev)
 Turn off a serial port. More...
 
void usart_foreach (void(*fn)(const usart_dev *))
 Call a function on each USART. More...
 
static uint32_t usart_txfifo_nbytes (const usart_dev *dev)
 
static uint32_t usart_txfifo_freebytes (const usart_dev *dev)
 
static void usart_disable_all (void)
 Disable all serial ports. More...
 
uint32_t usart_tx (const usart_dev *dev, const uint8_t *buf, uint32_t len)
 Nonblocking USART transmit. More...
 
void usart_putudec (const usart_dev *dev, uint32_t val)
 Transmit an unsigned integer to the specified serial port in decimal format. More...
 
static uint32_t usart_putc (const usart_dev *dev, uint8_t bt)
 Transmit one character on a serial port. More...
 
static void usart_putstr (const usart_dev *dev, const char *str)
 Transmit a character string on a serial port. More...
 
static uint8_t usart_getc (const usart_dev *dev)
 Read one character from a serial port. More...
 
static uint32_t usart_data_available (const usart_dev *dev)
 Return the amount of data available in a serial port's RX buffer. More...
 
static void usart_reset_rx (const usart_dev *dev)
 Discard the contents of a serial port's RX buffer. More...
 
static void usart_reset_tx (const usart_dev *dev)
 
static void usart_set_callback (const usart_dev *dev, Handler cb)
 
void USART1_IRQHandler (void)
 
void USART2_IRQHandler (void)
 
void USART3_IRQHandler (void)
 
void USART4_IRQHandler (void)
 
void USART5_IRQHandler (void)
 
void USART6_IRQHandler (void)
 
void UART4_IRQHandler (void)
 
void UART5_IRQHandler (void)
 

Variables

const usart_dev *const UARTS []
 
const usart_dev *const _USART1
 
const usart_dev *const _USART2
 
const usart_dev *const _USART3
 
const usart_dev *const _UART4
 
const usart_dev *const _UART5
 
const usart_dev *const _USART6
 

Macro Definition Documentation

◆ UART_HardwareFlowControl_CTS

#define UART_HardwareFlowControl_CTS   (0x0200)

Definition at line 80 of file usart.h.

◆ UART_HardwareFlowControl_None

#define UART_HardwareFlowControl_None   (0x0000)

Definition at line 78 of file usart.h.

Referenced by F4Light::UARTDriver::begin().

◆ UART_HardwareFlowControl_RTS

#define UART_HardwareFlowControl_RTS   (0x0100)

Definition at line 79 of file usart.h.

◆ UART_HardwareFlowControl_RTS_CTS

#define UART_HardwareFlowControl_RTS_CTS   (0x0300)

Definition at line 81 of file usart.h.

◆ UART_Mode_Rx

#define UART_Mode_Rx   (0x0004)

Definition at line 75 of file usart.h.

◆ UART_Mode_Tx

#define UART_Mode_Tx   (0x0008)

Definition at line 76 of file usart.h.

◆ UART_Word_8b

#define UART_Word_8b   ((uint16_t)0x0000)

Definition at line 83 of file usart.h.

Referenced by F4Light::UARTDriver::begin().

◆ UART_Word_9b

#define UART_Word_9b   ((uint16_t)0x1000)

Definition at line 84 of file usart.h.

◆ USART_F_ORE

#define USART_F_ORE   0x8

Definition at line 62 of file usart.h.

Referenced by usart_rx_irq().

◆ USART_F_RXNE

#define USART_F_RXNE   0x20

Definition at line 60 of file usart.h.

Referenced by usart_rx_irq().

◆ USART_F_TXE

#define USART_F_TXE   0x80

Definition at line 61 of file usart.h.

Referenced by usart_tx_irq().

◆ USART_MASK2_LBDIE

#define USART_MASK2_LBDIE   0x40

Definition at line 70 of file usart.h.

Referenced by usart_setup().

◆ USART_MASK3_CTSIE

#define USART_MASK3_CTSIE   0x400

Definition at line 72 of file usart.h.

Referenced by usart_setup().

◆ USART_MASK3_EIE

#define USART_MASK3_EIE   0x1

Definition at line 73 of file usart.h.

Referenced by usart_setup().

◆ USART_MASK_IDLEIE

#define USART_MASK_IDLEIE   0x10

Definition at line 64 of file usart.h.

Referenced by usart_setup().

◆ USART_MASK_PEIE

#define USART_MASK_PEIE   0x100

Definition at line 68 of file usart.h.

Referenced by usart_setup().

◆ USART_MASK_RXNEIE

#define USART_MASK_RXNEIE   0x20

Definition at line 65 of file usart.h.

Referenced by usart_rx_irq(), and usart_setup().

◆ USART_MASK_TCEIE

#define USART_MASK_TCEIE   0x40

Definition at line 66 of file usart.h.

Referenced by usart_setup().

◆ USART_MASK_TXEIE

#define USART_MASK_TXEIE   0x80

Definition at line 67 of file usart.h.

Referenced by usart_setup(), usart_tx(), and usart_tx_irq().

◆ USART_RX_BUF_SIZE

#define USART_RX_BUF_SIZE   512

Definition at line 12 of file usart.h.

Referenced by usart_setup().

◆ USART_TX_BUF_SIZE

#define USART_TX_BUF_SIZE   1024

Definition at line 16 of file usart.h.

Referenced by usart_setup(), and usart_txfifo_freebytes().

Typedef Documentation

◆ usart_cb

typedef void(* usart_cb) ()

Definition at line 19 of file usart.h.

◆ usart_dev

typedef struct usart_dev usart_dev

USART device type

◆ usart_state

typedef struct usart_state usart_state

Function Documentation

◆ UART4_IRQHandler()

void UART4_IRQHandler ( void  )

Referenced by USART3_IRQHandler(), and usart_set_callback().

Here is the caller graph for this function:

◆ UART5_IRQHandler()

void UART5_IRQHandler ( void  )

Referenced by USART3_IRQHandler(), and usart_set_callback().

Here is the caller graph for this function:

◆ USART1_IRQHandler()

void USART1_IRQHandler ( void  )

Definition at line 362 of file usart.c.

Referenced by usart_set_callback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ USART2_IRQHandler()

void USART2_IRQHandler ( void  )

Referenced by USART1_IRQHandler(), and usart_set_callback().

Here is the caller graph for this function:

◆ USART3_IRQHandler()

void USART3_IRQHandler ( void  )

Definition at line 376 of file usart.c.

Referenced by usart_set_callback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ USART4_IRQHandler()

void USART4_IRQHandler ( void  )

Referenced by usart_set_callback().

Here is the caller graph for this function:

◆ USART5_IRQHandler()

void USART5_IRQHandler ( void  )

Referenced by usart_set_callback().

Here is the caller graph for this function:

◆ USART6_IRQHandler()

void USART6_IRQHandler ( void  )

Referenced by USART3_IRQHandler(), and usart_set_callback().

Here is the caller graph for this function:

◆ usart_data_available()

static uint32_t usart_data_available ( const usart_dev dev)
inlinestatic

Return the amount of data available in a serial port's RX buffer.

Parameters
devSerial port to check
Returns
Number of bytes in dev's RX buffer.

Definition at line 286 of file usart.h.

Referenced by F4Light::UARTDriver::available().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usart_disable()

static void usart_disable ( const usart_dev dev)
inlinestatic

Turn off a serial port.

Parameters
devSerial port to be disabled

Definition at line 180 of file usart.h.

Referenced by F4Light::UARTDriver::begin(), F4Light::UARTDriver::end(), usart_disable_all(), and usart_setup().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usart_disable_all()

static void usart_disable_all ( void  )
inlinestatic

Disable all serial ports.

Definition at line 206 of file usart.h.

Referenced by HAL_F4Light::run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usart_enable()

static void usart_enable ( const usart_dev dev)
inlinestatic

Enable a serial port.

USART is enabled in single buffer transmission mode, multibuffer receiver mode, 8n1.

Serial port must have a baud rate configured to work properly.

Parameters
devSerial port to enable.
See also
usart_set_baud_rate()

Definition at line 155 of file usart.h.

Referenced by F4Light::UARTDriver::begin().

Here is the caller graph for this function:

◆ usart_foreach()

void usart_foreach ( void(*)(const usart_dev *)  fn)

Call a function on each USART.

Parameters
fnFunction to call.

Definition at line 163 of file usart.c.

Referenced by usart_disable(), and usart_disable_all().

Here is the caller graph for this function:

◆ usart_getc()

static uint8_t usart_getc ( const usart_dev dev)
inlinestatic

Read one character from a serial port.

It's not safe to call this function if the serial port has no data available.

Parameters
devSerial port to read from
Returns
byte read
See also
usart_data_available()

Definition at line 277 of file usart.h.

Referenced by F4Light::UARTDriver::read().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usart_init()

void usart_init ( const usart_dev dev)

Initialize a serial port.

Parameters
devSerial port to be initialized

Definition at line 187 of file usart.c.

Referenced by F4Light::UARTDriver::begin().

Here is the caller graph for this function:

◆ usart_is_used()

static uint8_t usart_is_used ( const usart_dev dev)
inlinestatic

Definition at line 170 of file usart.h.

◆ usart_putc()

static uint32_t usart_putc ( const usart_dev dev,
uint8_t  bt 
)
inlinestatic

Transmit one character on a serial port.

This function blocks until the character has been successfully transmitted.

Parameters
devSerial port to send on.
byteByte to transmit.

Definition at line 240 of file usart.h.

Referenced by usart_putstr(), usart_putudec(), and F4Light::UARTDriver::write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usart_putstr()

static void usart_putstr ( const usart_dev dev,
const char *  str 
)
inlinestatic

Transmit a character string on a serial port.

This function blocks until str is completely transmitted.

Parameters
devSerial port to send on
strString to send used to inform about exception

Definition at line 260 of file usart.h.

Here is the call graph for this function:

◆ usart_putudec()

void usart_putudec ( const usart_dev dev,
uint32_t  val 
)

Transmit an unsigned integer to the specified serial port in decimal format.

This function blocks until the integer's digits have been completely transmitted.

Parameters
devSerial port to send on
valNumber to print

Definition at line 282 of file usart.c.

Referenced by usart_disable_all().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usart_reset_rx()

static void usart_reset_rx ( const usart_dev dev)
inlinestatic

Discard the contents of a serial port's RX buffer.

Parameters
devSerial port whose buffer to empty.

Definition at line 295 of file usart.h.

Referenced by F4Light::UARTDriver::flush().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usart_reset_tx()

static void usart_reset_tx ( const usart_dev dev)
inlinestatic

Definition at line 299 of file usart.h.

Referenced by F4Light::UARTDriver::flush().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usart_set_callback()

static void usart_set_callback ( const usart_dev dev,
Handler  cb 
)
inlinestatic

Definition at line 304 of file usart.h.

Referenced by F4Light::UARTDriver::begin(), and F4Light::UARTDriver::setCallback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usart_setup()

void usart_setup ( const usart_dev dev,
uint32_t  baudRate,
uint16_t  wordLength,
uint16_t  stopBits,
uint16_t  parity,
uint16_t  mode,
uint16_t  hardwareFlowControl 
)

Configure a serial port's baud rate.

Parameters
devSerial port to be configured
baudRateBaud rate for transmit/receive.
wordLengthSpecifies the number of data bits transmitted or received in a frame. This parameter can be a value of USART_Word_Length
stopBitsSpecifies the number of stop bits transmitted. This parameter can be a value of USART_Stop_Bits
paritySpecifies the parity mode. This parameter can be a value of USART_Parity
modeSpecifies wether the Receive or Transmit mode is enabled or disabled. This parameter can be a value of USART_Mode
hardwareFlowControlSpecifies wether the hardware flow control mode is enabled or disabled. This parameter can be a value of USART_Hardware_Flow_Control

Definition at line 199 of file usart.c.

Referenced by F4Light::UARTDriver::begin().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usart_tx()

uint32_t usart_tx ( const usart_dev dev,
const uint8_t *  buf,
uint32_t  len 
)

Nonblocking USART transmit.

Parameters
devSerial port to transmit over
bufBuffer to transmit
lenMaximum number of bytes to transmit
Returns
Number of bytes transmitted

Definition at line 258 of file usart.c.

Referenced by usart_disable_all(), usart_putc(), and F4Light::UARTDriver::write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usart_txfifo_freebytes()

static uint32_t usart_txfifo_freebytes ( const usart_dev dev)
inlinestatic

Definition at line 198 of file usart.h.

Referenced by F4Light::UARTDriver::txspace().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usart_txfifo_nbytes()

static uint32_t usart_txfifo_nbytes ( const usart_dev dev)
inlinestatic

Definition at line 195 of file usart.h.

Referenced by F4Light::UARTDriver::tx_pending().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ _UART4

const usart_dev* const _UART4

◆ _UART5

const usart_dev* const _UART5

◆ _USART1

const usart_dev* const _USART1

USART1 device

Definition at line 34 of file usart.c.

◆ _USART2

const usart_dev* const _USART2

◆ _USART3

const usart_dev* const _USART3

USART3 device

Definition at line 74 of file usart.c.

◆ _USART6

const usart_dev* const _USART6

◆ UARTS

const usart_dev* const UARTS[]

Definition at line 136 of file usart.c.

Referenced by F4Light::UARTDriver::begin().