|
|
| (37 intermediate revisions by the same user not shown) |
| Line 1: |
Line 1: |
| | + | <i><b>Keywords:</b> sensor interrupts : sensor triggers : Zephyr triggers : sensor interrupt support</i> |
| | | | |
| − | == [[#top|^]] OVERVIEW ==
| + | OVERVIEW |
| | | | |
| − | 2022 Q4 notes on Zephyr RTOS drivers, driver frameworks and driver design. Current projects include work to extend KX132-1211 Zephyr out-of-tree driver, plus work to craft an IIS2DH driver which supports in Zephyr RTOS context both I2C and SPI bus connections for this sensor.
| + | 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. |
| | | | |
| − | <!-- comentario -->
| + | == [[#top|^]] Zephyr Sensor Model == |
| | | | |
| − | == [[#top|^]] Example Zephyr drivers ==
| + | Zephyr 4.3.0 device driver documentation: |
| | | | |
| − | Example apps here offer varying amounts of insight into Zephyr RTOS' framework(s) for device driver development. Of particular interest 2022 Q4 are the ways in which developers use Zephyr device tree macros to obtain pointers to devices, in their mostly C based code, at compile time. Though not fully explained in Zephyr's extensive documentation it appears that device pointers which can be correctly passed to and used with API function [https://docs.zephyrproject.org/latest/kernel/drivers/index.html#c.device_is_ready device_is_ready()] and macro [https://docs.zephyrproject.org/latest/kernel/drivers/index.html#c.DEVICE_DT_GET DEVICE_DT_GET()] involve pairings of macros, which must be expanded prior to these calls in order to compile correctly.
| + | * https://docs.zephyrproject.org/4.3.0/doxygen/html/group__io__interfaces.html |
| | | | |
| − | * https://github.com/circuitdojo/pcf85063a
| + | Zephyr 4.3.0 sensor model: |
| | | | |
| − | * https://docs.zephyrproject.org/latest/services/smf/index.html | + | * https://docs.zephyrproject.org/4.3.0/doxygen/html/group__sensor__interface.html |
| | | | |
| − | * https://github.com/zephyrproject-rtos/zephyr/tree/main/samples/drivers/spi_bitbang
| + | "Sensor attribute get" function prototype. This prototype reveals Zephyr's enumerations for sensor yyy . . . |
| | | | |
| − | * https://github.com/zephyrproject-rtos/zephyr/tree/main/samples/drivers/uart/stm32/single_wire | + | * https://docs.zephyrproject.org/4.3.0/doxygen/html/group__sensor__interface.html#gaedfdfc71dce702dc1fb1c6e60ff0f73a |
| − | ted@localhost1:~/projects-sandbox/workspace-out-of-tree/zephyr$ grep -nr init_res ./* | grep -v reset | grep init_res
| |
| − | ./include/zephyr/device.h:416: unsigned int init_res : 8;
| |
| − | ./include/zephyr/net/dns_resolve.h:459:void dns_init_resolver(void); | |
| − | ./include/zephyr/net/dns_resolve.h:462:#define dns_init_resolver(...) | |
| − | ./kernel/device.c:84: dev->state->init_res = rc;
| |
| − | ./kernel/device.c:161: return dev->state->initialized && (dev->state->init_res == 0U);
| |
| − | ./subsys/net/ip/net_core.c:476: dns_init_resolver();
| |
| − | ./subsys/net/lib/dns/resolve.c:1428:void dns_init_resolver(void)
| |
| − | What's going on in LM77 temperature sensor Kconfig file, on line 10? Interesting Kconfig stanza contains $(dt_compat_enabled,lm77):
| |
| | | | |
| − | * https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/sensor/lm77/Kconfig#L10
| + | 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 _ |
| | | | |
| − | Zephyr sensor API routine `device_is_ready()` is prototyped in [https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/device.h#L710 zephyr/include/zephyr/device.h]. A search for `device_is_ready()` implementation gives:
| + | * https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/drivers/sensor.h#L65 |
| | | | |
| − | <pre>
| + | _ Sensor attribute enum _ |
| − | ted@localhost1:~/projects-sandbox/workspace-out-of-tree/zephyr$ grep -nr z_device_is_ready ./*
| |
| − | ./include/zephyr/device.h:782:bool z_device_is_ready(const struct device *dev);
| |
| − | ./include/zephyr/device.h:803: return z_device_is_ready(dev);
| |
| − | ./kernel/device.c:108: if (z_device_is_ready(dev) && (dev->name == name)) {
| |
| − | ./kernel/device.c:114: if (z_device_is_ready(dev) && (strcmp(name, dev->name) == 0)) {
| |
| − | ./kernel/device.c:151:bool z_device_is_ready(const struct device *dev)
| |
| − | </pre>
| |
| | | | |
| − | The body of this API routine is:
| + | * https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/drivers/sensor.h#L342 |
| − | <pre>
| |
| − | bool z_device_is_ready(const struct device *dev)
| |
| − | {
| |
| − | /*
| |
| − | * if an invalid device pointer is passed as argument, this call
| |
| − | * reports the `device` as not ready for usage.
| |
| − | */
| |
| − | if (dev == NULL) {
| |
| − | return false;
| |
| − | }
| |
| | | | |
| − | return dev->state->initialized && (dev->state->init_res == 0U);
| + | 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. |
| − | }
| |
| − | </pre>
| |
| | | | |
| − | Having some trouble, however, finding places where `init_res` and `initialized` data members of device state structure get initialized. Looked recursively in both modules and zephyr source tree. Looks like there may be one place where this assignment takes place, in `zephyr/kernel/device.c`:
| + | For a Zephyr driver to support configuration of a sensor's attributes, it must implement a function of the form: |
| | | | |
| | <pre> | | <pre> |
| − | ted@localhost1:~/projects-sandbox/workspace-out-of-tree/zephyr$ grep -nr init_res ./* | grep -v reset | grep init_res
| + | sensor_attr_set() |
| − | ./include/zephyr/device.h:416: unsigned int init_res : 8;
| + | int sensor_attr_set(const struct device* dev, |
| − | ./include/zephyr/net/dns_resolve.h:459:void dns_init_resolver(void);
| + | enum sensor_channel chan, |
| − | ./include/zephyr/net/dns_resolve.h:462:#define dns_init_resolver(...)
| + | enum sensor_attribute attr, |
| − | ./kernel/device.c:84: dev->state->init_res = rc;
| + | const struct sensor_value* val) |
| − | ./kernel/device.c:161: return dev->state->initialized && (dev->state->init_res == 0U);
| |
| − | ./subsys/net/ip/net_core.c:476: dns_init_resolver();
| |
| − | ./subsys/net/lib/dns/resolve.c:1428:void dns_init_resolver(void)
| |
| | </pre> | | </pre> |
| | | | |
| − | Routine which iterates over and initializes all devices needing initialization prior to app code starting:
| + | . . . whose typedef is at https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/drivers/sensor.h#L431. |
| − | | |
| − | <pre>
| |
| − | 54 void z_sys_init_run_level(int32_t level)
| |
| − | 55 {
| |
| − | 56 static const struct init_entry *levels[] = {
| |
| − | 57 __init_PRE_KERNEL_1_start,
| |
| − | 58 __init_PRE_KERNEL_2_start,
| |
| − | 59 __init_POST_KERNEL_start,
| |
| − | 60 __init_APPLICATION_start,
| |
| − | 61 #ifdef CONFIG_SMP
| |
| − | 62 __init_SMP_start,
| |
| − | 63 #endif
| |
| − | 64 /* End marker */
| |
| − | 65 __init_end,
| |
| − | 66 };
| |
| − | 67 const struct init_entry *entry;
| |
| − | 68
| |
| − | 69 for (entry = levels[level]; entry < levels[level+1]; entry++) {
| |
| − | 70 const struct device *dev = entry->dev;
| |
| − | 71 int rc = entry->init(dev);
| |
| − | 72
| |
| − | 73 if (dev != NULL) {
| |
| − | 74 /* Mark device initialized. If initialization
| |
| − | 75 * failed, record the error condition.
| |
| − | 76 */
| |
| − | 77 if (rc != 0) {
| |
| − | 78 if (rc < 0) {
| |
| − | 79 rc = -rc;
| |
| − | 80 }
| |
| − | 81 if (rc > UINT8_MAX) {
| |
| − | 82 rc = UINT8_MAX;
| |
| − | 83 }
| |
| − | 84 dev->state->init_res = rc;
| |
| − | 85 }
| |
| − | 86 dev->state->initialized = true;
| |
| − | 87 }
| |
| − | 88 }
| |
| − | 89 }
| |
| − | </pre>
| |
| | | | |
| − | Sensor init priority:
| + | == [[#top|^]] Bosch BMI323 Driver Example == |
| | | | |
| − | <pre>
| + | 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. |
| − | ted@localhost1:~/projects-sandbox/workspace-for-nexus/zephyr$ grep -nr SENSOR_INIT_PRIORITY ./* | grep -v CONFIG_SENSOR | grep SENSOR_INIT_PRIORITY
| |
| − | ./boards/arm/thingy52_nrf52832/board.c:33:#error BOARD_CCS_VDD_PWR_CTRL_INIT_PRIORITY must be lower than SENSOR_INIT_PRIORITY | |
| − | ./boards/arm/thingy52_nrf52832/Kconfig:15: BOARD_VDD_PWR_CTRL_INIT_PRIORITY, but smaller than SENSOR_INIT_PRIORITY. | |
| − | ./drivers/sensor/Kconfig:17:config SENSOR_INIT_PRIORITY | |
| − | </pre>
| |
| | | | |
| − | | + | The structure is defined starting at about line 384, bmi323.c:384: |
| − | <!-- comentario -->
| |
| − | | |
| − | == [[#top|^]] Zephyr Kernel Level Device Support ==
| |
| − | | |
| − | Interesting code excerpt from `zephy/kernel/device.c`, this following routine is the implementation behind a wrapper like function `device_is_read()`. But this routine, and sensibly so, doesn't say anything about how a given device is initialized. It only looks at two data members of a device instance data structure:
| |
| | | | |
| | <pre> | | <pre> |
| − | 150
| + | static DEVICE_API(sensor, bosch_bmi323_api) = { |
| − | 151 bool z_device_is_ready(const struct device *dev)
| + | .attr_set = bosch_bmi323_driver_api_attr_set, <-- attribute setter |
| − | 152 {
| + | .attr_get = bosch_bmi323_driver_api_attr_get, |
| − | 153 /*
| + | .trigger_set = bosch_bmi323_driver_api_trigger_set, |
| − | 154 * if an invalid device pointer is passed as argument, this call
| + | .sample_fetch = bosch_bmi323_driver_api_sample_fetch, |
| − | 155 * reports the `device` as not ready for usage.
| + | .channel_get = bosch_bmi323_driver_api_channel_get, |
| − | 156 */
| + | }; |
| − | 157 if (dev == NULL) {
| |
| − | 158 return false;
| |
| − | 159 }
| |
| − | 160
| |
| − | 161 return dev->state->initialized && (dev->state->init_res == 0U);
| |
| − | 162 }
| |
| − | 163
| |
| | </pre> | | </pre> |
| | | | |
| − | On a somewhat unrelated note, a couple of important Zephyr documents, including Zephyr device tree macros used to obtain node identifiers:
| + | The attribute setter function is defined at about line 1101, bmi323.c:1101: |
| − | | |
| − | * https://docs.zephyrproject.org/latest/build/dts/api-usage.html#node-identifiers
| |
| − | | |
| − | * https://docs.zephyrproject.org/latest/build/dts/dt-vs-kconfig.html Device Tree versus Kconfig, a fairly short read
| |
| − | | |
| − | * https://docs.zephyrproject.org/1.14.0/guides/kconfig/index.html
| |
| − | | |
| − | | |
| − | <!-- comentario -->
| |
| − | | |
| − | == [[#top|^]] Understanding Zephyr SPI API ==
| |
| − | | |
| − | To make use of Zephyr API for SPI bus, we must in part understand how each of the three parameters passed to [https://docs.zephyrproject.org/latest/hardware/peripherals/spi.html#c.spi_read spi_read()] and [https://docs.zephyrproject.org/latest/hardware/peripherals/spi.html#c.spi_write spi_write()] are defined. Further, when we have a sensor with which we communicate in our Zephyr based app we need to understand how parts of this sensor's data structures in the firmware relate to the SPI bus to which the sensor is connected. Our sensor driver's code which talks with the sensor via SPI must pass certain SPI bus related pointers to Zephyr's SPI Application Programmers' Interface.
| |
| − | | |
| − | Another important SPI defining resource in Zephyr RTOS is the header file [https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/drivers/spi.h#L379 spi.h#L379]. This file defines the small data structure named `spi_dt_spec`. It gives us the data member names for a SPI bus peripheral, as Zephyr sees them.
| |
| − | | |
| − | 2022-11-25
| |
| − | | |
| − | The best Zephyr SPI instructions we find so far are in the sample app `spi_bitbang`. The source file main.c there, sets up RX and TX buffers, and some Zephyr driver structures about these. There is an interesting design pattern in Zephyr's SPI API: receive and transmit buffer pairs can be made multiple, in two arrays of these same buffers. The SPI interface function `spi_tranceive()`, about which spi_write() and spi_read() wrap, steps through the count of write buffers and receive buffers it is passed from calling code. In this way, a careful choosing of sets of buffers permits app developers to express more complex SPI communications by defining the buffers with needed lengths, and then call the SPI API of Zephyr RTOS just once. | |
| − | | |
| − | <!-- comentario -->
| |
| − | | |
| − | == [[#top|^]] Zephyr logging ==
| |
| − | | |
| − | Note, in a Zephyr project or an out-of-tree driver, what we have learned here is that LOG_MODULE_REGISTER(project_name) must be called in one -- possibly the first -- of the project's source files. Any other dot c files from which developers need logging capability must include the function like macro LOG_MODULE_DECLARE().
| |
| − | | |
| − | * https://docs.zephyrproject.org/3.0.0/reference/logging/index.html#logging-in-a-module
| |
| − | * https://docs.zephyrproject.org/3.0.0/reference/logging/index.html#c.LOG_MODULE_DECLARE
| |
| − | | |
| − | | |
| − | <!-- comentario -->
| |
| − | | |
| − | == [[#top|^]] LPC55S69 flexcomm2 and hs_lspi pins conflict ==
| |
| − | | |
| − | zephyr/boards/arm/lpcxpresso55s69/lpcxpresso55s69-pinctrl.dtsi lines 21 - 28
| |
| − | | |
| − | Note that in spite of the comment above in the dot dtsi file from NXP, SPI communications with an IIS2DH sensor work while UART2 is also in use and working - TMH
| |
| − | | |
| − | <!-- comentario -->
| |
| − | | |
| − | == [[#top|^]] STMicro device context data structure ==
| |
| − | | |
| − | Symbol `stmdev_ctx_t` is defined in file <b>modules/hal/st/sensor/stmemsc/iis2dh_STdC/driver/iis2dh_reg.c</b>. On github this file as of 2022-11-18 located at https://github.com/zephyrproject-rtos/hal_st/tree/master/sensor/stmemsc/iis2dh_STdC/driver'
| |
| − | | |
| − | Data structure `stmdev_ctx_t` defined as follows:
| |
| | | | |
| | <pre> | | <pre> |
| − | 104 /** @addtogroup Interfaces_Functions
| + | static int bosch_bmi323_driver_api_attr_set(const struct device *dev, |
| − | 105 * @brief This section provide a set of functions used to read and
| + | enum sensor_channel chan, |
| − | 106 * write a generic register of the device.
| + | enum sensor_attribute attr, |
| − | 107 * MANDATORY: return 0 -> no Error.
| + | const struct sensor_value *val) |
| − | 108 * @{
| + | { |
| − | 109 *
| + | struct bosch_bmi323_data *data = (struct bosch_bmi323_data *)dev->data; |
| − | 110 */
| + | int ret; |
| − | 111
| |
| − | 112 typedef int32_t (*stmdev_write_ptr)(void *, uint8_t, const uint8_t *, uint16_t);
| |
| − | 113 typedef int32_t (*stmdev_read_ptr)(void *, uint8_t, uint8_t *, uint16_t);
| |
| − | 114
| |
| − | 115 typedef struct
| |
| − | 116 {
| |
| − | 117 /** Component mandatory fields **/
| |
| − | 118 stmdev_write_ptr write_reg;
| |
| − | 119 stmdev_read_ptr read_reg;
| |
| − | 120 /** Customizable optional pointer **/
| |
| − | 121 void *handle;
| |
| − | 122 } stmdev_ctx_t;
| |
| − | </pre>
| |
| | | | |
| − | Parameter lists for the register write and register read functions are nearly the same, with exception of the third parameter. In the register write function this parameter is qualified with C `const`, as it is only expected to be read by the function using it.
| + | 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; |
| | | | |
| − | With the above definition, an instance of stmdev_cts_t is created for each device with device tree compatible property of 'st_iis2dh', and is assigned in `zephyr/drivers/sensor/iis2dh/iis2dh-i2c.c` as follows:
| + | case SENSOR_ATTR_FULL_SCALE: |
| − | | + | ret = bosch_bmi323_driver_api_set_acc_full_scale(dev, val); |
| − | <pre>
| |
| − | 38 stmdev_ctx_t iis2dh_i2c_ctx = {
| |
| − | 39 .read_reg = (stmdev_read_ptr) iis2dh_i2c_read,
| |
| − | 40 .write_reg = (stmdev_write_ptr) iis2dh_i2c_write,
| |
| − | 41 };
| |
| − | 42
| |
| − | 43 int iis2dh_i2c_init(const struct device *dev)
| |
| − | 44 {
| |
| − | 45 struct iis2dh_data *data = dev->data;
| |
| − | 46 const struct iis2dh_device_config *config = dev->config;
| |
| − | 47
| |
| − | 48 if (!device_is_ready(config->i2c.bus)) {
| |
| − | 49 LOG_ERR("Bus device is not ready");
| |
| − | 50 return -ENODEV;
| |
| − | 51 }
| |
| − | 52
| |
| − | 53 data->ctx = &iis2dh_i2c_ctx;
| |
| − | 54 data->ctx->handle = (void *)dev;
| |
| − | 55
| |
| − | 56 return 0;
| |
| − | 57 }
| |
| − | 58 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
| |
| − | </pre>
| |
| − | | |
| − | The above init function is called from iis2dh.c:
| |
| − | | |
| − | <pre>
| |
| − | 232 static int iis2dh_init_interface(const struct device *dev)
| |
| − | 233 {
| |
| − | 234 int res;
| |
| − | 235
| |
| − | 236 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
| |
| − | 237 res = iis2dh_spi_init(dev);
| |
| − | 238 if (res) {
| |
| − | 239 return res;
| |
| − | 240 }
| |
| − | 241 #elif DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
| |
| − | 242 res = iis2dh_i2c_init(dev);
| |
| − | 243 if (res) {
| |
| − | 244 return res;
| |
| − | 245 }
| |
| − | 246 #else
| |
| − | 247 #error "BUS MACRO NOT DEFINED IN DTS"
| |
| − | 248 #endif
| |
| − | 249
| |
| − | 250 return 0;
| |
| − | 251 }
| |
| − | 252
| |
| − | 253 static int iis2dh_init(const struct device *dev)
| |
| − | 254 {
| |
| − | 255 struct iis2dh_data *iis2dh = dev->data;
| |
| − | 256 const struct iis2dh_device_config *cfg = dev->config;
| |
| − | 257 uint8_t wai;
| |
| − | 258
| |
| − | 259 if (iis2dh_init_interface(dev)) {
| |
| − | 260 return -EINVAL;
| |
| − | 261 }
| |
| − | | |
| − | </pre>
| |
| | | | |
| | + | break; |
| | | | |
| | + | case SENSOR_ATTR_FEATURE_MASK: |
| | + | ret = bosch_bmi323_driver_api_set_acc_feature_mask(dev, val); |
| | | | |
| − | Pointer *dev has a member named 'data', which in turn has a member named 'ctx', which in turn gets assigned the address of a not named instance of `iis2dh_i2c_ctx`. This means that we have `dev->data->ctx->read_reg()` and `dev->data->ctx->write_reg` on successful completion of routine iis2dh_i2c_init(). This is reflected in <b>modules/hal/st/sensor/stmemsc/iis2dh_STdC/driver/iis2dh_reg.c</b>, in the two generalized register read and write functions:
| + | break; |
| | | | |
| − | <pre>
| + | default: |
| − | 39 /**
| + | ret = -ENODEV; |
| − | 40 * @brief Read generic device register
| |
| − | 41 *
| |
| − | 42 * @param ctx read / write interface definitions(ptr)
| |
| − | 43 * @param reg register to read
| |
| − | 44 * @param data pointer to buffer that store the data read(ptr)
| |
| − | 45 * @param len number of consecutive register to read
| |
| − | 46 * @retval interface status (MANDATORY: return 0 -> no Error)
| |
| − | 47 *
| |
| − | 48 */
| |
| − | 49 int32_t iis2dh_read_reg(stmdev_ctx_t *ctx, uint8_t reg, uint8_t *data,
| |
| − | 50 uint16_t len)
| |
| − | 51 {
| |
| − | 52 int32_t ret;
| |
| − | 53
| |
| − | 54 ret = ctx->read_reg(ctx->handle, reg, data, len);
| |
| − | 55
| |
| − | 56 return ret;
| |
| − | 57 }
| |
| − | 58
| |
| − | 59 /**
| |
| − | 60 * @brief Write generic device register
| |
| − | 61 *
| |
| − | 62 * @param ctx read / write interface definitions(ptr)
| |
| − | 63 * @param reg register to write
| |
| − | 64 * @param data pointer to data to write in register reg(ptr)
| |
| − | 65 * @param len number of consecutive register to write
| |
| − | 66 * @retval interface status (MANDATORY: return 0 -> no Error)
| |
| − | 67 *
| |
| − | 68 */
| |
| − | 69 int32_t iis2dh_write_reg(stmdev_ctx_t *ctx, uint8_t reg,
| |
| − | 70 uint8_t *data,
| |
| − | 71 uint16_t len)
| |
| − | 72 {
| |
| − | 73 int32_t ret;
| |
| − | 74
| |
| − | 75 ret = ctx->write_reg(ctx->handle, reg, data, len);
| |
| − | 76
| |
| − | 77 return ret;
| |
| − | 78 }
| |
| − | </pre>
| |
| | | | |
| | + | 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; |
| | | | |
| − | https://docs.zephyrproject.org/latest/hardware/peripherals/i2c.html#c.i2c_burst_read_dt
| + | 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); |
| | | | |
| − | <!-- comentario -->
| + | break; |
| | | | |
| − | == [[#top|^]] Zephyr interrupt set up code == | + | default: |
| | + | ret = -ENODEV; |
| | | | |
| − | In Zephyr RTOS context, triggers are interrupts which are generated by sensors. Zephyr constructs a notion of "trigger types" and "trigger channels". Some pages from Zephyr 3.2.0 documentation:
| + | break; |
| | + | } |
| | | | |
| − | * https://docs.zephyrproject.org/latest/hardware/peripherals/sensor.html#triggers
| + | break; |
| − | * https://docs.zephyrproject.org/latest/hardware/peripherals/sensor.html#c.sensor_trigger_type
| |
| | | | |
| | + | default: |
| | + | ret = -ENODEV; |
| | | | |
| − | This section created to help address a challenge we have, setting up a sensor interrupt or Zephyr "trigger" in KX132 driver. While unsure, there is suspicion that the `int_gpio` member of the sensor's `config` data structure may not be getting assigned a GPIO port at all, in an early Zephyr init level at boot time. In a diagnostic, `cfg->int_gpio.port->name` shows as "=&", which does not look like a valid port name.
| + | break; |
| | + | } |
| | | | |
| − | KX132's config data structure includes this snippet of code:
| + | k_mutex_unlock(&data->lock); |
| | | | |
| − | <pre>
| + | return ret; |
| − | 58 uint8_t pm;
| + | } |
| − | 59 #ifdef CONFIG_KX132_TRIGGER
| |
| − | 60 #warning "KX132 1211 driver - compiling gpio_dt_spec instance in struct 'kx132_device_config'"
| |
| − | 61 // # REF https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/drivers/gpio.h#L271
| |
| − | 62 struct gpio_dt_spec int_gpio;
| |
| − | 63 #endif /* CONFIG_KX132_TRIGGER */
| |
| − | 64 };
| |
| | </pre> | | </pre> |
| | | | |
| − | In a different sample app, iis2dh-driver-demo, we were able to set up a trigger per the code from Zephyr's `samples/basic/button` app. The early declarative elements to prepare to use a GPIO pin on the MCU, as an interrupt input line connected to a sensor, are in this sample:
| + | == [[#top|^]] Mutex Use in BMI323 Driver == |
| | | | |
| − | <pre>
| + | BMI323 driver code locks a mutex in the following routines of bmi323.c: |
| − | 127 #include <zephyr/drivers/gpio.h> // needed to provide GPIO_DT_SPEC_GET_OR Zephyr device tree macro
| |
| − | 128
| |
| − | 129 #define SENSOR_INT1_NODE DT_ALIAS(sensorinterrupt1)
| |
| − | 130 #if !DT_NODE_HAS_STATUS(SENSOR_INT1_NODE, okay)
| |
| − | 131 #error "- DEV 1108 - Could not find in device tree source any sensor interrupt type node!"
| |
| − | 132 #endif
| |
| − | 133 //static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET_OR(SW0_NODE, gpios, {0});
| |
| − | 134 static const struct gpio_dt_spec interrupt_line_1 = GPIO_DT_SPEC_GET_OR(SENSOR_INT1_NODE, gpios, {0});
| |
| − | 135
| |
| − | 136 static struct gpio_callback sensor_interrupt_1_cb_data;
| |
| − | </pre>
| |
| − | | |
| − | Device tree overlay expresses the above referenced DTS node alias:
| |
| | | | |
| − | <pre>
| + | 1. line 384: static int bosch_bmi323_driver_api_attr_set(...) |
| − | 5 / {
| |
| − | 6 aliases {
| |
| − | 9 sensorinterrupt1 = &iis2dhint1;
| |
| − | 11 }; | |
| − | 12 };
| |
| | | | |
| − | 65 / { | + | 2. line 727: static int bosch_bmi323_driver_api_attr_get(...) |
| − | 66 sensor_interrupts {
| |
| − | 67 compatible = "gpio-keys";
| |
| − | 68 iis2dhint1: iis2dh_int1 {
| |
| − | 69 gpios = < &gpio1 9 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
| |
| − | 70 };
| |
| − | 71 /* ... other buttons ... */
| |
| − | 72 };
| |
| − | 73 };
| |
| − | </pre>
| |
| | | | |
| − | In a driver use case, the interrupt line(s) or pins themselves likely won't require a DTS node alias. These GPIO pins will be expressed as values in the sensor's device tree node. The property will be one of `irq-gpios`, `drdy-gpios` and similar, depending on the given sensor's device tree bindings file. Of course, the port controlling the given pins must somewhere in device tree or DTS overlay files be enabled.
| + | 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(...) |
| | | | |
| − | Zephyr interrupt set up code from button sample app . . .
| + | == [[#top|^]] BMI323 Driver Routines == |
| | | | |
| | <pre> | | <pre> |
| − | MODULE_ID__THREAD_IIS2DH, sensor->state->init_res);
| + | 59:static int bosch_bmi323_bus_init(const struct device *dev) |
| − | | + | 68:static int bosch_bmi323_bus_read_words(const struct device *dev, uint8_t offset, uint16_t *words, |
| − | | + | 78:static int bosch_bmi323_bus_write_words(const struct device *dev, uint8_t offset, uint16_t *words, |
| − | | + | 88:static int32_t bosch_bmi323_lsb_from_fullscale(int64_t fullscale) |
| − | // 'button' from samples/basic/button becomes 'interrupt_line_1':
| + | 94:static int64_t bosch_bmi323_value_to_micro(int16_t value, int32_t lsb) |
| − | | + | 100:static void bosch_bmi323_value_to_sensor_value(struct sensor_value *result, int16_t value, |
| − | // STEP config step
| + | 111:static bool bosch_bmi323_value_is_valid(int16_t value) |
| − | if (!device_is_ready(interrupt_line_1.port)) {
| + | 116:static int bosch_bmi323_validate_chip_id(const struct device *dev) |
| − | printk("Error: sensor interrupt 1 port '%s' detected as not ready\n",
| + | 134:static int bosch_bmi323_soft_reset(const struct device *dev) |
| − | interrupt_line_1.port->name);
| + | 152:static int bosch_bmi323_enable_feature_engine(const struct device *dev) |
| − | // return;
| + | 178:static int bosch_bmi323_driver_api_set_acc_odr(const struct device *dev, |
| − | }
| + | 226:static int bosch_bmi323_driver_api_set_acc_full_scale(const struct device *dev, |
| − | | + | 257:static int bosch_bmi323_driver_api_set_acc_feature_mask(const struct device *dev, |
| − | // STEP config step
| + | 280:static int bosch_bmi323_driver_api_set_gyro_odr(const struct device *dev, |
| − | rstatus = gpio_pin_configure_dt(&interrupt_line_1, GPIO_INPUT);
| + | 328:static int bosch_bmi323_driver_api_set_gyro_full_scale(const struct device *dev, |
| − | if (rstatus != 0) {
| + | 361:static int bosch_bmi323_driver_api_set_gyro_feature_mask(const struct device *dev, |
| − | printk("Error %d: failed to configure %s pin %d\n",
| + | 384:static int bosch_bmi323_driver_api_attr_set(const struct device *dev, enum sensor_channel chan, |
| − | rstatus, interrupt_line_1.port->name, interrupt_line_1.pin);
| + | 455:static int bosch_bmi323_driver_api_get_acc_odr(const struct device *dev, struct sensor_value *val) |
| − | // return;
| + | 530:static int bosch_bmi323_driver_api_get_acc_full_scale(const struct device *dev, |
| − | }
| + | 566:static int bosch_bmi323_driver_api_get_acc_feature_mask(const struct device *dev, |
| − | | + | 589:static int bosch_bmi323_driver_api_get_gyro_odr(const struct device *dev, struct sensor_value *val) |
| − | // STEP config step
| + | 664:static int bosch_bmi323_driver_api_get_gyro_full_scale(const struct device *dev, |
| − | rstatus = gpio_pin_interrupt_configure_dt(&interrupt_line_1,
| + | 704:static int bosch_bmi323_driver_api_get_gyro_feature_mask(const struct device *dev, |
| − | GPIO_INT_EDGE_TO_ACTIVE);
| + | 727:static int bosch_bmi323_driver_api_attr_get(const struct device *dev, enum sensor_channel chan, |
| − | if (rstatus != 0) {
| + | 796:static int bosch_bmi323_driver_api_trigger_set_acc_drdy(const struct device *dev) |
| − | printk("Error %d: failed to configure interrupt on %s pin %d\n",
| + | 806:static int bosch_bmi323_driver_api_trigger_set_acc_motion(const struct device *dev) |
| − | rstatus, interrupt_line_1.port->name, interrupt_line_1.pin);
| + | 843:static int bosch_bmi323_driver_api_trigger_set(const struct device *dev, |
| − | // return;
| + | 883:static int bosch_bmi323_driver_api_fetch_acc_samples(const struct device *dev) |
| − | }
| + | 925:static int bosch_bmi323_driver_api_fetch_gyro_samples(const struct device *dev) |
| − | | + | 968:static int bosch_bmi323_driver_api_fetch_temperature(const struct device *dev) |
| − | // STEP config step
| + | 996:static int bosch_bmi323_driver_api_sample_fetch(const struct device *dev, enum sensor_channel chan) |
| − | // gpio_init_callback(&button_cb_data, button_pressed, BIT(button.pin));
| + | 1047:static int bosch_bmi323_driver_api_channel_get(const struct device *dev, enum sensor_channel chan, |
| − | // gpio_add_callback(button.port, &button_cb_data);
| + | 1100:static DEVICE_API(sensor, bosch_bmi323_api) = { |
| − | // printk("Set up button at %s pin %d\n", button.port->name, button.pin);
| + | 1108:static void bosch_bmi323_irq_callback(const struct device *dev) |
| − | | + | 1115:static int bosch_bmi323_init_irq(const struct device *dev) |
| − | gpio_init_callback(&sensor_interrupt_1_cb_data, sensor_interrupt_1_cb, BIT(interrupt_line_1.pin));
| + | 1139:static int bosch_bmi323_init_int1(const struct device *dev) |
| − | gpio_add_callback(interrupt_line_1.port, &sensor_interrupt_1_cb_data);
| + | 1150:static void bosch_bmi323_irq_callback_handler(struct k_work *item) |
| − | printk("Set up button at %s pin %d\n", interrupt_line_1.port->name, interrupt_line_1.pin);
| + | 1164:static int bosch_bmi323_pm_resume(const struct device *dev) |
| − | | + | 1225:static int bosch_bmi323_pm_suspend(const struct device *dev) |
| | + | 1241:static int bosch_bmi323_pm_action(const struct device *dev, enum pm_device_action action) |
| | + | 1271:static int bosch_bmi323_init(const struct device *dev) |
| | + | 1318: static struct bosch_bmi323_data bosch_bmi323_data_##inst; \ |
| | + | 1322: static void bosch_bmi323_irq_callback##inst(const struct device *dev, \ |
| | + | 1328: static const struct bosch_bmi323_config bosch_bmi323_config_##inst = { \ |
| | </pre> | | </pre> |
| | | | |
| − | Interestingly this demo code gives the following port name after the GPIO based interrupt is successfully configured:
| + | == [[#top|^]] Sensor Bus API and Sensor Bus == |
| | | | |
| | <pre> | | <pre> |
| − | *** Booting Zephyr OS build zephyr-v3.2.0 *** | + | 178 struct bosch_bmi323_bus_api { |
| − | - scoreboard - in setter function test val holds 0,
| + | 179 /* Read up to multiple words from the BMI323 */ |
| − | - scoreboard - setting test val to 5,
| + | 180 int (*read_words)(const void *context, uint8_t offset, uint16_t *words, |
| − | Success in init of KX132!
| + | 181 uint16_t words_count); |
| − | - DEV 1028 - symbol ST_IIS2DH got assigned 'DT_N_S_soc_S_peripheral_50000000_S_spi_9f000_S_iis2dh_0'
| + | 182 |
| − | - kd_thread_iis2dh - Success finding iis2dh device,
| + | 183 /* Write up to multiple words to the BLI323 */ |
| − | - kd_thread_iis2dh - sensor device 'iis2dh@0' is ready,
| + | 184 int (*write_words)(const void *context, uint8_t offset, uint16_t *words, |
| − | - kd_thread_iis2dh - INFO: device name (Zephyr compile time setting) found to be 'iis2dh@0'
| + | 185 uint16_t words_count); |
| − | - kd_thread_iis2dh - INFO: boot time Zephyr init result value holds 0,
| + | 186 |
| − | INFO: sensor interrupt 1 port 'gpio@1' detected as ready!
| + | 187 /* Initialize the bus */ |
| − | Set up button at gpio@1 pin 9
| + | 188 int (*init)(const void *context); |
| | + | 189 }; |
| | + | 190 |
| | + | 191 struct bosch_bmi323_bus { |
| | + | 192 const void *context; |
| | + | 193 const struct bosch_bmi323_bus_api *api; |
| | + | 194 }; |
| | </pre> | | </pre> |
| | | | |
| − | Name `gpio@1` is definitely not equivalent or like `=&`. So QUESTION: where in the run time process, and in the source code of a given out-of-tree driver, is a valid port controller assigned to `const struct device *dev->cfg->int_gpio.port`?
| + | == [[#top|^]] References == |
| − | | |
| − | In Jared Wolff's sgp40 driver there's explicit code to assign values to sensor data structure members which support an enable line. Line of interest is [https://github.com/circuitdojo/air-quality-wing-zephyr-drivers/blob/main/drivers/sgp40/sgp40.c#L153 sgp40.c line 153]. In this case the line communication is an enable line, not an interrupt, but the needed set up should be equivalent.
| |
| − | | |
| − | QUESTION: where and how is STMicro IIS2DH driver achieving the same set up for its "data ready", drdy gpio?
| |
| | | | |
| | + | <span id="ref--zephyr-interrupts--zephyr-s-issues-s-30133"></span> |
| | + | 1. https://github.com/zephyrproject-rtos/zephyr/issues/30133 |
| | | | |
| | <!-- comentario --> | | <!-- comentario --> |
| | | | |
| − | == [[#top|^]] C Preprocessor macros ==
| |
| − |
| |
| − | * https://stackoverflow.com/questions/1489932/how-can-i-concatenate-twice-with-the-c-preprocessor-and-expand-a-macro-as-in-ar
| |
| − |
| |
| − | <!-- comentario -->
| |
| − |
| |
| − | == [[#top|^]] Zephyr device tree macros ==
| |
| − |
| |
| − | Section on Zephyr device tree macros, many defined in devicetree.h. Also worth noting is [https://docs.zephyrproject.org/3.2.0/build/dts/howtos.html#device-drivers-that-depend-on-other-devices Zephyr 3.2.0 notes on device drivers that depend on other devices].
| |
| − |
| |
| − | <i>Definition of DT_DRV_INST</i>
| |
| − |
| |
| − | <pre>
| |
| − | 2903 /**
| |
| − | 2904 * @defgroup devicetree-inst Instance-based devicetree APIs
| |
| − | 2905 * @ingroup devicetree
| |
| − | 2906 * @{
| |
| − | 2907 */
| |
| − | 2908
| |
| − | 2909 /**
| |
| − | 2910 * @brief Node identifier for an instance of a `DT_DRV_COMPAT` compatible
| |
| − | 2911 * @param inst instance number
| |
| − | 2912 * @return a node identifier for the node with `DT_DRV_COMPAT` compatible and
| |
| − | 2913 * instance number @p inst
| |
| − | 2914 */
| |
| − | 2915 #define DT_DRV_INST(inst) DT_INST(inst, DT_DRV_COMPAT)
| |
| − | </pre>
| |
| − |
| |
| − |
| |
| − | <i>Definition of DT_INST</i>
| |
| − |
| |
| − | <pre>
| |
| − | 232 /**
| |
| − | 233 * @brief Get a node identifier for an instance of a compatible
| |
| − | 234 *
| |
| − | 235 * All nodes with a particular compatible property value are assigned
| |
| − | 236 * instance numbers, which are zero-based indexes specific to that
| |
| − | 237 * compatible. You can get a node identifier for these nodes by
| |
| − | 238 * passing DT_INST() an instance number, @p inst, along with the
| |
| − | 239 * lowercase-and-underscores version of the compatible, @p compat.
| |
| − | 240 *
| |
| − | 241 * Instance numbers have the following properties:
| |
| − | 242 *
| |
| − | 243 * - for each compatible, instance numbers start at 0 and are contiguous
| |
| − | 244 * - exactly one instance number is assigned for each node with a compatible,
| |
| − | 245 * **including disabled nodes**
| |
| − | 246 * - enabled nodes (status property is `okay` or missing) are assigned the
| |
| − | 247 * instance numbers starting from 0, and disabled nodes have instance
| |
| − | 248 * numbers which are greater than those of any enabled node
| |
| − | 249 *
| |
| − | 250 * No other guarantees are made. In particular:
| |
| − | 251 *
| |
| − | 252 * - instance numbers **in no way reflect** any numbering scheme that
| |
| − | 253 * might exist in SoC documentation, node labels or unit addresses,
| |
| − | 254 * or properties of the /aliases node (use DT_NODELABEL() or DT_ALIAS()
| |
| − | 255 * for those)
| |
| − | 256 * - there **is no general guarantee** that the same node will have
| |
| − | 257 * the same instance number between builds, even if you are building
| |
| − | 258 * the same application again in the same build directory
| |
| − | 259 *
| |
| − | 260 * Example devicetree fragment:
| |
| − | 261 *
| |
| − | 262 * @code{.dts}
| |
| − | 263 * serial1: serial@40001000 {
| |
| − | 264 * compatible = "vnd,soc-serial";
| |
| − | 265 * status = "disabled";
| |
| − | 266 * current-speed = <9600>;
| |
| − | 267 * ...
| |
| − | 268 * };
| |
| − | 269 *
| |
| − | 270 * serial2: serial@40002000 {
| |
| − | 271 * compatible = "vnd,soc-serial";
| |
| − | 272 * status = "okay";
| |
| − | 273 * current-speed = <57600>;
| |
| − | 274 * ...
| |
| − | 275 * };
| |
| − | 276 *
| |
| − | 277 * serial3: serial@40003000 {
| |
| − | 278 * compatible = "vnd,soc-serial";
| |
| − | 279 * current-speed = <115200>;
| |
| − | 280 * ...
| |
| − | 281 * };
| |
| − |
| |
| − | 282 * @endcode
| |
| − | 283 *
| |
| − | 284 * Assuming no other nodes in the devicetree have compatible
| |
| − | 285 * `"vnd,soc-serial"`, that compatible has nodes with instance numbers
| |
| − | 286 * 0, 1, and 2.
| |
| − | 287 *
| |
| − | 288 * The nodes `serial@40002000` and `serial@40003000` are both enabled, so
| |
| − | 289 * their instance numbers are 0 and 1, but no guarantees are made
| |
| − | 290 * regarding which node has which instance number.
| |
| − | 291 *
| |
| − | 292 * Since `serial@40001000` is the only disabled node, it has instance
| |
| − | 293 * number 2, since disabled nodes are assigned the largest instance
| |
| − | 294 * numbers. Therefore:
| |
| − | 295 *
| |
| − | 296 * @code{.c}
| |
| − | 297 * // Could be 57600 or 115200. There is no way to be sure:
| |
| − | 298 * // either serial@40002000 or serial@40003000 could
| |
| − | 299 * // have instance number 0, so this could be the current-speed
| |
| − | 300 * // property of either of those nodes.
| |
| − | 301 * DT_PROP(DT_INST(0, vnd_soc_serial), current_speed)
| |
| − | 302 *
| |
| − | 303 * // Could be 57600 or 115200, for the same reason.
| |
| − | 304 * // If the above expression expands to 57600, then
| |
| − | 305 * // this expands to 115200, and vice-versa.
| |
| − | 306 * DT_PROP(DT_INST(1, vnd_soc_serial), current_speed)
| |
| − | 307 *
| |
| − | 308 * // 9600, because there is only one disabled node, and
| |
| − | 309 * // disabled nodes are "at the the end" of the instance
| |
| − | 310 * // number "list".
| |
| − | 311 * DT_PROP(DT_INST(2, vnd_soc_serial), current_speed)
| |
| − | 312 * @endcode
| |
| − | 313 *
| |
| − | 314 * Notice how `"vnd,soc-serial"` in the devicetree becomes `vnd_soc_serial`
| |
| − | 315 * (without quotes) in the DT_INST() arguments. (As usual, `current-speed`
| |
| − | 316 * in the devicetree becomes `current_speed` as well.)
| |
| − | 317 *
| |
| − | 318 * Nodes whose `compatible` property has multiple values are assigned
| |
| − | 319 * independent instance numbers for each compatible.
| |
| − | 320 *
| |
| − | 321 * @param inst instance number for compatible @p compat
| |
| − | 322 * @param compat lowercase-and-underscores compatible, without quotes
| |
| − | 323 * @return node identifier for the node with that instance number and
| |
| − | 324 * compatible
| |
| − | 325 */
| |
| − | 326 #define DT_INST(inst, compat) UTIL_CAT(DT_N_INST, DT_DASH(inst, compat))
| |
| − | </pre>
| |
| − |
| |
| − | <!-- comentario -->
| |
| | | | |
| − | == [[#top|^]] Sensors to seek for Zephyr demo exercising ==
| + | <center> |
| | + | [[#top|- - - top of page - - -]] |
| | + | </center> |
| | | | |
| − | [ ] [https://www.adafruit.com/product/1782 MCP9808] I2C based temperature sensor, [https://www.mouser.com/datasheet/2/268/25095A-15487.pdf datasheet], long lead times at Mouser
| |
| | | | |
| | <!-- comentario --> | | <!-- comentario --> |
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(...)
^ BMI323 Driver Routines
59:static int bosch_bmi323_bus_init(const struct device *dev)
68:static int bosch_bmi323_bus_read_words(const struct device *dev, uint8_t offset, uint16_t *words,
78:static int bosch_bmi323_bus_write_words(const struct device *dev, uint8_t offset, uint16_t *words,
88:static int32_t bosch_bmi323_lsb_from_fullscale(int64_t fullscale)
94:static int64_t bosch_bmi323_value_to_micro(int16_t value, int32_t lsb)
100:static void bosch_bmi323_value_to_sensor_value(struct sensor_value *result, int16_t value,
111:static bool bosch_bmi323_value_is_valid(int16_t value)
116:static int bosch_bmi323_validate_chip_id(const struct device *dev)
134:static int bosch_bmi323_soft_reset(const struct device *dev)
152:static int bosch_bmi323_enable_feature_engine(const struct device *dev)
178:static int bosch_bmi323_driver_api_set_acc_odr(const struct device *dev,
226:static int bosch_bmi323_driver_api_set_acc_full_scale(const struct device *dev,
257:static int bosch_bmi323_driver_api_set_acc_feature_mask(const struct device *dev,
280:static int bosch_bmi323_driver_api_set_gyro_odr(const struct device *dev,
328:static int bosch_bmi323_driver_api_set_gyro_full_scale(const struct device *dev,
361:static int bosch_bmi323_driver_api_set_gyro_feature_mask(const struct device *dev,
384:static int bosch_bmi323_driver_api_attr_set(const struct device *dev, enum sensor_channel chan,
455:static int bosch_bmi323_driver_api_get_acc_odr(const struct device *dev, struct sensor_value *val)
530:static int bosch_bmi323_driver_api_get_acc_full_scale(const struct device *dev,
566:static int bosch_bmi323_driver_api_get_acc_feature_mask(const struct device *dev,
589:static int bosch_bmi323_driver_api_get_gyro_odr(const struct device *dev, struct sensor_value *val)
664:static int bosch_bmi323_driver_api_get_gyro_full_scale(const struct device *dev,
704:static int bosch_bmi323_driver_api_get_gyro_feature_mask(const struct device *dev,
727:static int bosch_bmi323_driver_api_attr_get(const struct device *dev, enum sensor_channel chan,
796:static int bosch_bmi323_driver_api_trigger_set_acc_drdy(const struct device *dev)
806:static int bosch_bmi323_driver_api_trigger_set_acc_motion(const struct device *dev)
843:static int bosch_bmi323_driver_api_trigger_set(const struct device *dev,
883:static int bosch_bmi323_driver_api_fetch_acc_samples(const struct device *dev)
925:static int bosch_bmi323_driver_api_fetch_gyro_samples(const struct device *dev)
968:static int bosch_bmi323_driver_api_fetch_temperature(const struct device *dev)
996:static int bosch_bmi323_driver_api_sample_fetch(const struct device *dev, enum sensor_channel chan)
1047:static int bosch_bmi323_driver_api_channel_get(const struct device *dev, enum sensor_channel chan,
1100:static DEVICE_API(sensor, bosch_bmi323_api) = {
1108:static void bosch_bmi323_irq_callback(const struct device *dev)
1115:static int bosch_bmi323_init_irq(const struct device *dev)
1139:static int bosch_bmi323_init_int1(const struct device *dev)
1150:static void bosch_bmi323_irq_callback_handler(struct k_work *item)
1164:static int bosch_bmi323_pm_resume(const struct device *dev)
1225:static int bosch_bmi323_pm_suspend(const struct device *dev)
1241:static int bosch_bmi323_pm_action(const struct device *dev, enum pm_device_action action)
1271:static int bosch_bmi323_init(const struct device *dev)
1318: static struct bosch_bmi323_data bosch_bmi323_data_##inst; \
1322: static void bosch_bmi323_irq_callback##inst(const struct device *dev, \
1328: static const struct bosch_bmi323_config bosch_bmi323_config_##inst = { \
^ Sensor Bus API and Sensor Bus
178 struct bosch_bmi323_bus_api {
179 /* Read up to multiple words from the BMI323 */
180 int (*read_words)(const void *context, uint8_t offset, uint16_t *words,
181 uint16_t words_count);
182
183 /* Write up to multiple words to the BLI323 */
184 int (*write_words)(const void *context, uint8_t offset, uint16_t *words,
185 uint16_t words_count);
186
187 /* Initialize the bus */
188 int (*init)(const void *context);
189 };
190
191 struct bosch_bmi323_bus {
192 const void *context;
193 const struct bosch_bmi323_bus_api *api;
194 };
^ References
1. https://github.com/zephyrproject-rtos/zephyr/issues/30133
- - - top of page - - -