How use ICU Driver?

ChibiOS public support forum for topics related to the STMicroelectronics SPC56x family of automotive micro-controllers.
GianlucaLaManna
Posts: 25
Joined: Fri Mar 13, 2015 12:12 pm
Location: Palermo, Italy

How use ICU Driver?

Postby GianlucaLaManna » Tue Jul 07, 2015 12:37 pm

Hello,
I have to simulate the behavior of this function Arduino on Chibios for the management of my sensor.

Code: Select all

duration = pulseIn(ECHO_PIN,HIGH);


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

This is the Arduino code:

Code: Select all

/*
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:
  Serial.begin(9600);
 
  pinMode(TRIG_PIN,OUTPUT);
  pinMode(ECHO_PIN,INPUT);
}
 
void loop()
{
   long duration, distanceCm, distanceIn;
 
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  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(distanceIn);
    Serial.print("in, ");
    Serial.print(distanceCm);
    Serial.print("cm");
    Serial.println();
  }
  delay(1000);
}


This is the code that I wrote, but something is wrong:

Code: Select all

/*
 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

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 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
 automatically.*/
#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) {

  (void)arg;
  chRegSetThreadName("detection");

  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
          osalThreadSleepMicroseconds(2);
          palSetPad(PORT_A, TRIG_PIN); //high
          osalThreadSleepMicroseconds(10);
          palClearPad(PORT_A, TRIG_PIN); // low
          palTogglePad(PORT_A,Led_D12);

        icuStart(&ICUD4, &icu_config_icucfg);
        icuEnable(&ICUD4);

          palSetPad(PORT_A, ECHO_PIN); //high

        icuwidthcb(&ICUD4);
        icuperiodcb(&ICUD4);

          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);

         icuDisable(&ICUD4);
         icuStop(&ICUD4);
          osalThreadSleepMilliseconds(2000);


           //chnWriteTimeout(&SD1, (uint8_t *)"%u\r\n", 14, TIME_INFINITE);

          //chprintf((BaseSequentialStream *)&SD1, "%u", duration);
          osalThreadSleepMilliseconds(500);
  }
  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.*/
  componentsInit();

  /*
   * Activates the serial driver 1 using the driver default configuration.
   */
  sdStart(&SD1, NULL);

  /*
   * Creates the detection thread.
   */
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
    chThdSleepMilliseconds(1000);
}



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
Gianluca

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

Re: How use ICU Driver?

Postby Giovanni » Tue Jul 07, 2015 3:13 pm

You are calling the "callbacks". Please see the demos in SPC5Studio and the HAL documentation.

Giovanni


Return to “SPC56x Support”

Who is online

Users browsing this forum: No registered users and 2 guests