Debugging tool for mbed enabled microcontrollers, especially for NUCLEO-F303RE and STM32F042F6P6.
debug_register.cpp@14:22a3b68860af, 2019-05-13 (annotated)
- Committer:
- bieleluk
- Date:
- Mon May 13 11:04:54 2019 +0000
- Revision:
- 14:22a3b68860af
- Parent:
- 13:92533bcf6542
- Child:
- 15:83d4dced2a28
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 | 3:3d7837ae4a37 | 3 | |
bieleluk | 0:e36b454cc2e6 | 4 | // create object of class Debug_register |
bieleluk | 0:e36b454cc2e6 | 5 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 6 | Debug_register::Debug_register(PinName tx_pin, PinName rx_pin, int baudrate) : pc(tx_pin,rx_pin, baudrate) { |
bieleluk | 0:e36b454cc2e6 | 7 | init(); |
bieleluk | 0:e36b454cc2e6 | 8 | } |
bieleluk | 0:e36b454cc2e6 | 9 | |
bieleluk | 0:e36b454cc2e6 | 10 | // init function |
bieleluk | 0:e36b454cc2e6 | 11 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 12 | void Debug_register::init() { |
bieleluk | 12:a8ab6e018422 | 13 | pc.printf("\ec"); //clear entire screen |
bieleluk | 12:a8ab6e018422 | 14 | wait_ms(50); //wait until clearing is done |
bieleluk | 12:a8ab6e018422 | 15 | pc.printf("-----------------\n\r|\e[1m\e[97;40m\e[93;40mBREAKPOINT AREA\e[22m\e[97;40m|\n\r-----------------\n\r\033[s"); //print breakpoint label |
bieleluk | 0:e36b454cc2e6 | 16 | pc.printf("serial successfully initialised\n\r\e[32;40mto start program press any button\e[97;40m"); |
bieleluk | 0:e36b454cc2e6 | 17 | breakpoint_count=0; |
bieleluk | 12:a8ab6e018422 | 18 | pc.getc(); // wait until user sends any character |
bieleluk | 12:a8ab6e018422 | 19 | pc.printf("\r\e[2K\e[31;40mprogram is running\e[97;40m\r"); //running program message |
bieleluk | 12:a8ab6e018422 | 20 | 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"); //print serial port label |
bieleluk | 0:e36b454cc2e6 | 21 | } |
bieleluk | 0:e36b454cc2e6 | 22 | |
bieleluk | 0:e36b454cc2e6 | 23 | |
bieleluk | 0:e36b454cc2e6 | 24 | // perform one breakpoint and print one register |
bieleluk | 0:e36b454cc2e6 | 25 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 6:1ee26b7b9c2f | 26 | void Debug_register::breakpoint(int line_number, uint32_t address){ |
bieleluk | 3:3d7837ae4a37 | 27 | |
bieleluk | 12:a8ab6e018422 | 28 | uint32_t reg; //value of register |
bieleluk | 13:92533bcf6542 | 29 | uint32_t tmp; //help variable |
bieleluk | 12:a8ab6e018422 | 30 | char sign; //received character |
bieleluk | 0:e36b454cc2e6 | 31 | |
bieleluk | 12:a8ab6e018422 | 32 | address -= address%4; //if address is not divisible by 4, set address to the closest lower number divisible by 4 |
bieleluk | 3:3d7837ae4a37 | 33 | |
bieleluk | 12:a8ab6e018422 | 34 | pc.printf("\e[s");//save actual cursor position |
bieleluk | 12:a8ab6e018422 | 35 | wait_ms(50);//wait until the position is saved |
bieleluk | 3:3d7837ae4a37 | 36 | |
bieleluk | 0:e36b454cc2e6 | 37 | breakpoint_count++; |
bieleluk | 3:3d7837ae4a37 | 38 | |
bieleluk | 12:a8ab6e018422 | 39 | while(1){ //endless cycle until Enter is pressed |
bieleluk | 12:a8ab6e018422 | 40 | address -= address%4; //if address is not divisible by 4, set address to the closest lower number divisible by 4 |
bieleluk | 12:a8ab6e018422 | 41 | clear_from_n_up_to_m(13,3); //clear breakpoint area |
bieleluk | 3:3d7837ae4a37 | 42 | |
bieleluk | 12:a8ab6e018422 | 43 | if (line_number < 0){ //print breakpoint and line number |
bieleluk | 3:3d7837ae4a37 | 44 | pc.printf(" Breakpoint number %d\t unknown line number\n\r",breakpoint_count); |
bieleluk | 3:3d7837ae4a37 | 45 | }else{ |
bieleluk | 3:3d7837ae4a37 | 46 | pc.printf("| Breakpoint number %d\tline number %d\n\r",breakpoint_count, line_number); |
bieleluk | 3:3d7837ae4a37 | 47 | } |
bieleluk | 3:3d7837ae4a37 | 48 | |
bieleluk | 12:a8ab6e018422 | 49 | reg = read_word(address, 0); // read register value from address shifted by offset 0 |
bieleluk | 3:3d7837ae4a37 | 50 | |
bieleluk | 12:a8ab6e018422 | 51 | //print entire breakpoint |
bieleluk | 3:3d7837ae4a37 | 52 | pc.printf("---------------------------------------------------------------------------------------------------------\n\r"); |
bieleluk | 11:90164cad21ed | 53 | pc.printf("| address hex 0x%8x | value hex 0x%8x | address decimal %10u | value decimal %10u |\n\r", address, reg, address, reg); |
bieleluk | 3:3d7837ae4a37 | 54 | pc.printf("---------------------------------------------------------------------------------------------------------\n\r"); |
bieleluk | 3:3d7837ae4a37 | 55 | pc.printf("|bit_num|31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|\n\r"); |
bieleluk | 3:3d7837ae4a37 | 56 | pc.printf("---------------------------------------------------------------------------------------------------------\n\r|bit_val|"); |
bieleluk | 12:a8ab6e018422 | 57 | // print register value in binary form |
bieleluk | 3:3d7837ae4a37 | 58 | for (int i = 0; i < 32; i++){ |
bieleluk | 3:3d7837ae4a37 | 59 | pc.printf("%2d|",reg >> (31 - i)); |
bieleluk | 3:3d7837ae4a37 | 60 | reg -= ((reg >> (31 - i)) << (31 - i)); |
bieleluk | 3:3d7837ae4a37 | 61 | } |
bieleluk | 3:3d7837ae4a37 | 62 | pc.printf("\n\r---------------------------------------------------------------------------------------------------------\n\r"); |
bieleluk | 3:3d7837ae4a37 | 63 | |
bieleluk | 3:3d7837ae4a37 | 64 | |
bieleluk | 12:a8ab6e018422 | 65 | pc.printf("\e[32;40mto continue press enter or p, to see higher press i, to see lower press j\n\rto read register press r, to write to register press w\e[97;40m"); |
bieleluk | 3:3d7837ae4a37 | 66 | sign = pc.getc(); |
bieleluk | 12:a8ab6e018422 | 67 | if (sign == 13 || sign == 'P' || sign == 'p'){ // if P or enter was pressed, end the breakpoint |
bieleluk | 3:3d7837ae4a37 | 68 | pc.printf("\r\e[2K\e[31;40mprogram is running\e[97;40m\n\r"); |
bieleluk | 12:a8ab6e018422 | 69 | pc.printf("\e[u"); // return cursor position to serial port area |
bieleluk | 3:3d7837ae4a37 | 70 | break; |
bieleluk | 12:a8ab6e018422 | 71 | }else if (sign == 'i' || sign == 'I'){ // if i was pressed, show register on higher address |
bieleluk | 3:3d7837ae4a37 | 72 | pc.printf("\e[u"); |
bieleluk | 12:a8ab6e018422 | 73 | address += 0x4; // return cursor position to serial port area |
bieleluk | 3:3d7837ae4a37 | 74 | continue; |
bieleluk | 12:a8ab6e018422 | 75 | }else if (sign == 'k' || sign == 'K'){ // if i was pressed, show register on higher address |
bieleluk | 3:3d7837ae4a37 | 76 | pc.printf("\e[u"); |
bieleluk | 12:a8ab6e018422 | 77 | address -= 0x4; // return cursor position to serial port area |
bieleluk | 3:3d7837ae4a37 | 78 | continue; |
bieleluk | 12:a8ab6e018422 | 79 | }else if (sign == 'r' || sign == 'R'){ // if r was pressed, insert new address value |
bieleluk | 13:92533bcf6542 | 80 | pc.printf("\r\e[K\e[12;0H\r\e[K\e[32;40m use l or j to move the cursor, esc to delete value or enter to continue\e[97;40m"); |
bieleluk | 12:a8ab6e018422 | 81 | //pc.printf("\e[6;46H"); //move cursor to 6th row 46th column |
bieleluk | 12:a8ab6e018422 | 82 | address = modify_value(address, 17); // insert or modify actual address(starting in 17th column) to read register value from it |
bieleluk | 3:3d7837ae4a37 | 83 | continue; |
bieleluk | 12:a8ab6e018422 | 84 | }else if (sign == 'w' || sign == 'W'){ // if r was pressed, insert address value and register value to write register value on that address |
bieleluk | 13:92533bcf6542 | 85 | pc.printf("\r\e[K\e[12;0H\r\e[K\e[32;40m use l or j to move the cursor, esc to delete value or enter to continue\e[97;40m"); |
bieleluk | 12:a8ab6e018422 | 86 | //pc.printf("\e[6;46H"); |
bieleluk | 12:a8ab6e018422 | 87 | address = modify_value(address, 17); // insert or modify actual address(starting in 17th column) to write register value on that address |
bieleluk | 14:22a3b68860af | 88 | address -= address%4; |
bieleluk | 14:22a3b68860af | 89 | pc.printf("\e[6;17H"); |
bieleluk | 14:22a3b68860af | 90 | pc.printf("%8x",address); |
bieleluk | 13:92533bcf6542 | 91 | pc.printf("\e[12;0H\r\e[K\e[32;40m use l or j to move the cursor, esc to delete value or enter to continue\e[97;40m"); |
bieleluk | 12:a8ab6e018422 | 92 | //pc.printf("\e[6;96H"); |
bieleluk | 12:a8ab6e018422 | 93 | reg = *((volatile unsigned int *)address); //read register value from address |
bieleluk | 13:92533bcf6542 | 94 | |
bieleluk | 13:92533bcf6542 | 95 | tmp = reg; |
bieleluk | 13:92533bcf6542 | 96 | pc.printf("\e[10;10H"); |
bieleluk | 13:92533bcf6542 | 97 | for (int i = 0; i < 32; i++){ |
bieleluk | 13:92533bcf6542 | 98 | pc.printf("%2d|",tmp >> (31 - i)); |
bieleluk | 13:92533bcf6542 | 99 | tmp -= ((tmp >> (31 - i)) << (31 - i)); |
bieleluk | 13:92533bcf6542 | 100 | } |
bieleluk | 13:92533bcf6542 | 101 | |
bieleluk | 12:a8ab6e018422 | 102 | reg = modify_value(reg, 40); // insert or modify actual register value(starting in 40th column) to that write register value on address |
bieleluk | 12:a8ab6e018422 | 103 | *((volatile unsigned int *)address) = reg; // write new register value on the address |
bieleluk | 3:3d7837ae4a37 | 104 | continue; |
bieleluk | 3:3d7837ae4a37 | 105 | }else{ |
bieleluk | 3:3d7837ae4a37 | 106 | } |
bieleluk | 0:e36b454cc2e6 | 107 | } |
bieleluk | 0:e36b454cc2e6 | 108 | } |
bieleluk | 0:e36b454cc2e6 | 109 | |
bieleluk | 0:e36b454cc2e6 | 110 | // print formatted string to debug serial port |
bieleluk | 0:e36b454cc2e6 | 111 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 112 | int Debug_register::printf(const char* format, ...){ |
bieleluk | 12:a8ab6e018422 | 113 | |
bieleluk | 12:a8ab6e018422 | 114 | int ret = pc.printf(format); |
bieleluk | 12:a8ab6e018422 | 115 | return ret; |
bieleluk | 12:a8ab6e018422 | 116 | } |
bieleluk | 0:e36b454cc2e6 | 117 | |
bieleluk | 12:a8ab6e018422 | 118 | // read formatted string from debug serial port |
bieleluk | 12:a8ab6e018422 | 119 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 12:a8ab6e018422 | 120 | int Debug_register::scanf(const char* format, ...){ |
bieleluk | 12:a8ab6e018422 | 121 | |
bieleluk | 12:a8ab6e018422 | 122 | int ret = pc.scanf(format); |
bieleluk | 0:e36b454cc2e6 | 123 | return ret; |
bieleluk | 0:e36b454cc2e6 | 124 | } |
bieleluk | 0:e36b454cc2e6 | 125 | |
bieleluk | 0:e36b454cc2e6 | 126 | // print character to debug serial port |
bieleluk | 0:e36b454cc2e6 | 127 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 128 | int Debug_register::putc(int character){ |
bieleluk | 0:e36b454cc2e6 | 129 | return pc.putc(character); |
bieleluk | 0:e36b454cc2e6 | 130 | } |
bieleluk | 0:e36b454cc2e6 | 131 | |
bieleluk | 0:e36b454cc2e6 | 132 | // read character from debug serial port |
bieleluk | 0:e36b454cc2e6 | 133 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 134 | int Debug_register::getc(){ |
bieleluk | 0:e36b454cc2e6 | 135 | return pc.getc(); |
bieleluk | 0:e36b454cc2e6 | 136 | } |
bieleluk | 0:e36b454cc2e6 | 137 | |
bieleluk | 12:a8ab6e018422 | 138 | // check whether there is any character to read |
bieleluk | 12:a8ab6e018422 | 139 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 140 | bool Debug_register::readable(){ |
bieleluk | 0:e36b454cc2e6 | 141 | return pc.readable(); |
bieleluk | 0:e36b454cc2e6 | 142 | } |
bieleluk | 0:e36b454cc2e6 | 143 | |
bieleluk | 12:a8ab6e018422 | 144 | // check whether it is possible to write a sign to debug serial port |
bieleluk | 12:a8ab6e018422 | 145 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 2:478ba8b83e3f | 146 | bool Debug_register::writable(){ |
bieleluk | 2:478ba8b83e3f | 147 | return pc.writable(); |
bieleluk | 2:478ba8b83e3f | 148 | } |
bieleluk | 2:478ba8b83e3f | 149 | |
bieleluk | 0:e36b454cc2e6 | 150 | // clear screen from m line up to n line |
bieleluk | 0:e36b454cc2e6 | 151 | //------------------------------------------------------------------------------------------------------------------ |
bieleluk | 0:e36b454cc2e6 | 152 | void Debug_register::clear_from_n_up_to_m(int m, int n){ |
bieleluk | 0:e36b454cc2e6 | 153 | pc.printf("\033[%d;0H",m); |
bieleluk | 0:e36b454cc2e6 | 154 | wait(0.1); |
bieleluk | 0:e36b454cc2e6 | 155 | while (m > n){ |
bieleluk | 0:e36b454cc2e6 | 156 | m--; |
bieleluk | 0:e36b454cc2e6 | 157 | pc.printf("\033[K\033[%d;0H",m); |
bieleluk | 0:e36b454cc2e6 | 158 | } |
bieleluk | 0:e36b454cc2e6 | 159 | pc.printf("\n\r"); |
bieleluk | 0:e36b454cc2e6 | 160 | |
bieleluk | 0:e36b454cc2e6 | 161 | } |
bieleluk | 0:e36b454cc2e6 | 162 | |
bieleluk | 3:3d7837ae4a37 | 163 | uint32_t Debug_register::modify_value(uint32_t value, int horizontal){ |
bieleluk | 3:3d7837ae4a37 | 164 | |
bieleluk | 3:3d7837ae4a37 | 165 | uint32_t tmp = value; |
bieleluk | 3:3d7837ae4a37 | 166 | char val_str[8]; |
bieleluk | 3:3d7837ae4a37 | 167 | int horizontal_min = horizontal; |
bieleluk | 3:3d7837ae4a37 | 168 | char sign; |
bieleluk | 3:3d7837ae4a37 | 169 | |
bieleluk | 9:59344a584e18 | 170 | ONCE_AGAIN: |
bieleluk | 9:59344a584e18 | 171 | pc.printf("\e[6;%dH", horizontal); |
bieleluk | 3:3d7837ae4a37 | 172 | for (int i = 0; i < 8; i++){ |
bieleluk | 3:3d7837ae4a37 | 173 | if ((tmp >> (28 - 4*i)) > 9){ |
bieleluk | 3:3d7837ae4a37 | 174 | val_str[i] = ((tmp >> (28 - 4*i)) + 87); |
bieleluk | 3:3d7837ae4a37 | 175 | }else{ |
bieleluk | 3:3d7837ae4a37 | 176 | val_str[i] = ((tmp >> (28 - 4*i)) + 48); |
bieleluk | 3:3d7837ae4a37 | 177 | } |
bieleluk | 9:59344a584e18 | 178 | pc.putc(val_str[i]); |
bieleluk | 3:3d7837ae4a37 | 179 | tmp -= ((tmp >> (28 - 4*i)) << (28 - 4*i)) ; |
bieleluk | 3:3d7837ae4a37 | 180 | } |
bieleluk | 9:59344a584e18 | 181 | pc.printf("\e[6;%dH", horizontal); |
bieleluk | 9:59344a584e18 | 182 | |
bieleluk | 3:3d7837ae4a37 | 183 | |
bieleluk | 13:92533bcf6542 | 184 | |
bieleluk | 3:3d7837ae4a37 | 185 | while(1){ |
bieleluk | 3:3d7837ae4a37 | 186 | sign = pc.getc(); |
bieleluk | 3:3d7837ae4a37 | 187 | if (sign == 13){ |
bieleluk | 3:3d7837ae4a37 | 188 | value = (uint32_t)strtol(val_str, NULL, 16); |
bieleluk | 3:3d7837ae4a37 | 189 | break; |
bieleluk | 3:3d7837ae4a37 | 190 | }else if(sign == 'l' || sign == 'L'){ |
bieleluk | 3:3d7837ae4a37 | 191 | horizontal = min(horizontal+1,horizontal_min+7); |
bieleluk | 3:3d7837ae4a37 | 192 | pc.printf("\e[6;%dH",horizontal ); |
bieleluk | 3:3d7837ae4a37 | 193 | continue; |
bieleluk | 3:3d7837ae4a37 | 194 | }else if(sign == 'j' || sign == 'J'){ |
bieleluk | 3:3d7837ae4a37 | 195 | horizontal = max(horizontal-1,horizontal_min); |
bieleluk | 3:3d7837ae4a37 | 196 | pc.printf("\e[6;%dH", horizontal); |
bieleluk | 3:3d7837ae4a37 | 197 | continue; |
bieleluk | 3:3d7837ae4a37 | 198 | }else if ( ((sign >= 48 && sign <= 57) || (sign >= 65 && sign <= 70) || (sign >= 97 && sign <= 102)) && horizontal <= horizontal_min + 7 ){ |
bieleluk | 3:3d7837ae4a37 | 199 | if (sign >= 65 && sign <= 70){ |
bieleluk | 3:3d7837ae4a37 | 200 | val_str[horizontal - horizontal_min] = sign+32; |
bieleluk | 3:3d7837ae4a37 | 201 | pc.putc(sign+32); |
bieleluk | 3:3d7837ae4a37 | 202 | }else{ |
bieleluk | 3:3d7837ae4a37 | 203 | val_str[horizontal - horizontal_min] = sign; |
bieleluk | 3:3d7837ae4a37 | 204 | pc.putc(sign); |
bieleluk | 3:3d7837ae4a37 | 205 | } |
bieleluk | 3:3d7837ae4a37 | 206 | if (horizontal == (horizontal_min+7)){ |
bieleluk | 3:3d7837ae4a37 | 207 | pc.putc('\b'); |
bieleluk | 3:3d7837ae4a37 | 208 | }else{ |
bieleluk | 3:3d7837ae4a37 | 209 | horizontal++; |
bieleluk | 3:3d7837ae4a37 | 210 | } |
bieleluk | 3:3d7837ae4a37 | 211 | continue; |
bieleluk | 9:59344a584e18 | 212 | }else if(sign == 27){ |
bieleluk | 9:59344a584e18 | 213 | horizontal = horizontal_min; |
bieleluk | 9:59344a584e18 | 214 | tmp = value; |
bieleluk | 9:59344a584e18 | 215 | goto ONCE_AGAIN; |
bieleluk | 3:3d7837ae4a37 | 216 | } |
bieleluk | 3:3d7837ae4a37 | 217 | } |
bieleluk | 3:3d7837ae4a37 | 218 | return value; |
bieleluk | 3:3d7837ae4a37 | 219 | } |