darrylring opened a new pull request, #21814:
URL: https://github.com/apache/nuttx/pull/21814

   ## Summary
   
   While testing the timer capture (specifically with PWM inputs) on the 
STM32H5, I encountered a number of issues, all fairly small. This PR attempts 
to resolve them all, though I'm happy to split it into smaller PRs if preferred.
   
   1. For PWM input mode, two channels use the same pin. However, 
`stm32_cap_getcapture` only actually handled the second channel if the board 
defined a pin macro `GPIO_TIMx_CHxIN` for it. I didn't do this, and so reading 
the capture register returns 0xFFFFFFFF and the duty cycle is garbage.
   2. Avoid dividing by zero when calculating frequency. The STM32H5 has this 
fix already, and this is applying it to the other capture drivers.
   3. Capture counter is reset on the capture edge, so the count is one less 
than the actual number of timer ticks. This leads to the capture values being 
off-by-one, and for frequency and duty cycle measurements being slightly off.
   4. `maxtimeout` gets set to `(1 << resolution) - 1` but if resolution is 32, 
this is UB. Do this calculation in 64-bits instead. This matches what is done 
in the timer drivers.
   5. Properly disable capture channels in `stm32_stop`.
   
   ## Impact
   
   Previously slightly wrong PWM input values are now corrected. Possibly 
someone is relying on the slightly incorrect values, or correcting them in 
software after the fact? Not sure how to judge this impact.
   
   `stm32_stop` now actually stops the capture channels. Any application 
counting on the capture timer to remain running will now fail.
   
   ## Testing
   
   **NOTE: This touches STM32H7 code and code common to other STM32 families. I 
can only test STM32H5. @raiden00pl would you be able to help me out here and 
run a similar test on other boards?**
   
   Tested on the NUCLEO-H563ZI board with the `capture` defconfig with the 
following changes: 
   
[nucleo-h563zi-capture.patch](https://github.com/user-attachments/files/34761680/nucleo-h563zi-capture.patch)
   
   I have a jumper wire from PB6 (CN10-14, TIM4 CH1) to PE9 (CN10-4, TIM1 CH1), 
generate PWM on TIM4 channel 1 using the `pwm` NSH builtin, and read the TIM1 
channel 1 PWM input using the `cap` NSH builtin:
   
   ```
   nsh> pwm -f 2000 -d 50 -t 60 &
   nsh> cap -n 5
   ```
   
   **BEFORE: With jumper wire**
   
   ```
   nsh> pwm -f 2000 -d 50 -t 60 &
   pwm [4:100]
   nsh> pwm_main: starting output with frequency: 2000 channel: 1 duty: 00007fff
   cap -n 5
   cap_main: Hardware initialized. Opening the capture device: /dev/cap0
   cap_main: Number of samples: 5
   pwm duty cycle: 49 % 
   pwm frequency: 2004 Hz 
   pwm edges counting: 6 
   pwm duty cycle: 49 % 
   pwm frequency: 2004 Hz 
   pwm edges counting: 221 
   pwm duty cycle: 49 % 
   pwm frequency: 2004 Hz 
   pwm edges counting: 441 
   pwm duty cycle: 49 % 
   pwm frequency: 2004 Hz 
   pwm edges counting: 661 
   pwm duty cycle: 49 % 
   pwm frequency: 2004 Hz 
   pwm edges counting: 881 
   Terminating!
   ```
   
   **AFTER: With jumper wire**
   
   ```
   sh> pwm -f 2000 -d 50 -t 60 &
   pwm [4:100]
   nsh> pwm_main: starting output with frequency: 2000 channel: 1 duty: 00007fff
   cap -n 5
   cap_main: Hardware initialized. Opening the capture device: /dev/cap0
   cap_main: Number of samples: 5
   pwm duty cycle: 50 % 
   pwm frequency: 2000 Hz 
   pwm edges counting: 6 
   pwm duty cycle: 50 % 
   pwm frequency: 2000 Hz 
   pwm edges counting: 225 
   pwm duty cycle: 50 % 
   pwm frequency: 2000 Hz 
   pwm edges counting: 445 
   pwm duty cycle: 50 % 
   pwm frequency: 2000 Hz 
   pwm edges counting: 665 
   pwm duty cycle: 50 % 
   pwm frequency: 2000 Hz 
   pwm edges counting: 885 
   Terminating!
   ```
   
   **AFTER: No jumper wire**
   
   STM32H5 already has the divide-by-0 fix, so this works fine before as well.
   
   ```
   nsh> cap -n 5
   cap_main: Hardware initialized. Opening the capture device: /dev/cap0
   cap_main: Number of samples: 5
   pwm duty cycle: 0 % 
   pwm frequency: 0 Hz 
   pwm edges counting: 0 
   pwm duty cycle: 0 % 
   pwm frequency: 0 Hz 
   pwm edges counting: 0 
   pwm duty cycle: 0 % 
   pwm frequency: 0 Hz 
   pwm edges counting: 0 
   pwm duty cycle: 0 % 
   pwm frequency: 0 Hz 
   pwm edges counting: 0 
   pwm duty cycle: 0 % 
   pwm frequency: 0 Hz 
   pwm edges counting: 0 
   Terminating!
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to