Non-obvious behaviour of PWM_Start() API routine

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

cross mob
Anonymous
Not applicable

I recently had a look at the following API call of a PSoC4 Pulse Width Modulation component.

   
void PWM_Start(void) {   if (0u == PWM_initVar) {     PWM_Init();     PWM_initVar = 1u;   }
   
  PWM_Enable(); }
   

The routine documentation clearly states that the PWM will be initialized upon first call to PWM_Start(), which is fine. What I don't like about this implementation is that the PWM_Start() routine sets PWM_initVar. In my opinion, this should be done within PWM_Init() because then I can initialize the PWM without immediately starting it, allowing me to defer the actual start to the point where I need it.

   

Could you please fix this in the next version of PSoC Creator?

   

Cheers,

   

Adrian

0 Likes
6 Replies
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored
        But why do you want to do that? The data sheet explicitly recommends to call only PWM_Start() instead of Init() and Enable()...   
0 Likes
Anonymous
Not applicable

A call to the PWM_Start() function will automatically call the PWM_Init() and PWM_Enable() functions (as the case with most of the PSoC components). So, in most of the cases, there is no need to call them again.

   

 

   

Regards.

0 Likes
Anonymous
Not applicable

 Well, the problem is that with this implementation you cannot configure the PWM in software before actually starting the PWM. Any configurations that you do will be overwritten by PWM_Init() called from within PWM_Start().

   

For example, using the setup below, the following piece of code won't have the desired effect, which is making the RGB LED emit orange light:

   

Configurator setup:
- 2 PWMs driving the two LED pins on the PSoC 4 Pioneer board
- Both PWMs have period=255, and compare=0

   
PWM_LED_Red_WriteCompare(128u); PWM_LED_Red_Start(); PWM_LED_Green_Start(); CyDelay(1000); PWM_LED_Red_Stop(); PWM_LED_Green_Stop();
   
When you run this code, the PWMs will output 'low' (and thus the pins go 'high') for the entire period. The LED will emit yellow light.
0 Likes
Anonymous
Not applicable

You could of course fix it by writing:

   
PWM_LED_Red_Start(); PWM_LED_Red_WriteCompare(128u); PWM_LED_Green_Start(); CyDelay(1000); PWM_LED_Red_Stop(); PWM_LED_Green_Stop();
   

Personally, I find this very unintuitive…

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored
        So instead of using the default values configured in the component dialog, you want to set them via the API? Is there a reason for that?   
In that case, you in fact need to call Init() and Enable() on your own. Maybe you can send a request to Cypress to somehow provide this capability, or at least explain (in the data sheet or an AppNote) how to do this properly.   
0 Likes
ScottA_91
Employee
Employee
10 solutions authored 100 replies posted 50 replies posted

This is actually desired and correct behavior.

   

 

   

See page 16 of the datasheet:

   

 

   

   

 

   

Basically, if you call start, stop, change settings and start again it will not overwrite your settings.  That said the first time start is called, it is expected the settings in the customizer are correct.

   

 

   

This is true with other components such as UART.

0 Likes