with Dr Jasmine Banks Debugging embedded systems CRICOS No.00213J CAB202 Microprocessors and Digital Systems CAB202 Microprocessors and Digital Systems School of Electrical Engineering and Robotics, Faculty of Engineering Introduction • Debugging embedded systems • Toggling pins • Serial communications • Using stdio on AVR microcontrollers • Assembly listing CRICOS No.00213J CAB202 Microprocessors and Digital Systems School of Electrical Engineering and Robotics, Faculty of Engineering Debugging embedded systems • We have a very limited view into the internal operation of a microcontroller • More sophisticated (and often expensive) debugging tools are available for (most) microcontrollers that let you interrogate the contents of memory as your programme is running • However it is common to not have access to these tools • It is important to develop strategies to be able to systematically debug embedded programmes when we only have access to basic I/O • Limited to microcontroller pins and interfaces CRICOS No.00213J CAB202 Microprocessors and Digital Systems School of Electrical Engineering and Robotics, Faculty of Engineering Toggling pins • Toggling a pin is a simple and effective way of getting a simple status indication from a programme running on a microcontroller • If connected to an LED provides an easy visual indicator • Can use more sophisticated instrumentation (e.g. an oscilloscope) to get more detailed timing information • Use cases: • Test whether a section of code is executing (e.g. interrupt) • Test the timing of a section of code (e.g. set a pin at start, clear at end) • Detect the occurrence of infrequent events (set pin on event, never clear) CRICOS No.00213J CAB202 Microprocessors and Digital Systems School of Electrical Engineering and Robotics, Faculty of Engineering Serial communications • If we need more detailed information that a single pin (or set of pins) can provide, use of serial communications (e.g. UART) is a useful next step • For simple cases, writing bytes directly to a UART might be good enough • If we want formatted strings, writing the infrastructure required to use printf/scanf might be worthwhile • Use cases: • Printing variable values to a serial console to check arithmetic, logic etc. • Getting user input for interactive debugging, entering test data etc. • Printing descriptive error messages in the case of unexpected behaviour. CRICOS No.00213J CAB202 Microprocessors and Digital Systems School of Electrical Engineering and Robotics, Faculty of Engineering Using stdio on AVR microcontrollers • For AVR microcontrollers there is a simple mechanism to rout stdin and stdout to/from any serial communications interface which can read and write characters (e.g. UART, SPI, I2C, etc.). • stdio.h must also be included • Step 1: Declare function prototypes for the read/write functions (can use a different name, but must have this signature) static int stdio_putchar(char c, FILE *stream); static int stdio_getchar(FILE *stream); • Step 2: Declare a stream to be used for stdin/stdout; the FDEV_SETUP_STREAM macro takes as arguments a reference to the functions declared in step 1. static FILE stdio = FDEV_SETUP_STREAM(stdio_putchar, stdio_getchar, _FDEV_SETUP_RW); CRICOS No.00213J CAB202 Microprocessors and Digital Systems School of Electrical Engineering and Robotics, Faculty of Engineering Using stdio on AVR microcontrollers • Step 3: Implement the functions prototyped in step 1 that read/write from the serial interface. The implementation will depend on the particular serial interface used. In this example we use some predefined functions for writing to/reading from the UART. static int stdio_putchar(char c, FILE *stream) { uart_putc(c); return c; //the putchar function must return the character written to the stream } static int stdio_getchar(FILE *stream) { return uart_getc(); } CRICOS No.00213J CAB202 Microprocessors and Digital Systems School of Electrical Engineering and Robotics, Faculty of Engineering Using stdio on AVR microcontrollers • Step 4: To link the stream we have created to stdin and stdout the following assignments must be made during the initialisation of your programme: void stdio_init(void) { // Assumes serial interface is initialised elsewhere stdout = &stdio; stdin = &stdio; } • Usage: Once steps 1-4 have been completed the functions defined in stdio.h (e.g. printf, scanf) can be used throughout your programme, and characters will be read/writing from the serial interface CRICOS No.00213J CAB202 Microprocessors and Digital Systems School of Electrical Engineering and Robotics, Faculty of Engineering Assembly listing • Some problems are impossible to identify and debug based on the C code alone • In these cases it may be necessary to inspect the assembly code which is generated by the compiler and linker from a C programme • For the gcc toolchain we can do this using the objdump utility, e.g.: avr-objdump -d firmware.elf • By inspecting the assembly directly we can identify problems that might be introduced due to optimisation, low level timing issues, pre-emption of critical code sections by ISRs etc., which are not visible at the C programme level. CRICOS No.00213J CAB202 Microprocessors and Digital Systems School of Electrical Engineering and Robotics, Faculty of Engineering Assembly listing: Example • For a standard PlatformIO installation avr-objdump is found at: C:\Users\<username>\.platformio\packages\toolchain-atmelavr\bin • The ELF file for your compiled programme is found at: <project dir>\.pio\build\QUTy\firmware.elf • Example output of avr-objdump -d firmware.elf: 0000010c <uart_getchar>: 10c: 80 91 2a 39 110: 88 23 112: e1 f3 114: e0 91 2a 39 118: e1 50 11a: e0 93 2a 39 11e: f0 e0 120: e6 5e 122: f7 4c 124: 80 81 126: 90 e0 128: 08 95 lds and breq lds subi sts ldi subi sbci ld ldi ret CRICOS No.00213J CAB202 Microprocessors and Digital Systems School of Electrical Engineering and Robotics, Faculty of Engineering r24, 0x392A ; 0x80392a <rxn> r24, r24 .-8 ; 0x10c <uart_getchar> r30, 0x392A ; 0x80392a <rxn> r30, 0x01 ; 1 0x392A, r30 ; 0x80392a <rxn> r31, 0x00 ; 0 r30, 0xE6 ; 230 r31, 0xC7 ; 199 r24, Z r25, 0x00 ; 0 Next week… Review CRICOS No.00213J CAB202 Microprocessors and Digital Systems School of Electrical Engineering and Robotics, Faculty of Engineering