cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F302R8. TIM2 as output and TIM15 as input does not work for me.

JPere.9
Associate II

I have the STM32F302R8 board and I have this problem.

The clock is at default 8 Mhz.

TIM2 is configured with toggle output CH1 on PA0. (This works fine).

TIM15 is configured as input capture CH1 on PA2.

I have a jumper between PA0 and PA2.

The aim is that when TIM2 reaches the value of CCR1 it triggers the PA0 pin, which happens, and having the jumper with the PA2 pin should trigger the TIM15 input but this does not happen.

The while that checks the CC1IF flag never ends, it doesn't detect anything.

What can it be?

while (1)
{
  // wait until input edge is captured
  while ( !(TIM15->SR & TIM_SR_CC1IF)) {} 
  timestamp = TIM15->CCR1;		// read captured counter value
}
 
void mi_GPIO_Init ( void ) {
  RCC->AHBENR  |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN;
  GPIOA->MODER  &= ~GPIO_MODER_MODER0;	 // clear PA0 mode
  GPIOA->MODER  |=  GPIO_MODER_MODER0_1; // set pin to alt function
  GPIOA->AFR[0] &= ~GPIO_AFRL_AFRL0; 	 // clear pin AF bits
  GPIOA->AFR[0] |=  0x0000001;	         // set pin to AF1 for TIM2_CH1
 
  GPIOA->MODER  &= ~GPIO_MODER_MODER2;	 // clear PA2 mode
  GPIOA->MODER  |=  GPIO_MODER_MODER2_1; // set pin to alt function
  GPIOA->AFR[0] &= ~GPIO_AFRL_AFRL2;	 // clear pin AF bits
  GPIOA->AFR[0] |=  0x0000900;			 // set pin to AF9 for TIM15_CH1
 
  // Configure TIM2 to wrap around at 1 Hz and toggle CH1 output when the counter value is 0
  RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;	// enable TIM2 clock
  TIM2->PSC = 800 - 1;			// divided by 800
  TIM2->ARR = 10000 -1;			// divided by 10000
  TIM2->CCMR1 = TIM_CCMR1_OC1M_0 | TIM_CCMR1_OC1M_1; // set output to toggle on match
  	  	  	  	  	  	  	  	// The rest of the bits are set to 0.
  TIM2->CCR1 = 0;				// set match value
  TIM2->CCER |= TIM_CCER_CC1E;	// enable CH1 compare mode
  TIM2->CNT = 0;				// clear timer counter
  TIM2->CR1 |= TIM_CR1_CEN;		// enable TIM2
 
  // Configure TIM15 to do input capture
  RCC->APB2ENR |= RCC_APB2ENR_TIM15EN;	// enable TIM15 clock
  TIM15->PSC = 8000 - 1;			// divided by 800
  TIM15->ARR = 0xFFFF;			// count until ARR
  TIM15->CCMR1 &= ~TIM_CCMR1_CC1S; // set CH1 to capture at every edge
  TIM15->CCMR1 |= TIM_CCMR1_CC1S_0; // CC1 as input, IC1 is mapped on TI1
  TIM15->CCER |= TIM_CCER_CC1E;	// enable CH1 capture rising edge
  TIM15->CR1 |= TIM_CR1_CEN;	// enable TIM15
}

1 ACCEPTED SOLUTION

Accepted Solutions
Petr DAVID
ST Employee

Hi JPere.9,

so I was going through your code and it looks fine to me. Only difference I would make is to move enabling the peripheral clock further away from lines where you are editing the registers. In few manuals there is recommended to add delay after RCC peripheral clock enabling. So maybe you could move the lines as up like this:

void mi_GPIO_Init ( void ) {
  RCC->AHBENR  |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN;
  RCC->APB2ENR |= RCC_APB2ENR_TIM15EN;	// enable TIM15 clock
  RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;	// enable TIM2 clock
 
  GPIOA->MODER  &= ~GPIO_MODER_MODER0;	 // clear PA0 mode
  GPIOA->MODER  |=  GPIO_MODER_MODER0_1; // set pin to alt function
  GPIOA->AFR[0] &= ~GPIO_AFRL_AFRL0; 	 // clear pin AF bits
  GPIOA->AFR[0] |=  0x0000001;	         // set pin to AF1 for TIM2_CH1
 

But I do not think that would be the problem. Are you using Nuleo-64 board? I would recommend you to check the hardware connections. If you have Nucleo-64 board MB1136C then by default PA2 is connected to ST-Link USART RX pin. If you would like to use the the arduino or morpho connector assigned to PA2 for the connection to PA0, then you would need to check you have solder bridge SB13 open and SB63 closed. Let me know if any this information helped you solve the problem.

Best regards,

Petr David

View solution in original post

5 REPLIES 5
Petr DAVID
ST Employee

Hi JPere.9,

so I was going through your code and it looks fine to me. Only difference I would make is to move enabling the peripheral clock further away from lines where you are editing the registers. In few manuals there is recommended to add delay after RCC peripheral clock enabling. So maybe you could move the lines as up like this:

void mi_GPIO_Init ( void ) {
  RCC->AHBENR  |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN;
  RCC->APB2ENR |= RCC_APB2ENR_TIM15EN;	// enable TIM15 clock
  RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;	// enable TIM2 clock
 
  GPIOA->MODER  &= ~GPIO_MODER_MODER0;	 // clear PA0 mode
  GPIOA->MODER  |=  GPIO_MODER_MODER0_1; // set pin to alt function
  GPIOA->AFR[0] &= ~GPIO_AFRL_AFRL0; 	 // clear pin AF bits
  GPIOA->AFR[0] |=  0x0000001;	         // set pin to AF1 for TIM2_CH1
 

But I do not think that would be the problem. Are you using Nuleo-64 board? I would recommend you to check the hardware connections. If you have Nucleo-64 board MB1136C then by default PA2 is connected to ST-Link USART RX pin. If you would like to use the the arduino or morpho connector assigned to PA2 for the connection to PA0, then you would need to check you have solder bridge SB13 open and SB63 closed. Let me know if any this information helped you solve the problem.

Best regards,

Petr David

Read out and check/post content of relevant GPIO and TIM registers.

Disconnect the jumper, set TIM15 to Compare , toggle the pin, and observe (this is aimed at any hardware problem with PA2 - short, bad solder joint).

Restore the original program, toggle a GPIO pin when the condition in while() is fulfilled, and observe (this is aimed at possible incorrect way of observation of the TIM15_SR.CC1IF bit being set).

JW

PS. @Petr DAVID​ , while it's a good idea to disconnect the on-board connections, in this particular case, why would connection to STLink's *Rx* signal interfere?

Hi @Community member,

you are right that the SB13 could stay closed as it would not matter for the functionality. The important part of my previous reply is, that by default the SB63 on MB1136 is not present and it could be the reason why the code is not seems to be working. Thank you for pointing that out.

Petr

Hi @Petr DAVID​ ,

> The important part of my previous reply is, that by default the SB63 on MB1136 is not present

... and I overlooked that part... :)

Jan

JPere.9
Associate II

Hi, thank you for your replies.

I use the nucleo-64 board.

I moved the clock trigger lines to the beginning of the code and instead of PA2 I use PB14 and now it works fine.

Cheers.