-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart1.c
300 lines (232 loc) · 7.2 KB
/
uart1.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* uart1.c
*
* Created on: Apr 11, 2018
* Author: mak
*/
#include <stdlib.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/usart.h>
#include <libopencm3/cm3/nvic.h>
#include "uart1.h"
int nndebcc;
void nndeb()
{
NOOP();
nndebcc++;
}
int nndeb0cc;
void nndeb0()
{
NOOP();
nndeb0cc++;
}
#define ComBufSize ( 1 << 9 )
char ComBuf2[ComBufSize];
volatile int ComBufIn2=1;
volatile int ComBufOut2=0;
void ComClr2()
{ ComBufIn2=1; ComBufOut2=0;
}
volatile int ComCnt2() /* get how many bytes are in the buffer*/
{ return ( (ComBufIn2-ComBufOut2-1) & (ComBufSize-1) );
}
unsigned char GetPort2() /* get a character from the buffer*/
{ return ComBuf2[ (++ComBufOut2) & (ComBufSize-1) ];
}
void ComPut2(unsigned char Data)
{ ComBuf2[ (ComBufIn2++) & (ComBufSize-1) ] = Data;
}
unsigned char WGetPort2() /* get a character from the buffer*/
{ while(!ComCnt2());
return GetPort2();
}
void UART2_Transmit(unsigned char * buf, int len)
{
while(len--) usart_send_blocking(USART2,*buf++);
}
void clock_setup(void)
{
rcc_clock_setup_in_hse_8mhz_out_72mhz();
/* Enable GPIOA clock (for LED GPIOs). */
rcc_periph_clock_enable(RCC_GPIOC);
/* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_AFIO);
rcc_periph_clock_enable(RCC_USART1);
rcc_periph_clock_enable(RCC_USART2);
rcc_periph_clock_enable(RCC_USART3);
}
void usart1_setup(void)
{
/* Enable the USART1 interrupt. */
nvic_enable_irq(NVIC_USART1_IRQ);
/* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port B for transmit. */
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
/* Setup GPIO pin GPIO_USART1_RE_RX on GPIO port B for receive. */
gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX);
/* Setup UART parameters. */
// usart_set_baudrate(USART1, 230400);
usart_set_baudrate(USART1, 115200);
usart_set_databits(USART1, 8);
usart_set_stopbits(USART1, USART_STOPBITS_1);
usart_set_parity(USART1, USART_PARITY_NONE);
usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
usart_set_mode(USART1, USART_MODE_TX_RX);
/* Enable USART1 Receive interrupt. */
USART_CR1(USART1) |= USART_CR1_RXNEIE;
/* Finally enable the USART. */
usart_enable(USART1);
}
void usart1_isr(void)
{
static uint8_t data = 'A';
/* Check if we were called because of RXNE. */
if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) &&
((USART_SR(USART1) & USART_SR_RXNE) != 0)) {
/* Indicate that we got data. */
gpio_toggle(GPIOC, GPIO12);
/* Retrieve the data from the peripheral. */
data = usart_recv(USART1);
/* Enable transmit interrupt so it sends back the data. */
USART_CR1(USART1) |= USART_CR1_TXEIE;
}
/* Check if we were called because of TXE. */
if (((USART_CR1(USART1) & USART_CR1_TXEIE) != 0) &&
((USART_SR(USART1) & USART_SR_TXE) != 0)) {
/* Indicate that we are sending out data. */
// gpio_toggle(GPIOA, GPIO7);
/* Put data into the transmit register. */
// usart_send(USART1, data);
// ttwrite(&data , 1);
// ComPut1(data);
/* Disable the TXE interrupt as we don't need it anymore. */
USART_CR1(USART1) &= ~USART_CR1_TXEIE;
}
}
void usart2_isr(void)
{
static uint8_t data = 'A';
/* Check if we were called because of RXNE. */
if (((USART_CR1(USART2) & USART_CR1_RXNEIE) != 0) &&
((USART_SR(USART2) & USART_SR_RXNE) != 0)) {
/* Indicate that we got data. */
gpio_toggle(GPIOA, GPIO8);
/* Retrieve the data from the peripheral. */
data = usart_recv(USART2);
/* Enable transmit interrupt so it sends back the data. */
USART_CR1(USART2) |= USART_CR1_TXEIE;
}
/* Check if we were called because of TXE. */
if (((USART_CR1(USART2) & USART_CR1_TXEIE) != 0) &&
((USART_SR(USART2) & USART_SR_TXE) != 0)) {
/* Indicate that we are sending out data. */
gpio_toggle(GPIOC, GPIO15);
/* Put data into the transmit register. */
// mmo usart_send(USART2, data);
ComPut2(data);
/* Disable the TXE interrupt as we don't need it anymore. */
USART_CR1(USART2) &= ~USART_CR1_TXEIE;
}
}
void slcan_usart2_write( uint8_t * adr, int len)
{
while(len--) usart_send_blocking(USART2,*adr++);
}
void br_rest()
{
usart_set_baudrate(USART2, 115200);
}
void usart2_setup(void)
{
/* Enable the USART2 interrupt. */
nvic_enable_irq(NVIC_USART2_IRQ);
/* Setup GPIO pin GPIO_USART2_TX on GPIO port A for transmit. */
gpio_set_mode(GPIO_BANK_USART2_TX, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);
/* Setup GPIO pin GPIO_USART2_RX on GPIO port A for receive. */
gpio_set_mode(GPIO_BANK_USART2_RX, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX);
/* Setup UART parameters. */
usart_set_baudrate(USART2, 115200);
usart_set_databits(USART2, 8);
usart_set_stopbits(USART2, USART_STOPBITS_1);
usart_set_parity(USART2, USART_PARITY_NONE);
usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE);
usart_set_mode(USART2, USART_MODE_TX_RX);
/* Enable USART2 Receive interrupt. */
USART_CR1(USART2) |= USART_CR1_RXNEIE;
/* Finally enable the USART. */
usart_enable(USART2);
}
void usart3_isr(void)
{
static uint8_t data = 'A';
/* Check if we were called because of RXNE. */
if (((USART_CR1(USART3) & USART_CR1_RXNEIE) != 0) &&
((USART_SR(USART3) & USART_SR_RXNE) != 0)) {
/* Indicate that we got data. */
gpio_toggle(GPIOA, GPIO8);
/* Retrieve the data from the peripheral. */
data = usart_recv(USART3);
/* Enable transmit interrupt so it sends back the data. */
USART_CR1(USART3) |= USART_CR1_TXEIE;
}
/* Check if we were called because of TXE. */
if (((USART_CR1(USART3) & USART_CR1_TXEIE) != 0) &&
((USART_SR(USART3) & USART_SR_TXE) != 0)) {
/* Indicate that we are sending out data. */
gpio_toggle(GPIOC, GPIO15);
/* Put data into the transmit register. */
// mmo usart_send(USART3, data);
ComPut2(data);
/* Disable the TXE interrupt as we don't need it anymore. */
USART_CR1(USART3) &= ~USART_CR1_TXEIE;
}
}
void usart3_setup(void)
{
/* Enable the USART3 interrupt. */
nvic_enable_irq(NVIC_USART3_IRQ);
/* Setup GPIO pin GPIO_USART3_TX on GPIO port A for transmit. */
gpio_set_mode(GPIO_BANK_USART3_TX, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART3_TX);
/* Setup GPIO pin GPIO_USART3_RX on GPIO port A for receive. */
gpio_set_mode(GPIO_BANK_USART3_RX, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_FLOAT, GPIO_USART3_RX);
/* Setup UART parameters. */
usart_set_baudrate(USART3, 115200);
usart_set_databits(USART3, 8);
usart_set_stopbits(USART3, USART_STOPBITS_1);
usart_set_parity(USART3, USART_PARITY_NONE);
usart_set_flow_control(USART3, USART_FLOWCONTROL_NONE);
usart_set_mode(USART3, USART_MODE_TX_RX);
/* Enable USART3 Receive interrupt. */
USART_CR1(USART3) |= USART_CR1_RXNEIE;
/* Finally enable the USART. */
usart_enable(USART3);
}
void hht(uint32_t hh )
{ uint8_t pp;
uint8_t ii=8;
for(;;)
{
pp=(hh>>28)&0xf;
if(!--ii) break;
if(pp) break;
// usart_send_blocking(USART1,'_');
hh<<=4;
}
do
{
pp+=0x30;
if(pp>0x39)pp+= 'A' - 0x3a;
usart_send_blocking(USART3,pp);
hh<<=4;
pp=(hh>>28)&0xf;
} while(ii--);
// usart_send_blocking(USART1,'#');
}