-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug.cpp
521 lines (521 loc) · 14.3 KB
/
debug.cpp
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
//-----------------------------------------------------------------------------
// debug.cpp
//-----------------------------------------------------------------------------
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include "debug.h"
//-----------------------------------------------------------------------------
#ifdef DEBUG
//char dbg_fmt[DEBUG_BUF_SIZE];
char dbg_buf[DEBUG_BUF_SIZE];
//-----------------------------------------------------------------------------
#pragma GCC diagnostic ignored "-Wunused-function"
//-----------------------------------------------------------------------------
__inline void _write_byte_4M_230400(char data)
{
register BYTE cbit;
asm volatile
(
"cbi %[port],%[pin]\n" // start bit
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"nop\n"
"ldi %[cbit],8 \n"
"loop: sbrc %[data],0 \n" // 1-2-3
"rjmp one\n" // 2 clk
"nop\n" // to-0 delay
"cbi %[port],%[pin]\n" // 2 clk
"rjmp .+4 \n" // to-1 anti-delay
"one: sbi %[port],%[pin]\n" // 2 clk
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"lsr %[data] \n"
"dec %[cbit] \n"
"brne loop \n"
"rjmp .+0 \n"
"rjmp .+0 \n" // before-stop delay
"sbi %[port],%[pin]\n" // stop bit
: [cbit] "=&r" (cbit)
: [port] "I" (_SFR_IO_ADDR(DEBUG_PORT)),
[pin] "I" (DEBUG_BIT),
[data] "r" (data)
);
}
//-----------------------------------------------------------------------------
__inline void _write_byte_4M_115200(char data)
{
register BYTE cbit;
asm volatile
(
"cbi %[port],%[pin]\n" // start bit
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"ldi %[cbit],8 \n"
"loop: sbrc %[data],0 \n" // 1-2-3
"rjmp one\n" // 2 clk
"rjmp .+0 \n" // to-0 delay
"cbi %[port],%[pin]\n" // 2 clk
"rjmp .+4 \n" // to-1 anti-delay
"one: sbi %[port],%[pin]\n" // 2 clk
"nop\n"
"nop\n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"lsr %[data] \n"
"dec %[cbit] \n"
"brne loop \n"
"rjmp .+0 \n"
"rjmp .+0 \n" // before-stop delay
"sbi %[port],%[pin]\n" // stop bit
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
"rjmp .+0 \n"
: [cbit] "=&r" (cbit)
: [port] "I" (_SFR_IO_ADDR(DEBUG_PORT)),
[pin] "I" (DEBUG_BIT),
[data] "r" (data)
);
}
//-----------------------------------------------------------------------------
__inline void _write_byte_16M_1152000(char data)
{
register BYTE cbit;
asm volatile
(
"cbi %[port],%[pin]\n" // start bit
"rjmp .+0\n"
"rjmp .+0\n"
"rjmp .+0\n"
"rjmp .+0\n"
"ldi %[cbit],8 \n"
"loop: sbrc %[data],0 \n"
"rjmp one\n"
"cbi %[port],%[pin]\n"
"rjmp .+6 \n"
"one: sbi %[port],%[pin]\n"
"rjmp .+0\n"
"nop \n"
"nop \n"
"rjmp .+0\n"
"lsr %[data] \n"
"dec %[cbit] \n"
"brne loop \n"
"rjmp .+0\n"
"rjmp .+0\n"
"sbi %[port],%[pin]\n" // stop bit
"rjmp .+0\n"
: [cbit] "=&r" (cbit)
: [port] "I" (_SFR_IO_ADDR(DEBUG_PORT)),
[pin] "I" (DEBUG_BIT),
[data] "r" (data)
);
}
//-----------------------------------------------------------------------------
__inline void _write_byte_16M_115200(char data)
{
#define D_DELAY 139
#define D_DELAY2 (D_DELAY - 2)
#define D_DELAY7 (D_DELAY - 7)
#define D_DELAY8 (D_DELAY - 8)
clr_bit(DEBUG_PORT, DEBUG_BIT); // start bit
delay_cycles(D_DELAY2); // ceil(F_CPU)/115200-2 (-2 clocks for 'for' initialization)
for(int i = 0; i < 8; i++) // data bits
{
if(data & 0x01)
{
set_bit(DEBUG_PORT, DEBUG_BIT);
delay_cycles(D_DELAY8); // -8 clocks
}
else
{
clr_bit(DEBUG_PORT, DEBUG_BIT);
delay_cycles(D_DELAY7); // -7 clocks
}
data>>=1;
}
set_bit(DEBUG_PORT, DEBUG_BIT); // stop bit
delay_cycles(D_DELAY2); // -2 clocks
}
//-----------------------------------------------------------------------------
__inline void _write_byte_20M_1152000(char data)
{
register BYTE cbit;
asm volatile
(
"cbi %[port],%[pin]\n" // start bit
"rjmp .+0\n"
"rjmp .+0\n"
"rjmp .+0\n"
"rjmp .+0\n"
"rjmp .+0\n"
"nop \n"
"ldi %[cbit],8 \n"
"loop: sbrc %[data],0 \n"
"rjmp one\n"
"cbi %[port],%[pin]\n"
"nop \n"
"nop \n"
"rjmp .+8 \n"
"one: sbi %[port],%[pin]\n"
"rjmp .+0\n"
"nop \n"
"rjmp .+0\n"
"rjmp .+0\n"
"rjmp .+0\n"
"lsr %[data] \n"
"dec %[cbit] \n"
"brne loop \n"
"rjmp .+0\n"
"rjmp .+0\n"
"sbi %[port],%[pin]\n" // stop bit
"rjmp .+0\n"
: [cbit] "=&r" (cbit)
: [port] "I" (_SFR_IO_ADDR(DEBUG_PORT)),
[pin] "I" (DEBUG_BIT),
[data] "r" (data)
);
}
//-----------------------------------------------------------------------------
static void DEBUG_SECTION _write_byte(char data)
{
disable_interrupts();
#if F_CPU==4000000UL // 4 MHz
#if DEBUG_BAUD_RATE==230400
_write_byte_4M_230400(data);
#elif DEBUG_BAUD_RATE==115200
_write_byte_4M_115200(data);
#else
#error "Unsupported baud rate, sorry"
#endif
#elif F_CPU==16000000UL // 16 MHz
#if DEBUG_BAUD_RATE==1152000
_write_byte_16M_1152000(data);
#elif DEBUG_BAUD_RATE==115200
_write_byte_16M_115200(data);
#else
#error "Unsupported baud rate, sorry"
#endif
#elif F_CPU==20000000UL // 20 MHz
#if DEBUG_BAUD_RATE==1152000
_write_byte_20M_1152000(data);
#else
#error "Unsupported baud rate, sorry"
#endif
#else
#error "Unsupported CPU frequency, sorry"
#endif
resume_interrupts();
}
//-----------------------------------------------------------------------------
static void DEBUG_SECTION _write(const char *data, int num)
{
for(int i = 0; i < num; i++)
{
disable_interrupts();
_write_byte(data[i]);
resume_interrupts();
}
}
//-----------------------------------------------------------------------------
__inline void _endl()
{
_write_byte('\n');
}
//-----------------------------------------------------------------------------
void DEBUG_SECTION debug_print_hex1(char data,bool new_line)
{
char buf[2];
char c = data & 0x0F;
buf[1] = c < 10 ? c + '0' : c - 10 + 'A';
c= (data >> 4) & 0x0F;
buf[0] = c < 10 ? c + '0' : c - 10 + 'A';
_write(buf, 2);
if(new_line)
{
_endl();
}
}
//-----------------------------------------------------------------------------
__inline void DEBUG_SECTION _print_hexN(long int data, BYTE bytes)
{
char buf[8];
char *p = buf + 7;
bytes *= 2;
for(int i = 0; i < bytes; i++)
{
char c = data & 0x0F;
*p-- = (c < 10) ? c + '0' : c - 10 + 'A';
data >>= 4;
}
_write(buf + sizeof(buf) - bytes, bytes);
}
//-----------------------------------------------------------------------------
void DEBUG_SECTION debug_print_pm(const char *str,bool new_line)
{
char val = 0;
while((val = pgm_read_byte(str++)))
_write_byte(val);
if(new_line)
_endl();
}
//-----------------------------------------------------------------------------
void DEBUG_SECTION debug_print_dump(WORD from, const unsigned int sz, enum mem_area area)
{
WORD index;
for(WORD k = 0; k <= (sz - 1) / 16; k++)
{
#ifdef DEBUG_EXTENDED_DUMP
_write_byte(' ');
debug_print_hex(from + k * 16, false);
_write_byte(':');
_write_byte(' ');
_write_byte(' ');
#endif
for(WORD i = 0; i < 16; i++)
{
index = k * 16 + i;
if(index < sz)
{
BYTE val = 0;
switch(area)
{
case EE:
#ifdef DEBUG_EEPROM
val = EEPROM_read(from + index);
#endif
break;
case PGM:
val = pgm_read_byte((BYTE*)from + index);
break;
case RAM:
val = *(BYTE*)((BYTE*)from + index);
}
debug_print_hex1(val);
}
else
{
_write_byte(' ');
_write_byte(' ');
}
_write_byte(' ');
}
#ifdef DEBUG_EXTENDED_DUMP
_write_byte(' ');
_write_byte(' ');
for(WORD i = 0; i < 16; i++)
{
index = k * 16 + i;
if(index >= sz) break;
BYTE c = 0;
switch(area)
{
case EE:
#ifdef DEBUG_EEPROM
c = EEPROM_read(from + index);
#endif
break;
case PGM:
c = pgm_read_byte((BYTE*)from + index);
break;
case RAM:
c = *(BYTE*)((BYTE*)from + index);
}
switch(c)
{
// непечатаемые символы
case '\r':
case '\n':
case '\v':
case '\f':
case '\t':
case '\b':
case '\0':
case 0x05:
case 0x07:
case 0x0E:
case 0x0F:
case 0x8E:
case 0x8F:
case 0xCF:
case 0xDF:
case 0xEF:
c = '.';
}
_write_byte(c);
}
#endif
if((k + 1) * 16 < sz)
_endl();
}
_endl();
}
//-----------------------------------------------------------------------------
__inline void _print_dec(unsigned long data, bool new_line)
{
char buf[10];
BYTE i = 0;
if(data < 10)
{
_write_byte(data+'0');
}
else
{
while(data)
{
buf[i++] = data % 10;
data /= 10;
}
while(i)
{
_write_byte(buf[--i] + '0');
}
}
if(new_line) _endl();
}
//-----------------------------------------------------------------------------
void DEBUG_SECTION debug_print_dec(char data, bool new_line)
{
if(data<0)
{
_write_byte('-');
_print_dec(~data + 1, new_line);
}
else
{
_print_dec(data, new_line);
}
}
//-----------------------------------------------------------------------------
void DEBUG_SECTION debug_print_dec(void* data, bool new_line)
{
_print_dec((unsigned int)data, new_line);
}
//-----------------------------------------------------------------------------
void DEBUG_SECTION debug_print_dec(int data, bool new_line)
{
if(data<0)
{
_write_byte('-');
_print_dec(~data + 1, new_line);
}
else
{
_print_dec(data, new_line);
}
}
//-----------------------------------------------------------------------------
void DEBUG_SECTION debug_print_dec(long data, bool new_line)
{
if(data<0)
{
_write_byte('-');
_print_dec(~data + 1, new_line);
}
else
{
_print_dec(data, new_line);
}
}
//-----------------------------------------------------------------------------
void DEBUG_SECTION debug_print_dec(unsigned char data, bool new_line)
{
_print_dec(data, new_line);
}
//-----------------------------------------------------------------------------
void DEBUG_SECTION debug_print_dec(unsigned int data, bool new_line)
{
_print_dec(data, new_line);
}
//-----------------------------------------------------------------------------
void DEBUG_SECTION debug_print_dec(unsigned long data, bool new_line)
{
_print_dec(data, new_line);
}
//-----------------------------------------------------------------------------
void DEBUG_SECTION debug_print_hex(char data)
{
debug_print_hex1(data);
_endl();
}
//-----------------------------------------------------------------------------
void DEBUG_SECTION debug_print_hex(int data)
{
_print_hexN(data, 2);
_endl();
}
//-----------------------------------------------------------------------------
void DEBUG_SECTION debug_print_hex(long int data)
{
_print_hexN(data, 4);
_endl();
}
//-----------------------------------------------------------------------------
void DEBUG_SECTION debug_print_hex(unsigned char data)
{
debug_print_hex1(data);
_endl();
}
//-----------------------------------------------------------------------------
void DEBUG_SECTION debug_print_hex(void* data, bool new_line)
{
_print_hexN((unsigned int)data, 2);
if(new_line) _endl();
}
//-----------------------------------------------------------------------------
void DEBUG_SECTION debug_print_hex(unsigned int data, bool new_line)
{
_print_hexN(data, 2);
if(new_line) _endl();
}
//-----------------------------------------------------------------------------
void DEBUG_SECTION debug_print_hex(unsigned long int data)
{
_print_hexN(data, 4);
_endl();
}
//-----------------------------------------------------------------------------
void DEBUG_SECTION debug_print_hex8(BYTE count, ...)
{
va_list list;
va_start(list, count);
while(count-- > 0)
{
debug_print_hex1((char)va_arg(list, int));
_write_byte(' ');
}
_endl();
}
//-----------------------------------------------------------------------------
#endif