Skip to main content
SKrey.1
Associate II
August 26, 2020
Question

Trigger TIM2 using a GPIO as external trigger

  • August 26, 2020
  • 5 replies
  • 2010 views

Hi,

on a STM32L451CE I try to make use of TIM2 timer. I want to start this timer on an external trigger event. From the device datasheet I know that the pin PA0 can be used with AF14 as a TIM2_ETR. However when I setup the timer it doesnt start counting, although I received a rising edge on that line. Can you help me why its not start counting?

Here are the relevant code parts I've written to enable the mechanism:

#define TIM2_CCMR1 (volatile uint32_t *) (TIM2_BASE + 0x18)
#define TIM2_CCER (volatile uint16_t *) (TIM2_BASE + 0x20)
#define TIM2_SMCR (volatile uint32_t *) (TIM2_BASE + 0x08)
 
 
void Init()
{
	LL_GPIO_StructInit(&GPIO_InitStruct);
	// Serializier BNE
	GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
	GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
	GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
	GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
	GPIO_InitStruct.Alternate = LL_GPIO_AF_14;
	LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
	LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
	
	*TIM2_CCMR1 &= 0xFFFFFFFC;
 *TIM2_CCMR1 |= 0x1;
 *TIM2_CCER &= 0xFFFFFFFC;
 *TIM2_CCER |= 0x2;
 *TIM2_SMCR &= 0xFFFFFFF8;
 *TIM2_SMCR |= 0x6;
 *TIM2_SMCR &= 0xFFFFFF8F;
 *TIM2_SMCR |= 0x50;
}

What am I missing here?

Any help is appreciated, if you need additional info please let me know.

Thanks, Steve

This topic has been closed for replies.

5 replies

waclawek.jan
Super User
August 26, 2020

For ETR, you need to set SMCR.TS=0b111.

Read out and check/post the content of TIM2 and the relevant GPIO registers. Did you enable TIM2 and GPIOA clock in RCC?

Style: why don't you use symbols, or at least the peripherals' struts (ie. TIM2->CCER etc.), defined in the device header?

JW

SKrey.1
SKrey.1Author
Associate II
August 26, 2020

Hello Jan,

thanks for your answer. Yeah setting SMCR.TS to 111 seems to do it.

However I wonder if my approach is correct overall. What I actually want to do is to trigger a DMA transfer for SPI transmit, however I want that the DMA is not triggered based on TXE event but on an external trigger (via a PIN). Would this be possible via the timers? I mean I got it counting now, however what I actually want is to forward this event to the DMA. What kind of peripheral request I should choose for the DMA? And what mode to choose for the timer so that the event can be forwarded to the DMA in order to trigger SPI transmission?

@style, yeah I couldn't found any abstraction this is why I choosed to define the addresses for my own, however I'll have a look at the header file.

Thanks for any hints on this issue.

waclawek.jan
Super User
August 26, 2020

> What kind of peripheral request I should choose for the DMA?

TIMx_DIER.TDE

> @style, yeah I couldn't found any abstraction

I don't know what do you mean by "abstraction" here. Maybe you meant examples. It's a shame ST does not provide clean examples, they just promote the Cube stuff. A request for this here, please vote if you agree. Examples e.g. see 'F0/'L0 Snippets at ST, the same as appendix in 'F0/'L0 RMs, I have some at efton dot sk, there are more out there if you search.

JW

SKrey.1
SKrey.1Author
Associate II
August 26, 2020

Hi,

no with the term "abstraction" I was referring to symbols to use instead of my own macros. Anyway thanks for the answer.

Can you also give me a hint which dma / channel to use ? Is it DMA1.Ch5 request 0110 ? (Table 41 of the TRM)

Thanks, Steve

waclawek.jan
Super User
August 26, 2020

Steve,

My copy of RM0394 rev4 Tab.41 shows no connection on DMA1/Ch5/sel.0110... Can you please recheck/post a screenshot?

Actually, I see no DMA trigger for TIM2_TRG, so you may want to use the other AF from the same pin to TIM2_CH1 and trigger DMA from that.

JW