cancel
Showing results for 
Search instead for 
Did you mean: 

writing to timer CR1 register before MX_TIM_Init() doesn't work

Benjamin Brammer
Senior II

Hello!

Can anybody explain to me why the following code is not writing to the TIM2 and TIM3 CR1 register:

int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
  
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* timers will only generate an UI when the counter wraps around */
  TIM2->CR1 |= TIM_CR1_URS;
  TIM3->CR1 |= TIM_CR1_URS;
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_ADC1_Init();
  MX_OPAMP1_Init();
  MX_OPAMP2_Init();
  MX_TIM2_Init();
  MX_TIM15_Init();
  MX_USB_DEVICE_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */

but when I move the register writes after the peripheral initializations it works:

int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
  
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_ADC1_Init();
  MX_OPAMP1_Init();
  MX_OPAMP2_Init();
  MX_TIM2_Init();
  MX_TIM15_Init();
  MX_USB_DEVICE_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */
 
  /* timers will only generate an UI when the counter wraps around */
  TIM2->CR1 |= TIM_CR1_URS;
  TIM3->CR1 |= TIM_CR1_URS;

I don't understand this.

best regards

Benjamin

1 ACCEPTED SOLUTION

Accepted Solutions

You should look perhaps into the MX_TIMx_Init() routines, and then into the routines they call. Cube is open source.

My guess is, the clocks for timers are enabled only in those _Init() regisers, and/or they might overwrite TIMx_CR1 themselves.

I don't Cube/CubeMX.

JW

View solution in original post

2 REPLIES 2

You should look perhaps into the MX_TIMx_Init() routines, and then into the routines they call. Cube is open source.

My guess is, the clocks for timers are enabled only in those _Init() regisers, and/or they might overwrite TIMx_CR1 themselves.

I don't Cube/CubeMX.

JW

Hey Jan,

thanks for your answer. Yes the clocks are enabled inside these init functions, but I did not know, that without these clock no register write is possible. But that explains that behaviour.