cancel
Showing results for 
Search instead for 
Did you mean: 

PSoC 6 MCU

GuGa_1322886
New Contributor II

Hi,

I'm setting up a PSOC6 timer using PDL functions in ModusToolbox. The timer is preconfigured using DeviceConfigurator 3.0 and then initialized and started using the definitions created in the generated code.  The timer generates an interrupt on terminal count to have a periodic event, initially set to 1 second, that will trigger other actions.

So far so good, but I need to dynamically change the timer's terminal count to alter the frequency of the periodic event. But calling  Cy_TCPWM_Counter_SetPeriod() alone doesn't change it. Stopping or disabling the timer before calling ..SetPeriod()  doesn't make any difference.   Investigating further I found out that the companion  Cy_TCPWM_Counter_GetPeriod() on first call returns FFFF FFFF FFFF FFFF (that is -1). On subsequent calls it returns the period set in the prior call to Cy_TCPWM_Counter_SetPeriod(), however, the timer remains fix at the same initial period of 1 second. 

The only way to successfully change period is to  de-initialize and reinitialize the timer as follows:

uint32_t _Timer_1_clock = 100000U;

void Change_Timer_1_Frequency(uint32_t _new_frequency)
{
uint32_t period = 0;

/* validate frequency ranges */
if (_new_frequency < MIN_FREQUENCY || _new_frequency > MAX_FREQUENCY)
return;

period = Cy_TCPWM_Counter_GetPeriod(Timer_1_HW, Timer_1_MASK);

if (period == 0 ) // the only reason for this test is so period is not optimized away!
return;

cy_stc_tcpwm_counter_config_t T1_config;

/*Timer_1_config is CONST, we need to make a copy to change it*/
memcpy(&T1_config, &Timer_1_config, sizeof(cy_stc_tcpwm_counter_config_t));

Cy_TCPWM_Counter_Disable(Timer_1_HW, Timer_1_MASK);
Cy_TCPWM_Counter_DeInit(Timer_1_HW, Timer_1_MASK, &Timer_1_config);

T1_config.period = _Timer_1_clock /_new_frequency;

Cy_TCPWM_Counter_Init(Timer_1_HW, Timer_1_NUM, &T1_config);
Cy_TCPWM_Counter_Enable(Timer_1_HW, Timer_1_MASK);
}

0 Likes
1 Solution
Ekta
Moderator
Moderator

Hello @GuGa_1322886 

I tried creating a project to change the period of TCPWM during runtime. In the project, I had configured the period of TCPWM as 10000. The TCPWM counter is clocked at 10kHz and generates an interrupt on the terminal count.

In the interrupt handler of the TCPWM, an LED is toggled. After the LED toggles thrice, I changed the period of the counter to 2000 and observed that the rate at which led toggles (increases) reflects the change in this period.

I am attaching the main.c from the project I created.

The device configurator settings are as follows:

Ekta_0-1629910480680.png

If required I can also attach the complete project for your reference.

Best Regards

Ekta



 

View solution in original post

0 Likes
3 Replies
Ekta
Moderator
Moderator

Hello @GuGa_1322886 

I tried creating a project to change the period of TCPWM during runtime. In the project, I had configured the period of TCPWM as 10000. The TCPWM counter is clocked at 10kHz and generates an interrupt on the terminal count.

In the interrupt handler of the TCPWM, an LED is toggled. After the LED toggles thrice, I changed the period of the counter to 2000 and observed that the rate at which led toggles (increases) reflects the change in this period.

I am attaching the main.c from the project I created.

The device configurator settings are as follows:

Ekta_0-1629910480680.png

If required I can also attach the complete project for your reference.

Best Regards

Ekta



 

View solution in original post

0 Likes
GuGa_1322886
New Contributor II

Hi Ekta,

Thanks for the quick return. At first glance I don't see any glaring difference on the code, the counter configuration is different: 32 bits, with a faster clock and clock dividers. I'll see if that makes a difference and report back.

0 Likes
GuGa_1322886
New Contributor II

Update: Starting with a Hello World project, I recreated a similar project using 16 and 32 bits counters and indeed I could change the period on both clocks as documented.  Now I need to get back to my problem project, which is more involved, using serial port DMA and RTC, to find out if there is any interference from the other PDL functions with this one.

0 Likes
Top labels