Page 1 of 2

Time measurement(s).

Posted: Fri Oct 28, 2016 8:02 pm
by kb1gtt
Is there a better way to do this? I'm looking to measure time of a pulse that will be as low as 2mS and up to 150mS, more accuracy is better, so the faster I can capture the elapsed time the better off I am. In this case it's a dosing pump, and the pulses = amount of fluid dosed. So more accurate means more accurate dosages. I was looking at the Time Measurement Driver below, but I think it's not the best approach for this application.
http://chibios.sourceforge.net/html/group___t_m.html

For that function I believe I would basically need to setup a task, which could trigger every 0.01mS, which would then do the pseudo code posted below.

Now = pin state
If last = 0 and now = 1, then
stop timer,
save time,
restart timer,
Set flag that new time has been captured for processing.
last = now

I don't like this because the min increment is 0.01mS. I know that the timers are operating with a 8MHz clock, if I could connect the pin to the timer IRQ, then I could get a minimum increment of 0.000125mS. In my case a time of 2.03mS is 117.3 RPM, while 2.04mS is 116.7. That tolerance is more than I would prefer, it would cause some problems else where in the system. If I could 0.001mS I would probably be good, but 0.01mS is not enough.I tried pushing the Chibios tick time to 0.001mS, but it seemed to become unstable. I suspect there was to much overhead and not enough time to allow tasks to operate. I didn't dig into it very much, just backed up up to 0.01mS and it all seemed to work as expected.

Is there an example of how to configure a timer connecting a MCU pin to an TIM IRQ? I'm using an STM32F429. I'm hoping I can connect TIM3_CH1 to PB4 and TIM4_CH2 to PB7. However I don't know how to set the pins to start and stop the timer. I know that functions like pwmStart exist for when you are using these timers as a PWM, but I'm not sure if functions exist for other timer features. I don't know how to configure it for input capture vs PWM output. I know for PWM output you use these functions.
pwmStart to initialize the PWM driver with clock scales and such determined by PWMConfig
pwmEnableChannel to do the actual PWM.

I'm not finding initialization functions for the input capture modes of operation. Do they exist? Am I simply not finding them?

In board.h I have set the below.

Code: Select all

#define VAL_GPIOB_MODER             (PIN_MODE_ALTERNATE(GPIOB_LCD_R3) |     \
                                      PIN_MODE_ALTERNATE(GPIOB_PIN4) |           \
                                     PIN_MODE_ALTERNATE(GPIOB_PIN7) |           \
                                     PIN_MODE_ALTERNATE(GPIOB_OTG_HS_DP))
#define VAL_GPIOB_OTYPER            (PIN_OTYPE_PUSHPULL(GPIOB_LCD_R3) |     \
                                     PIN_PUPDR_FLOATING(GPIOB_PIN4) |       \
                                     PIN_PUPDR_FLOATING(GPIOB_PIN7) |       \
                                     PIN_OTYPE_PUSHPULL(GPIOB_OTG_HS_DP))
define VAL_GPIOB_OSPEEDR           (PIN_OSPEED_100M(GPIOB_LCD_R3) |        \
                                     PIN_OSPEED_100M(GPIOB_PIN4) |            \
                                     PIN_OSPEED_100M(GPIOB_PIN7) |            \
                                    PIN_OSPEED_100M(GPIOB_OTG_HS_DP))
#define VAL_GPIOB_PUPDR             (PIN_PUPDR_FLOATING(GPIOB_LCD_R3) |     \
                                     PIN_PUPDR_PULLUP(GPIOB_PIN4) |         \
                                     PIN_PUPDR_PULLUP(GPIOB_PIN7) |         \
                                     PIN_PUPDR_FLOATING(GPIOB_OTG_HS_DP))
#define VAL_GPIOB_ODR               (PIN_ODR_HIGH(GPIOB_LCD_R3) |           \
                                     PIN_ODR_HIGH(GPIOB_PIN4) |             \
                                     PIN_ODR_HIGH(GPIOB_PIN7) |             \
                                     PIN_ODR_HIGH(GPIOB_OTG_HS_DP))
#define VAL_GPIOB_AFRL              (PIN_AFIO_AF(GPIOB_LCD_R3, 14) |        \
                                     PIN_AFIO_AF(GPIOB_PIN4, 2) |           \
                                     PIN_AFIO_AF(GPIOB_PIN7, 2))

Re: Time measurement(s).

Posted: Fri Oct 28, 2016 8:11 pm
by Giovanni
Hi,

You need the ICU driver, look at the PWM-ICU demos, a PWM generates a signal and an ICU measures it. Very accurate if the timer resolution is used entirely.

Giovanni

Re: Time measurement(s).

Posted: Fri Oct 28, 2016 9:54 pm
by kb1gtt
I have found several examples, they seem to all be using more inputs to ICUConfig than I expect. In the below, I see the example uses 7 inputs, but the header seems to indicate it need 5. Should I be concerned with "ICU_Channel_1" and the following 0? Could that be a non-important part of the example code, perhaps some extra stuff is defined but not used.
Untitled.png

Example found here.
http://www.playembedded.org/blog/en/201 ... biOSRT_300

Re: Time measurement(s).

Posted: Sat Oct 29, 2016 7:43 am
by Giovanni
You are looking at ICUConfig into a template file, look at the STM32 version, it contains more fields.

Giovanni

Re: Time measurement(s).

Posted: Sun Nov 06, 2016 1:23 pm
by kb1gtt
I still don't fully understand the 2 extra fields. However I tried the defaults in the online examples and it appears to work. I believe these fields are doing some DMA stuff that in my application is not required, as well I believe these 2 default fields are basically turning off the DMA feature, which I'm OK with. I'm now faced with something different.

I'm trying to get information from the timer configured on ICUD3 and ICUD4. Is there a way to get timer3 vs timer4 via one callback function? Can IcuPeriodCallBack determine if it has captured period3 instead of period4? Or do I need to create the below, which is basically a copy and paste of all the call backs with slightly different names.
icucfg3 --> IcuWidthCallBack3, IcuPeriodCallBack3, IcuOverFlowCallBack3
icucfg4 --> IcuWidthCallBack4, IcuPeriodCallBack4, IcuOverFlowCallBack4

This is a trimmed down snippet of the code I have now.

Code: Select all

In main.c
/*===============================================================*/
/* ICU related code.                                                         */
/*===============================================================*/
static void IcuWidthCallBack(ICUDriver *icup) {
  width = icuGetWidthX(icup); // capture to global var
}
static void IcuPeriodCallBack(ICUDriver *icup) {
  period = icuGetPeriodX(icup); // capture to global var
}
static void IcuOverFlowCallBack(ICUDriver *icup) {
  width = 0xFFFF; // If we are to slow, then set to max measured time
  period = 0xFFFF;// If we are to slow, then set to max measured time
}

static ICUConfig icucfg = {
  ICU_INPUT_ACTIVE_HIGH,
  4000000,            // 4 MHz ICU clock frequency.
  IcuWidthCallBack,
  IcuPeriodCallBack,
  IcuOverFlowCallBack,
  ICU_CHANNEL_1, // ch1 or 2,
  0  // appears to be DIER register for DMA operations
};

blah

int main(void) {

blah

  // Start timers to capture input pulses
  icuStart(&ICUD3, &icucfg);
  icuStart(&ICUD4, &icucfg);

  icuStartCapture(&ICUD3); // start timing measurements
  icuEnableNotifications(&ICUD3);
  icuStartCapture(&ICUD4); // start timing measurements
  icuEnableNotifications(&ICUD4);

blah

  while(1) {
    chThdSleepMilliseconds(100);    // Pause such that other tasks get to run.
    chprintf(chp, "main %d period %d\n\r", i, period);// Send some notes out the serial port viewed on terminal1

blah

  }
}

Re: Time measurement(s).

Posted: Sun Nov 06, 2016 1:33 pm
by Giovanni
Hi,

The first parameter of the callback (icup) is the pointer to the driver instance that triggered it, &ICUD3 or &ICUD4 in your case. You may compare the pointer with the driver address.

Giovanni

Re: Time measurement(s).

Posted: Sun Nov 06, 2016 2:54 pm
by kb1gtt
Sorry I'm a bit more novice than expert. your comment doesn't make full sense to me. I believe you are saying I can do something in IcuPeriodCallBack like this

if (icup = ICUD3) then {period3 = icuGetPeriodX(icup); }

However I do not know how to do the compare. I'm having trouble figuring out what is in the icup structure. I think that icup is pretty much the ICUConfig structure, but I do not see how to determine if it's ICUD3 or ICUD4.

Can you provide a quick reference or example? I'm not seeing the forest from the trees.

Re: Time measurement(s).

Posted: Sun Nov 06, 2016 4:21 pm
by Giovanni
"icup" points to the ICUDriver structure so your comparison goes this way:

if (icup == &ICUD3) {
/* do ICUD3 stuff */
}
else {
/* dp ICUD4 stuff */
}

If you need to access the configuration structure then use icud->config

Giovanni

Re: Time measurement(s).

Posted: Mon Nov 07, 2016 6:58 pm
by kb1gtt
Ah I was being a bit thick skulked. I now see that (&icup == &ICUD3) and (icup == &ICUD3) are different things. I incorrectly read the parameter. Thanks for the sample code.

Is there a more elegant way to deal with 2 channels? I'm using TIM3 CH1 and TIM4 CH2. It works if I have icucfgCh1 and icucfgCh2 each with a changed ICU_CHANNEL_# declaration. Is there a better way to switch channels, or is this the proper way of doing it? Snippet of icucfgCh2 below.

Code: Select all

static icucfgCh2 icucfg = {
  ICU_INPUT_ACTIVE_HIGH,
  4000000,            // 4 MHz ICU clock frequency.
  IcuWidthCallBack,
  IcuPeriodCallBack,
  IcuOverFlowCallBack,
  ICU_CHANNEL_2,
  0  // appears to be DIER register for DMA operations
};

Re: Time measurement(s).

Posted: Mon Nov 07, 2016 7:44 pm
by Giovanni
Hi,

Channels are selected in the configuration structures only, this is the correct way to do this.

Giovanni