sketchbook
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
debug.h
Go to the documentation of this file.
1 #ifndef debug_h
2 #define debug_h
3 
4 /*
5  * Copyright © 2010-2015, Matthias Urlichs <matthias@urlichs.de>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License (included; see the file LICENSE)
16  * for more details.
17  */
18 
19 #include "dev_config.h"
20 
21 #ifndef NO_DEBUG //
22 
23 #ifdef UART_DEBUG
24 #ifndef HAVE_UART
25 #error "Cannot debug using UART without having one"
26 #endif
27 #include "uart.h"
28 
29 /* These macros always send something. They should only be used in
30  * sync-safe places, i.e. not in interrupt code or whatever.
31  */
32 #define DBG_C(x) uart_putc(x)
33 #define DBG_S(x) uart_puts(x)
34 #define DBG_P(x) uart_puts_P(x)
35 #define DBG_P_(x) uart_puts_p(x)
36 #define DBG_N(x) uart_puthex_nibble(x)
37 #define DBG_W(x) uart_puthex_word(x)
38 #define DBG_X(x) uart_puthex_byte_(x)
39 #define DBG_NL() uart_putc('\n')
40 #endif /* UART_DEBUG */
41 
42 #ifdef CONSOLE_DEBUG
43 #ifdef DBG_C
44 #error "Cannot use two debug methods concurrently"
45 #endif
46 #ifndef N_CONSOLE
47 #error "Cannot debug using console without having one"
48 #endif
49 #include "console.h"
50 
51 #define DBG_C(x) console_putc(x)
52 #define DBG_S(x) console_puts(x)
53 #define DBG_P(x) console_puts_P(x)
54 #define DBG_P_(x) console_puts_p(x)
55 #define DBG_N(x) console_puthex_nibble(x)
56 #define DBG_W(x) console_puthex_word(x)
57 #define DBG_X(x) console_puthex_byte_(x)
58 #define DBG_NL() console_putc('\n')
59 #endif // console
60 
61 #endif // !NO_DEBUG
62 
63 #ifndef DBG_C
64 #define DBG_C(x) do { } while(0)
65 #define DBG_S(x) do { } while(0)
66 #define DBG_P(x) do { } while(0)
67 #define DBG_P_(x) do { } while(0)
68 #define DBG_N(x) do { } while(0)
69 #define DBG_W(x) do { } while(0)
70 #define DBG_X(x) do { } while(0)
71 #define DBG_NL() do { } while(0)
72 #endif
73 
74 //************* second part: debugging via emitting a bit/byte to a port
75 
76 #if defined(HAVE_DBG_PORT) && !defined(NO_DEBUG)
77 #define DBGA(x,v) do { v=(x); asm volatile("out %0,%1" :: "i"(((int)&DBGPORT)-__SFR_OFFSET),"r"(v)); } while(0)
78 #define DBG(x) do { uint8_t _x; DBGA(x,_x); } while(0)
79 #else
80 #define DBG(x) do { } while(0)
81 #define DBGA(x,v) do { } while(0)
82 #undef DBGPORT
83 #endif
84 
85 #if defined(HAVE_DBG_PIN) && !defined(NO_DEBUG)
86 #define DBG_ON() do { DBGPINPORT |= (1<<DBGPIN); } while(0)
87 #define DBG_OFF() do { DBGPINPORT &= ~(1<<DBGPIN); } while(0)
88 #else
89 #define DBG_ON() do { } while(0)
90 #define DBG_OFF() do { } while(0)
91 #undef DBGPINPORT
92 #endif
93 
94 #endif // debug_h