9 Replies Latest reply on Apr 23, 2015 6:19 AM by user_1377889

    Attempt to generate a fast software pin toggle

    awec.verdf

      Hi, I'm using the PSoC 4 CY8CKIT-049 and trying to get the fastest SW pin toggle.

         

      I set all the clocks to the maximum of 48MHz, and set TestPin to P0[2].

         

      then, I toggled the pin using

         

       for(;;)

         

      {

         

      TestPin_DR |= TestPin_MASK;

         

      TestPin_DR &= ~IndexPin_MASK;

         

      }

         

       

         

      However, i only get a 1.337 MHz square wave, which means the pin is being toggled at 2 * 1.337 MHz.

         

      Why is there such a discrepancy with the 48MHz speed? Shouldn't it only take two cycles to toggle a pin in this method? Would there be anyway to do it faster using asm?

         

       

         

      Thanks so much!

        • 1. Re: Attempt to generate a fast software pin toggle
          user_1377889

          Welcome in the forum!

             

          We had this software-pin-toggle-discussion here in the forum a couple of times, mostly by users that just started using PSoCs.

             

          The result was: Yes, you can do it faster by using some asm instructions, but what for? PSoCs are not MPUs only, PSoCs have internal hardware that is routable just as if you are designing electronics with a CAD-software.

             

          PSoC internal signals can be routed to pins and use frequencies up to 24MHz.

             

          So start "Thinking PSoC": Which parts of my project can be done in hardware using the components? Which parts have to be done in software?

             

          And if that is not enough: You can define your own hardware using a Hardware Description Language (HDL) named VeriLog to describe special functions that run in hardware at 48MHz.

             

          Some more: There are UDBs containing a small programmable ALU (Arithmetic and Logical Unit) , FIFOs to store data and some registers.

             

          When you get all this, who asks for the toggle frequency of a pin in software?

             

           

             

          Bob

          • 2. Re: Attempt to generate a fast software pin toggle
            user_342122993

            On PSoC5 it takes 5 CPU cycles (see example below). Check if it works for PSoC4:

               

            https://vimeo.com/65092394

               

            odissey1

            • 3. Re: Attempt to generate a fast software pin toggle
              awec.verdf

              That's all well and good, but I just want to determine the fastest way I can toggle a pin in software using PSoC's MPU. I am trying to evaluate a number of microcontrollers to determine the fastest way I can do this, for the cheapest price. It is for an application where I don't need the other PSoC fancy stuff, I just need to evaluate a  few logic rules and toggle a pin based on them.

                 

               

                 

              So, how would I go about doing it faster in asm? When i searched, it seemed like this method mapped to the fastest asm instructions, but it still seems slow compared to the 48MHz clock.

                 

               

                 

               odissey1: great link, watching it now

              • 4. Re: Attempt to generate a fast software pin toggle
                user_1377889

                Did you set the optimization to something better than "None"? (Project -> Build Settings -> Arm -> Compiler -> Optimization)

                   

                 

                   

                Bob

                • 5. Re: Attempt to generate a fast software pin toggle
                  user_14586677

                  Discussion of this in this ap note -

                     

                   

                     

                      

                     

                           

                     

                  http://www.cypress.com/?rID=93401     AN86439 - PSoC® 4 - Using GPIO Pins

                     

                   

                     

                   

                     

                  Regards, Dana.

                  • 6. Re: Attempt to generate a fast software pin toggle
                    awec.verdf

                    @Bob: Thanks, using an optimization flag got the square wave up to 2.29MHz... Could asm do anything beyond that?

                       

                     

                       

                    @Dana: Isn't that the exact method I'm using?

                    • 7. Re: Attempt to generate a fast software pin toggle
                      user_1377889

                      Yes, of course: keep the values you want to write to the port in two registers and loop. two move instructions and a jump.

                         

                      But you will not reach 10MHz this way and you have no control about the frequency, start or stop of the output.

                         

                       

                         

                      Bob

                      • 8. Re: Attempt to generate a fast software pin toggle
                        user_14586677

                        Sorry I did not look closely at your approach.

                           

                         

                           

                        The for loop lots of overhead, so an ASM approach would, I think, be

                           

                        faster as its a move imeadiate (twice) followed by a short jmp.

                           

                         

                           

                        Look at the .lst file and you will see all the cycles eaten up by the C

                           

                        approach.

                           

                         

                           

                        Clearly if you need speed and you still want MIPS for other stuff use

                           

                        the PSOC HW approach to pin toggling.

                           

                         

                           

                        Regards, Dana.

                        • 9. Re: Attempt to generate a fast software pin toggle
                          user_1377889

                          When using the PSoC internal hardware you are able to control (start, stop, change period, change duty-cycle) a signal in the MHz range with a reaction-time of a few µs. CPU load is needed only for the changes, the component runs free as configured without any further intervention. When the algorithm for changing one of those parameters is simple logic, this can be accomplished in hardware, too. This logic is evaluated within a few ns when a look-up-table (LUT) component is used, cannot be beaten by any software design.

                             

                           

                             

                          Bob