Skip to main content
Benjamin Brammer
Senior II
November 11, 2019
Solved

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

  • November 11, 2019
  • 2 replies
  • 1338 views

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

This topic has been closed for replies.
Best answer by waclawek.jan

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

2 replies

waclawek.jan
waclawek.janBest answer
Super User
November 11, 2019

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

Benjamin Brammer
Senior II
November 11, 2019

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.