-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusart_hello.c
75 lines (65 loc) · 1.96 KB
/
usart_hello.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* Say hello on USART1
*/
#include <init.h>
#include <rcc.h>
#include <gpio.h>
#include <usart.h>
int
main(void)
{
volatile struct usart *usart = USART1;
const char *message = "Hello, world!\r\n";
const char *p;
unsigned int c;
/* Basic init */
init();
/*
* Configure pins
*/
/* Enable clock to I/O port A */
RCC->apb2enr |= RCC_APB2ENR_IOPAEN_MASK;
/* Configure TX pin (PA9) */
GPIO_A->crh =
(GPIO_A->crh & (~GPIO_CRH_MODE9_MASK) & (~GPIO_CRH_CNF9_MASK)) |
(GPIO_MODE_OUTPUT_50MHZ << GPIO_CRH_MODE9_LSB) |
(GPIO_CNF_OUTPUT_AF_PUSH_PULL << GPIO_CRH_CNF9_LSB);
/* Configure RX pin (PA10) */
GPIO_A->crh =
(GPIO_A->crh & (~GPIO_CRH_MODE10_MASK) & (~GPIO_CRH_CNF10_MASK)) |
(GPIO_MODE_INPUT << GPIO_CRH_MODE10_LSB) |
(GPIO_CNF_INPUT_FLOATING << GPIO_CRH_CNF10_LSB);
/*
* Configure USART
*/
/* Enable clock to USART1 */
RCC->apb2enr |= RCC_APB2ENR_USART1EN_MASK;
/* Enable USART, leave the default mode of 8N1 */
usart->cr1 |= USART_CR1_UE_MASK;
/* Set baud rate of 115200 based on APB2 clock at 72MHz */
usart->brr = usart_brr_val(72 * 1000 * 1000, 115200);
/* Enable receiver and transmitter */
usart->cr1 |= USART_CR1_RE_MASK | USART_CR1_TE_MASK;
/*
* Transfer
*/
while (1) {
/* Wait for Enter to be pressed */
do {
/* Wait for receive register to fill */
while (!(usart->sr & USART_SR_RXNE_MASK));
/* Read the byte */
c = usart->dr;
} while (c != '\r');
/* Output the message */
for (p = message; *p != 0; p++) {
/* Wait for transmit register to empty */
while (!(usart->sr & USART_SR_TXE_MASK));
/* Write the byte */
usart->dr = *p;
}
/* Wait for transfer to complete, if any */
if (p > message)
while (!(usart->sr & USART_SR_TC_MASK));
}
}