29
29
#undef Serial
30
30
#endif
31
31
32
+ static tx_buffer_index_t _tx_buffer_head[10 ];
33
+ static tx_buffer_index_t _tx_buffer_tail[10 ];
34
+ static rx_buffer_index_t _rx_buffer_tail[10 ];
35
+
36
+ static unsigned char *_rx_buffer[10 ];
37
+ static unsigned char *_tx_buffer[10 ];
38
+
39
+ static bool _sending[10 ];
40
+
32
41
33
42
uint8_t tx_buff[SERIAL_TX_BUFFER_SIZE];
34
43
int to_send_i = -1 ;
35
44
int send_i = -1 ;
36
45
37
- typedef enum {
38
- TX_STARTED,
39
- TX_STOPPED
40
- } TxStatus_t;
46
+
41
47
42
48
TxStatus_t tx_status = TX_STOPPED;
43
49
50
+ #define MAX_UARTS 10
51
+
52
+ static UART *_uarts[MAX_UARTS];
53
+
54
+
55
+ void uart_callback (uart_callback_args_t *p_args)
56
+ {
57
+ /* This callback function is not used but it is referenced into
58
+ FSP configuration so that (for the moment it is necessary to keep it) */
59
+ }
60
+
61
+ /* -------------------------------------------------------------------------- */
62
+ void UART::WrapperCallback (uart_callback_args_t *p_args) {
63
+ /* -------------------------------------------------------------------------- */
64
+
65
+ uint32_t channel = p_args->channel ;
66
+
67
+ switch (p_args->event ){
68
+ case UART_EVENT_RX_COMPLETE:
69
+ {
70
+ R_SCI_UART_Read ((uart_ctrl_t *)(SciTable[channel].uart_instance ->p_ctrl ), _rx_buffer[channel], SERIAL_RX_BUFFER_SIZE);
71
+ break ;
72
+ }
73
+ case UART_EVENT_ERR_PARITY:
74
+ case UART_EVENT_ERR_FRAMING:
75
+ case UART_EVENT_ERR_OVERFLOW:
76
+ {
77
+ break ;
78
+ }
79
+ case UART_EVENT_TX_COMPLETE:
80
+ {
81
+ if (send_i != to_send_i) {
82
+ inc (send_i, SERIAL_TX_BUFFER_SIZE);
83
+ R_SCI_UART_Write ((uart_ctrl_t *)(SciTable[channel].uart_instance ->p_ctrl ), (tx_buff + send_i), 1 );
84
+ }
85
+ else {
86
+ tx_status = TX_STOPPED;
87
+ }
88
+ break ;
89
+ }
90
+ case UART_EVENT_RX_CHAR:
91
+ case UART_EVENT_BREAK_DETECT:
92
+ case UART_EVENT_TX_DATA_EMPTY:
93
+ {
94
+ break ;
95
+ }
96
+ }
97
+
98
+ }
99
+
100
+ /* -------------------------------------------------------------------------- */
101
+ UART::UART (int ch) :
102
+ tx_st(TX_STOPPED),
103
+ _channel(ch)
104
+ /* -------------------------------------------------------------------------- */
105
+ {
106
+ _uarts[_channel] = this ;
107
+ }
108
+
109
+
44
110
45
- static tx_buffer_index_t _tx_buffer_head[10 ];
46
- static tx_buffer_index_t _tx_buffer_tail[10 ];
47
- static rx_buffer_index_t _rx_buffer_tail[10 ];
48
111
49
- static unsigned char *_rx_buffer[10 ];
50
- static unsigned char *_tx_buffer[10 ];
51
112
52
- static bool _sending[10 ];
53
113
54
114
55
115
UART::UART (
@@ -63,9 +123,7 @@ UART::UART(
63
123
{ }
64
124
65
125
66
- UART::UART (int ch) :
67
- _channel(ch)
68
- { }
126
+
69
127
70
128
/* -------------------------------------------------------------------------- */
71
129
bool UART::setUpUartIrqs (uart_cfg_t &cfg) {
@@ -140,7 +198,7 @@ void UART::begin(unsigned long baudrate, uint16_t config) {
140
198
141
199
_config = *_uart_config;
142
200
143
- _config.p_callback = irq_callback ;
201
+ _config.p_callback = UART::WrapperCallback ;
144
202
145
203
switch (config){
146
204
case SERIAL_8N1:
@@ -253,49 +311,7 @@ void UART::flush() {
253
311
254
312
255
313
256
- void irq_callback (uart_callback_args_t *p_args) {
257
- uint32_t channel = p_args->channel ;
258
-
259
- switch (p_args->event ){
260
- case UART_EVENT_RX_COMPLETE:
261
- {
262
- R_SCI_UART_Read ((uart_ctrl_t *)(SciTable[channel].uart_instance ->p_ctrl ), _rx_buffer[channel], SERIAL_RX_BUFFER_SIZE);
263
- break ;
264
- }
265
- case UART_EVENT_ERR_PARITY:
266
- case UART_EVENT_ERR_FRAMING:
267
- case UART_EVENT_ERR_OVERFLOW:
268
- {
269
- break ;
270
- }
271
- case UART_EVENT_TX_COMPLETE:
272
- {
273
- if (send_i != to_send_i) {
274
- inc (send_i, SERIAL_TX_BUFFER_SIZE);
275
- R_SCI_UART_Write ((uart_ctrl_t *)(SciTable[channel].uart_instance ->p_ctrl ), (tx_buff + send_i), 1 );
276
- }
277
- else {
278
- tx_status = TX_STOPPED;
279
- }
280
- break ;
281
- }
282
- case UART_EVENT_RX_CHAR:
283
- case UART_EVENT_BREAK_DETECT:
284
- case UART_EVENT_TX_DATA_EMPTY:
285
- {
286
- break ;
287
- }
288
- }
289
- }
290
314
291
- bool is_write_buffer_possible (int to_send, int send, int _max ) {
292
- int a = next (to_send,_max);
293
-
294
- if (a == send) {
295
- return false ;
296
- }
297
- return true ;
298
- }
299
315
300
316
301
317
@@ -420,40 +436,6 @@ void UART::enableUartIrqs() {
420
436
}
421
437
422
438
423
- void uart_callback (uart_callback_args_t *p_args)
424
- {
425
- uint32_t channel = p_args->channel ;
426
- switch (p_args->event ){
427
- case UART_EVENT_RX_COMPLETE:
428
- {
429
- R_SCI_UART_Read ((uart_ctrl_t *)(SciTable[channel].uart_instance ->p_ctrl ), _rx_buffer[channel], SERIAL_RX_BUFFER_SIZE);
430
- break ;
431
- }
432
- case UART_EVENT_ERR_PARITY:
433
- case UART_EVENT_ERR_FRAMING:
434
- case UART_EVENT_ERR_OVERFLOW:
435
- {
436
- break ;
437
- }
438
- case UART_EVENT_TX_COMPLETE:
439
- {
440
- if (_tx_buffer_head[channel] != _tx_buffer_tail[channel]) {
441
- R_SCI_UART_Write ((uart_ctrl_t *)(SciTable[channel].uart_instance ->p_ctrl ), &_tx_buffer[channel][_tx_buffer_tail[channel]], 1 );
442
- _tx_buffer_tail[channel] = (tx_buffer_index_t )((_tx_buffer_tail[channel] + 1 ) % SERIAL_TX_BUFFER_SIZE);
443
- } else {
444
- _sending[channel] = false ;
445
- }
446
- break ;
447
- }
448
- case UART_EVENT_RX_CHAR:
449
- case UART_EVENT_BREAK_DETECT:
450
- case UART_EVENT_TX_DATA_EMPTY:
451
- {
452
- break ;
453
- }
454
- }
455
- }
456
-
457
439
458
440
#if SERIAL_HOWMANY > 0
459
441
UART _UART1_ (UART1_CHANNEL);
0 commit comments