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

debug_register.cpp

Committer:
bieleluk
Date:
2019-05-06
Revision:
2:478ba8b83e3f
Parent:
0:e36b454cc2e6
Child:
3:3d7837ae4a37

File content as of revision 2:478ba8b83e3f:

#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 offset){

    pc.printf("\e[s");
    wait_ms(50);
    breakpoint_count++;
    clear_from_n_up_to_m(12,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);
    }
    
    uint32_t reg = read_word(address, offset);
    pc.printf("---------------------------------------------------------------------------------------------------------\n\r");    
    pc.printf("| address decimal %10u | address hex 0x%8x | value decimal %10u | value hex 0x%8x |\n\r",address+offset,address+offset, 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 any button\e[97;40m");
    pc.getc();
    pc.printf("\r\e[2K\e[31;40mprogram is running\e[97;40m\n\r");  
    pc.printf("\e[u");
}



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

}