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