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.

MSP432P401R: Timer32 period is configured incorrectly

Part Number: MSP432P401R

Hello! I'm trying to use Timer32 on a MSP-EXP432P401R Rev. 1.0 board (DriverLib - v4_00_00_11). This is the code I'm using to toggle LED1 every 1 second:

int main(void)
{
    // Enabling the FPU with stacking enabled for floating point calculations
    MAP_FPU_enableModule();
    MAP_FPU_enableLazyStacking();

    // Stop Watchdog
    MAP_WDT_A_holdTimer();
    MAP_Interrupt_disableMaster();

    // Setting the external clock frequency
    MAP_CS_setExternalClockSourceFrequency(32768, 48000000);
    MAP_PCM_setCoreVoltageLevel(PCM_VCORE1);

    // Configuring pins for crystal usage
    MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_PJ, GPIO_PIN0 | GPIO_PIN1, GPIO_PRIMARY_MODULE_FUNCTION); // LFXT
    MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_PJ, GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION); // HFXT

    MAP_FlashCtl_setWaitState(FLASH_BANK0, 2); // For 48 MHz
    MAP_FlashCtl_setWaitState(FLASH_BANK1, 2); // For 48 MHz

    // Starting LFXT
    MAP_CS_startLFXTWithTimeout(CS_LFXT_DRIVE0, 10);
    // Starting HFXT
    MAP_CS_startHFXT(false);

    // Initializing MCLK, HSMCLK, SMCLK and ACLK
    MAP_CS_initClockSignal(CS_MCLK, CS_HFXTCLK_SELECT, CS_CLOCK_DIVIDER_1);
    MAP_CS_initClockSignal(CS_HSMCLK, CS_HFXTCLK_SELECT, CS_CLOCK_DIVIDER_1);
    MAP_CS_initClockSignal(CS_SMCLK, CS_HFXTCLK_SELECT, CS_CLOCK_DIVIDER_2); // Maximum CS_HSMCLK / 2
    MAP_CS_initClockSignal(CS_ACLK, CS_LFXTCLK_SELECT, CS_CLOCK_DIVIDER_1); // Maximum 128 kHz
    MAP_CS_initClockSignal(CS_BCLK, CS_LFXTCLK_SELECT, CS_CLOCK_DIVIDER_1); // Maximum 32768 kHz

    GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0);

    MAP_Interrupt_enableSleepOnIsrExit();
    MAP_Interrupt_enableMaster(); // Enabling MASTER interrupts

    while(1)
    {
        Timer32_sleep_cycles(5000000);
    }
}

void Timer32_sleep_cycles(uint32_t cycles)
{
    /* Configuring Timer32 to "uint32_t cycles" cycles of MCLK in periodic mode */
    MAP_Timer32_initModule(TIMER32_0_BASE, TIMER32_PRESCALER_1, TIMER32_32BIT, TIMER32_PERIODIC_MODE);
    MAP_Interrupt_enableInterrupt(INT_T32_INTC);
    
    if (cycles <= 4294967295) MAP_Timer32_setCount(TIMER32_0_BASE, cycles);
    else MAP_Timer32_setCount(TIMER32_0_BASE, 4294967295);

    MAP_Timer32_enableInterrupt(TIMER32_0_BASE);
    MAP_Timer32_startTimer(TIMER32_0_BASE, true);

    MAP_Interrupt_enableSleepOnIsrExit();
    MAP_Interrupt_enableMaster();
    MAP_PCM_gotoLPM0(); // Go to sleep until timer reaches the number of cycles
}

void T32_INTC_IRQHandler(void)
{
    MAP_Timer32_clearInterruptFlag(TIMER32_0_BASE);
    
    GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN0);

    MAP_Interrupt_disableSleepOnIsrExit();
}

The problem is that the timer period is approx. 10 times greater than the value set.

E.g., for 48MHz, the MCLK period is ~20.8ns and in order to sleep for 1 second the timer would need to be set with approx. 50 milion cycles. In the code above I use 5 million cycles to get it sleeping for 1 second. If I use 50 million cycles, the period is about 10 seconds.

What is causing this problem and how can it be resolved?

**Attention** This is a public forum