There are many ways to debug code:

- In circuit debugger: Use the ICSD/ICSP port, a debugger, and the appropriate host software. Lets you step through, watch memory, set breakpoints. This probably does not work well when the USB module is enabled, since its operations are time-sensitive.
- "printf" debugging: printf() statements in code are transmitted over TTL serial line (described below)
- Report memory contents in OSC messages
- Write status of some register in question to an output pin and monitor that with a logic probe / scope / LED, etc.
- For low-level USB problems, use a hardware bus analyzer.

Printf-debugging has gotten me through most problems. I also use a bus analyzer for very-hard-to-solve USB issues.

# Printf Debugging in uOSC

1. Obtain a serial port with TTL capability (or an TTL to RS232 conversion circuit is needed). I use the [http://www.acroname.com/robotics/parts/S22-USB-SERIAL-INT-CONN.html|USB-Serial Interface Conn V2.0]. Then wire the /tx pin (also, /rc/6 on the PIC) to the input pin of the serial adapter (pictured).

[inline-center:1]

2. Enable the DEBUG macro in the code and recompile. Uncomment "#define DEBUG 1" at the top of user/user.h. Flash the new firmware onto the device.

3. Monitor the serial port with the catserial.py utility:

python ./py/catserial.py /dev/tty.usbserialXXXXXXX

4. Reset the device; some stuff should print out, e.g.:

----- Serial debugging enabled
... (etc)

5. Use special macros to setup debugging messages and checks in your code.

- ASSERT(statement, message) checks that statement evaluates to TRUE, else prints the message and corresponding filename / line number.
- PDEBUG(message) prints a message to the serial port.
- PDEBUG1(message, arg1) prints a message with a single extra argument interpolated (like printf)
- PDEBUG2(message, arg1, arg2); PDEBUG3(message, arg1, arg2, arg3); See PDEBUG1.
- PERROR(message); print message along with file and line number information.

When DEBUG is not defined, all these macros collapse, so that whatever code is contained therein does not take up program memory. Use macros #ifdef DEBUG / #endif to enclose larger code blocks for debugging.

Attachments