Zephyr Example 2: ToF Sensor

B-L475E-IOT01A Discovery kit for IoT from ST is equipped with a ToF sensor (VL53L0X), connected to the second I2C bus. You can find a sample code under the folder:

(Zephyr_Root)/samples/sensor/vl53l0x

where prj.conf is set to

    1 CONFIG_STDOUT_CONSOLE=y
    2 CONFIG_I2C=y
    3 CONFIG_GPIO=y
    4 CONFIG_SENSOR=y
    5 CONFIG_VL53L0X=y
    6 CONFIG_VL53L0X_PROXIMITY_THRESHOLD=100

Comparing with the default board setting defined in the file below, you will notice that all the lines except the line number 3 are new.

(Zephyr_Root)/board/arm/disco_l475_iot1/disco_l475_iot1_defconfig

Alternatively if you already have your project generated with cmake you can add the sensor feature with menuconfig. Once you created a build setting, run menuconfig (ninja menuconfig)

And select following items:

  • Device Drivers -> I2C Drivers
  • Device Drivers -> Sensor Drivers -> VL53L0X ToF sensor
  • C Library -> Send stdout to console

Then save the configuration and exit. This will replace the original project configuration stored in the file build/zephyr/.config. Corresponding changes include the settings shown at the top. But other settings will be added such as

    1 CONFIG_I2C_1 = y
    2 CONFIG_I2C_2 = y
    3 ...
    4 CONFIG_I2C_STM32 = y
    5 CONFIG_I2C_STM32_V2 = y
    6 ...
    7 CONFIG_USE_STM32_LL_I2C = y
    8 ...
    9 CONFIG_HAS_STLIB = y

Such settings are inserted automatically by the dependencies specified by the Kconfig files in the Zephyr SDK tree such as

  • (Zephyr_Root)/boards/arm/disco_l475_iot1/Kconfig.defconfig
  • (Zephyr_Root)/soc/arm/st_stm32/common/Kconfig.defconfig.series
  • (Zephyr_Root)/soc/arm/st_stm32/stm32l4/Kconfig.defconfig.series

Thus when you create your prj.conf, you do not have to specify all the CONFIG settings as they are appear in the .config file. Check corresponding Kconfig files for the CONFIG parameters.

  • (Zephyr_Root)/drivers/sensor/Kconfig
  • (Zephyr_Root)/drivers/sensor/vl53l0x/Kconfig

Now following main.c should be compiled.

    1 #include <zephyr.h>
    2 #include <device.h>
    3 #include <sensor.h>
    4 #include <gpio.h>
    5 #include <stdio.h>
    6 #include <misc/printk.h>
    7 
    8 void main(void)
    9 {
   10     struct device *dev;
   11     struct sensor_value val;
   12     int ret;
   13 
   14     dev = device_get_binding(DT_VL53L0X_NAME);
   15 
   16     while(1)
   17     {
   18         ret = sensor_sample_fetch(dev);
   19 
   20         if(ret)
   21         {
   22             printk("sensor_sample_fetch failed: %d\n", ret);
   23             return;
   24         }
   25 
   26         ret = sensor_channel_get(dev, SENSOR_CHAN_PROX, &val);
   27         printk("prox is %d\n", val.val1);
   28 
   29         ret = sensor_channel_get(dev, SENSOR_CHAN_DISTANCE, &val);
   30         printf("distance is %.3fm\n", sensor_value_to_double(&val));
   31 
   32         k_sleep(1000);
   33     }
   34 }

This is the same as the sample code provided by Zephyr SDK. Note that at line 30, printf is used instead of printk. This is because printk does not provide floating point format conversion.

Leave a comment