@@ -35,6 +35,10 @@ uint8_t rx_data[7];
35
35
36
36
const struct device * i2c_dev = DEVICE_DT_GET (I2C_DEV_NODE );
37
37
38
+ #ifdef CONFIG_I2C_RTIO
39
+ static void i2c_ram_rtio_before (void );
40
+ #endif
41
+
38
42
/* Address from datasheet is 0b1010xxxr where x bits are additional
39
43
* memory address bits and r is the r/w i2c bit.
40
44
*
@@ -75,6 +79,10 @@ static void i2c_ram_before(void *f)
75
79
addr += ARRAY_SIZE (tx_data ) - TX_DATA_OFFSET ;
76
80
memset (rx_data , 0 , ARRAY_SIZE (rx_data ));
77
81
82
+ #ifdef CONFIG_I2C_RTIO
83
+ i2c_ram_rtio_before ();
84
+ #endif
85
+
78
86
#ifdef CONFIG_PM_DEVICE_RUNTIME
79
87
pm_device_runtime_get (i2c_dev );
80
88
#endif
@@ -178,6 +186,26 @@ ZTEST(i2c_ram, test_ram_transfer_cb)
178
186
I2C_IODEV_DEFINE (i2c_iodev , I2C_DEV_NODE , RAM_ADDR );
179
187
RTIO_DEFINE (i2c_rtio , 2 , 2 );
180
188
189
+ static void i2c_ram_rtio_before (void )
190
+ {
191
+ struct rtio_cqe * cqe ;
192
+
193
+ while ((cqe = rtio_cqe_consume (& i2c_rtio ))) {
194
+ rtio_cqe_release (& i2c_rtio , cqe );
195
+ }
196
+ }
197
+
198
+ static void check_completion (struct rtio_cqe * cqe , const char * msg )
199
+ {
200
+ int32_t result ;
201
+
202
+ result = cqe -> result ;
203
+ rtio_cqe_release (& i2c_rtio , cqe );
204
+ if (result ) {
205
+ zassert_ok (result , msg );
206
+ }
207
+ }
208
+
181
209
ZTEST (i2c_ram , test_ram_rtio )
182
210
{
183
211
struct rtio_sqe * wr_sqe , * rd_sqe ;
@@ -190,8 +218,7 @@ ZTEST(i2c_ram, test_ram_rtio)
190
218
zassert_ok (rtio_submit (& i2c_rtio , 1 ), "submit should succeed" );
191
219
192
220
wr_cqe = rtio_cqe_consume (& i2c_rtio );
193
- zassert_ok (wr_cqe -> result , "i2c write should succeed" );
194
- rtio_cqe_release (& i2c_rtio , wr_cqe );
221
+ check_completion (wr_cqe , "i2c write should succeed" );
195
222
196
223
/* Write the address and read the data back */
197
224
msgs [0 ].len = ARRAY_SIZE (rx_cmd );
@@ -209,11 +236,9 @@ ZTEST(i2c_ram, test_ram_rtio)
209
236
zassert_ok (rtio_submit (& i2c_rtio , 2 ), "submit should succeed" );
210
237
211
238
wr_cqe = rtio_cqe_consume (& i2c_rtio );
239
+ check_completion (wr_cqe , "i2c write should succeed" );
212
240
rd_cqe = rtio_cqe_consume (& i2c_rtio );
213
- zassert_ok (wr_cqe -> result , "i2c write should succeed" );
214
- zassert_ok (rd_cqe -> result , "i2c read should succeed" );
215
- rtio_cqe_release (& i2c_rtio , wr_cqe );
216
- rtio_cqe_release (& i2c_rtio , rd_cqe );
241
+ check_completion (rd_cqe , "i2c read should succeed" );
217
242
218
243
zassert_equal (memcmp (& tx_data [TX_DATA_OFFSET ], & rx_data [0 ], ARRAY_SIZE (rx_data )), 0 ,
219
244
"Written and Read data should match" );
@@ -242,11 +267,9 @@ ZTEST(i2c_ram, test_ram_rtio_write_with_transaction)
242
267
zassert_ok (rtio_submit (& i2c_rtio , 2 ), "submit should succeed" );
243
268
244
269
wr_cqe = rtio_cqe_consume (& i2c_rtio );
245
- zassert_ok (wr_cqe -> result , "i2c write should succeed" );
246
- rtio_cqe_release (& i2c_rtio , wr_cqe );
270
+ check_completion (wr_cqe , "i2c write should succeed" );
247
271
wr_cqe = rtio_cqe_consume (& i2c_rtio );
248
- zassert_ok (wr_cqe -> result , "i2c write should succeed" );
249
- rtio_cqe_release (& i2c_rtio , wr_cqe );
272
+ check_completion (wr_cqe , "i2c write should succeed" );
250
273
251
274
/** Now read the register address to confirm the write was performed */
252
275
wr_sqe = rtio_sqe_acquire (& i2c_rtio );
@@ -258,11 +281,9 @@ ZTEST(i2c_ram, test_ram_rtio_write_with_transaction)
258
281
zassert_ok (rtio_submit (& i2c_rtio , 2 ), "submit should succeed" );
259
282
260
283
wr_cqe = rtio_cqe_consume (& i2c_rtio );
284
+ check_completion (wr_cqe , "i2c write should succeed" );
261
285
rd_cqe = rtio_cqe_consume (& i2c_rtio );
262
- zassert_ok (wr_cqe -> result , "i2c write should succeed" );
263
- zassert_ok (rd_cqe -> result , "i2c read should succeed" );
264
- rtio_cqe_release (& i2c_rtio , wr_cqe );
265
- rtio_cqe_release (& i2c_rtio , rd_cqe );
286
+ check_completion (rd_cqe , "i2c read should succeed" );
266
287
267
288
zassert_equal (memcmp (& reg_data [0 ], & rx_data [0 ], ARRAY_SIZE (reg_data )), 0 ,
268
289
"Written and Read data should match" );
@@ -296,8 +317,7 @@ void ram_rtio_isr(struct k_timer *tid)
296
317
wr_cqe = rtio_cqe_consume (& i2c_rtio );
297
318
if (wr_cqe ) {
298
319
TC_PRINT ("timer checking write result, submitting read\n" );
299
- zassert_ok (wr_cqe -> result , "i2c write should succeed" );
300
- rtio_cqe_release (& i2c_rtio , wr_cqe );
320
+ check_completion (wr_cqe , "i2c write should succeed" );
301
321
302
322
/* Write the address and read the data back */
303
323
msgs [0 ].len = ARRAY_SIZE (rx_cmd );
@@ -322,17 +342,15 @@ void ram_rtio_isr(struct k_timer *tid)
322
342
wr_cqe = rtio_cqe_consume (& i2c_rtio );
323
343
if (wr_cqe ) {
324
344
TC_PRINT ("read command complete\n" );
325
- zassert_ok (wr_cqe -> result , "i2c read command should succeed" );
326
- rtio_cqe_release (& i2c_rtio , wr_cqe );
345
+ check_completion (wr_cqe , "i2c read command should succeed" );
327
346
isr_state += 1 ;
328
347
}
329
348
break ;
330
349
case READ_DATA_WAIT :
331
350
rd_cqe = rtio_cqe_consume (& i2c_rtio );
332
351
if (rd_cqe ) {
333
352
TC_PRINT ("read data complete\n" );
334
- zassert_ok (rd_cqe -> result , "i2c read data should succeed" );
335
- rtio_cqe_release (& i2c_rtio , rd_cqe );
353
+ check_completion (rd_cqe , "i2c read data should succeed" );
336
354
isr_state += 1 ;
337
355
k_sem_give (& ram_rtio_isr_sem );
338
356
k_timer_stop (tid );
0 commit comments