embox

Форк
0
/
stm32_usart_vl.c 
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

20
EMBOX_UNIT_INIT(stm32uart_mod_init);
21

22
struct uart_stm32 {
23
	uint16_t sr;
24
	uint16_t reserv0;
25
	uint16_t dr;
26
	uint16_t reserv1;
27
	uint16_t brr;
28
	uint16_t reserv2;
29
	uint16_t cr1;
30
	uint16_t reserv3;
31
	uint16_t cr2;
32
	uint16_t reserv4;
33
	uint16_t cr3;
34
	uint16_t reserv5;
35
	uint16_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

64
static int stm32_uart_putc(struct uart *dev, int ch) {
65
	struct uart_stm32 *uart = (struct uart_stm32 *) dev->base_addr;
66

67
	while (!(REG_LOAD(&uart->sr) & USART_FLAG_TXE)) { }
68

69
	REG_STORE(&uart->dr, ch);
70

71
	return 0;
72
}
73

74
static int stm32_uart_hasrx(struct uart *dev) {
75
	struct uart_stm32 *uart = (struct uart_stm32 *) dev->base_addr;
76

77
	return (REG_LOAD(&uart->sr) & USART_FLAG_RXNE);
78
}
79

80
static int stm32_uart_getc(struct uart *dev) {
81
	struct uart_stm32 *uart = (struct uart_stm32 *) dev->base_addr;
82
	return REG_LOAD(&uart->dr);
83
}
84

85
static int stm32_uart_setup(struct uart *dev, const struct uart_params *params) {
86
	struct uart_stm32 *uart = (struct uart_stm32 *) dev->base_addr;
87

88
	REG_ORIN(RCC_APB1RSTR,RCC_APB1PWR);
89
	REG_ORIN(RCC_APB2ENR,RCC_APB2GPIOx);
90
	REG_ORIN(RCC_APB2ENR,RCC_APB2AFIO);
91

92
	REG_ORIN(RCC_APB2ENR,RCC_APB2ENR_USART1EN);
93

94
	gpio_setup_mode(UART_GPIO, TX_PIN ,
95
			GPIO_MODE_OUT | GPIO_MODE_OUT_ALTERNATE);
96
	gpio_setup_mode(UART_GPIO, RX_PIN, GPIO_MODE_IN);
97

98
	REG_STORE(&uart->brr, SYS_CLOCK / params->baud_rate);
99
	REG_ORIN(&uart->cr1, USART_FLAG_RE | USART_FLAG_TE);
100

101
	REG_ORIN(&uart->cr1, USART_FLAG_UE);
102

103
	return 0;
104
}
105

106
static 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

113
static struct uart stm32_uart0 = {
114
		.uart_ops = &stm32_uart_ops,
115
		.irq_num = 0,
116
		.base_addr = UART0,
117
};
118

119
static 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

124
DIAG_SERIAL_DEF(&stm32_uart0, &uart_defparams);
125

126
static int stm32uart_mod_init(void) {
127
	/*if (!uart_register(&stm32_uart0, &uart_defparams)) {*/
128
		/*return -1;*/
129
	/*}*/
130

131
	return 0;
132
}
133

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.