Skip to content

Commit

Permalink
Gen2 I2C: move identical code into a common function.
Browse files Browse the repository at this point in the history
  • Loading branch information
XuGuohui committed Sep 4, 2020
1 parent 45a1f91 commit 5abdd99
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions hal/src/stm32f2xx/i2c_hal.c
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5abdd99

Please sign in to comment.