diff --git a/hal/src/stm32f2xx/i2c_hal.c b/hal/src/stm32f2xx/i2c_hal.c index 614ee5b570..bc466a9ac8 100644 --- a/hal/src/stm32f2xx/i2c_hal.c +++ b/hal/src/stm32f2xx/i2c_hal.c @@ -218,6 +218,18 @@ static bool isBuffersInitialized(hal_i2c_interface_t i2c) { return true; } +static void onSlaveReceived(hal_i2c_interface_t i2c) { + if (i2cMap[i2c]->receiving) { + i2cMap[i2c]->receiving = 0; + i2cMap[i2c]->rxIndexTail = i2cMap[i2c]->rxIndexHead; + i2cMap[i2c]->rxIndexHead = 0; + if (NULL != i2cMap[i2c]->callback_onReceive) { + // alert user program + i2cMap[i2c]->callback_onReceive(i2cMap[i2c]->rxIndexTail); + } + } +} + int hal_i2c_init(hal_i2c_interface_t i2c, const hal_i2c_config_t* config) { #if PLATFORM_ID == PLATFORM_ELECTRON_PRODUCTION // Electron // For now we only enable this for PMIC I2C bus @@ -970,15 +982,7 @@ static void HAL_I2C_EV_InterruptHandler(hal_i2c_interface_t i2c) { i2cMap[i2c]->transmitting = 0; } - if (i2cMap[i2c]->receiving) { // Slave receiver - i2cMap[i2c]->receiving = 0; - i2cMap[i2c]->rxIndexTail = i2cMap[i2c]->rxIndexHead; - i2cMap[i2c]->rxIndexHead = 0; - if (NULL != i2cMap[i2c]->callback_onReceive) { - // alert user program - i2cMap[i2c]->callback_onReceive(i2cMap[i2c]->rxIndexTail); - } - } + onSlaveReceived(i2c); } uint32_t st = I2C_GetLastEvent(i2cMap[i2c]->peripheral); @@ -988,15 +992,7 @@ static void HAL_I2C_EV_InterruptHandler(hal_i2c_interface_t i2c) { /********** Slave Transmitter Events ************/ /* Check on EV1 */ case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED: { - if (i2cMap[i2c]->receiving) { - i2cMap[i2c]->receiving = 0; - i2cMap[i2c]->rxIndexTail = i2cMap[i2c]->rxIndexHead; - i2cMap[i2c]->rxIndexHead = 0; - if (NULL != i2cMap[i2c]->callback_onReceive) { - // alert user program - i2cMap[i2c]->callback_onReceive(i2cMap[i2c]->rxIndexTail); - } - } + onSlaveReceived(i2c); i2cMap[i2c]->transmitting = 1; i2cMap[i2c]->txIndexHead = 0; @@ -1028,16 +1024,8 @@ static void HAL_I2C_EV_InterruptHandler(hal_i2c_interface_t i2c) { /*********** Slave Receiver Events *************/ /* check on EV1*/ case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED: { - if (i2cMap[i2c]->receiving) { - i2cMap[i2c]->rxIndexTail = i2cMap[i2c]->rxIndexHead; - i2cMap[i2c]->rxIndexHead = 0; - if (NULL != i2cMap[i2c]->callback_onReceive) { - // WARN: User must read out all the received data immediately. - i2cMap[i2c]->callback_onReceive(i2cMap[i2c]->rxIndexTail); - } - } else { - i2cMap[i2c]->receiving = 1; - } + onSlaveReceived(i2c); + i2cMap[i2c]->receiving = 1; i2cMap[i2c]->rxIndexHead = 0; i2cMap[i2c]->rxIndexTail = 0; break;