7 Replies Latest reply on Jun 1, 2015 7:21 PM by user_14586677

    PSoC 4 - Study of pin toggle max speed,  comparison of several ways of toggle.

    user_373995786

       Hello,

         

      In attachment i send an PSoC 4 example project that compares the several ways to toggle a pin.     

       

         

       

         

      #include <project.h>     

       

         

       

         

      volatile uint32 volatile_reg_val_1;     

       

         

      volatile uint32 volatile_reg_val_0;     

       

         

       

         

      int main()     

       

         

      {     

       

         

              

       

         

          // Settings:     

       

         

          //    3.3V 48MHZ  Compilation in Release mode.     

       

         

          //    Note: Sould be equal with 5V.     

       

         

              

       

         

              

       

         

          ///////////////////     

       

         

          // Hardware Max Speed     

       

         

              

       

         

          ///////////     

       

         

          // Case 6   Pin toogle max speed: 24MHz     

       

         

          ///////////     

       

         

              

       

         

          Clock_HD_MAX_SPEED_Start();     

       

         

          PWM_hardware_max_speed_Start();     

       

         

          ////////////////////        

       

         

              

       

         

          ////////     

       

         

          // Note: Pin_Soft is the name od the pin componente in the diagram.     

       

         

              

       

         

          Pin_Soft_DR    |= Pin_Soft_MASK;   // Sets pin     

       

         

          volatile_reg_val_1 = Pin_Soft_DR;     

       

         

          Pin_Soft_DR    &= ~Pin_Soft_MASK;  // Clear pin     

       

         

          volatile_reg_val_0 = Pin_Soft_DR;     

       

         

              

       

         

          for(;;)     

       

         

          {     

       

         

              ///////////     

       

         

              // Case 1     Pin toogle max speed: 855KHz     

       

         

              ///////////     

       

         

                  

       

         

              // Slowest.     

       

         

              Pin_Soft_Write(0u);    // Clear pin     

       

         

              Pin_Soft_Write(1u);    // Set pin     

       

         

              Pin_Soft_Write(0u);    // Clear pin     

       

         

       

         

              ///////////     

       

         

              // Case 2     Pin toogle max speed: 2.65MHz     

       

         

              ///////////     

       

         

                  

       

         

              // Intermediate speed (equal speed to the next case, safe way)     

       

         

              CyPins_ClearPin(Pin_Soft_0);  // Clear pin     

       

         

              CyPins_SetPin(Pin_Soft_0);    // Set pin     

       

         

              CyPins_ClearPin(Pin_Soft_0);  // Clear pin     

       

         

       

         

              ///////////     

       

         

              // Case 3     Pin toogle max speed: 2.65MHz     

       

         

              ///////////     

       

         

                  

       

         

              // Intermediate speed (equal speed to the next case, safe way)     

       

         

              Pin_Soft_DR &= ~Pin_Soft_MASK;  // Clear pin     

       

         

              Pin_Soft_DR |= Pin_Soft_MASK;   // Sets pin     

       

         

              Pin_Soft_DR &= ~Pin_Soft_MASK;  // Clear pin     

       

         

              

       

         

              ///////////     

       

         

              // Case 4     Pin toogle max speed: 4MHZ     

       

         

              ///////////     

       

         

                  

       

         

              // Second fastest speed (equal speed to the next case, unsafe way)     

       

         

              // Volatile.     

       

         

              Pin_Soft_DR = volatile_reg_val_0;   // Clear pin (afects all register (dangerous because can afect all bits)     

       

         

              Pin_Soft_DR = volatile_reg_val_1;   // Sets pin (afects all register  (    "  )     

       

         

              Pin_Soft_DR = volatile_reg_val_0;   // Clear pin (afects all register (    "  )     

       

         

       

         

                         

       

         

              ///////////     

       

         

              // Case 5     Pin toogle max speed: 6MHz     

       

         

              ///////////     

       

         

                         

       

         

              uint32 local_val_1 = volatile_reg_val_1;     

       

         

              uint32 local_val_0 = volatile_reg_val_0;     

       

         

                  

       

         

              // Fastest speed (equal speed to the previous case, unsafe way)     

       

         

              // Without volatile varibels [it uses only one ARM assembly instruction the STR [store to register],     

       

         

              // because the value of one and zero is already in the register].     

       

         

              Pin_Soft_DR = local_val_0;   // Clear pin (afects all register (dangerous because can afect all bits)     

       

         

              Pin_Soft_DR = local_val_1;   // Sets pin (afects all register  (    "  )     

       

         

              Pin_Soft_DR = local_val_0;   // Clear pin (afects all register (    "  )     

       

         

          }     

       

         

                  

       

         

      }     

       

         

       

         

      Best regards,     

       

         

      João Nuno Carvalho