> I now capture the input of PA3
Is it externally connected to PA2? Fine, then you can use timer 2 input capture 3 and 4 DMA requests to control any GPIO pin you like, provided the necessary resources, TIM2 channels 3 and 4 and DMA1 channels 1 and 7 are free.
- Map PA3 to TIM2_CH4, PA8 as GPIO output.
- Define two memory constants. When copied to GPIOA->BSRR, they would set the output pin high or low
volatile uint32_t pa8_low = (1 << 24);
volatile uint32_t pa8_high = (1 << 8);
- Set up DMA1 channel 1 to copy one word in circular mode from &pa8_low to &GPIOA->BSRR
DMA1_Channel1->CMAR = (uint32_t)&pa8_low;
DMA1_Channel1->CPAR = (uint32_t)&GPIOA->BSRR;
DMA1_Channel1->CNDTR = 1;
DMA1_Channel1->CCR =
DMA_CCR_MSIZE_1 | // memory size 32 bits
DMA_CCR_PSIZE_1 | // peripheral size 32 bits
DMA_CCR_CIRC | // circular mode
DMA_CCR_DIR | // memory to peripheral
DMA_CCR_EN; // enable channel
- Set DMA channel 7 the same way, but with &pa8_high instead
- Configure TIM2 input capture channels 3 and 4 to capture on opposite edges of TI4
TIM2->CCMR2 =
TIM_CCMR2_CC3S_1 | // 10: CC3 channel is configured as input, IC3 is mapped on TI4
TIM_CCMR2_CC4S_0; // 01: CC4 channel is configured as input, IC4 is mapped on TI4
TIM2->CCER =
TIM_CCER_CC3E | // enable capture on channel 3
TIM_CCER_CC4E | // enable capture on channel 4
TIM_CCER_CC4P; // invert capture edge on channel 4
- Enable DMA requests on TIM2 channels 3 and 4
TIM2->DIER = TIM_DIER_CC3DE|TIM_DIER_CC4DE;
TIM2->CR1 = TIM_CR1_CEN;