0

Is there any mistake I am making in applying delay the delay?

This is the code I am working with to blink led 3 and 4 after with a delay.

use cortex_m_rt::entry;
use stm32f30x_hal as hal;
use hal::delay::Delay;
use hal::prelude::*;
use hal::stm32f30x;
use panic_halt;

#[entry]
fn main() -> ! {
    let device_p = stm32f30x::Peripherals::take().unwrap();
    let core_periphs=cortex_m::Peripherals::take().unwrap();
    let mut reset_clock_control = device_p.RCC.constrain();
    let mut gpioe = device_p.GPIOE.split(&mut reset_clock_control.ahb);
    **let mut flash = device_p.FLASH.constrain();
    let clocks = reset_clock_control.cfgr.freeze(&mut flash.acr);
    let mut delay = Delay::new(core_periphs.SYST,clocks);**
    let mut led_3 = gpioe
        .pe9
        .into_push_pull_output(&mut (gpioe.moder), &mut (gpioe.otyper));
    let mut led_4=gpioe.pe8.into_push_pull_output(&mut gpioe.moder,&mut gpioe.otyper);


    loop {
        led_3.set_high();
        **delay.delay_ms(2_000_u16);**
        led_4.set_high();

    }
}

If I am not using delay part it is working fine

Gerhard
  • 6,850
  • 8
  • 51
  • 81
  • What exactly do you see without the delay? – Gerhard Jun 10 '21 at 08:20
  • okay, so if i remove the delay part from my code that is flash, clocks & delay and simply provide the 'led_3.set_high();' inside loop with then it is working fine. –  Jun 10 '21 at 10:19

2 Answers2

0

I think you set up your clocks wrong. For the delay to work correctly you should use the system clock.

This is how to create the Delay for STM32 based on this sample (stm32f4xx, but should work for you, too):

// Set up the system clock. We want to run at 48MHz for this one.
let rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.sysclk(48.mhz()).freeze();

// Create a delay abstraction based on SysTick
let mut delay = hal::delay::Delay::new(cp.SYST, clocks);

where dp are my device peripherals (e.g. let dp = stm32::Peripherals::take().unwrap()) and cp are the core peripherals.

So this uses the sysclk.

Alternatively you could also try to replace your delay with cortex_m::delay(8_000_000);, where the delay is given using the number of clock cycles.

frankenapps
  • 5,800
  • 6
  • 28
  • 69
  • Hi frankenapps, thanks for the answer but i think my device peripheral is not supporting this command line "let clocks = rcc.cfgr.sysclk(48.mhz()).freeze();" **it is asking for missing arguments inside freeze()** –  Jun 10 '21 at 10:12
  • Ah, I see. You have to add the `ACR`, too: [`freeze`](https://docs.rs/stm32f3xx-hal/0.6.1/stm32f3xx_hal/rcc/struct.CFGR.html#method.freeze) – frankenapps Jun 10 '21 at 10:18
  • Did you try my second suggestion? It should be simpler... – frankenapps Jun 10 '21 at 10:20
  • i provide the flash.acr inside freeze but nothing happen. –  Jun 10 '21 at 10:23
  • I tried the second suggestion **cortex::delay** and it worked. Thanks for the help @frankenapps –  Jun 10 '21 at 11:10
0

In the loop you set the LED high led_3.set_high();. However never set led_3 low again so it would never blink. So change your loop to:

led_3.set_high();
led_4.set_low();
delay.delay_ms(2_000_u16);
led_4.set_high();
led_3.set_low();
Gerhard
  • 6,850
  • 8
  • 51
  • 81
  • Thanks for this suggestion @Gerhard. I was trying to glow led 3 then led 4 with some time gap by providing delay. Although I tried your suggestion also but no result. –  Jun 10 '21 at 10:06
  • @Samar: What does no result mean? The software enters an infinite loop. – Gerhard Jun 10 '21 at 10:08
  • Led didn't glow. –  Jun 10 '21 at 10:16
  • This sounds like your program panics when executing the delay statement. Maybe try running it using semihosting or use OpenOCD to get a trace: https://docs.rust-embedded.org/debugonomicon/ – frankenapps Jun 10 '21 at 10:28