Using Timer 1 as a counter with external pin

I am using a STM32f103 (Blue Pill) as a frequency counter but as my frequency get higher it looses accuracy. At the moment I am using pulseIn but it is too slow. My SMT32f103 is running at 72MHz.

The frequencies I wish to count start at 3MHz up to 30MHz. I am using hardware prescaling using an additional IC so after the frequency is hardware scaled it becomes 375Hz to 3700Hz presented to the STM32f103. According to my Scope My hardware scaling is working fine.

Section of my code

const int pulsePin = PC14; // Input signal connected to PC14 of Arduino
float pulseHigh; // Integer variable to capture High time of the incoming pulse
float pulseLow; // Integer variable to capture Low time of the incoming pulse


void loop(){
    
    //read input pin in Hz
    pulseHigh = pulseIn(pulsePin, HIGH);
    pulseLow = pulseIn(pulsePin, LOW);
    pulseTotal = pulseHigh + pulseLow; // Time period of the pulse in microseconds
    frequency = 1000000 / pulseTotal; // Frequency in Hertz (Hz)
    
    }

I have spent the last 3 days reading about TIMx timers and timers as counters. I have also see written using an External timer clock source so I am wondering if this is what I need. Unfortunately I can not see any examples.

Looking at the datasheet for the STM32f10x It states that it can use an external clock for Timer1 but I am unsure if this is the best way to do it.

The pinout shows;

T1ETR which I think is Timer 1 External Trigger
T1KBKIN ??

http://wiki.stm32duino.com/images/a/ae/Bluepillpinout.gif

I feel I may need a timer with external clock setup as a counter with interrupts but after that I am lost.

Thank you for any help that you can give me.

You are sailing into mostly uncharted waters. The Blue Pill is mostly in beta level support. Since you need to sidestep the Arduino IDE to accomplish your task, it's really a big DIY from scratch engineering project. Do you have what it takes for that? Have you asked about it over on the STM forum?

Thanks for your reply aarg.

I would be happy to use an Arduino with a small footprint like a mini Pro. I did try that and even though I am using hardware prescaling before it gets to the microcontroller, it was too slow/ my code was too slow. I could of course be very wrong. I used a hardware prescaling IC to keep the value of the frequency say below 2MHz before it went into the microcontroller but my serial debug showed readings correct, above and below of what was being sent on the pin(in pulseIn mode). My 'scope is showing that the scaling into the microcontroller is correct and steady but it is all over the place when viewed in the serial debug. That is why I was trying a STM32f103 as it has a much higher working frequency and a hardware timer/counter.

This is new to me as I have not done anything like this in a few years. I just read quite a lot about it for the last 2 weeks. If there is anything that you could suggest that should work using a small foot print standard Arduino board I would be very grateful. I am just unsure why it is not working correctly if my scope is showing a reasonable value.

Yesterday evening I read that Arduino IDE does not support using an External clock for a timer. So that is out of the question anyway.

Meanwhile I have installed STMCube but I do not really want to start from scratch on new software if I do not have too. I would rather stick to Arduino if I could but if that is the only way then I guess I will try it.

Thanks again, Paul