From 852578d91e59289655189416da757c76ecbba935 Mon Sep 17 00:00:00 2001 From: haizhouyang Date: Thu, 6 May 2021 20:53:43 +0800 Subject: [PATCH] using mutex as default lock of i2c controller --- support/platform/include/i2c_core.h | 2 -- support/platform/src/i2c_core.c | 28 ++++++++++------------------ 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/support/platform/include/i2c_core.h b/support/platform/include/i2c_core.h index 81208c183..0e5d513ac 100644 --- a/support/platform/include/i2c_core.h +++ b/support/platform/include/i2c_core.h @@ -11,7 +11,6 @@ #include "hdf_base.h" #include "i2c_if.h" -#include "osal_atomic.h" #include "osal_mutex.h" #ifdef __cplusplus @@ -28,7 +27,6 @@ struct I2cLockMethod; struct I2cCntlr { struct OsalMutex lock; - OsalAtomic atom; void *owner; int16_t busId; void *priv; diff --git a/support/platform/src/i2c_core.c b/support/platform/src/i2c_core.c index 0ee4f3bd6..815e43c4d 100644 --- a/support/platform/src/i2c_core.c +++ b/support/platform/src/i2c_core.c @@ -32,20 +32,7 @@ static int32_t I2cCntlrLockDefault(struct I2cCntlr *cntlr) if (cntlr == NULL) { return HDF_ERR_INVALID_OBJECT; } - -__TRY_LOCK: - if (OsalAtomicRead(&cntlr->atom) >= 1) { - OsalAtomicDec(&cntlr->atom); - return HDF_SUCCESS; - } - - if (PlatInIrqContext()) { - OsalMDelay(LOCK_WAIT_SECONDS_M); - } else { - OsalMSleep(LOCK_WAIT_SECONDS_M); - } - - goto __TRY_LOCK; + return OsalMutexLock(&cntlr->lock); } static void I2cCntlrUnlockDefault(struct I2cCntlr *cntlr) @@ -53,8 +40,7 @@ static void I2cCntlrUnlockDefault(struct I2cCntlr *cntlr) if (cntlr == NULL) { return; } - - OsalAtomicInc(&cntlr->atom); + (void)OsalMutexUnlock(&cntlr->lock); } static const struct I2cLockMethod g_i2cLockOpsDefault = { @@ -165,6 +151,8 @@ void I2cCntlrPut(struct I2cCntlr *cntlr) int32_t I2cCntlrAdd(struct I2cCntlr *cntlr) { + int32_t ret; + if (cntlr == NULL) { return HDF_ERR_INVALID_OBJECT; } @@ -183,9 +171,13 @@ int32_t I2cCntlrAdd(struct I2cCntlr *cntlr) HDF_LOGE("I2cCntlrAdd: init lock fail!"); return HDF_FAILURE; } - OsalAtomicSet(&cntlr->atom, 1); - return I2cManagerAddCntlr(cntlr); + ret = I2cManagerAddCntlr(cntlr); + if (ret != HDF_SUCCESS) { + (void)OsalMutexDestroy(&cntlr->lock); + return ret; + } + return HDF_SUCCESS; } void I2cCntlrRemove(struct I2cCntlr *cntlr) -- Gitee