From 6ef8a7361454cc8822dc09d1500546f994c7b0ab Mon Sep 17 00:00:00 2001 From: adriendesp Date: Thu, 1 Aug 2024 15:25:58 +0200 Subject: [PATCH] arch/xmc4 : fixed critical section in i2c_transfer The critical section was declared at the wrong place. The critical section wasn't left if error returned. --- arch/arm/src/xmc4/xmc4_i2c.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/arch/arm/src/xmc4/xmc4_i2c.c b/arch/arm/src/xmc4/xmc4_i2c.c index a7cd996afcb..608b7585777 100644 --- a/arch/arm/src/xmc4/xmc4_i2c.c +++ b/arch/arm/src/xmc4/xmc4_i2c.c @@ -813,6 +813,10 @@ static int i2c_transfer(struct i2c_master_s *dev, nxmutex_lock(&priv->lock); + /* Enter critical section to avoid interrupts during i2c transfert */ + + irqstate_t state = enter_critical_section(); + for (int i = 0; i < count; i++) { /* Check if frequency must be changed */ @@ -832,6 +836,7 @@ static int i2c_transfer(struct i2c_master_s *dev, else { i2cerr("Can't update frequency between Start & Stop symbols\n"); + leave_critical_section(state); nxmutex_unlock(&priv->lock); return -EINVAL; } @@ -841,10 +846,6 @@ static int i2c_transfer(struct i2c_master_s *dev, bool read = ((msgs[i].flags & I2C_M_READ) != 0); - /* Enter critical section to avoid interrupts during i2c transfert */ - - irqstate_t state = enter_critical_section(); - /* Check if you should start or restart a new i2c frame */ if ((msgs[i].flags & I2C_M_NOSTART) == 0)