Code: Select all
#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION < 0x01090000)
#error "Due to API change, this sketch is compatible with STM32_CORE_VERSION >= 0x01090000"
#endif
#define pin PA8
uint32_t channel;
volatile uint16_t TimerCountOverflow=0;
HardwareTimer *MyTim;
TIM_HandleTypeDef *MyTimHandle;
void Rollover_IT_callback(void)
{
TimerCountOverflow++;
}
void setup()
{
Serial.begin(115200);
TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(pin), PinMap_PWM);
channel = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(pin), PinMap_PWM));
MyTim = new HardwareTimer(Instance);
MyTim->setMode(channel, TIMER_INPUT_CAPTURE_RISING, pin);
uint32_t PrescalerFactor = 1;
MyTim->setPrescaleFactor(PrescalerFactor);
MyTim->setOverflow(0x10000); // Max Period value to have the largest possible time to detect rising edge and avoid timer rollover
MyTim->attachInterrupt(Rollover_IT_callback);
MyTimHandle = MyTim->getHandle(); // HAL handle address
MyTimHandle->Instance->SMCR |= TIM_SMCR_TS_0 | TIM_SMCR_TS_2 // Filtered Timer Input 1 (TI1FP1)
| TIM_SMCR_SMS_0 | TIM_SMCR_SMS_1 | TIM_SMCR_SMS_2; // External Clock Mode 1
MyTim->resume();
}
void loop()
{
static uint32_t lastcount;
uint32_t counter;
uint16_t overflow;
do
{
overflow = TimerCountOverflow;
counter = MyTim->getCount();
}
while ( overflow != TimerCountOverflow ); // repeat if overflow changed
counter |= overflow << 16; // combine
Serial.println((String)" counter-lastcount = " + (counter-lastcount) );
lastcount = counter;
delay(1000);
}
Code: Select all
14:45:40.417 -> counter-lastcount = 10000672
14:45:41.420 -> counter-lastcount = 10000675
14:45:42.423 -> counter-lastcount = 10000675
14:45:43.425 -> counter-lastcount = 10000673
14:45:44.428 -> counter-lastcount = 10000673
14:45:45.431 -> counter-lastcount = 10000674
It is using a 16-bit overflow counter, but that could be changed if necessary.
Give it a try anyway. If it does not work on F103, I have some of those here I can dig out if necessary.