-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathserial.c
740 lines (611 loc) · 19.3 KB
/
serial.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*
serial.c - serial port implementation for STM32F4xx ARM processors
Part of grblHAL
Copyright (c) 2019-2022 Terje Io
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "serial.h"
#include "grbl/hal.h"
#include "grbl/protocol.h"
#include "main.h"
static stream_rx_buffer_t rxbuf = {0};
static stream_tx_buffer_t txbuf = {0};
static enqueue_realtime_command_ptr enqueue_realtime_command = protocol_enqueue_realtime_command;
#ifdef SERIAL2_MOD
static stream_rx_buffer_t rxbuf2 = {0};
static stream_tx_buffer_t txbuf2 = {0};
static enqueue_realtime_command_ptr enqueue_realtime_command2 = protocol_enqueue_realtime_command;
#endif
#ifndef SERIAL_MOD
#if IS_NUCLEO_DEVKIT
#define SERIAL_MOD 2
#else
#define SERIAL_MOD 1
#endif
#endif
#define USART usart(SERIAL_MOD)
#define USART_IRQ usartINT(SERIAL_MOD)
#define USART_IRQHandler usartHANDLER(SERIAL_MOD)
#if SERIAL_MOD == 1 || SERIAL_MOD == 6
#define USART_CLK HAL_RCC_GetPCLK2Freq()
#else
#define USART_CLK HAL_RCC_GetPCLK1Freq()
#endif
#ifdef SERIAL2_MOD
#if SERIAL_MOD == SERIAL_MOD2
#error Conflicting use of USART ports!
#endif
#define UART2 usart(SERIAL2_MOD)
#define UART2_IRQ usartINT(SERIAL2_MOD)
#define UART2_IRQHandler usartHANDLER(SERIAL2_MOD)
#if SERIAL2_MOD == 1 || SERIAL2_MOD == 6
#define UART2_CLK HAL_RCC_GetPCLK2Freq()
#else
#define UART2_CLK HAL_RCC_GetPCLK1Freq()
#endif
#endif
static io_stream_properties_t serial[] = {
{
.type = StreamType_Serial,
.instance = 0,
.flags.claimable = On,
.flags.claimed = Off,
.flags.connected = On,
.flags.can_set_baud = On,
.flags.modbus_ready = On,
.claim = serialInit
},
#ifdef SERIAL2_MOD
{
.type = StreamType_Serial,
.instance = 1,
.flags.claimable = On,
.flags.claimed = Off,
.flags.connected = On,
.flags.can_set_baud = On,
.flags.modbus_ready = On,
.claim = serial2Init
}
#endif
};
void serialRegisterStreams (void)
{
static io_stream_details_t streams = {
.n_streams = sizeof(serial) / sizeof(io_stream_properties_t),
.streams = serial,
};
stream_register_streams(&streams);
}
//
// Returns number of free characters in serial input buffer
//
static uint16_t serialRxFree (void)
{
uint16_t tail = rxbuf.tail, head = rxbuf.head;
return (RX_BUFFER_SIZE - 1) - BUFCOUNT(head, tail, RX_BUFFER_SIZE);
}
//
// Returns number of characters in serial input buffer
//
static uint16_t serialRxCount (void)
{
uint32_t tail = rxbuf.tail, head = rxbuf.head;
return BUFCOUNT(head, tail, RX_BUFFER_SIZE);
}
//
// Flushes the serial input buffer
//
static void serialRxFlush (void)
{
rxbuf.tail = rxbuf.head;
}
//
// Flushes and adds a CAN character to the serial input buffer
//
static void serialRxCancel (void)
{
rxbuf.data[rxbuf.head] = ASCII_CAN;
rxbuf.tail = rxbuf.head;
rxbuf.head = BUFNEXT(rxbuf.head, rxbuf);
}
//
// Writes a character to the serial output stream
//
static bool serialPutC (const char c)
{
uint16_t next_head = BUFNEXT(txbuf.head, txbuf); // Get pointer to next free slot in buffer
while(txbuf.tail == next_head) { // While TX buffer full
if(!hal.stream_blocking_callback()) // check if blocking for space,
return false; // exit if not (leaves TX buffer in an inconsistent state)
}
txbuf.data[txbuf.head] = c; // Add data to buffer,
txbuf.head = next_head; // update head pointer and
USART->CR1 |= USART_CR1_TXEIE; // enable TX interrupts
return true;
}
//
// Writes a null terminated string to the serial output stream, blocks if buffer full
//
static void serialWriteS (const char *s)
{
char c, *ptr = (char *)s;
while((c = *ptr++) != '\0')
serialPutC(c);
}
//
// Writes a number of characters from string to the serial output stream followed by EOL, blocks if buffer full
//
static void serialWrite (const char *s, uint16_t length)
{
char *ptr = (char *)s;
while(length--)
serialPutC(*ptr++);
}
//
// Flushes the serial output buffer
//
static void serialTxFlush (void)
{
USART->CR1 &= ~USART_CR1_TXEIE; // Disable TX interrupts
txbuf.tail = txbuf.head;
}
//
// Returns number of characters pending transmission
//
static uint16_t serialTxCount (void)
{
uint32_t tail = txbuf.tail, head = txbuf.head;
return BUFCOUNT(head, tail, TX_BUFFER_SIZE) + (USART->SR & USART_SR_TC ? 0 : 1);
}
//
// serialGetC - returns -1 if no data available
//
static int16_t serialGetC (void)
{
uint_fast16_t tail = rxbuf.tail; // Get buffer pointer
if(tail == rxbuf.head)
return -1; // no data available
char data = rxbuf.data[tail]; // Get next character
rxbuf.tail = BUFNEXT(tail, rxbuf); // and update pointer
return (int16_t)data;
}
static bool serialSuspendInput (bool suspend)
{
return stream_rx_suspend(&rxbuf, suspend);
}
static bool serialSetBaudRate (uint32_t baud_rate)
{
USART->CR1 = USART_CR1_RE|USART_CR1_TE;
USART->BRR = UART_BRR_SAMPLING16(USART_CLK, baud_rate);
USART->CR1 |= (USART_CR1_UE|USART_CR1_RXNEIE);
return true;
}
static bool serialDisable (bool disable)
{
if(disable)
USART->CR1 &= ~USART_CR1_RXNEIE;
else
USART->CR1 |= USART_CR1_RXNEIE;
return true;
}
static bool serialEnqueueRtCommand (char c)
{
return enqueue_realtime_command(c);
}
static enqueue_realtime_command_ptr serialSetRtHandler (enqueue_realtime_command_ptr handler)
{
enqueue_realtime_command_ptr prev = enqueue_realtime_command;
if(handler)
enqueue_realtime_command = handler;
return prev;
}
const io_stream_t *serialInit (uint32_t baud_rate)
{
static const io_stream_t stream = {
.type = StreamType_Serial,
.state.connected = On,
.read = serialGetC,
.write = serialWriteS,
.write_n = serialWrite,
.write_char = serialPutC,
.enqueue_rt_command = serialEnqueueRtCommand,
.get_rx_buffer_free = serialRxFree,
.get_rx_buffer_count = serialRxCount,
.get_tx_buffer_count = serialTxCount,
.reset_write_buffer = serialTxFlush,
.reset_read_buffer = serialRxFlush,
.cancel_read_buffer = serialRxCancel,
.suspend_read = serialSuspendInput,
.disable_rx = serialDisable,
.set_baud_rate = serialSetBaudRate,
.set_enqueue_rt_handler = serialSetRtHandler
};
if(serial[0].flags.claimed)
return NULL;
serial[0].flags.claimed = On;
#if SERIAL_MOD == 1
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStructure = {
.Mode = GPIO_MODE_AF_PP,
.Pull = GPIO_NOPULL,
.Speed = GPIO_SPEED_FREQ_VERY_HIGH,
.Pin = GPIO_PIN_9|GPIO_PIN_10,
.Alternate = GPIO_AF7_USART1
};
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
static const periph_pin_t tx = {
.function = Output_TX,
.group = PinGroup_UART1,
.port = GPIOA,
.pin = 9,
.mode = { .mask = PINMODE_OUTPUT },
.description = "UART1"
};
static const periph_pin_t rx = {
.function = Input_RX,
.group = PinGroup_UART1,
.port = GPIOA,
.pin = 10,
.mode = { .mask = PINMODE_NONE },
.description = "UART1"
};
#elif SERIAL_MOD == 2
__HAL_RCC_USART2_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStructure = {
.Mode = GPIO_MODE_AF_PP,
.Pull = GPIO_NOPULL,
.Speed = GPIO_SPEED_FREQ_VERY_HIGH,
.Pin = GPIO_PIN_2|GPIO_PIN_3,
.Alternate = GPIO_AF7_USART1
};
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
static const periph_pin_t tx = {
.function = Output_TX,
.group = PinGroup_UART1,
.port = GPIOA,
.pin = 2,
.mode = { .mask = PINMODE_OUTPUT },
.description = "UART1"
};
static const periph_pin_t rx = {
.function = Input_RX,
.group = PinGroup_UART1,
.port = GPIOA,
.pin = 3,
.mode = { .mask = PINMODE_NONE },
.description = "UART1"
};
#elif SERIAL_MOD == 3
__HAL_RCC_USART3_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStructure = {
.Mode = GPIO_MODE_AF_PP,
.Pull = GPIO_NOPULL,
.Speed = GPIO_SPEED_FREQ_VERY_HIGH,
.Pin = GPIO_PIN_8|GPIO_PIN_9,
.Alternate = GPIO_AF7_USART3
};
HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
static const periph_pin_t tx = {
.function = Output_TX,
.group = PinGroup_UART1,
.port = GPIOD,
.pin = 8,
.mode = { .mask = PINMODE_OUTPUT },
.description = "UART1"
};
static const periph_pin_t rx = {
.function = Input_RX,
.group = PinGroup_UART1,
.port = GPIOD,
.pin = 9,
.mode = { .mask = PINMODE_NONE },
.description = "UART1"
};
#else
#error Code has to be added to support serial module 1
#endif
serialSetBaudRate(baud_rate);
HAL_NVIC_SetPriority(USART_IRQ, 0, 0);
HAL_NVIC_EnableIRQ(USART_IRQ);
hal.periph_port.register_pin(&rx);
hal.periph_port.register_pin(&tx);
return &stream;
}
void USART_IRQHandler (void)
{
if(USART->SR & USART_SR_RXNE) {
uint32_t data = USART->DR;
if(!enqueue_realtime_command((char)data)) { // Check and strip realtime commands...
uint16_t next_head = BUFNEXT(rxbuf.head, rxbuf); // Get and increment buffer pointer
if(next_head == rxbuf.tail) // If buffer full
rxbuf.overflow = 1; // flag overflow
else {
rxbuf.data[rxbuf.head] = (char)data; // if not add data to buffer
rxbuf.head = next_head; // and update pointer
}
}
}
if((USART->SR & USART_SR_TXE) && (USART->CR1 & USART_CR1_TXEIE)) {
uint_fast16_t tail = txbuf.tail; // Get buffer pointer
USART->DR = txbuf.data[tail]; // Send next character
txbuf.tail = tail = BUFNEXT(tail, txbuf); // and increment pointer
if(tail == txbuf.head) // If buffer empty then
USART->CR1 &= ~USART_CR1_TXEIE; // disable UART TX interrupt
}
}
#ifdef SERIAL2_MOD
//
// Returns number of free characters in serial input buffer
//
static uint16_t serial2RxFree (void)
{
uint32_t tail = rxbuf2.tail, head = rxbuf2.head;
return (RX_BUFFER_SIZE - 1) - BUFCOUNT(head, tail, RX_BUFFER_SIZE);
}
//
// Returns number of characters in serial input buffer
//
static uint16_t serial2RxCount (void)
{
uint32_t tail = rxbuf2.tail, head = rxbuf2.head;
return BUFCOUNT(head, tail, RX_BUFFER_SIZE);
}
//
// Flushes the serial input buffer
//
static void serial2RxFlush (void)
{
rxbuf2.tail = rxbuf2.head;
}
//
// Flushes and adds a CAN character to the serial input buffer
//
static void serial2RxCancel (void)
{
rxbuf2.data[rxbuf2.head] = ASCII_CAN;
rxbuf2.tail = rxbuf2.head;
rxbuf2.head = BUFNEXT(rxbuf2.head, rxbuf2);
}
//
// Writes a character to the serial output stream
//
static bool serial2PutC (const char c)
{
uint32_t next_head = BUFNEXT(txbuf2.head, txbuf2); // Set and update head pointer
while(txbuf2.tail == next_head) { // While TX buffer full
if(!hal.stream_blocking_callback()) // check if blocking for space,
return false; // exit if not (leaves TX buffer in an inconsistent state)
UART2->CR1 |= USART_CR1_TXEIE; // Enable TX interrupts???
}
txbuf2.data[txbuf2.head] = c; // Add data to buffer
txbuf2.head = next_head; // and update head pointer
UART2->CR1 |= USART_CR1_TXEIE; // Enable TX interrupts
return true;
}
//
// Writes a null terminated string to the serial output stream, blocks if buffer full
//
static void serial2WriteS (const char *s)
{
char c, *ptr = (char *)s;
while((c = *ptr++) != '\0')
serial2PutC(c);
}
// Writes a number of characters from a buffer to the serial output stream, blocks if buffer full
//
static void serial2Write (const char *s, uint16_t length)
{
char *ptr = (char *)s;
while(length--)
serial2PutC(*ptr++);
}
//
// Flushes the serial output buffer
//
static void serial2TxFlush (void)
{
UART2->CR1 &= ~USART_CR1_TXEIE; // Disable TX interrupts
txbuf2.tail = txbuf2.head;
}
//
// Returns number of characters pending transmission
//
static uint16_t serial2TxCount (void)
{
uint32_t tail = txbuf2.tail, head = txbuf2.head;
return BUFCOUNT(head, tail, TX_BUFFER_SIZE) + (UART2->SR & USART_SR_TC ? 0 : 1);
}
//
// serialGetC - returns -1 if no data available
//
static int16_t serial2GetC (void)
{
uint_fast16_t tail = rxbuf2.tail; // Get buffer pointer
if(tail == rxbuf2.head)
return -1; // no data available
char data = rxbuf2.data[tail]; // Get next character
rxbuf2.tail = BUFNEXT(tail, rxbuf2); // and update pointer
return (int16_t)data;
}
static bool serial2SuspendInput (bool suspend)
{
return stream_rx_suspend(&rxbuf2, suspend);
}
static bool serial2SetBaudRate (uint32_t baud_rate)
{
UART2->CR1 = USART_CR1_RE|USART_CR1_TE;
UART2->BRR = UART_BRR_SAMPLING16(UART2_CLK, baud_rate);
UART2->CR1 |= (USART_CR1_UE|USART_CR1_RXNEIE);
return true;
}
static bool serial2Disable (bool disable)
{
if(disable)
UART2->CR1 &= ~USART_CR1_RXNEIE;
else
UART2->CR1 |= USART_CR1_RXNEIE;
return true;
}
static bool serial2EnqueueRtCommand (char c)
{
return enqueue_realtime_command2(c);
}
static enqueue_realtime_command_ptr serial2SetRtHandler (enqueue_realtime_command_ptr handler)
{
enqueue_realtime_command_ptr prev = enqueue_realtime_command2;
if(handler)
enqueue_realtime_command2 = handler;
return prev;
}
const io_stream_t *serial2Init (uint32_t baud_rate)
{
static const io_stream_t stream = {
.type = StreamType_Serial,
.instance = 1,
.state.connected = On,
.read = serial2GetC,
.write = serial2WriteS,
.write_n = serial2Write,
.write_char = serial2PutC,
.enqueue_rt_command = serial2EnqueueRtCommand,
.get_rx_buffer_free = serial2RxFree,
.get_rx_buffer_count = serial2RxCount,
.get_tx_buffer_count = serial2TxCount,
.reset_write_buffer = serial2TxFlush,
.reset_read_buffer = serial2RxFlush,
.cancel_read_buffer = serial2RxCancel,
.suspend_read = serial2SuspendInput,
.disable_rx = serial2Disable,
.set_baud_rate = serial2SetBaudRate,
.set_enqueue_rt_handler = serial2SetRtHandler
};
if(serial[1].flags.claimed)
return NULL;
serial[1].flags.claimed = On;
#if SERIAL2_MOD == 1
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStructure = {
.Mode = GPIO_MODE_AF_PP,
.Pull = GPIO_NOPULL,
.Speed = GPIO_SPEED_FREQ_VERY_HIGH,
.Pin = GPIO_PIN_9|GPIO_PIN_10,
.Alternate = GPIO_AF7_USART1
};
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
static const periph_pin_t tx = {
.function = Output_TX,
.group = PinGroup_UART2,
.port = GPIOA,
.pin = 9,
.mode = { .mask = PINMODE_OUTPUT },
.description = "UART2"
};
static const periph_pin_t rx = {
.function = Input_RX,
.group = PinGroup_UART2,
.port = GPIOA,
.pin = 10,
.mode = { .mask = PINMODE_NONE },
.description = "UART2"
};
#elif SERIAL2_MOD == 2
__HAL_RCC_USART2_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStructure = {
.Mode = GPIO_MODE_AF_PP,
.Pull = GPIO_NOPULL,
.Speed = GPIO_SPEED_FREQ_VERY_HIGH,
.Pin = GPIO_PIN_2|GPIO_PIN_3,
.Alternate = GPIO_AF7_USART1
};
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
static const periph_pin_t tx = {
.function = Output_TX,
.group = PinGroup_UART2,
.port = GPIOA,
.pin = 2,
.mode = { .mask = PINMODE_OUTPUT },
.description = "UART2"
};
static const periph_pin_t rx = {
.function = Input_RX,
.group = PinGroup_UART2,
.port = GPIOA,
.pin = 3,
.mode = { .mask = PINMODE_NONE },
.description = "UART2"
};
#elif SERIAL2_MOD == 3
__HAL_RCC_USART3_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStructure = {
.Mode = GPIO_MODE_AF_PP,
.Pull = GPIO_NOPULL,
.Speed = GPIO_SPEED_FREQ_VERY_HIGH,
.Pin = GPIO_PIN_8|GPIO_PIN_9,
.Alternate = GPIO_AF7_USART3
};
HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
static const periph_pin_t tx = {
.function = Output_TX,
.group = PinGroup_UART2,
.port = GPIOD,
.pin = 8,
.mode = { .mask = PINMODE_OUTPUT },
.description = "UART2"
};
static const periph_pin_t rx = {
.function = Input_RX,
.group = PinGroup_UART2,
.port = GPIOD,
.pin = 9,
.mode = { .mask = PINMODE_NONE },
.description = "UART2"
};
#else
#error Code has to be added to support serial module 2
#endif
serial2SetBaudRate(baud_rate);
HAL_NVIC_SetPriority(UART2_IRQ, 0, 0);
HAL_NVIC_EnableIRQ(UART2_IRQ);
hal.periph_port.register_pin(&rx);
hal.periph_port.register_pin(&tx);
return &stream;
}
void UART2_IRQHandler (void)
{
if(UART2->SR & USART_SR_RXNE) {
uint32_t data = UART2->DR;
if(!enqueue_realtime_command2((char)data)) { // Check and strip realtime commands...
uint16_t next_head = BUFNEXT(rxbuf2.head, rxbuf2); // Get and increment buffer pointer
if(next_head == rxbuf2.tail) // If buffer full
rxbuf2.overflow = 1; // flag overflow
else {
rxbuf2.data[rxbuf2.head] = (char)data; // if not add data to buffer
rxbuf2.head = next_head; // and update pointer
}
}
}
if((UART2->SR & USART_SR_TXE) && (UART2->CR1 & USART_CR1_TXEIE)) {
uint_fast16_t tail = txbuf2.tail; // Get buffer pointer
UART2->DR = txbuf2.data[tail]; // Send next character
txbuf2.tail = tail = BUFNEXT(tail, txbuf2); // and increment pointer
if(tail == txbuf2.head) // If buffer empty then
UART2->CR1 &= ~USART_CR1_TXEIE; // disable UART TX interrupt
}
}
#endif