AnsweredAssumed Answered

How use ICU driver?

Question asked by la_manna.gianluca on Jul 7, 2015
Latest reply on Jul 17, 2015 by Erwan Y
I have to simulate the behavior of this function Arduino on Chibios for the management of my sensor. 

duration = pulseIn(ECHO_PIN,HIGH);

I use ICU driver to detect the rising and falling edge.

This is the Arduino code:
Tested with HY-SRF05, HC-SR04
Assuming a room temp of 20 degrees centigrade
The circuit:
    * VVC connection of the sensor attached to +5V
    * GND connection of the sensor attached to ground
    * TRIG connection of the sensor attached to digital pin 12
        * ECHO connection of the sensor attached to digital pin 13
const int TRIG_PIN = 12;
const int ECHO_PIN = 13;
void setup() {
  // initialize serial communication:
void loop()
   long duration, distanceCm, distanceIn;
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(TRIG_PIN, LOW);
  digitalWrite(TRIG_PIN, HIGH);
  digitalWrite(TRIG_PIN, LOW);
  duration = pulseIn(ECHO_PIN,HIGH);
  // convert the time into a distance
  distanceCm = duration / 29.1 / 2 ;
  distanceIn = duration / 74 / 2;
  if (distanceCm <= 0){
    Serial.println("Out of range");
  else {
    Serial.print("in, ");

This is the code that I wrote, but something is wrong:
ChibiOS/RT - Copyright (C) 2006-2014 Giovanni Di Sirio
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.
/* Inclusion of the main header files of all the imported components in the
order specified in the application wizard. The file is generated
#include "components.h"
#include "icu_lld_cfg.h"
icucnt_t last_width, last_period;
void icuwidthcb(ICUDriver *icup) {
  palSetPad(PORT_A, Led_D12);
  last_width = icuGetWidth(icup);
void icuperiodcb(ICUDriver *icup) {
  palClearPad(PORT_A, Led_D12);
  last_period = icuGetPeriod(icup);
* LEDs blinker thread, times are in milliseconds.
static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *arg) {
  while (TRUE) {
     uint32_t duration, distanceCm;
       SIU.PCR[3].R =  0b0100010100000100;           /* PA3 alternate function.  */
       SIU.PCR[58].R = 0b0100010100000100;           /* PD10 alternate function.  */
          palClearPad(PORT_A, TRIG_PIN); // low
          palSetPad(PORT_A, TRIG_PIN); //high
          palClearPad(PORT_A, TRIG_PIN); // low
        icuStart(&ICUD4, &icu_config_icucfg);
          palSetPad(PORT_A, ECHO_PIN); //high
          duration = last_width;
         chprintf((BaseSequentialStream *)&SD1,"Input Capture 3 Value is %u, %u\r\n", last_width, last_period);
         // convert the time into a distance
         distanceCm = duration / 29.1 / 2 ;
         chprintf((BaseSequentialStream *)&SD1,"Distance cm: %u\r\n", distanceCm);
           //chnWriteTimeout(&SD1, (uint8_t *)"%u\r\n", 14, TIME_INFINITE);
          //chprintf((BaseSequentialStream *)&SD1, "%u", duration);
  return 0;
* Application entry point.
int main(void) {
  /* Initialization of all the imported components in the order specified in
   the application wizard. The function is generated automatically.*/
   * Activates the serial driver 1 using the driver default configuration.
  sdStart(&SD1, NULL);
   * Creates the detection thread.
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);

I am getting output as 1, 1 only. What have I missed?
Are the callbacks?

Should I read the time that the signal Echo go back. How can I do? Is the right way?

Best regards