embox
132 строки · 2.9 Кб
1/**
2* @file
3* @brief
4*
5* @author Anton Kozlov
6* @date 03.07.2012
7*/
8
9#include <stdint.h>
10
11#include <drivers/gpio/gpio.h>
12#include <hal/reg.h>
13#include <hal/system.h>
14#include <drivers/diag.h>
15#include <drivers/serial/diag_serial.h>
16#include <embox/unit.h>
17
18#include <drivers/serial/uart_dev.h>
19
20EMBOX_UNIT_INIT(stm32uart_mod_init);
21
22struct uart_stm32 {
23uint16_t sr;
24uint16_t reserv0;
25uint16_t dr;
26uint16_t reserv1;
27uint16_t brr;
28uint16_t reserv2;
29uint16_t cr1;
30uint16_t reserv3;
31uint16_t cr2;
32uint16_t reserv4;
33uint16_t cr3;
34uint16_t reserv5;
35uint16_t gtpr;
36} __attribute__ ((packed));
37
38#define RCC_APB1RSTR 0x40021010
39#define RCC_APB1PWR 0x10000000
40#define RCC_APB2ENR 0x40021018
41#define RCC_APB2GPIOC 0x00000010
42
43#define RCC_APB2GPIOx 0x000001fc
44#define RCC_APB2AFIO 0x00000001
45
46#define UART0 (0x40013800)
47
48#define UART_GPIO GPIO_PORT_A
49
50#define TX_PIN (1 << 9)
51#define RX_PIN (1 << 10)
52#define CTS_PIN (1 << 11)
53#define RTS_PIN (1 << 12)
54
55#define USART_FLAG_RXNE (1 << 5)
56#define USART_FLAG_TXE (1 << 7)
57
58#define USART_FLAG_UE (1 << 13) /* USART enable */
59#define USART_FLAG_RE (1 << 2) /* Receive enable */
60#define USART_FLAG_TE (1 << 3) /* Transmit enable */
61
62#define RCC_APB2ENR_USART1EN (1 << 14)
63
64static int stm32_uart_putc(struct uart *dev, int ch) {
65struct uart_stm32 *uart = (struct uart_stm32 *) dev->base_addr;
66
67while (!(REG_LOAD(&uart->sr) & USART_FLAG_TXE)) { }
68
69REG_STORE(&uart->dr, ch);
70
71return 0;
72}
73
74static int stm32_uart_hasrx(struct uart *dev) {
75struct uart_stm32 *uart = (struct uart_stm32 *) dev->base_addr;
76
77return (REG_LOAD(&uart->sr) & USART_FLAG_RXNE);
78}
79
80static int stm32_uart_getc(struct uart *dev) {
81struct uart_stm32 *uart = (struct uart_stm32 *) dev->base_addr;
82return REG_LOAD(&uart->dr);
83}
84
85static int stm32_uart_setup(struct uart *dev, const struct uart_params *params) {
86struct uart_stm32 *uart = (struct uart_stm32 *) dev->base_addr;
87
88REG_ORIN(RCC_APB1RSTR,RCC_APB1PWR);
89REG_ORIN(RCC_APB2ENR,RCC_APB2GPIOx);
90REG_ORIN(RCC_APB2ENR,RCC_APB2AFIO);
91
92REG_ORIN(RCC_APB2ENR,RCC_APB2ENR_USART1EN);
93
94gpio_setup_mode(UART_GPIO, TX_PIN ,
95GPIO_MODE_OUT | GPIO_MODE_OUT_ALTERNATE);
96gpio_setup_mode(UART_GPIO, RX_PIN, GPIO_MODE_IN);
97
98REG_STORE(&uart->brr, SYS_CLOCK / params->baud_rate);
99REG_ORIN(&uart->cr1, USART_FLAG_RE | USART_FLAG_TE);
100
101REG_ORIN(&uart->cr1, USART_FLAG_UE);
102
103return 0;
104}
105
106static const struct uart_ops stm32_uart_ops = {
107.uart_getc = stm32_uart_getc,
108.uart_putc = stm32_uart_putc,
109.uart_hasrx = stm32_uart_hasrx,
110.uart_setup = stm32_uart_setup,
111};
112
113static struct uart stm32_uart0 = {
114.uart_ops = &stm32_uart_ops,
115.irq_num = 0,
116.base_addr = UART0,
117};
118
119static const struct uart_params uart_defparams = {
120.baud_rate = OPTION_GET(NUMBER,baud_rate),
121.uart_param_flags = UART_PARAM_FLAGS_8BIT_WORD,
122};
123
124DIAG_SERIAL_DEF(&stm32_uart0, &uart_defparams);
125
126static int stm32uart_mod_init(void) {
127/*if (!uart_register(&stm32_uart0, &uart_defparams)) {*/
128/*return -1;*/
129/*}*/
130
131return 0;
132}
133