7 years, 8 months ago.

Interrupt driven comparator

I want to set up an interrupt driven comparator on a xDot (STM32L151CC). This is what I basically tried (inspired by https://github.com/bjornfor/stm32-test/tree/master/STM32L1xx_StdPeriph_Lib_V1.1.1/Project/STM32L1xx_StdPeriph_Examples/COMP/COMP_Interrupt):

COMP_HandleTypeDef Comp1Handle;
DigitalOut myled(LED1);
AnalogIn ain(PB_0);

void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp) {
  myled = myled ? 0 : 1;
}

int main() {
  Comp1Handle.Instance = COMP1;
  Comp1Handle.Init.NonInvertingInput = COMP_NONINVERTINGINPUT_PB0;
  Comp1Handle.Init.NonInvertingInputPull = COMP_NONINVERTINGINPUT_NOPULL;
  Comp1Handle.Init.TriggerMode = COMP_TRIGGERMODE_IT_RISING_FALLING;
  HAL_COMP_Init(&Comp1Handle);
  HAL_COMP_Start_IT(&Comp1Handle);
  while ( true ) { wait(1); }
}

But the interrupt handler is never called. What should I do to get this running?

Many thanks,

Mark

Well, I think I figured it out... For a comparator on PB0 which generates interrupts:

void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp) {
    // Add application code
}

void irq_comp_handler(void) {
    HAL_COMP_IRQHandler(&COMP_HandleStruct);
}

void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp) {
    GPIO_InitStruct.Pin = GPIO_PIN_0;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    __HAL_RCC_COMP_CLK_ENABLE();

    NVIC_SetVector(COMP_IRQn, (uint32_t)irq_comp_handler);
    HAL_NVIC_EnableIRQ(COMP_IRQn);
    HAL_NVIC_SetPriority(COMP_IRQn, 3, 0);
}

void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp) {
    // TO DO
}

int main() {
    COMP_HandleStruct.Instance = COMP1;
    COMP_HandleStruct.Init.NonInvertingInput = COMP_NONINVERTINGINPUT_PB0;
    COMP_HandleStruct.Init.NonInvertingInputPull = COMP_NONINVERTINGINPUT_NOPULL;
    COMP_HandleStruct.Init.TriggerMode = COMP_TRIGGERMODE_IT_FALLING;
    HAL_COMP_Init(&COMP_HandleStruct);

    HAL_COMP_Start_IT(&COMP_HandleStruct);
    ....
}

Especially NVIC_SetVector(...) was not obvious to me. I expected that the HAL driver would call HAL_COMP_TriggerCallback(), but that is not the case.

posted by Mark Ruys 23 Mar 2017
Be the first to answer this question.