Generate N pulse with a fix frequency STM32f107
I can think of 3 possible ways to generate N pulses.

The first method is obvious: Generate a PWM with 50% duty cycle using a TIM module. Count overflows with update event interrupt. In the interrupt, stop TIM when you reach N overflows. This method has some interrupt overhead and may not be suitable if the required frequency is high.

The second method is a little bit strange: You can abuse an SPI module. In this case your frequency and N options are limited: You can only choose one of the possible SPI clock options and N must be divisible by 8 (Not STM32F107 but some other devices may provide more flexible options for N). Using DMA, send N / 8 bytes, and the SPI clock pin will generate your N pulses. This methods doesn't need interrupts, but you may set a DMA transfer complete interrupt to be notified when the transmission of the N pulses is completed.

This one is the modified version of the first method. Instead of counting pulses in the interrupt service routine (ISR), you can configure another TIM module to be the slave of the first one. The master TIM generates the pulses as usual, but each update event causes slave timer to increase its counter value. You can configure the slave timer to generate interrupt when the required pulse count is reached. Compared to the first method, this method has no periodic interrupt overhead, but it costs you an additional TIM module. Also in very high frequencies and in the presence of other high priority interrupts, you may get additional pulses as the ISR may need some time to find a chance to run and disable the master timer.