Debugging tool for mbed enabled microcontrollers, especially for NUCLEO-F303RE and STM32F042F6P6.
debug_serial.cpp@0:e36b454cc2e6, 2019-05-06 (annotated)
- Committer:
- bieleluk
- Date:
- Mon May 06 00:01:17 2019 +0000
- Revision:
- 0:e36b454cc2e6
- Child:
- 1:dbb9fcc20d07
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bieleluk | 0:e36b454cc2e6 | 1 | #include "Debug.h" |
bieleluk | 0:e36b454cc2e6 | 2 | |
bieleluk | 0:e36b454cc2e6 | 3 | // create object of class Debug_serial |
bieleluk | 0:e36b454cc2e6 | 4 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 5 | Debug_serial::Debug_serial(PinName tx_pin, PinName rx_pin, int baudrate) : pc(tx_pin,rx_pin, baudrate) { |
bieleluk | 0:e36b454cc2e6 | 6 | init(); |
bieleluk | 0:e36b454cc2e6 | 7 | } |
bieleluk | 0:e36b454cc2e6 | 8 | |
bieleluk | 0:e36b454cc2e6 | 9 | // init function |
bieleluk | 0:e36b454cc2e6 | 10 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 11 | void Debug_serial::init() { |
bieleluk | 0:e36b454cc2e6 | 12 | pc.printf("\ec"); |
bieleluk | 0:e36b454cc2e6 | 13 | wait_ms(50); |
bieleluk | 0:e36b454cc2e6 | 14 | pc.printf("-----------------\n\r|\e[1m\e[97;40m\e[93;40mBREAKPOINT AREA\e[22m\e[97;40m|\n\r-----------------\n\r\033[s"); |
bieleluk | 0:e36b454cc2e6 | 15 | pc.printf("serial successfully initialised\n\r\e[32;40mto start program press any button\e[97;40m"); |
bieleluk | 0:e36b454cc2e6 | 16 | breakpoint_count=0; |
bieleluk | 0:e36b454cc2e6 | 17 | pc.getc(); |
bieleluk | 0:e36b454cc2e6 | 18 | pc.printf("\r\e[2K\e[31;40mprogram is running\e[97;40m\r"); |
bieleluk | 0:e36b454cc2e6 | 19 | break_line[0] = -1; |
bieleluk | 0:e36b454cc2e6 | 20 | break_line[1] = -1; |
bieleluk | 0:e36b454cc2e6 | 21 | break_line[2] = -1; |
bieleluk | 0:e36b454cc2e6 | 22 | strcpy(var[0], "\0"); |
bieleluk | 0:e36b454cc2e6 | 23 | strcpy(var[1], "\0"); |
bieleluk | 0:e36b454cc2e6 | 24 | strcpy(var[2], "\0"); |
bieleluk | 0:e36b454cc2e6 | 25 | pc.printf("\033[14;0H------------------\n\r|\e[1m\e[93;40mSERIAL PORT AREA\e[22m\e[97;40m|\n\r------------------\n\r\033[s"); |
bieleluk | 0:e36b454cc2e6 | 26 | } |
bieleluk | 0:e36b454cc2e6 | 27 | |
bieleluk | 0:e36b454cc2e6 | 28 | |
bieleluk | 0:e36b454cc2e6 | 29 | // perform one breakpoint without printing variable |
bieleluk | 0:e36b454cc2e6 | 30 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 31 | void Debug_serial::breakpoint(int line_number) { |
bieleluk | 0:e36b454cc2e6 | 32 | strcpy(var[2], var[1]); |
bieleluk | 0:e36b454cc2e6 | 33 | strcpy(var[1], var[0]); |
bieleluk | 0:e36b454cc2e6 | 34 | sprintf(var[0],"\0"); |
bieleluk | 0:e36b454cc2e6 | 35 | print_3_breaks(line_number); |
bieleluk | 0:e36b454cc2e6 | 36 | } |
bieleluk | 0:e36b454cc2e6 | 37 | |
bieleluk | 0:e36b454cc2e6 | 38 | // perform one breakpoint and print variable of type int |
bieleluk | 0:e36b454cc2e6 | 39 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 40 | void Debug_serial::breakpoint(int line_number, char name[20], int variable){ |
bieleluk | 0:e36b454cc2e6 | 41 | |
bieleluk | 0:e36b454cc2e6 | 42 | strcpy(var[2], var[1]); |
bieleluk | 0:e36b454cc2e6 | 43 | strcpy(var[1], var[0]); |
bieleluk | 0:e36b454cc2e6 | 44 | sprintf(var[0],"int %s = %d",name,variable); |
bieleluk | 0:e36b454cc2e6 | 45 | print_3_breaks(line_number); |
bieleluk | 0:e36b454cc2e6 | 46 | |
bieleluk | 0:e36b454cc2e6 | 47 | } |
bieleluk | 0:e36b454cc2e6 | 48 | |
bieleluk | 0:e36b454cc2e6 | 49 | // perform one breakpoint and print variable of type char |
bieleluk | 0:e36b454cc2e6 | 50 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 51 | void Debug_serial::breakpoint(int line_number, char name[20], char variable){ |
bieleluk | 0:e36b454cc2e6 | 52 | |
bieleluk | 0:e36b454cc2e6 | 53 | strcpy(var[2], var[1]); |
bieleluk | 0:e36b454cc2e6 | 54 | strcpy(var[1], var[0]); |
bieleluk | 0:e36b454cc2e6 | 55 | sprintf(var[0],"char %s = %c",name,variable); |
bieleluk | 0:e36b454cc2e6 | 56 | print_3_breaks(line_number); |
bieleluk | 0:e36b454cc2e6 | 57 | |
bieleluk | 0:e36b454cc2e6 | 58 | } |
bieleluk | 0:e36b454cc2e6 | 59 | |
bieleluk | 0:e36b454cc2e6 | 60 | // perform one breakpoint and print variable of type float |
bieleluk | 0:e36b454cc2e6 | 61 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 62 | void Debug_serial::breakpoint(int line_number, char name[20], float variable){ |
bieleluk | 0:e36b454cc2e6 | 63 | |
bieleluk | 0:e36b454cc2e6 | 64 | strcpy(var[2], var[1]); |
bieleluk | 0:e36b454cc2e6 | 65 | strcpy(var[1], var[0]); |
bieleluk | 0:e36b454cc2e6 | 66 | sprintf(var[0],"float %s = %f",name,variable); |
bieleluk | 0:e36b454cc2e6 | 67 | print_3_breaks(line_number); |
bieleluk | 0:e36b454cc2e6 | 68 | |
bieleluk | 0:e36b454cc2e6 | 69 | } |
bieleluk | 0:e36b454cc2e6 | 70 | |
bieleluk | 0:e36b454cc2e6 | 71 | // perform one breakpoint and print variable of type string |
bieleluk | 0:e36b454cc2e6 | 72 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 73 | void Debug_serial::breakpoint(int line_number, char name[20], char * variable){ |
bieleluk | 0:e36b454cc2e6 | 74 | |
bieleluk | 0:e36b454cc2e6 | 75 | strcpy(var[2], var[1]); |
bieleluk | 0:e36b454cc2e6 | 76 | strcpy(var[1], var[0]); |
bieleluk | 0:e36b454cc2e6 | 77 | sprintf(var[0],"string %s = %s",name,variable); |
bieleluk | 0:e36b454cc2e6 | 78 | print_3_breaks(line_number); |
bieleluk | 0:e36b454cc2e6 | 79 | |
bieleluk | 0:e36b454cc2e6 | 80 | } |
bieleluk | 0:e36b454cc2e6 | 81 | |
bieleluk | 0:e36b454cc2e6 | 82 | // print formatted string to debug serial port |
bieleluk | 0:e36b454cc2e6 | 83 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 84 | int Debug_serial::printf(const char* format, ...){ |
bieleluk | 0:e36b454cc2e6 | 85 | |
bieleluk | 0:e36b454cc2e6 | 86 | int ret = pc.printf(format); |
bieleluk | 0:e36b454cc2e6 | 87 | return ret; |
bieleluk | 0:e36b454cc2e6 | 88 | } |
bieleluk | 0:e36b454cc2e6 | 89 | |
bieleluk | 0:e36b454cc2e6 | 90 | // print character to debug serial port |
bieleluk | 0:e36b454cc2e6 | 91 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 92 | int Debug_serial::putc(int character){ |
bieleluk | 0:e36b454cc2e6 | 93 | |
bieleluk | 0:e36b454cc2e6 | 94 | return pc.putc(character); |
bieleluk | 0:e36b454cc2e6 | 95 | } |
bieleluk | 0:e36b454cc2e6 | 96 | |
bieleluk | 0:e36b454cc2e6 | 97 | // read character from debug serial port |
bieleluk | 0:e36b454cc2e6 | 98 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 99 | int Debug_serial::getc(){ |
bieleluk | 0:e36b454cc2e6 | 100 | return pc.getc(); |
bieleluk | 0:e36b454cc2e6 | 101 | } |
bieleluk | 0:e36b454cc2e6 | 102 | |
bieleluk | 0:e36b454cc2e6 | 103 | // clear screen from m line up to n line |
bieleluk | 0:e36b454cc2e6 | 104 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 105 | void Debug_serial::clear_from_n_up_to_m(int m, int n){ |
bieleluk | 0:e36b454cc2e6 | 106 | pc.printf("\033[%d;0H",m); |
bieleluk | 0:e36b454cc2e6 | 107 | wait(0.1); |
bieleluk | 0:e36b454cc2e6 | 108 | while (m > n){ |
bieleluk | 0:e36b454cc2e6 | 109 | m--; |
bieleluk | 0:e36b454cc2e6 | 110 | pc.printf("\033[K\033[%d;0H",m); |
bieleluk | 0:e36b454cc2e6 | 111 | } |
bieleluk | 0:e36b454cc2e6 | 112 | pc.printf("\n\r"); |
bieleluk | 0:e36b454cc2e6 | 113 | |
bieleluk | 0:e36b454cc2e6 | 114 | } |
bieleluk | 0:e36b454cc2e6 | 115 | |
bieleluk | 0:e36b454cc2e6 | 116 | // print 3 last breakpoints |
bieleluk | 0:e36b454cc2e6 | 117 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 118 | void Debug_serial::print_3_breaks(int line_number){ |
bieleluk | 0:e36b454cc2e6 | 119 | pc.printf("\e[s"); |
bieleluk | 0:e36b454cc2e6 | 120 | wait_ms(50); |
bieleluk | 0:e36b454cc2e6 | 121 | breakpoint_count++; |
bieleluk | 0:e36b454cc2e6 | 122 | break_line[2] = break_line[1]; |
bieleluk | 0:e36b454cc2e6 | 123 | break_line[1] = break_line[0]; |
bieleluk | 0:e36b454cc2e6 | 124 | break_line[0] = line_number; |
bieleluk | 0:e36b454cc2e6 | 125 | clear_from_n_up_to_m(12,3); |
bieleluk | 0:e36b454cc2e6 | 126 | int i = (breakpoint_count>=3)?3:breakpoint_count; |
bieleluk | 0:e36b454cc2e6 | 127 | while (i > 0){ |
bieleluk | 0:e36b454cc2e6 | 128 | if (i == 1 ){ |
bieleluk | 0:e36b454cc2e6 | 129 | pc.printf("\e[1m\e[96;40m"); |
bieleluk | 0:e36b454cc2e6 | 130 | print_one_break(i); |
bieleluk | 0:e36b454cc2e6 | 131 | pc.printf("\e[22m\e[97;40m"); |
bieleluk | 0:e36b454cc2e6 | 132 | }else{ |
bieleluk | 0:e36b454cc2e6 | 133 | print_one_break(i); |
bieleluk | 0:e36b454cc2e6 | 134 | } |
bieleluk | 0:e36b454cc2e6 | 135 | i--; |
bieleluk | 0:e36b454cc2e6 | 136 | } |
bieleluk | 0:e36b454cc2e6 | 137 | pc.printf("\e[32;40mto continue press any button\e[97;40m"); |
bieleluk | 0:e36b454cc2e6 | 138 | pc.getc(); |
bieleluk | 0:e36b454cc2e6 | 139 | pc.printf("\r\e[2K\e[31;40mprogram is running\e[97;40m\n\r"); |
bieleluk | 0:e36b454cc2e6 | 140 | pc.printf("\e[u"); |
bieleluk | 0:e36b454cc2e6 | 141 | |
bieleluk | 0:e36b454cc2e6 | 142 | } |
bieleluk | 0:e36b454cc2e6 | 143 | |
bieleluk | 0:e36b454cc2e6 | 144 | // print one breakpoint |
bieleluk | 0:e36b454cc2e6 | 145 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 146 | void Debug_serial::print_one_break(int n){ |
bieleluk | 0:e36b454cc2e6 | 147 | if (break_line[n-1] < 0){ |
bieleluk | 0:e36b454cc2e6 | 148 | pc.printf("Breakpoint number %d\t unknown line number\n\r%s\n\n\r",breakpoint_count - n + 1,var[n-1]); |
bieleluk | 0:e36b454cc2e6 | 149 | }else{ |
bieleluk | 0:e36b454cc2e6 | 150 | pc.printf("Breakpoint number %d\tline number %d\n\r%s\n\n\r",breakpoint_count - n + 1,break_line[n-1],var[n-1]); |
bieleluk | 0:e36b454cc2e6 | 151 | } |
bieleluk | 0:e36b454cc2e6 | 152 | } |