STM32: basic timer tim6 L432 falscher Increment

Gast #6553859
Lesenswert?

main.c
1
//global
2
volatile uint32_t tim6_cnt = 0;
3
volatile uint8_t tim6_irq = 0;
4

5
int main(void)  {
6
    //
7

8
    /* TIM6 interrupt Init */
9
    NVIC_SetPriority(TIM6_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
10
    NVIC_EnableIRQ(TIM6_IRQn);
11
    RCC->APB1ENR1 |= RCC_APB1ENR1_TIM6EN;
12
    TIM6->CR1 = 0;      // stop timer
13
    TIM6->EGR = 1;      // event generation register
14
    TIM6->ARR = 10000;  // 16 bits auto reload register
15
    TIM6->PSC = 8000;   // 16bits prescaler
16
    TIM6->CNT = 0;      // 16bits counter value
17
    TIM6->DIER = 1;     // DMA interrupt enable register
18
    TIM6->CR2 = 0;      // control register 2 with master mode selection
19
    TIM6->SR = 0;       // clear interrupt flag
20
    TIM6->CR1 = TIM_CR1_URS | TIM_CR1_ARPE | TIM_CR1_CEN;     // start timer
21

22
    while(1) {
23
        tim6_irq = 0;
24
        while(tim6_irq == 0) {}
25
        LL_GPIO_SetOutputPin(LED1_PB3_GPIO_Port, LED1_PB3_Pin);
26

27
        tim6_irq = 0;
28
        while(tim6_irq == 0) {}
29
        LL_GPIO_ResetOutputPin(LED1_PB3_GPIO_Port, LED1_PB3_Pin);
30

31
        cnt += 1;
32
        UART2_print_uint32(tim6_cnt, "tim6_cnt = ");
33
    }
1
void TIM6_IRQHandler(void) {
2
    extern volatile uint32_t tim6_cnt;
3
    extern volatile uint8_t tim6_irq;
4
    tim6_cnt += 1;
5
    tim6_irq = 1;
6
    TIM6->SR = 0;   // clear after intterupt event => otherwise stopped
7
}

$ arm-none-eabi-nm build/tim6_432.elf | grep -i tim6
200001f4 B tim6_cnt
08000fb8 T TIM6_DAC_IRQHandler
200001f8 B tim6_irq

ist nicht W = weak
08000fb8 T TIM6_DAC_IRQHandler

80MHz werden auf 1 Sekunden heruntergeteilt.
LED1_PB3_Pin schaltet für 1 Sekunde 1 und für eine Sekunde aus.

output sieht so aus
tim6_cnt = 00000005<\r>
tim6_cnt = 00000009<\r>
tim6_cnt = 0000000D<\r>
tim6_cnt = 00000011<\r>
tim6_cnt = 00000015<\r>

Hat jemand eine Idee warum Increment 4 ist?
Ich bin im Refman vom L432 die Register durchgegangen und
kann es mir nicht erklären. Google gibt dazu auch nicht viel aus.

Gruß,
Entwickler
Gast #6554175
Lesenswert?

TIM6->SR = 0;
als letzter Befehl ist keine gute Idee. Bis das durch die Pipeline 
rasselt, hat der IRQ noch einmal ausgeloest. Loesche SR am Anfang des 
Handlers und alles wird gut. Dann sollte die erwartetet Differenz von 2 
ausgegeben werden

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren