debug tool for STM32F042F6P6

Debug.h

Committer:
bieleluk
Date:
2019-04-17
Revision:
13:3d959f23fd8f
Parent:
12:5e618e97cb24
Child:
14:5a3b0cabbcd4

File content as of revision 13:3d959f23fd8f:

#pragma once

// include files
//------------------------------------------------------------------------------------------------------------------
#include "mbed.h"

// macros
//------------------------------------------------------------------------------------------------------------------
#define name(var)  #var 


// 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_complete class.
 *  Used for printing complete configuration of peripherals
 *
 * Example:
 * @code
 * // ----------------------------------------------------------------------------
 * // "THE BEER-WARE LICENSE" (Revision 42):
 * // <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
 * // can do whatever you want with this stuff. If we meet some day, and you think
 * // this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp
 * // ----------------------------------------------------------------------------
 * // Author: Lukas Bielesch 
 * // Department of Measurement, Czech technical university in Prague, Czech Republic 
 * // Date of publication: 15. Apr 2019
 * // ----------------------------------------------------------------------------
 * AnalogIn analog(PA_5);
 * PwmOut pwm(PA_6);
 * DigitalOut out(PA_4);
 * Debug_complete pc(PA_2, PA_3, 115200);
 *  
 * int main(){
 *     out = 1;
 *     pc.breakpoint(__LINE__); 
 *     pwm = 0.5;
 *     pwm.period(1);
 *     while(1){
 *         pc.breakpoint(__LINE__);
 *         wait(1);
 *         pc.breakpoint();        
 *         pc.printf("value of adc is %f*VDD\n\r",analog.read());
 *         pc.print_reg(0x48000000,0x0);
 *     }
 * }
 * @endcode
 */
class Debug_complete {
public:

    /** Create object of class Debug_complete
     * @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 serial port
     */
    Debug_complete(PinName tx_pin, PinName rx_pin, int baudrate = 115200);
     
    /** Perform one breakpoint
     * @param line_number line number of the breakpoint
     */ 
    void breakpoint(int line_number = -1);

    
    /** Print one 32-bit word from memory from address shifted by offset
     * @param address 
     * @param offset     
     */     
    void print_reg(uint32_t address, uint32_t offset = 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();


  
private:  
// objects
    Serial pc; //debug serial device
// variables
    int breakpoint_count; //stores number of the current breakpoint 
//functions
    // print alternate function of pin    
    void print_af_mode( char portx, int pin_number, int af_mode);
    // show configuration of pin
    void show_pin_config(pin_t pin);
    // print configuration of timer
    void show_tim_config(int timer);    
    // print configuration of pin in pwm output mode
    void show_pwm_config(int timer, int channel);
    // print configuration of pin in analog input mode    
    void show_analog_config(int channel);
    // print configuration of adc1 converter
    void show_adc1_config();
    //print configuration of board's clock
    void show_clk_config();
    // clear screen from m line up to n line
    void clear_from_n_up_to_m(int m, int n);   
    //initialization function   
    void init();
};

/** Debug_serial class.
 *  Used for printing for actual position of running program and one variable
 *
 * Example:
 * @code
 * // ----------------------------------------------------------------------------
 * // "THE BEER-WARE LICENSE" (Revision 42):
 * // <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
 * // can do whatever you want with this stuff. If we meet some day, and you think
 * // this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp
 * // ----------------------------------------------------------------------------
 * // Author: Lukas Bielesch 
 * // Department of Measurement, Czech technical university in Prague, Czech Republic 
 * // Date of publication: 15. Apr 2019
 * // ----------------------------------------------------------------------------
 * 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");
 *         character = pc.getc();
 *         pc.printf("you have inserted %c\n\r",character);
 *         var++;
 *         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);
    
   /** 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();
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.
 *  Used for stepping the program
 *
 * Example:
 * @code
 * // ----------------------------------------------------------------------------
 * // "THE BEER-WARE LICENSE" (Revision 42):
 * // <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
 * // can do whatever you want with this stuff. If we meet some day, and you think
 * // this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp
 * // ----------------------------------------------------------------------------
 * // Author: Lukas Bielesch 
 * // Department of Measurement, Czech technical university in Prague, Czech Republic 
 * // Date of publication: 15. Apr 2019
 * // ----------------------------------------------------------------------------
 * AnalogIn analog(PA_5);
 * PwmOut pwm(PA_6);
 * DigitalOut out(PA_4);
 * 
 * Debug_led deb(PA_5, PA_6, "BUTTON_VDD");
 * 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();

    
};