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

Committer:
bieleluk
Date:
Mon May 06 00:29:00 2019 +0000
Revision:
1:dbb9fcc20d07
Parent:
0:e36b454cc2e6
Child:
2:478ba8b83e3f

        

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 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 1:dbb9fcc20d07 82 // perform one breakpoint and print register
bieleluk 1:dbb9fcc20d07 83 //------------------------------------------------------------------------------------------------------------------
bieleluk 1:dbb9fcc20d07 84 void Debug_serial::breakpoint(int line_number, uint32_t address, uint32_t offset){
bieleluk 1:dbb9fcc20d07 85
bieleluk 1:dbb9fcc20d07 86 strcpy(var[2], var[1]);
bieleluk 1:dbb9fcc20d07 87 strcpy(var[1], var[0]);
bieleluk 1:dbb9fcc20d07 88 sprintf(var[0],"Address 0x%8x Value 0x%8x", address + offset,read_word(address, offset));
bieleluk 1:dbb9fcc20d07 89 print_3_breaks(line_number);
bieleluk 1:dbb9fcc20d07 90
bieleluk 1:dbb9fcc20d07 91 }
bieleluk 0:e36b454cc2e6 92 // print formatted string to debug serial port
bieleluk 0:e36b454cc2e6 93 //------------------------------------------------------------------------------------------------------------------
bieleluk 0:e36b454cc2e6 94 int Debug_serial::printf(const char* format, ...){
bieleluk 0:e36b454cc2e6 95
bieleluk 0:e36b454cc2e6 96 int ret = pc.printf(format);
bieleluk 0:e36b454cc2e6 97 return ret;
bieleluk 0:e36b454cc2e6 98 }
bieleluk 0:e36b454cc2e6 99
bieleluk 0:e36b454cc2e6 100 // print character to debug serial port
bieleluk 0:e36b454cc2e6 101 //------------------------------------------------------------------------------------------------------------------
bieleluk 0:e36b454cc2e6 102 int Debug_serial::putc(int character){
bieleluk 0:e36b454cc2e6 103
bieleluk 0:e36b454cc2e6 104 return pc.putc(character);
bieleluk 0:e36b454cc2e6 105 }
bieleluk 0:e36b454cc2e6 106
bieleluk 0:e36b454cc2e6 107 // read character from debug serial port
bieleluk 0:e36b454cc2e6 108 //------------------------------------------------------------------------------------------------------------------
bieleluk 0:e36b454cc2e6 109 int Debug_serial::getc(){
bieleluk 0:e36b454cc2e6 110 return pc.getc();
bieleluk 0:e36b454cc2e6 111 }
bieleluk 0:e36b454cc2e6 112
bieleluk 0:e36b454cc2e6 113 // clear screen from m line up to n line
bieleluk 0:e36b454cc2e6 114 //------------------------------------------------------------------------------------------------------------------
bieleluk 0:e36b454cc2e6 115 void Debug_serial::clear_from_n_up_to_m(int m, int n){
bieleluk 0:e36b454cc2e6 116 pc.printf("\033[%d;0H",m);
bieleluk 0:e36b454cc2e6 117 wait(0.1);
bieleluk 0:e36b454cc2e6 118 while (m > n){
bieleluk 0:e36b454cc2e6 119 m--;
bieleluk 0:e36b454cc2e6 120 pc.printf("\033[K\033[%d;0H",m);
bieleluk 0:e36b454cc2e6 121 }
bieleluk 0:e36b454cc2e6 122 pc.printf("\n\r");
bieleluk 0:e36b454cc2e6 123
bieleluk 0:e36b454cc2e6 124 }
bieleluk 0:e36b454cc2e6 125
bieleluk 0:e36b454cc2e6 126 // print 3 last breakpoints
bieleluk 0:e36b454cc2e6 127 //------------------------------------------------------------------------------------------------------------------
bieleluk 0:e36b454cc2e6 128 void Debug_serial::print_3_breaks(int line_number){
bieleluk 0:e36b454cc2e6 129 pc.printf("\e[s");
bieleluk 0:e36b454cc2e6 130 wait_ms(50);
bieleluk 0:e36b454cc2e6 131 breakpoint_count++;
bieleluk 0:e36b454cc2e6 132 break_line[2] = break_line[1];
bieleluk 0:e36b454cc2e6 133 break_line[1] = break_line[0];
bieleluk 0:e36b454cc2e6 134 break_line[0] = line_number;
bieleluk 0:e36b454cc2e6 135 clear_from_n_up_to_m(12,3);
bieleluk 0:e36b454cc2e6 136 int i = (breakpoint_count>=3)?3:breakpoint_count;
bieleluk 0:e36b454cc2e6 137 while (i > 0){
bieleluk 0:e36b454cc2e6 138 if (i == 1 ){
bieleluk 0:e36b454cc2e6 139 pc.printf("\e[1m\e[96;40m");
bieleluk 0:e36b454cc2e6 140 print_one_break(i);
bieleluk 0:e36b454cc2e6 141 pc.printf("\e[22m\e[97;40m");
bieleluk 0:e36b454cc2e6 142 }else{
bieleluk 0:e36b454cc2e6 143 print_one_break(i);
bieleluk 0:e36b454cc2e6 144 }
bieleluk 0:e36b454cc2e6 145 i--;
bieleluk 0:e36b454cc2e6 146 }
bieleluk 0:e36b454cc2e6 147 pc.printf("\e[32;40mto continue press any button\e[97;40m");
bieleluk 0:e36b454cc2e6 148 pc.getc();
bieleluk 0:e36b454cc2e6 149 pc.printf("\r\e[2K\e[31;40mprogram is running\e[97;40m\n\r");
bieleluk 0:e36b454cc2e6 150 pc.printf("\e[u");
bieleluk 0:e36b454cc2e6 151
bieleluk 0:e36b454cc2e6 152 }
bieleluk 0:e36b454cc2e6 153
bieleluk 0:e36b454cc2e6 154 // print one breakpoint
bieleluk 0:e36b454cc2e6 155 //------------------------------------------------------------------------------------------------------------------
bieleluk 0:e36b454cc2e6 156 void Debug_serial::print_one_break(int n){
bieleluk 0:e36b454cc2e6 157 if (break_line[n-1] < 0){
bieleluk 0:e36b454cc2e6 158 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 159 }else{
bieleluk 0:e36b454cc2e6 160 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 161 }
bieleluk 0:e36b454cc2e6 162 }