Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

PSoC™ 6

Level 4
First comment on KBA 25 replies posted 10 replies posted
Level 4

Hi Everyone,

A few weeks ago I posted a message here complaining that Cy_TCPM_Counter_SetPeriod() is not behaving as documented. In my application I need to dynamically change the period of a counter anywhere from 1 to 400 Hz to generate a periodic interrupt that will trigger other events. The problem was that no mater what period I tried to set, the counter ignored it and remained running at the initial frequency when it was initialized. Frustrated I resorted to just de-initialize and reinitialize the counter with the new frequency.  A few weeks later the problem came back to bite me again when I needed to change the period of a timer in PMW mode, and posted a second message here.

After detailed code review and debugging, and stepping through the code, I finally found a few errors in my code, AND, understood how the API really has to be used. This is not clearly documented anywhere, at least in the places where I looked (this forum and others).

First, don't repeat my errors:

1. Pay attention to the type of parameters that each function expects.  For example,

Cy_TCPWM_TriggerStart_Single(Timer_1_HW, Timer_1_NUM);  // The second parameter must be the timer NUMBER
Cy_TCPWM_TriggerStart(Timer_1_HW, Timer_1_MASK);  // The second parameter must be the timer MASK (or the OR '|'  combined mask of several timers, if you want to start them all in sync)

2. Double check the parameters used in the PDL documentation. In one instance the example has the second parameter incorrect (it uses counter MASK where it should use counter NUMBER, twice!).  All xx_Single() versions of the functions work on a single timer and expect the timer number.  That error in the example led me to believe that xx_MASK and xx_NUMBER are interchangeable, when they are NOT.

3. You must STOP the counter to make changes to the period, duty cycle,  compare values, etc. There may be instances where this is not necessary, but better safe than sorry, in the PDL documentation the examples do not mention this, (at least for the functions that I used).

4. It seems that Cy_TCPWM_TriggerStopOrKill_Single() is not an atomic operation. You must test and make sure that the counter has stopped before continuing. This was the principal problem I had since the beginning.

So, here is the the safe method to alter counter parameters that I came up with:

uint32_t loops;   // this variable is for debugging purposes only

void Change_Timer_1_Frequency(uint32_t _new_frequency)
      uint32_t period = 0;

   /* validate frequency range */
   if (_new_frequency < MIN_FREQUENCY || _new_frequency > MAX_FREQUENCY)

    period = _Timer_1_clock / _new_frequency;

    loops = 0;

    Cy_TCPWM_TriggerStopOrKill_Single(Timer_1_HW, Timer_1_NUM);

    /* IMPORTANT: wait for the timer to stop before changing registers */
     while (Cy_TCPWM_Counter_GetStatus(Timer_1_HW, Timer_1_NUM) &      CY_TCPWM_COUNTER_STATUS_COUNTER_RUNNING )

    Cy_TCPWM_Counter_SetCounter(Timer_1_HW, Timer_1_NUM, 0);
    Cy_TCPWM_Counter_SetPeriod(Timer_1_HW, Timer_1_NUM, period);
    Cy_TCPWM_TriggerStart_Single(Timer_1_HW, Timer_1_NUM);

I introduced the variable Loops for testing purposes. In a simple, Hello World type of program when only this code is running Loops may be 0, most of the time. But as the program grows in complexity with interrupts and external events going on at the same time, that won't be the case.  In my application Loops ends up anywhere from 65 a to over 600 iterations before the counter stopped!

In summary, I thing that PDL is a good helper, but its documentation is minimal and the examples just snippets not really tested in an actual program.  As a former boss of mine used to say "Trust is good, check is better!"



5 Replies
Top labels