-
Notifications
You must be signed in to change notification settings - Fork 1
/
usart.c
6076 lines (5010 loc) · 142 KB
/
usart.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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//**************************************************************
// ****** FUNCTIONS FOR AVR USART COMMUNICATION *******
//**************************************************************
//Compiler : AVR-GCC (c99/gnu99)
//Author : jnk0le@hotmail.com
// https://github.com/jnk0le
//License : MIT
//**************************************************************
#include <avr/io.h>
#include <util/delay.h>
#include <util/atomic.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <stdio.h>
#include "usart.h"
#ifndef NO_TX0_INTERRUPT
volatile uint8_t tx0_Tail, tx0_Head;
char tx0_buffer[TX0_BUFFER_SIZE];
#endif
#ifndef NO_RX0_INTERRUPT
volatile uint8_t rx0_Tail, rx0_Head;
char rx0_buffer[RX0_BUFFER_SIZE];
#endif
#ifndef NO_TX1_INTERRUPT
volatile uint8_t tx1_Tail, tx1_Head;
char tx1_buffer[TX1_BUFFER_SIZE];
#endif
#ifndef NO_RX1_INTERRUPT
volatile uint8_t rx1_Tail, rx1_Head;
char rx1_buffer[RX1_BUFFER_SIZE];
#endif
#ifndef NO_TX2_INTERRUPT
volatile uint8_t tx2_Tail, tx2_Head;
char tx2_buffer[TX2_BUFFER_SIZE];
#endif
#ifndef NO_RX2_INTERRUPT
volatile uint8_t rx2_Tail, rx2_Head;
char rx2_buffer[RX2_BUFFER_SIZE];
#endif
#ifndef NO_TX3_INTERRUPT
volatile uint8_t tx3_Tail, tx3_Head;
char tx3_buffer[TX3_BUFFER_SIZE];
#endif
#ifndef NO_RX3_INTERRUPT
volatile uint8_t rx3_Tail, rx3_Head;
char rx3_buffer[RX3_BUFFER_SIZE];
#endif
#ifdef USART_NO_LOCAL_BUFFERS
extern char u_tmp_buff[];
#endif
#ifdef USE_USART0
/*
void uart_init(uint16_t ubrr_value)
{
#ifdef USART0_RS485_MODE
RS485_CONTROL0_DDR |= (1<<RS485_CONTROL0_IONUM); // default pin state is low
#endif
UBRR0L_REGISTER = (uint8_t) ubrr_value;
UBRR0H_REGISTER = (ubrr_value>>8);
#ifdef USART0_U2X_SPEED
#ifdef USART0_MPCM_MODE
UCSR0A_REGISTER = (1<<U2X0_BIT)|(1<<MPCM0_BIT);
#else
UCSR0A_REGISTER = (1<<U2X0_BIT); // enable double speed
#endif
#elif defined(USART0_MPCM_MODE)
UCSR0A_REGISTER |= (1<<MPCM0_BIT);
#endif
UCSR0B_REGISTER = USART0_CONFIG_B;
// 8n1 is set by default, setting UCSRC is not needed
#ifdef USART0_USE_SOFT_RTS
RTS0_DDR |= (1<<RTS0_IONUM);
#endif
}
*/
//******************************************************************
//Function : To reinitialize USART interface (runtime speed changing).
//Arguments : Calculated UBRR value to initialize equal speed.
//Return : none
//Note : Use BAUD_CALC(speed) macro to calculate UBRR value.
// : All data inside UDR shift register will be lost.
// : U2X bit is cleared if USARTn_U2X_SPEED is not defined.
//******************************************************************
void uart0_reinit(uint16_t ubrr_value)
{
#ifdef USART0_USE_SOFT_RTS
RTS0_PORT |= (1<<RTS0_IONUM);
#endif
#ifdef USART0_RS485_MODE
RS485_CONTROL0_PORT &= ~(1<<RS485_CONTROL0_IONUM); //set low
RS485_CONTROL0_DDR |= (1<<RS485_CONTROL0_IONUM);
#endif
UCSR0B_REGISTER = 0; //flush all hardware buffers
//(writing TXENn to zero) will not become effective until ongoing and pending transmissions are completed
UBRR0L_REGISTER = (uint8_t) ubrr_value;
UBRR0H_REGISTER = (ubrr_value>>8);
#ifdef USART0_U2X_SPEED
#ifdef USART0_MPCM_MODE
UCSR0A_REGISTER = (1<<U2X0_BIT)|(1<<MPCM0_BIT);
#else
UCSR0A_REGISTER = (1<<U2X0_BIT); // enable double speed
#endif
#elif defined(USART0_MPCM_MODE)
UCSR0A_REGISTER = (1<<MPCM0_BIT);
#endif
UCSR0B_REGISTER = USART0_CONFIG_B;
// 8n1 is set by default, setting UCSRC is not needed
#ifdef USART0_USE_SOFT_RTS
RTS0_DDR |= (1<<RTS0_IONUM);
RTS0_PORT &= ~(1<<RTS0_IONUM);
#endif
}
#endif // USE_USART0
#ifdef USE_USART1
void uart1_reinit(uint16_t ubrr_value)
{
#ifdef USART1_USE_SOFT_RTS
RTS1_PORT |= (1<<RTS1_IONUM);
#endif
#ifdef USART1_RS485_MODE
RS485_CONTROL1_PORT &= ~(1<<RS485_CONTROL1_IONUM); //set low
RS485_CONTROL1_DDR |= (1<<RS485_CONTROL1_IONUM);
#endif
UCSR1B_REGISTER = 0; //flush all hardware buffers
//(writing TXENn to zero) will not become effective until ongoing and pending transmissions are completed
UBRR1L_REGISTER = (uint8_t) ubrr_value;
UBRR1H_REGISTER = (ubrr_value>>8);
#ifdef USART1_U2X_SPEED
#ifdef USART1_MPCM_MODE
UCSR1A_REGISTER = (1<<U2X1_BIT)|(1<<MPCM1_BIT);
#else
UCSR1A_REGISTER = (1<<U2X1_BIT); // enable double speed
#endif
#elif defined(USART1_MPCM_MODE)
UCSR1A_REGISTER = (1<<MPCM1_BIT);
#endif
UCSR1B_REGISTER = USART1_CONFIG_B;
// 8n1 is set by default, setting UCSRC is not needed
#ifdef USART1_USE_SOFT_RTS
RTS1_DDR |= (1<<RTS1_IONUM);
RTS1_PORT &= ~(1<<RTS1_IONUM);
#endif
}
#endif // USE_USART1
#ifdef USE_USART2
void uart2_reinit(uint16_t ubrr_value)
{
#ifdef USART2_USE_SOFT_RTS
RTS2_PORT |= (1<<RTS2_IONUM);
#endif
#ifdef USART2_RS485_MODE
RS485_CONTROL2_PORT &= ~(1<<RS485_CONTROL2_IONUM); //set low
RS485_CONTROL2_DDR |= (1<<RS485_CONTROL2_IONUM);
#endif
UCSR2B_REGISTER = 0; //flush all hardware buffers
//(writing TXENn to zero) will not become effective until ongoing and pending transmissions are completed
UBRR2L_REGISTER = (uint8_t) ubrr_value;
UBRR2H_REGISTER = (ubrr_value>>8);
#ifdef USART2_U2X_SPEED
#ifdef USART2_MPCM_MODE
UCSR2A_REGISTER = (1<<U2X2_BIT)|(1<<MPCM2_BIT);
#else
UCSR2A_REGISTER = (1<<U2X2_BIT); // enable double speed
#endif
#elif defined(USART2_MPCM_MODE)
UCSR2A_REGISTER = (1<<MPCM2_BIT);
#endif
UCSR2B_REGISTER = USART2_CONFIG_B;
// 8n1 is set by default, setting UCSRC is not needed
#ifdef USART2_USE_SOFT_RTS
RTS2_DDR |= (1<<RTS2_IONUM);
RTS2_PORT &= ~(1<<RTS2_IONUM);
#endif
}
#endif // USE_USART2
#ifdef USE_USART3
void uart3_reinit(uint16_t ubrr_value)
{
#ifdef USART3_USE_SOFT_RTS
RTS3_PORT |= (1<<RTS3_IONUM);
#endif
#ifdef USART3_RS485_MODE
RS485_CONTROL3_PORT &= ~(1<<RS485_CONTROL3_IONUM); //set low
RS485_CONTROL3_DDR |= (1<<RS485_CONTROL3_IONUM);
#endif
UCSR3B_REGISTER = 0; //flush all hardware buffers
UBRR3L_REGISTER = (uint8_t) ubrr_value;
UBRR3H_REGISTER = (ubrr_value>>8);
#ifdef USART3_U2X_SPEED
#ifdef USART3_MPCM_MODE
UCSR3A_REGISTER = (1<<U2X3_BIT)|(1<<MPCM3_BIT);
#else
UCSR3A_REGISTER = (1<<U2X3_BIT); // enable double speed
#endif
#elif defined(USART3_MPCM_MODE)
UCSR3A_REGISTER = (1<<MPCM3_BIT);
#endif
UCSR3B_REGISTER = USART3_CONFIG_B;
// 8n1 is set by default, setting UCSRC is not needed
#ifdef USART3_USE_SOFT_RTS
RTS3_DDR |= (1<<RTS3_IONUM);
RTS3_PORT &= ~(1<<RTS3_IONUM);
#endif
}
#endif // USE_USART3
#ifndef NO_USART_TX
#ifndef NO_TX0_INTERRUPT
//******************************************************************
//Function : Send single character/byte.
//Arguments : Character/byte to send.
//Return : none
//******************************************************************
#ifdef USART_NO_ABI_BREAKING_PREMATURES
void uart0_putc(char data)
{
#ifdef PUTC0_CONVERT_LF_TO_CRLF
if (data == '\n')
uart0_putc('\r');
#endif
#ifdef USART0_PUTC_FAST_INSERTIONS
register uint8_t tmp_tx_Head = tx0_Head;
register uint8_t tmp_tx_Tail = tx0_Tail;
#ifdef USART0_USE_SOFT_CTS
if(!(CTS0_PIN & (1<<CTS0_IONUM)))
#endif
if(tmp_tx_Tail == tmp_tx_Head && (UCSR0A_REGISTER & UDRE0_BIT))
{
UDR0_REGISTER = data;
return;
}
tmp_tx_Head = (tmp_tx_Head + 1) & TX0_BUFFER_MASK;
while(tmp_tx_Tail == tmp_tx_Head) // wait for free space in buffer
{
tmp_tx_Tail = tx0_Tail; // for faster pass through, results in a little bigger code
}
#else
register uint8_t tmp_tx_Head = (tx0_Head + 1) & TX0_BUFFER_MASK; // calculate new position of TX head in buffer
while(tx0_Tail == tmp_tx_Head); // wait for free space in buffer
#endif
tx0_buffer[tmp_tx_Head] = data;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
tx0_Head = tmp_tx_Head;
#ifdef USART0_RS485_MODE
RS485_CONTROL0_PORT |= (1<<RS485_CONTROL0_IONUM); //set high
#endif
#ifdef USART0_USE_SOFT_CTS
if(!(CTS0_PIN & (1<<CTS0_IONUM)))
#endif
{
UCSR0B_REGISTER |= (1<<UDRIE0_BIT); // enable UDRE interrupt
}
}
}
#else // !USART_NO_ABI_BREAKING_PREMATURES
void uart0_putc(char data)
{
register uint8_t tmp_tx_Head asm("r25");
#ifdef PUTC0_CONVERT_LF_TO_CRLF
asm volatile("\n\t"
"cpi %[dat], '\n' \n\t"
"brne skip_recursive_%=\n\t"
"push %[dat] \n\t"
"ldi %[dat], '\r' \n\t"
"rcall uart0_putc \n\t"
"pop %[dat] \n\t"
"skip_recursive_%=:"
: // outputs
[dat] "+r" (data) // will be used later, do not let the compiler to do anything weird
: // inputs
: // clobbers
);
#endif
#ifdef USART0_PUTC_FAST_INSERTIONS
asm volatile("\n\t"
"lds %[head], (tx0_Head) \n\t"
#ifdef USART0_USE_SOFT_CTS
"sbic %M[cts_port], %M[cts_pin] \n\t"
"rjmp normal_insert_%= \n\t"
#endif
"lds r27, (tx0_Tail) \n\t"
"cpse r27, %[head] \n\t"
"rjmp normal_insert_%= \n\t"
#ifdef USART0_IN_IO_ADDRESS_SPACE
"sbis %M[UCSRA_reg_IO], %M[udre_bit] \n\t"
#elif defined(USART0_IN_UPPER_IO_ADDRESS_SPACE)
"in r26, %M[UCSRA_reg_IO] \n\t"
"sbrs r26, %M[udre_bit] \n\t"
#else
"lds r26, %M[UCSRA_reg] \n\t"
"sbrs r26, %M[udre_bit] \n\t"
#endif
"rjmp normal_insert_%= \n\t"
#ifdef USART0_IN_IO_ADDRESS_SPACE
"out %M[UDR_reg], %[dat] \n\t"
#else
"sts %M[UDR_reg], %[dat] \n\t"
#endif
"ret \n\t"
"normal_insert_%=:"
"inc %[head] \n\t"
#if (TX0_BUFFER_MASK != 0xff)
"andi %[head], %M[mask] \n\t"
#endif
"waitforspace_%=:"
"lds r27, (tx0_Tail) \n\t"
"cp r27, %[head] \n\t"
"breq waitforspace_%= \n\t"
: // outputs
[head] "=r" (tmp_tx_Head),
[dat] "+r" (data) // will be used later, do not let the compiler to do anything weird
: // inputs
#ifdef USART0_USE_SOFT_CTS
[cts_port] "M" (_SFR_IO_ADDR(CTS0_PORT)),
[cts_pin] "M" (CTS0_IONUM),
#endif
[mask] "M" (TX0_BUFFER_MASK),
[UCSRA_reg] "n" (_SFR_MEM_ADDR(UCSR0A_REGISTER)),
[UCSRA_reg_IO] "M" (_SFR_IO_ADDR(UCSR0A_REGISTER)),
[UDR_reg] "n" (_SFR_MEM_ADDR(UDR0_REGISTER)),
[UDR_reg_IO] "M" (_SFR_IO_ADDR(UDR0_REGISTER)),
[udre_bit] "M" (UDRE0_BIT)
: // clobbers
"r26","r27"
);
#else
asm volatile("\n\t"
"lds %[head], (tx0_Head) \n\t"
"inc %[head] \n\t"
#if (TX0_BUFFER_MASK != 0xff)
"andi %[head], %M[mask] \n\t"
#endif
"waitforspace_%=:"
"lds r27, (tx0_Tail) \n\t"
"cp r27, %[head] \n\t"
"breq waitforspace_%= \n\t"
: // outputs
[head] "=r" (tmp_tx_Head)
: // inputs
[mask] "M" (TX0_BUFFER_MASK)
: // clobbers
"r27"
);
#endif
asm volatile("\n\t"
"mov r26, %[index] \n\t"
#if !defined(__AVR_ATtiny2313__)&&!defined(__AVR_ATtiny2313A__) // on ATtiny2313 upper byte in pointer pair is ignored
"ldi r27, 0x00 \n\t"
#endif
"subi r26, lo8(-(tx0_buffer)) \n\t"
#ifndef USART_USE_TINY_MEMORY_MODEL
"sbci r27, hi8(-(tx0_buffer)) \n\t"
#endif
"st X, %[dat] \n\t"
: // outputs
[index] "+r" (tmp_tx_Head), // will be used later, do not let the compiler to do anything weird
[dat] "+r" (data) // not modified, so reduce register moves if inlined
: // inputs
: // clobbers
"r26","r27"
);
cli();
{
tx0_Head = tmp_tx_Head;
#ifdef USART0_RS485_MODE
RS485_CONTROL0_PORT |= (1<<RS485_CONTROL0_IONUM); // start transmitting
#endif
#ifdef USART0_USE_SOFT_CTS
if(!(CTS0_PIN & (1<<CTS0_IONUM)))
#endif
{
#ifdef USART0_IN_IO_ADDRESS_SPACE
UCSR0B_REGISTER |= (1<<UDRIE0_BIT); // enable UDRE interrupt
#else
asm volatile("\n\t"
#ifdef USART0_IN_UPPER_IO_ADDRESS_SPACE
"in r25, %M[control_reg_IO] \n\t"
"ori r25, (1<<%M[udrie_bit]) \n\t"
"out %M[control_reg_IO], r25\n\t"
#else
"lds r25, %M[control_reg] \n\t"
"ori r25, (1<<%M[udrie_bit]) \n\t"
"sts %M[control_reg], r25 \n\t"
#endif
: // outputs
: // inputs
[control_reg] "n" (_SFR_MEM_ADDR(UCSR0B_REGISTER)),
[udrie_bit] "M" (UDRIE0_BIT)
: // clobbers
"r25"
);
#endif
}
}
reti();
asm volatile("\n\t"::"r" (data):); // data was passed in r24 and will be returned in the same register, make sure it is not affected by the compiler
}
char uart0_putc_(char data) __attribute__ ((alias ("uart0_putc"))); // alias for uart_putc that returns passed argument unaffected by omitting any existent rule
#endif // USART_NO_ABI_BREAKING_PREMATURES
//******************************************************************
//Function : Send single character/byte.
//Arguments : Character/byte to send.
//Return : Status value: 0 = BUFFER_FULL, 1 = COMPLETED.
//Note : If character cannot be sent due to full transmit buffer, function will abort transmitting character
//******************************************************************
#ifdef USART_NO_ABI_BREAKING_PREMATURES
uint8_t uart0_putc_noblock(char data)
{
#ifdef USART0_PUTC_FAST_INSERTIONS
register uint8_t tmp_tx_Head = tx0_Head;
register uint8_t tmp_tx_Tail = tx0_Tail;
#ifdef USART0_USE_SOFT_CTS
if(!(CTS0_PIN & (1<<CTS0_IONUM)))
#endif
if(tmp_tx_Tail == tmp_tx_Head && (UCSR0A_REGISTER & UDRE0_BIT))
{
UDR0_REGISTER = data;
return COMPLETED;
}
tmp_tx_Head = (tmp_tx_Head + 1) & TX0_BUFFER_MASK;
if(tmp_tx_Tail == tmp_tx_Head)
return BUFFER_FULL;
#else
register uint8_t tmp_tx_Head = (tx0_Head + 1) & TX0_BUFFER_MASK; // calculate new position of TX head in buffer
if(tx0_Tail == tmp_tx_Head)
return BUFFER_FULL;
#endif
tx0_buffer[tmp_tx_Head] = data;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
tx0_Head = tmp_tx_Head;
#ifdef USART0_RS485_MODE
RS485_CONTROL0_PORT |= (1<<RS485_CONTROL0_IONUM); //set high
#endif
#ifdef USART0_USE_SOFT_CTS
if(!(CTS0_PIN & (1<<CTS0_IONUM)))
#endif
{
UCSR0B_REGISTER |= (1<<UDRIE0_BIT); // enable UDRE interrupt
}
}
return COMPLETED;
}
#else //!USART_NO_ABI_BREAKING_PREMATURES
uint8_t uart0_putc_noblock(char data)
{
#ifdef USART0_PUTC_FAST_INSERTIONS
register uint8_t tmp_tx_Head = tx0_Head;
register uint8_t tmp_tx_Tail = tx0_Tail;
#ifdef USART0_USE_SOFT_CTS
if(!(CTS0_PIN & (1<<CTS0_IONUM)))
#endif
if(tmp_tx_Tail == tmp_tx_Head && (UCSR0A_REGISTER & UDRE0_BIT))
{
UDR0_REGISTER = data;
return COMPLETED;
}
tmp_tx_Head = (tmp_tx_Head + 1) & TX0_BUFFER_MASK;
if(tmp_tx_Tail == tmp_tx_Head)
return BUFFER_FULL;
#else
register uint8_t tmp_tx_Head = (tx0_Head + 1) & TX0_BUFFER_MASK; // calculate new position of TX head in buffer
if(tx0_Tail == tmp_tx_Head)
return BUFFER_FULL;
#endif
asm volatile("\n\t"
"mov r26, %[index] \n\t"
#if !defined(__AVR_ATtiny2313__)&&!defined(__AVR_ATtiny2313A__) // on ATtiny2313 upper byte in pointer pair is ignored
"ldi r27, 0x00 \n\t"
#endif
"subi r26, lo8(-(tx0_buffer)) \n\t"
#ifndef USART_USE_TINY_MEMORY_MODEL
"sbci r27, hi8(-(tx0_buffer)) \n\t"
#endif
"st X, %[dat] \n\t"
: // outputs
[index] "+r" (tmp_tx_Head), // will be used later, do not let the compiler to do anything weird
[dat] "+r" (data) // not modified, so reduce register moves if inlined
: // inputs
: // clobbers
#if !defined(__AVR_ATtiny2313__)&&!defined(__AVR_ATtiny2313A__)
"r27",
#endif
"r26"
);
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
tx0_Head = tmp_tx_Head;
#ifdef USART0_RS485_MODE
RS485_CONTROL0_PORT |= (1<<RS485_CONTROL0_IONUM); // start transmitting
#endif
#ifdef USART0_USE_SOFT_CTS
if(!(CTS0_PIN & (1<<CTS0_IONUM)))
#endif
{
UCSR0B_REGISTER |= (1<<UDRIE0_BIT); // enable UDRE interrupt
}
}
return COMPLETED;
}
#endif
//******************************************************************
//Function : Send string array.
//Arguments : Pointer to string array terminated by NULL.
//Return : none
//******************************************************************
#ifdef USART_NO_ABI_BREAKING_PREMATURES
void uart0_putstr(char *string)
{
char c;
while ((c = *string++)) uart0_putc(c);
}
#else // !USART_NO_ABI_BREAKING_PREMATURES
void uart0_putstr(const char *string)
{
asm volatile("\n\t"
"load_loop_%=:"
"ld r24, Z+ \n\t"
"and r24, r24 \n\t" // test for NULL
"breq skip_loop_%= \n\t"
"rcall uart0_putc \n\t" // Z pointer will not be affected in uart_putc()
"rjmp load_loop_%= \n\t"
"skip_loop_%=:"
: // outputs
: // inputs
"z" (string)
: // clobbers
"r24",
"r25","r26","r27" // uart_putc()
);
}
#endif // USART_NO_ABI_BREAKING_PREMATURES
//******************************************************************
//Function : Send string not terminated by NULL or part of the string array.
//Arguments : 1. Pointer to string array.
// : 2. Number of characters/bytes to send.
//Return : none
//******************************************************************
#ifdef USART_NO_ABI_BREAKING_PREMATURES
void uart0_putstrl(char *string, uint8_t BytesToWrite)
{
while(BytesToWrite--)
uart0_putc(*string++);
}
#else // !USART_NO_ABI_BREAKING_PREMATURES
void uart0_putstrl(char *string, uint8_t BytesToWrite)
{
asm volatile("\n\t"
"add %[counter], r30 \n\t" // add ZL to a counter to compare against current pointer (8 bit length, doesn't care if overflow)
"load_loop_%=:"
"cp %[counter], r30\n\t"
"breq skip_loop_%= \n\t"
"ld r24, Z+ \n\t"
"rcall uart0_putc \n\t" // counter and Z pointer will not be affected in uart_putc()
"rjmp load_loop_%= \n\t"
"skip_loop_%=:"
: // outputs
: // inputs
[counter] "r" (BytesToWrite),
"z" (string)
: // clobbers
"r24",
"r25","r26","r27" // uart_putc()
);
}
#endif // USART_NO_ABI_BREAKING_PREMATURES
//******************************************************************
//Function : Send string from flash memory.
//Arguments : Pointer to string placed in flash memory.
//Return : none
//******************************************************************
#ifdef USART_NO_ABI_BREAKING_PREMATURES
void uart0_puts_p(const __flash char *string)
{
#ifndef USART0_NOT_ACCESIBLE_FROM_CBI // tiny 102/104
register char c;
while ( (c = *string++) ) uart0_putc(c);
#endif
}
#else // !USART_NO_ABI_BREAKING_PREMATURES
void uart0_puts_p(const __flash char *string)
{
#if !defined(__AVR_ATtiny102__)||!defined(__AVR_ATtiny104__)
asm volatile("\n\t"
"load_loop_%=:"
"lpm r24, Z+ \n\t"
"and r24, r24 \n\t" // test for NULL
"breq skip_loop_%= \n\t"
"rcall uart0_putc \n\t" // Z pointer will not be affected in uart_putc()
"rjmp load_loop_%= \n\t"
"skip_loop_%=:"
: // outputs
: // inputs
"z" (string)
: // clobbers
"r24",
"r25","r26","r27" // uart_putc()
);
#endif
}
#endif // USART_NO_ABI_BREAKING_PREMATURES
//******************************************************************
//Function : Send integer formated into ASCI string (base 10).
//Arguments : int16_t data value.
//Return : none
//******************************************************************
void uart0_putint(int16_t data)
{
#ifndef USART_NO_LOCAL_BUFFERS
char u_tmp_buff[7]; // heading, 5 digit bytes, NULL
#endif
itoa(data, u_tmp_buff, 10);
uart0_putstr(u_tmp_buff);
}
//******************************************************************
//Function : Send integer formated into ASCI string.
//Arguments : 1. uint16_t data value.
// : 2. Base value (DEC, HEX, OCT, BIN, etc.).
//Return : none
//******************************************************************
void uart0_putintr(int16_t data, uint8_t radix)
{
#ifndef USART_NO_LOCAL_BUFFERS
char u_tmp_buff[17]; // heading, 15 digit bytes, NULL
#endif
itoa(data, u_tmp_buff, radix);
uart0_putstr(u_tmp_buff);
}
//******************************************************************
//Function : Send unsigned integer formated into ASCI string (base 10).
//Arguments : uint16_t data value.
//Return : none
//******************************************************************
void uart0_putuint(uint16_t data)
{
#ifndef USART_NO_LOCAL_BUFFERS
char u_tmp_buff[7]; // heading, 5 digit bytes, NULL
#endif
utoa(data, u_tmp_buff, 10);
uart0_putstr(u_tmp_buff);
}
//******************************************************************
//Function : Send unsigned integer formated into ASCI string.
//Arguments : 1. uint16_t data value.
// : 2. Base value (DEC, HEX, OCT, BIN, etc.).
//Return : none
//******************************************************************
void uart0_putuintr(uint16_t data, uint8_t radix)
{
#ifndef USART_NO_LOCAL_BUFFERS
char u_tmp_buff[17]; // heading, 15 digit bytes, NULL
#endif
utoa(data, u_tmp_buff, radix);
uart0_putstr(u_tmp_buff);
}
//******************************************************************
//Function : Send unsigned integer formated into ASCI string (base 16)
//Arguments : uint16_t data value.
//Return : none
//******************************************************************
void uart0_puthex(uint8_t data)
{
uint8_t tmp;
tmp = (data >> 4) & 0x0f;
#ifdef USART_PUTHEX_IN_UPPERCASE
uart0_putc( (tmp <= 9 ? '0' + tmp : 'A' - 10 + tmp));
#else
uart0_putc( (tmp <= 9 ? '0' + tmp : 'a' - 10 + tmp));
#endif
tmp = data & 0x0f;
#ifdef USART_PUTHEX_IN_UPPERCASE
uart0_putc( (tmp <= 9 ? '0' + tmp : 'A' - 10 + tmp));
#else
uart0_putc( (tmp <= 9 ? '0' + tmp : 'a' - 10 + tmp));
#endif
}
//******************************************************************
//Function : Send long integer formated into ASCI string (base 10).
//Arguments : int32_t data value.
//Return : none
//******************************************************************
void uart0_putlong(int32_t data)
{
#ifndef USART_NO_LOCAL_BUFFERS
char u_tmp_buff[12]; // heading, 10 digit bytes, NULL
#endif
ltoa(data, u_tmp_buff, 10);
uart0_putstr(u_tmp_buff);
}
//******************************************************************
//Function : Send long integer formated into ASCI string.
//Arguments : 1. int32_t data value.
// : 2. Base value (DEC, HEX, OCT, BIN, etc.).
//Return : none
//******************************************************************
void uart0_putlongr(int32_t data, uint8_t radix)
{
#ifndef USART_NO_LOCAL_BUFFERS
char u_tmp_buff[17]; // heading, 15 digit bytes, NULL
#endif
ltoa(data, u_tmp_buff, radix);
uart0_putstr(u_tmp_buff);
}
//******************************************************************
//Function : Send unsigned long integer formated into ASCI string (base 10).
//Arguments : uint32_t data value.
//Return : none
//******************************************************************
void uart0_putulong(uint32_t data)
{
#ifndef USART_NO_LOCAL_BUFFERS
char u_tmp_buff[12]; // heading, 10 digit bytes, NULL
#endif
ultoa(data, u_tmp_buff, 10);
uart0_putstr(u_tmp_buff);
}
//******************************************************************
//Function : Send unsigned long integer formated into ASCI string.
//Arguments : 1. uint32_t data value.
// : 2. Base value (DEC, HEX, OCT, BIN, etc.).
//Return : none
//******************************************************************
void uart0_putulongr(uint32_t data, uint8_t radix)
{
#ifndef USART_NO_LOCAL_BUFFERS
char u_tmp_buff[17]; // heading, 15 digit bytes, NULL
#endif
ultoa(data, u_tmp_buff, radix);
uart0_putstr(u_tmp_buff);
}
//******************************************************************
//Function : Send floating point value formated into ASCI string.
//Arguments : float data value.
//Return : none
//******************************************************************
void uart0_putfloat(float data)
{
#ifndef USART_NO_LOCAL_BUFFERS
char u_tmp_buff[16];
#endif
dtostrf(data, 15, 6, u_tmp_buff);
char *p = u_tmp_buff;
while(*p == ' ') // remove all unwanted spaces
p++;
uart0_putstr(p);
}
//******************************************************************
//Function : Send floating point integer formated into ASCI string.
//Arguments : 1. Float data value.
// : 2. Number of displayed digits after the dot.
//Return : none
//******************************************************************
void uart0_fputfloat(float data, uint8_t precision)
{
#ifndef USART_NO_LOCAL_BUFFERS
char u_tmp_buff[16];
#endif
dtostrf(data, 15, precision, u_tmp_buff);
char *p = u_tmp_buff;
while(*p == ' ') // remove all unwanted spaces
p++;
uart0_putstr(p);
}
//******************************************************************
//Function : Wait until all data in TX buffer are flushed.
//Arguments : none
//Return : none
//******************************************************************
void uart0_flush(void)
{
#ifdef USART0_RS485_MODE // flush UDR buffer
while (RS485_CONTROL0_PORT & (1<<RS485_CONTROL0_IONUM));
#else
while(tx0_Tail != tx0_Head); // just flush the ring buffer
#endif
}
//******************************************************************
//Function : To check how many bytes are waiting in the transmitter buffer.
//Arguments : none
//Return : Number of bytes waiting in transmitter buffer.
//******************************************************************
//uint8_t uart0_BytesToSend(void)
// {
// return (tx0_Head - tx0_Tail) & TX0_BUFFER_MASK;
// }
//******************************************************************
//Function : Transmit address of selected slave in MPCM mode.
//Arguments : Address of selected slave.
//Return : none
//******************************************************************
#ifdef USART0_MPCM_MODE
void uart0_mpcm_transmit_addres_Frame(uint8_t dat)
{
while(tx0_Tail != tx0_Head);
UCSR0B_REGISTER |= (1<<TXB80_BIT);
uart_putc(dat);
while(tx0_Tail != tx0_Head);
UCSR0B_REGISTER &= ~(1<<TXB80_BIT); // not sure if necessary
}
#endif
#endif // NO_TX0_INTERRUPT
#ifndef NO_TX1_INTERRUPT
#ifdef USART_NO_ABI_BREAKING_PREMATURES
void uart1_putc(char data)
{
#ifdef PUTC1_CONVERT_LF_TO_CRLF
if (data == '\n')
uart1_putc('\r');
#endif
#ifdef USART1_PUTC_FAST_INSERTIONS
register uint8_t tmp_tx_Head = tx1_Head;
register uint8_t tmp_tx_Tail = tx1_Tail;
#ifdef USART1_USE_SOFT_CTS
if(!(CTS1_PIN & (1<<CTS1_IONUM)))
#endif
if(tmp_tx_Tail == tmp_tx_Head && (UCSR1A_REGISTER & UDRE1_BIT))
{
UDR1_REGISTER = data;
return;
}
tmp_tx_Head = (tmp_tx_Head + 1) & TX1_BUFFER_MASK;
while(tmp_tx_Tail == tmp_tx_Head) // wait for free space in buffer
{
tmp_tx_Tail = tx1_Tail; // for faster pass through, results in a little bigger code
}
#else
register uint8_t tmp_tx_Head = (tx1_Head + 1) & TX1_BUFFER_MASK; // calculate new position of TX head in buffer
while(tx1_Tail == tmp_tx_Head); // wait for free space in buffer
#endif
tx1_buffer[tmp_tx_Head] = data;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
tx1_Head = tmp_tx_Head;
#ifdef USART1_RS485_MODE
RS485_CONTROL1_PORT |= (1<<RS485_CONTROL1_IONUM); //set high
#endif
#ifdef USART1_USE_SOFT_CTS
if(!(CTS1_PIN & (1<<CTS1_IONUM)))
#endif
{
UCSR1B_REGISTER |= (1<<UDRIE1_BIT); // enable UDRE interrupt
}
}
}
#else // !USART_NO_ABI_BREAKING_PREMATURES
void uart1_putc(char data)
{
register uint8_t tmp_tx_Head asm("r25");