390 likes | 555 Views
Thank you for joining this Elektor Academy webinar. The session will start soon. Please note that the audio broadcast is switched off for now and will start at 15:00 BST (16:00 CEST ). Low-Cost Debugging.
E N D
Thank you for joining this Elektor Academy webinar. The session will start soon. Please note that the audio broadcast is switched off for now and will start at 15:00 BST (16:00 CEST)
Low-Cost Debugging for the poor who are not rich nor wealthy and who do not have a lot of money to spend, even on food www.elektor-labs.com/low-cost-debugging
Presented by Clemens Valens • CEO Elektor.Labs • Contributing editor mmmh! www.elektor-labs.com/low-cost-debugging
About Elektor Electronics magazine for people passionate about electronics • 5main editions: • English, Spanish, German, French & Dutch • We publish projects • We have our own lab • We design our own PCBs www.elektor-labs.com/low-cost-debugging
Elektor.Labs • Sharing electronics • Be an expert, get rewarded • Get more out of your passion www.elektor-labs.com/low-cost-debugging
MCU Debugging on a shoestring • Lots of cheap microcontroller boards are available • Hobbyists are no longer frowned upon by MCU manufacturers • Anyone can create embedded applications www.elektor-labs.com/low-cost-debugging
Programming means Debugging • Time-consuming • Good tools are expensive • Effective debugging requires skills www.elektor-labs.com/low-cost-debugging
Use Your Brain → www.elektor-labs.com/low-cost-debugging
Utopia • Programmers spend 90% of their time thinking, the remaining 10% is used for typing code • Debuggers spend 90% of their time thinking and only 10% is used for single-stepping through code Code for testing! www.elektor-labs.com/low-cost-debugging
Train Your Debug Muscle www.elektor-labs.com/low-cost-debugging
Enter the LED www.elektor-labs.com/low-cost-debugging
Using the LED • Make sure it works • Know which leg is the anode • Keep its brightness low • Use dedicated MCU pins • Write a test program to prove that you master the LED www.elektor-labs.com/low-cost-debugging
Use Macros (or Functions) Example: #define DEBUG_LED0_ON PORTB |= 0x01 #define DEBUG_LED0_OFF PORTB &= 0xfe (or something nicer but with a similar effect) Done debugging: #define DEBUG_LED0_ON /* PORTB |= 0x01 */ #define DEBUG_LED0_OFF /* PORTB &= 0xfe */ (or something nicer but with a similar effect) www.elektor-labs.com/low-cost-debugging
Using LEDs • Set LEDs to a known state at start-up • Use well-defined animations • Activate/deactivate at strategic positions • One debug function per LED per session • Only one well-defined execution path www.elektor-labs.com/low-cost-debugging
Blink from main() void main(void) { // Increment delta_time by timer; delta_time = 0ms; DEBUG_LED_OFF; while (1) { if (delta_time==250ms) DEBUG_LED_ON; else if (delta_time==500ms) { DEBUG_LED_OFF; delta_time = 0ms; } } } www.elektor-labs.com/low-cost-debugging
Don’t Blink from ISRs Interrupts may continue to tick even if the main program crashed. Exception: when testing ISR functionality. www.elektor-labs.com/low-cost-debugging
LEDs are Really Fast • They can reveal activity on (serial) ports • Brightness is a measure for duty-cycle www.elektor-labs.com/low-cost-debugging
Build a Logic Level Probe www.elektor-labs.com/low-cost-debugging
No LEDs? • Use a logic level probe • Use a multimeter • Use an oscilloscope • Use your ears • Use your smartphone www.elektor-labs.com/low-cost-debugging
Serial Sound www.elektor-labs.com/low-cost-debugging
Demo! www.elektor-labs.com/low-cost-debugging
The Serial Port Use a solid connection, f.i. like this: www.elektor-labs.com/low-cost-debugging
The Serial Port • Create a working putch • Create a debug putch: • #define DEBUG_PUTCH putch • Only send printable ASCII characters • Exception: Bell (0x07) www.elektor-labs.com/low-cost-debugging
The Serial Port • Create a working puts • Create a debug puts: • #define DEBUG_PUTS puts • Use meaningful strings • Print program name & version www.elektor-labs.com/low-cost-debugging
The Serial Port • Keep debug strings short • Format cleverly • Use brackets: <>, (), [], {} • Prefer separators to whitespace • Use CR/LF • Lower-case is easier to read • ‘B8’ looks like ’88’, write ‘b8’ instead www.elektor-labs.com/low-cost-debugging
Following the Call Stack • Be verbose • Print real function names • Print at function entry and exit • Cover all exit points www.elektor-labs.com/low-cost-debugging
Following Execution Paths • Be concise • Use single characters like numbers or letters • Increase character along path • Cover all paths www.elektor-labs.com/low-cost-debugging
Example int16_t foo(int8_t value) { DEBUG_PUTCH('0'); if (value<0) { DEBUG_PUTCH('1'); if (value<-5) { DEBUG_PUTCH('2'); } else if (value<-3) { DEBUG_PUTCH('3'); } DEBUG_PUTCH('4'); } DEBUG_PUTCH('5'); return -10*value; } www.elektor-labs.com/low-cost-debugging
printf • Comfortable • Needs a lot of memory • Slow • Partly implemented • Platform dependent • Undefined behavior www.elektor-labs.com/low-cost-debugging
printf Coprocessor www.elektor-labs.com/low-cost-debugging
MCU: void my_printf(intint_count, char *format, ...) { va_listap; va_start(ap,format); puts("printf"); // Send keyword word. puts(format); // Send format string. putch('\0'); // Terminate format string. putch(sizeof(int)); // Send size of int. putch(int_count); // Send number of integers. while (int_count>0) // Send the arguments. { send_int(va_arg(ap,int)); int_count -= 1; } va_end(ap); } my_printf(3,"u=%05u, p=%p, c=%c\r\n",234,0x4321,'$'); www.elektor-labs.com/low-cost-debugging
Coprocessor: void main(void) { while (serial_data_available==true) { uint8_t ch= read_serial_input(); if (keyword_found(ch)==false) { write_serial_output(ch); } else { if (printf_statement_complete==true) { printf(received_format_string,received_arguments); } } } } www.elektor-labs.com/low-cost-debugging
Complications • How to handle fast data streams? • How to handle errors? As always: use with care www.elektor-labs.com/low-cost-debugging
One Step Beyond • Build a €$£10,- debug coprocessor: • Use a proven platform • Use known good libraries • Debug communications (SPI, I²C, other) • Provide stimuli • Measure voltages www.elektor-labs.com/low-cost-debugging
Debug Coprocessor www.elektor-labs.com/low-cost-debugging
The Next Level • JTAG pod • In-circuit debugger (ICD) • In-circuit emulator (ICE) www.elektor-labs.com/low-cost-debugging
Any questions? www.elektor-labs.com/low-cost-debugging
Next Elektor Webinar Automation and Test using Flowcodepresented by Ben RowlandandJonathan Woodrow Thursday November 21, 2013 (15:00 GMT / 16:00 CET) Check www.elektor.com/webinarfor details or subscribe to our newsletter www.elektor.com www.elektor-labs.com/low-cost-debugging
Code available at www.elektor-labs.com/low-cost-debugging Thank you for attending! www.elektor-labs.com/low-cost-debugging