STM32: Triggering the ADC from a Timer.

The other day I needed to start an ADC conversion once per second, and it immediately became clear that this can't be done with the SMPR[2:1] bits, which set the number of cycles between samples, so I had to find another way.
The maximum number of cycles between samples that can be set with these bits is 239.5. If you add the conversion time of 12.5 cycles to this number, you get a conversion period of 252 cycles; in short, very far from 1 second.
Let me remind you that the following events can serve as the source of an external trigger for a regular conversion:
STM32: Triggering the ADC from a Timer.

That is, a conversion can be started on capture/compare (CCx event), on timer overflow (TRGO), on an external interrupt (EXTI line), or by setting the SWSTART bit. Triggering on timer overflow (TRGO) suits us; all that's left is to figure out how to make the timer generate that signal to start the ADC, but first let's initialize the ADC.

void ADC_Init(void)
{
   //enable port A clocking 
   RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
   //configure the ADC pin in analog input mode 
   GPIOA->CRL &= ~GPIO_CRL_CNF1;
   //since the ADC clock frequency must not exceed 14MHz 
   RCC->CFGR |= RCC_CFGR_ADCPRE_DIV8; 
   //enable ADC clocking 
   RCC->APB2ENR |= RCC_APB2ENR_ADC1EN ; 
   //start calibration and wait until it completes; this doesn't work in the simulator, but on hardware it's mandatory 
   /* ADC1->CR2 |= ADC_CR2_CAL; while (!(ADC1->CR2 & ADC_CR2_CAL))*/ 
   //enable the ADC interrupt 
   NVIC->ISER[0] |= NVIC_ISER_SETENA_18; 
   //7.5 cycles between samples for the first channel 
   ADC1->SMPR2 = ADC_SMPR2_SMP1_0; 
   //enable end-of-conversion interrupts 
   ADC1->CR1 = ADC_CR1_EOCIE;
   //enable triggering on timer3 match, enable the ADC, enable triggering by external signals, 
   ADC1->CR2 &= ~ADC_CR2_EXTSEL;
   ADC1->CR2 = ADC_CR2_EXTSEL_2 | ADC_CR2_ADON | ADC_CR2_EXTTRIG ;
   //sequence length is 1, first channel 
   ADC1->SQR1 = 0x00000000; 
   ADC1->SQR2 = 0x00000000;
   ADC1->SQR3 = 0x00000001; 
}

To make the timer generate the TRGO signal on overflow, this must be enabled with the MMS (Master mode selection) bits; then the timer initialization will look like this.

void Tim3_Init(void)
{
   //enable timer clocking
   RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;     
   TIM3->PSC = 7200 - 1;//10KHz or 0.0001s
   TIM3->ARR  = 10000; //period = 1sec
   //enable TRGO generation
   TIM3->CR2 |= TIM_CR2_MMS_1; 
   //start the timer
   TIM3->CR1 |= TIM_CR1_CEN;
}

Now let's put it all together.

#include "stm32f10x.h" 

unsigned temp;

void ADC1_2_IRQHandler(void)
{
   if(ADC1->SR & ADC_SR_EOC)
   {
      //write the result to a variable 
      temp =ADC1->DR;
   }
   //clear all flags in the status register
   ADC1->SR=0;
}
int main(void)
{
   ADC_Init();
   Tim3_Init();
   while(1)
   {
		
   }
}

As a result, the ADC will be started every second, and the conversion result can be obtained either in the end-of-conversion interrupt or using DMA.
I should say that in the simulator the program didn't work as it should: the interrupts sometimes came one after another with a pause of a few milliseconds, and sometimes once per second. Then I tried debugging the program on hardware, connecting via jtag and setting a breakpoint on the line temp =ADC1->DR;, but that didn't help; the program behaved the same as in the simulator. After looking for the error and not finding it, I tried using the SWO pin of the SWD interface for real-time debugging, and it turned out the code works almost correctly. In the interrupt, after checking for the end of conversion, I printed a message like "+1"; in 10 seconds I got 11 messages, i.e. a conversion starts right after the timer is enabled. I don't yet know why this happens; once I figure it out, I'll definitely fix it.