Interconnect comparator to timer internally not working
Hello,
I have a nucleof303k8 board on which I implemented a hysteresis current control.
This is done with the COMP2. I would like to know the average voltage which is connected to the inductive load in order to achieve the desired current.
My idea was to connect the comparator output to a timer input capture, which could then use dma to store the switching times, which are perfect to calculate this value.
However, I cannot get the comparator to trigger an input capture on the timer.
The comparator is switching correctly, and I can trigger the input capture with an input pin.
The init code of the comparator:
void MX_COMP2_Init(void)
{
LL_COMP_InitTypeDef COMP_InitStruct = {0};
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
/**COMP2 GPIO Configuration
PA7 ------> COMP2_INP
PA12 ------> COMP2_OUT
*/
GPIO_InitStruct.Pin = LL_GPIO_PIN_7;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = LL_GPIO_PIN_12;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
GPIO_InitStruct.Alternate = LL_GPIO_AF_8;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* COMP2 interrupt Init */
NVIC_SetPriority(COMP2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
NVIC_EnableIRQ(COMP2_IRQn);
COMP_InitStruct.InputPlus = LL_COMP_INPUT_PLUS_IO1;
COMP_InitStruct.InputMinus = LL_COMP_INPUT_MINUS_DAC1_CH2;
COMP_InitStruct.OutputSelection = LL_COMP_OUTPUT_TIM3_IC1;
COMP_InitStruct.OutputPolarity = LL_COMP_OUTPUTPOL_NONINVERTED;
COMP_InitStruct.OutputBlankingSource = LL_COMP_BLANKINGSRC_NONE;
LL_COMP_Init(COMP2, &COMP_InitStruct);
}The init code of the timer:
void MX_TIM3_Init(void)
{
LL_TIM_InitTypeDef TIM_InitStruct = {0};
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
/* Peripheral clock enable */
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
/**TIM3 GPIO Configuration
PA6 ------> TIM3_CH1
*/
GPIO_InitStruct.Pin = LL_GPIO_PIN_6;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
GPIO_InitStruct.Alternate = LL_GPIO_AF_2;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* TIM3 DMA Init */
/* TIM3_CH1_TRIG Init */
LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_6, LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_6, LL_DMA_PRIORITY_LOW);
LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_6, LL_DMA_MODE_CIRCULAR);
LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_6, LL_DMA_PERIPH_NOINCREMENT);
LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_6, LL_DMA_MEMORY_INCREMENT);
LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_6, LL_DMA_PDATAALIGN_HALFWORD);
LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_6, LL_DMA_MDATAALIGN_HALFWORD);
/* TIM3 interrupt Init */
NVIC_SetPriority(TIM3_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
NVIC_EnableIRQ(TIM3_IRQn);
TIM_InitStruct.Prescaler = 0;
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
TIM_InitStruct.Autoreload = 0xffff;
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
LL_TIM_Init(TIM3, &TIM_InitStruct);
LL_TIM_DisableARRPreload(TIM3);
LL_TIM_SetClockSource(TIM3, LL_TIM_CLOCKSOURCE_INTERNAL);
LL_TIM_SetTriggerOutput(TIM3, LL_TIM_TRGO_UPDATE);
LL_TIM_DisableMasterSlaveMode(TIM3);
LL_TIM_IC_SetActiveInput(TIM3, LL_TIM_CHANNEL_CH1, LL_TIM_ACTIVEINPUT_DIRECTTI);
LL_TIM_IC_SetPrescaler(TIM3, LL_TIM_CHANNEL_CH1, LL_TIM_ICPSC_DIV1);
LL_TIM_IC_SetFilter(TIM3, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV1);
LL_TIM_IC_SetPolarity(TIM3, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_BOTHEDGE);
}And how I start the comparator and the timer:
LL_COMP_Enable(COMP2); //reference is provided by dac
TIM3->CCER &= ~TIM_CCER_CC1E;
TIM3->CCMR1 &= ~TIM_CCMR1_CC1S_Msk;
TIM3->CCMR1 = 01 << TIM_CCMR1_CC1S_Pos;
//Enable Capture-compare on channel1
TIM3->CCER |= TIM_CCER_CC1P_Msk | TIM_CCER_CC1NP_Msk; //both edges
LL_TIM_EnableCounter(TIM3);
LL_TIM_EnableIT_CC1(TIM3);
TIM3->CCER |= TIM_CCER_CC1E;
With this setup, the comparator is working (output is visible on a led), the input capture can be triggered with a jumper, but the comparator cannot trigger an input capture.
Am I missing some additional register which needs to be set in order to enable the comparator input?
Thank you for the help!
