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

Debug.h

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

File content as of revision 8:a5570452154e:

#pragma once

// include files
//------------------------------------------------------------------------------------------------------------------
#include "mbed.h"
#include <stdlib.h>


// macros
//------------------------------------------------------------------------------------------------------------------
#define name(var)  #var 
#define min(a, b) (((a) < (b)) ? (a) : (b)) 
#define max(a, b) (((a) > (b)) ? (a) : (b)) 

// assembly functions
//------------------------------------------------------------------------------------------------------------------
extern "C" int check_1_bit(uint32_t gpiox, uint32_t offset, uint32_t pin);
extern "C" int check_2_bit(uint32_t gpiox, uint32_t offset, uint32_t pin);
extern "C" int check_alternative_mode(uint32_t gpiox, uint32_t offset, uint32_t pin);
extern "C" int read_word(uint32_t address, uint32_t offset);

// structs
//------------------------------------------------------------------------------------------------------------------
typedef struct Pin {
    char port;
    int number;
} pin_t;


/** Debug_serial class.
 *  Class for stepping programme and printing actual position of the running programme with optional print of one variable (int, float, char or char*).
 *  Functions printf, putc and getc are also defined in the class.
 *
 * Example program:
 * @code
 * // ----------------------------------------------------------------------------
 * // Author: Lukas Bielesch 
 * // Department of Measurement, Czech technical university in Prague, Czech Republic 
 * // Date of publication: 15. Apr 2019
 * // ----------------------------------------------------------------------------
 * #include "Debug.h"
 * AnalogIn analog(PA_5);
 * PwmOut pwm(PA_6);
 * DigitalOut out(PA_4);
 * Debug_serial pc(PA_2, PA_3, 115200); //
 * 
 * int main(){
 *     int var = 0;
 *     char character;
 *     out = 1;
 *     float pi = 3.14159265359;
 *     pc.breakpoint(__LINE__,name(pi),pi);
 *     char* arr = "this is string";
 *     pc.breakpoint(__LINE__,name(arr),arr);    
 *     pwm = 0.5;
 *     pwm.period(1);
 *     while(1){
 *         pc.breakpoint(__LINE__,name(var),var);
 *         pc.printf("insert character\n\r");
 *         while (!pc.readable()){}
 *         character = pc.getc();
 *         pc.printf("you have inserted %c\n\r",character);
 *         var++;
 *         pc.breakpoint(__LINE__, 0x48000000);
 *         wait(1);
 *     }
 * }
 * @endcode
 */
//------------------------------------------------------------------------------------------------------------------
class Debug_serial {
public:

    /** Create object of class Debug_serial
     * @param tx_pin TX pin of serial port of the board
     * @param rx_pin RX pin of serial port of the board
     * @param baudrate desired baudrate value of debug serial port
     */
    Debug_serial(PinName tx_pin, PinName rx_pin, int baudrate = 115200);
    
    /** Perform one breakpoint without printing variable
     * @param line_number line number of the breakpoint
     */ 
    void breakpoint(int line_number = -1);

    /** Perform one breakpoint and print variable of type int
     * @param line_number Line number of the breakpoint
     * @param name name of printed variable(max length is 19) 
     * @param variable
     */     
    void breakpoint(int line_number, char name[20], int variable);
    
    /** Perform one breakpoint and print variable of type char
     * @param line_number Line number of the breakpoint
     * @param name name of printed variable(max length is 19) 
     * @param variable
     */     
    void breakpoint(int line_number, char name[20], char variable);
    
    /** Perform one breakpoint and print variable of type string
     * @param line_number Line number of the breakpoint
     * @param name name of printed variable(max length is 19) 
     * @param variable
     */     
    void breakpoint(int line_number, char name[20], char * variable);
    
    /** Perform one breakpoint and print variable of type float
     * @param line_number Line number of the breakpoint
     * @param name name of printed variable(max length is 19) 
     * @param variable
     */ 
    void breakpoint(int line_number, char name[20], float variable);
    
    /** Perform one breakpoint and print one register value
     * @param line_number Line number of the breakpoint
     * @param address
     */ 
    void breakpoint(int line_number, uint32_t address);
    
   /** Print formatted string to debug serial port
     * @param string
     * @param format (optional)
     * @returns total number of printed characters or negative value if an output error or an encoding error
     */
    int printf(const char* format, ...);
    
    /** Print one character to debug serial port 
     * @param character
     * @returns character written as an unsigned char cast to an int
     */
    int putc(int character);
    
    /** Read one character from debug serial port 
    * @returns character written as an unsigned char cast to an int
    */
    int getc();
    
    bool readable();
    bool writable();
    
private:
    int break_line[3]; //store number of lines of three previous breakpoints
    char var[3][50]; //store variables of three previous breakpoints
  
protected:  
// objects:
    Serial pc; //debug serial device
// variables:
    int breakpoint_count; //stores number of the current breakpoint
// functions
    // initialization function    
    void init();
    // print 3 last breakpoints
    void print_3_breaks(int line_number);
    // print one breakpoint
    void print_one_break(int n);
    // clear screen from m line up to n line
    void clear_from_n_up_to_m(int m, int n);
};




/** Debug_led class.
 *  Class for stepping the program with debug LED and button, that is connected to GND(default) or VCC.
 *
 * Example program:
 * @code
 * // ----------------------------------------------------------------------------
 * // Author: Lukas Bielesch 
 * // Department of Measurement, Czech technical university in Prague, Czech Republic 
 * // Date of publication: 15. Apr 2019
 * // ----------------------------------------------------------------------------
 * #include "Debug.h"
 * AnalogIn analog(PA_0);
 * PwmOut pwm(PA_4);
 * DigitalOut out(PA_1);
 * Debug_led deb(PA_5, PA_6, "BUTTON_VDD"); //debug led on PA5, debug button connected to VDD on PA6
 * int main(){
 * 
 *     out = 1;
 *     deb.breakpoint(1);
 *     pwm = 0.5;
 *     pwm.period(1);
 *     deb.breakpoint(2);    
 * 
 *     while(1){
 *         deb.breakpoint();
 *         pwm = pwm + 0.1;
 *         wait(2);
 *     }
 * }
 * @endcode
 */
// class Debug_led
//------------------------------------------------------------------------------------------------------------------
class Debug_led {
public:

    /** Create object of class Debug_led
     * @param led_pin pin of of debug led
     * @param button_pin pin of of debug button
     * @param mode mode of button connection("BUTTON_GND", "BUTTON_VCC", "BUTTON_VDD")
     */
    Debug_led(PinName led_pin, PinName button_pin, char mode[11] = "BUTTON_GND");
    
    /** Perform one breakpoint
     * @param number number of flashes of LED during the breakpoint(optional)
     */
    void breakpoint(int number = -1);

private:  
// objects
    DigitalOut led; //debug led
    InterruptIn button; //debug button
// variables
    int button_mode; //mode of button 1->pullupt, 0->pulldown 
    volatile bool end_breakpoint;
    int number_of_breakpoints;
    
    /** Initialization */
    void init(char mode[11]);
    
    /** Blinks the debug led n-times with blink period  wait_time_ms */
    void flash_n_times(int wait_time_ms, int n);
    
    /** end the break after the button is pushed */
    void end_break();

    
};


/** Debug_register class.
 *
 * Example program:
 * @code
 * //------------------------------------------------------------------------------------------------------------------
 * // ----------------------------------------------------------------------------
 * // Author: Lukas Bielesch 
 * // Department of Measurement, Czech technical university in Prague, Czech Republic 
 * // Date of publication: 15. Apr 2019
 * // ----------------------------------------------------------------------------
 * #include "Debug.h"
 * AnalogIn analog(PA_5);
 * PwmOut pwm(PA_6);
 * DigitalOut out(PA_4);
 * Debug_register pc(PA_2, PA_3, 115200);
 *  
 * int main(){
 *     pc.breakpoint(__LINE__,0x48000000);
 *     DigitalOut out2 (PA_0);
 *     pc.breakpoint(__LINE__,0x48000000);
 *     AnalogIn analog2 (PA_1);
 *     pc.breakpoint(__LINE__,0x4800000C);
 *     DigitalIn di1(PA_7, PullUp);
 *     pc.breakpoint(__LINE__,0x4800000C);
 *     pc.breakpoint(__LINE__,0x48000004);
 *     while(1){
 *         if(pc.readable()){
 *             pc.putc(pc.getc());
 *         }
 *         wait(0.1);
 *     }
 * }
 * @endcode
 */
class Debug_register {
public:

    /** Create object of class Debug_serial
     * @param tx_pin TX pin of serial port of the board
     * @param rx_pin RX pin of serial port of the board
     * @param baudrate desired baudrate value of debug serial port
     */
    Debug_register(PinName tx_pin, PinName rx_pin, int baudrate = 115200);
    
    /** Perform one breakpoint and print one register
     * @param line_number line number of the breakpoint
     * @param address
     */ 
    void breakpoint(int line_number = -1, uint32_t address = 0);

    
    
   /** Print formatted string to debug serial port
     * @param string
     * @param format (optional)
     * @returns total number of printed characters or negative value if an output error or an encoding error
     */
    int printf(const char* format, ...);
    
    /** Print one character to debug serial port 
     * @param character
     * @returns character written as an unsigned char cast to an int
     */
    int putc(int character);
    
    /** Read one character from debug serial port 
    * @returns character written as an unsigned char cast to an int
    */
    int getc();
    
    bool readable();
    bool writable();
  
protected:  
// objects:
    Serial pc; //debug serial device
// variables:
    int breakpoint_count; //stores number of the current breakpoint
// functions
    // initialization function    
    void init();
    // clear screen from m line up to n line
    void clear_from_n_up_to_m(int m, int n);
    uint32_t modify_value(uint32_t value, int horizontal);

};




/** Debug_register_print class.
 *
 * Example program:
 * @code
 * //------------------------------------------------------------------------------------------------------------------
 * // ----------------------------------------------------------------------------
 * // Author: Lukas Bielesch 
 * // Department of Measurement, Czech technical university in Prague, Czech Republic 
 * // Date of publication: 15. Apr 2019
 * // ----------------------------------------------------------------------------
 * #include "Debug.h"
 * AnalogIn analog(PA_5);
 * PwmOut pwm(PA_6);
 * DigitalOut out(PA_4);
 * Debug_register_print pc(PA_2, PA_3, 115200);
 *  
 * int main(){
 *     pc.format(2,2,1,3);//breakpoint count,line number, address, value
 *     pc.breakpoint(__LINE__,0x48000000, 2);
 *     DigitalOut out2 (PA_0);
 *     pc.breakpoint(__LINE__,0x48000014, -3);
 *     AnalogIn analog2 (PA_1);
 *     pc.breakpoint(__LINE__,0x48000008);
 * 
 *     while(1){
 *         wait(1);
 *     }
 * }
 * @endcode
 */
class Debug_register_print {
public:

    /** Create object of class Debug_serial
     * @param tx_pin TX pin of serial port of the board
     * @param rx_pin RX pin of serial port of the board
     * @param baudrate desired baudrate value of debug serial port
     */
    Debug_register_print(PinName tx_pin, PinName rx_pin, int baudrate = 115200);


    void format(int break_number = 2, int line = 2, int address = 1, int value = 1);
    
    /** Perform one breakpoint and print one register
     * @param line_number line number of the breakpoint
     * @param address
     * @param offset
     */ 
    void breakpoint(int line_number = -1, uint32_t address = 0, int number_of_words = 1);

  
protected:  
// objects:
    Serial pc; //debug serial device
// variables:
    int breakpoint_count; //stores number of the current breakpoint
    int count_format;
    int line_format; 
    int address_format;
    int register_format;
// functions
    // initialization function    
    void init();

};