Loading...
Searching...
No Matches
uart.c
1/***********************************************************************************
2 * @file uart.c *
3 * @author Matt Ricci *
4 * @addtogroup UART *
5 * @brief Brief description of the file's purpose. *
6 * *
7 * @todo Tidy up `_UART_setup` *
8 * @todo Implement printf *
9 * @todo Add println function *
10 ***********************************************************************************/
11
12#include "uart.h"
13
14/* =============================================================================== */
27 UART_t *uart,
28 USART_TypeDef *interface,
29 GPIO_TypeDef *port,
30 UART_Pins pins,
31 uint32_t baud,
32 OversampleMode over8
33) {
34 uart->setBaud = UART_setBaud;
35 uart->send = UART_send;
37 uart->print = UART_print;
38 uart->receive = UART_receive;
39 uart->interface = interface;
40 uart->port = port;
41 uart->pins = pins;
42 uart->baud = baud;
43 uart->over8 = over8;
44
45 _UART_setup(uart, pins);
46
47 return *uart;
48}
49
50/********************************** PRIVATE METHODS *********************************/
51
52#ifndef DOXYGEN_PRIVATE
53
54/* =============================================================================== */
62void _UART_setup(UART_t *uart, UART_Pins pins) {
63 GPIO_TypeDef *port = uart->port;
64 USART_TypeDef *interface = uart->interface;
65
66 // Clear MODER bits for the TX and RX pins
67 port->MODER &= ~((0x03 << GPIO_MODER(pins.TX)) | (0x03 << GPIO_MODER(pins.RX))); // Clear mode bits
68 port->MODER |= (GPIO_MODE_AF << GPIO_MODER(pins.TX)) | (GPIO_MODE_AF << GPIO_MODER(pins.RX)); // Set mode to AF
69
70 // Clear AFR for the TX and RX pins and set AF8
71 port->AFR[pins.TX / 8] &= ~(0x0F << ((pins.TX % 8) * 4)); // Clear AF bits for TX
72 port->AFR[pins.RX / 8] &= ~(0x0F << ((pins.RX % 8) * 4)); // Clear AF bits for RX
73 port->AFR[pins.TX / 8] |= ((interface <= USART3 ? UART_AF7 : UART_AF8) << ((pins.TX % 8) * 4)); // Set AF8 for TX
74 port->AFR[pins.RX / 8] |= ((interface <= USART3 ? UART_AF7 : UART_AF8) << ((pins.RX % 8) * 4)); // Set AF8 for RX
75
76 // Set pull-up for RX pin
77 port->PUPDR &= ~(0x03 << GPIO_PUPDR(pins.RX)); // Clear pull-up/pull-down bits for RX
78 port->PUPDR |= (GPIO_PULL_UP << GPIO_PUPDR(pins.TX)); // Set pull-up for RX
79
80 // Set speed for TX and RX pins
81 port->OSPEEDR &= ~((0x03 << GPIO_OSPEEDR(pins.TX)) | (0x03 << GPIO_OSPEEDR(pins.RX))); // Clear speed bits
82 port->OSPEEDR |= (GPIO_SPEED_HIGH << GPIO_OSPEEDR(pins.TX)); // Set high speed for TX
83 port->OSPEEDR |= (GPIO_SPEED_HIGH << GPIO_OSPEEDR(pins.RX)); // Set high speed for RX
84
85 // Calculate USARTDIV
86 uint16_t usartDiv = 168000000 / ((2 - (uart->over8)) * uart->baud);
87 interface->BRR &= 0xFFFF0000; // Clear mantissa and div in baud rate reg
88 interface->BRR |= usartDiv; // Set baud rate
89
90 interface->CR1 &= ~USART_CR1_PCE; // disable parity
91 interface->CR2 &= ~USART_CR2_CLKEN; // disable synchrnous mode
92 interface->CR3 &= ~(USART_CR3_CTSE | USART_CR3_RTSE); // disable flow control
93 interface->CR1 |= (USART_CR1_RXNEIE); // enable RXNE interrupt
94 interface->CR1 |= (USART_CR1_UE | USART_CR1_RE | USART_CR1_TE); // enable usart, enable receive and transmit
95}
96
97#endif
98
99/********************************** INTERFACE METHODS ********************************/
100
101void UART_setBaud(UART_t *uart, uint32_t baud) {
102 GPIO_TypeDef *port = uart->port;
103 USART_TypeDef *interface = uart->interface;
104
105 USART1->CR1 &= ~USART_CR1_UE;
106
107 // Calculate USARTDIV
108 uint16_t usartDiv = 168000000 / ((2 - (uart->over8)) * baud);
109 interface->BRR &= 0xFFFF0000; // Clear mantissa and div in baud rate reg
110 interface->BRR |= usartDiv; // Set baud rate
111
112 USART1->CR1 |= USART_CR1_UE;
113}
114
115/* =============================================================================== */
124void UART_send(UART_t *uart, uint8_t data) {
125 USART_TypeDef *interface = uart->interface;
126 while ((interface->SR & USART_SR_TXE) == 0);
127 interface->DR = data;
128 while ((interface->SR & USART_SR_TC) == 0);
129}
130
131/* =============================================================================== */
141void UART_sendBytes(UART_t *uart, uint8_t *data, int length) {
142 for (int i = 0; i < length; i++)
143 UART_send(uart, data[i]);
144}
145
146/* =============================================================================== */
155
156void UART_print(UART_t *uart, char *data) {
157 int i = 0;
158 while (data[i] != '\0')
159 UART_send(uart, data[i++]);
160}
161
162/* =============================================================================== */
170uint8_t UART_receive(UART_t *uart) {
171 USART_TypeDef *interface = uart->interface;
172 while ((interface->SR & USART_SR_RXNE) == 0);
173 return (uint8_t)(interface->DR & 0xFF);
174}
@ GPIO_MODE_AF
Alternate function mode.
Definition gpiopin.h:98
#define USART_CR1_UE
#define USART_SR_TXE
#define USART_SR_TC
#define USART_SR_RXNE
__IO uint32_t MODER
__IO uint32_t AFR[2]
General Purpose I/O.
Universal Synchronous Asynchronous Receiver Transmitter.
void(* print)(struct UART *, char *)
UART print string method.
Definition uart.h:61
void(* sendBytes)(struct UART *, uint8_t *, int)
UART send multiple bytes method.
Definition uart.h:60
void(* send)(struct UART *, uint8_t)
UART send method.
Definition uart.h:59
uint8_t(* receive)(struct UART *)
UART receive method.
Definition uart.h:62
void UART_send(UART_t *, uint8_t data)
Sends a single byte of data over the UART interface.
Definition uart.c:124
void UART_sendBytes(UART_t *, uint8_t *data, int length)
Sends an array of bytes over the UART interface.
Definition uart.c:141
void _UART_setup(UART_t *uart, UART_Pins pins)
Configures the UART interface for communication.
Definition uart.c:62
uint8_t UART_receive(UART_t *)
Receives a single byte of data from the UART interface.
Definition uart.c:170
UART_t UART_init(UART_t *uart, USART_TypeDef *interface, GPIO_TypeDef *port, UART_Pins pins, uint32_t baud, OversampleMode over8)
Initialiser for a UART device interface.
Definition uart.c:26
void UART_print(UART_t *, char *data)
Sends a string of characters over the UART interface.
Definition uart.c:156
Struct definition for UART interface.
Definition uart.h:52