felias-fogg / dw-link

An Arduino-based debugWIRE hardware-debugger
https://www.codeproject.com/Articles/5321801/Debugging-an-Arduino-project-with-GDB-on-Classic-A
GNU General Public License v3.0
55 stars 11 forks source link

Debugging hangs when calling function _delay_ms(N) #10

Closed grinchyk closed 1 year ago

grinchyk commented 1 year ago

Hello. I am Dmitry. I had a very pleasant time studying your dw-link in practice, but in the end I ran into an unsolvable problem for me.

I try to debug following simply code on target board UNO (AtMega328P) For debugging with dw-link I have built firmware according to default platformio.ini (from dw-link repository) avr-g++ -Og -g -fno-lto main.cpp -o firmware.elf -mmcu=atmega328p -v

Than I start debugging the target with avr-gdb. And the avr-gdb hangs on line with _delay_ms(10);
Unfortunately the same behavior is achieved in VSCode Platformio IDE too.

I have tried debug with simavr. Fortunately the simavr is able to simulate _delay_ms(N) simavr -m atmega328p -f16000000L -g1234 with simavr call _delay_ms(10) produce delay about two minutes... and program does not hang.

Also I test this code with MPLABX. I have my own PicKit4. Debugger successfully completed _delay_ms(10) works fine too.

Could you help me to undestand why _delay_ms(10); produce such behaviour with your dw-link. in background _delay_ms(10) uses assembler for delay.

Thank You!

Мy code is below.

`

define F_CPU 16000000UL

include <avr/io.h> //Defines pins, ports, etc

include <util/delay.h> //Functions to waste time

int main(void) { DDRB |= 0b00100000; //set direction while (1) { for (long i =0; i<1000000;){//wait 1 sec
i++; } //This works fine.... PORTB = 0b00100000; //Turn on UNO board LED

 _delay_ms(10);               //_delay_ms(10);  (  util/delay.h)   this leads dw-link to an infinite loop 
                                       // debug system hangs  
                                       // also seems impossible to restart it programmatically only with Power OFF

//------------------------------------------------------------------------------------------------------------------------

 for (long i =0; i<1000000;) {//wait 1 sec      
   i++; } 
 PORTB = 0b00000000;         //Turn off all

//_delay_ms(10);   

}
return 0; // This line is never reached } `