Timer Behaves Strange in ChibiOS on STM32F030 NUCLEO

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
carldong
Posts: 62
Joined: Wed Oct 15, 2014 6:08 pm
Has thanked: 1 time
Been thanked: 1 time

Timer Behaves Strange in ChibiOS on STM32F030 NUCLEO

Post by carldong »

Hello,

I already have a bare-metal code for timer working with ChibiOS which simply blinks an LED. However, when I try to use the HAL in ChibiOS, the LED stops blinking. Interestingly, if I put a breakpoint in callback function, the led actually lights up / turns off as I continue. However, when no break points present, LED does not seem to change state. Using the same ARR/PSC configuration in my bare metal code gives a 2Hz blinking rate, but the ChibiOS one does not work.

Code: Select all

void vTIM3Init(void) {
  TIM_TypeDef *t = TIM3;
  uint16_t tmpcr1 = 0;

  RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;

  nvicEnableVector(STM32_TIM3_NUMBER, CORTEX_PRIORITY_MASK(STM32_GPT_TIM3_IRQ_PRIORITY));

  tmpcr1 = t->CR1;
 
  /* Auto reload buffer enabled.*/
  tmpcr1 |=  TIM_CR1_ARPE;
  tmpcr1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS);

  t->CR1 = tmpcr1;
 
  t->ARR = 24999;
  /* Update Interrupt TIM3 */
  t->DIER |= TIM_DIER_UIE;
  /* Prescale CLKAPB / 1 */
  t->PSC = 959;
  t->EGR |= TIM_EGR_UG;

  /* Update as TRGO */
  t->CR2 &= ~TIM_CR2_MMS;
  t->CR2 |= TIM_CR2_MMS_1;

  /* Enable Timer */
  t->CR1 |= TIM_CR1_CEN;
}

void STM32_TIM3_HANDLER(void) {
  if (TIM3->SR & 0x0001) {
    if (!ledval) {
      GPIOA->BSRR.H.set = 1 << 5;
      ledval = 1;
    }
    else {
      GPIOA->BSRR.H.clear = 1 << 5;
      ledval = 0;
    }
  }
  TIM3->SR &= ~TIM_SR_UIF;
}


The followint is the ChibiOS code:

Code: Select all

static const GPTConfig xTIM3Config = {
  50000,
  cbTIM3
};

void vTIM3Config(void) {
  gptStart(&GPTD3, &xTIM3Config);
  gptStartContinuous(&GPTD3, 25000);
}

void cbTIM3(GPTDriver* gptp) {
  /* (void) gptp; */
  if (TIM3->SR & 0x0001) {
    if (!ledval) {
      GPIOA->BSRR.H.set = 1 << 5;
      ledval = 1;
    }
    else {
      GPIOA->BSRR.H.clear = 1 << 5;
      ledval = 0;
    }
  }
  TIM3->SR &= ~TIM_SR_UIF;
}
carldong
Posts: 62
Joined: Wed Oct 15, 2014 6:08 pm
Has thanked: 1 time
Been thanked: 1 time

Re: Timer Behaves Strange in ChibiOS on STM32F030 NUCLEO

Post by carldong »

OK, I found the problem. In

Code: Select all

gpt_lld.c:gpt_lld_serve_interrupt()
, the TIM_SR register is cleared before entering the callback function. Since my callback function

Code: Select all

cbTIM3()
tries to condition using

Code: Select all

TIM3->SR
, it won't work on *regular* operation. While debugging, the timer is still running, and an overflow triggers that flag, but the MCU is blocked in debugging, so the LED can change state.
Post Reply