1212
1313LOG_MODULE_REGISTER (sensor_compat , CONFIG_SENSOR_LOG_LEVEL );
1414
15+ /*
16+ * Ensure that the size of the generic header aligns with the sensor channel enum. If it doesn't,
17+ * then cores that require aligned memory access will fail to read channel[0].
18+ */
19+ BUILD_ASSERT ((sizeof (struct sensor_data_generic_header ) % sizeof (enum sensor_channel )) == 0 );
20+
1521static void sensor_submit_fallback (const struct device * dev , struct rtio_iodev_sqe * iodev_sqe );
1622
1723static void sensor_iodev_submit (struct rtio_iodev_sqe * iodev_sqe )
@@ -49,6 +55,21 @@ static inline int compute_num_samples(const enum sensor_channel *channels, size_
4955 return num_samples ;
5056}
5157
58+ /**
59+ * @brief Compute the required header size
60+ *
61+ * This function takes into account alignment of the q31 values that will follow the header.
62+ *
63+ * @param[in] num_output_samples The number of samples to represent
64+ * @return The number of bytes needed for this sample frame's header
65+ */
66+ static inline uint32_t compute_header_size (int num_output_samples )
67+ {
68+ uint32_t size = sizeof (struct sensor_data_generic_header ) +
69+ (num_output_samples * sizeof (enum sensor_channel ));
70+ return (size + 3 ) & ~0x3 ;
71+ }
72+
5273/**
5374 * @brief Compute the minimum number of bytes needed
5475 *
@@ -57,8 +78,7 @@ static inline int compute_num_samples(const enum sensor_channel *channels, size_
5778 */
5879static inline uint32_t compute_min_buf_len (int num_output_samples )
5980{
60- return sizeof (struct sensor_data_generic_header ) + (num_output_samples * sizeof (q31_t )) +
61- (num_output_samples * sizeof (enum sensor_channel ));
81+ return compute_header_size (num_output_samples ) + (num_output_samples * sizeof (q31_t ));
6282}
6383
6484/**
@@ -121,8 +141,7 @@ static void sensor_submit_fallback(const struct device *dev, struct rtio_iodev_s
121141 header -> num_channels = num_output_samples ;
122142 header -> shift = 0 ;
123143
124- q31_t * q = (q31_t * )(buf + sizeof (struct sensor_data_generic_header ) +
125- num_output_samples * sizeof (enum sensor_channel ));
144+ q31_t * q = (q31_t * )(buf + compute_header_size (num_output_samples ));
126145
127146 /* Populate values, update shift, and set channels */
128147 for (size_t i = 0 , sample_idx = 0 ; i < cfg -> count ; ++ i ) {
0 commit comments