12
12
13
13
LOG_MODULE_REGISTER (sensor_compat , CONFIG_SENSOR_LOG_LEVEL );
14
14
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
+
15
21
static void sensor_submit_fallback (const struct device * dev , struct rtio_iodev_sqe * iodev_sqe );
16
22
17
23
static 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_
49
55
return num_samples ;
50
56
}
51
57
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
+
52
73
/**
53
74
* @brief Compute the minimum number of bytes needed
54
75
*
@@ -57,8 +78,7 @@ static inline int compute_num_samples(const enum sensor_channel *channels, size_
57
78
*/
58
79
static inline uint32_t compute_min_buf_len (int num_output_samples )
59
80
{
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 ));
62
82
}
63
83
64
84
/**
@@ -121,8 +141,7 @@ static void sensor_submit_fallback(const struct device *dev, struct rtio_iodev_s
121
141
header -> num_channels = num_output_samples ;
122
142
header -> shift = 0 ;
123
143
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 ));
126
145
127
146
/* Populate values, update shift, and set channels */
128
147
for (size_t i = 0 , sample_idx = 0 ; i < cfg -> count ; ++ i ) {
0 commit comments