hal
Hardware Abstraction Layer
stm32f4_uart.c File Reference

Provides serial communication over UART1 and UART2. More...

#include "uart.h"
#include "stm32f4_uart1.h"
#include "stm32f4_uart2.h"
Include dependency graph for stm32f4_uart.c:

Functions

int __io_putchar (int ch)
 syscall declaration for putchar so that printf may be used. More...
 
hal_status_t hal_uart_init (hal_uart_t uart)
 Initialize the UART channel associated with the parameter uart. Must be called prior to using the channel. More...
 
hal_status_t hal_uart_deinit (hal_uart_t uart)
 Deinitialize the UART channel associated with the parameter uart. More...
 
hal_status_t hal_uart_read (hal_uart_t uart, uint8_t *data, size_t len, size_t *bytes_read)
 Read an incoming byte stream. More...
 
hal_status_t hal_uart_write (hal_uart_t uart, const uint8_t *data, size_t len, size_t *bytes_written)
 Write an outgoing byte stream. More...
 

Detailed Description

Provides serial communication over UART1 and UART2.

Copyright (c) 2025 Cory McKiel. Licensed under the MIT License. See LICENSE file in the project root.

Function Documentation

◆ __io_putchar()

int __io_putchar ( int  ch)

syscall declaration for putchar so that printf may be used.

Parameters
chThe character to print.
Returns
ch, the value that was passed in as parameter.

◆ hal_uart_deinit()

hal_status_t hal_uart_deinit ( hal_uart_t  uart)

Deinitialize the UART channel associated with the parameter uart.

Only a channel that has been initialized can be successfully deinitialized.

Parameters
uartThe UART channel to deinitialize.
Returns
HAL_STATUS_OK on successful deinitialization, HAL_STATUS_ERROR otherwise.
Note
Brings down the peripheral, but not the gpio port that was initialized. This is because gpio ports are shared and unexpected deinits could disrupt other drivers without coordination.

◆ hal_uart_init()

hal_status_t hal_uart_init ( hal_uart_t  uart)

Initialize the UART channel associated with the parameter uart. Must be called prior to using the channel.

Parameters
uartThe UART channel to initialize.
Returns
HAL_STATUS_OK on successful initialization, HAL_STATUS_ERROR otherwise.

◆ hal_uart_read()

hal_status_t hal_uart_read ( hal_uart_t  uart,
uint8_t *  data,
size_t  len,
size_t *  bytes_read 
)

Read an incoming byte stream.

Data is placed into the buffer in the same order it was received on the UART channel. There is no way to partition it using this API. Any processing (i.e. parsing for the start of a command) needs to happen at a higher level.

Parameters
uartThe UART channel to read from.
dataA buffer to return read data to client.
lenThe max number of bytes to read into buffer. Should not exceed buffer size.
bytes_readReturn the actual number of bytes that were successfully read.
Returns
HAL_STATUS_OK on success, HAL_STATUS_ERROR otherwise.

◆ hal_uart_write()

hal_status_t hal_uart_write ( hal_uart_t  uart,
const uint8_t *  data,
size_t  len,
size_t *  bytes_written 
)

Write an outgoing byte stream.

Parameters
uartThe UART channel to write to.
dataA buffer filled with data to send.
lenThe number of bytes to send. Should not exceed buffer size.
bytes_writtenReturn the actual number of bytes that were successfully written.
Returns
HAL_STATUS_OK on success, HAL_STATUS_ERROR otherwise.