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: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

No timer output on pin in F4 core

Post by ag123 »

In the libmaple F4 core for stm32f40x and maybe 4xx

i've this code, this is supposed to put a 1khz square waves on PA1. it works on stm32f103 but it seemed it didn't work in F4 core.
i did not get an output on PA1

Code: Select all

	Timer2.pause();
	Timer2.setPrescaleFactor(1); //36mhz
	timer_oc_set_mode(TIMER2,TIMER_CH2,TIMER_OC_MODE_PWM_1,0);
	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);

	//Timer2.attachCompare2Interrupt(timer2handle);

	// start the timer
	Timer2.refresh();
	TIMER2->regs.gen->SR = 0; //clear interrupt flags, not really needed but an update event seem to set some flags
	Timer2.resume();
i'm still debugging the problem and hope to find the 'bug' or 'feature' soon ;)
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: No timer output on pin in F4 core

Post by ag123 »

there is a smaller issue of Timers not clocked at init (i.e. reset)
https://github.com/stevstrong/Arduino_STM32/issues/39
this can be easily solved by uncommenting the 'return' in timerDefaultConfig
https://github.com/stevstrong/Arduino_S ... s.cpp#L125

Code: Select all

static void timerDefaultConfig(const timer_dev *dev)
{
return;
....
    timer_init(dev);
    timer_pause(dev);
or more appropriately simply call timer_init(dev). before using the timers.
Note that this codes are in flux so they may change anytime soon.
this is different from the original issue in 1st post.
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 »

Insert

Code: Select all

Timer2.init():
at the beginning of your code and it will work.

Alternatively, it would maybe make sense to enable

Code: Select all

timer_init(dev);
timer_pause(dev);
at startup,
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: No timer output on pin in F4 core

Post by ag123 »

thanks, things are more devilish than i thought.
i'm getting a flat 0 v on the pin PA1 T2_CH2, i'm actually measuring voltages from PA0 - ADC channel 0.
i've verified that the voltages measured correctly from PA0 (i did that by measuring 3v3), but if i patch PA0 ADC0 - PA1 T2_CH2 i get a flat 0 volt instead.
i actually switch to PA2 (and changing the codes accordingly) but with the same effect observed. i meddled with the gpio ospeeds but the results are same.
the timers are running, i check by interrupting and looking at the timer count, the count is changing.

As each of those pins has multiple afio on the same pin, e.g. PA1 has T2_CH2, T2_CH5, ADC1, RTS2 and the gpio itself on the same pin.
I'm next trying to figure out if 2 of the AFIO has an output enabled on the same pin. that may 'short' the signals from one block e.g. TIM2 to UART2.
But i'd guess shouldn't happen as Serial2 isn't initialised with a begin() statement.

i also found out that i should probably call

Code: Select all

timer_oc_set_mode(TIMER2,TIMER_CH2,TIMER_OC_MODE_PWM_1,TIMER_OC_PE);
the preload enable flag is probably needed. I've since changed to simply calling

Code: Select all

timer_set_mode(TIMER2, TIMER_CH2, TIMER_PWM);
that does the same thing.

i've also adopted the initializations at reset to be simply clock the timers and disable them, setting ARPE
https://github.com/stevstrong/Arduino_S ... 114f14bc95
this mainly to prevent spurious initialization e.g. that both TIM2 and TIM5 are sending output to the same pin.
shouldn't happen, since the timers are not configured and disabled.

it seemed turning on (i.e. clock) blocks that one is not about to use may have unintended consequences, e.g. the afio output from one block e.g. TIM2 short into another block. i've been thinking that if the output isn't enabled it should be high-z, but i'd guess sometimes we'd not be aware that some initialization may have enabled output on the same bus. still debugging the problem, but i'd let leave for now.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: No timer output on pin in F4 core

Post by ag123 »

i did one more test

Code: Select all

pinMode(PA1, OUTPUT);
digitalWrite(PA1, HIGH);
PA1 goes to 3v3. so the pin PA1 is working. the answer probably need to be found in the configs for TIM2 or that like i speculate the adjacent afio going into the same PA1 pin 'shorting out' the output.

disabled initialization in the core

Code: Select all

    //setupADC();
    //setupTimers();
and did my own

Code: Select all

	adc_init(ADC1);
	Timer2.init();
set period to 0.5s, patched a timer interrupt handler on Timer2, make it blink the leds. ok the led blinks.
but strange still no output on PA1 T2_CH2
checking the registers, TIMER2->CR1 ARPE auto preload enable is set ok
TIMER2->CCMR1 OC2M 110 PWM mode 1
TIMER2->CCMR1 OC2PE preload enabled ok

full timer2 setup codes

Code: Select all

void timer_trig() {
	fled = fled?0:1;
	digitalWrite(errorPin, fled);
	TIMER2->regs.gen->SR = 0; //clear interrupt flags
}

void initTesttimer(void) {

	Timer2.init();
	Timer2.pause();
	//Timer2.setPrescaleFactor(1); //36mhz
	//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(500000); // 0.5 s
	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));

	//Timer2.attachInterrupt(TIMER_CH2, timer_trig);

	// start the timer
	Timer2.refresh();
	TIMER2->regs.gen->SR = 0; //clear interrupt flags
	Timer2.resume();
}
TIM5 is not clocked, UART2 is not clocked, so it can't be shorted by TIM5 or UART2. is that correct?
Last edited by ag123 on Sun May 10, 2020 8:41 am, edited 2 times 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 am investigating the issue.

Nevertheless, you made two errors.
First, the GPIOs have to be set on PWM mode, not OUTPUT.
Second, the timers are mapped to the pins according to the table defines in libmaple/timer_map.c
There you can see that PA1 is set to output TIM5 CH2, not TIM2.

However, even in this setup, PA1 will not pulse.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: No timer output on pin in F4 core

Post by ag123 »

thanks!
oh i forget to mention, i'm playing with the F401CC pill board

reading the specs, TIM2 CH2 and TIM5 CH2 shares the same PA1 pin
PA1 is set to do afio when the timer is running

Code: Select all

gpio_set_mode(PA1, GPIO_AF_OUTPUT_PP);
i'm checking registers values in debug, so that should be quite accurate.
while reading the specs i noticed one more thing EVENTOUT on same pin, hmm, seemed like a blind spot.
but EVENTOUT is normally 'disabled' right? :P
EVENTOUT seem to be related to "sev" instruction, doesn't seem to be the problem here
https://electronics.stackexchange.com/q ... t-eventout

strange TIM2 registers 'looked correct' for now, leaving for the time being.
Last edited by ag123 on Sun May 10, 2020 9:04 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 »

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
Last edited by stevestrong on Sun May 10, 2020 9:06 am, edited 1 time in total.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: No timer output on pin in F4 core

Post by ag123 »

oh thanks, let me check that in my copy, i could be mistaken
gpio_set_mode(PA1, GPIO_AF_OUTPUT_PP); ... D_50MHZ));
strange GPIO_AF_OUTPUT_PP = GPIO_MODE_AF | GPIO_OTYPE_PP | GPIO_OSPEED_50MHZ
it actually looks correct.
checking the registers show
MODER1 0b10 alternate function
OT1 0 output push pull
OSPEEDR1 0b10 50 mhz
PUDR1 0 no pull up / pull down

well push / pull output doesn't need pull up or pull down resistors right?

omg i found a register i've been so noob about AFRL / AFRH ! didn't know it exist :lol:

Code: Select all

	//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;
yay :D
girinoscope stm32f401ccu
girinoscope stm32f401ccu
girino1khz.png (57.62 KiB) Viewed 5528 times
Last edited by ag123 on Sun May 10, 2020 9:37 am, edited 2 times 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 »

This setting is not enough.
You have to tell the AF part which AF mode to activate.

Code: Select all

gpio_set_af_mode(PA1, GPIO_AFMODE_TIM3_5); // or GPIO_AFMODE_TIM1_2
And this will only be executed in core if you set pinMode to PWM.
Post Reply

Return to “General discussion”