ArduinoMega2560_SPI 1.0
Cargando...
Buscando...
Nada coincide
avr8-stub.h
Ir a la documentación de este archivo.
1/*
2 * avr8-stub.h
3 *
4 * Created on: 25. 3. 2015
5 * Author: Jan Dolinay
6 *
7 * Header file for
8 * GDB Debugging Agent (GDB stub) for ATMega328 and ATMega1280 and ATMega2560
9 * This code is actually "gdb stub" - code which runs in the debugged MCU and
10 * communicates with the gdb debugger. It uses gdb RCP protocol.
11 * The code needs to handle UART Receive interrupt and an interrupt for simulating
12 * software interrupt (EXT INT).
13 *
14 * Configuration options
15 * You can pass the symbols to compiler or change the value below
16 * (For details see the defines in the code below)
17 *
18 * AVR8_BREAKPOINT_MODE - how to implement breakpoints and stepping.
19 *
20 * AVR8_SWINT_SOURCE - which external interrupt is used by the debugger (the corresponding pin cannot be used by user program!)
21 *
22 * AVR8_USE_TIMER0_INSTEAD_OF_WDT - use timer0 instead of watchdog to detect breakpoints.
23 *
24 * AVR8_LOAD_SUPPORT - whether the stub should support loading the program by GDB. Then you can upload the program
25 * and start debugging with single click in eclipse IDE.
26 *
27 * AVR8_USER_BAUDRATE - serial port baud rate for communication with the debugger.
28 * If not provided, default is 115200 for Arduino Uno, Mega, etc.
29 * Example: AVR8_USER_BAUDRATE=9600
30 *
31 * AVR8_UART_NUMBER - which USART is used by the debugger. Default is 0 (use USART0).
32 * On Arduino Mega, by using other USART than the UART0 you can use this debugger together with
33 * the Arduino Serial functions. On Arduino Uno and other boards based on ATmega328 only 0 is available.
34 *
35 *
36 * The following project were used (and combined) to create this stub:
37 * 1) AVR-GDBServer (https://github.com/rouming/AVR-GDBServer)
38 * 2) avr-stub from uOS - embedded operating system (https://code.google.com/p/uos-embedded/)
39 * 3) AVR gdbstub (http://zevv.nl/play/code/avr-gdbstub/)
40 *
41 * The API is inspired by the GDB documentation for stubs: (quote)
42 * The debugging stub for your architecture supplies these three subroutines:
43 * set_debug_traps
44 * This routine arranges for handle_exception to run when your program stops. You must call this subroutine explicitly in your program's startup code.
45 * handle_exception
46 * This is the central workhorse, but your program never calls it explicitly�the setup code arranges for handle_exception to run when a trap is triggered.
47 * handle_exception takes control when your program stops during execution (for example, on a breakpoint), and mediates communications with gdb on the host machine. This is where the communications protocol is implemented; handle_exception acts as the gdb representative on the target machine. It begins by sending summary information on the state of your program, then continues to execute, retrieving and transmitting any information gdb needs, until you execute a gdb command that makes your program resume; at that point, handle_exception returns control to your own code on the target machine.
48 * breakpoint
49 * Use this auxiliary subroutine to make your program contain a breakpoint. Depending on the particular situation, this may be the only way for gdb to get control. For instance, if your target machine has some sort of interrupt button, you won't need to call this; pressing the interrupt button transfers control to handle_exception�in effect, to gdb. On some machines, simply receiving characters on the serial port may also trigger a trap; again, in that situation, you don't need to call breakpoint from your own program�simply running �target remote� from the host gdb session gets control.
50 * Call breakpoint if none of these is true, or if you simply want to make certain your program stops at a predetermined point for the start of your debugging session.
51 * (end quote)
52 *
53 * Note about porting to other AVR MCUs:
54 * You need to change:
55 * 1) UART code in uart_init, putChar and getChar
56 * 2) Code for software interrupt emulation in gdb_enable_swinterrupt and gdb_disable_swinterrupt
57 *
58 * Port to Atmega 1280 (2560), Arduino Mega
59 * USART routines work as is; ISR name changes.
60 * PC is 3 bytes on Mega2560 and 2 bytes on 1280 as follows from Stack Pointer chapter in the datasheet:
61 * The ATmega48A/PA/88A/PA/168A/PA/328/P Program Counter (PC) is 11/12/13/14 bits wide
62 * The ATmega640/1280/1281/2560/2561 Program Counter (PC) is 15/16/17 bits wide
63 *
64 *
65 * This is free software; you can redistribute it and/or
66 * modify it under the terms of the GNU Lesser General Public
67 * License as published by the Free Software Foundation; either
68 * version 3 of the License, or (at your option) any later version.
69 *
70 * This library is distributed in the hope that it will be useful,
71 * but WITHOUT ANY WARRANTY; without even the implied warranty of
72 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
73 * See the GNU Lesser General Public License for more details.
74 *
75 */
76
77#ifndef DRV_DEBUG_H_
78#define DRV_DEBUG_H_
79
80
81#include <avr/io.h>
82#include <stdint.h>
83
84#ifdef __cplusplus
85extern "C" {
86#endif
87
88
89/* --------- Configuration ------------ */
90/** AVR8_BREAKPOINT_MODE
91 * Select the mode for handling breakpoints and step command.
92 * Options:
93 * 0 - FLASH breakpoints with avr-stub bootloader
94 * 1 - RAM only - RAM breakpoints and stepping
95 * 2 - FLASH breakpoints with Optiboot bootloader (using do_spm() function)
96 *
97 * More info:
98 * RAM breakpoints - use external interrupt to stop the program after each instruction and compare PC
99 * with breakpoints. If any BP address is reached, the program is halted. Program runs slow with BP enabled.
100 * (+) No wear of flash memory
101 * (-) Debugged program runs slowly - interrupt occurs after every instruction
102 * and in the ISR there are perhaps 100 cycles needed to save and restore context.
103 * How much the debugged program is affected depends also on the design of the program,
104 * for example, blinking LED with delays based of poling timer value may be affected
105 * little, while delays using busy loop will be much much longer than expected.
106 *
107 * Flash breakpoints - writes special instruction at the position where program should stop.
108 * (-) Flash memory is overwritten often during debug session. It should survive 10 000 erase-write cycles.
109 * (+) Debugged program runs at normal (full) speed between breakpoints.
110 *
111 *
112 * Note: The step command (going to next line) is implemented by comparing the PC register
113 * with desired address even in FLASH breakpoints mode. In principle it would be possible to
114 * implement it by writing to flash but it would wear the flash memory very fast. There are
115 * about 10 overwrites for a single step in the debugger in such case.
116 *
117 * */
118#ifndef AVR8_BREAKPOINT_MODE
119 #define AVR8_BREAKPOINT_MODE (1)
120#endif
121
122/** AVR8_USE_TIMER0_INSTEAD_OF_WDT
123 * Select one of the following options for the interrupt to detect that
124 * the program stopped on a breakpoint:
125 * 0 - do not use TIMER0 interrupts but the watchdog timer interrupt
126 * 1 - use the output compare A interrupt of timer 0 (the timer for millis and delays)
127 *
128 * Using the watchdog timer is the default, but it means that we cannot use the watchdog timer
129 * in the user program.
130 *
131 * The other option is to use the output compare interrupt A of timer0 (TIMER0 COMPA).
132 * To use this option your program must use the Arduino core and timer0 must be configured
133 * by this core. Since the timer is used by Arduino for counting milliseconds, this is
134 * usually the case.
135 * With this option enabled, the user program can use watchdog timer.
136 *
137 * */
138#ifndef AVR8_USE_TIMER0_INSTEAD_OF_WDT
139 #define AVR8_USE_TIMER0_INSTEAD_OF_WDT (0)
140#endif
141#if AVR8_BREAKPOINT_MODE == 1
142 #undef AVR8_USE_TIMER0_INSTEAD_OF_WDT
143 #define AVR8_USE_TIMER0_INSTEAD_OF_WDT (0)
144#endif
145
146/**
147 * AVR8_SWINT_SOURCE
148 * Source for software interrupt.
149 * Valid values are listed below. The pin selected here will be used for generating SW interrupt
150 * by the debugger and cannot be used for anything else - it becomes reserved for the debugger.
151 *
152 * Supported values for Atmega328 (UNO):
153 * 0 - use INT0 (pin PD2, Arduino pin 2)
154 * 1 - use INT1 (pin PD3, Arduino pin 3)
155 *
156 * Supported values for ATmega1284(P):
157 * 0 - use INT0 (pin PD2)
158 * 1 - use INT1 (pin PD3)
159 * 2 - use INT3 (pin PB2)
160 *
161 * Supported values for Atmega2560 and Atmega1280:
162 * TIP: Use value 6 or 7; the pins for these INTs are not connected on Arduino
163 * mega board, so you will not waste any pin.
164 * 0 - INT0 (pin PD0, Arduino pin 21)
165 * 1 - INT1 (pin PD1, Arduino pin 20)
166 * 2 - INT2 (pin PD2, Arduino pin 19)
167 * 3 - INT3 (pin PD3, Arduino pin 18)
168 * 4 - INT4 (pin PE4, Arduino pin 2)
169 * 5 - INT5 (pin PE5, Arduino pin 3)
170 * 6 - INT6 (pin PE6, not used on Arduino)
171 * 7 - INT7 (pin PE7, not used on Arduino)
172 * Note to implementors
173 * INT0-3 uses EICRA reg. and port D
174 * INT4 - 7 uses EICRB reg. and port E
175 * Pins PE6 and PE7 are not connected on Arduino Mega boards.
176 *
177 * NOTE: Special value -1 can be defined to use output compare interrupt of timer0
178 * instead of external interrupt.
179 * IMPORTANT:
180 * - The debugger may not work properly if any interrupt with higher priority that the
181 * timer0 compare interrupt (TIMER0 COMPA) is enabled in the debugged program.
182 * - analogWrite for the pin connected to COMPA channel of timer0 (pin 6 on Arduino Uno)
183 * may be affected by this option.
184 * This option is only available if you use FLASH breakpoints (see AVR8_BREAKPOINT_MODE).
185 *
186 * Note: Pin Change Interrupt (PCINT) could be used also.
187 * It could be one of the Arduino analog pins (PC0 - PC5) which are less likely
188 * to be used by the user program.
189 * Note that if PCINT is used, then INT0 and INT1 used by the user program
190 * could cause troubles, because they have higher priority than PCINT and could prevent
191 * the debugger from catching the program properly...
192 */
193
194#ifndef AVR8_SWINT_SOURCE
195 #define AVR8_SWINT_SOURCE (0)
196#endif
197
198
199/**
200 AVR8_LOAD_SUPPORT
201 Enable or disable support for loading the program from the debugger.
202 If enabled you can just click the Debug button in IDE to upload new program
203 and debug it.
204 IMPORTANT: requires support in bootloader so the bootloader in Arduino must
205 be replaced with the bootloader provided in this package.
206 It doesn't work with the Optiboot bootloader
207
208 Options:
209 0 - load from GDB disabled. Load the program using avrdude as usual.
210 1 - load from GDB enabled.
211*/
212#ifndef AVR8_LOAD_SUPPORT
213 #define AVR8_LOAD_SUPPORT (0)
214#endif
215
216/**
217 * UART module used by the debugger.
218 * This select the serial communication peripheral used for communicating with the GDB.
219 *
220 * This option only makes sense for Arduino Mega because the Atmega2560 and 1280
221 * have 4 hardware UARTs. By using other UART than the UART0 you can use this debugger
222 * together with the Arduino Serial functions.
223 * For Arduino boards based on ATmega328 (like Uno, Nano,...) the only valid
224 * option is 0 (to use UART0) because there are no other hardware UARTs.
225 *
226 * Supported values for Atmega328 (UNO):
227 * 0 - use USART0 (Arduino Serial cannot be used with the debugger)
228 *
229 * Supported values for ATmega1284(P):
230 * 0 - use USART0
231 * 1 - use USART1
232 *
233 * Supported values for Atmega2560 and Atmega1280:
234 * 0 - use USART0 (Arduino Serial cannot be used with the debugger)
235 * 1 - use USART1 (Rx=PD2=D19, Tx=PD3=D18)
236 * 2 - use USART2 (Rx=PH0=D17, Tx=PH1=D16)
237 * 3 - use USART3 (Rx=PJ0=D15, Tx=PJ1=D14)
238 *
239 * Note: the number in AVR8_UART_NUMBER is used to generate register names in macros;
240 * don't put it into parentheses.
241 */
242#ifndef AVR8_UART_NUMBER
243 #define AVR8_UART_NUMBER 0
244#endif
245
246/**
247 * Maximum number of breakpoints supported.
248 * Note that gdb will set temporary breakpoint, for example, for step into function
249 * or the Run to line command, so the actual number of breakpoints user can set will
250 * be lower.
251 * For flash breakpoints it is better to use lower number - each breakpoint means
252 * writing to flash, user will often forget about old breakpoints and these will be
253 * rewritten to flash with no need...So it is better to limit the number and make
254 * the user delete breakpoints when not needed. It is quite possible to debug
255 * a program with single breakpoint.
256 */
257#if (AVR8_BREAKPOINT_MODE == 1) /* RAM only breakpoints; recommended 8 breakpoints */
258 #define AVR8_MAX_BREAKS (8)
259#else /* Flash breakpoints, RAM stepping; recommended 4 breakpoints */
260 #define AVR8_MAX_BREAKS (4)
261#endif
262
263
264/**
265 * Define this to enable some global variables to make it easier to debug this stub.
266 * This is for advanced users who need to debug the debugger (gbd stub) itself.
267 */
268/* #define AVR8_STUB_DEBUG */
269
270
271
272
273/**
274 * Initialize the debugger driver
275 * ? Is this our version of set_debug_traps ? (see above)
276 * But the interrupts are re-directed in compile time, so we do not need such function.
277 */
278void debug_init(void);
279
280/**
281 * Insert breakpoint at the position where this function is called.
282 * To set-up breakpoints before compilation.
283*/
284void breakpoint(void);
285
286
287/**
288 * Send text message to gdb console.
289 * The function appends "\n" to the msg.
290 * Note that GDB will queue the messages until "\n" (0x0a) is received; then
291 * it displays the messages in console.
292 */
293void debug_message(const char* msg);
294
295
296
297#ifdef __cplusplus
298}
299#endif
300
301
302#endif /* DRV_DEBUG_H_ */
void debug_message(const char *msg)
void debug_init(void)
void breakpoint(void)