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
|
/************************************************************************/
/* */
/* File: avr/usart.c */
/* Description: serial line device. */
/* Version: 1.0 */
/* Author: Otto Mattik */
/* */
/* (C)Copyright Otto Mattik 2014-2021. */
/* */
/* This file is a part of 'armen' (a tiny operating system). */
/* 'armen' is distributed under the CeCILL-V2.1 licence. For more */
/* details about this licence, please visit the website cecill.info */
/* */
/************************************************************************/
#ifndef AVR_UART_C
#error "do not use this code directly, it must be included via avr_uart.c"
#endif
/*
#define OUTPUT_INTERRUPT
*/
#include <avr/io.h>
#include <avr/interrupt.h>
/*
* the uart is configured with 8 data bits, no parity and 1 stop bit.
*/
#if defined(__AVR_ATtiny841__)
#define UART0_INPUT_PIN PA2 /* receive */
#define UART0_OUTPUT_PIN PA1 /* transmit */
#define UART0_CONFIG_PINS() (\
DDRA = (DDRA|(1<<UART0_OUTPUT_PIN))\
& ~(1<<UART0_INPUT_PIN)\
)
#define UART1_INPUT_PIN PA4 /* receive */
#define UART1_OUTPUT_PIN PA5 /* transmit */
#define UART1_CONFIG_PINS() (\
DDRA = (DDRA|(1<<UART1_OUTPUT_PIN))\
& ~(1<<UART1_INPUT_PIN)\
)
#elif defined(__AVR_ATtiny1634__)
#define UART0_INPUT_PIN PA7 /* receive */
#define UART0_OUTPUT_PIN PB0 /* transmit */
#define UART0_CONFIG_PINS() (\
DDRA = (DDRA|(1<<UART0_OUTPUT_PIN))\
& ~(1<<UART0_INPUT_PIN)\
)
#define UART1_INPUT_PIN PB1 /* receive */
#define UART1_OUTPUT_PIN PB2 /* transmit */
#define UART1_CONFIG_PINS() (\
DDRB = (DDRB|(1<<UART1_OUTPUT_PIN))\
& ~(1<<UART1_INPUT_PIN)\
)
#elif defined(__AVR_ATmega328P__)
#define UART0_INPUT_PIN PD0 /* receive (uno:0) */
#define UART0_OUTPUT_PIN PD1 /* transmit (uno:1) */
#define UART0_CONFIG_PINS() (\
DDRD = (DDRD|(1<<UART0_OUTPUT_PIN))\
& ~(1<<UART0_INPUT_PIN)\
)
#elif defined(__AVR_ATmega2560__)
#define UART0_INPUT_PIN PE0
#define UART0_OUTPUT_PIN PE1
#define UART0_CONFIG_PINS() (\
DDRE = (DDRE|(1<<UART0_OUTPUT_PIN))\
& ~(1<<UART0_INPUT_PIN)\
)
#define UART1_INPUT_PIN PD2 /* receive (mega:19) */
#define UART1_OUTPUT_PIN PD3 /* transmit (mega:18) */
#define UART1_CONFIG_PINS() (\
DDRD = (DDRD|(1<<UART1_OUTPUT_PIN))\
& ~(1<<UART1_INPUT_PIN)\
)
#define UART2_INPUT_PIN PH0 /* receive (mega:17) */
#define UART2_OUTPUT_PIN PH1 /* transmit (mega:16) */
#define UART2_CONFIG_PINS() (\
DDRH = (DDRH|(1<<UART2_OUTPUT_PIN))\
& ~(1<<UART2_INPUT_PIN)\
)
#define UART3_INPUT_PIN PJ0 /* receive (mega:15) */
#define UART3_OUTPUT_PIN PJ1 /* transmit (mega:14) */
#define UART3_CONFIG_PINS() (\
DDRJ = (DDRJ|(1<<UART3_OUTPUT_PIN))\
& ~(1<<UART3_INPUT_PIN)\
)
#else
#error "no or unavailable cpu defined"
#endif
#if defined(USART_RX_vect)
#define UART0_INPUT_INT USART_RX_vect
#define UART0_OUTPUT_INT USART_UDRE_vect
#else
#define UART0_INPUT_INT USART0_RX_vect
#define UART0_OUTPUT_INT USART_UDRE_vect
#endif
#define UART0_RX_BYTE UDR0
#define UART0_RX_INT_ENABLE() (UCSR0B |= (1<<RXCIE0))
#define UART0_RX_INT_DISABLE() (UCSR0B &= ~(1<<RXCIE0))
#define UART0_TX_BYTE UDR0
#define UART0_TX_INT_ENABLE() (UCSR0B |= (1<<UDRIE0))
#define UART0_TX_INT_DISABLE() (UCSR0B &= ~(1<<UDRIE0))
#define UART0_TX_NOT_READY() ((UCSR0A & (1<<UDRE0))==0)
#define UART0_CONFIG_BAUDRATE(b) (\
UCSR0A = (1<<U2X0),\
UBRR0H = (b)>>8, UBRR0L = (b)&0xFF\
)
#define UART0_CONFIG_DEVICE() (\
UCSR0B = (1<<RXEN0)|(1<<TXEN0),\
UCSR0C = (1<<UCSZ00)|(1<<UCSZ01)\
)
#define UART0_CONFIG(b) (\
UART0_CONFIG_PINS(),\
UART0_CONFIG_BAUDRATE(b),\
UART0_CONFIG_DEVICE()\
)
#define UART1_INPUT_INT USART1_RX_vect
#define UART1_OUTPUT_INT USART1_UDRE_vect
#define UART1_RX_BYTE UDR1
#define UART1_RX_INT_ENABLE() (UCSR1B |= (1<<RXCIE1))
#define UART1_RX_INT_DISABLE() (UCSR1B &= ~(1<<RXCIE1))
#define UART1_TX_BYTE UDR1
#define UART1_TX_INT_ENABLE() (UCSR1B |= (1<<UDRIE1))
#define UART1_TX_INT_DISABLE() (UCSR1B &= ~(1<<UDRIE1))
#define UART1_TX_NOT_READY() ((UCSR1A & (1<<UDRE1))==0)
#define UART1_CONFIG_BAUDRATE(b) (\
UCSR1A = (1<<U2X1),\
UBRR1H = (b)>>8, UBRR1L = (b)&0xFF\
)
#define UART1_CONFIG_DEVICE() (\
UCSR1B = (1<<RXEN1)|(1<<TXEN1),\
UCSR1C = (1<<UCSZ10)|(1<<UCSZ11)\
)
#define UART1_CONFIG(b) (\
UART1_CONFIG_PINS(),\
UART1_CONFIG_BAUDRATE(b),\
UART1_CONFIG_DEVICE()\
)
#define UART2_INPUT_INT USART2_RX_vect
#define UART2_OUTPUT_INT USART2_UDRE_vect
#define UART2_RX_BYTE UDR2
#define UART2_RX_INT_ENABLE() (UCSR2B |= (1<<RXCIE2))
#define UART2_RX_INT_DISABLE() (UCSR2B &= ~(1<<RXCIE2))
#define UART2_TX_BYTE UDR2
#define UART2_TX_INT_ENABLE() (UCSR2B |= (1<<UDRIE2))
#define UART2_TX_INT_DISABLE() (UCSR2B &= ~(1<<UDRIE2))
#define UART2_TX_NOT_READY() ((UCSR2A & (1<<UDRE2))==0)
#define UART2_CONFIG_BAUDRATE(b) (\
UCSR2A = (1<<U2X2),\
UBRR2H = (b)>>8, UBRR2L = (b)&0xFF\
)
#define UART2_CONFIG_DEVICE() (\
UCSR2B = (1<<RXEN2)|(1<<TXEN2),\
UCSR2C = (1<<UCSZ10)|(1<<UCSZ11)\
)
#define UART2_CONFIG(b) (\
UART2_CONFIG_PINS(),\
UART2_CONFIG_BAUDRATE(b),\
UART2_CONFIG_DEVICE()\
)
#define UART3_INPUT_INT USART3_RX_vect
#define UART3_OUTPUT_INT USART3_UDRE_vect
#define UART3_RX_BYTE UDR3
#define UART3_RX_INT_ENABLE() (UCSR3B |= (1<<RXCIE3))
#define UART3_RX_INT_DISABLE() (UCSR3B &= ~(1<<RXCIE3))
#define UART3_TX_BYTE UDR3
#define UART3_TX_INT_ENABLE() (UCSR3B |= (1<<UDRIE3))
#define UART3_TX_INT_DISABLE() (UCSR3B &= ~(1<<UDRIE3))
#define UART3_TX_NOT_READY() ((UCSR3A & (1<<UDRE3))==0)
#define UART3_CONFIG_BAUDRATE(b) (\
UCSR3A = (1<<U2X3),\
UBRR3H = (b)>>8, UBRR3L = (b)&0xFF\
)
#define UART3_CONFIG_DEVICE() (\
UCSR3B = (1<<RXEN3)|(1<<TXEN3),\
UCSR3C = (1<<UCSZ10)|(1<<UCSZ11)\
)
#define UART3_CONFIG(b) (\
UART3_CONFIG_PINS(),\
UART3_CONFIG_BAUDRATE(b),\
UART3_CONFIG_DEVICE()\
)
static uint16_t uart_bauds[8] = {
#if (F_CPU == 16000000UL )
1666, 832, 416, 207, 103, 51, 34, 16
#elif (F_CPU == 12000000UL )
1249, 624, 312, 155, 77, 38, 25, 12
#elif (F_CPU == 8000000UL )
832, 416, 207, 103, 51, 34, 16, 8
#elif (F_CPU == 4000000UL )
416, 207, 103, 51, 25, 16, 8, 3
#else
#error "no parameters for the clock frequency"
#endif
};
#define UBRR_VALUE(b) (((F_CPU)+4UL*(b))/(8UL*(b))-1UL)
#define UART_OPEN 0x80
#define UART_ECHOO_CHECK 0x40
#define UART_TX_PROGRESS 0x20
#define UART_CONFIG_MASK 0x0F
static struct {
uint8_t state; /* state and configuration */
uint8_t cd_byte; /* received byte if collision detection */
uint8_t rx_head; /* fifo index */
uint8_t rx_tail; /* " " */
uint8_t rx_buffer[UART_RX_BUFFER_SIZE+1];
} volatile uarts[ARMEN_UARTS];
static void _uart_recv_byte( uint8_t device, uint8_t byte )
{
if( uarts[device].state & UART_ECHOO_CHECK )
{
uarts[device].cd_byte = byte;
uarts[device].state &= ~UART_ECHOO_CHECK;
}
else
{
uint8_t head;
head = uarts[device].rx_head + 1;
if( head == (UART_RX_BUFFER_SIZE + 1) )
head = 0;
if( head != uarts[device].rx_tail )
{
uarts[device].rx_buffer[head] = byte;
uarts[device].rx_head = head;
_wakeup( EVENT_UART0 + device, 0 );
}
}
}
/*
* occurs at end of byte reception
*/
ISR( UART0_INPUT_INT )
{
uint8_t byte = UART0_RX_BYTE;
_uart_recv_byte( 0, byte );
}
#if (ARMEN_UARTS >= 2)
ISR( UART1_INPUT_INT )
{
uint8_t byte = UART1_RX_BYTE;
_uart_recv_byte( 1, byte );
}
#endif
#if (ARMEN_UARTS >= 3)
ISR( UART2_INPUT_INT )
{
uint8_t byte = UART2_RX_BYTE;
_uart_recv_byte( 2, byte );
}
#endif
#if (ARMEN_UARTS == 4)
ISR( UART3_INPUT_INT )
{
uint8_t byte = UART3_RX_BYTE;
_uart_recv_byte( 3, byte );
}
#endif
/*
* occurs at end of byte transmission
*/
#ifdef OUTPUT_INTERRUPT
ISR( UART0_OUTPUT_INT )
{
uarts[0].state &= ~UART_TX_PROGRESS;
UART0_TX_INT_DISABLE( );
}
#if (ARMEN_UARTS >= 2)
ISR( UART1_OUTPUT_INT )
{
uarts[1].state &= ~UART_TX_PROGRESS;
UART1_TX_INT_DISABLE( );
}
#endif
#if (ARMEN_UARTS >= 3)
ISR( UART2_OUTPUT_INT )
{
uarts[2].state &= ~UART_TX_PROGRESS;
UART2_TX_INT_DISABLE( );
}
#endif
#if (ARMEN_UARTS == 4)
ISR( UART3_OUTPUT_INT )
{
uarts[3].state &= ~UART_TX_PROGRESS;
UART3_TX_INT_DISABLE( );
}
#endif
#endif
/*
* initialize device
*
* input:
* device identifier
* config uart configuration
* output:
* 0 if success, otherwise -1
*/
static int8_t _uart_open( uint8_t device, uint8_t config )
{
uint8_t bauds = config & BAUDRATE;
uint16_t ubrr = uart_bauds[bauds];
#ifdef CHECK_PARAM
if( (uarts[device].state & UART_OPEN) != 0 || bauds > B115200 )
return( -1 );
#endif
uarts[device].rx_head = uarts[device].rx_tail = 0;
uarts[device].state = (config & UART_CONFIG_MASK) | UART_OPEN;
uarts[device].rx_head = uarts[device].rx_tail = 0;
if( device == 0 )
{
UART0_CONFIG( ubrr );
UART0_RX_INT_ENABLE( );
}
#if (ARMEN_UARTS >= 2)
else if( device == 1 )
{
UART1_CONFIG( ubrr );
UART1_RX_INT_ENABLE( );
}
#endif
#if (ARMEN_UARTS >= 3)
else if( device == 2 )
{
UART2_CONFIG( ubrr );
UART2_RX_INT_ENABLE( );
}
#endif
#if (ARMEN_UARTS == 4)
else
{
UART3_CONFIG( ubrr );
UART3_RX_INT_ENABLE( );
}
#endif
}
/*
* release device
*
* input:
* device identifier
* output:
* 0 if success, otherwise -1
*/
static int8_t _uart_close( uint8_t device )
{
#ifdef CHECK_PARAM
if( (uarts[device].state & UART_OPEN) == 0 )
return( -1 );
#endif
if( device == 0 )
{
UART0_RX_INT_DISABLE( );
#ifdef OUTPUT_INTERRUPT
UART0_TX_INT_DISABLE( );
#endif
}
#if (ARMEN_UARTS >= 2)
else if( device == 1 )
{
UART1_RX_INT_DISABLE( );
#ifdef OUTPUT_INTERRUPT
UART1_TX_INT_DISABLE( );
#endif
}
#endif
#if (ARMEN_UARTS >= 3)
else if( device == 2 )
{
UART2_RX_INT_DISABLE( );
#ifdef OUTPUT_INTERRUPT
UART2_TX_INT_DISABLE( );
#endif
}
#endif
#if (ARMEN_UARTS == 4)
else
{
UART3_RX_INT_DISABLE( );
#ifdef OUTPUT_INTERRUPT
UART3_TX_INT_DISABLE( );
#endif
}
#endif
uarts[device].state = 0;
uarts[device].rx_head = uarts[device].rx_tail = 0;
return( 0 );
}
/*
* read from device
*
* input:
* device identifier
* byte address of byte to read
* output:
* 0 if success, otherwise -1
*/
static int8_t _uart_read( uint8_t device, uint8_t* byte )
{
uint8_t tail;
#ifdef CHECK_PARAM
if( (uarts[device].state & UART_OPEN) == 0 || byte == (uint8_t*)0 )
return( -1 );
#endif
tail = uarts[device].rx_tail;
if( tail == uarts[device].rx_head )
wait_event( EVENT_UART0 + device );
if( ++tail == (UART_RX_BUFFER_SIZE + 1) )
tail = 0;
*byte = uarts[device].rx_buffer[tail];
uarts[device].rx_tail = tail;
return( 0 );
}
/*
* write to device
*
* input:
* device identifier
* byte byte to write
* output:
* 0 if success, otherwise -1
*/
static int8_t _uart_write( uint8_t device, uint8_t byte )
{
if( (uarts[device].state & UART_OPEN) == 0 )
return( -1 );
#ifdef OUTPUT_INTERRUPT
uarts[device].state |= UART_TX_PROGRESS;
#endif
if( uarts[device].state & ECHOO )
uarts[device].state |= UART_ECHOO_CHECK;
if( device == 0 )
{
#ifdef OUTPUT_INTERRUPT
UART0_TX_BYTE = byte;
UART0_TX_INT_ENABLE( );
#else
while( UART0_TX_NOT_READY( ) ) ;
UART0_TX_BYTE = byte;
#endif
}
#if (ARMEN_UARTS >= 2)
else if( device == 1 )
{
#ifdef OUTPUT_INTERRUPT
UART1_TX_BYTE = byte;
UART1_TX_INT_ENABLE( );
#else
while( UART1_TX_NOT_READY( ) ) ;
UART1_TX_BYTE = byte;
#endif
}
#endif
#if (ARMEN_UARTS >= 3)
else if( device == 2 )
{
#ifdef OUTPUT_INTERRUPT
UART2_TX_BYTE = byte;
UART2_TX_INT_ENABLE( );
#else
while( UART2_TX_NOT_READY( ) ) ;
UART2_TX_BYTE = byte;
#endif
}
#endif
#if (ARMEN_UARTS == 4)
else
{
#ifdef OUTPUT_INTERRUPT
UART3_TX_BYTE = byte;
UART3_TX_INT_ENABLE( );
#else
while( UART3_TX_NOT_READY( ) ) ;
UART3_TX_BYTE = byte;
#endif
}
#endif
#ifdef OUTPUT_INTERRUPT
while( uarts[device].state & UART_TX_PROGRESS ) ;
#else
if( device == 0 )
while( UART0_TX_NOT_READY( ) ) ;
#if (ARMEN_UARTS >= 2)
else if( device == 1 )
while( UART1_TX_NOT_READY( ) ) ;
#endif
#if (ARMEN_UARTS >= 3)
else if( device == 2 )
while( UART2_TX_NOT_READY( ) ) ;
#endif
#if (ARMEN_UARTS == 4)
else
while( UART3_TX_NOT_READY( ) ) ;
#endif
#endif
if( uarts[device].state & ECHOO )
{
while( uarts[device].state & UART_ECHOO_CHECK ) ;
if( uarts[device].cd_byte != byte )
return( -1 );
}
return( 0 );
}
/*
* device special feature
*
* input:
* device identifier
* request device dependent
* args list of arguments
* output:
* 0 if success, otherwise -1
*/
#ifdef UART_IOCTL
static int8_t _uart_ioctl( uint8_t device, uint8_t request, va_list args )
{
if( (uarts[device].state & UART_OPEN) != 0 )
{
if( request == UART_GETP )
{
uint8_t* p = va_arg( args, uint8_t* );
*p = uarts[device].state & UART_CONFIG_MASK;
return( 0 )
}
}
return( -1 );
}
#endif
|