Difference between revisions of "Zephyr drivers"
m (Add section q Mutex Use in BMI323 Driver q) |
m (Double-space list items) |
||
| Line 140: | Line 140: | ||
1. line 384: static int bosch_bmi323_driver_api_attr_set(...) | 1. line 384: static int bosch_bmi323_driver_api_attr_set(...) | ||
| + | |||
2. line 727: static int bosch_bmi323_driver_api_attr_get(...) | 2. line 727: static int bosch_bmi323_driver_api_attr_get(...) | ||
| + | |||
3. line 843: static int bosch_bmi323_driver_api_trigger_set(...) | 3. line 843: static int bosch_bmi323_driver_api_trigger_set(...) | ||
| + | |||
4. line 996: static int bosch_bmi323_driver_api_sample_fetch(const struct device *dev, enum sensor_channel chan) | 4. line 996: static int bosch_bmi323_driver_api_sample_fetch(const struct device *dev, enum sensor_channel chan) | ||
| + | |||
5. line 1047: static int bosch_bmi323_driver_api_channel_get(...) | 5. line 1047: static int bosch_bmi323_driver_api_channel_get(...) | ||
| − | 6. line 1150: static void bosch_bmi323_irq_callback_handler(...) | + | |
| + | 6. line 1150: static void bosch_bmi323_irq_callback_handler(...) | ||
| + | |||
7. line 1241: static int bosch_bmi323_pm_action(...) | 7. line 1241: static int bosch_bmi323_pm_action(...) | ||
Revision as of 04:35, 22 February 2026
Keywords: sensor interrupts : sensor triggers : Zephyr triggers : sensor interrupt support
OVERVIEW
This page a stash point to hold Zephyr driver notes. Zephyr RTOS project includes a range of types of drivers. First type referenced by this page will be Zephyr's sensor drivers.
^ Zephyr Sensor Model
Zephyr 4.3.0 device driver documentation:
Zephyr 4.3.0 sensor model:
"Sensor attribute get" function prototype. This prototype reveals Zephyr's enumerations for sensor yyy . . .
Zephyr's sensor model plays a big role in the design of sensor drivers for this RTOS. Zephyr's [... sensor.h] header defines many things. Two important enums from this header file are:
_ Sensor channel enum _
_ Sensor attribute enum _
Sensor channels generally correspond to readings of a quantity in specific units, like degrees centigrade and acceleration in the x-axis. Sensor attributes generally corrrespond to configuration settings for a sensor, where these settings modify the way in which readings are taken and reported.
For a Zephyr driver to support configuration of a sensor's attributes, it must implement a function of the form:
sensor_attr_set()
int sensor_attr_set(const struct device* dev,
enum sensor_channel chan,
enum sensor_attribute attr,
const struct sensor_value* val)
. . . whose typedef is at https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/drivers/sensor.h#L431.
^ Bosch BMI323 Driver Example
The Bosch BMI323 accelerometer magnetometer combined sensor has a driver in Zephyr 4.3.0. This driver implements its sensor attribute setter function in bmi323.c in two places. The first place is a sensor API struct which the driver declares. This API struct organizes the public API function of the driver. These APIs implement and honor each a function signature that's type defined in Zephyr's sensor.h header file.
The structure is defined starting at about line 384, bmi323.c:384:
static DEVICE_API(sensor, bosch_bmi323_api) = {
.attr_set = bosch_bmi323_driver_api_attr_set, <-- attribute setter
.attr_get = bosch_bmi323_driver_api_attr_get,
.trigger_set = bosch_bmi323_driver_api_trigger_set,
.sample_fetch = bosch_bmi323_driver_api_sample_fetch,
.channel_get = bosch_bmi323_driver_api_channel_get,
};
The attribute setter function is defined at about line 1101, bmi323.c:1101:
static int bosch_bmi323_driver_api_attr_set(const struct device *dev,
enum sensor_channel chan,
enum sensor_attribute attr,
const struct sensor_value *val)
{
struct bosch_bmi323_data *data = (struct bosch_bmi323_data *)dev->data;
int ret;
k_mutex_lock(&data->lock, K_FOREVER);
switch (chan) {
case SENSOR_CHAN_ACCEL_XYZ:
switch (attr) {
case SENSOR_ATTR_SAMPLING_FREQUENCY:
ret = bosch_bmi323_driver_api_set_acc_odr(dev, val);
break;
case SENSOR_ATTR_FULL_SCALE:
ret = bosch_bmi323_driver_api_set_acc_full_scale(dev, val);
break;
case SENSOR_ATTR_FEATURE_MASK:
ret = bosch_bmi323_driver_api_set_acc_feature_mask(dev, val);
break;
default:
ret = -ENODEV;
break;
}
break;
case SENSOR_CHAN_GYRO_XYZ:
switch (attr) {
case SENSOR_ATTR_SAMPLING_FREQUENCY:
ret = bosch_bmi323_driver_api_set_gyro_odr(dev, val);
break;
case SENSOR_ATTR_FULL_SCALE:
ret = bosch_bmi323_driver_api_set_gyro_full_scale(dev, val);
break;
case SENSOR_ATTR_FEATURE_MASK:
ret = bosch_bmi323_driver_api_set_gyro_feature_mask(dev, val);
break;
default:
ret = -ENODEV;
break;
}
break;
default:
ret = -ENODEV;
break;
}
k_mutex_unlock(&data->lock);
return ret;
}
^ Mutex Use in BMI323 Driver
BMI323 driver code locks a mutex in the following routines of bmi323.c:
1. line 384: static int bosch_bmi323_driver_api_attr_set(...)
2. line 727: static int bosch_bmi323_driver_api_attr_get(...)
3. line 843: static int bosch_bmi323_driver_api_trigger_set(...)
4. line 996: static int bosch_bmi323_driver_api_sample_fetch(const struct device *dev, enum sensor_channel chan)
5. line 1047: static int bosch_bmi323_driver_api_channel_get(...)
6. line 1150: static void bosch_bmi323_irq_callback_handler(...)
7. line 1241: static int bosch_bmi323_pm_action(...)
^ References
1. https://github.com/zephyrproject-rtos/zephyr/issues/30133