debug tool for STM32F042F6P6

Revision:
24:bc7c4e8f3fe0
Parent:
19:841a5c532145
Child:
27:e255eaf79cd2
--- a/debug_complete.cpp	Thu May 09 21:37:50 2019 +0000
+++ b/debug_complete.cpp	Mon May 27 00:15:37 2019 +0000
@@ -1,4 +1,5 @@
 #include "Debug.h"
+    // definition of important constants
 
 
 // create object of class Debug_led
@@ -13,14 +14,12 @@
 //------------------------------------------------------------------------------------------------------------------
 void Debug_complete::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("\ec"); // erase whole window
+    wait_ms(50); // wait until the erase is done
     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[60;0H------------------\n\r|\e[1m\e[93;40mSERIAL PORT AREA\e[22m\e[97;40m|\n\r------------------\n\r\033[s");
+    breakpoint_count=0; // set breakpoint count to 0
+    pc.getc(); // wait for character to continue
+    pc.printf("\r\e[2K\e[31;40mprogram is running\e[97;40m\r"); // continue in program
 }
 
 
@@ -28,12 +27,11 @@
 //------------------------------------------------------------------------------------------------------------------
 void Debug_complete::breakpoint(int line_number) {
     breakpoint_count++;
-    //clear_from_n_up_to_m(59,3);
-    pc.printf("\ec");
-    wait_ms(50);
-    line();
+    pc.printf("\ec"); // erase whole window
+    wait_ms(50); // wait until the erase is done
+    line(); // print line
     pc.printf("| Break number %d\t",breakpoint_count);
-    if (line_number == -1){
+    if (line_number == -1){ // if line number was not inserted
         pc.printf("unknown line \n\r");
     }else{
         pc.printf("line %d\n\r",line_number);    
@@ -44,53 +42,36 @@
     show_all_timers_config();
     show_all_pins_config();
     
-    
-        
     pc.printf("\n\r\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");
-        wait(0.1);
+    pc.getc(); // wait for character to continue
+    pc.printf("\r\e[2K\e[31;40mprogram is running\e[97;40m\n\r");  // continue in program
+
 }
 
-
-// show configuration of pin
+// show configuration of one pin
 //------------------------------------------------------------------------------------------------------------------
 void Debug_complete::show_pin_config(pin_t pin){
-    char portx = pin.port;
-    int pin_number = pin.number;
-     
-    // definition of important constants
-    const uint32_t AFRL_Offset = 0x20;
-    const uint32_t AFRH_Offset = 0x24;
-
-    const uint32_t MODER_Offset = 0x00;
-    const uint32_t PUPDR_Offset = 0x0C;
-    const uint32_t OSPEEDR_Offset = 0x08;
     
-    const uint32_t OTYPER_Offset = 0x04;
-    const uint32_t IDR_Offset = 0x10;
-    const uint32_t ODR_Offset = 0x14;
-
-    const uint32_t gpioa = 0x48000000;
-    const uint32_t gpiob = 0x48000400;
-    const uint32_t gpiof = 0x48001400;
+    char portx = pin.port; // port of pin
+    int pin_number = pin.number; // number of pin
+    uint32_t gpio_address;
+    uint32_t address_offset;
     
-    uint32_t reg_0;
+    // print pin port
     switch (portx){
         case 'A':
         case 'a':
-            reg_0 = gpioa;
+            gpio_address = gpioa;
             pc.printf("| PA");
             break;
         case 'B':
         case 'b':
-            reg_0 = gpiob;
+            gpio_address = gpiob;
             pc.printf("| PB");
             break;
         case 'f':
         case 'F':
-            reg_0 = gpiof;
+            gpio_address = gpiof;
             pc.printf("| PF");
             break;
         default:
@@ -98,19 +79,18 @@
             return;
 
     }    
-    pc.printf("%d\t| ",pin_number);
+    pc.printf("%d\t| ",pin_number); // print pin number
 
     // check the mode of pin
-    int return_value;
-    uint32_t reg_1 = MODER_Offset;
-    return_value = check_2_bit(reg_0,reg_1,pin_number);
+    address_offset = MODER_Offset;
+    int return_value = check_2_bit(gpio_address,address_offset,pin_number);
     
     // check other meaningful features depending on mode of the pin
-    if (return_value == 0){                     //input mode
+    if (return_value == 0){ //input mode
         pc.printf("input mode\t\t| ");
         // chceck pupdr register
-        reg_1 = PUPDR_Offset;
-        return_value = check_2_bit(reg_0,reg_1,pin_number);
+        address_offset = PUPDR_Offset;
+        return_value = check_2_bit(gpio_address,address_offset,pin_number);
         if (return_value == 0){ // pull none
             pc.printf("no resistor\t|\t\t");
         }else if (return_value == 1){ // pull up
@@ -123,62 +103,62 @@
         }
         pc.printf("| ");
         // check input value
-        reg_1 = IDR_Offset;
-        return_value = check_1_bit(reg_0,reg_1,pin_number);
-        if (return_value == 0){                         // log.0
+        address_offset = IDR_Offset;
+        return_value = check_1_bit(gpio_address,address_offset,pin_number);
+        if (return_value == 0){  // log.0
             pc.printf("log.0");
-        }else {                                         // log.1
+        }else {                  // log.1
             pc.printf("log.1");
         }
-        pc.printf(" |\n\r");         
-    }else if (return_value == 1){               //output mode
+        pc.printf(" |\n\r");  // go to next line       
+    }else if (return_value == 1){ //output mode
         pc.printf("output mode\t\t| ");
         
         // check output mode register
-        reg_1 = OTYPER_Offset;
-        return_value = check_1_bit(reg_0,reg_1,pin_number);
-        if (return_value == 0){                         // push pull
+        address_offset = OTYPER_Offset;
+        return_value = check_1_bit(gpio_address,address_offset,pin_number);
+        if (return_value == 0){        // push pull
             pc.printf("push pull\t");
-        }else{                                          // open drain
+        }else{                         // open drain
             pc.printf("open drain\t");
         } 
         pc.printf("| ");    
         
         // check speed of output pin
-        reg_1 = OSPEEDR_Offset;
-        return_value = check_2_bit(reg_0,reg_1,pin_number);
-        if (return_value == 0 || return_value == 2){    // low speed
+        address_offset = OSPEEDR_Offset;
+        return_value = check_2_bit(gpio_address,address_offset,pin_number);
+        if (return_value == 0 || return_value == 2){ // low speed
             pc.printf("low speed\t");
-        }else if (return_value == 1){                   // medium speed
+        }else if (return_value == 1){                // medium speed
             pc.printf("medium speed\t");
-        }else{                                          // high speed
+        }else{                                       // high speed
             pc.printf("high speed\t");
         }
         pc.printf("| ");    
         
         // check output value
-        reg_1 = ODR_Offset;
-        return_value = check_1_bit(reg_0,reg_1,pin_number);
-        if (return_value == 0){                         // log.0
+        address_offset = ODR_Offset;
+        return_value = check_1_bit(gpio_address,address_offset,pin_number);
+        if (return_value == 0){   // log.0
             pc.printf("log.0");
-        }else{                                          // log.1
+        }else{                    // log.1
             pc.printf("log.1");
         } 
-        pc.printf("\t|\n\r");
-    }else if(return_value == 2){                // alternate mode
+        pc.printf("\t|\n\r"); // go to next line
+    }else if(return_value == 2){  // alternate mode
         pc.printf("alternate mode\t| ");
         int af_mode;
         // chceck whether the pin belongs between the lower 8 pins and use AFRL register 
         // or he pin belongs between the higher 8 pins and use AFRH register 
         if (pin_number <= 7){
-            af_mode = check_alternative_mode(reg_0,AFRL_Offset,pin_number);    
+            af_mode = check_alternative_mode(gpio_address,AFRL_Offset,pin_number);    
         }else{
-            af_mode = check_alternative_mode(reg_0,AFRH_Offset,pin_number - 8);    
+            af_mode = check_alternative_mode(gpio_address,AFRH_Offset,pin_number - 8);    
         }  
         // print exact alternate functionality
         print_af_mode(portx, pin_number, af_mode);
 
-    }else{                                      //analog mode
+    }else{   //analog mode
         pc.printf("analog mode\t\t|");
         if ((portx == 'A' || portx == 'a') && pin_number >= 0 && pin_number <= 7){
             show_analog_config(pin_number);
@@ -322,18 +302,19 @@
 // show configuration of pin in analog mode 
 //------------------------------------------------------------------------------------------------------------------ 
 void Debug_complete::show_analog_config(int channel){
-        int config = ADC1->CHSELR;
-        ADC1->CR|=ADC_CR_ADSTP;
-        ADC1->CR &= !ADC_CR_ADSTART;
-        ADC1->CHSELR = 1 << channel;
+    int config = ADC1->CHSELR; // save actual configuration of adc 
+    // adc conversion on channel from parameter
+    ADC1->CR|=ADC_CR_ADSTP; // stop ongoing conversion
+    ADC1->CR &= !ADC_CR_ADSTART;
+    ADC1->CHSELR = 1 << channel; // select channel
 
-        ADC1->CR |= ADC_CR_ADEN;
-        while ( (ADC1->ISR & ADC_ISR_ADRDY) == 0){}
-        ADC1->CR |= ADC_CR_ADSTART; /* Start the ADC conversion */
-        while ((ADC1->ISR & ADC_ISR_EOC) == 0){}
-        
-        pc.printf("\t\t|\t\t| %d\t|\n\r",ADC1->DR);
-        ADC1->CHSELR = config;
+    ADC1->CR |= ADC_CR_ADEN; 
+    while ( (ADC1->ISR & ADC_ISR_ADRDY) == 0){}
+    ADC1->CR |= ADC_CR_ADSTART; // Start the ADC conversion
+    while ((ADC1->ISR & ADC_ISR_EOC) == 0){} // Wait end of conversion
+    
+    pc.printf("\t\t|\t\t| %d\t|\n\r",ADC1->DR);
+    ADC1->CHSELR = config; // restore configuratoin of adc
 }
 
 // print configuration of pin in pwm output mode
@@ -359,9 +340,9 @@
 // print configuration of timer
 //------------------------------------------------------------------------------------------------------------------
 void Debug_complete::show_tim_config(int timer){
-    int alignment;
-    int psc;
-    int arr;
+    int alignment; // alignment of PWM
+    int psc; // prescaler
+    int arr; // auto reload value
     if (timer == 1){
         psc = TIM1->PSC;
         arr = TIM1->ARR;
@@ -491,31 +472,14 @@
     pc.printf("\n\r");             
 }
 
-// clear screen from m line up to n line
+//print configuration of timers
 //------------------------------------------------------------------------------------------------------------------
-/*void Debug_complete::clear_from_n_up_to_m(int m, int n){
-    pc.printf("\033[%d;0H",m);
-    wait(1);
-    while (m > n){ 
-        m--;
-        pc.printf("\033[K\033[%d;0H",m);
-    }
-    pc.printf("\n\r");
-
-}*/
-
-
 void Debug_complete::show_all_timers_config(){
-    //pc.printf("\e[93;40mTimer configuration:\e[97;40m\n\r");
     line();
     pc.printf("| \e[93;40mTIM\e[97;40m\t| Prescaler\t| Auto reload value\t| Alignment\t\t|\n\r");
-    //line();
     show_tim_config(1);
-    //line();
     show_tim_config(3);
-    //line();
     show_tim_config(14);
-    //line();
     show_tim_config(16);
 }
 
@@ -526,18 +490,17 @@
     {'A',9},{'A',10},{'A',13},{'A',14},{'B',1},{'B',8},{'F',0},{'F',1} };
     int num_of_pins = 16;
     
-    //line();
-    //pc.printf("\e[93;40mPin configurations:\e[97;40m\n\r");
     line();
     pc.printf("| \e[93;40mPIN\e[97;40m\t| mode of pin\t\t| Configuration\t\t\t| value\t|\n\r");
-    //line();
     
     for (int i = 0; i < num_of_pins; i++ ){
         show_pin_config(pins_F042[i]);
-        //line();
     }
     line();
 }
+
+//print line to terminal window
+//------------------------------------------------------------------------------------------------------------------
 void Debug_complete::line(){
     pc.printf("-------------------------------------------------------------------------\n\r");
 }
\ No newline at end of file