Debugging tool for mbed enabled microcontrollers, especially for NUCLEO-F303RE and STM32F042F6P6.
Diff: Debug.h
- Revision:
- 0:e36b454cc2e6
- Child:
- 1:dbb9fcc20d07
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Debug.h Mon May 06 00:01:17 2019 +0000 @@ -0,0 +1,365 @@ +#pragma once + +// include files +//------------------------------------------------------------------------------------------------------------------ +#include "mbed.h" + +// macros +//------------------------------------------------------------------------------------------------------------------ +#define name(var) #var + + +// assembly functions +//------------------------------------------------------------------------------------------------------------------ +extern "C" int check_1_bit(uint32_t gpiox, uint32_t offset, uint32_t pin); +extern "C" int check_2_bit(uint32_t gpiox, uint32_t offset, uint32_t pin); +extern "C" int check_alternative_mode(uint32_t gpiox, uint32_t offset, uint32_t pin); +extern "C" int read_word(uint32_t address, uint32_t offset); + +// structs +//------------------------------------------------------------------------------------------------------------------ +typedef struct Pin { + char port; + int number; +} pin_t; + + +/** Debug_serial class. + * Class for stepping programme and printing actual position of the running programme with optional print of one variable (int, float, char or char*). + * Functions printf, putc and getc are also defined in the class. + * + * Example program: + * @code + * // ---------------------------------------------------------------------------- + * // Author: Lukas Bielesch + * // Department of Measurement, Czech technical university in Prague, Czech Republic + * // Date of publication: 15. Apr 2019 + * // ---------------------------------------------------------------------------- + * #include "Debug.h" + * AnalogIn analog(PA_5); + * PwmOut pwm(PA_6); + * DigitalOut out(PA_4); + * Debug_serial pc(PA_2, PA_3, 115200); // + * + * int main(){ + * int var = 0; + * char character; + * out = 1; + * float pi = 3.14159265359; + * pc.breakpoint(__LINE__,name(pi),pi); + * char* arr = "this is string"; + * pc.breakpoint(__LINE__,name(arr),arr); + * pwm = 0.5; + * pwm.period(1); + * while(1){ + * pc.breakpoint(__LINE__,name(var),var); + * pc.printf("insert character\n\r"); + * character = pc.getc(); + * pc.printf("you have inserted %c\n\r",character); + * var++; + * wait(1); + * } + * } + * @endcode + */ +//------------------------------------------------------------------------------------------------------------------ +class Debug_serial { +public: + + /** Create object of class Debug_serial + * @param tx_pin TX pin of serial port of the board + * @param rx_pin RX pin of serial port of the board + * @param baudrate desired baudrate value of debug serial port + */ + Debug_serial(PinName tx_pin, PinName rx_pin, int baudrate = 115200); + + /** Perform one breakpoint without printing variable + * @param line_number line number of the breakpoint + */ + void breakpoint(int line_number = -1); + + /** Perform one breakpoint and print variable of type int + * @param line_number Line number of the breakpoint + * @param name name of printed variable(max length is 19) + * @param variable + */ + void breakpoint(int line_number, char name[20], int variable); + + /** Perform one breakpoint and print variable of type char + * @param line_number Line number of the breakpoint + * @param name name of printed variable(max length is 19) + * @param variable + */ + void breakpoint(int line_number, char name[20], char variable); + + /** Perform one breakpoint and print variable of type string + * @param line_number Line number of the breakpoint + * @param name name of printed variable(max length is 19) + * @param variable + */ + void breakpoint(int line_number, char name[20], char * variable); + + /** Perform one breakpoint and print variable of type float + * @param line_number Line number of the breakpoint + * @param name name of printed variable(max length is 19) + * @param variable + */ + void breakpoint(int line_number, char name[20], float variable); + + /** Print formatted string to debug serial port + * @param string + * @param format (optional) + * @returns total number of printed characters or negative value if an output error or an encoding error + */ + int printf(const char* format, ...); + + /** Print one character to debug serial port + * @param character + * @returns character written as an unsigned char cast to an int + */ + int putc(int character); + + /** Read one character from debug serial port + * @returns character written as an unsigned char cast to an int + */ + int getc(); +private: + int break_line[3]; //store number of lines of three previous breakpoints + char var[3][50]; //store variables of three previous breakpoints + +protected: +// objects: + Serial pc; //debug serial device +// variables: + int breakpoint_count; //stores number of the current breakpoint +// functions + // initialization function + void init(); + // print 3 last breakpoints + void print_3_breaks(int line_number); + // print one breakpoint + void print_one_break(int n); + // clear screen from m line up to n line + void clear_from_n_up_to_m(int m, int n); +}; + + + + +/** Debug_led class. + * Class for stepping the program with debug LED and button, that is connected to GND(default) or VCC. + * + * Example program: + * @code + * // ---------------------------------------------------------------------------- + * // Author: Lukas Bielesch + * // Department of Measurement, Czech technical university in Prague, Czech Republic + * // Date of publication: 15. Apr 2019 + * // ---------------------------------------------------------------------------- + * #include "Debug.h" + * AnalogIn analog(PA_3); + * PwmOut pwm(PA_4); + * DigitalOut out(PA_2); + * Debug_led deb(PA_5, PA_6, "BUTTON_VDD"); //debug led on PA5, debug button connected to VDD on PA6 + * int main(){ + * + * out = 1; + * deb.breakpoint(1); + * pwm = 0.5; + * pwm.period(1); + * deb.breakpoint(2); + * + * while(1){ + * deb.breakpoint(); + * pwm = pwm + 0.1; + * wait(2); + * } + * } + * @endcode + */ +// class Debug_led +//------------------------------------------------------------------------------------------------------------------ +class Debug_led { +public: + + /** Create object of class Debug_led + * @param led_pin pin of of debug led + * @param button_pin pin of of debug button + * @param mode mode of button connection("BUTTON_GND", "BUTTON_VCC", "BUTTON_VDD") + */ + Debug_led(PinName led_pin, PinName button_pin, char mode[11] = "BUTTON_GND"); + + /** Perform one breakpoint + * @param number number of flashes of LED during the breakpoint(optional) + */ + void breakpoint(int number = -1); + +private: +// objects + DigitalOut led; //debug led + InterruptIn button; //debug button +// variables + int button_mode; //mode of button 1->pullupt, 0->pulldown + volatile bool end_breakpoint; + int number_of_breakpoints; + + /** Initialization */ + void init(char mode[11]); + + /** Blinks the debug led n-times with blink period wait_time_ms */ + void flash_n_times(int wait_time_ms, int n); + + /** end the break after the button is pushed */ + void end_break(); + + +}; + + +/** Debug_register class. + * + * Example program: + * @code + * //------------------------------------------------------------------------------------------------------------------ + * // ---------------------------------------------------------------------------- + * // Author: Lukas Bielesch + * // Department of Measurement, Czech technical university in Prague, Czech Republic + * // Date of publication: 15. Apr 2019 + * // ---------------------------------------------------------------------------- + * #include "Debug.h" + * AnalogIn analog(PA_5); + * PwmOut pwm(PA_6); + * DigitalOut out(PA_4); + * Debug_register pc(PA_2, PA_3, 115200); + * + * int main(){ + * pc.breakpoint(__LINE__,0x48000000); + * DigitalOut out2 (PA_0); + * pc.breakpoint(__LINE__,0x48000000); + * AnalogIn analog2 (PA_1); + * pc.breakpoint(__LINE__,0x48000000); + * + * while(1){ + * if(pc.readable()){ + * pc.putc(pc.getc()); + * } + * wait(0.1); + * } + * } + * @endcode + */ +class Debug_register { +public: + + /** Create object of class Debug_serial + * @param tx_pin TX pin of serial port of the board + * @param rx_pin RX pin of serial port of the board + * @param baudrate desired baudrate value of debug serial port + */ + Debug_register(PinName tx_pin, PinName rx_pin, int baudrate = 115200); + + /** Perform one breakpoint and print one register + * @param line_number line number of the breakpoint + * @param address + * @param offset + */ + void breakpoint(int line_number = -1, uint32_t address = 0, uint32_t offset = 0); + + + + /** Print formatted string to debug serial port + * @param string + * @param format (optional) + * @returns total number of printed characters or negative value if an output error or an encoding error + */ + int printf(const char* format, ...); + + /** Print one character to debug serial port + * @param character + * @returns character written as an unsigned char cast to an int + */ + int putc(int character); + + /** Read one character from debug serial port + * @returns character written as an unsigned char cast to an int + */ + int getc(); + + bool readable(); + +protected: +// objects: + Serial pc; //debug serial device +// variables: + int breakpoint_count; //stores number of the current breakpoint +// functions + // initialization function + void init(); + // clear screen from m line up to n line + void clear_from_n_up_to_m(int m, int n); +}; +/** Debug_register_print class. + * + * Example program: + * @code + * //------------------------------------------------------------------------------------------------------------------ + * // ---------------------------------------------------------------------------- + * // Author: Lukas Bielesch + * // Department of Measurement, Czech technical university in Prague, Czech Republic + * // Date of publication: 15. Apr 2019 + * // ---------------------------------------------------------------------------- + * #include "Debug.h" + * AnalogIn analog(PA_5); + * PwmOut pwm(PA_6); + * DigitalOut out(PA_4); + * Debug_register_print pc(PA_2, PA_3, 115200); + * + * int main(){ + * pc.format(2,2,1,3);//breakpoint count,line number, address, value + * pc.breakpoint(__LINE__,0x48000000); + * DigitalOut out2 (PA_0); + * pc.breakpoint(__LINE__,0x48000000); + * AnalogIn analog2 (PA_1); + * pc.breakpoint(__LINE__,0x48000000); + * + * while(1){ + * wait(1); + * } + * } + * @endcode + */ +class Debug_register_print { +public: + + /** Create object of class Debug_serial + * @param tx_pin TX pin of serial port of the board + * @param rx_pin RX pin of serial port of the board + * @param baudrate desired baudrate value of debug serial port + */ + Debug_register_print(PinName tx_pin, PinName rx_pin, int baudrate = 115200); + + + void format(int break_number = 2, int line = 2, int address = 1, int value = 1); + + /** Perform one breakpoint and print one register + * @param line_number line number of the breakpoint + * @param address + * @param offset + */ + void breakpoint(int line_number = -1, uint32_t address = 0, uint32_t offset = 0); + + +protected: +// objects: + Serial pc; //debug serial device +// variables: + int breakpoint_count; //stores number of the current breakpoint + int count_format; + int line_format; + int address_format; + int register_format; +// functions + // initialization function + void init(); + +}; \ No newline at end of file