@leonardo
i tried modifying the codes you've posted as follows
Code: Select all
int32_t channel_1_start, channel_1_stop, channel_1;
void handler_channel_1(void);
void handler_channel_1(void) {
if (0b1 & GPIOA->IDR) {
channel_1_start = TIM2->CCR1;
TIM2->CCER |= TIM_CCER_CC1P;
}
else {
channel_1 = TIM2->CCR1 - channel_1_start;
if (channel_1 < 0)channel_1 += 0xFFFF;
TIM2->CCER &= ~TIM_CCER_CC1P;
}
}
HardwareTimer *pTimer2 = new HardwareTimer(TIM2);
void setup() {
Serial.begin(57600);
pinMode(PA0, INPUT);
delay(250);
pTimer2->attachInterrupt(1, handler_channel_1);
//Timer2.attachCompare1Interrupt(handler_channel_1);
TIM2->CR1 = TIM_CR1_CEN;
TIM2->CR2 = 0;
TIM2->SMCR = 0;
TIM2->DIER = TIM_DIER_CC1IE;
TIM2->EGR = 0;
TIM2->CCMR1 = TIM_CCMR1_IC1PSC;
TIM2->CCMR2 = 0;
TIM2->CCER = TIM_CCER_CC1E;
TIM2->PSC = 71;
TIM2->ARR = 0xFFFF;
TIM2->DCR = 0;
}
void loop() {
delay(1000);
Serial.println(channel_1);
}
the codes actually compiles but that i'm not sure if at all what it does.
the main additions are i defined
Code: Select all
HardwareTimer *pTimer2 = new HardwareTimer(TIM2);
^ note it is a pointer, not an object
then i called attach interrupt this way
Code: Select all
pTimer2->attachInterrupt(1, handler_channel_1);
that is based on hints described in the api
https://github.com/stm32duino/wiki/wiki ... er-library
but really, the codes that you have posted made many direct register access, e.g. configures the timers
i've not deciphered them and do not understand what they are intending to do.
to really understand your codes it would be necessary to review the reference manual
in particular chpt 17 general purpose timers, review the functionalities and features of the timers how they work.
and check that against the registers referenced to figure out what it does.
https://www.st.com/resource/en/referenc ... ronics.pdf
what would be more appropriate is to review and try the examples that has been created by the authors e.g. fpistm et.al.
e.g. some short ones here
https://github.com/stm32duino/wiki/wiki ... ry#3-usage
then many more here
https://github.com/stm32duino/wiki/wiki ... 4-examples
https://github.com/stm32duino/STM32Examples
https://github.com/stm32duino/STM32Exam ... dwareTimer
i tried this code it blinks my led. I created this reading the api docs and examples.
Code: Select all
#include <Arduino.h>
#include <HardwareTimer.h>
HardwareTimer *pTimer2 = new HardwareTimer(TIM2);
void blink() {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
void setup() {
uint8_t channel = 1;
pTimer2->pause(); // pause the timer
pTimer2->setMode(channel, TIMER_OUTPUT_COMPARE); //output compare mode, got this from reading the api notes
pTimer2->setOverflow(500000, MICROSEC_FORMAT); // blink my led every 0.5 sec
pTimer2->attachInterrupt(blink); // attach the blink handler
pTimer2->refresh(); //reset the counters
pTimer2->resume(); //start the timer
}
void loop() {
delay(1000);
}
note that loop() here does nothing. that delay(1000) statement can be deleted and the led still blinks
note that i've been messing with libmaple core, so official stm core is just as 'new' to me as anyone.
it takes time to learn all these and keep the manual handy and review the manual especially about the hardware timers.
that will help you understand the actual hardware features offered by the timers and review the examples and api and try them out.
the api is to a large extent somehow related to the (timer) hardware features, hence reading both the manual and the api and trying
the examples would actually help. some of the api features i'd guess are 'in development' so it may after all 'grow' over time to encompass
more features available on stm32.
the board i used is stm32f401ccu black pill, rather common to find on aliexpress, ebay etc these days
https://stm32-base.org/boards/STM32F401 ... -Pill-V1.2
stm32's timers are pretty advanced (lots of complicated, small and large features). a main use i commonly used them for
is just like my code above, i.e. call back my isr handler, function. then quite a lot of things can be done from within the isr call.
just that doing it this way has very high overheads. normally, it may not even go above 1 mhz for that isr calls with all the codes
done in the isr handler. other more advanced uses of timers is that they can initiate dma, linked with adc etc.
and they do pwm. it takes quite a few settings to setup, but once done the pulse train is generated by hardware, the cpu can process other stuff.