Zephyr device driver model
From Wiki at Neela Nurseries
Excerpt from Zephyr project drivers documentation:
-
Driver Data Structures
The device initialization macros populate some data structures at build time which are split into read-only and runtime-mutable parts. At a high level we have:
struct device { const char *name; const void *config; const void *api; void * const data; };
Excerpt from file ~/ncs/zephyr/include/drivers/sensor.h
:
382 /** 383 * @brief Set an attribute for a sensor 384 * 385 * @param dev Pointer to the sensor device 386 * @param chan The channel the attribute belongs to, if any. Some 387 * attributes may only be set for all channels of a device, depending on 388 * device capabilities. 389 * @param attr The attribute to set 390 * @param val The value to set the attribute to 391 * 392 * @return 0 if successful, negative errno code if failure. 393 */ 394 __syscall int sensor_attr_set(const struct device *dev, 395 enum sensor_channel chan, 396 enum sensor_attribute attr, 397 const struct sensor_value *val); 398 399 static inline int z_impl_sensor_attr_set(const struct device *dev, 400 enum sensor_channel chan, 401 enum sensor_attribute attr, 402 const struct sensor_value *val) 403 { 404 const struct sensor_driver_api *api = 405 (const struct sensor_driver_api *)dev->api; 406 407 if (api->attr_set == NULL) { 408 return -ENOSYS; 409 } 410 411 return api->attr_set(dev, chan, attr, val); 412 } 413
From these two code excerpts the big question is, how is dev->api assigned in early configuring code for an I2C based sensor?
./include/drivers/sensor.h:61: SENSOR_CHAN_ACCEL_XYZ, . . . hmm, this is part of a large enumeration:
53 enum sensor_channel { 54 /** Acceleration on the X axis, in m/s^2. */ 55 SENSOR_CHAN_ACCEL_X, 56 /** Acceleration on the Y axis, in m/s^2. */ 57 SENSOR_CHAN_ACCEL_Y, 58 /** Acceleration on the Z axis, in m/s^2. */ 59 SENSOR_CHAN_ACCEL_Z, 60 /** Acceleration on the X, Y and Z axes. */ 61 SENSOR_CHAN_ACCEL_XYZ, 62 /** Angular velocity around the X axis, in radians/s. */ 63 SENSOR_CHAN_GYRO_X, 64 /** Angular velocity around the Y axis, in radians/s. */ 65 SENSOR_CHAN_GYRO_Y, 66 /** Angular velocity around the Z axis, in radians/s. */ 67 SENSOR_CHAN_GYRO_Z, 68 /** Angular velocity around the X, Y and Z axes. */ 69 SENSOR_CHAN_GYRO_XYZ, . . .