This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Timer not firing fast enough



Hello,

I am trying to create a timer that will toggle a GPIO pin at a specified rate. The fastest that I can get the pin to toggle is 1.77us. But unfortunately, I need to be able to toggle this pin at 0.33us. I have attached my code below on how I am setting the timers and servicing the interrupt. Would anyone be able to make a recommendation as to how I might achieve a shorter period?

Thanks,
Logan

void ConfigureTimer0(void){
    // Enable timer0 peripheral
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    // Configure timer0 to run once and stop
    MAP_TimerConfigure(TIMER0_BASE, TIMER_CFG_ONE_SHOT_UP);
    TimerClockSourceSet(TIMER0_BASE,TIMER_CLOCK_SYSTEM);

    // Enable timerA interrupt
    MAP_IntEnable(INT_TIMER0A);
    // Enable interrupt for timerA0
    MAP_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
}

void SetPeriodEnableTimer0(unsigned long period){
    // Set timerA0 period
    TimerLoadSet(TIMER0_BASE, TIMER_A, period);
    MAP_TimerEnable(TIMER0_BASE, TIMER_A);
}

void Timer0IntHandler(void){
    // Clear interrupt
    MAP_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    if(GPIOPinRead(GPIO_PORTH_BASE, GPIO_PIN_3)){
        GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_3, 0);
    }
    else{
        GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_3, 255);
    }
    SetPeriodEnableTimer0(1);
}

  • Hi Logan,

    0.33uS is very fast!!

    I don't know what MCU you have but let's consider the fastest Tiva: 120Mhz.

    1/120Mhz = 0,008333333 uS.

    Now note, the interrupt takes about 22 cycles to pop and push so just with that you "waste" about 183nS. So you have 330-183=147nS to execute the ISR. That's 147-8.33=17 cycles, give or take. Well I can tell you that that's impossible by using the API.
    What you have is very simple so maybe with direct register access and some optimizations you can do what you need. But this is considering you have the full dedication of the MCU!

    The MCUs are great because they have hardware that can ease such a task... you are wasting the power you have available.
    True 0.33uS is really fast even for hardware.
    Use the timer in PWM mode or even a PWM module!
    At 120Mhz you would just need to load the timer with the value 40 ( it's the math 120Mhz/(1/0.33uS)) and 20 in the match value (50% duty). Much simple and doesn't require at all intervention of the CPU while generating the wave constantly.
  • By instead - configuring the Timer in its PWM mode - multiple benefits result:

    a) you'll likely reach your desired output toggle speed
    b) the Timer's output pin carries this signal (it's the only way to directly output a timer signal)
    c) no interrupt is required

    To insure highest output frequency the MCU should run at full speed and your Timer must be properly set-up & configured. Peripheral Driver Library Guide details...
  • Hi and thank you for your responses.

    Unfortunately, using PWM will not work as the signal is not uniform. To clarify, my LaunchPad does run at 120MHz. Do you have any suggestions that are not register direct or PWM based?

    Thanks,
    Logan
  • Without registers or taking advantage of hardware, use the new ARM-M7 at 200Mhz. It might work. Or a beaglebone or a FPGA...

    With the timers in PWM mode you can change the period of the cycle or the duty just as easily. The time on can be bigger than the timer off and vice-versa. If after a number of cycles you need to change the duty/period fast you can have a timer in capture mode counting and generating a interupt or a DMA trigger
  • Hello,

    Thank you for your response. Do you happen to know the shortest interval that a timer can fire an interrupt at?

    Thanks,

    Logan
  • Hi Logan,


    If at all possible, do tell us what you're trying to accomplish with the fast non-uniform signal you're reaching for - someone might just know how to do it "right". In any case, signals of such speed are usually outside the realm of pure software implementation, at least in this class of hardware.

    -Veikko

  • Hi Veikko,

    Thank you for your reply.  The non-uniform signal is SPI data that I was attempting to bit-bang at 800kHz.  I have chosen to use a register direct timed loop and that seems to work fine for me.

    Thanks, 

  • OK. May I ask why you had to bit-bang it? There are several hardware SPI peripherals available on the Tiva, which are quite flexible especially if you handle the chip-select pins manually...
  • Hi Veikko,

    Yes. We understand that, and have used the hardware SPI. We are using an obscure proprietary protocol, and were examining our options.

    Thanks,