Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limitations to PWM frequency? #32

Open
radwouters opened this issue Jun 23, 2024 · 2 comments
Open

Limitations to PWM frequency? #32

radwouters opened this issue Jun 23, 2024 · 2 comments

Comments

@radwouters
Copy link

I don't know if the following is an issue specific to the implementation in this HAL, or that it is a limitation in the chip, so please let me know if this issue is in the wrong place.

For a project in which I am sending 38 kHz NEC messages on a CH32V003F4P6 dev board, I am using SimplePWM to generate a 38 kHz PWM signal and then modulate this by calling pwm.enable and pwm.disable, as follows:

    let mut pwm = SimplePwm::new(
        p.TIM1,
        None,
        None,
        None,
        Some(pin),
        Hertz::khz(38),
        CountingMode::default(),
    );
    let ch = hal::timer::Channel::Ch4;

    // Futher, in the loop to send bits:
    pwm.enable(ch);
    Delay.delay_us(563);
    pwm.disable(ch);
    if bit == 0 {
        Delay.delay_us(563);
    } else {
        Delay.delay_us(1688);
    }

When looking at an oscilloscope connected to the output pin, it looks as follows:
pic_49_1
pic_49_2

Is this a limitation of the CH32V003? That seems unlikely to me, given that the clock of a CH32V003 is about 1000 times higher than the signal frequency? Did I configure the PWM wrong?

I see that in the WS2811 example the signal is bitbanged in code as well, was that done for the same reason?

If any additional information is needed, please let me know :) And thanks for the great work on this project!

@andelf
Copy link
Contributor

andelf commented Jun 23, 2024

The FLASH accessing speed limits the speed of CH32V family MCUs.
So Delay.delay_us might be unreliable.

#[highcode] is used in gpio_ws2812.rs demo, which moves code to SRAM. I'll add a timer-driven example soon.

@radwouters
Copy link
Author

I think that this behaviour can be expected and I just did not think about it properly: Only the first pulse is deviating in width
pic_49_3
Which of course is pretty logical, since when the pwm channel gets enabled, the timer can be in the middle of a cycle, but the oscilloscope only gets triggered once the channel is enabled. Therefore, it sees only the tail end of that cycle.

If you have an alternative solution, I'd love to see that, but I think that for my application the odd first pulse won't be too much of an issue.

Many thanks for your quick response :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants