You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@nuttx.apache.org by Michal Lenc <mi...@seznam.cz> on 2021/07/21 20:19:56 UTC

Using PWM with less channels than defined in CONFIG_PWM_NCHANNELS

Hello,




currently most of the PWM drivers (STM32, NRF52, iMXRT  etc.) use for loop 
from 0 to CONFIG_PWM_NCHANNELS when setting multiple channels in function 
pwm_start, for example here in STM32 code <https://github.com/apache/
incubator-nuttx/blob/master/arch/arm/src/stm32/stm32_pwm.c#L4386>. This 
works fine when CONFIG_PWM_NCHANNELS is equal to the number of used 
channels, but there can be situations when less channels are used. For 
example if I configure PWM1 with 2 channels and PWM2 with 3 channels, CONFIG
_PWM_NCHANNELS would have to be 3, but that would mean the loop would go 
from 0 to 3 also for PWM1 instead of just from 0 to 2. This resulted in hard
fault when I tried something similar in an application designed in 
pysimCoder as info->channels[2].channel is not defined because channel 3 is 
not used for PWM1.




I was thinking about adding something like int used_channels; to include/
nuttx/timers/pwm.h <https://github.com/apache/incubator-nuttx/blob/master/
include/nuttx/timers/pwm.h#L137> and then defined number of used channels in
the application (the same way as defining frequency, channel number and duty
cycle) rather than to have a global definition CONFIG_PWM_NCHANNELS. That 
would also mean replacing the for loops in pwm_start function (and maybe 
some other lines of code) so there would be something like "for (i = 0; ret 
== OK && i < info->used_channels; i++)" instead of "for (i = 0; ret == OK &&
i < CONFIG_PWM_NCHANNELS; i++)".




The tricky part of this is multiplatform change that would probably result 
in some bugs that I would not be able to test as I don´t have all the boards
with those MCUs. Or am I missing something and using less channels than 
defined in CONFIG_PWM_NCHANNELS should actually work? Thanks for your 
inputs.


Best regards,
Michal Lenc

Re: Using PWM with less channels than defined in CONFIG_PWM_NCHANNELS

Posted by raiden00pl <ra...@gmail.com>.
For this reason, stm32_pwm.c has an additional condition checking whether
the channel in 'struct pwm_info_s' is set:

4389 │       int i;
> 4390 │
> 4391 │       for (i = 0; ret == OK && i < CONFIG_PWM_NCHANNELS; i++)
> 4392 │         {
> 4393 │           /* Set output if channel configured */
> 4394 │
> *4395 │           if (info->channels[i].channel != 0)*
> 4396 │             {
> 4397 │               ret = pwm_duty_update(dev, info->channels[i].channel,
> 4398 │                                     info->channels[i].duty);
> 4399 │             }
> 4400 │         }
>

All channels set to 0 are ignored.
This line is present in STM32 and NRF52 but not in iMXRT.


śr., 21 lip 2021 o 22:20 Michal Lenc <mi...@seznam.cz> napisał(a):

>
> Hello,
>
>
>
>
> currently most of the PWM drivers (STM32, NRF52, iMXRT  etc.) use for loop
> from 0 to CONFIG_PWM_NCHANNELS when setting multiple channels in function
> pwm_start, for example here in STM32 code <https://github.com/apache/
> incubator-nuttx/blob/master/arch/arm/src/stm32/stm32_pwm.c#L4386
> <https://github.com/apache/incubator-nuttx/blob/master/arch/arm/src/stm32/stm32_pwm.c#L4386>>.
> This
> works fine when CONFIG_PWM_NCHANNELS is equal to the number of used
> channels, but there can be situations when less channels are used. For
> example if I configure PWM1 with 2 channels and PWM2 with 3 channels,
> CONFIG
> _PWM_NCHANNELS would have to be 3, but that would mean the loop would go
> from 0 to 3 also for PWM1 instead of just from 0 to 2. This resulted in
> hard
> fault when I tried something similar in an application designed in
> pysimCoder as info->channels[2].channel is not defined because channel 3
> is
> not used for PWM1.
>
>
>
>
> I was thinking about adding something like int used_channels; to include/
> nuttx/timers/pwm.h <https://github.com/apache/incubator-nuttx/blob/master/
> include/nuttx/timers/pwm.h#L137
> <https://github.com/apache/incubator-nuttx/blob/master/include/nuttx/timers/pwm.h#L137>>
> and then defined number of used channels in
> the application (the same way as defining frequency, channel number and
> duty
> cycle) rather than to have a global definition CONFIG_PWM_NCHANNELS. That
> would also mean replacing the for loops in pwm_start function (and maybe
> some other lines of code) so there would be something like "for (i = 0;
> ret
> == OK && i < info->used_channels; i++)" instead of "for (i = 0; ret == OK
> &&
> i < CONFIG_PWM_NCHANNELS; i++)".
>
>
>
>
> The tricky part of this is multiplatform change that would probably result
> in some bugs that I would not be able to test as I don´t have all the
> boards
> with those MCUs. Or am I missing something and using less channels than
> defined in CONFIG_PWM_NCHANNELS should actually work? Thanks for your
> inputs.
>
>
> Best regards,
> Michal Lenc