cancel
Showing results for 
Search instead for 
Did you mean: 

PWM configuration structure

ssk
Associate II
Posted on April 03, 2015 at 08:01

I am using SPC560D40L3 with SPC5Studio. I am trying to generate a PWM signal. Using Application configuration, I configured the HAL drivers to generate a PWM of 5Hz. Here, I configured the clock frequency and period for the PWM using application configuration which gets stored to structure 'PWMConfig pwm_config'.

Now I want to use a runtime value for PWM frequency i.e. my device will get input for PWM frequency (say via UART) and will use the same for 'PWMConfig pwm_config'. How to pass the clock frequency and period values (derived from PWM frequency) to this structure? Is there any other way to implement this?

Thanks in advance.

Mike.

#pwm-configuration
7 REPLIES 7
ssk
Associate II
Posted on April 03, 2015 at 08:29

It looks that I can access the two elements - frequency and period from the structure 'pwm_config' but assigning the value to these results in following error,

 error: assignment of member 'frequency' in read-only object

 error: assignment of member 'period' in read-only object

What is the solution for this?

Thanks.

Mike.

Erwan YVIN
ST Employee
Posted on April 03, 2015 at 10:00

Hello Mike ,

The structure is a const you can not update it. (Cf Code attached)

/**
* @brief Structure defining the PWM configuration ''cfg0''.
*/
const PWMConfig pwm_config_cfg
0
= {
100000
,
20000
,
pwmpcb,
{
{
PWM_OUTPUT_ACTIVE_HIGH,
pwmc
1
cb
},
{
PWM_OUTPUT_DISABLED,
NULL
},
{
PWM_OUTPUT_DISABLED,
NULL
},
{
PWM_OUTPUT_DISABLED,
NULL
},
{
PWM_OUTPUT_DISABLED,
NULL
},
{
PWM_OUTPUT_DISABLED,
NULL
},
{
PWM_OUTPUT_DISABLED,
NULL
}
},
PWM_ALIGN_EDGE
};

Anyway , if you want to change on the fly , i recommend to use an other global configuration structure copy-paste of the first one (the const). This structure will be updated by UART. after each update , pwm should be stopped and restart.

pwmStop(&PWMD1);
...............
/* Update my GLOBAL CONFIGURATION */
pwmStart(&PWMD
1
, &pwm_my_config);

Best regards Erwan
ssk
Associate II
Posted on April 03, 2015 at 13:06

Yes Erwan. I used my own config structure similar to one used by HAL driver but a non constant one.

Thanks.

Mike.

Erwan YVIN
ST Employee
Posted on April 03, 2015 at 15:11

Dear Mike ,

Do not hesitate to send me your application by email

I will check

Best Regards

                    Erwan

ssk
Associate II
Posted on May 13, 2015 at 14:40

Hello Erwan,

I am trying to pass a non-constant PWMConfig structre to function 'pwmstart'. It looks that the compiler accepts it but the configuration not getting applied to the MCU and hence no PWM on the respective pin.

Earlier I only verified the compilation part but not the execution of it. From your reply, it looks that it is not so easy to pass a non constant struct to this function..:). Can you please explain the way to implement this? May be with a small example!

Thanks in advance.

Mike.

ssk
Associate II
Posted on May 14, 2015 at 11:55

Hello Erwan,

It looks that I am able to pass a non-constant PWMConfig struct to the function. Can you please provide info on PWM frequency or duty cycle calculations? Any formula for this?

Thanks.

Mike.

Erwan YVIN
ST Employee

Posted on May 18, 2015 at 17:13

Hello Mike ,

In the reference manual RM0045,

http://www.st.com/web/en/resource/technical/document/reference_manual/CD00259pdf

You can find useful information on chapter 24 by charts and detailed description.

at the current time , PWM is usesModulus Counter Buffered (Up counter)

(Cf Figure 346)

  •  
  •  
  • the cycle is defined by the period set in your configuration (set in CADR register)

pwmp->emiosp->CH[8U].CCR.B.UCPEN = 0;

pwmp->emiosp->CH[8U].CCNTR.R = 1U;

pwmp->emiosp->CH[8U].CADR.R = pwmp->config->period;

pwmp->emiosp->CH[8U].CCR.R |= EMIOSC_BSL(EMIOS_BSL_INTERNAL_COUNTER);

pwmp->emiosp->CH[8U].CCR.R |= EMIOS_CCR_MODE_MCB_UP;

pwmp->emiosp->CH[8U].CCR.B.UCPRE = psc - 1U;

pwmp->emiosp->CH[8U].CCR.R |= EMIOSC_UCPREN;

Anyway , you can find in this attachment an example of application.

if you want to change of PWM mode ,you can patch the driver using the patch mode

Best regards

Erwan

____________________

Attachments :

SPC560Dxx_OS-Less_PWM-ICU_Own_Configuration_Test_Application_for_Discovery.7z : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006qaV6&d=%2Fa%2F0X0000000bpC%2FvcpzsMbdg_hmDODD98BguHcKsbkkbl5nc3FnJwsUv9g&asPdf=false