Frequency Counter function is not inbuilt with any APIs of mbed?

I have an issue that I can not measure frequency more the 400 KHZ??? I have been measuring it on pin PB_10 [Timer2, Channel3], Some one have idea how to implement code for this pin, I have been successfuly done to measure frequency at pin : PA_0 [timer2, channel1]. but could do that on channel3 for timer2 [PB_10].
The configuration for PA_0 is :
// Select PA_0 pin for frequency counter
GPIOA->AFR[0] &= 0xfffffff0;
GPIOA->AFR[0] |= GPIO_AF1_TIM2;
GPIOA->MODER &= ~(GPIO_MODER_MODER0);
GPIOA->MODER |= 0x2;
// Initialize Timer2(32bit) for an external up counter mode
RCC->APB1ENR |= ((uint32_t)0x00000001);
TIM2->CR1 &= (uint16_t)(~(TIM_CR1_DIR | TIM_CR1_CMS | TIM_CR1_CKD)); // count_up + div by 1
TIM2->ARR = 0xFFFFFFFF;
TIM2->PSC = 0x0000;
TIM2->CCMR1 &= (uint16_t)~TIM_CCMR1_IC1F; // input filter
TIM2->CCER = TIM_CCER_CC1P; // positive edge
TIM2->SMCR &= (uint16_t)~(TIM_SMCR_SMS | TIM_SMCR_TS | TIM_SMCR_ECE);
TIM2->SMCR |= (uint16_t)(TIM_TS_TI1FP1 | TIM_SMCR_SMS); // external mode 1
TIM2->CR1 |= TIM_CR1_CEN; //Enable the TIM Counter */

what is to be done for changing tghis configuration for channel3, timer2 for PB_10 pin, any idea???

Hi Sandeep,

Mbed does not have a built-in API for measuring the frequency so you will have to write your own. Some community members have posted their own solutions and here are two examples that might prove useful:

If you still need assistance, please tell us:

  • What target are you using?
  • What version of Mbed are you using?
  • How are you generating the input signal?
  • Can you post your complete program?

Regards,
Ralph, Team Mbed

HI,

Please find the below Information related to program/board

  • What target are you using? → Nucleo -64 (STM32F401RE)
  • What version of Mbed are you using? → 5.8
  • How are you generating the input signal? → Function generator [upto 8-10 MHZ]
  • Can you post your complete program? → I have to do POC for frequency counting at pin PB_10 [Timer 2, channel3], I have found the refrence and made changes for requirement from below reference code of KenjiArai, but could not do as far:
    Might be some configuration issue, assigned to register value of

I have made changes as following:

//GPIOB Pin configuration for pin: PB_10 [Timer2, channel3]
RCC->AHB1ENR |= (RCC_AHB1ENR_GPIOAEN);

GPIOB->AFR[1] &= 0xfffff0ff;

GPIOB->AFR[1] |= GPIO_AF1_TIM2 << 8;



GPIOB->MODER &= ~(GPIO_MODER_MODER10);  // AF

GPIOB->MODER |= GPIO_MODER_MODER10_1;

GPIOB->OTYPER &= ~(GPIO_OTYPER_OT_10);// Output Push-Pull=0

GPIOB->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR10;// Speed full=11

GPIOB->PUPDR &= ~(GPIO_PUPDR_PUPDR10);   // Pull-up=01

GPIOB->PUPDR |= GPIO_PUPDR_PUPDR10_0;

// Initialize Timer2(32bit) for an external up counter mode

RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;

TIM2->CR1 &= (uint16_t)(~(TIM_CR1_DIR | TIM_CR1_CMS | TIM_CR1_CKD));// count_up + div by 1

TIM2->CR1 |= TIM_CR1_URS;

TIM2->ARR = 0xffffffff;

TIM2->PSC = 0x0000;

TIM2->CCER &= (uint16_t)~TIM_CCER_CC3E;      // Disable the CC1

TIM2->CCMR2 &= (uint16_t)~(TIM_CCMR1_IC1F | TIM_CCMR2_CC3S);    // input filter + input select

TIM2->CCMR2 |= (uint16_t)TIM_CCMR2_CC3S_0;

TIM2->CCER &= (uint16_t)~(TIM_CCER_CC3P | TIM_CCER_CC3NE | TIM_CCER_CC3NP); // positive edge

TIM2->SMCR  = (uint16_t)(TIM_SMCR_ECE| TIM_SMCR_ETPS_0 | TIM_SMCR_TS);  // clock/2 !!

TIM2->CR1 |= (uint16_t)TIM_CR1_CEN;          // Enable the TIM Counter

And I am reading TIM2-> CNT register for frequency with GATE time of 2 seconds.

Kindly review register’s configuration once and let me know what changes are required. at my end I could not debug for Timer register.

************************************************************************************************************************************************************* eInfochips Business Disclaimer: This e-mail message and all attachments transmitted with it are intended solely for the use of the addressee and may contain legally privileged and confidential information. If the reader of this message is not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution, copying, or other use of this message or its attachments is strictly prohibited. If you have received this message in error, please notify the sender immediately by replying to this message and please delete it from your computer. Any views expressed in this message are those of the individual sender unless otherwise stated. Company has taken enough precautions to prevent the spread of viruses. However the company accepts no liability for any damage caused by any virus transmitted by this email. *************************************************************************************************************************************************************

Hi RalphF,

I have tried your following links and what I observed that:

https://os.mbed.com/questions/78786/Read-Frequency-from-external-Input/
—> This is only capable to count pulse in MHZ Range; but work on Pin: PB_10 to count pulse.

https://os.mbed.com/users/kenjiArai/notebook/simple-frequency-counter/**–> This is only count the pulse @ PA0 [TIM2, channel1] in MHZ range, but could not change to PB_10.

* I have used datasheet, but according it could not be resolved. And I have made the changes as according the previous reply with taken care of datasheet.*

Review it and reply. I have to count pulse @ PB_10 [TIM2, channel3] , Is it possible on this pin to count the pulse, and can this pin work as input for pulse counting?

Sandeep

************************************************************************************************************************************************************* eInfochips Business Disclaimer: This e-mail message and all attachments transmitted with it are intended solely for the use of the addressee and may contain legally privileged and confidential information. If the reader of this message is not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution, copying, or other use of this message or its attachments is strictly prohibited. If you have received this message in error, please notify the sender immediately by replying to this message and please delete it from your computer. Any views expressed in this message are those of the individual sender unless otherwise stated. Company has taken enough precautions to prevent the spread of viruses. However the company accepts no liability for any damage caused by any virus transmitted by this email. *************************************************************************************************************************************************************

Hi RalphF,

  • Crrected word,

I have tried your following links and what I observed that:

https://os.mbed.com/questions/78786/Read-Frequency-from-external-Input/
—> This is not capable to count pulse in MHZ Range; but work on Pin: PB_10 to count pulse.

https://os.mbed.com/users/kenjiArai/notebook/simple-frequency-counter/**–> This is only count the pulse @ PA0 [TIM2, channel1] in MHZ range, but could not change to PB_10.

* I have used datasheet, but according it could not be resolved. And I have made the changes as according the previous reply with taken care of datasheet.*

Review it and reply. I have to count pulse @ PB_10 [TIM2, channel3] , Is it possible on this pin to count the pulse, and can this pin work as input for pulse counting?

Sandeep

************************************************************************************************************************************************************* eInfochips Business Disclaimer: This e-mail message and all attachments transmitted with it are intended solely for the use of the addressee and may contain legally privileged and confidential information. If the reader of this message is not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution, copying, or other use of this message or its attachments is strictly prohibited. If you have received this message in error, please notify the sender immediately by replying to this message and please delete it from your computer. Any views expressed in this message are those of the individual sender unless otherwise stated. Company has taken enough precautions to prevent the spread of viruses. However the company accepts no liability for any damage caused by any virus transmitted by this email. *************************************************************************************************************************************************************

Hi Sandeep,

Please clarify - you are successfully counting on PB_10? If so - how fast can you count?

We haven’t been able to exercise this code, but it doesn’t appear that are taking advantage of the prescalar feature of the input capture. You can try setting TIM2_CCMR.IC2PSC and IC1PSC bits to 11b such that a capture is made only every 8 events.

It looks like we might not have bit defines for TIM2, but you can use those for TIM1 (from stm32f401xe.h):

#define TIM_CCMR1_IC1PSC_0   (0x1U << TIM_CCMR1_IC1PSC_Pos)
#define TIM_CCMR1_IC1PSC_1   (0x2U << TIM_CCMR1_IC1PSC_Pos)
...
#define TIM_CCMR1_IC2PSC_0   (0x1U << TIM_CCMR1_IC2PSC_Pos)
#define TIM_CCMR1_IC2PSC_1   (0x2U << TIM_CCMR1_IC2PSC_Pos)

Hopefully this will work for you.

-Ralph, Team Mbed

I use the MBed 5.14 and
TIM2_CCMR.IC2PSC=1 ;
TIM2->PSC = 0x0000;

is error.
the following is error message
Error: Identifier “TIM2_CCMR” is undefined in “main.cpp”