34 uart->setBaud = UART_setBaud;
39 uart->interface = interface;
52#ifndef DOXYGEN_PRIVATE
67 port->
MODER &= ~((0x03 << GPIO_MODER(pins.TX)) | (0x03 << GPIO_MODER(pins.RX)));
71 port->
AFR[pins.TX / 8] &= ~(0x0F << ((pins.TX % 8) * 4));
72 port->
AFR[pins.RX / 8] &= ~(0x0F << ((pins.RX % 8) * 4));
73 port->
AFR[pins.TX / 8] |= ((
interface <= USART3 ? UART_AF7 : UART_AF8) << ((pins.TX % 8) * 4));
74 port->AFR[pins.RX / 8] |= ((interface <= USART3 ? UART_AF7 : UART_AF8) << ((pins.RX % 8) * 4));
77 port->PUPDR &= ~(0x03 << GPIO_PUPDR(pins.RX));
78 port->PUPDR |= (GPIO_PULL_UP << GPIO_PUPDR(pins.TX));
81 port->OSPEEDR &= ~((0x03 << GPIO_OSPEEDR(pins.TX)) | (0x03 << GPIO_OSPEEDR(pins.RX)));
82 port->OSPEEDR |= (GPIO_SPEED_HIGH << GPIO_OSPEEDR(pins.TX));
83 port->OSPEEDR |= (GPIO_SPEED_HIGH << GPIO_OSPEEDR(pins.RX));
86 uint16_t usartDiv = 168000000 / ((2 - (uart->over8)) * uart->baud);
87 interface->BRR &= 0xFFFF0000;
88 interface->BRR |= usartDiv;
90 interface->CR1 &= ~USART_CR1_PCE;
91 interface->CR2 &= ~USART_CR2_CLKEN;
92 interface->CR3 &= ~(USART_CR3_CTSE | USART_CR3_RTSE);
93 interface->CR1 |= (USART_CR1_RXNEIE);
94 interface->CR1 |= (USART_CR1_UE | USART_CR1_RE | USART_CR1_TE);
101void UART_setBaud(UART_t *uart, uint32_t baud) {
102 GPIO_TypeDef *port = uart->port;
108 uint16_t usartDiv = 168000000 / ((2 - (uart->over8)) * baud);
109 interface->BRR &= 0xFFFF0000;
110 interface->BRR |= usartDiv;
127 interface->DR = data;
142 for (
int i = 0; i < length; i++)
158 while (data[i] !=
'\0')
173 return (uint8_t)(interface->DR & 0xFF);
@ GPIO_MODE_AF
Alternate function mode.
Universal Synchronous Asynchronous Receiver Transmitter.
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_t *, uint8_t data)
Sends a single byte of data over the UART interface.
void UART_sendBytes(UART_t *, uint8_t *data, int length)
Sends an array of bytes over the UART interface.
void _UART_setup(UART_t *uart, UART_Pins pins)
Configures the UART interface for communication.
uint8_t UART_receive(UART_t *)
Receives a single byte of data from the UART interface.
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.
void UART_print(UART_t *, char *data)
Sends a string of characters over the UART interface.
Struct definition for UART interface.