Skip to main content
H W
Associate II
Aug 6, 2018 7:14 AM UTC
Question

Hi, I'm using a STM32L476 and have LPTIM1 in free running mode with compare match enabled. I never get interrupts with compare match value 0x7FFF. Values higher/lower are OK. Any ideas? Cheers HW

..

    This topic has been closed for replies.

    7 replies

    waclawek.jan
    Super User
    Aug 6, 2018 12:06 PM UTC

    Read out and post the LPTIM register values.

    JW

    H W
    H WAuthor
    Associate II
    Aug 6, 2018 12:44 PM UTC

    LPTIM1:

    LPTIM_ISR 40007c00: 0000001a

    LPTIM_ICR 40007c04: 00000000

    LPTIM_IER 40007c08: 00000001

    LPTIM_CFGR 40007c0c: 00000000

    LPTIM_CR  40007c10: 00000001

    LPTIM_CMP 40007c14: 00007fff

    LPTIM_ARR 40007c18: 0000ffff

    LPTIM_CNT 40007c1c: 0000eb0a

    LPTIM_OR  40007c20: 00000000

    waclawek.jan
    Super User
    Aug 6, 2018 1:24 PM UTC

    whooops sorry looked at wrong register...

    ...looks good. I'll try to reproduce in the evening.

    JW

    waclawek.jan
    Super User
    Aug 6, 2018 1:29 PM UTC

    One more thing: you've followed this, did you?

    Caution: The LPTIM_CMP register must only be modified when the LPTIM is enabled (ENABLE bit set to ‘1’).

    H W
    H WAuthor
    Associate II
    Aug 6, 2018 2:22 PM UTC

    Yes, it is enabled.​

    waclawek.jan
    Super User
    Aug 6, 2018 11:25 PM UTC

    Quick test on the L476-DISCO, works for me with LPTIM1_CMP=0x7FFF - green LED blinked in the ISR blinks.

    (gdb) p /x *LPTIM1

    $57 = {ISR = 0x3, ICR = 0x0, IER = 0x1, CFGR = 0x0, CR = 0x1, CMP = 0x7fff,

     ARR = 0xffff, CNT = 0x9357, OR = 0x0}

    (gdb)

    (higher LPTIM1->ISR bits are cleared, as I was lazy and in the ISR simply cleared all bits)

    Will be some other problem at your place. Perhaps tell us why do you think the interrupt is not working.

    JW

    // trying LPTIM compare interrupt
    // https://community.st.com/s/question/0D50X00009bNKtNSAW/hi-im-using-a-stm32l476-and-have-lptim1-in-free-running-mode-with-compare-match-enabled-i-never-get-interrupts-with-compare-match-value-0x7fff-values-higherlower-are-ok-any-ideascheershw
     
     
    #define DISCOL4 // LEDs are: PB2 (red), PE8 (green)
     
    #define LPTIM_PWM_RED // PB2 at AF1 is LPTIM1_OUT
     
     
    #include <stdint.h>
    #include "stm32l476xx.h"
     
    // -------- stuff missing from the stm32XXXxx.h
     // GPIOx_MODER - 2 bits per pin
    #define GPIO_Mode_In 0x00 // GPIO Input Mode
    #define GPIO_Mode_Out 0x01 // GPIO Output Mode
    #define GPIO_Mode_AlternateFunction 0x02 // GPIO Alternate function Mode
    #define GPIO_Mode_Analog 0x03 // GPIO Analog Mode
     
     
    #define AFRL AFR[0]
    #define AFRH AFR[1]
    #define GPIO_AFRL_AFRL0_0 ((uint32_t)0x00000001)
    #define GPIO_AFRL_AFRL1_0 ((uint32_t)0x00000010)
    #define GPIO_AFRL_AFRL2_0 ((uint32_t)0x00000100)
    #define GPIO_AFRL_AFRL3_0 ((uint32_t)0x00001000)
    #define GPIO_AFRL_AFRL4_0 ((uint32_t)0x00010000)
    #define GPIO_AFRL_AFRL5_0 ((uint32_t)0x00100000)
    #define GPIO_AFRL_AFRL6_0 ((uint32_t)0x01000000)
    #define GPIO_AFRL_AFRL7_0 ((uint32_t)0x10000000)
    #define GPIO_AFRH_AFRH8_0 ((uint32_t)0x00000001)
    #define GPIO_AFRH_AFRH9_0 ((uint32_t)0x00000010)
    #define GPIO_AFRH_AFRH10_0 ((uint32_t)0x00000100)
    #define GPIO_AFRH_AFRH11_0 ((uint32_t)0x00001000)
    #define GPIO_AFRH_AFRH12_0 ((uint32_t)0x00010000)
    #define GPIO_AFRH_AFRH13_0 ((uint32_t)0x00100000)
    #define GPIO_AFRH_AFRH14_0 ((uint32_t)0x01000000)
    #define GPIO_AFRH_AFRH15_0 ((uint32_t)0x10000000)
     
    #define GPIO_AlternateFunction_SYS 0 /* default */
    #define GPIO_AlternateFunction_TIM1_1 1
    #define GPIO_AlternateFunction_TIM2_1 1
    #define GPIO_AlternateFunction_TIM5_1 1
    #define GPIO_AlternateFunction_TIM8_1 1
    #define GPIO_AlternateFunction_LPTIM1 1
    #define GPIO_AlternateFunction_TIM1_2 2
    #define GPIO_AlternateFunction_TIM2_2 2
    #define GPIO_AlternateFunction_TIM3 2
    #define GPIO_AlternateFunction_TIM4 2
    #define GPIO_AlternateFunction_TIM5_2 2
    #define GPIO_AlternateFunction_TIM8_3 3
    // [etc]
    #define GPIO_AlternateFunction_EVENTOUT 15
     
     
    // -------- trivial loopdelay
     
    void LoopDelay(volatile uint32_t n) {
    	while(n > 0) n--;
    }
     
    #define DELAY_CONSTANT 50000
     
     
    // -------- main
    int main(void) {
     RCC->AHB2ENR |= 0
     | RCC_AHB2ENR_GPIOBEN
     | RCC_AHB2ENR_GPIOEEN
     ; 
     GPIOB->MODER = (GPIOB->MODER 
     & (~GPIO_MODER_MODER2) // RED LED
     ) | (0 
     | (GPIO_Mode_AlternateFunction * GPIO_MODER_MODER2_0) // RED LED
     );
     GPIOE->MODER = (GPIOE->MODER 
     & (~GPIO_MODER_MODER8) // GREEN LED
     ) | (0 
     | (GPIO_Mode_Out * GPIO_MODER_MODER8_0) // GREEN LED
     );
     
     GPIOB->AFRL = (GPIOB->AFRL
     & (~GPIO_AFRL_AFRL2) // RED LED
     ) | (0 
     | (GPIO_AlternateFunction_LPTIM1 * GPIO_AFRL_AFRL2_0) // RED LED
     );
     
     RCC->APB1ENR1 |= 0
     | RCC_APB1ENR1_LPTIM1EN
     ;
     LPTIM1->CR = 0 
     | LPTIM_CR_ENABLE
     ;
    /*
     LPTIM1->CFGR = 0
     | LPTIM_CFGR_WAVPOL // Waveform shape polarity 
     ;
    */
     LPTIM1->ARR = 0xFFFF; // must be changed only when _CR.ENABLE = 1
     LPTIM1->CMP = 0x7FFF;
     LPTIM1->IER = 0
     | LPTIM_IER_CMPMIE // enable interrupt upon compare match
     ;
     LPTIM1->CR = 0 
     | LPTIM_CR_ENABLE
     | LPTIM_CR_CNTSTRT // must be started only when _CR.ENABLE = 1
     ;
     NVIC_EnableIRQ(LPTIM1_IRQn);
     
     
     while(1) {
     
    /*
     GPIOE->BSRR = 0
     | GPIO_BSRR_BS_8 // GREEN LED - set
     ;
     LoopDelay(DELAY_CONSTANT);
     GPIOE->BSRR = 0
     | GPIO_BSRR_BR_8 // GREEN LED - reset
     ;
     LoopDelay(DELAY_CONSTANT);
    */
     
     
     }
    }
     
    // ISR
    void LPTIM1_IRQHandler(void) __attribute__((interrupt));
    void LPTIM1_IRQHandler(void) {
     static uint32_t blinkenCount;
     #define BLINKEN_NR 10
     
     LPTIM1->ICR = 0xFF; // as we have enabled only one, we don't care here, just summarily clear all interrupt sources
     
     blinkenCount++;
     if (blinkenCount == BLINKEN_NR) {
     GPIOE->BSRR = 0
     | GPIO_BSRR_BS_8 // GREEN LED - set
     ;
     } else if (blinkenCount >= 2 * BLINKEN_NR) {
     GPIOE->BSRR = 0
     | GPIO_BSRR_BR_8 // GREEN LED - reset
     ;
     blinkenCount = 0;
     }
    }

    H W
    H WAuthor
    Associate II
    Aug 7, 2018 9:44 AM UTC

    Hello Jan, thanks for your efforts. It was a signed/unsigned bug in my IR-Handler! (0x7fff was already suspicious ;)

    Regards

    HW