-
Notifications
You must be signed in to change notification settings - Fork 0
/
ATtinySerialOut.h
207 lines (182 loc) · 7.8 KB
/
ATtinySerialOut.h
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
/*
* ATtinySerialOut.h
*
* Copyright (C) 2015-2019 Armin Joachimsmeyer
* Email: [email protected]
*
* 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>.
*
*/
//
// ATMEL ATTINY85
//
// +-\/-+
// PCINT5/!RESET/ADC0/dW (D5) PB5 1| |8 Vcc
// PCINT3/XTAL1/CLKI/!OC1B/ADC3 (D3) PB3 2| |7 PB2 (D2) SCK/USCK/SCL/ADC1/T0/INT0/PCINT2 / TX Debug output
// PCINT4/XTAL2/CLKO/OC1B/ADC2 (D4) PB4 3| |6 PB1 (D1) MISO/DO/AIN1/OC0B/OC1A/PCINT1
// GND 4| |5 PB0 (D0) MOSI/DI/SDA/AIN0/OC0A/!OC1A/AREF/PCINT0
// +----+
#ifndef TINY_SERIAL_OUT_H_
#define TINY_SERIAL_OUT_H_
#if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny87__) || defined(__AVR_ATtiny167__)
#include <Arduino.h>
#include <stdint.h>
#include <stddef.h> // for size_t
#include <avr/interrupt.h> // for cli() and sei()
#include <avr/io.h>
#if (F_CPU != 1000000) && (F_CPU != 8000000) && (F_CPU != 16000000)
#error "F_CPU value must be 1000000, 8000000 or 16000000."
#endif
/*
* Change this, if you need another pin as serial output
* or set it as Symbol like "-DTX_PIN PB1"
* or when switching port (e.g. for ATiny167), then we need more Symbols like "-DTX_PIN PB1 -DTX_PORT PORTB -DTX_PORT_ADDR 0x05 -TX_DDR DDRB"
*/
#if defined(__AVR_ATtiny87__) || defined(__AVR_ATtiny167__)
#ifndef TX_PIN
#define TX_PIN PA1 // (package pin 2 on Tiny167) - can use one of PA0 to PA7 here
#endif
#ifndef TX_PORT
#define TX_PORT PORTA
#define TX_PORT_ADDR 0x02 // PORTA
#define TX_DDR DDRA
//#define TX_PORT PORTB
//#define TX_PORT_ADDR 0x05
//#define TX_DDR DDRB
#endif
#else // defined(__AVR_ATtiny87__) || defined(__AVR_ATtiny167__)
#ifndef TX_PIN
#define TX_PIN PB2 // (package pin 7 on Tiny85) - can use one of PB0 to PB4 (+PB5) here
#endif
#define TX_PORT PORTB
#define TX_PORT_ADDR 0x18 // PORTB
#define TX_DDR DDRB
#endif // defined(__AVR_ATtiny87__) || defined(__AVR_ATtiny167__)
/*
* @1 MHz use bigger (+120 bytes for unrolled loop) but faster code. Otherwise only 38400 baud is possible.
* @8/16 MHz use 115200 baud instead of 230400 baud.
*/
#ifndef TINY_SERIAL_DO_NOT_USE_115200BAUD // define this to force using other baud rates
#define USE_115200BAUD
#endif
/*
* Define or comment this out, if you want to use this class as a replacement for standard Serial as the print class
* Adds around 800 Bytes of code
*/
//#define TINY_SERIAL_INHERIT_FROM_PRINT
/*
* Define or comment this out, if you want to save code size and if you can live with 87 micro seconds intervals of disabled interrupts for each sent byte.
*/
//#define USE_ALWAYS_CLI_SEI_GUARD_FOR_OUTPUT
extern bool sUseCliSeiForWrite; // default is true
void useCliSeiForStrings(bool aUseCliSeiForWrite); // might be useful to set to false if output is done from ISR, to avoid to call unwanted sei().
inline void initTXPin() {
// TX_PIN is active LOW, so set it to HIGH initially
TX_PORT |= (1 << TX_PIN);
// set pin direction to output
TX_DDR |= (1 << TX_PIN);
}
void write1Start8Data1StopNoParity(uint8_t aValue);
inline void write1Start8Data1StopNoParityWithCliSei(uint8_t aValue) {
uint8_t oldSREG = SREG;
cli();
write1Start8Data1StopNoParity(aValue);
SREG = oldSREG;
}
inline void writeValue(uint8_t aValue) {
write1Start8Data1StopNoParity(aValue);
}
// The same class as for plain arduino
#if defined(ARDUINO_AVR_DIGISPARK)
// The digispark library defines (2/2019) F but not __FlashStringHelper
//# define F(string_literal) ((fstr_t*)PSTR(string_literal))
#define __FlashStringHelper fstr_t
#endif
#if not defined(F)
class __FlashStringHelper;
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
#endif
void writeString(const char * aStringPtr);
void writeString(const __FlashStringHelper * aStringPtr);
void writeString_P(const char * aStringPtr);
void writeString_E(const char * aStringPtr);
void writeStringWithCliSei(const char * aStringPtr);
void writeStringWithoutCliSei(const char * aStringPtr);
void writeStringSkipLeadingSpaces(const char * aStringPtr);
void writeBinary(uint8_t aByte); // write direct without decoding
void writeChar(uint8_t aChar); // Synonym for writeBinary
void writeByte(int8_t aByte);
void writeUnsignedByte(uint8_t aByte);
void writeUnsignedByteHex(uint8_t aByte);
void writeUnsignedByteHexWithPrefix(uint8_t aByte);
void writeInt(int aInteger);
void writeUnsignedInt(unsigned int aInteger);
void writeLong(long aLong);
void writeUnsignedLong(unsigned long aLong);
void writeFloat(double aFloat);
void writeFloat(double aFloat, uint8_t aDigits);
char nibbleToHex(uint8_t aByte);
#if defined(TINY_SERIAL_INHERIT_FROM_PRINT)
class TinySerialOut: public Print
#else
class TinySerialOut
#endif
{
public:
void begin(long);
void end();
void flush(void);
void printHex(uint8_t aByte); // with 0x prefix
void printHex(uint16_t aWord); // with 0x prefix
void printlnHex(uint8_t aByte); // with 0x prefix
void printlnHex(uint16_t aWord); // with 0x prefix
// virtual functions of Print class
size_t write(uint8_t aByte);
#if !defined(TINY_SERIAL_INHERIT_FROM_PRINT)
void print(const __FlashStringHelper * aStringPtr);
void print(const char* aStringPtr);
void print(char aChar);
void print(uint8_t aByte, uint8_t aBase = 10);
void print(int aInteger, uint8_t aBase = 10);
void print(unsigned int aInteger, uint8_t aBase = 10);
void print(long aLong, uint8_t aBase = 10);
void print(unsigned long aLong, uint8_t aBase = 10);
void print(double aFloat, uint8_t aDigits = 2);
void println(const char* aStringPtr);
void println(const __FlashStringHelper * aStringPtr);
void println(char aChar);
void println(uint8_t aByte, uint8_t aBase = 10);
void println(int aInteger, uint8_t aBase = 10);
void println(unsigned int aInteger, uint8_t aBase = 10);
void println(long aLong, uint8_t aBase = 10);
void println(unsigned long aLong, uint8_t aBase = 10);
void println(double aFloat, uint8_t aDigits = 2);
void println(void);
#endif // TINY_SERIAL_INHERIT_FROM_PRINT
};
// #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
extern TinySerialOut SerialOut;
#define Serial SerialOut
#else
extern TinySerialOut Serial;
#endif
#endif // defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny87__) || defined(__AVR_ATtiny167__)
#endif /* TINY_SERIAL_OUT_H_ */