square / pylink

Python Library for device debugging/programming via J-Link
https://pylink.readthedocs.io/en/latest/
Other
344 stars 127 forks source link

SWO output only works in debug mode? #81

Open Benjoyo opened 3 years ago

Benjoyo commented 3 years ago

I am trying out the Serial Wire Viewer example: serial-wire-viewer

While it does work, it only seems to work when I put the device in debug mode from the SES IDE. Why is that? Can I do the same thing via pylink, so that the tool runs on its own?

Thanks

hkpeprah commented 3 years ago

It could be possible that the IDE is writing to the registers necessary to configure SWO on your behalf. The example above doesn't do that on your behalf. An example of a firmware image that does do that is here: https://github.com/square/pylink/blob/master/tests/functional/firmware/k21-swo/src/swo.c#L75

While I haven't tried it myself, I think you should be able to write to those registers on the host-side, which would enable SWO then allow you to start collecting.

Benjoyo commented 3 years ago

@hkpeprah Thank you for your reply. I tried the linked code to enable SWO, but it didn't change anything. It seems like the configuration doesn't have any effect, e.g. if I look at ITM_LAR before and after setting it, it is always zero. I also tried CMSIS, same thing. Is there anything else one needs to do first? I am using an nrf52840 DK, which also has a Cortex M4.

hkpeprah commented 3 years ago

Could this be of use? Namely the bit about the preprocessor defines in system_nrf52.c to enable SWO and trace. It might require re-compiling your application with the defines set. Alternatively, converting those statements to register / memory read/writes. https://devzone.nordicsemi.com/nordic/nordic-blog/b/blog/posts/debugging-nrf52-series-devices-using-trace

boraozgen commented 2 years ago

@Benjoyo were you able to solve this?

I am also struggling to get SWO working. It works if I run the JLinkSWOViewer tool before the example, otherwise it does not work. I wonder what the tool might be doing additionally.

boraozgen commented 2 years ago

After some trial and error, I found the missing step. My firmware was configuring everything but the SWO clock speed, which is done using the TPIU_ACPR register. It works when I set the correct prescaler value using the memory_write32 command. This is probably done automatically by the J-Link SWO tool.