ICU Measurement 10 times too slow

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderators: RoccoMarco, barthess

THS
Posts: 3
Joined: Thu Jan 06, 2022 7:36 pm

ICU Measurement 10 times too slow

Postby THS » Thu Jan 06, 2022 8:35 pm

Hi,
I am trying to measure a 1kHz PWM Signal on Pin PB5 on a STM32F407 Discovery Board (I checked the 1kHz signal with a scope).
So I copied the example from HAL-STM32F407-DISCOVERY Demo folder and adapted it.
I expect to get a period of 1ms but instead I get a 10ms (see variable "too_slow_period") instead.
Which setting do I have to adjust to get correct measurement ?

Here is my code:

Code: Select all

icucnt_t last_period, too_slow_period;

static void Thread1(void const *arg)
{
  (void)arg;
  while (true)
  {
    too_slow_period = last_period;
    osDelay(1000);
  }
}

/*
 * Thread definition block.
 */
osThreadDef(Thread1, osPriorityAboveNormal, 128, "blinker");

static void icuperiodcb(ICUDriver *icup) {

  last_period = icuGetPeriodX(icup);
}

static ICUConfig icucfg = {
  ICU_INPUT_ACTIVE_HIGH,
  10000,                                    /* 10kHz ICU clock frequency.   */
  NULL,
  icuperiodcb,
  NULL,
  ICU_CHANNEL_2,
  0U,
  0xFFFFFFFFU
};

/* Application entry point. */
int main(void) {
  halInit();
  chSysInit();
  osKernelInitialize();
  osThreadCreate(osThread(Thread1), NULL);
  osKernelStart();


// ---------------------ICU Measuring----------------------
icuStart(&ICUD3, &icucfg);
palSetPadMode(GPIOB, 5, PAL_MODE_ALTERNATE(2));
icuStartCapture(&ICUD3);
icuEnableNotifications(&ICUD3);
// ---------------------ICU Measuring----------------------


 while (true) {

    /* Doing nothing. This thread can be used for any other
       user activity.*/
    chThdSleepMilliseconds(1000);

  }
}

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: ICU Measurement 10 times too slow

Postby Giovanni » Thu Jan 06, 2022 8:56 pm

Hi,

Periods are measured using the timer frequency, in you case it is 10kHz, this means you get 100uS resolution, icuGetPeriod() should return 10.

Giovanni

THS
Posts: 3
Joined: Thu Jan 06, 2022 7:36 pm

Re: ICU Measurement 10 times too slow

Postby THS » Thu Jan 06, 2022 9:44 pm

Hi Giovanni,

thanks for the quick reply.
I changed the timer frequency from 10000 to 1000 and now icuGetPeriod() returns 5 instead of the expected 1

Any idea ?

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: ICU Measurement 10 times too slow

Postby Giovanni » Thu Jan 06, 2022 9:57 pm

Hi,

If you want measure a signal around 1000Hz then you need a timer frequency much higher or you get an extreme error. Try with a 1MHz clock and it should return 1000.

If there is an error then the timer takes a wrong clock frequency for some reason, enable assertions in chconf.h and see if something is caught.

Giovanni

THS
Posts: 3
Joined: Thu Jan 06, 2022 7:36 pm

Re: ICU Measurement 10 times too slow

Postby THS » Thu Jan 06, 2022 10:05 pm

Hi Giovanni,

now it works as expected.
I think I missed that the ticks are based on the clock frequency in the ICUConfig.

Thanks a lot.


Return to “STM32 Support”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 10 guests