NavHAL 0.1.0
NAVRobotec's architecture-agnostic HAL for embedded systems.
Loading...
Searching...
No Matches
uart.h
Go to the documentation of this file.
1
24
25#ifndef CORTEX_M4_UART_H
26#define CORTEX_M4_UART_H
27
28#include "common/hal_types.h"
29#include <stdint.h>
35typedef enum {
40
58
59#define uart2_write(val) \
60 _Generic((val), \
61 char: uart2_write_char, \
62 signed char: uart2_write_int, \
63 unsigned char: uart2_write_int, \
64 short: uart2_write_int, \
65 unsigned short: uart2_write_int, \
66 int: uart2_write_int, \
67 unsigned int: uart2_write_int, \
68 long: uart2_write_int, \
69 unsigned long: uart2_write_int, \
70 long long: uart2_write_int, \
71 unsigned long long: uart2_write_int, \
72 float: uart2_write_float, \
73 double: uart2_write_float, \
74 const char *: uart2_write_string, \
75 char *: uart2_write_string)(val)
76
77#define uart1_write(val) \
78 _Generic((val), \
79 char: uart1_write_char, \
80 signed char: uart1_write_int, \
81 unsigned char: uart1_write_int, \
82 short: uart1_write_int, \
83 unsigned short: uart1_write_int, \
84 int: uart1_write_int, \
85 unsigned int: uart1_write_int, \
86 long: uart1_write_int, \
87 unsigned long: uart1_write_int, \
88 long long: uart1_write_int, \
89 unsigned long long: uart1_write_int, \
90 float: uart1_write_float, \
91 double: uart1_write_float, \
92 const char *: uart1_write_string, \
93 char *: uart1_write_string)(val)
94
95#define uart6_write(val) \
96 _Generic((val), \
97 char: uart6_write_char, \
98 signed char: uart6_write_int, \
99 unsigned char: uart6_write_int, \
100 short: uart6_write_int, \
101 unsigned short: uart6_write_int, \
102 int: uart6_write_int, \
103 unsigned int: uart6_write_int, \
104 long: uart6_write_int, \
105 unsigned long: uart6_write_int, \
106 long long: uart6_write_int, \
107 unsigned long long: uart6_write_int, \
108 float: uart6_write_float, \
109 double: uart6_write_float, \
110 const char *: uart6_write_string, \
111 char *: uart6_write_string)(val)
112 // end of UART_WRITE_MACROS
114
123
124/* Peripheral memory map */
125#define PERIPH_BASE 0x40000000UL
126#define AHB1PERIPH_BASE (PERIPH_BASE + 0x00020000UL)
127#define APB1PERIPH_BASE (PERIPH_BASE + 0x00000000UL)
128#define APB2PERIPH_BASE (PERIPH_BASE + 0x7400)
129
130/* Clock control */
131#define RCC_BASE (AHB1PERIPH_BASE + 0x3800)
132
133/* UART base addresses */
134#define USART2_BASE (APB1PERIPH_BASE + 0x4400)
135#define USART1_BASE 0x40011000
136#define USART6_BASE 0x40011400
137
138/* Register definitions */
139#define RCC_APB1ENR (*(__IO uint32_t *)(RCC_BASE + 0x40))
140#define RCC_APB2ENR (*(__IO uint32_t *)(RCC_BASE + 0x44))
141
142/* USART2 registers */
143#define USART2_SR (*(__IO uint32_t *)(USART2_BASE + 0x00))
144#define USART2_DR (*(__IO uint32_t *)(USART2_BASE + 0x04))
145#define USART2_BRR (*(__IO uint32_t *)(USART2_BASE + 0x08))
146#define USART2_CR1 (*(__IO uint32_t *)(USART2_BASE + 0x0C))
147
148/* USART1 registers */
149#define USART1_SR (*(__IO uint32_t *)(USART1_BASE + 0x00))
150#define USART1_DR (*(__IO uint32_t *)(USART1_BASE + 0x04))
151#define USART1_BRR (*(__IO uint32_t *)(USART1_BASE + 0x08))
152#define USART1_CR1 (*(__IO uint32_t *)(USART1_BASE + 0x0C))
153
154/* USART6 registers */
155#define USART6_SR (*(__IO uint32_t *)(USART6_BASE + 0x00))
156#define USART6_DR (*(__IO uint32_t *)(USART6_BASE + 0x04))
157#define USART6_BRR (*(__IO uint32_t *)(USART6_BASE + 0x08))
158#define USART6_CR1 (*(__IO uint32_t *)(USART6_BASE + 0x0C))
159
160/* Clock enable bits */
161#define RCC_APB1ENR_USART2EN (1 << 17)
162#define RCC_APB2ENR_USART1EN (1 << 4)
163#define RCC_APB2ENR_USART6EN (1 << 5)
164
165/* Control register bits */
166#define USART_CR1_UE (1 << 13)
167#define USART_CR1_TE (1 << 3)
168#define USART_CR1_RE (1 << 2)
169
170/* Status register bits */
171#define USART_SR_TXE (1 << 7)
172#define USART_SR_RXNE (1 << 5)
173 // end of UART_REGISTERS
175
184
194void uart_init(uint32_t baudrate, hal_uart_t uart);
195
204void uart_write_char(char c, hal_uart_t uart);
205
214void uart_write_int(int32_t num, hal_uart_t uart);
215
225void uart_write_float(float num, hal_uart_t uart);
226
235void uart_write_string(const char *s, hal_uart_t uart);
236
237/* USART1 specific functions */
238
244void uart1_init(uint32_t baudrate);
245
249void uart1_write_char(char c);
250
254void uart1_write_int(int32_t num);
255
259void uart1_write_float(float num);
260
264void uart1_write_string(const char *s);
265
266/* USART2 specific functions */
267
271void uart2_init(uint32_t baudrate);
272void uart2_write_char(char c);
273void uart2_write_int(int32_t num);
274void uart2_write_float(float num);
275void uart2_write_string(const char *s);
276
277/* USART6 specific functions */
278
282void uart6_init(uint32_t baudrate);
283void uart6_write_char(char c);
284void uart6_write_int(int32_t num);
285void uart6_write_float(float num);
286void uart6_write_string(const char *s);
287
296char uart_read_char(hal_uart_t uart);
297
304int uart_available(hal_uart_t uart);
305
306/* USART1 specific receive functions */
307int uart1_available(void);
308char uart1_read_char(void);
309
310/* USART2 specific receive functions */
311int uart2_available(void);
312char uart2_read_char(void);
313
314/* USART6 specific receive functions */
315int uart6_available(void);
316char uart6_read_char(void);
317
336uint32_t uart2_read_until(char *buffer, uint32_t maxlen, char delimiter);
337 // end of UART_API
339
340#endif // !CORTEX_M4_UART_H
void uart6_write_int(int32_t num)
Transmit a 32-bit signed integer via USART6.
Definition uart.c:307
void uart_write_int(int32_t num, hal_uart_t uart)
Transmit a 32-bit signed integer via UART.
Definition uart.c:81
char uart1_read_char(void)
Read a single character from USART1.
Definition uart.c:463
int uart1_available(void)
Check if data is available to read from USART1.
Definition uart.c:516
void uart2_write_float(float num)
Transmit a floating-point number via USART2.
Definition uart.c:344
void uart6_write_float(float num)
Transmit a floating-point number via USART6.
Definition uart.c:385
void uart1_init(uint32_t baudrate)
Initialize USART1 peripheral.
Definition uart.c:136
void uart2_write_string(const char *s)
Transmit a null-terminated string via USART2.
Definition main.c:97
uint32_t uart2_read_until(char *buffer, uint32_t maxlen, char delimiter)
Read characters into a buffer until a delimiter is found or max length is reached.
Definition uart.c:546
void uart_write_float(float num, hal_uart_t uart)
Transmit a floating-point number via UART.
Definition uart.c:99
void uart_write_char(char c, hal_uart_t uart)
Transmit a single character via the specified UART.
Definition uart.c:54
void uart6_init(uint32_t baudrate)
Initialize USART6 peripheral.
Definition uart.c:157
void uart1_write_int(int32_t num)
Transmit a 32-bit signed integer via USART1.
Definition uart.c:273
void uart1_write_float(float num)
Transmit a floating-point number via USART1.
Definition uart.c:365
char uart_read_char(hal_uart_t uart)
Read a single character from the specified UART.
Definition uart.c:447
char uart2_read_char(void)
Read a single character from USART2.
Definition uart.c:475
int uart_available(hal_uart_t uart)
Check if data is available to read.
Definition uart.c:500
char uart6_read_char(void)
Read a single character from USART6.
Definition uart.c:487
void uart1_write_char(char c)
Transmit a single character via USART1.
Definition uart.c:212
void uart_write_string(const char *s, hal_uart_t uart)
Transmit a null-terminated string via UART.
Definition uart.c:115
void uart6_write_char(char c)
Transmit a single character via USART6.
Definition uart.c:224
void uart1_write_string(const char *s)
Transmit a null-terminated string via USART1.
Definition uart.c:418
void uart2_write_int(int32_t num)
Transmit a 32-bit signed integer via USART2.
Definition uart.c:239
void uart6_write_string(const char *s)
Transmit a null-terminated string via USART6.
Definition uart.c:431
void uart2_write_char(char c)
Transmit a single character via USART2.
Definition main.c:61
void uart_init(uint32_t baudrate, hal_uart_t uart)
Initialize the specified UART peripheral.
Definition uart.c:38
int uart2_available(void)
Check if data is available to read from USART2.
Definition uart.c:523
int uart6_available(void)
Check if data is available to read from USART6.
Definition uart.c:530
Hardware Abstraction Layer (HAL) common type definitions.
void uart2_init(void)
Definition main.c:41
float char c
Definition test_unity_parameterized.c:279
hal_uart_t
UART identifier enumeration.
Definition uart.h:35
@ UART2
USART2 - APB1 peripheral.
Definition uart.h:37
@ UART6
USART6 - APB2 peripheral.
Definition uart.h:38
@ UART1
USART1 - APB2 peripheral, typically higher speed.
Definition uart.h:36