Embedded rust on STM32F3 driving pwm using acc: and gyro sensor

i am new to rust, [https://github.com/stm32-rs/stm32f3xx-hal/blob/master/examples/pwm.rs ]. I am using stm32f303 and i am working with gyroscope and accelerometer and i wan to drive pwm using these sensor. I want to use TIM15 for PWM purpose, can any one help me?

What problem are you facing? You can modify the example code and use TIM15. This code below works on my STM32F3DISCOVERY board. It outputs PWM from PA2 and PA3.

let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
let pa2 = gpioa.pa2.into_af9(&mut gpioa.moder, &mut gpioa.afrl);
let pa3 = gpioa.pa3.into_af9(&mut gpioa.moder, &mut gpioa.afrl);

let tim15_channels = tim15(
    1280,    // resolution of duty cycle
    50.hz(), // frequency of period
    &clocks, // To get the timer's clock speed

let mut tim15_ch1 = tim15_channels.0.output_to_pa2(pa2);
tim15_ch1.set_duty(tim15_ch1.get_max_duty() / 2); // 50% duty cyle

let mut tim15_ch2 = tim15_channels.1.output_to_pa3(pa3);
tim15_ch2.set_duty(tim15_ch2.get_max_duty() / 20); // 5% duty cyle
1 Like

Thanks for your response. Actually i have just started working on embedded rust, previously i was working on embedded C. when i run this example [https://github.com/stm32-rs/stm32f3xx-hal/blob/master/examples/pwm.rs ] i get error " this crate requires one of the following device features enable"

You need to specify your microcontroller in Cargo.toml. I have this for my Discovery board.

version = "0.4.0"
features = ["stm32f303xc"]
1 Like

bro thanks for your response, the given example is too complex for me. can you please simplified it in new project just for PWM using TIM15? I am using stm32f3 discovery, stm32f303vc.

If the example code is too complex for you, you may want to check out the Discovery book first.

I am a new Rust developer like you. After many years working on embedded c/c++ projects, I decided to learn Rust a few months ago. When I started playing around, everything looked too difficult. Reading the book and doing the exercises made me comfortable enough to explore source codes.

1 Like

Thank you @lonesometraveler, i have just finished this book :slight_smile: i am working on STM32F3 gyroscope and accelerometer sensor and want to auto-balance a robot using PID, just having difficulty in PWM :), i have calculated angle using complementary filter,

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.