Page 1 of 1

Timer number from pin

Posted: Thu Sep 28, 2023 9:58 pm
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!!!

Re: Timer number from pin

Posted: Thu Sep 28, 2023 10:01 pm
by fpiSTM

Re: Timer number from pin

Posted: Fri Sep 29, 2023 5:23 am
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.

Re: Timer number from pin

Posted: Fri Sep 29, 2023 5:37 am
by fpiSTM
So don't know.

Re: Timer number from pin

Posted: Fri Sep 29, 2023 5:42 am
by sigi
thanks from Costa Rica... I will try to move to that core...

Re: Timer number from pin

Posted: Fri Sep 29, 2023 6:16 am
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

Re: Timer number from pin

Posted: Sat Sep 30, 2023 4:35 am
by sigi
I just use stm32f103 series

So there is no way to obtain the timer number from a particular pin?

Re: Timer number from pin

Posted: Sat Sep 30, 2023 6:01 am
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.

Re: Timer number from pin

Posted: Sun Oct 08, 2023 11:36 pm
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() {}