-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathterminal.c
183 lines (156 loc) · 4.47 KB
/
terminal.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* terminal.c
*
* Example initialization
*
* Initialize_USART_RCC();
* Initialize_USART(USART2);
*
*/
#include "terminal.h"
/*Configure which USARTs should be used*/
#define USE_UART1
#define USE_UART2
//#define USE_UART3
/*Configure which USARTs should use interrupts*/
//#define USE_INT1
#define USE_INT2
//#define USE_INT3
/*Configure which USARTs should use DMA*/
//#define USE_DMA1
//#define USE_DMA2
//#define USE_DMA3
void Initialize_UART(void)
{
/*************************************************************************************************************/
/*Initialize USARTs RCCs*/
#ifdef USE_UART1
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//uncomment if not turned on earlier
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
#endif
#ifdef USE_UART2
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
#endif
#ifdef USE_UART3
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
#endif
/*************************************************************************************************************/
/*Initialize GPIO for USART*/
GPIO_InitTypeDef gpio;
GPIO_StructInit(&gpio);
#ifdef USE_UART1
//TX LINE
gpio.GPIO_Pin = GPIO_Pin_9;
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &gpio);
//RX LINE
gpio.GPIO_Pin = GPIO_Pin_10;
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &gpio);
//USARTx configuration
USART_InitTypeDef uart;
USART_StructInit(&uart);
uart.USART_BaudRate = 115385;///////////////////////////////////////////////////////
USART_Init(USART1, &uart);
USART_Cmd(USART1, ENABLE);
#ifdef USE_INT1
//Uruchomienie przerwania od USART1
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
#endif
#endif
#ifdef USE_UART2
//TX LINE
gpio.GPIO_Pin = GPIO_Pin_2;
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &gpio);
//RX LINE
gpio.GPIO_Pin = GPIO_Pin_3;
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &gpio);
//USARTx configuration
USART_InitTypeDef uart2;
USART_StructInit(&uart2);
uart2.USART_BaudRate = 115200;
USART_Init(USART2, &uart2);
USART_Cmd(USART2, ENABLE);
#ifdef USE_INT2
//Uruchomienie przerwania od USART2
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
USART_ITConfig(USART2,USART_IT_TXE,ENABLE);
#endif
#endif
#ifdef USE_UART3
//TX LINE
gpio.GPIO_Pin = GPIO_Pin_10;
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &gpio);
//RX LINE
gpio.GPIO_Pin = GPIO_Pin_11;
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &gpio);
//USARTx configuration
USART_InitTypeDef uart;
USART_StructInit(&uart);
uart.USART_BaudRate = 115200;
USART_Init(USART3, &uart);
USART_Cmd(USART3, ENABLE);
#ifdef USE_INT3
//Uruchomienie przerwania od USART3
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
USART_ITConfig(USART3,USART_IT_TXE,ENABLE);
#endif
#endif
/*************************************************************************************************************/
// NOT TESTED
// #ifdef USE_DMA
// USART_DMACmd(USART1, USART_DMAReq_Tx | USART_DMAReq_Rx, ENABLE);
// #endif
/*************************************************************************************************************/
}
/*use to send single byte without interrupts*/
void UART_SendChar(USART_TypeDef * USARTx, char c)
{
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
USART_SendData(USARTx, c);
}
/*use to send string without interrupts*/
void UART_SendString(USART_TypeDef * USARTx, char* s)
{
while(*s)
UART_SendChar(USARTx,*s++);
}
/*get char from the UART Data Register without interrupts*/
char UART_GetChar(USART_TypeDef * USARTx)
{
char data;
//czekaj az bufor odbiorczy bedzie pusty
while(USART_GetFlagStatus(USARTx, USART_FLAG_RXNE) == RESET);
data = USART_ReceiveData(USARTx);
return data;
}
//uncomment to use printf() and in terminal.h uncomment #include <stdio.h>
//useful when debugging
//int __io_putchar(int c)
//{
//if (c=='\n')
//
////#ifdef USE_UART1
////send_char(USART1,'\r');
////send_char(USART1,c);
////#endif
//
//#ifdef USE_UART2
//send_char(USART2,'\r');
//send_char(USART2,c);
//#endif
//
////#ifdef USE_UART3
////send_char(USART3,'\r');
////send_char(USART3,c);
////#endif
//
//return c;
//}