Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 3843

SDK • Re: Could use a sanity check of this code

$
0
0
Several parts of this look a bit odd.

Starting with the trivial,

Code:

pwm_set_enabled(Tach_Slice, true);pwm_set_mask_enabled(1 << Tach_Slice);
Those two lines do exactly the same thing - you don't need both.

These probably work:

Code:

// Read the pulse count from the PWM sliceuint32_t pulse_count = pwm_hw->slice[ pwm_gpio_to_slice_num(FAN_TACH) ].slice->cc;// Reset the pulse count by writing 0 to the capture registerpwm_hw->slice[ pwm_gpio_to_slice_num(FAN_TACH) ].slice->cc = 0;
but there exist SDK equivalents that you'd maybe prefer to use:

Code:

    pulse_count = pwm_get_counter(pwm_gpio_to_slice_num(MY_PIN));    pwm_set_counter(pwm_gpio_to_slice_num(MY_PIN), 0);
Then you have a hopeful comment, but no associated code that actually sets the timer to count pulses:

Code:

// Set PWM slice to capture mode for pulse counting
You probably want something more like:

Code:

    pwm_config cfg;    cfg = pwm_get_default_config();    pwm_config_set_clkdiv_mode(&cfg, PWM_DIV_B_RISING);    pwm_config_set_clkdiv(&cfg, MY_CLKDIV);    pwm_init(pwm_gpio_to_slice_num(MY_PIN), &cfg, true);
with the critical call apparently missing in your code being the pwm_config_set_clkdiv_mode() to make it count input pulses rather than the internal clock. Your calls to set the limit and level aren't wrong, but I suspect you don't need them for your use-case.
I'm also using PWM output on GPIO 9.
That's OK. For input (pulse counting), your input pin must be a PWM 'B' channel, which means an odd-numbered GPIO, so your choice of GPIO5 is fine there, and GPIO5 is PWM2 while GPIO9 is PWM4 so there's no clash there.

Statistics: Posted by arg001 — Sat May 04, 2024 10:58 am



Viewing all articles
Browse latest Browse all 3843

Trending Articles