@@ -38,6 +38,9 @@ struct i2c_smartbond_data {
38
38
#if defined(CONFIG_PM_DEVICE )
39
39
ATOMIC_DEFINE (pm_policy_state_flag , 1 );
40
40
#endif
41
+ #ifdef CONFIG_I2C_CALLBACK
42
+ k_spinlock_key_t spinlock_key ;
43
+ #endif
41
44
};
42
45
43
46
#if defined(CONFIG_PM_DEVICE )
@@ -111,7 +114,7 @@ static int i2c_smartbond_apply_configure(const struct device *dev, uint32_t dev_
111
114
const struct i2c_smartbond_cfg * config = dev -> config ;
112
115
struct i2c_smartbond_data * data = dev -> data ;
113
116
uint32_t con_reg = 0x0UL ;
114
- k_spinlock_key_t key ;
117
+ k_spinlock_key_t key = k_spin_lock ( & data -> lock ) ;
115
118
116
119
/* Configure Speed (SCL frequency) */
117
120
switch (I2C_SPEED_GET (dev_config )) {
@@ -144,8 +147,6 @@ static int i2c_smartbond_apply_configure(const struct device *dev, uint32_t dev_
144
147
/* Enable sending RESTART as master */
145
148
con_reg |= I2C_I2C_CON_REG_I2C_RESTART_EN_Msk ;
146
149
147
- key = k_spin_lock (& data -> lock );
148
-
149
150
i2c_smartbond_disable_when_inactive (dev );
150
151
151
152
/* Write control register*/
@@ -221,8 +222,6 @@ static inline void i2c_smartbond_set_target_address(const struct i2c_smartbond_c
221
222
struct i2c_smartbond_data * data ,
222
223
const struct i2c_msg * const msg , uint16_t addr )
223
224
{
224
- k_spinlock_key_t key = k_spin_lock (& data -> lock );
225
-
226
225
/* Disable I2C Controller */
227
226
config -> regs -> I2C_ENABLE_REG &= ~I2C_I2C_ENABLE_REG_I2C_EN_Msk ;
228
227
/* Configure addressing mode*/
@@ -237,8 +236,6 @@ static inline void i2c_smartbond_set_target_address(const struct i2c_smartbond_c
237
236
(addr & I2C_I2C_TAR_REG_IC_TAR_Msk ));
238
237
/* Enable again the I2C to use the new address */
239
238
config -> regs -> I2C_ENABLE_REG |= I2C_I2C_ENABLE_REG_I2C_EN_Msk ;
240
-
241
- k_spin_unlock (& data -> lock , key );
242
239
}
243
240
244
241
static inline int i2c_smartbond_set_msg_flags (struct i2c_msg * msgs , uint8_t num_msgs )
@@ -289,16 +286,13 @@ static inline int i2c_smartbond_prep_transfer(const struct device *dev, struct i
289
286
static inline int i2c_smartbond_tx (const struct i2c_smartbond_cfg * const config ,
290
287
struct i2c_smartbond_data * data )
291
288
{
292
- k_spinlock_key_t key ;
293
289
const bool rw = ((data -> msgs -> flags & I2C_MSG_RW_MASK ) == I2C_MSG_READ );
294
290
int ret = 0 ;
295
291
296
292
if (!data -> msgs -> buf || data -> msgs -> len == 0 ) {
297
293
return - EINVAL ;
298
294
}
299
295
300
- key = k_spin_lock (& data -> lock );
301
-
302
296
/* Transmits data or read commands with correct flags */
303
297
while ((data -> transmit_cnt < data -> msgs -> len ) &&
304
298
(config -> regs -> I2C_STATUS_REG & I2C_I2C_STATUS_REG_TFNF_Msk )) {
@@ -329,23 +323,18 @@ static inline int i2c_smartbond_tx(const struct i2c_smartbond_cfg *const config,
329
323
(void )config -> regs -> I2C_CLR_TX_ABRT_REG ;
330
324
}
331
325
332
- k_spin_unlock (& data -> lock , key );
333
-
334
326
return ret ;
335
327
}
336
328
337
329
static inline int i2c_smartbond_rx (const struct i2c_smartbond_cfg * const config ,
338
330
struct i2c_smartbond_data * data )
339
331
{
340
- k_spinlock_key_t key ;
341
332
int ret = 0 ;
342
333
343
334
if (!data -> msgs -> buf || data -> msgs -> len == 0 ) {
344
335
return - EINVAL ;
345
336
}
346
337
347
- key = k_spin_lock (& data -> lock );
348
-
349
338
/* Reads the data register until fifo is empty */
350
339
while ((data -> receive_cnt < data -> transmit_cnt ) &&
351
340
(config -> regs -> I2C_STATUS_REG & I2C2_I2C2_STATUS_REG_RFNE_Msk )) {
@@ -354,8 +343,6 @@ static inline int i2c_smartbond_rx(const struct i2c_smartbond_cfg *const config,
354
343
data -> receive_cnt ++ ;
355
344
}
356
345
357
- k_spin_unlock (& data -> lock , key );
358
-
359
346
return ret ;
360
347
}
361
348
@@ -365,6 +352,7 @@ static int i2c_smartbond_transfer(const struct device *dev, struct i2c_msg *msgs
365
352
const struct i2c_smartbond_cfg * config = dev -> config ;
366
353
struct i2c_smartbond_data * data = dev -> data ;
367
354
int ret = 0 ;
355
+ k_spinlock_key_t key = k_spin_lock (& data -> lock );
368
356
369
357
i2c_smartbond_pm_policy_state_lock_get (dev );
370
358
@@ -405,6 +393,7 @@ static int i2c_smartbond_transfer(const struct device *dev, struct i2c_msg *msgs
405
393
while (!i2c_smartbond_is_idle (dev )) {
406
394
};
407
395
i2c_smartbond_pm_policy_state_lock_put (dev );
396
+ k_spin_unlock (& data -> lock , key );
408
397
409
398
return ret ;
410
399
}
@@ -414,8 +403,6 @@ static int i2c_smartbond_transfer(const struct device *dev, struct i2c_msg *msgs
414
403
static int i2c_smartbond_enable_msg_interrupts (const struct i2c_smartbond_cfg * const config ,
415
404
struct i2c_smartbond_data * data )
416
405
{
417
- k_spinlock_key_t key = k_spin_lock (& data -> lock );
418
-
419
406
if ((data -> msgs -> flags & I2C_MSG_RW_MASK ) == I2C_MSG_READ ) {
420
407
uint32_t remaining = data -> msgs -> len - data -> receive_cnt ;
421
408
uint32_t tx_space = 32 - config -> regs -> I2C_TXFLR_REG ;
@@ -430,8 +417,6 @@ static int i2c_smartbond_enable_msg_interrupts(const struct i2c_smartbond_cfg *c
430
417
config -> regs -> I2C_TX_TL_REG = 0UL ;
431
418
config -> regs -> I2C_INTR_MASK_REG |= I2C_I2C_INTR_MASK_REG_M_TX_EMPTY_Msk ;
432
419
433
- k_spin_unlock (& data -> lock , key );
434
-
435
420
return 0 ;
436
421
}
437
422
@@ -442,6 +427,7 @@ static int i2c_smartbond_transfer_cb(const struct device *dev, struct i2c_msg *m
442
427
const struct i2c_smartbond_cfg * config = dev -> config ;
443
428
struct i2c_smartbond_data * data = dev -> data ;
444
429
int ret = 0 ;
430
+ k_spinlock_key_t key = k_spin_lock (& data -> lock );
445
431
446
432
if (cb == NULL ) {
447
433
return - EINVAL ;
@@ -451,6 +437,7 @@ static int i2c_smartbond_transfer_cb(const struct device *dev, struct i2c_msg *m
451
437
return - EWOULDBLOCK ;
452
438
}
453
439
440
+ data -> spinlock_key = key ;
454
441
data -> cb = cb ;
455
442
data -> userdata = userdata ;
456
443
@@ -459,6 +446,7 @@ static int i2c_smartbond_transfer_cb(const struct device *dev, struct i2c_msg *m
459
446
ret = i2c_smartbond_prep_transfer (dev , msgs , num_msgs , addr );
460
447
if (ret != 0 ) {
461
448
i2c_smartbond_pm_policy_state_lock_put (dev );
449
+ k_spin_unlock (& data -> lock , key );
462
450
return ret ;
463
451
}
464
452
@@ -471,9 +459,7 @@ static int i2c_smartbond_transfer_cb(const struct device *dev, struct i2c_msg *m
471
459
472
460
static inline void isr_tx (const struct i2c_smartbond_cfg * config , struct i2c_smartbond_data * data )
473
461
{
474
-
475
462
const bool rw = ((data -> msgs -> flags & I2C_MSG_RW_MASK ) == I2C_MSG_READ );
476
- k_spinlock_key_t key = k_spin_lock (& data -> lock );
477
463
478
464
while ((data -> transmit_cnt < data -> msgs -> len ) &&
479
465
(config -> regs -> I2C_STATUS_REG & I2C_I2C_STATUS_REG_TFNF_Msk )) {
@@ -490,22 +476,16 @@ static inline void isr_tx(const struct i2c_smartbond_cfg *config, struct i2c_sma
490
476
: 0 );
491
477
data -> transmit_cnt ++ ;
492
478
}
493
-
494
- k_spin_unlock (& data -> lock , key );
495
479
}
496
480
497
481
static inline void isr_rx (const struct i2c_smartbond_cfg * config , struct i2c_smartbond_data * data )
498
482
{
499
- k_spinlock_key_t key = k_spin_lock (& data -> lock );
500
-
501
483
while ((data -> receive_cnt < data -> transmit_cnt ) &&
502
484
(config -> regs -> I2C_STATUS_REG & I2C2_I2C2_STATUS_REG_RFNE_Msk )) {
503
485
data -> msgs -> buf [data -> receive_cnt ] =
504
486
config -> regs -> I2C_DATA_CMD_REG & I2C_I2C_DATA_CMD_REG_I2C_DAT_Msk ;
505
487
data -> receive_cnt ++ ;
506
488
}
507
-
508
- k_spin_unlock (& data -> lock , key );
509
489
}
510
490
511
491
static inline void i2c_smartbond_async_msg_done (const struct device * dev )
@@ -526,7 +506,10 @@ static inline void i2c_smartbond_async_msg_done(const struct device *dev)
526
506
data -> cb = NULL ;
527
507
LOG_INF ("async transfer finished" );
528
508
cb (dev , 0 , data -> userdata );
509
+ while (!i2c_smartbond_is_idle (dev )) {
510
+ };
529
511
i2c_smartbond_pm_policy_state_lock_put (dev );
512
+ k_spin_unlock (& data -> lock , data -> spinlock_key );
530
513
}
531
514
}
532
515
0 commit comments