How to Trigger ADC1 with TIM1_CH4?
On the STM32F401RE, I'm trying to get a compare match on CC4 on TIM1 to trigger an ADC conversion in injected mode, but the ADC is never seeing the trigger.
How do I set TIM1 to generate the TIM1_CH4 trigger on a compare match on CC4?
TIM1->CR2 has an MMS field, which can be set to 0b111 to generate TRGO from CC4REF, but there's nothing here to indicate how to set TIM1 to generate TIM1_CH4 from CC4REF.
As described in the reference manual, the ADC has many injected group trigger options, including TIM1_TRGO and TIM1_CH4, but nothing in the TIM1/TIM8 section of the manual says anything about how to enable TIM1_CH4. Is it always enabled?
I'm using TIM1 to generate three PWM outputs, on channels 0-3, for motor control, and I'm using channel 4 to generate a ADC trigger to read motor phase currents with the right timing. When I set channel 4 to interrupt on a compare match, I do get those interrupts, indicating a compare match has occurred, but it either doesn't generate a TIM1_TRGO or a TIM1_CH4 trigger or the ADC never sees it, because the ADC never starts an injected group conversion.
I've tried changing the settings to use TIM1_TRGO to trigger the ADC, but that didn't work either.
I have the following settings for:
TIM1->CR1 = TIM_CR1_ARPE | (0b10 << TIM_CR1_CMS_Pos);
TIM1->CR2 = 0b111 << TIM_CR2_MMS_Pos;
TIM1->SMCR = 0;
TIM1->DIER = TIM_DIER_CC4IE;
TIM1->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_0 | TIM_CCMR1_OC1PE |
TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2M_0 | TIM_CCMR1_OC2PE;
TIM1->CCMR2 = TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1 | TIM_CCMR2_OC3M_0 | TIM_CCMR2_OC3PE |
TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4M_1 | TIM_CCMR2_OC4M_0 | TIM_CCMR2_OC4PE;
TIM1->CCER = TIM_CCER_CC1E | TIM_CCER_CC2E | TIM_CCER_CC3E;
TIM1->BDTR = TIM_BDTR_MOE; And for the ADC:
ADC1[0]->CR1 = 0;
ADC1[0]->CR1 = ADC_CR1_JEOCIE;
ADC1[0]->CR2 = (0b01 << ADC_CR2_JEXTEN_Pos) | (0b0001 << ADC_CR2_JEXTSEL_Pos) | ADC_CR2_ADON;
ADC1[0]->SMPR1 = (0b001 << ADC_SMPR1_SMP10_Pos) | (0b001 << ADC_SMPR1_SMP11_Pos);
ADC1[0]->SMPR2 = (0b01 << ADC_SMPR2_SMP0_Pos);
ADC1[0]->JSQR = (0 << ADC_JSQR_JL_Pos) | (0 << ADC_JSQR_JSQ1_Pos);
ADC->CCR |= (0b11 << ADC_CCR_ADCPRE_Pos); 