PER Firmware
Loading...
Searching...
No Matches
usart.h File Reference
#include "stm32g474xx.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.

Enumerations

enum  PHAL_USART_Idx_t { USART1_IDX = 0 , USART2_IDX = 1 , USART3_IDX = 2 , NUM_USART }
 

Functions

bool PHAL_USART_init (PHAL_USART_Idx_t periph_idx, uint32_t baud_rate, const uint32_t clock_rate)
 Initialize a USART peripheral for DMA-driven communication.
 
bool PHAL_USART_tx (PHAL_USART_Idx_t periph_idx, uint8_t *data, uint16_t len)
 Start a transmission using DMA.
 
void PHAL_USART_rx (PHAL_USART_Idx_t periph_idx, uint8_t *data, uint16_t len, bool cont)
 Start a reception using DMA of a specific length.
 
bool PHAL_USART_txBusy (PHAL_USART_Idx_t periph_idx)
 Returns whether the USART peripheral is currently transmitting data.
 
uint16_t PHAL_USART_rxCount (PHAL_USART_Idx_t periph_idx)
 Number of bytes received in the last completed frame.
 
bool PHAL_USART_txBlocking (PHAL_USART_Idx_t periph_idx, uint8_t *data, uint16_t len)
 Transmit data, blocking until the transfer completes.
 
void PHAL_USART_rxBlocking (PHAL_USART_Idx_t periph_idx, uint8_t *data, uint16_t len)
 Receive data, blocking until a one-shot reception completes.
 
void PHAL_USART_rxCallback (PHAL_USART_Idx_t periph_idx, uint16_t len)
 Weak callback invoked when a full RX frame is received. Override in application code. Runs in ISR context, so keep it light.
 

Enumeration Type Documentation

◆ PHAL_USART_Idx_t

Enumerator
USART1_IDX 
USART2_IDX 
USART3_IDX 
NUM_USART 

Function Documentation

◆ PHAL_USART_init()

bool PHAL_USART_init ( PHAL_USART_Idx_t periph_idx,
uint32_t baud_rate,
const uint32_t clock_rate )

Initialize a USART peripheral for DMA-driven communication.

Enables the peripheral clock and applies a fixed frame format:

  • 8 data bits, no parity, 1 stop bit (8N1)
  • 16x oversampling
  • no hardware flow control Derives the baud-rate divisor from clock_rate, enables the IDLE-line interrupt (RX frame completion) and the TX DMA transfer-complete interrupt, then initializes the TX and RX DMA channels.

Call once per USART before any tx/rx. The USART GPIO pins must already be configured by the caller.

Parameters
periph_idxWhich USART peripheral to initialize
baud_rateDesired baud rate
clock_rateFrequency (Hz) of the bus clock feeding this USART (APBx)
Returns
true on success, false if DMA init failed
Parameters
periph_idxWhich USART peripheral to initialize
baud_rateDesired baud rate
clock_rateFrequency (Hz) of the bus clock feeding this USART (APB1/APB2)
Returns
true on success, false if DMA init failed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PHAL_USART_rx()

void PHAL_USART_rx ( PHAL_USART_Idx_t periph_idx,
uint8_t * data,
uint16_t len,
bool cont )

Start a reception using DMA of a specific length.

Parameters
periph_idxWhich USART peripheral to receive on
dataThe address to put the received data
lenMaximum number of bytes (the buffer size). A frame shorter than this still completes on the IDLE line; the actual count is reported to PHAL_USART_rxCallback. uint16_t because that is the DMA's transfer counter width.
contEnable continuous RX using the IDLE-line interrupt. When set, call this function once and the HAL keeps receiving frames of the same maximum length, invoking PHAL_USART_rxCallback after each.

Start a reception using DMA of a specific length.

Parameters
periph_idxWhich USART peripheral to receive on
dataBuffer to receive into
lenMaximum number of bytes to receive (buffer size)
contEnable continuous RX. When set, call this once and the HAL keeps receiving frames of the same maximum length, invoking PHAL_USART_rxCallback after each.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PHAL_USART_rxBlocking()

void PHAL_USART_rxBlocking ( PHAL_USART_Idx_t periph_idx,
uint8_t * data,
uint16_t len )

Receive data, blocking until a one-shot reception completes.

Starts a one-shot DMA reception (see PHAL_USART_rx) and busy-waits until the IDLE-line ISR signals the frame is complete. Call PHAL_USART_rxCount() afterwards for the byte count. Do not call from an ISR.

Parameters
periph_idxWhich USART peripheral to receive on
dataBuffer to receive into
lenMaximum number of bytes to receive
periph_idxWhich USART peripheral to receive on
dataBuffer to receive into
lenNumber of bytes to receive
Here is the call graph for this function:

◆ PHAL_USART_rxCallback()

void PHAL_USART_rxCallback ( PHAL_USART_Idx_t periph_idx,
uint16_t len )
extern

Weak callback invoked when a full RX frame is received. Override in application code. Runs in ISR context, so keep it light.

Parameters
periph_idxWhich USART peripheral received the frame
lenNumber of bytes in this frame. Bytes past this offset in the buffer are stale and must not be decoded.
Here is the caller graph for this function:

◆ PHAL_USART_rxCount()

uint16_t PHAL_USART_rxCount ( PHAL_USART_Idx_t periph_idx)

Number of bytes received in the last completed frame.

The IDLE line ends a frame whenever the sender stops, which may be short of the length passed to PHAL_USART_rx. Anything past this count in the buffer is leftover from an earlier frame.

Parameters
periph_idxWhich USART peripheral to query
Returns
byte count, valid once PHAL_USART_rxCallback has fired (or PHAL_USART_rxBlocking has returned)
Parameters
periph_idxWhich USART peripheral to query
Returns
byte count, valid once PHAL_USART_rxCallback has fired

◆ PHAL_USART_tx()

bool PHAL_USART_tx ( PHAL_USART_Idx_t periph_idx,
uint8_t * data,
uint16_t len )

Start a transmission using DMA.

Fails rather than truncating a transfer already in flight - poll PHAL_USART_txBusy() (or use PHAL_USART_txBlocking) before calling again.

Parameters
periph_idxWhich USART peripheral to transmit on
dataThe address of the data to send
lenNumber of bytes.
Returns
true if every DMA reconfiguration step succeeded, false if a transmission is already in flight or a step failed

Start a transmission using DMA.

Parameters
periph_idxWhich USART peripheral to transmit on
dataBuffer to send
lenNumber of bytes to send
Returns
true if every DMA reconfiguration step succeeded, false if a transmission is already in flight or a step failed
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PHAL_USART_txBlocking()

bool PHAL_USART_txBlocking ( PHAL_USART_Idx_t periph_idx,
uint8_t * data,
uint16_t len )

Transmit data, blocking until the transfer completes.

Starts a DMA transmission (see PHAL_USART_tx) and busy-waits until the USART reports the frame fully shifted out. Do not call from an ISR.

Parameters
periph_idxWhich USART peripheral to transmit on
dataBuffer to send
lenNumber of bytes to send
Returns
true if the transfer completed, false if it failed to start
Parameters
periph_idxWhich USART peripheral to transmit on
dataBuffer to send
lenNumber of bytes to send
Returns
true if the transfer completed, false if it failed to start
Here is the call graph for this function:

◆ PHAL_USART_txBusy()

bool PHAL_USART_txBusy ( PHAL_USART_Idx_t periph_idx)

Returns whether the USART peripheral is currently transmitting data.

Tracks the USART's own transmission-complete flag, so it stays true until the last bit has left the wire - not merely until the DMA has finished writing to the data register.

Parameters
periph_idxWhich USART peripheral to check
Returns
true if the peripheral is currently sending a message, false otherwise

Returns whether the USART peripheral is currently transmitting data.

Parameters
periph_idxWhich USART peripheral to check
Returns
true if a transmission is in flight, false otherwise
Here is the caller graph for this function: