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

debug_register.cpp

Committer:
bieleluk
Date:
2019-05-09
Revision:
6:1ee26b7b9c2f
Parent:
3:3d7837ae4a37
Child:
9:59344a584e18

File content as of revision 6:1ee26b7b9c2f:

#include "Debug.h"


// create object of class Debug_register
//------------------------------------------------------------------------------------------------------------------ 
Debug_register::Debug_register(PinName tx_pin, PinName rx_pin, int baudrate) : pc(tx_pin,rx_pin, baudrate) {
    init();
}

// init function
//------------------------------------------------------------------------------------------------------------------ 
void Debug_register::init() {
    pc.printf("\ec");
    wait_ms(50);
    pc.printf("-----------------\n\r|\e[1m\e[97;40m\e[93;40mBREAKPOINT AREA\e[22m\e[97;40m|\n\r-----------------\n\r\033[s");
    pc.printf("serial successfully initialised\n\r\e[32;40mto start program press any button\e[97;40m");
    breakpoint_count=0;
    pc.getc();
    pc.printf("\r\e[2K\e[31;40mprogram is running\e[97;40m\r");

    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");
}




// perform one breakpoint and print one register
//------------------------------------------------------------------------------------------------------------------  
void Debug_register::breakpoint(int line_number, uint32_t address){
    
    uint32_t reg;
    
    address -= address%4;
    //int horizontal_location = 46;
    


    
    char sign;
    pc.printf("\e[s");
    wait_ms(50);
    breakpoint_count++;
 
    while(1){
        address -= address%4;
        clear_from_n_up_to_m(13,3);
        
        if (line_number < 0){
            pc.printf(" Breakpoint number %d\t unknown line number\n\r",breakpoint_count);
        }else{
            pc.printf("| Breakpoint number %d\tline number %d\n\r",breakpoint_count, line_number);
        }
        
        reg = read_word(address, 0);
        //reg = *((volatile unsigned int *)address);
      
        pc.printf("---------------------------------------------------------------------------------------------------------\n\r");    
        pc.printf("| address decimal %10u | address hex 0x%8x | value decimal %10u | value hex 0x%8x |\n\r",address,address, reg, reg);
        pc.printf("---------------------------------------------------------------------------------------------------------\n\r");
        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");
        pc.printf("---------------------------------------------------------------------------------------------------------\n\r|bit_val|");
        for (int i = 0; i < 32; i++){
            pc.printf("%2d|",reg >> (31 - i));
            reg -= ((reg >> (31 - i)) << (31 - i));
        }
        pc.printf("\n\r---------------------------------------------------------------------------------------------------------\n\r");
        
        
        pc.printf("\e[32;40mto continue press enter, 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");
        sign = pc.getc();
        if (sign == 13 || sign == 'P' || sign == 'p'){
            pc.printf("\r\e[2K\e[31;40mprogram is running\e[97;40m\n\r");  
            pc.printf("\e[u");
            break;
        }else if (sign == 'i' || sign == 'I'){
            pc.printf("\e[u");
            address += 0x4;
            continue; 
        }else if (sign == 'k' || sign == 'K'){
            pc.printf("\e[u");
            address -= 0x4;
            continue; 
        }else if (sign == 'r' || sign == 'R'){
            pc.printf("\r\e[K\e[12;0H\r\e[K\e[32;40m use l or j to xyzzzzzzzzx\e[97;40m");
            pc.printf("\e[6;46H");
            address = modify_value(address, 46);
            continue;            
        }else if (sign == 'w' || sign == 'W'){
            pc.printf("\r\e[K\e[12;0H\r\e[K\e[32;40m use l or j to xxxxxxxxxxx\e[97;40m");
            pc.printf("\e[6;46H");
            address = modify_value(address, 46); 
            pc.printf("\e[12;0H\r\e[K\e[32;40m use l or j to yyyyyyyx\e[97;40m");  
            pc.printf("\e[6;96H");
            reg = modify_value(reg, 96); 
            *((volatile unsigned int *)address) = reg;
            continue;
        }else{
        }
    }
}



// print formatted string to debug serial port
//------------------------------------------------------------------------------------------------------------------ 
int Debug_register::printf(const char* format, ...){

    int ret = pc.printf(format);
    return ret;
}

// print character to debug serial port
//------------------------------------------------------------------------------------------------------------------ 
int Debug_register::putc(int character){

    return pc.putc(character);
}

// read character from debug serial port
//------------------------------------------------------------------------------------------------------------------ 
int Debug_register::getc(){
    return pc.getc();
}

bool Debug_register::readable(){
    return pc.readable();
}

bool Debug_register::writable(){
    return pc.writable();
}

// clear screen from m line up to n line
//------------------------------------------------------------------------------------------------------------------
void Debug_register::clear_from_n_up_to_m(int m, int n){
    pc.printf("\033[%d;0H",m);
    wait(0.1);
    while (m > n){ 
        m--;
        pc.printf("\033[K\033[%d;0H",m);
    }
    pc.printf("\n\r");

}

uint32_t Debug_register::modify_value(uint32_t value, int horizontal){
    
    uint32_t tmp = value;
    char val_str[8];
    int horizontal_min = horizontal;
    char sign; 

    
    for (int i = 0; i < 8; i++){
        if ((tmp >> (28 - 4*i)) > 9){
            val_str[i] = ((tmp >> (28 - 4*i)) + 87);
        }else{
            val_str[i] = ((tmp >> (28 - 4*i)) + 48);
        }
        tmp -= ((tmp >> (28 - 4*i)) << (28 - 4*i)) ;
    }
    

    
    while(1){
        sign = pc.getc();
        if (sign == 13){
            value = (uint32_t)strtol(val_str, NULL, 16);
            break;
        }else if(sign == 'l' || sign == 'L'){
            horizontal = min(horizontal+1,horizontal_min+7);
            pc.printf("\e[6;%dH",horizontal );
            continue;
        }else if(sign == 'j' || sign == 'J'){
            horizontal = max(horizontal-1,horizontal_min);
            pc.printf("\e[6;%dH", horizontal);
            continue;
        }else if ( ((sign >= 48 && sign <= 57) || (sign >= 65 && sign <= 70) || (sign >= 97 && sign <= 102)) && horizontal <= horizontal_min + 7 ){
            if (sign >= 65 && sign <= 70){
                val_str[horizontal - horizontal_min] = sign+32;
                pc.putc(sign+32);
            }else{
                val_str[horizontal - horizontal_min] = sign;
                pc.putc(sign);
            }
            if (horizontal == (horizontal_min+7)){
                pc.putc('\b');
            }else{
                horizontal++;
            }
            continue;
        }
    }
    return value;
}