mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-05-24 16:34:46 +02:00
112 lines
3.3 KiB
C
112 lines
3.3 KiB
C
/**
|
|
* @file uart.h
|
|
* @brief Header for bare-metal UART0 driver (raw TX/RX, GPIO 0/1)
|
|
* @author Kevin Thomas
|
|
* @date 2025
|
|
*
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2025 Kevin Thomas
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
|
|
#ifndef UART_H
|
|
#define UART_H
|
|
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
|
|
/**
|
|
* @brief Release UART0 from reset and wait until it is ready
|
|
*
|
|
* Clears the UART0 reset bit in the Reset controller and polls
|
|
* RESET_DONE until the UART0 block is out of reset.
|
|
*
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
void UART_Release_Reset(void);
|
|
|
|
/**
|
|
* @brief Initialize UART0 (pins, baud divisors, line control, enable)
|
|
*
|
|
* Configures IO_BANK0 pins 0 (TX) and 1 (RX) to the UART function,
|
|
* programs pad controls, sets baud rate divisors (IBRD=6, FBRD=33
|
|
* for 115200 baud at 14.5 MHz), configures 8N1 with FIFOs, and
|
|
* enables UART0 with TX and RX.
|
|
*
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
void UART_Init(void);
|
|
|
|
/**
|
|
* @brief Check whether a received character is waiting in the UART FIFO
|
|
*
|
|
* Returns immediately without blocking. Use this to poll for incoming
|
|
* data before calling uart_driver_getchar().
|
|
*
|
|
* @return bool true if at least one byte is available, false otherwise
|
|
*/
|
|
bool uart_driver_is_readable(void);
|
|
|
|
/**
|
|
* @brief Read one character from UART0 (blocking)
|
|
*
|
|
* Blocks until a byte arrives in the receive FIFO, then returns it.
|
|
* Prefer pairing with uart_driver_is_readable() to avoid indefinite blocking.
|
|
*
|
|
* @return char The received character
|
|
*/
|
|
char uart_driver_getchar(void);
|
|
|
|
/**
|
|
* @brief Transmit one character over UART0 (blocking)
|
|
*
|
|
* Waits until the transmit FIFO has space, then writes the character
|
|
* to UARTDR.
|
|
*
|
|
* @param c Character to transmit
|
|
*/
|
|
void uart_driver_putchar(char c);
|
|
|
|
/**
|
|
* @brief Transmit a null-terminated string over UART0
|
|
*
|
|
* Calls uart_driver_putchar() for every character in the string up to
|
|
* and not including the null terminator.
|
|
*
|
|
* @param str Pointer to the null-terminated ASCII string to send
|
|
*/
|
|
void uart_driver_puts(const char *str);
|
|
|
|
/**
|
|
* @brief Convert a lowercase ASCII character to uppercase
|
|
*
|
|
* Returns the uppercase equivalent if the character is in 'a'-'z';
|
|
* all other characters are passed through unchanged.
|
|
*
|
|
* @param c Input character
|
|
* @return char Uppercase equivalent, or the original character
|
|
*/
|
|
char uart_driver_to_upper(char c);
|
|
|
|
#endif // UART_H
|