joan2937 / pigpio

pigpio is a C library for the Raspberry which allows control of the General Purpose Input Outputs (GPIO).
The Unlicense
1.45k stars 407 forks source link

gpioGetPWMfrequency doesn't get clock frequency after reload #557

Open cinderblock opened 1 year ago

cinderblock commented 1 year ago

I'm trying to write a cli program to manage gpio on my pi. One of the things I want to use is the hardware clock output. I also want to be able to query the current status of all the IO pins of interest. This is mostly working, except the frequency of the hardware clock output.

Steps

  1. Start a hardware clock output with gpioHardwareClock()
  2. Read clock output frequency from gpioGetPWMfrequency() as expected
  3. Exit (Clock output keeps going 🎉)
  4. Run a new program linked to pigpio
  5. Read current clock output frequency with gpioGetPWMfrequency()

Expected Result

The current frequency being output on that gpio

Actual Results

800 - The default PWM value.

Observations

Looking at the code, I see the gpioGetPWMfrequency() checks if gpioInfo[gpio].is == GPIO_HW_CLK to read the correct frequency. gpioInfo[gpio].is appears to only be set to GPIO_HW_CLK when enabling the clock and is otherwise initialized to 0 (or GPIO_UNDEFINED). So on a fresh start, pigpio will use the default case instead of checking if the gpio is currently linked to a clock (or pwm) output.

Options

As I see them...

  1. Add dedicated gpioGetClockFrequency()
  2. If gpioInfo[gpio].is == GPIO_UNDEFINED, check if it's a clock pin and which alt mode is selected, if any
  3. Scan state of gpio on startup

Or maybe something else, like a gpioIsHardwareClock() that fills gpioInfo[gpio].is as a byproduct?