-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathds.h
301 lines (248 loc) · 13.3 KB
/
ds.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
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
/*==============================================================================
TinyDebugSerial.h - Tiny write-only software serial.
Copyright 2010 Rowdy Dog Software.
This file 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.
Arduino-Tiny 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Arduino-Tiny. If not, see <http://www.gnu.org/licenses/>.
==============================================================================*/
#include <avr/io.h>
#define SERPORT 0x1B
#define SERPIN PINA2
void ser_init () {
asm volatile (
"sbi %[port]-1, %[pin]" "\n\t"
"sbi %[port], %[pin]" "\n\t"
:
:
[port] "I" ( SERPORT ),
[pin] "I" ( SERPIN )
:
);
}
uint8_t ser_write_1_115200 (uint8_t value) {
asm volatile (
"cli" "\n\t"
"cbi %[port], %[pin]" "\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 %[port], %[pin]" "\n\t" /* 2 <--- st is 9 cycles */
"rjmp L%=b0z" "\n\t" /* 2 */
"L%=b0h: " /* 2 (taken) */
"sbi %[port], %[pin]" "\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 %[port], %[pin]" "\n\t" /* 2 <--- b0 is 8 cycles */
"rjmp L%=b1z" "\n\t" /* 2 */
"L%=b1h: " /* 2 (taken) */
"sbi %[port], %[pin]" "\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 %[port], %[pin]" "\n\t" /* 2 <--- b1 is 9 cycles */
"rjmp L%=b2z" "\n\t" /* 2 */
"L%=b2h: " /* 2 (taken) */
"sbi %[port], %[pin]" "\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 %[port], %[pin]" "\n\t" /* 2 <--- b2 is 9 cycles */
"rjmp L%=b3z" "\n\t" /* 2 */
"L%=b3h: " /* 2 (taken) */
"sbi %[port], %[pin]" "\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 %[port], %[pin]" "\n\t" /* 2 <--- b3 is 8 cycles */
"rjmp L%=b4z" "\n\t" /* 2 */
"L%=b4h: " /* 2 (taken) */
"sbi %[port], %[pin]" "\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 %[port], %[pin]" "\n\t" /* 2 <--- b4 is 9 cycles */
"rjmp L%=b5z" "\n\t" /* 2 */
"L%=b5h: " /* 2 (taken) */
"sbi %[port], %[pin]" "\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 %[port], %[pin]" "\n\t" /* 2 <--- b5 is 9 cycles */
"rjmp L%=b6z" "\n\t" /* 2 */
"L%=b6h: " /* 2 (taken) */
"sbi %[port], %[pin]" "\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 %[port], %[pin]" "\n\t" /* 2 <--- b6 is 8 cycles */
"rjmp L%=b7z" "\n\t" /* 2 */
"L%=b7h: " /* 2 (taken) */
"sbi %[port], %[pin]" "\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 %[port], %[pin]" "\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 */
"sei" "\n\t"
:
:
[value] "r" ( value ),
[port] "I" ( SERPORT ),
[pin] "I" ( SERPIN )
);
return 1;
}
__attribute__((always_inline, unused)) static inline void TinyDebugSerialWriterBangOneByte( uint8_t value, uint8_t lom, uint8_t him, uint8_t oloops, uint8_t iloops, uint8_t nops ) {
uint8_t i;
uint8_t j;
uint8_t ol;
uint8_t il;
uint8_t b; // Initialized to the low bits
uint8_t hib;
uint8_t m;
b = ((value << 1) & 0x1F);
hib = ((value >> 4) & 0x1F) | 0x10;
asm volatile
(
"ldi %[j], 2" "\n\t"
"ldi %[i], 5" "\n\t"
"ldi %[m], %[lom]" "\n\t"
// Note: 8 MHz, 9600 baud ---> disabling interrupts does not appear to be necessary
"cli" "\n\t"
"rjmp L%=ntop" "\n\t"
"L%=btop: "
"nop" "\n\t" // ---> 7
"nop" "\n\t" //
"nop" "\n\t" //
"nop" "\n\t" //
"nop" "\n\t" //
"nop" "\n\t" //
"nop" "\n\t" //
"L%=ntop: "
"ror %[b]" "\n\t" // ---> 1
"brcs L%=bxh" "\n\t" // 1 (not taken)
"cbi %[serreg], %[serbit]" "\n\t" // 2
"rjmp L%=bxz" "\n\t" // 2
"L%=bxh: " // 2 (taken)
"sbi %[serreg], %[serbit]" "\n\t" // 2
"nop" "\n\t" // 1
// ---> 5
"L%=bxz: "
"ror %[m]" "\n\t" // ---> 3 or 4
"brcc L%=bnoe" "\n\t" //
"nop" "\n\t" //
"nop" "\n\t" //
"L%=bnoe: "
// ---> 1
".if %[oloops] >= 1" "\n\t" // if oloops >= 1 then...
"ldi %[ol], %[oloops]" "\n\t" // 4*oloops + oloops*(3*iloops) or oloops*((3*iloops)+4)
"L%=odelay: " "\n\t"
".endif" "\n\t"
"ldi %[il], %[iloops]" "\n\t" // if oloops == 0 then...
"L%=idelay: " "\n\t" // (3*iloops)
"dec %[il]" "\n\t"
"brne L%=idelay" "\n\t"
"nop" "\n\t"
".if %[oloops] >= 1" "\n\t"
"dec %[ol]" "\n\t"
"brne L%=odelay" "\n\t"
"nop" "\n\t"
".endif" "\n\t"
".if %[nops] >= 1" "\n\t"
"nop" "\n\t" //
".endif" "\n\t"
".if %[nops] >= 2" "\n\t"
"nop" "\n\t" //
".endif" "\n\t"
"dec %[i]" "\n\t" // ---> 3
"brne L%=btop" "\n\t" //
"nop" "\n\t" //
"dec %[j]" "\n\t" // ---> 7
"breq L%=bfin" "\n\t" //
"ldi %[i], 5" "\n\t" //
"mov %[b], %[hib]" "\n\t" //
"ldi %[m], %[him]" "\n\t" //
"rjmp L%=ntop" "\n\t" //
"L%=bfin: "
"sei" "\n\t"
:
[i] "=&r" ( i ),
[j] "=&r" ( j ),
[ol] "=&r" ( ol ),
[il] "=&r" ( il ),
[m] "=&r" ( m )
:
[b] "r" ( b ),
[hib] "r" ( hib ),
[serreg] "I" ( SERPORT ),
[serbit] "M" ( SERPIN ),
[lom] "M" ( lom ),
[him] "M" ( him ),
[oloops] "M" ( oloops ),
[iloops] "M" ( iloops ),
[nops] "M" ( nops )
:
"r31",
"r30"
);
}
#define ser_write_16_115200(value) TinyDebugSerialWriterBangOneByte(value,0b11110,0b11111,0,39,1)
#define ser_write_8_115200(value) TinyDebugSerialWriterBangOneByte(value,0b01010,0b10100,0,16,1)
#define ser_write(value) ser_write_8_115200(value)