Beispiel #1
0
uint32_t attachTimerCallback(uint8_t timer, ISRType ISR, uint32_t usDelay) {
  // Get the correct PCLK
  RCC_ClocksTypeDef RCC_Clocks;
  RCC_GetClocksFreq(&RCC_Clocks);
  uint32_t PCLK = RCC_Clocks.PCLK1_Frequency;// Frequency in Hz
  TIM_TypeDef *TIMx = TIMER_MAP[timer].TIMx;
  // For timers on APB2 use PCLK2
#if defined(STM32F446xx)
  if (TIMx == TIM1 || TIMx == TIM8 || TIMx == TIM9 || TIMx == TIM10 || TIMx == TIM11)
    PCLK = RCC_Clocks.PCLK2_Frequency;
#elif defined(SERIES_STM32F30x)
  if (TIMx == TIM1 || TIMx == TIM8 || TIMx == TIM15 || TIMx == TIM16 || TIMx == TIM17)
    PCLK = RCC_Clocks.PCLK2_Frequency;
#endif

  // Set the update ISR
  TimerInfo *cfg = &TIMER_MAP[timer];
  cfg->isr = ISR;

  // Start interrupts with low priority
  uint8_t priority = 0xd;
  nvicEnable(TIMER_MAP[timer].IRQn, priority);

  // Start 1MHz timebase
  TIM_Cmd(TIMx, DISABLE);
  TIM_DeInit(TIMx);
  // FIXME always getting half of what expected...
  timerInitHelper(timer, PCLK/(1000000)-1, usDelay-1);
  TIMx->CNT = 0;
  TIM_ClearITPendingBit(TIMx, TIM_IT_Update);
  TIM_ITConfig(TIMx, TIM_IT_Update, ENABLE);
  TIM_Cmd(TIMx, ENABLE);

  return PCLK;
}
Beispiel #2
0
void timerInit(uint8_t timer, int freqHz) {
  // Enable interrupts for the timer (but not any of the timer updates yet)
  nvicEnable(TIMER_MAP[timer].IRQn, 2);

  timerInitHelper(timer, 1, TIMER_PERIOD(freqHz));
  TIM_Cmd(TIMER_MAP[timer].TIMx, ENABLE);
}
Beispiel #3
0
// Helper function
void setupTimerUpdateInterrupt(uint8_t i, ISRType ISR, uint16_t prescaler, uint32_t period) {
  uint8_t timer = TIMEBASE_MAP[i].timer;
  timerInitHelper(timer, prescaler, period);
  TIM_ITConfig(TIMER_MAP[timer].TIMx, TIM_IT_Update, ENABLE);
  TIM_Cmd(TIMER_MAP[timer].TIMx, ENABLE);
  // Start interrupts with low priority
  uint8_t priority = (i==0) ? 0xe : 0xf;
  nvicEnable(TIMER_MAP[timer].IRQn, priority);
  
  TIMEBASE_MAP[i].isr = ISR;
}
Beispiel #4
0
void Encoder::init(uint8_t pin1, uint8_t pin2, uint16_t maxCount) {
  // NEW: read from pin config.
  timer = PIN_MAP[pin1].timer;
  pinModeAlt(pin1, GPIO_OType_PP, GPIO_PuPd_UP, PIN_MAP[pin1].altFunc);
  pinModeAlt(pin2, GPIO_OType_PP, GPIO_PuPd_UP, PIN_MAP[pin2].altFunc);

  // Start timer
  timerInitHelper(timer, 0, maxCount);

  // Configure input capture
  TIM_ICInitTypeDef TIM_ICInitStruct;
  // filter is between 0x0 and 0xF, 0 for no filter, 0xF for a lot
  TIM_ICInitStruct.TIM_ICFilter = 0x0;
  TIM_ICInitStruct.TIM_Channel = TIM_Channel_1;
  TIM_ICInit(TIMER_MAP[timer].TIMx, &TIM_ICInitStruct);
  TIM_ICInitStruct.TIM_Channel = TIM_Channel_2;
  TIM_ICInit(TIMER_MAP[timer].TIMx, &TIM_ICInitStruct);
  TIM_EncoderInterfaceConfig(TIMER_MAP[timer].TIMx, TIM_EncoderMode_TI12, TIM_ICPolarity_Falling, TIM_ICPolarity_Falling);
  
  // Enable
  TIM_Cmd(TIMER_MAP[timer].TIMx, ENABLE);
}
Beispiel #5
0
void pinTimerInit(uint8_t pin) {
  uint8_t timer = PIN_MAP[pin].timer;

  nvicEnable(TIMER_MAP[timer].IRQn, 2);
  // Use the frequency set using analogWriteFrequency
  timerInitHelper(timer, 1, TIMER_PERIOD(TIMER_MAP[timer].freqHz));
  TIM_Cmd(TIMER_MAP[timer].TIMx, ENABLE);
  
  // FIXME: Advanced timers need to be specified in variant.cpp
  // isAdvancedTimer() ir something
  // http://www.disca.upv.es/aperles/arm_cortex_m3/curset/STM32F4xx_DSP_StdPeriph_Lib_V1.0.1/html/group___t_i_m___group4.html
#if defined(SERIES_STM32F4xx)
  if (TIMER_MAP[timer].TIMx == TIM1) {
    TIM_CtrlPWMOutputs(TIM1, ENABLE);
  }
#endif
#if defined(SERIES_STM32F30x)
  if (IS_TIM_LIST6_PERIPH(TIMER_MAP[timer].TIMx)) {
    TIM_CtrlPWMOutputs(TIMER_MAP[timer].TIMx, ENABLE);
  }
#endif
}