Electrical – stm32f407 timer setup stm32duino

stm32f4timer

I'm trying to understand how timers work in this board.
I want to make a simple led blink after X amount of microseconds and then keep the led on for X amount of microseconds

stimer_t _timer;

void setup() {

    _timer.timer = TIM3; //84Mhz

    TimerHandleInit(&_timer, 60000, ??);
    attachIntHandle(&_timer,TimerTestIsr);
}

volatile bool isOn = false;

static void TimerTestIsr(stimer_t *timer) {

    UNUSED(timer);

    //just to mesure time
    previusTime = nowTime;
    nowTime = micros();

    if (isOn == false) {
        digitalWrite(LedPin, HIGH);
        isOn = true;
        setTimerCounter(&_timer,(65535 - 1234)); //so the counter will start from 64301 till 65535 is 1234
    }
    else {
        //on the next trigger it will shutdown the led
        digitalWrite(LedPin, LOW);
        isOn == false;
    }
}

As I understand the timer can go up to 65535 and then overflows to trigger the interrupt. But I'm strugling to understand how to set the prescaler for 1us or 1ms or 1sec. So what I want to do exactly is set the prescaler so every time the counter increase its 1us or what ever time I want. So if the counter is 50000 I want this to be a delay of 50000us.

Best Answer

I think I solved that, but I had to mix some functions because for some reason I wasn't able to trigger the interrupt by setting TIM registers only

uint16_t returnPeriod(uint16_t prescaler, unsigned long time, uint8_t mhz) {
    return (time) / ((1000000 / mhz) * 0.00001) / prescaler;
}

const byte LedPin = PG7;

stimer_t timerx;

void setup() {

    pinMode(LedPin, OUTPUT);
    //digitalWrite(LedPin, HIGH);
    //NVIC_EnableIRQ(TIM5_IRQn); this is not working for some reason i have to use TimerHandleInit &  attachIntHandle
    timerx.timer = TIM5;
    TimerHandleInit(&timerx, 0, 0);
    TIM5->PSC = 10000;
    attachIntHandle(&timerx, tim5TestInterrupt);

}


volatile bool enableFlag = false;
unsigned long time_now = 0, time_before = 0;

void loop() {
    time_before = time_now;
    time_now = micros();

    if (enableFlag == false) { //make sure we enable after timeout
        TIM5->ARR = returnPeriod(TIM5->PSC, 5000000, 84); //wait 0.5seconds
        TIM5->CNT = 0;
        TIM5->CR1 |= TIM_CR1_CEN;
    }

    while ((TIM5->CR1 & TIM_CR1_CEN)) { // wait till timeout aka timer disabled

    }
    if(Serial){
        Serial.println(time_now-time_before); //it should print delay time + timeout time
    }
}

void tim5TestInterrupt(stimer_t* st32timer) {

    if (enableFlag == false) {
        enableFlag = true;
        digitalWrite(LedPin,HIGH);

        TIM5->ARR = returnPeriod(TIM5->PSC, 30000000, 84); //timeout after 3 seconds
        TIM5->CNT = 0;
    }
    else {
        enableFlag = false;
        digitalWrite(LedPin, LOW);

        TIM5->CR1 &= ~TIM_CR1_CEN; //disable timer

    }
}