Core
Loading...
Searching...
No Matches
usart.h File Reference

Core UART library. More...

#include <stdint.h>
#include <stdbool.h>
#include <stm32g4xx_hal.h>
#include "core_config.h"

Go to the source code of this file.

Data Structures

struct  core_USART_module_s
 

Macros

#define CORE_USART1_UPDATE   0x01
 
#define CORE_USART2_UPDATE   0x02
 
#define CORE_USART3_UPDATE   0x04
 

Typedefs

typedef struct core_USART_module_s core_USART_module_t
 

Functions

bool core_USART_init (USART_TypeDef *usart, uint32_t baud)
 Initialize a USART module in asynchronous mode with the given baud rate.
 
bool core_USART_transmit (USART_TypeDef *usart, uint8_t *txbuf, uint8_t txbuflen)
 Transmit data from a USART.
 
bool core_USART_register_callback (USART_TypeDef *usart, void(*callback)(uint8_t *, uint32_t))
 Set the RX callback and start the receiver for the given USART module.
 
uint32_t core_USART_receive (USART_TypeDef *usart, uint8_t *rxbuf, uint32_t rxbuflen, uint32_t timeout)
 Synchronously receive data from a USART.
 
int uprintf (USART_TypeDef *usart, const char *format,...)
 Print a formatted string to a USART.
 

Detailed Description

Core UART library.

This core library component is used to initialize USARTs, transmit data over USART, and asynchronously receive data over USART.

Initialization

To initialize a USART for transmitting, user code must call the function core_USART_init() and specify the desired baud rate.

Receiving

To receive data, the user must call core_USART_register_callback() with a pointer to a function that will be called when data has been received. If no data has been received for CORE_USART_TIMEOUT bit periods, then the callback function is called with a pointer to the internal buffer as well as the number of received bytes.

Function Documentation

◆ core_USART_init()

bool core_USART_init ( USART_TypeDef * usart,
uint32_t baud )

Initialize a USART module in asynchronous mode with the given baud rate.

Parameters
usartThe USART module to initialize
baudBaud rate
Return values
0if the given USART is not valid of if the initialization failed
1otherwise

◆ core_USART_receive()

uint32_t core_USART_receive ( USART_TypeDef * usart,
uint8_t * rxbuf,
uint32_t rxbuflen,
uint32_t timeout )

Synchronously receive data from a USART.

Note
This function is blocking and will not return until all data has been read or until the timeout has elapsed
Parameters
usartThe USART module
rxbufLocation where the data to be received is stored
rxbuflenSize of the RX buffer
timeoutRX timeout in microseconds
Returns
Returns the number of bytes received

◆ core_USART_register_callback()

bool core_USART_register_callback ( USART_TypeDef * usart,
void(* callback )(uint8_t *, uint32_t) )

Set the RX callback and start the receiver for the given USART module.

Parameters
usartThe USART module
callbackFunction to be called after data is received (the RX timeout elapses). The function must take a pointer to a byte array as the first argument and a uint32_t length as the second argument
Return values
0if the given USART is not valid
1otherwise

◆ core_USART_transmit()

bool core_USART_transmit ( USART_TypeDef * usart,
uint8_t * txbuf,
uint8_t txbuflen )

Transmit data from a USART.

Note
This function is blocking and will not return until all data has been transmitted.
Parameters
usartThe USART module
txbufLocation where the data to be transmitted is read from
txbuflenNumber of bytes to transmit
Return values
1if transmission was successful
0otherwise

◆ uprintf()

int uprintf ( USART_TypeDef * usart,
const char * format,
... )

Print a formatted string to a USART.

Note
This function is blocking and will not return until all data has been transmitted. This function is only defined if CORE_USART_UPRINTF is not zero
Parameters
usartThe USART module
formatFormat string
txbuflenNumber of bytes to transmit
Returns
Returns -1 if the transmission failed. Otherwise, returns the number of transmitted bytes.