-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusart.cpp
104 lines (90 loc) · 2.63 KB
/
usart.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "usart.h"
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
UsartDriver::UsartDriver(uint32 usartn)
{
usart = usartn;
}
void UsartDriver::begin(uint32 baud, uint32 bits, uint32 stopbits, uint32 parity)
{
/* Setup GPIO pins */
switch(usart)
{
case USART1:
{
rcc_periph_clock_enable(RCC_USART1);
nvic_enable_irq(NVIC_USART1_IRQ);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX);
}
break;
case USART2:
{
rcc_periph_clock_enable(RCC_USART2);
nvic_enable_irq(NVIC_USART2_IRQ);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);
gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX);
}
break;
case USART3:
{
rcc_periph_clock_enable(RCC_USART3);
nvic_enable_irq(NVIC_USART3_IRQ);
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART3_TX);
gpio_set_mode(GPIOB, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_FLOAT, GPIO_USART3_RX);
}
break;
}
/* Setup UART parameters. */
usart_set_baudrate(usart, baud);
usart_set_databits(usart, bits);
usart_set_stopbits(usart, stopbits);
usart_set_parity(usart, parity);
usart_set_flow_control(usart, USART_FLOWCONTROL_NONE);
usart_set_mode(usart, USART_MODE_TX_RX);
USART_CR1(usart) |= USART_CR1_RXNEIE;
/* Finally enable the USART. */
usart_enable(usart);
}
void UsartDriver::disable()
{
usart_disable(usart);
}
void usart1_isr(void)
{
/* Check if we were called because of RXNE. */
if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) &&
((USART_SR(USART1) & USART_SR_RXNE) != 0))
{
Serial1.push(usart_recv(USART1));
}
}
void usart2_isr(void)
{
/* Check if we were called because of RXNE. */
if (((USART_CR1(USART2) & USART_CR1_RXNEIE) != 0) &&
((USART_SR(USART2) & USART_SR_RXNE) != 0))
{
char c = usart_recv(USART2);
Serial2.push(c);
}
}
void usart3_isr(void)
{
/* Check if we were called because of RXNE. */
if (((USART_CR1(USART3) & USART_CR1_RXNEIE) != 0) &&
((USART_SR(USART3) & USART_SR_RXNE) != 0))
{
char c = usart_recv(USART3);
Serial3.push(c);
}
}
UsartDriver Serial1(USART1);
UsartDriver Serial2(USART2);
UsartDriver Serial3(USART3);