28 char name[DEVICE_NAME_LENGTH],
29 USART_TypeDef *interface,
35 uart->setBaud = UART_setBaud;
40 uart->interface = interface;
48 strcpy(handle.name, name);
56#ifndef DOXYGEN_PRIVATE
67 GPIO_TypeDef *port = uart->port;
68 USART_TypeDef *
interface = uart->interface;
71 port->MODER &= ~((0x03 << GPIO_MODER(pins.TX)) | (0x03 << GPIO_MODER(pins.RX)));
72 port->MODER |= (GPIO_MODE_AF << GPIO_MODER(pins.TX)) | (GPIO_MODE_AF << GPIO_MODER(pins.RX));
75 port->AFR[pins.TX / 8] &= ~(0x0F << ((pins.TX % 8) * 4));
76 port->AFR[pins.RX / 8] &= ~(0x0F << ((pins.RX % 8) * 4));
77 port->AFR[pins.TX / 8] |= ((
interface <= USART3 ? UART_AF7 : UART_AF8) << ((pins.TX % 8) * 4));
78 port->AFR[pins.RX / 8] |= ((interface <= USART3 ? UART_AF7 : UART_AF8) << ((pins.RX % 8) * 4));
81 port->PUPDR &= ~(0x03 << GPIO_PUPDR(pins.RX));
82 port->PUPDR |= (GPIO_PULL_UP << GPIO_PUPDR(pins.TX));
85 port->OSPEEDR &= ~((0x03 << GPIO_OSPEEDR(pins.TX)) | (0x03 << GPIO_OSPEEDR(pins.RX)));
86 port->OSPEEDR |= (GPIO_SPEED_HIGH << GPIO_OSPEEDR(pins.TX));
87 port->OSPEEDR |= (GPIO_SPEED_HIGH << GPIO_OSPEEDR(pins.RX));
90 uint16_t usartDiv = 168000000 / ((2 - (uart->over8)) * uart->baud);
91 interface->BRR &= 0xFFFF0000;
92 interface->BRR |= usartDiv;
94 interface->CR1 &= ~USART_CR1_PCE;
95 interface->CR2 &= ~USART_CR2_CLKEN;
96 interface->CR3 &= ~(USART_CR3_CTSE | USART_CR3_RTSE);
97 interface->CR1 |= (USART_CR1_RXNEIE);
98 interface->CR1 |= (USART_CR1_UE | USART_CR1_RE | USART_CR1_TE);
105void UART_setBaud(UART *uart, uint32_t baud) {
106 GPIO_TypeDef *port = uart->port;
107 USART_TypeDef *interface = uart->interface;
109 USART1->CR1 &= ~USART_CR1_UE;
112 uint16_t usartDiv = 168000000 / ((2 - (uart->over8)) * baud);
113 interface->BRR &= 0xFFFF0000;
114 interface->BRR |= usartDiv;
116 USART1->CR1 |= USART_CR1_UE;
129 USART_TypeDef *
interface = uart->interface;
130 while ((interface->SR & USART_SR_TXE) == 0);
131 interface->DR = data;
132 while ((interface->SR & USART_SR_TC) == 0);
146 for (
int i = 0; i < length; i++)
162 while (data[i] !=
'\0')
175 USART_TypeDef *
interface = uart->interface;
176 while ((interface->SR & USART_SR_RXNE) == 0);
177 return (uint8_t)(interface->DR & 0xFF);
void(* print)(struct UART *, char *)
UART print string method.
void(* sendBytes)(struct UART *, uint8_t *, int)
UART send multiple bytes method.
void(* send)(struct UART *, uint8_t)
UART send method.
uint8_t(* receive)(struct UART *)
UART receive method.
void UART_send(UART *, uint8_t data)
Sends a single byte of data over the UART interface.
void UART_print(UART *, char *data)
Sends a string of characters over the UART interface.
void _UART_setup(UART *uart, UART_Pins pins)
Configures the UART interface for communication.
DeviceHandle_t UART_init(UART *uart, char name[DEVICE_NAME_LENGTH], USART_TypeDef *interface, GPIO_TypeDef *port, UART_Pins pins, uint32_t baud, OversampleMode over8)
Initialiser for a UART device interface.
void UART_sendBytes(UART *, uint8_t *data, int length)
Sends an array of bytes over the UART interface.
uint8_t UART_receive(UART *)
Receives a single byte of data from the UART interface.
Struct definition for UART interface.