ataradov / free-dap

Free and open implementation of the CMSIS-DAP debugger firmware
BSD 3-Clause "New" or "Revised" License
261 stars 62 forks source link

add dap_init() at beginning of main() for same70 #18

Closed johnldean closed 3 years ago

johnldean commented 3 years ago

Seems that not having this here was a bug. I was able to use free-dap to program a samd21 with the same70 without any changes (besides pin configs) a few months ago when I was testing it with the same70 explained dev board, but recently when trying to use a same70 on a custom board, I ran into issues where it would hit the dummy handler if I try to connect to a target device when no target is connected.

#0  irq_handler_dummy () at ../startup_same70.c:238
#1  <signal handler called>
#2  0x00000000 in ?? ()
#3  0x004011ae in dap_swj_sequence () at ../../../dap.c:1163
#4  0x0040177a in dap_process_request (req=<optimized out>, req_size=req_size@entry=1024, resp=<optimized out>, 
    resp_size=resp_size@entry=1024) at ../../../dap.c:1433
#5  0x004003e0 in dap_task () at ../main.c:242
#6  main () at ../main.c:291
(gdb) 

looks like the line dap_swd_write(dap_req_get_byte(), sz); segfaults, because dap_swd_write was uninitialized.

How this ever worked for me a few months ago, I'm not sure. It might be that when a target board is properly connected, a different code path executes where dap_init is called before dap_swd_write. In my recent testing, I didn't actually have a target connected. Adding dap_init() to main fixes this issue.

johnldean commented 3 years ago

Actually, I was looking back at some old comment threads from when I was doing my initial testing on this, it looks like the issue doesn't occur when using the most recent edbg build, but if I used and older version of edbg that I had forked, I had issues. In any case though, initializing at the beginning doesn't sound like a bad thing, unless I am missing something,