Debugging tool for mbed enabled microcontrollers, especially for NUCLEO-F303RE and STM32F042F6P6.

Committer:
bieleluk
Date:
Sun Mar 21 20:16:42 2021 +0000
Revision:
22:ac9b2cbb31a8
Parent:
15:83d4dced2a28

        

Who changed what in which revision?

UserRevisionLine numberNew 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 15:83d4dced2a28 12 pc.printf("\ec"); //clear entire screen
bieleluk 15:83d4dced2a28 13 wait_ms(50); //wait until clearing is done
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 15:83d4dced2a28 16 breakpoint_count = 0; //set breakpoint count to 0
bieleluk 15:83d4dced2a28 17 pc.getc(); // wait until user sends any character
bieleluk 0:e36b454cc2e6 18 pc.printf("\r\e[2K\e[31;40mprogram is running\e[97;40m\r");
bieleluk 15:83d4dced2a28 19
bieleluk 15:83d4dced2a28 20 // information about last three breakpoints
bieleluk 0:e36b454cc2e6 21 break_line[0] = -1;
bieleluk 0:e36b454cc2e6 22 break_line[1] = -1;
bieleluk 0:e36b454cc2e6 23 break_line[2] = -1;
bieleluk 0:e36b454cc2e6 24 strcpy(var[0], "\0");
bieleluk 0:e36b454cc2e6 25 strcpy(var[1], "\0");
bieleluk 0:e36b454cc2e6 26 strcpy(var[2], "\0");
bieleluk 0:e36b454cc2e6 27 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 28 }
bieleluk 0:e36b454cc2e6 29
bieleluk 0:e36b454cc2e6 30 // perform one breakpoint without printing variable
bieleluk 0:e36b454cc2e6 31 //------------------------------------------------------------------------------------------------------------------
bieleluk 0:e36b454cc2e6 32 void Debug_serial::breakpoint(int line_number) {
bieleluk 0:e36b454cc2e6 33 strcpy(var[2], var[1]);
bieleluk 0:e36b454cc2e6 34 strcpy(var[1], var[0]);
bieleluk 0:e36b454cc2e6 35 sprintf(var[0],"\0");
bieleluk 0:e36b454cc2e6 36 print_3_breaks(line_number);
bieleluk 0:e36b454cc2e6 37 }
bieleluk 0:e36b454cc2e6 38
bieleluk 0:e36b454cc2e6 39 // perform one breakpoint and print variable of type int
bieleluk 0:e36b454cc2e6 40 //------------------------------------------------------------------------------------------------------------------
bieleluk 0:e36b454cc2e6 41 void Debug_serial::breakpoint(int line_number, char name[20], int variable){
bieleluk 0:e36b454cc2e6 42
bieleluk 0:e36b454cc2e6 43 strcpy(var[2], var[1]);
bieleluk 0:e36b454cc2e6 44 strcpy(var[1], var[0]);
bieleluk 0:e36b454cc2e6 45 sprintf(var[0],"int %s = %d",name,variable);
bieleluk 0:e36b454cc2e6 46 print_3_breaks(line_number);
bieleluk 0:e36b454cc2e6 47
bieleluk 0:e36b454cc2e6 48 }
bieleluk 0:e36b454cc2e6 49
bieleluk 0:e36b454cc2e6 50 // perform one breakpoint and print variable of type char
bieleluk 0:e36b454cc2e6 51 //------------------------------------------------------------------------------------------------------------------
bieleluk 0:e36b454cc2e6 52 void Debug_serial::breakpoint(int line_number, char name[20], char variable){
bieleluk 0:e36b454cc2e6 53
bieleluk 0:e36b454cc2e6 54 strcpy(var[2], var[1]);
bieleluk 0:e36b454cc2e6 55 strcpy(var[1], var[0]);
bieleluk 0:e36b454cc2e6 56 sprintf(var[0],"char %s = %c",name,variable);
bieleluk 0:e36b454cc2e6 57 print_3_breaks(line_number);
bieleluk 0:e36b454cc2e6 58
bieleluk 0:e36b454cc2e6 59 }
bieleluk 0:e36b454cc2e6 60
bieleluk 0:e36b454cc2e6 61 // perform one breakpoint and print variable of type float
bieleluk 0:e36b454cc2e6 62 //------------------------------------------------------------------------------------------------------------------
bieleluk 0:e36b454cc2e6 63 void Debug_serial::breakpoint(int line_number, char name[20], float variable){
bieleluk 0:e36b454cc2e6 64
bieleluk 0:e36b454cc2e6 65 strcpy(var[2], var[1]);
bieleluk 0:e36b454cc2e6 66 strcpy(var[1], var[0]);
bieleluk 0:e36b454cc2e6 67 sprintf(var[0],"float %s = %f",name,variable);
bieleluk 0:e36b454cc2e6 68 print_3_breaks(line_number);
bieleluk 0:e36b454cc2e6 69
bieleluk 0:e36b454cc2e6 70 }
bieleluk 0:e36b454cc2e6 71
bieleluk 0:e36b454cc2e6 72 // perform one breakpoint and print variable of type string
bieleluk 0:e36b454cc2e6 73 //------------------------------------------------------------------------------------------------------------------
bieleluk 0:e36b454cc2e6 74 void Debug_serial::breakpoint(int line_number, char name[20], char * variable){
bieleluk 0:e36b454cc2e6 75
bieleluk 0:e36b454cc2e6 76 strcpy(var[2], var[1]);
bieleluk 0:e36b454cc2e6 77 strcpy(var[1], var[0]);
bieleluk 0:e36b454cc2e6 78 sprintf(var[0],"string %s = %s",name,variable);
bieleluk 0:e36b454cc2e6 79 print_3_breaks(line_number);
bieleluk 0:e36b454cc2e6 80
bieleluk 0:e36b454cc2e6 81 }
bieleluk 0:e36b454cc2e6 82
bieleluk 1:dbb9fcc20d07 83 // perform one breakpoint and print register
bieleluk 1:dbb9fcc20d07 84 //------------------------------------------------------------------------------------------------------------------
bieleluk 6:1ee26b7b9c2f 85 void Debug_serial::breakpoint(int line_number, uint32_t address){
bieleluk 1:dbb9fcc20d07 86
bieleluk 1:dbb9fcc20d07 87 strcpy(var[2], var[1]);
bieleluk 1:dbb9fcc20d07 88 strcpy(var[1], var[0]);
bieleluk 15:83d4dced2a28 89 sprintf(var[0],"Address 0x%8x Value 0x%8x", address - address%4, *((volatile unsigned int *)(address - address % 4)));
bieleluk 1:dbb9fcc20d07 90 print_3_breaks(line_number);
bieleluk 1:dbb9fcc20d07 91
bieleluk 1:dbb9fcc20d07 92 }
bieleluk 0:e36b454cc2e6 93 // print formatted string to debug serial port
bieleluk 0:e36b454cc2e6 94 //------------------------------------------------------------------------------------------------------------------
bieleluk 0:e36b454cc2e6 95 int Debug_serial::printf(const char* format, ...){
bieleluk 12:a8ab6e018422 96
bieleluk 12:a8ab6e018422 97 int ret = pc.printf(format);
bieleluk 12:a8ab6e018422 98 return ret;
bieleluk 12:a8ab6e018422 99 }
bieleluk 0:e36b454cc2e6 100
bieleluk 12:a8ab6e018422 101 // read formatted string from debug serial port
bieleluk 12:a8ab6e018422 102 //------------------------------------------------------------------------------------------------------------------
bieleluk 12:a8ab6e018422 103 int Debug_serial::scanf(const char* format, ...){
bieleluk 12:a8ab6e018422 104
bieleluk 12:a8ab6e018422 105 int ret = pc.scanf(format);
bieleluk 0:e36b454cc2e6 106 return ret;
bieleluk 0:e36b454cc2e6 107 }
bieleluk 0:e36b454cc2e6 108
bieleluk 0:e36b454cc2e6 109 // print character to debug serial port
bieleluk 0:e36b454cc2e6 110 //------------------------------------------------------------------------------------------------------------------
bieleluk 0:e36b454cc2e6 111 int Debug_serial::putc(int character){
bieleluk 0:e36b454cc2e6 112 return pc.putc(character);
bieleluk 0:e36b454cc2e6 113 }
bieleluk 0:e36b454cc2e6 114
bieleluk 0:e36b454cc2e6 115 // read character from debug serial port
bieleluk 0:e36b454cc2e6 116 //------------------------------------------------------------------------------------------------------------------
bieleluk 0:e36b454cc2e6 117 int Debug_serial::getc(){
bieleluk 0:e36b454cc2e6 118 return pc.getc();
bieleluk 0:e36b454cc2e6 119 }
bieleluk 0:e36b454cc2e6 120
bieleluk 12:a8ab6e018422 121 // check whether there is any character to read
bieleluk 12:a8ab6e018422 122 //------------------------------------------------------------------------------------------------------------------
bieleluk 2:478ba8b83e3f 123 bool Debug_serial::readable(){
bieleluk 2:478ba8b83e3f 124 return pc.readable();
bieleluk 2:478ba8b83e3f 125 }
bieleluk 2:478ba8b83e3f 126
bieleluk 12:a8ab6e018422 127 // check whether it is possible to write a sign to debug serial port
bieleluk 12:a8ab6e018422 128 //------------------------------------------------------------------------------------------------------------------
bieleluk 2:478ba8b83e3f 129 bool Debug_serial::writable(){
bieleluk 2:478ba8b83e3f 130 return pc.writable();
bieleluk 2:478ba8b83e3f 131 }
bieleluk 2:478ba8b83e3f 132
bieleluk 0:e36b454cc2e6 133 // clear screen from m line up to n line
bieleluk 0:e36b454cc2e6 134 //------------------------------------------------------------------------------------------------------------------
bieleluk 0:e36b454cc2e6 135 void Debug_serial::clear_from_n_up_to_m(int m, int n){
bieleluk 12:a8ab6e018422 136 pc.printf("\033[%d;0H",m); //replace cursor to m-th row, 0 column
bieleluk 0:e36b454cc2e6 137 wait(0.1);
bieleluk 0:e36b454cc2e6 138 while (m > n){
bieleluk 0:e36b454cc2e6 139 m--;
bieleluk 12:a8ab6e018422 140 pc.printf("\033[K\033[%d;0H",m); // clear entire line
bieleluk 0:e36b454cc2e6 141 }
bieleluk 12:a8ab6e018422 142 pc.printf("\n\r");// go to new line
bieleluk 0:e36b454cc2e6 143
bieleluk 0:e36b454cc2e6 144 }
bieleluk 0:e36b454cc2e6 145
bieleluk 12:a8ab6e018422 146 // print information about 3 last breakpoints
bieleluk 0:e36b454cc2e6 147 //------------------------------------------------------------------------------------------------------------------
bieleluk 0:e36b454cc2e6 148 void Debug_serial::print_3_breaks(int line_number){
bieleluk 12:a8ab6e018422 149 pc.printf("\e[s"); //save current cursor position
bieleluk 15:83d4dced2a28 150 wait_ms(50); // wait until saving is done
bieleluk 0:e36b454cc2e6 151 breakpoint_count++;
bieleluk 12:a8ab6e018422 152 break_line[2] = break_line[1]; //save information about line of breakpoints
bieleluk 0:e36b454cc2e6 153 break_line[1] = break_line[0];
bieleluk 0:e36b454cc2e6 154 break_line[0] = line_number;
bieleluk 12:a8ab6e018422 155
bieleluk 12:a8ab6e018422 156 clear_from_n_up_to_m(12,3); // clear screen from 12th to 3rd line
bieleluk 12:a8ab6e018422 157
bieleluk 12:a8ab6e018422 158 int i = (breakpoint_count>=3)?3:breakpoint_count; //print 3 previous breakpoints if possible
bieleluk 0:e36b454cc2e6 159 while (i > 0){
bieleluk 0:e36b454cc2e6 160 if (i == 1 ){
bieleluk 22:ac9b2cbb31a8 161 pc.printf("\e[96;40m"); //change color of the last breakpoint
bieleluk 0:e36b454cc2e6 162 print_one_break(i);
bieleluk 12:a8ab6e018422 163 pc.printf("\e[22m\e[97;40m"); //return to normal color
bieleluk 0:e36b454cc2e6 164 }else{
bieleluk 0:e36b454cc2e6 165 print_one_break(i);
bieleluk 0:e36b454cc2e6 166 }
bieleluk 0:e36b454cc2e6 167 i--;
bieleluk 0:e36b454cc2e6 168 }
bieleluk 0:e36b454cc2e6 169 pc.printf("\e[32;40mto continue press any button\e[97;40m");
bieleluk 0:e36b454cc2e6 170 pc.getc();
bieleluk 0:e36b454cc2e6 171 pc.printf("\r\e[2K\e[31;40mprogram is running\e[97;40m\n\r");
bieleluk 12:a8ab6e018422 172 pc.printf("\e[u"); // return cursor to serial port part
bieleluk 0:e36b454cc2e6 173
bieleluk 0:e36b454cc2e6 174 }
bieleluk 0:e36b454cc2e6 175
bieleluk 0:e36b454cc2e6 176 // print one breakpoint
bieleluk 0:e36b454cc2e6 177 //------------------------------------------------------------------------------------------------------------------
bieleluk 0:e36b454cc2e6 178 void Debug_serial::print_one_break(int n){
bieleluk 0:e36b454cc2e6 179 if (break_line[n-1] < 0){
bieleluk 0:e36b454cc2e6 180 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 181 }else{
bieleluk 0:e36b454cc2e6 182 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 183 }
bieleluk 0:e36b454cc2e6 184 }