Creating and Configuring a Sensor Device

The steps to create and configure OS devices for onboard and off-board sensors are very similar. The BSP creates the OS devices for onboard sensors and the hw/sensor/creator/ package creates the OS devices for off-board sensors.

We discuss how a BSP creates a device for an onboard sensor and then discuss what the hw/sensor/creator package does differently to create an off-board sensor. We also discuss how an application can change the default configuration for a sensor device.

Note: All example excerpts are from the code that creates the LIS2DH12 onboard sensor in the nrf52_thingy BSP.


syscfg.defs: LIS2DH12_ONB: description: 'NRF52 Thingy onboard lis2dh12 sensor' value: 0

#if MYNEWT_VAL(LIS2DH12_ONB) #include <lis2dh12/lis2dh12.h> static struct lis2dh12 lis2dh12; #endif

#if MYNEWT_VAL(LIS2DH12_ONB) static struct sensor_itf i2c_0_itf_lis = { .si_type = SENSOR_ITF_I2C, .si_num = 0, .si_addr = 0x19 <br>
  • A pointer to the sensorname variable from step 3.
  • A pointer to the <sensorname>_init() callback function. Note that the device driver package exports this function.
  • A pointer to the struct sensor_itf variable from step 4.

For example:


static void sensor_dev_create(void) { int rc; (void)rc; #if MYNEWT_VAL(LIS2DH12_ONB) rc = os_dev_create((struct os_dev *) &lis2dh12, "lis2dh12_0", OS_DEV_INIT_PRIMARY, 0, lis2dh12_init, (void *)&i2c_0_itf_lis); assert(rc == 0); #endif } void hal_bsp_init(void) { int rc; ... sensor_dev_create(); }

int config_lis2dh12_sensor(void) { #if MYNEWT_VAL(LIS2DH12_ONB) int rc; struct os_dev *dev; struct lis2dh12_cfg cfg; dev = (struct os_dev *) os_dev_open("lis2dh12_0", OS_TIMEOUT_NEVER, NULL); assert(dev != NULL); memset(&cfg, 0, sizeof(cfg)); cfg.lc_s_mask = SENSOR_TYPE_ACCELEROMETER; cfg.lc_rate = LIS2DH12_DATA_RATE_HN_1344HZ_L_5376HZ; cfg.lc_fs = LIS2DH12_FS_2G; cfg.lc_pull_up_disc = 1; rc = lis2dh12_config((struct lis2dh12 *)dev, &cfg); SYSINIT_PANIC_ASSERT(rc == 0); os_dev_close(dev); #endif return 0; }
  • A conditional package dependency for the hw/drivers/sensors/<sensorname> package when the <SENSORNAME>_ONB setting is enabled.

  • The config_<sensorname>_sensor function with an init stage of 400 to the pkg.init parameter.

For example:


pkg.deps.LIS2DH12_ONB: - hw/drivers/sensors/lis2dh12 pkg.init: config_lis2dh12_sensor: 400

The steps to create an off-board sensor is very similar to the steps for a BSP. The hw/sensor/creator/ package also declares the variables and implements the config_<sensorname>_sensor() function described for a BSP. The package does the following differently.

Note: All example excerpts are from the code that creates the BNO055 off-board sensor in hw/sensor/creator package.


# Package: hw/sensor/creator syscfg.defs: ... BNO055_OFB: description: 'BNO055 is present' value : 0 ...

For example:


void sensor_dev_create(void) { int rc; ... #if MYNEWT_VAL(BNO055_OFB) rc = os_dev_create((struct os_dev *) &bno055, "bno055_0", OS_DEV_INIT_PRIMARY, 0, bno055_init, (void *)&i2c_0_itf_bno); assert(rc == 0); rc = config_bno055_sensor(); assert(rc == 0); #endif .... }

pkg.deps.BNO055_OFB: - hw/drivers/sensors/bno055

Reconfiguring A Sensor Device by an Application

The BSP and sensor creator package use a default configuration and enable all supported sensors on a sensor device by default. If the default configuration does not meet your application requirements, you may change the default configuration for a sensor device. As in the config_<sensorname>_sensor function, an application must open the OS device for the sensor, set up the values for the <sensorname>_cfg structure, call the <sensorname>_config() device driver function to change the configuration in the device, and close the OS device.

We recommend that you copy the config_<sensorname>_sensor() function from the BSP or the sensor creator package in to your application code and change the desired settings. Note that you must keep all the fields in the <sensorname>_cfg structure initialized with the default values for the settings that you do not want to change.

See the Changing the Default Configuration for a Sensor Tutorial for more details on how to change the default sensor configuration from an application.