-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATtinySerialOut.cpp
More file actions
812 lines (721 loc) · 26.1 KB
/
ATtinySerialOut.cpp
File metadata and controls
812 lines (721 loc) · 26.1 KB
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
/*
* ATtinySerialOut.cpp
*
* For transmitting debug data over bit bang serial with 115200 baud for 1/8/16 MHz ATtiny clock.
* For 1 MHz you can choose also 38400 baud (120 bytes smaller code size).
* For 8/16 MHz you can choose also 230400 baud (just faster).
* 1 Start, 8 Data, 1 Stop, No Parity
*
* Using PB2 // (Pin7 on Tiny85) as default TX pin to be compatible with digispark board
* To change the output pin, modify line 38 in TinySerialOut.h or or set it as compiler symbol like "-DTX_PIN PB1".
*
* Using the Serial.print commands needs 4 bytes extra for each call.
*
*
* Copyright (C) 2015-2019 Armin Joachimsmeyer
* Email: armin.joachimsmeyer@gmail.com
*
* This file is part of TinySerialOut https://github.com/ArminJo/ATtinySerialOut.
*
* TinySerialOut 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/gpl.html>.
*
*/
#if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny87__) || defined(__AVR_ATtiny167__)
#include "ATtinySerialOut.h"
#include <avr/io.h> // for PORTB - is also included by <avr/interrupt.h>
#include <avr/pgmspace.h> // for pgm_read_byte()
#include <avr/eeprom.h> // for eeprom_read_byte()
#include <stdlib.h> // for utoa() etc.
#ifndef _NOP
#define _NOP() __asm__ volatile ("nop")
#endif
#ifndef PORTB
#define PORTB (*(volatile uint8_t *)((0x18) + 0x20))
#endif
/*
* Used for writeString() and therefore all write<type>() and print<type>
*/
bool sUseCliSeiForWrite = true;
void useCliSeiForStrings(bool aUseCliSeiForWrite) {
sUseCliSeiForWrite = aUseCliSeiForWrite;
}
/*
* Write String residing in RAM
*/
void writeString(const char * aStringPtr) {
#ifndef USE_ALWAYS_CLI_SEI_GUARD_FOR_OUTPUT
if (sUseCliSeiForWrite) {
#endif
while (*aStringPtr != 0) {
write1Start8Data1StopNoParityWithCliSei(*aStringPtr++);
}
#ifndef USE_ALWAYS_CLI_SEI_GUARD_FOR_OUTPUT
} else {
while (*aStringPtr != 0) {
write1Start8Data1StopNoParity(*aStringPtr++);
}
}
#endif
}
/*
* Write string residing in program space (FLASH)
*/
void writeString_P(const char * aStringPtr) {
uint8_t tChar = pgm_read_byte((const uint8_t * ) aStringPtr);
// Comparing with 0xFF is safety net for wrong string pointer
while (tChar != 0 && tChar != 0xFF) {
#ifdef USE_ALWAYS_CLI_SEI_GUARD_FOR_OUTPUT
write1Start8Data1StopNoParityWithCliSei(tChar);
#else
if (sUseCliSeiForWrite) {
write1Start8Data1StopNoParityWithCliSei(tChar);
} else {
write1Start8Data1StopNoParity(tChar);
}
#endif
tChar = pgm_read_byte((const uint8_t * ) ++aStringPtr);
}
}
/*
* Write string residing in program space (FLASH)
*/
void writeString(const __FlashStringHelper * aStringPtr) {
PGM_P tPGMStringPtr = reinterpret_cast<PGM_P>(aStringPtr);
uint8_t tChar = pgm_read_byte((const uint8_t * ) aStringPtr);
// Comparing with 0xFF is safety net for wrong string pointer
while (tChar != 0 && tChar != 0xFF) {
#ifdef USE_ALWAYS_CLI_SEI_GUARD_FOR_OUTPUT
write1Start8Data1StopNoParityWithCliSei(tChar);
#else
if (sUseCliSeiForWrite) {
write1Start8Data1StopNoParityWithCliSei(tChar);
} else {
write1Start8Data1StopNoParity(tChar);
}
#endif
tChar = pgm_read_byte((const uint8_t * ) ++tPGMStringPtr);
}
}
/*
* Write string residing in EEPROM space
*/
void writeString_E(const char * aStringPtr) {
uint8_t tChar = eeprom_read_byte((const uint8_t *) aStringPtr);
// Comparing with 0xFF is safety net for wrong string pointer
while (tChar != 0 && tChar != 0xFF) {
#ifdef USE_ALWAYS_CLI_SEI_GUARD_FOR_OUTPUT
write1Start8Data1StopNoParityWithCliSei(tChar);
#else
if (sUseCliSeiForWrite) {
write1Start8Data1StopNoParityWithCliSei(tChar);
} else {
write1Start8Data1StopNoParity(tChar);
}
#endif
tChar = eeprom_read_byte((const uint8_t *) ++aStringPtr);
}
}
void writeStringWithoutCliSei(const char * aStringPtr) {
while (*aStringPtr != 0) {
write1Start8Data1StopNoParity(*aStringPtr++);
}
}
void writeStringWithCliSei(const char * aStringPtr) {
while (*aStringPtr != 0) {
write1Start8Data1StopNoParityWithCliSei(*aStringPtr++);
}
}
void writeStringSkipLeadingSpaces(const char * aStringPtr) {
// skip leading spaces
while (*aStringPtr == ' ' && *aStringPtr != 0) {
aStringPtr++;
}
#ifndef USE_ALWAYS_CLI_SEI_GUARD_FOR_OUTPUT
if (sUseCliSeiForWrite) {
#endif
while (*aStringPtr != 0) {
write1Start8Data1StopNoParityWithCliSei(*aStringPtr++);
}
#ifndef USE_ALWAYS_CLI_SEI_GUARD_FOR_OUTPUT
} else {
while (*aStringPtr != 0) {
write1Start8Data1StopNoParity(*aStringPtr++);
}
}
#endif
}
void writeBinary(uint8_t aByte) {
#ifdef USE_ALWAYS_CLI_SEI_GUARD_FOR_OUTPUT
write1Start8Data1StopNoParityWithCliSei(aByte);
#else
if (sUseCliSeiForWrite) {
write1Start8Data1StopNoParityWithCliSei(aByte);
} else {
write1Start8Data1StopNoParity(aByte);
}
#endif
}
void writeChar(uint8_t aChar) {
writeBinary(aChar);
}
void writeUnsignedByte(uint8_t aByte) {
char tStringBuffer[4];
utoa(aByte, tStringBuffer, 10);
writeStringSkipLeadingSpaces(tStringBuffer);
}
/*
* 2 Byte Hex output with 2 Byte prefix "0x"
*/
void writeUnsignedByteHexWithPrefix(uint8_t aByte) {
writeBinary('0');
writeBinary('x');
writeUnsignedByteHex(aByte);
}
char nibbleToHex(uint8_t aByte) {
aByte = aByte & 0x0F;
if (aByte < 10) {
return aByte + '0';
}
return aByte + 'A' - 10;
}
/*
* 2 Byte Hex output
*/
void writeUnsignedByteHex(uint8_t aByte) {
char tStringBuffer[3];
// tStringBuffer[0] = nibbleToHex(aByte >> 4);
// tStringBuffer[1] = nibbleToHex(aByte);
// tStringBuffer[2] = '\0';
// the utoa() version is 8 bytes smaller than the version with nibbleToHex(), if utoa() is allocated by another function.
utoa(aByte, &tStringBuffer[0], 16);
if (tStringBuffer[1] == '\0') {
tStringBuffer[2] = '\0';
tStringBuffer[1] = tStringBuffer[0];
tStringBuffer[0] = '0';
}
writeString(tStringBuffer);
}
void writeByte(int8_t aByte) {
char tStringBuffer[5];
itoa(aByte, tStringBuffer, 10);
writeStringSkipLeadingSpaces(tStringBuffer);
}
void writeInt(int aInteger) {
char tStringBuffer[7];
itoa(aInteger, tStringBuffer, 10);
writeStringSkipLeadingSpaces(tStringBuffer);
}
void writeUnsignedInt(unsigned int aInteger) {
char tStringBuffer[6];
itoa(aInteger, tStringBuffer, 10);
writeStringSkipLeadingSpaces(tStringBuffer);
}
void writeLong(long aLong) {
char tStringBuffer[12];
ltoa(aLong, tStringBuffer, 10);
writeStringSkipLeadingSpaces(tStringBuffer);
}
void writeUnsignedLong(unsigned long aLong) {
char tStringBuffer[11];
ltoa(aLong, tStringBuffer, 10);
writeStringSkipLeadingSpaces(tStringBuffer);
}
void writeFloat(double aFloat) {
char tStringBuffer[11];
dtostrf(aFloat, 10, 3, tStringBuffer);
writeStringSkipLeadingSpaces(tStringBuffer);
}
void writeFloat(double aFloat, uint8_t aDigits) {
char tStringBuffer[11];
dtostrf(aFloat, 10, aDigits, tStringBuffer);
writeStringSkipLeadingSpaces(tStringBuffer);
}
/*
* The Serial Instance!!!
*/
// #if ... to be compatible with ATTinyCores and AttinyDigisparkCores
#if ((!defined(UBRRH) && !defined(UBRR0H)) || (defined(USE_SOFTWARE_SERIAL) && (USE_SOFTWARE_SERIAL != 0))) || defined(TINY_DEBUG_SERIAL_SUPPORTED) || ((defined(UBRRH) || defined(UBRR0H) || defined(LINBRRH)) && (defined(USE_SOFTWARE_SERIAL) && (USE_SOFTWARE_SERIAL == 0)))
// Switch to SerialOut since Serial is already defined or comment out
// at line 54 in TinySoftwareSerial.h included in in ATTinyCores/src/tiny/Arduino.h at line 228 for ATTinyCores
// or line 71 in HardwareSerial.h included in ATTinyCores/src/tiny/Arduino.h at line 227 for ATTinyCores
// or line 627ff TinyDebugSerial.h included in AttinyDigisparkCores/src/tiny/WProgram.h at line 18 for AttinyDigisparkCores
TinySerialOut SerialOut;
#else
TinySerialOut Serial;
#endif
/*
* Member functions for TinySerialOut
*/
/*
* An alternative way to call the init function :-)
*/
void TinySerialOut::begin(long aBaudrate) {
initTXPin();
#if defined(USE_115200BAUD) //else smaller code, but only 38400 baud at 1 MHz
if (aBaudrate != 115200) {
println(F("Only 115200 supported!"));
}
#else
#if (F_CPU == 1000000)
if (aBaudrate != 38400) {
println(F("Only 38400 supported!"));
}
#else
if (aBaudrate != 230400) {
println(F("Only 230400 supported!"));
}
#endif
#endif
}
void TinySerialOut::end() {
// no action needed
}
void TinySerialOut::flush() {
// no action needed, since we do not use a buffer
}
/*
* 2 Byte Hex output with 2 Byte prefix "0x"
*/
void TinySerialOut::printHex(uint8_t aByte) {
writeUnsignedByteHexWithPrefix(aByte);
}
void TinySerialOut::printHex(uint16_t aWord) {
writeUnsignedByteHexWithPrefix(aWord >> 8);
writeUnsignedByteHex(aWord);
}
void TinySerialOut::printlnHex(uint8_t aByte) {
printHex(aByte);
println();
}
void TinySerialOut::printlnHex(uint16_t aWord) {
printHex(aWord);
println();
}
// virtual functions of Print class
size_t TinySerialOut::write(uint8_t aByte) {
writeBinary(aByte);
return 1;
}
#if !defined(TINY_SERIAL_INHERIT_FROM_PRINT)
void TinySerialOut::print(const char* aStringPtr) {
writeString(aStringPtr);
}
void TinySerialOut::print(const __FlashStringHelper * aStringPtr) {
writeString(aStringPtr);
}
void TinySerialOut::print(char aChar) {
writeBinary(aChar);
}
void TinySerialOut::print(uint8_t aByte, uint8_t aBase) {
if (aBase == 16) {
/*
* Print Hex always with two characters
*/
writeUnsignedByteHex(aByte);
} else {
char tStringBuffer[4];
utoa(aByte, tStringBuffer, aBase);
writeStringSkipLeadingSpaces(tStringBuffer);
}
}
void TinySerialOut::print(int aInteger, uint8_t aBase) {
char tStringBuffer[7];
itoa(aInteger, tStringBuffer, aBase);
writeStringSkipLeadingSpaces(tStringBuffer);
}
void TinySerialOut::print(unsigned int aInteger, uint8_t aBase) {
char tStringBuffer[6];
itoa(aInteger, tStringBuffer, aBase);
writeStringSkipLeadingSpaces(tStringBuffer);
}
void TinySerialOut::print(long aLong, uint8_t aBase) {
char tStringBuffer[12];
ltoa(aLong, tStringBuffer, aBase);
writeStringSkipLeadingSpaces(tStringBuffer);
}
void TinySerialOut::print(unsigned long aLong, uint8_t aBase) {
char tStringBuffer[11];
ltoa(aLong, tStringBuffer, aBase);
writeStringSkipLeadingSpaces(tStringBuffer);
}
void TinySerialOut::print(double aFloat, uint8_t aDigits) {
char tStringBuffer[11];
dtostrf(aFloat, 10, aDigits, tStringBuffer);
writeStringSkipLeadingSpaces(tStringBuffer);
}
void TinySerialOut::println(const char* aStringPtr) {
print(aStringPtr);
println();
}
void TinySerialOut::println(const __FlashStringHelper * aStringPtr) {
print(aStringPtr);
println();
}
void TinySerialOut::println(uint8_t aByte, uint8_t aBase) {
print(aByte, aBase);
println();
}
void TinySerialOut::println(int aInteger, uint8_t aBase) {
print(aInteger, aBase);
println();
}
void TinySerialOut::println(unsigned int aInteger, uint8_t aBase) {
print(aInteger, aBase);
println();
}
void TinySerialOut::println(long aLong, uint8_t aBase) {
print(aLong, aBase);
println();
}
void TinySerialOut::println(unsigned long aLong, uint8_t aBase) {
print(aLong, aBase);
println();
}
void TinySerialOut::println(double aFloat, uint8_t aDigits) {
print(aFloat, aDigits);
println();
}
void TinySerialOut::println() {
print('\r');
print('\n');
}
#endif // !defined(TINY_SERIAL_INHERIT_FROM_PRINT)
/********************************
* Basic serial output function
*******************************/
inline void delay4CyclesInlineExact(uint16_t a4Microseconds) {
/*
* The loop takes 4 cycles (4 microseconds at 1 MHz). Last loop is only 3 cycles. Setting of loop counter a4Microseconds needs 2 cycles
* 3 -> 13 cycles (3*4 -1 + 2) = 3*4 + 1
* 4 -> 17 cycles
* 5 -> 21 cycles
*/
asm volatile (
"1: sbiw %0,1" "\n\t" // 2 cycles
"brne .-4" : "=w" (a4Microseconds) : "0" (a4Microseconds)// 2 cycles
);
}
#if (F_CPU == 1000000) && defined(USE_115200BAUD) //else smaller code, but only 38400 baud at 1 MHz
/*
* 115200 baud - 8,680 cycles per bit, 86,8 per byte at 1 MHz
*
* Assembler code for 115200 baud extracted from Digispark core files:
* Code size is 196 Byte (including first call)
*
* TinySerialOut.h - Tiny write-only software serial.
* Copyright 2010 Rowdy Dog Software. This code is part of Arduino-Tiny.
*
* Arduino-Tiny is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*/
void write1Start8Data1StopNoParity(uint8_t aValue) {
asm volatile
(
"cbi %[txport], %[txpin]" "\n\t" /* 2 <--- 0 */
"ror %[value]" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"brcs L%=b0h" "\n\t" /* 1 (not taken) */
"nop" "\n\t" /* 1 */
"cbi %[txport], %[txpin]" "\n\t" /* 2 <--- st is 9 cycles */
"rjmp L%=b0z" "\n\t" /* 2 */
"L%=b0h: " /* 2 (taken) */
"sbi %[txport], %[txpin]" "\n\t" /* 2 <--- st is 9 cycles */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"L%=b0z: "
"ror %[value]" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"brcs L%=b1h" "\n\t" /* 1 (not taken) */
"nop" "\n\t" /* 1 */
"cbi %[txport], %[txpin]" "\n\t" /* 2 <--- b0 is 8 cycles */
"rjmp L%=b1z" "\n\t" /* 2 */
"L%=b1h: " /* 2 (taken) */
"sbi %[txport], %[txpin]" "\n\t" /* 2 <--- b0 is 8 cycles */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"L%=b1z: "
"ror %[value]" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"brcs L%=b2h" "\n\t" /* 1 (not taken) */
"nop" "\n\t" /* 1 */
"cbi %[txport], %[txpin]" "\n\t" /* 2 <--- b1 is 9 cycles */
"rjmp L%=b2z" "\n\t" /* 2 */
"L%=b2h: " /* 2 (taken) */
"sbi %[txport], %[txpin]" "\n\t" /* 2 <--- b1 is 9 cycles */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"L%=b2z: "
"ror %[value]" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"brcs L%=b3h" "\n\t" /* 1 (not taken) */
"nop" "\n\t" /* 1 */
"cbi %[txport], %[txpin]" "\n\t" /* 2 <--- b2 is 9 cycles */
"rjmp L%=b3z" "\n\t" /* 2 */
"L%=b3h: " /* 2 (taken) */
"sbi %[txport], %[txpin]" "\n\t" /* 2 <--- b2 is 9 cycles */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"L%=b3z: "
"ror %[value]" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"brcs L%=b4h" "\n\t" /* 1 (not taken) */
"nop" "\n\t" /* 1 */
"cbi %[txport], %[txpin]" "\n\t" /* 2 <--- b3 is 8 cycles */
"rjmp L%=b4z" "\n\t" /* 2 */
"L%=b4h: " /* 2 (taken) */
"sbi %[txport], %[txpin]" "\n\t" /* 2 <--- b3 is 8 cycles */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"L%=b4z: "
"ror %[value]" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"brcs L%=b5h" "\n\t" /* 1 (not taken) */
"nop" "\n\t" /* 1 */
"cbi %[txport], %[txpin]" "\n\t" /* 2 <--- b4 is 9 cycles */
"rjmp L%=b5z" "\n\t" /* 2 */
"L%=b5h: " /* 2 (taken) */
"sbi %[txport], %[txpin]" "\n\t" /* 2 <--- b4 is 9 cycles */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"L%=b5z: "
"ror %[value]" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"brcs L%=b6h" "\n\t" /* 1 (not taken) */
"nop" "\n\t" /* 1 */
"cbi %[txport], %[txpin]" "\n\t" /* 2 <--- b5 is 9 cycles */
"rjmp L%=b6z" "\n\t" /* 2 */
"L%=b6h: " /* 2 (taken) */
"sbi %[txport], %[txpin]" "\n\t" /* 2 <--- b5 is 9 cycles */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"L%=b6z: "
"ror %[value]" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"brcs L%=b7h" "\n\t" /* 1 (not taken) */
"nop" "\n\t" /* 1 */
"cbi %[txport], %[txpin]" "\n\t" /* 2 <--- b6 is 8 cycles */
"rjmp L%=b7z" "\n\t" /* 2 */
"L%=b7h: " /* 2 (taken) */
"sbi %[txport], %[txpin]" "\n\t" /* 2 <--- b6 is 8 cycles */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"L%=b7z: "
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"sbi %[txport], %[txpin]" "\n\t" /* 2 <--- b7 is 9 cycles */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
"nop" "\n\t" /* 1 */
/* <---sp is 9 cycles */
:
:
[value] "r" ( aValue ),
[txport] "I" ( TX_PORT_ADDR ),
[txpin] "I" ( TX_PIN )
);
}
#else
/*
* Small code using loop. Code size is 76 Byte (including first call)
*
* 1 MHz CPU Clock
* 26,04 cycles per bit, 260,4 per byte for 38400 baud at 1 MHz Clock
* 17,36 cycles per bit, 173,6 per byte for 57600 baud at 1 MHz Clock -> therefore use 38400 baud
* 24 cycles between each cbi/sbi (Clear/Set Bit in IO-register) command.
*
* 8 MHz CPU Clock
* 69,44 cycles per bit, 694,4 per byte for 115200 baud at 8 MHz Clock
* 34,72 cycles per bit, 347,2 per byte for 230400 baud at 8 MHz Clock.
* 68 / 33 cycles between each cbi (Clear Bit in IO-register) or sbi command.
*
* 16 MHz CPU Clock
* 138,88 cycles per bit, 1388,8 per byte for 115200 baud at 16 MHz Clock
* 69,44 cycles per bit, 694,4 per byte for 230400 baud at 16 MHz Clock
* 137 / 68 cycles between each cbi (Clear Bit in IO-register) or sbi command.
*
* 2 cycles for each cbi/sbi instruction.
*/
void write1Start8Data1StopNoParity(uint8_t aValue) {
asm volatile
(
"cbi %[txport] , %[txpin]" "\n\t" // 2 PORTB &= ~(1 << TX_PIN);
#if (F_CPU == 1000000) && !defined(USE_115200BAUD) // 1 MHz 38400 baud
// 0 cycles padding to get additional 4 cycles
//delay4CyclesInlineExact(5); -> 20 cycles
"ldi r30 , 0x05" "\n\t"// 1
#elif ((F_CPU == 8000000) && defined(USE_115200BAUD)) || ((F_CPU == 16000000) && !defined(USE_115200BAUD)) // 8 MHz 115200 baud OR 16 MHz 230400 baud
// 3 cycles padding to get additional 7 cycles
"nop" "\n\t"// 1 _nop"();
"nop" "\n\t"// 1 _nop"();
"nop" "\n\t"// 1 _nop"();
//delay4CyclesInlineExact(15); -> 61 cycles
"ldi r30 , 0x0F" "\n\t"// 1
#elif (F_CPU == 8000000) && !defined(USE_115200BAUD) // 8 MHz 230400 baud
// 0 cycles padding to get additional 4 cycles
//delay4CyclesInlineExact(7); -> 29 cycles
"ldi r30 , 0x07" "\n\t"// 1
#elif (F_CPU == 16000000) && defined(USE_115200BAUD) // 16 MHz 115200 baud
// 0 cycles padding to get additional 4 cycles
//delay4CyclesInlineExact(33); -> 133 cycles
"ldi r30 , 0x21" "\n\t"// 1
#endif
"ldi r31 , 0x00" "\n\t" // 1
"delay1:"
"sbiw r30 , 0x01" "\n\t"// 2
"brne delay1" "\n\t"// 1-2
"ldi r25 , 0x08" "\n\t"// 1
// Start of loop
// if (aValue & 0x01) {
"txloop:"
"sbrs %[value] , 0" "\n\t"// 1
"rjmp .+6" "\n\t"// 2
"nop" "\n\t"// 1
"sbi %[txport] , %[txpin]" "\n\t"// 2 PORTB |= 1 << TX_PIN;
"rjmp .+6" "\n\t"// 2
"cbi %[txport] , %[txpin]" "\n\t"// 2 PORTB &= ~(1 << TX_PIN);
"nop" "\n\t"// 1
"nop" "\n\t"// 1
"lsr %[value]" "\n\t"// 1 aValue = aValue >> 1;
#if (F_CPU == 1000000) && !defined(USE_115200BAUD) // 1 MHz 38400 baud
// 3 cycles padding to get additional 11 cycles
"nop" "\n\t"// 1
"nop" "\n\t"// 1
"nop" "\n\t"// 1
// delay4CyclesInlineExact(3); -> 13 cycles
"ldi r30 , 0x03" "\n\t"// 1
#elif ((F_CPU == 8000000) && defined(USE_115200BAUD)) || ((F_CPU == 16000000) && !defined(USE_115200BAUD)) // 8 MHz 115200 baud OR 16 MHz 230400 baud
// 3 cycles padding to get additional 11 cycles
"nop" "\n\t"// 1
"nop" "\n\t"// 1
"nop" "\n\t"// 1
// delay4CyclesInlineExact(14); -> 57 cycles
"ldi r30 , 0x0E" "\n\t"// 1
#elif (F_CPU == 8000000) && !defined(USE_115200BAUD) // 8 MHz 230400 baud
// 0 cycles padding to get additional 8 cycles
// delay4CyclesInlineExact(6); -> 25 cycles
"ldi r30 , 0x05" "\n\t"// 1
#elif (F_CPU == 16000000) && defined(USE_115200BAUD) // 16 MHz 115200 baud
// 0 cycles padding to get additional 8 cycles
//delay4CyclesInlineExact(32); -> 129 cycles
"ldi r30 , 0x20" "\n\t"// 1
#endif
"ldi r31 , 0x00" "\n\t" // 1
"delay2:"
"sbiw r30 , 0x01" "\n\t"// 2
"brne delay2" "\n\t"// 1-2
// }while (i > 0);
"subi r25 , 0x01" "\n\t"// 1
"brne txloop" "\n\t"// 1-2
// To compensate for missing loop cycles at last bit
"nop" "\n\t"// 1
"nop" "\n\t"// 1
"nop" "\n\t"// 1
"nop" "\n\t"// 1
// Stop bit
"sbi %[txport] , %[txpin]" "\n\t"// 2 PORTB |= 1 << TX_PIN;
#if (F_CPU == 1000000) && !defined(USE_115200BAUD) // 1 MHz 38400 baud
// delay4CyclesInlineExact(4); -> 17 cycles - gives minimum 25 cycles for stop bit
"ldi r30 , 0x04" "\n\t"// 1
#elif ((F_CPU == 8000000) && defined(USE_115200BAUD)) || ((F_CPU == 16000000) && !defined(USE_115200BAUD)) // 8 MHz 115200 baud OR 16 MHz 230400 baud
// delay4CyclesInlineExact(15) -> 61 cycles - gives minimum 69 cycles for stop bit
"ldi r30 , 0x0F" "\n\t"// 1
#elif (F_CPU == 8000000) && !defined(USE_115200BAUD) // 8 MHz 230400 baud
// delay4CyclesInlineExact(5) -> 27 cycles - gives minimum 35 cycles for stop bit
"ldi r30 , 0x05" "\n\t"// 1
#elif (F_CPU == 16000000) && defined(USE_115200BAUD) // 16 MHz 115200 baud
// delay4CyclesInlineExact(32) -> 129 cycles - gives minimum 137 cycles for stop bit
"ldi r30 , 0x20" "\n\t"// 1
#endif
"ldi r31 , 0x00" "\n\t" // 1
"delay3:"
"sbiw r30 , 0x01" "\n\t"//
"brne delay3" "\n\t"// 1-2
// return needs 4 cycles, load of next value needs 1 cycle, next rcall needs 3 cycles -> gives additional 8 cycles minimum for stop bit
:
:
[value] "r" ( aValue ),
[txport] "I" ( TX_PORT_ADDR ) , /* 0x18 is PORTB on Attiny 85 */
[txpin] "I" ( TX_PIN )
:
"r25",
"r30",
"r31"
);
}
#endif
/*
* C Version which generates the assembler code above.
* In order to guarantee the correct timing, compile with Arduino standard settings or:
* avr-g++ -I"C:\arduino\hardware\arduino\avr\cores\arduino" -I"C:\arduino\hardware\arduino\avr\variants\standard" -c -g -w -Os -ffunction-sections -fdata-sections -mmcu=attiny85 -DF_CPU=1000000UL -MMD -o "TinySerialOut.o" "TinySerialOut.cpp"
* Tested with Arduino 1.6.8 and 1.8.5/gcc4.9.2
* C Version does not work with AVR gcc7.3.0, since optimization is too bad
*/
void write1Start8Data1StopNoParity_C_Version(uint8_t aValue) {
/*
* C Version here for 38400 baud at 1 MHz Clock. You see, it is simple :-)
*/
// start bit
TX_PORT &= ~(1 << TX_PIN);
_NOP();
delay4CyclesInlineExact(4);
// 8 data bits
uint8_t i = 8;
do {
if (aValue & 0x01) {
// bit=1
// to compensate for jump at data=0
_NOP();
TX_PORT |= 1 << TX_PIN;
} else {
// bit=0
TX_PORT &= ~(1 << TX_PIN);
// compensate for different cycles of sbrs
_NOP();
_NOP();
}
aValue = aValue >> 1;
// 3 cycles padding
_NOP();
_NOP();
_NOP();
delay4CyclesInlineExact(3);
--i;
} while (i > 0);
// to compensate for missing loop cycles at last bit
_NOP();
_NOP();
_NOP();
_NOP();
// Stop bit
TX_PORT |= 1 << TX_PIN;
// -8 cycles to compensate for fastest repeated call (1 ret + 1 load + 1 call)
delay4CyclesInlineExact(4); // gives minimum 25 cycles for stop bit :-)
}
#endif // defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny87__) || defined(__AVR_ATtiny167__)