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

Reply via email to