I started an embedded Rust project using the stm32f1xx_hal. I looked at the many examples. Most of these examples are very simple and realized in one module or file.
I wanted to implement a fan control with a PWM output and a frequency counter to check the fan speed. So I started with some code like this in my main.rs:
// Some peripheral unwrapping - cut out let mut gpioa = dp.GPIOA.split(&mut rcc.apb2); let pins = ( gpioa.pa0.into_alternate_push_pull(&mut gpioa.crl), gpioa.pa1.into_alternate_push_pull(&mut gpioa.crl), ); let mut pwm_out = Timer::tim2(dp.TIM2, &clocks, &mut rcc.apb1) .pwm::<Tim2NoRemap, _, _, _>(pins, &mut afio.mapr, 25.khz()).0; let pwm_in_pins= (gpioa.pa6, gpioa.pa7); let t3 = Timer::tim3(dp.TIM3, &clocks, &mut rcc.apb1); let pwm_in = t3.pwm_input(pwm_in_pins, &mut afio.mapr, &mut dbg, Configuration::Frequency(10.khz()));
Now I can use the pwm_in and the pwn_out to control the fan.
But I don't want to do this in the main file. I would like to have a module with functions like fan.set_power() and fan.get_speed(). There are some issues where I can't get further.
- The timer and the pwm functions have so many dependencies. The "new" function becomes horrible with a lots of parameters. There are: clocks, rcc, afio, gpio, ...
- If I create a new struct in a new file which contains the pwm_out and the pwm_in I need the type and the type arguments. For pwm_in my IntelliJ helps me and gives the type: PwmInput<TIM3, Tim3NoRemap, <PA6<Input<Floating_>>, PA7<Input<Floating_>>)> For pwm_out IntelliJ gives up. It will not show the type.
- There are so many macros in the hal to create all the timer and pwm functions. It's hard to understand. My feeling is that I have to understand the implementation details before I can use it.
I don't know how to modularize the source code. Which data should be passed to a module to ?
Is there a sample project which shows some of these organization.