No timer output on pin in F4 core

Post here all questions related to LibMaple core if you can't find a relevant section!
ag123
Posts: 1656
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: No timer output on pin in F4 core

Post by ag123 »

thanks would try this as well :)
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: No timer output on pin in F4 core

Post by stevestrong »

Can you please post your complete working code? I am still unable to make it work :?
ag123
Posts: 1656
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: No timer output on pin in F4 core

Post by ag123 »

Code: Select all

void initTesttimer(void) {

	Timer2.init();
	Timer2.pause();
	//enable preload
	TIMER2->regs.gen->CR1 |= TIMER_CR1_ARPE;
	timer_set_mode(TIMER2, TIMER_CH2, TIMER_PWM);
	//timer_oc_set_mode(TIMER2,TIMER_CH2,TIMER_OC_MODE_PWM_1,TIMER_OC_PE);
	//timer_cc_enable(TIMER2, TIMER_CH2);

	Timer2.setPeriod(1000); // 1khz
	Timer2.setCompare(TIMER_CH2,Timer2.getOverflow()/2);

	//Timer 2 Channel 2 timer output is on PA1
	//setup pin PA1 for alt function output
	gpio_set_mode(PA1, GPIO_AF_OUTPUT_PP);
	//gpio_set_mode(PA1, (gpio_pin_mode) (GPIO_MODE_AF | GPIO_OTYPE_PP | GPIO_OSPEED_50MHZ));
	GPIOA->regs->AFR[0] |= GPIO_AFMODE_TIM1_2 << 4;

	//Timer2.attachInterrupt(TIMER_CH2, timer_trig);

	// start the timer
	Timer2.refresh();
	TIMER2->regs.gen->SR = 0; //clear interrupt flags
	Timer2.resume();
}
i'd be sharing a new 'project' note i've made changes in adc.h and adc.c in the libmaple F4 core. i'd log a new issue / pr for that (to be discussed separately)

i think

Code: Select all

TIMER2->regs.gen->SR = 0; //clear interrupt flags
is after all unnecessary. There is no interrupt handlers, i hooked one earlier during debug just to see the timer is working.

leaving for now
Last edited by ag123 on Sun May 10, 2020 9:56 am, edited 1 time in total.
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: No timer output on pin in F4 core

Post by stevestrong »

I managed to get it work.
Important is to set pinMode() when the timer is initialized and paused.
Your code can be more elegantly written this way:

Code: Select all

//-----------------------------------------------------------------------------
void Timer_init()
{
	Timer5.init(); // this will also pause the timer
	
	pinMode(PA1, PWM);

//	Timer5.setPrescaleFactor(84); // 1µsec, optional if you use setPeriod()
	Timer5.setPeriod(1000);
	Timer5.setCompare(TIMER_CH2, Timer5.getOverflow()/2);
	Timer5.refresh();
	Timer5.resume();
}
Btw, why do you want to stick anyway to timer 2?
Last edited by stevestrong on Sun May 10, 2020 10:03 am, edited 2 times in total.
ag123
Posts: 1656
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: No timer output on pin in F4 core

Post by ag123 »

thanks! would tidy up codes as i go along :)
well TIM2 and TIM5 are pretty much the 'same' thing, i just happened to be using TIM1 to drive the ADC sampling and TIM2 is the 1khz test signal.
ag123
Posts: 1656
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: No timer output on pin in F4 core

Post by ag123 »

stevestrong wrote: Sun May 10, 2020 9:04 am The mode GPIO_AF_OUTPUT_PP for the timer is wrong, this will activate the AF mode 0 which is plain GPIO.
https://github.com/stevstrong/Arduino_S ... #L235-L253
just a quick question though, do we want to go about updating

Code: Select all

typedef enum {
    GPIO_AFMODE_SYSTEM   = 0,
    GPIO_AFMODE_TIM1_2   = 1,
    GPIO_AFMODE_TIM3_5   = 2,
...
} gpio_af_mode;
to

Code: Select all

typedef enum {
    GPIO_AFMODE_AF0   = 0,
    GPIO_AFMODE_AF1   = 1,
    GPIO_AFMODE_AF2   = 2,
...
} gpio_af_mode;
?
it seemed a little 'unnecessary' but that the AFx definitions are after all different for each pin.
it could help in a sense that codes that uses them would be less affected by changes going forward rather than if those use GPIO_AFMODE_TIM1_2 etc.
e.g. if that goes into libraries / shared sketches etc, it would create a lot of dependencies if they still remain as GPIO_AFMODE_TIM1_2 etc
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: No timer output on pin in F4 core

Post by stevestrong »

The AF definitions do not depend on the pin.
As you already checked, PA1 can be used as TIM2 as well as TIM5 output, but the AF mode of the same pin differs in these cases.
On the other hand, if you have two pins supporting the same AF, then the same AF mode shall apply for both of them. Hence you cannot say that the Af definitions are different or each pin.
So I would not change the definitions.
ag123
Posts: 1656
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: No timer output on pin in F4 core

Post by ag123 »

thanks steve! i think you are correct about it !
well as i got a little curious i did a review of the stm32f40x datasheet (this table is found immediately after the pinouts and pin description tables pages of the datasheet. incidentally, i found that stm32f103 has a different afio register map design (but for this we'd just discuss F4)

each of those gpio (e.g. PAxx) pins has slightly different AFxx AF mode assignments for the AFR register.
but in each of the AFxx 'columns' are for specific peripherals. e.g. AF1 are for TIM1 / TIM2, AF2 TIM3 / TIM4 / TIM5 .... AF4 I2C, AF5 SPI, AF7 UART ...
the main difference being the specific function of the AFIO pinout e.g.for timers it is the different channels, for SPI the different functions e.g. MOSI / MISO, / SCK, for UART TX / RX / CK / RTS / CTS and the different pheriperials.

apparently the definitions are already matching the column headings
gpio_def.h

Code: Select all

typedef enum {
    GPIO_AFMODE_SYSTEM   = 0,
    GPIO_AFMODE_TIM1_2   = 1,
    GPIO_AFMODE_TIM3_5   = 2,
    GPIO_AFMODE_TIM8_11  = 3,
    GPIO_AFMODE_I2C1_3   = 4,
    GPIO_AFMODE_SPI1_4   = 5,
    GPIO_AFMODE_SPI3_5   = 6,
    GPIO_AFMODE_USART1_3 = 7,
    GPIO_AFMODE_USART4_6 = 8,
    GPIO_AFMODE_CAN1_2   = 9,
    GPIO_AFMODE_TIM12_14 = 9,
    GPIO_AFMODE_OTG_FS   = 10,
    GPIO_AFMODE_ETH      = 11,
    GPIO_AFMODE_FSMC     = 12,
    GPIO_AFMODE_SDIO     = 12,
    GPIO_AFMODE_OTG_HS   = 12,
    GPIO_AFMODE_DCMI     = 13,
    GPIO_AFMODE_14       = 14,
    GPIO_AFMODE_EVENTOUT = 15,
} gpio_af_mode;
hence there is no need to change anything, the existing definitions are appropriate.
Attachments
stm32 f40x afio map
stm32 f40x afio map
afio_map.png (90.46 KiB) Viewed 4842 times
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: No timer output on pin in F4 core

Post by stevestrong »

ag123 wrote: Mon May 11, 2020 7:58 pm apparently the definitions are already matching the column headings
Well, I know, I've spent a lot of time to define the correct values. 8-)
ag123
Posts: 1656
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: No timer output on pin in F4 core

Post by ag123 »

well it is my goof, initially i read that as timer1 channel2 it isn't it is timer 1 or 2, these definitions are good :)
Post Reply

Return to “General discussion”