microHAL
An abstraction layer for your future F4xx projects
Loading...
Searching...
No Matches
usart.h
Go to the documentation of this file.
1
12#ifndef USART_H
13#define USART_H
14
15/* -- Includes -- */
16#include <stdint.h>
17#include "stm32f4xx.h"
18#include "defines.h"
19
20/* -- Structs -- */
24struct __attribute__((packed)) USARTRegs {
25 REG32 SR;
26 REG32 DR;
27 REG32 BRR;
28 REG32 CR1;
29 REG32 CR2;
30 REG32 CR3;
31 REG32 GTPR;
32};
33
34_Static_assert((sizeof(struct USARTRegs)) == (sizeof(uint32_t) * 7U),
35 "USART register struct size mismatch. Is it aligned?");
36
37#define USART(x) (struct USARTRegs *)(x)
38
42struct __attribute__((packed)) USARTISR {
43 volatile _Bool TXEI : 1;
44 volatile _Bool CTSI : 1;
45 volatile _Bool TCI : 1;
46 volatile _Bool RXNEI : 1;
47 volatile _Bool IDLEI : 1;
48 volatile _Bool PEI : 1;
49 volatile _Bool LBDI : 1;
50 volatile _Bool EI : 1;
51};
52
53_Static_assert((sizeof(struct USARTISR)) == (sizeof(uint8_t) * 1U),
54 "USART interrupt struct size mismatch. Is it aligned?");
55
56/* -- Enums -- */
60typedef enum usart_peripheral {
61#ifdef USART1_BASE
62 USART_PERIPH_1,
63#endif
64#ifdef USART2_BASE
65 USART_PERIPH_2,
66#endif
67#ifdef USART3_BASE
68 USART_PERIPH_3,
69#endif
70#ifdef USART6_BASE
71 USART_PERIPH_6,
72#endif
73#ifdef UART4_BASE
74 UART_PERIPH_4,
75#endif
76#ifdef UART5_BASE
77 UART_PERIPH_5
78#endif
80
84typedef enum usart_mode {
85 USART_MODE_TX = 0x00,
86 USART_MODE_RX = 0x01,
87 USART_MODE_BO = 0x02
89
93typedef enum usart_databits {
94 USART_DATABITS_DB8 = 0x00,
95 USART_DATABITS_DB9 = 0x01
97
101typedef enum usart_stopbits {
102 USART_STOPBITS_SB1 = 0x00,
103 USART_STOPBITS_SBH = 0x01,
104 USART_STOPBITS_SB2 = 0x02,
105 USART_STOPBITS_SBO = 0x03
107
111typedef enum usart_parity {
112 USART_PARITY_EVN = 0x00,
113 USART_PARITY_ODD = 0x01,
114 USART_PARITY_OFF = 0x02
116
129void usart_start(const usart_peripheral_t usart, const uint32_t baudrate,
130 const usart_mode_t mode);
131
140void usart_set_dma(const usart_peripheral_t usart, const _Bool forTX,
141 const _Bool forRX);
142
154 const struct USARTISR config);
155
169 const usart_stopbits_t stopbits,
170 const usart_databits_t databits);
171
185void usart_set_parity(const usart_peripheral_t usart,
186 const usart_parity_t parity);
187
199void usart_tx_message(const usart_peripheral_t usart, const char *message);
200
212uint16_t usart_rx_byte(const usart_peripheral_t usart);
213
223void usart_stop(const usart_peripheral_t usart);
224
225#endif
Defines used commonly in most files.
CMSIS device header stubs.
Contains USART interrupt configuration.
Definition usart.h:42
Contains USART registers.
Definition usart.h:24
void usart_set_dma(const usart_peripheral_t usart, const _Bool forTX, const _Bool forRX)
Configures the USART DMA functionality.
Definition usart.c:94
usart_mode
Available USART modes.
Definition usart.h:84
usart_databits
Available USART databit number.
Definition usart.h:93
usart_peripheral
Available USART peripherals.
Definition usart.h:60
enum usart_databits usart_databits_t
Available USART databit number.
void usart_stop(const usart_peripheral_t usart)
Disable the USART interface.
Definition usart.c:248
void usart_set_interrupts(const usart_peripheral_t usart, const struct USARTISR config)
Enables the specified USART interrupts.
Definition usart.c:111
enum usart_peripheral usart_peripheral_t
Available USART peripherals.
usart_parity
Available USART parities.
Definition usart.h:111
enum usart_parity usart_parity_t
Available USART parities.
void usart_set_databits(const usart_peripheral_t usart, const usart_stopbits_t stopbits, const usart_databits_t databits)
Sets the USART databits to the specified values.
Definition usart.c:145
uint16_t usart_rx_byte(const usart_peripheral_t usart)
Reads the received data from the USART buffer.
Definition usart.c:234
usart_stopbits
Available USART stopbit number.
Definition usart.h:101
void usart_start(const usart_peripheral_t usart, const uint32_t baudrate, const usart_mode_t mode)
Initiates the USART peripheral with specified options.
Definition usart.c:68
void usart_set_parity(const usart_peripheral_t usart, const usart_parity_t parity)
Sets the USART parity to the specified mode.
Definition usart.c:182
enum usart_stopbits usart_stopbits_t
Available USART stopbit number.
void usart_tx_message(const usart_peripheral_t usart, const char *message)
Writes specified message to USART buffer.
Definition usart.c:220
enum usart_mode usart_mode_t
Available USART modes.