Debugging tool for mbed enabled microcontrollers, especially for NUCLEO-F303RE and STM32F042F6P6.
Debug.h@19:b355ad429450, 2021-03-20 (annotated)
- Committer:
- bieleluk
- Date:
- Sat Mar 20 12:55:32 2021 +0000
- Revision:
- 19:b355ad429450
- Parent:
- 18:472b66aeb1f5
- Child:
- 20:0227bb22d7ce
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bieleluk | 0:e36b454cc2e6 | 1 | #pragma once |
bieleluk | 0:e36b454cc2e6 | 2 | |
bieleluk | 12:a8ab6e018422 | 3 | // include libraries |
bieleluk | 0:e36b454cc2e6 | 4 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 5 | #include "mbed.h" |
bieleluk | 3:3d7837ae4a37 | 6 | #include <stdlib.h> |
bieleluk | 16:7fc964b6d127 | 7 | |
bieleluk | 3:3d7837ae4a37 | 8 | |
bieleluk | 0:e36b454cc2e6 | 9 | |
bieleluk | 0:e36b454cc2e6 | 10 | // macros |
bieleluk | 0:e36b454cc2e6 | 11 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 12 | #define name(var) #var |
bieleluk | 3:3d7837ae4a37 | 13 | #define min(a, b) (((a) < (b)) ? (a) : (b)) |
bieleluk | 3:3d7837ae4a37 | 14 | #define max(a, b) (((a) > (b)) ? (a) : (b)) |
bieleluk | 0:e36b454cc2e6 | 15 | |
bieleluk | 0:e36b454cc2e6 | 16 | |
bieleluk | 18:472b66aeb1f5 | 17 | typedef enum |
bieleluk | 18:472b66aeb1f5 | 18 | { |
bieleluk | 18:472b66aeb1f5 | 19 | GND = 0, |
bieleluk | 18:472b66aeb1f5 | 20 | BUTTON_GND = 0, |
bieleluk | 18:472b66aeb1f5 | 21 | VDD = 1, |
bieleluk | 18:472b66aeb1f5 | 22 | BUTTON_VDD = 1, |
bieleluk | 18:472b66aeb1f5 | 23 | VCC = 1, |
bieleluk | 18:472b66aeb1f5 | 24 | BUTTON_VCC = 1, |
bieleluk | 18:472b66aeb1f5 | 25 | } button_mode; |
bieleluk | 18:472b66aeb1f5 | 26 | |
bieleluk | 12:a8ab6e018422 | 27 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 28 | /** Debug_serial class. |
bieleluk | 0:e36b454cc2e6 | 29 | * Class for stepping programme and printing actual position of the running programme with optional print of one variable (int, float, char or char*). |
bieleluk | 0:e36b454cc2e6 | 30 | * Functions printf, putc and getc are also defined in the class. |
bieleluk | 0:e36b454cc2e6 | 31 | * |
bieleluk | 0:e36b454cc2e6 | 32 | * Example program: |
bieleluk | 0:e36b454cc2e6 | 33 | * @code |
bieleluk | 0:e36b454cc2e6 | 34 | * // ---------------------------------------------------------------------------- |
bieleluk | 0:e36b454cc2e6 | 35 | * // Author: Lukas Bielesch |
bieleluk | 0:e36b454cc2e6 | 36 | * // Department of Measurement, Czech technical university in Prague, Czech Republic |
bieleluk | 12:a8ab6e018422 | 37 | * // Date of publication: 13. May 2019 |
bieleluk | 0:e36b454cc2e6 | 38 | * // ---------------------------------------------------------------------------- |
bieleluk | 0:e36b454cc2e6 | 39 | * #include "Debug.h" |
bieleluk | 9:59344a584e18 | 40 | * AnalogIn analog(PA_4); |
bieleluk | 0:e36b454cc2e6 | 41 | * PwmOut pwm(PA_6); |
bieleluk | 9:59344a584e18 | 42 | * DigitalOut out(PA_5); |
bieleluk | 12:a8ab6e018422 | 43 | * Debug_serial pc(PA_2, PA_3, 115200); |
bieleluk | 0:e36b454cc2e6 | 44 | * |
bieleluk | 0:e36b454cc2e6 | 45 | * int main(){ |
bieleluk | 0:e36b454cc2e6 | 46 | * int var = 0; |
bieleluk | 0:e36b454cc2e6 | 47 | * char character; |
bieleluk | 0:e36b454cc2e6 | 48 | * out = 1; |
bieleluk | 0:e36b454cc2e6 | 49 | * float pi = 3.14159265359; |
bieleluk | 0:e36b454cc2e6 | 50 | * pc.breakpoint(__LINE__,name(pi),pi); |
bieleluk | 0:e36b454cc2e6 | 51 | * char* arr = "this is string"; |
bieleluk | 0:e36b454cc2e6 | 52 | * pc.breakpoint(__LINE__,name(arr),arr); |
bieleluk | 0:e36b454cc2e6 | 53 | * pwm = 0.5; |
bieleluk | 0:e36b454cc2e6 | 54 | * pwm.period(1); |
bieleluk | 0:e36b454cc2e6 | 55 | * while(1){ |
bieleluk | 0:e36b454cc2e6 | 56 | * pc.breakpoint(__LINE__,name(var),var); |
bieleluk | 0:e36b454cc2e6 | 57 | * pc.printf("insert character\n\r"); |
bieleluk | 2:478ba8b83e3f | 58 | * while (!pc.readable()){} |
bieleluk | 0:e36b454cc2e6 | 59 | * character = pc.getc(); |
bieleluk | 0:e36b454cc2e6 | 60 | * pc.printf("you have inserted %c\n\r",character); |
bieleluk | 0:e36b454cc2e6 | 61 | * var++; |
bieleluk | 1:dbb9fcc20d07 | 62 | * pc.breakpoint(__LINE__, 0x48000000); |
bieleluk | 0:e36b454cc2e6 | 63 | * wait(1); |
bieleluk | 0:e36b454cc2e6 | 64 | * } |
bieleluk | 0:e36b454cc2e6 | 65 | * } |
bieleluk | 0:e36b454cc2e6 | 66 | * @endcode |
bieleluk | 0:e36b454cc2e6 | 67 | */ |
bieleluk | 0:e36b454cc2e6 | 68 | class Debug_serial { |
bieleluk | 0:e36b454cc2e6 | 69 | public: |
bieleluk | 0:e36b454cc2e6 | 70 | |
bieleluk | 12:a8ab6e018422 | 71 | /** Create object of Debug_serial class |
bieleluk | 12:a8ab6e018422 | 72 | * @param tx_pin TX pin of debug serial port |
bieleluk | 12:a8ab6e018422 | 73 | * @param rx_pin RX pin of debug serial port |
bieleluk | 12:a8ab6e018422 | 74 | * @param baudrate(optional) desired baudrate value of debug serial port, default baudrate is 115200 Bd/s |
bieleluk | 0:e36b454cc2e6 | 75 | */ |
bieleluk | 0:e36b454cc2e6 | 76 | Debug_serial(PinName tx_pin, PinName rx_pin, int baudrate = 115200); |
bieleluk | 0:e36b454cc2e6 | 77 | |
bieleluk | 0:e36b454cc2e6 | 78 | /** Perform one breakpoint without printing variable |
bieleluk | 12:a8ab6e018422 | 79 | * @param line_number(optional) line number of breakpoint,macro __LINE__ is recommended |
bieleluk | 0:e36b454cc2e6 | 80 | */ |
bieleluk | 0:e36b454cc2e6 | 81 | void breakpoint(int line_number = -1); |
bieleluk | 0:e36b454cc2e6 | 82 | |
bieleluk | 0:e36b454cc2e6 | 83 | /** Perform one breakpoint and print variable of type int |
bieleluk | 12:a8ab6e018422 | 84 | * @param line_number line number of breakpoint, macro __LINE__ is recommended |
bieleluk | 12:a8ab6e018422 | 85 | * @param name name of printed variable(max length is 19) , macro name(variable) is recommended |
bieleluk | 12:a8ab6e018422 | 86 | * @param variable variable of type int |
bieleluk | 0:e36b454cc2e6 | 87 | */ |
bieleluk | 0:e36b454cc2e6 | 88 | void breakpoint(int line_number, char name[20], int variable); |
bieleluk | 0:e36b454cc2e6 | 89 | |
bieleluk | 0:e36b454cc2e6 | 90 | /** Perform one breakpoint and print variable of type char |
bieleluk | 12:a8ab6e018422 | 91 | * @param line_number line number of breakpoint, macro __LINE__ is recommended |
bieleluk | 12:a8ab6e018422 | 92 | * @param name name of printed variable(max length is 19) , macro name(variable) is recommended |
bieleluk | 12:a8ab6e018422 | 93 | * @param variable variable of type int |
bieleluk | 0:e36b454cc2e6 | 94 | */ |
bieleluk | 0:e36b454cc2e6 | 95 | void breakpoint(int line_number, char name[20], char variable); |
bieleluk | 0:e36b454cc2e6 | 96 | |
bieleluk | 12:a8ab6e018422 | 97 | /** Perform one breakpoint and print string variable |
bieleluk | 12:a8ab6e018422 | 98 | * @param line_number line number of breakpoint, macro __LINE__ is recommended |
bieleluk | 12:a8ab6e018422 | 99 | * @param name name of printed variable(max length is 19) , macro name(variable) is recommended |
bieleluk | 12:a8ab6e018422 | 100 | * @param variable variable of type char* |
bieleluk | 0:e36b454cc2e6 | 101 | */ |
bieleluk | 0:e36b454cc2e6 | 102 | void breakpoint(int line_number, char name[20], char * variable); |
bieleluk | 0:e36b454cc2e6 | 103 | |
bieleluk | 0:e36b454cc2e6 | 104 | /** Perform one breakpoint and print variable of type float |
bieleluk | 12:a8ab6e018422 | 105 | * @param line_number line number of breakpoint, macro __LINE__ is recommended |
bieleluk | 12:a8ab6e018422 | 106 | * @param name name of printed variable(max length is 19) , macro name(variable) is recommended |
bieleluk | 12:a8ab6e018422 | 107 | * @param variable variable of type float |
bieleluk | 0:e36b454cc2e6 | 108 | */ |
bieleluk | 0:e36b454cc2e6 | 109 | void breakpoint(int line_number, char name[20], float variable); |
bieleluk | 0:e36b454cc2e6 | 110 | |
bieleluk | 1:dbb9fcc20d07 | 111 | /** Perform one breakpoint and print one register value |
bieleluk | 12:a8ab6e018422 | 112 | * @param line_number line number of breakpoint, macro __LINE__ is recommended |
bieleluk | 12:a8ab6e018422 | 113 | * @param address address of register, must be divisible by 4 |
bieleluk | 1:dbb9fcc20d07 | 114 | */ |
bieleluk | 6:1ee26b7b9c2f | 115 | void breakpoint(int line_number, uint32_t address); |
bieleluk | 1:dbb9fcc20d07 | 116 | |
bieleluk | 0:e36b454cc2e6 | 117 | /** Print formatted string to debug serial port |
bieleluk | 0:e36b454cc2e6 | 118 | * @returns total number of printed characters or negative value if an output error or an encoding error |
bieleluk | 0:e36b454cc2e6 | 119 | */ |
bieleluk | 0:e36b454cc2e6 | 120 | int printf(const char* format, ...); |
bieleluk | 12:a8ab6e018422 | 121 | |
bieleluk | 12:a8ab6e018422 | 122 | /** Read formatted string from debug serial port |
bieleluk | 12:a8ab6e018422 | 123 | * @returns total number of successfully read arguments or negative value if en error occured |
bieleluk | 12:a8ab6e018422 | 124 | */ |
bieleluk | 12:a8ab6e018422 | 125 | int scanf(const char* format, ...); |
bieleluk | 0:e36b454cc2e6 | 126 | |
bieleluk | 0:e36b454cc2e6 | 127 | /** Print one character to debug serial port |
bieleluk | 0:e36b454cc2e6 | 128 | * @param character |
bieleluk | 0:e36b454cc2e6 | 129 | * @returns character written as an unsigned char cast to an int |
bieleluk | 0:e36b454cc2e6 | 130 | */ |
bieleluk | 0:e36b454cc2e6 | 131 | int putc(int character); |
bieleluk | 0:e36b454cc2e6 | 132 | |
bieleluk | 0:e36b454cc2e6 | 133 | /** Read one character from debug serial port |
bieleluk | 0:e36b454cc2e6 | 134 | * @returns character written as an unsigned char cast to an int |
bieleluk | 0:e36b454cc2e6 | 135 | */ |
bieleluk | 0:e36b454cc2e6 | 136 | int getc(); |
bieleluk | 2:478ba8b83e3f | 137 | |
bieleluk | 12:a8ab6e018422 | 138 | /** Check whether there is some character available to read from debug serial port. |
bieleluk | 12:a8ab6e018422 | 139 | * @returns true when there is something available to read |
bieleluk | 12:a8ab6e018422 | 140 | */ |
bieleluk | 2:478ba8b83e3f | 141 | bool readable(); |
bieleluk | 12:a8ab6e018422 | 142 | |
bieleluk | 12:a8ab6e018422 | 143 | /** Check whether it is possible to write a character to debug serial port. |
bieleluk | 12:a8ab6e018422 | 144 | * @returns true when it is possible to write a character to debug serial port |
bieleluk | 12:a8ab6e018422 | 145 | */ |
bieleluk | 2:478ba8b83e3f | 146 | bool writable(); |
bieleluk | 2:478ba8b83e3f | 147 | |
bieleluk | 0:e36b454cc2e6 | 148 | private: |
bieleluk | 0:e36b454cc2e6 | 149 | int break_line[3]; //store number of lines of three previous breakpoints |
bieleluk | 0:e36b454cc2e6 | 150 | char var[3][50]; //store variables of three previous breakpoints |
bieleluk | 0:e36b454cc2e6 | 151 | |
bieleluk | 0:e36b454cc2e6 | 152 | protected: |
bieleluk | 0:e36b454cc2e6 | 153 | // objects: |
bieleluk | 12:a8ab6e018422 | 154 | Serial pc; //debug serial port |
bieleluk | 12:a8ab6e018422 | 155 | // variables |
bieleluk | 0:e36b454cc2e6 | 156 | int breakpoint_count; //stores number of the current breakpoint |
bieleluk | 0:e36b454cc2e6 | 157 | // functions |
bieleluk | 0:e36b454cc2e6 | 158 | // initialization function |
bieleluk | 0:e36b454cc2e6 | 159 | void init(); |
bieleluk | 0:e36b454cc2e6 | 160 | // print 3 last breakpoints |
bieleluk | 0:e36b454cc2e6 | 161 | void print_3_breaks(int line_number); |
bieleluk | 0:e36b454cc2e6 | 162 | // print one breakpoint |
bieleluk | 0:e36b454cc2e6 | 163 | void print_one_break(int n); |
bieleluk | 0:e36b454cc2e6 | 164 | // clear screen from m line up to n line |
bieleluk | 0:e36b454cc2e6 | 165 | void clear_from_n_up_to_m(int m, int n); |
bieleluk | 0:e36b454cc2e6 | 166 | }; |
bieleluk | 0:e36b454cc2e6 | 167 | |
bieleluk | 12:a8ab6e018422 | 168 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 169 | |
bieleluk | 0:e36b454cc2e6 | 170 | /** Debug_led class. |
bieleluk | 0:e36b454cc2e6 | 171 | * Class for stepping the program with debug LED and button, that is connected to GND(default) or VCC. |
bieleluk | 0:e36b454cc2e6 | 172 | * |
bieleluk | 19:b355ad429450 | 173 | * Example program for STM32F042F6P6: |
bieleluk | 0:e36b454cc2e6 | 174 | * @code |
bieleluk | 0:e36b454cc2e6 | 175 | * // ---------------------------------------------------------------------------- |
bieleluk | 18:472b66aeb1f5 | 176 | * // Example program of Debug_led class for STM32F042F6P6 |
bieleluk | 0:e36b454cc2e6 | 177 | * // Author: Lukas Bielesch |
bieleluk | 0:e36b454cc2e6 | 178 | * // Department of Measurement, Czech technical university in Prague, Czech Republic |
bieleluk | 18:472b66aeb1f5 | 179 | * // Date of publication: 20. March 2021 |
bieleluk | 0:e36b454cc2e6 | 180 | * // ---------------------------------------------------------------------------- |
bieleluk | 18:472b66aeb1f5 | 181 | * #include "mbed.h" |
bieleluk | 0:e36b454cc2e6 | 182 | * #include "Debug.h" |
bieleluk | 18:472b66aeb1f5 | 183 | * |
bieleluk | 18:472b66aeb1f5 | 184 | * PwmOut pwm(PA_4); |
bieleluk | 18:472b66aeb1f5 | 185 | * DigitalOut led(PA_1); |
bieleluk | 18:472b66aeb1f5 | 186 | * Debug_led deb(PA_5, PA_7, GND); //debug led on PA_5, debug button connected to ground on PA_7 |
bieleluk | 18:472b66aeb1f5 | 187 | * |
bieleluk | 0:e36b454cc2e6 | 188 | * int main(){ |
bieleluk | 0:e36b454cc2e6 | 189 | * |
bieleluk | 19:b355ad429450 | 190 | * led = 1; |
bieleluk | 18:472b66aeb1f5 | 191 | * deb.breakpoint(); // breakpoint with periodical flashing with 300 ms period until the button is pressed |
bieleluk | 0:e36b454cc2e6 | 192 | * pwm = 0.5; |
bieleluk | 0:e36b454cc2e6 | 193 | * pwm.period(1); |
bieleluk | 18:472b66aeb1f5 | 194 | * deb.breakpoint(2, 200); // breakpoint with periodical double-flashing with 200 ms period |
bieleluk | 0:e36b454cc2e6 | 195 | * |
bieleluk | 0:e36b454cc2e6 | 196 | * while(1){ |
bieleluk | 18:472b66aeb1f5 | 197 | * deb.breakpoint(3); // breakpoint with periodical triple-flashing until the button is pressed |
bieleluk | 17:5b0dd2a6b130 | 198 | * pwm = pwm + 0.1f; |
bieleluk | 18:472b66aeb1f5 | 199 | * |
bieleluk | 18:472b66aeb1f5 | 200 | * //init pwm on the pin without pwm functionality causes MbedOS Error |
bieleluk | 18:472b66aeb1f5 | 201 | * // error message is sent to uart on PA_2 and PA_3 |
bieleluk | 18:472b66aeb1f5 | 202 | * PwmOut pwm2(PA_0); |
bieleluk | 18:472b66aeb1f5 | 203 | * |
bieleluk | 0:e36b454cc2e6 | 204 | * wait(2); |
bieleluk | 0:e36b454cc2e6 | 205 | * } |
bieleluk | 0:e36b454cc2e6 | 206 | * } |
bieleluk | 18:472b66aeb1f5 | 207 | * |
bieleluk | 0:e36b454cc2e6 | 208 | * @endcode |
bieleluk | 0:e36b454cc2e6 | 209 | */ |
bieleluk | 0:e36b454cc2e6 | 210 | // class Debug_led |
bieleluk | 0:e36b454cc2e6 | 211 | class Debug_led { |
bieleluk | 0:e36b454cc2e6 | 212 | public: |
bieleluk | 0:e36b454cc2e6 | 213 | |
bieleluk | 0:e36b454cc2e6 | 214 | /** Create object of class Debug_led |
bieleluk | 0:e36b454cc2e6 | 215 | * @param led_pin pin of of debug led |
bieleluk | 0:e36b454cc2e6 | 216 | * @param button_pin pin of of debug button |
bieleluk | 18:472b66aeb1f5 | 217 | * @param mode (optional): mode of button connection(GND, VCC), default value value is GND |
bieleluk | 0:e36b454cc2e6 | 218 | */ |
bieleluk | 18:472b66aeb1f5 | 219 | Debug_led(PinName led_pin, PinName button_pin, button_mode mode = GND); |
bieleluk | 0:e36b454cc2e6 | 220 | |
bieleluk | 0:e36b454cc2e6 | 221 | /** Perform one breakpoint |
bieleluk | 18:472b66aeb1f5 | 222 | * @param number(optional): number of flashes of LED during the breakpoint, default value is 1 |
bieleluk | 18:472b66aeb1f5 | 223 | * @param period_ms(optional): period of flashing of the LED in ms, default value is 300ms |
bieleluk | 0:e36b454cc2e6 | 224 | */ |
bieleluk | 18:472b66aeb1f5 | 225 | void breakpoint(int number = 1, int period_ms = 300); |
bieleluk | 0:e36b454cc2e6 | 226 | |
bieleluk | 0:e36b454cc2e6 | 227 | private: |
bieleluk | 0:e36b454cc2e6 | 228 | // objects |
bieleluk | 0:e36b454cc2e6 | 229 | DigitalOut led; //debug led |
bieleluk | 0:e36b454cc2e6 | 230 | InterruptIn button; //debug button |
bieleluk | 0:e36b454cc2e6 | 231 | // variables |
bieleluk | 18:472b66aeb1f5 | 232 | int b_mode; // mode of button connection(GND = 0, VCC = 1) |
bieleluk | 12:a8ab6e018422 | 233 | volatile bool end_breakpoint; //true when button was pushed |
bieleluk | 18:472b66aeb1f5 | 234 | // int number_of_breakpoints; //number of the current breakpoint |
bieleluk | 0:e36b454cc2e6 | 235 | |
bieleluk | 0:e36b454cc2e6 | 236 | /** Initialization */ |
bieleluk | 18:472b66aeb1f5 | 237 | void init(button_mode mode); |
bieleluk | 0:e36b454cc2e6 | 238 | |
bieleluk | 12:a8ab6e018422 | 239 | /** Blink the debug led n-times with blink period wait_time_ms */ |
bieleluk | 0:e36b454cc2e6 | 240 | void flash_n_times(int wait_time_ms, int n); |
bieleluk | 0:e36b454cc2e6 | 241 | |
bieleluk | 18:472b66aeb1f5 | 242 | /** IRQ handler function, end breakpoint after the button is pushed */ |
bieleluk | 0:e36b454cc2e6 | 243 | void end_break(); |
bieleluk | 0:e36b454cc2e6 | 244 | }; |
bieleluk | 0:e36b454cc2e6 | 245 | |
bieleluk | 12:a8ab6e018422 | 246 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 247 | /** Debug_register class. |
bieleluk | 0:e36b454cc2e6 | 248 | * |
bieleluk | 0:e36b454cc2e6 | 249 | * Example program: |
bieleluk | 0:e36b454cc2e6 | 250 | * @code |
bieleluk | 0:e36b454cc2e6 | 251 | * // ---------------------------------------------------------------------------- |
bieleluk | 0:e36b454cc2e6 | 252 | * // Author: Lukas Bielesch |
bieleluk | 0:e36b454cc2e6 | 253 | * // Department of Measurement, Czech technical university in Prague, Czech Republic |
bieleluk | 0:e36b454cc2e6 | 254 | * // Date of publication: 15. Apr 2019 |
bieleluk | 0:e36b454cc2e6 | 255 | * // ---------------------------------------------------------------------------- |
bieleluk | 0:e36b454cc2e6 | 256 | * #include "Debug.h" |
bieleluk | 9:59344a584e18 | 257 | * AnalogIn analog(PA_4); |
bieleluk | 0:e36b454cc2e6 | 258 | * PwmOut pwm(PA_6); |
bieleluk | 9:59344a584e18 | 259 | * DigitalOut out(PA_5); |
bieleluk | 0:e36b454cc2e6 | 260 | * Debug_register pc(PA_2, PA_3, 115200); |
bieleluk | 0:e36b454cc2e6 | 261 | * |
bieleluk | 0:e36b454cc2e6 | 262 | * int main(){ |
bieleluk | 0:e36b454cc2e6 | 263 | * pc.breakpoint(__LINE__,0x48000000); |
bieleluk | 0:e36b454cc2e6 | 264 | * DigitalOut out2 (PA_0); |
bieleluk | 10:4d5f38d996e2 | 265 | * pc.breakpoint(__LINE__,0x48000001); |
bieleluk | 0:e36b454cc2e6 | 266 | * AnalogIn analog2 (PA_1); |
bieleluk | 6:1ee26b7b9c2f | 267 | * pc.breakpoint(__LINE__,0x4800000C); |
bieleluk | 5:e2e16ef72833 | 268 | * DigitalIn di1(PA_7, PullUp); |
bieleluk | 6:1ee26b7b9c2f | 269 | * pc.breakpoint(__LINE__,0x4800000C); |
bieleluk | 6:1ee26b7b9c2f | 270 | * pc.breakpoint(__LINE__,0x48000004); |
bieleluk | 0:e36b454cc2e6 | 271 | * while(1){ |
bieleluk | 0:e36b454cc2e6 | 272 | * if(pc.readable()){ |
bieleluk | 0:e36b454cc2e6 | 273 | * pc.putc(pc.getc()); |
bieleluk | 0:e36b454cc2e6 | 274 | * } |
bieleluk | 0:e36b454cc2e6 | 275 | * wait(0.1); |
bieleluk | 0:e36b454cc2e6 | 276 | * } |
bieleluk | 0:e36b454cc2e6 | 277 | * } |
bieleluk | 0:e36b454cc2e6 | 278 | * @endcode |
bieleluk | 0:e36b454cc2e6 | 279 | */ |
bieleluk | 0:e36b454cc2e6 | 280 | class Debug_register { |
bieleluk | 0:e36b454cc2e6 | 281 | public: |
bieleluk | 0:e36b454cc2e6 | 282 | |
bieleluk | 12:a8ab6e018422 | 283 | /** Create object of Debug_register class |
bieleluk | 12:a8ab6e018422 | 284 | * @param tx_pin TX pin of debug serial port |
bieleluk | 12:a8ab6e018422 | 285 | * @param rx_pin RX pin of debug serial port |
bieleluk | 12:a8ab6e018422 | 286 | * @param baudrate(optional) desired baudrate value of debug serial port, default baudrate is 115200 Bd/s |
bieleluk | 0:e36b454cc2e6 | 287 | */ |
bieleluk | 0:e36b454cc2e6 | 288 | Debug_register(PinName tx_pin, PinName rx_pin, int baudrate = 115200); |
bieleluk | 0:e36b454cc2e6 | 289 | |
bieleluk | 0:e36b454cc2e6 | 290 | /** Perform one breakpoint and print one register |
bieleluk | 12:a8ab6e018422 | 291 | * @param line_number line number of breakpoint, macro __LINE__ is recommended |
bieleluk | 12:a8ab6e018422 | 292 | * @param address address of register, must be divisible by 4 |
bieleluk | 0:e36b454cc2e6 | 293 | */ |
bieleluk | 12:a8ab6e018422 | 294 | void breakpoint(int line_number, uint32_t address); |
bieleluk | 0:e36b454cc2e6 | 295 | |
bieleluk | 0:e36b454cc2e6 | 296 | /** Print formatted string to debug serial port |
bieleluk | 0:e36b454cc2e6 | 297 | * @returns total number of printed characters or negative value if an output error or an encoding error |
bieleluk | 0:e36b454cc2e6 | 298 | */ |
bieleluk | 0:e36b454cc2e6 | 299 | int printf(const char* format, ...); |
bieleluk | 12:a8ab6e018422 | 300 | |
bieleluk | 12:a8ab6e018422 | 301 | /** Read formatted string from debug serial port |
bieleluk | 12:a8ab6e018422 | 302 | * @returns total number of successfully read arguments or negative value if en error occured |
bieleluk | 12:a8ab6e018422 | 303 | */ |
bieleluk | 12:a8ab6e018422 | 304 | int scanf(const char* format, ...); |
bieleluk | 0:e36b454cc2e6 | 305 | |
bieleluk | 0:e36b454cc2e6 | 306 | /** Print one character to debug serial port |
bieleluk | 0:e36b454cc2e6 | 307 | * @param character |
bieleluk | 0:e36b454cc2e6 | 308 | * @returns character written as an unsigned char cast to an int |
bieleluk | 0:e36b454cc2e6 | 309 | */ |
bieleluk | 0:e36b454cc2e6 | 310 | int putc(int character); |
bieleluk | 0:e36b454cc2e6 | 311 | |
bieleluk | 0:e36b454cc2e6 | 312 | /** Read one character from debug serial port |
bieleluk | 0:e36b454cc2e6 | 313 | * @returns character written as an unsigned char cast to an int |
bieleluk | 0:e36b454cc2e6 | 314 | */ |
bieleluk | 0:e36b454cc2e6 | 315 | int getc(); |
bieleluk | 0:e36b454cc2e6 | 316 | |
bieleluk | 12:a8ab6e018422 | 317 | /** Check whether there is some character available to read from debug serial port. |
bieleluk | 12:a8ab6e018422 | 318 | * @returns true when there is something available to read |
bieleluk | 12:a8ab6e018422 | 319 | */ |
bieleluk | 0:e36b454cc2e6 | 320 | bool readable(); |
bieleluk | 12:a8ab6e018422 | 321 | |
bieleluk | 12:a8ab6e018422 | 322 | /** Check whether it is possible to write a character to debug serial port. |
bieleluk | 12:a8ab6e018422 | 323 | * @returns true when it is possible to write a character to debug serial port |
bieleluk | 12:a8ab6e018422 | 324 | */ |
bieleluk | 2:478ba8b83e3f | 325 | bool writable(); |
bieleluk | 0:e36b454cc2e6 | 326 | |
bieleluk | 0:e36b454cc2e6 | 327 | protected: |
bieleluk | 0:e36b454cc2e6 | 328 | // objects: |
bieleluk | 0:e36b454cc2e6 | 329 | Serial pc; //debug serial device |
bieleluk | 0:e36b454cc2e6 | 330 | // variables: |
bieleluk | 12:a8ab6e018422 | 331 | int breakpoint_count; //number of the current breakpoint |
bieleluk | 0:e36b454cc2e6 | 332 | // functions |
bieleluk | 0:e36b454cc2e6 | 333 | // initialization function |
bieleluk | 0:e36b454cc2e6 | 334 | void init(); |
bieleluk | 0:e36b454cc2e6 | 335 | // clear screen from m line up to n line |
bieleluk | 0:e36b454cc2e6 | 336 | void clear_from_n_up_to_m(int m, int n); |
bieleluk | 15:83d4dced2a28 | 337 | // modify actual address or register value according to position in terminal |
bieleluk | 3:3d7837ae4a37 | 338 | uint32_t modify_value(uint32_t value, int horizontal); |
bieleluk | 15:83d4dced2a28 | 339 | // print address and register value |
bieleluk | 15:83d4dced2a28 | 340 | void print_break(uint32_t address); |
bieleluk | 15:83d4dced2a28 | 341 | // print number in binary form |
bieleluk | 15:83d4dced2a28 | 342 | void print_binary(uint32_t value); |
bieleluk | 0:e36b454cc2e6 | 343 | }; |
bieleluk | 3:3d7837ae4a37 | 344 | |
bieleluk | 12:a8ab6e018422 | 345 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 346 | /** Debug_register_print class. |
bieleluk | 0:e36b454cc2e6 | 347 | * |
bieleluk | 0:e36b454cc2e6 | 348 | * Example program: |
bieleluk | 0:e36b454cc2e6 | 349 | * @code |
bieleluk | 0:e36b454cc2e6 | 350 | * // ---------------------------------------------------------------------------- |
bieleluk | 0:e36b454cc2e6 | 351 | * // Author: Lukas Bielesch |
bieleluk | 0:e36b454cc2e6 | 352 | * // Department of Measurement, Czech technical university in Prague, Czech Republic |
bieleluk | 0:e36b454cc2e6 | 353 | * // Date of publication: 15. Apr 2019 |
bieleluk | 0:e36b454cc2e6 | 354 | * // ---------------------------------------------------------------------------- |
bieleluk | 0:e36b454cc2e6 | 355 | * #include "Debug.h" |
bieleluk | 9:59344a584e18 | 356 | * AnalogIn analog(PA_4); |
bieleluk | 0:e36b454cc2e6 | 357 | * PwmOut pwm(PA_6); |
bieleluk | 9:59344a584e18 | 358 | * DigitalOut out(PA_5); |
bieleluk | 0:e36b454cc2e6 | 359 | * Debug_register_print pc(PA_2, PA_3, 115200); |
bieleluk | 0:e36b454cc2e6 | 360 | * |
bieleluk | 0:e36b454cc2e6 | 361 | * int main(){ |
bieleluk | 0:e36b454cc2e6 | 362 | * pc.format(2,2,1,3);//breakpoint count,line number, address, value |
bieleluk | 10:4d5f38d996e2 | 363 | * pc.breakpoint(__LINE__,0x48000001, 2); |
bieleluk | 0:e36b454cc2e6 | 364 | * DigitalOut out2 (PA_0); |
bieleluk | 6:1ee26b7b9c2f | 365 | * pc.breakpoint(__LINE__,0x48000014, -3); |
bieleluk | 0:e36b454cc2e6 | 366 | * AnalogIn analog2 (PA_1); |
bieleluk | 6:1ee26b7b9c2f | 367 | * pc.breakpoint(__LINE__,0x48000008); |
bieleluk | 0:e36b454cc2e6 | 368 | * |
bieleluk | 0:e36b454cc2e6 | 369 | * while(1){ |
bieleluk | 0:e36b454cc2e6 | 370 | * wait(1); |
bieleluk | 0:e36b454cc2e6 | 371 | * } |
bieleluk | 0:e36b454cc2e6 | 372 | * } |
bieleluk | 0:e36b454cc2e6 | 373 | * @endcode |
bieleluk | 0:e36b454cc2e6 | 374 | */ |
bieleluk | 0:e36b454cc2e6 | 375 | class Debug_register_print { |
bieleluk | 0:e36b454cc2e6 | 376 | public: |
bieleluk | 0:e36b454cc2e6 | 377 | |
bieleluk | 12:a8ab6e018422 | 378 | /** Create object of Debug_register_print class |
bieleluk | 12:a8ab6e018422 | 379 | * @param tx_pin TX pin of debug serial port |
bieleluk | 12:a8ab6e018422 | 380 | * @param rx_pin RX pin of debug serial port |
bieleluk | 12:a8ab6e018422 | 381 | * @param baudrate(optional) desired baudrate value of debug serial port, default baudrate is 115200 Bd/s |
bieleluk | 0:e36b454cc2e6 | 382 | */ |
bieleluk | 0:e36b454cc2e6 | 383 | Debug_register_print(PinName tx_pin, PinName rx_pin, int baudrate = 115200); |
bieleluk | 0:e36b454cc2e6 | 384 | |
bieleluk | 12:a8ab6e018422 | 385 | /** Set format of breakpoint message |
bieleluk | 12:a8ab6e018422 | 386 | * @param break_number format of number of actual breakpoint: 0->not show, 1->show in hexadecimal, 2->show in decimal(default) |
bieleluk | 12:a8ab6e018422 | 387 | * @param line format of line of actual breakpoint: 0->not show, 1->show in hexadecimal, 2->show in decimal(default) |
bieleluk | 12:a8ab6e018422 | 388 | * @param address format of address of register: 0->not show, 1->show in hexadecimal(default), 2->show in decimal |
bieleluk | 12:a8ab6e018422 | 389 | * @param value format of register value: 0->not show, 1->show in hexadecimal, 2->show in decimal, 3->show in binary(default) |
bieleluk | 12:a8ab6e018422 | 390 | */ |
bieleluk | 12:a8ab6e018422 | 391 | void format(int break_number = 2, int line = 2, int address = 1, int value = 3); |
bieleluk | 0:e36b454cc2e6 | 392 | |
bieleluk | 12:a8ab6e018422 | 393 | /** Perform one breakpoint and print one register value |
bieleluk | 12:a8ab6e018422 | 394 | * @param line_number line number of breakpoint, macro __LINE__ is recommended |
bieleluk | 12:a8ab6e018422 | 395 | * @param address address of register , must be divisible by 4 |
bieleluk | 0:e36b454cc2e6 | 396 | */ |
bieleluk | 12:a8ab6e018422 | 397 | void breakpoint(int line_number, uint32_t address, int number_of_words = 1); |
bieleluk | 0:e36b454cc2e6 | 398 | |
bieleluk | 0:e36b454cc2e6 | 399 | |
bieleluk | 0:e36b454cc2e6 | 400 | protected: |
bieleluk | 0:e36b454cc2e6 | 401 | // objects: |
bieleluk | 0:e36b454cc2e6 | 402 | Serial pc; //debug serial device |
bieleluk | 0:e36b454cc2e6 | 403 | // variables: |
bieleluk | 12:a8ab6e018422 | 404 | int breakpoint_count; //number of the current breakpoint |
bieleluk | 12:a8ab6e018422 | 405 | int count_format; // format of breakpoint count, 0->not show, 1->show in hexadecimal, 2->show in decimal |
bieleluk | 12:a8ab6e018422 | 406 | int line_format; // format of lineof breakpoint, 0->not show, 1->show in hexadecimal, 2->show in decimal |
bieleluk | 12:a8ab6e018422 | 407 | int address_format; // format of address of register, 0->not show, 1->show in hexadecimal, 2->show in decimal |
bieleluk | 12:a8ab6e018422 | 408 | int register_format;// format of register value, 0->not show, 1->show in hexadecimal, 2->show in decimal, 3->show in binary |
bieleluk | 0:e36b454cc2e6 | 409 | // functions |
bieleluk | 0:e36b454cc2e6 | 410 | // initialization function |
bieleluk | 0:e36b454cc2e6 | 411 | void init(); |
bieleluk | 0:e36b454cc2e6 | 412 | |
bieleluk | 16:7fc964b6d127 | 413 | }; |
bieleluk | 16:7fc964b6d127 | 414 |