Search code examples
ctimerinterruptmicrochipatmelstudio

Why does my hardware timer not increment the count using HAL libraries?


I'm writing a program for the SAML21G18B using AtmelStart and ASF4 HAL libraries. My timer is intended to simply count up until it overflows, at which point a handler increments a count OVF and restarts the timer. My understanding is that the HAL libraries i.e. timer_start(timerInstance)(see API documentation here) should start the timer, and I need to override the TC0_Handler function so that it increments my count OVF. However, the timer_start function never returns, and it appears that the handler is repeatedly called despite the timer count register not changing from zero. It appears that every time the OVF bit is cleared via hri_tc_clear_interrupt_OVF_bit, it gets reset on the next step when stepping through in debug mode.

I have changed a couple of things in the configuration of the timer, namely:

  1. I have changed it to be 16 bit rather than the 32 bit that the AtmelStart generates, i.e. #define CONF_TC0_MODE TC_CTRLA_MODE_COUNT16_Val
  2. The timer is running at 16MHz (CPU speed) and thus the timer tick period is <1us, where the AtmelStart configuration only seems to allow integers, so I have overridden that to be a float.
  3. I am not wanting to use the timer tasks provided by the API as I am trying to keep my code as close to legacy as possible.

Is someone able to point me in the right direction here? Is there some timer configuration I'm missing? All examples I can find use the tasks. I'm not too familiar with posting SO questions so if you need more information I am happy to provide.

Edit: the code in question:

int main(void)
{
    /* Initializes MCU, drivers and middleware */
    atmel_start_init();
    ext_irq_register(MCU_SWI, TurnOff_Handler);
    
    //TODO make this separate driver init func.
    timerInit(&Timer0, &TIMER_0, COUNT_UP, 0);
    timerStart(&Timer0);
}

Solution

  • I found the solution is due to the count requiring a read sync. See here for solution on the SAMD21, which has identical solution for the SAML21.