Loading...
Searching...
No Matches
uart.h
1
8
9// ALLOW FORMATTING
10#ifndef UART_H
11#define UART_H
12
13#include "stdbool.h"
14#include "stm32f439xx.h"
15
16#define UART_PARITY_DISABLE 0x400
17#define UART_SYNC_DISABLE 0xE00
18#define UART_FLOW_DISABLE 0x300
19#define UART_ENABLE 0x2000
20#define UART_RX_ENABLE 0x0004
21#define UART_TX_ENABLE 0x0008
22#define UART_AF7 0x07
23#define UART_AF8 0x08
24
25#define UART_CR1_RESERVED (0x01 << 14)
26
27#define SIGINT 0x03
28#define BACKSPACE 0x08
29#define LINE_FEED 0x0A
30#define FORM_FEED 0x0B
31#define CARRIAGE_RETURN 0x0D
32#define SUBSTITUTE 0x1A
33#define DEL 0x7F
34
35// Macro definitions for UART config literals
36//
37// clang-format off
38
39// Default configuration for UART peripheral
40#define UART_CONFIG_DEFAULT \
41 (UART_Config) { \
42 .OVER8 = UART_OVER8, \
43 .M = UART_WORD8, \
44 .WAKE = UART_WAKEUP_IDLE, \
45 .PCE = false, \
46 .PS = UART_PARITY_EVEN, \
47 .PEIE = false, \
48 .TXEIE = false, \
49 .TCIE = false, \
50 .RXNEIE = false, \
51 .IDLEIE = false, \
52 .TE = true, \
53 .RE = true, \
54 .RWU = UART_RECEIVER_ACTIVE \
55 }
56// clang-format on
57
64
69typedef enum {
70 UART_OVER16, // Oversampling by 16
71 UART_OVER8 // Oversampling by 8
73
78typedef enum {
79 UART_WORD8, // Use 8 data bits
80 UART_WORD9 // Use 9 data bits
82
87typedef enum {
88 UART_WAKEUP_IDLE, //
89 UART_WAKEUP_ADDRESS //
90} UART_Wake;
91
96typedef enum {
97 UART_PARITY_EVEN, // Use even parity
98 UART_PARITY_ODD // Use odd parity
100
105typedef enum {
106 UART_RECEIVER_ACTIVE, // Receiver is active
107 UART_RECEIVER_MUTE // Receiver is in mute mode
109
110typedef struct UART_Config {
111 bool SBK : 1;
113 bool RE : 1;
114 bool TE : 1;
115 bool IDLEIE : 1;
116 bool RXNEIE : 1;
117 bool TCIE : 1;
118 bool TXEIE : 1;
119 bool PEIE : 1;
121 bool PCE : 1;
124 bool UE : 1;
125 unsigned int : 1;
128
132typedef struct UART {
133 USART_TypeDef *interface;
134 UART_Config config;
135 uint32_t baud;
137 void (*setBaud)(struct UART *, uint32_t);
138 void (*send)(struct UART *, uint8_t);
139 void (*sendBytes)(struct UART *, uint8_t *, int);
140 void (*print)(struct UART *, char *);
141 void (*println)(struct UART *, char *);
142 uint8_t (*receive)(struct UART *);
143} UART_t;
144
145UART_t UART_init(USART_TypeDef *interface, uint32_t baud, UART_Config *config);
147
148void UART_setBaud(UART_t *, uint32_t);
149
150void UART_send(UART_t *, uint8_t data);
151void UART_sendBytes(UART_t *, uint8_t *data, int length);
152void UART_print(UART_t *, char *data);
153void UART_println(UART_t *, char *data);
154uint8_t UART_receive(UART_t *);
155
157#endif
bool SBK
| (default )
Definition uart.h:111
void(* print)(struct UART *, char *)
UART print string method.
Definition uart.h:140
bool PCE
| (default )
Definition uart.h:121
void(* sendBytes)(struct UART *, uint8_t *, int)
UART send multiple bytes method.
Definition uart.h:139
bool RE
| (default )
Definition uart.h:113
void(* send)(struct UART *, uint8_t)
UART send method.
Definition uart.h:138
UART_WordLength M
| (default )
Definition uart.h:123
bool PEIE
| (default )
Definition uart.h:119
bool UE
| (default )
Definition uart.h:124
bool TCIE
| (default )
Definition uart.h:117
UART_OversampleMode OVER8
| (default )
Definition uart.h:126
uint8_t(* receive)(struct UART *)
UART receive method.
Definition uart.h:142
void(* println)(struct UART *, char *)
UART print line method.
Definition uart.h:141
bool IDLEIE
| (default )
Definition uart.h:115
bool TE
| (default )
Definition uart.h:114
bool TXEIE
| (default )
Definition uart.h:118
UART_ReceiverWake RWU
| (default )
Definition uart.h:112
unsigned int
RESERVED.
Definition uart.h:125
UART_Wake WAKE
| (default )
Definition uart.h:122
bool RXNEIE
| (default )
Definition uart.h:116
UART_ParitySelect PS
| (default )
Definition uart.h:120
void UART_send(UART_t *, uint8_t data)
Sends a single byte of data over the UART interface.
Definition uart.c:127
UART_ReceiverWake
UART receiver wake up enum.
Definition uart.h:105
void UART_updateConfig(UART_t *, UART_Config *)
Update UART peripheral configuration.
Definition uart.c:207
UART_ParitySelect
UART parity selection enum.
Definition uart.h:96
UART_OversampleMode
UART oversampling mode enum.
Definition uart.h:69
void UART_sendBytes(UART_t *, uint8_t *data, int length)
Sends an array of bytes over the UART interface.
Definition uart.c:144
UART_WordLength
UART word length enum.
Definition uart.h:78
uint8_t UART_receive(UART_t *)
Receives a single byte of data from the UART interface.
Definition uart.c:189
UART_t UART_init(USART_TypeDef *interface, uint32_t baud, UART_Config *config)
Initialiser for a UART device interface.
Definition uart.c:27
void UART_print(UART_t *, char *data)
Sends a null terminated string over the UART interface.
Definition uart.c:159
void UART_println(UART_t *, char *data)
Sends a null terminated string over the UART interface. Terminates with a line feed control character...
Definition uart.c:176
UART_Wake
UART wake up method enum.
Definition uart.h:87
Struct definition for UART interface.
Definition uart.h:132