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

Core UART library. More...

#include "usart.h"
#include "timestamp.h"
#include "core_config.h"
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stm32g4xx_hal.h>
#include "clock.h"
#include "gpio.h"

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_start_rx (USART_TypeDef *usart, volatile uint8_t *rxbuf, volatile uint32_t *rxbuflen)
 Start the receiver for the given USART module.
 
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.
 
void USART1_IRQHandler ()
 
void USART2_IRQHandler ()
 
void USART3_IRQHandler ()
 
void core_USART_update_disable (USART_TypeDef *usart)
 Disable updating the RX buffer for the given USART. Use this before reading from the buffer to which data is stored to prevent corruption.
 
void core_USART_update_enable (USART_TypeDef *usart)
 Enable updating the RX buffer for the given USART.
 
bool core_USART_transmit (USART_TypeDef *usart, uint8_t *txbuf, uint8_t txbuflen)
 Transmit data from a USART.
 
uint32_t core_USART_receive (USART_TypeDef *usart, uint8_t *rxbuf, uint32_t rxbuflen, uint32_t timeout)
 Synchronously receive data from 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. To initialize a USART for receiving, user code can use either the core_USART_start_rx() or the core_USART_register_callback() functions.

Receiving

And data received over the USART will be stored to an internal buffer. The USART is configured to raise an interrupt if no data has been received for a certain time. What happens with the received data depends on which function was used to start the receiver.

If the receiver was started with core_USART_start_rx(), then the contents of the internal buffer and the number of bytes read will be copied into the buffers provided to core_USART_start_rx(). In this configuration, the user code will generally initialize its buffer length variable to zero and wait for its value to change. Before processing the data, it should call core_USART_update_disable() to prevent the interrupt handler from overwriting the receive buffer while its contents are being buffered. After processing the data in the receive buffer, the user code should set the buffer length variable back to zero and call core_USART_update_enable().

If the receiver was started with core_USART_register_callback(), then the callback passed to core_USART_register_callback() will be called after a receiver timeout. A pointer to the internal buffer and the number of bytes received will be passed to the callback function. Note that the callback will be called from an ISR, so FreeRTOS operations may not work.

Note
If a receive timeout occurs while updating is disabled, then the data in the internal receive buffer will be lost.

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_start_rx()

bool core_USART_start_rx ( USART_TypeDef * usart,
volatile uint8_t * rxbuf,
volatile uint32_t * rxbuflen )

Start the receiver for the given USART module.

Parameters
usartThe USART module
rxbufLocation where received data from the USART should be stored
rxbuflenLocation where the number of received bytes should be stored
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

◆ core_USART_update_disable()

void core_USART_update_disable ( USART_TypeDef * usart)

Disable updating the RX buffer for the given USART. Use this before reading from the buffer to which data is stored to prevent corruption.

Parameters
usartThe USART module

◆ core_USART_update_enable()

void core_USART_update_enable ( USART_TypeDef * usart)

Enable updating the RX buffer for the given USART.

Note
Receiving is disabled by default. Make sure to call core_USART_update_enable after setting up the receiver with core_USART_start_rx.
Parameters
usartThe USART module