Timer number from pin

Post here first, or if you can't find a relevant section!
Post Reply
sigi
Posts: 57
Joined: Mon Sep 28, 2020 3:17 pm

Timer number from pin

Post by sigi »

HI

there is any way to obtain the timer number from the pin name on a STM32F103 generic board?
I mean using PA8 to obtain 1 as an integer (timer 1)...

I want to create a HardwareTimer object starting from the desired pin number, using TIMER = new HardwareTimer(TimNum);
so I need a way to obtain "TimNum" from the pin name PA8 for example. HELP!!!
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Timer number from pin

Post by fpiSTM »

sigi
Posts: 57
Joined: Mon Sep 28, 2020 3:17 pm

Re: Timer number from pin

Post by sigi »

thanks but I'm not using that core...
I use "Arduino_STM32-master" obtained here : https://github.com/rogerclarkmelbourne/Arduino_STM32

the code u send me does not work using that core.
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Timer number from pin

Post by fpiSTM »

So don't know.
sigi
Posts: 57
Joined: Mon Sep 28, 2020 3:17 pm

Re: Timer number from pin

Post by sigi »

thanks from Costa Rica... I will try to move to that core...
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Timer number from pin

Post by ag123 »

libmaple core is limited to stm32f103 series and some stm32f4xx series, for f103: Timer1, Timer2, Timer3 etc is pre-defined in the includes
https://github.com/rogerclarkmelbourne/ ... mer.h#L371

stm core is 'better' in a sense that the support across the series with the use of HAL etc is more complete, hence, it is much easier to 'port' codes across the series as and when you need to, e.g. it is likely possible to use a different board / series by selecting a board and rebuild. that is true if 'common' api is used. but that unfortunately, it is somewhat 'bulkier', optimization e.g. -Os helps to keep binaries small.

libmaple is 'faster' / 'leaner' but is practically restricted to a particular chip
sigi
Posts: 57
Joined: Mon Sep 28, 2020 3:17 pm

Re: Timer number from pin

Post by sigi »

I just use stm32f103 series

So there is no way to obtain the timer number from a particular pin?
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Timer number from pin

Post by ag123 »

as the timers are pre-defined, Timer1, Timer2 ... etc, they use the default AFIO pins for timer outputs
for the hardware details on how those timer works and the relevant registers that control them, do review the ref manual rm0008
https://www.st.com/resource/en/referenc ... ronics.pdf
as well as the spec sheets for the timer pins
https://www.st.com/resource/en/datashee ... f103c8.pdf
other sources of references are the source codes itself.

some of the old documentations are here
http://docs.leaflabs.com/static.leaflab ... dwaretimer
and here
https://github.com/leaflabs/leaflabs-docs
the codes are usually of the form

Code: Select all

Timer1.pause();
Timer1.setPeriod(usecs);
Timer1.attachInterrupt( interrupt_handler );
Timer1.refresh();
Timer1.resume();
where TimerX is the particular timer you intended to use.
libmaple is a 'just enough' core, there could be 'loose ends' e.g. 'missing' functions where you may need to access registers instead.
but that normally, 1st review the docs, examples and source codes, if it turns out the function 'isn't there' then try with registers etc.
sigi
Posts: 57
Joined: Mon Sep 28, 2020 3:17 pm

Re: Timer number from pin

Post by sigi »

final solution...

Code: Select all

#include <HardwareTimer.h>

#define pin PA1
#define PinTimer(pin) (PIN_MAP[pin].timer_device->clk_id-RCC_TIMER1+1)
#define PinChannel(pin) (PIN_MAP[pin].timer_channel)

void setup() {
delay(1900);
Serial.print("Pin ");
Serial.print(pin);
Serial.print(" Timer ");
Serial.print(PinTimer(pin));
Serial.print(" Channel ");
Serial.print(PinChannel(pin));
}

void loop() {}
Post Reply

Return to “General discussion”