bool bSMBusRxCpltFlag = RESET; bool bSMBusTxCpltFlag = RESET; HAL_StatusTypeDef xBatteryMessage( enum BATCMD ucCommand, SMBusCommunicationType_t xDirection, uint8_t *ucReceiveBuffer, uint8_t dataLength ) { bSMBusRxCpltFlag = RESET; bSMBusTxCpltFlag = RESET; uint16_t usTimeCon = 0; uint8_t ucCalTemp[ 32 ] = { 0x16, 0x00, 0x17 }; ucCalTemp[1] = ucCommand; if ( xDirection == read ) { // Read Word Protocol with PEC // CALL SLAVE DEVICE HAL_SMBUS_Master_Transmit_IT( &hsmbus1, BATTERY_ADD, &ucCalTemp[ 1 ], 1, SMBUS_FIRST_FRAME ); for ( usTimeCon = 0; bSMBusTxCpltFlag == RESET; usTimeCon++ ) { if ( usTimeCon >= 0xFF00 ) return HAL_ERROR; } bSMBusTxCpltFlag = RESET; HAL_Delay(1); dataL = dataLength + 1; HAL_SMBUS_Master_Receive_IT( &hsmbus1, BATTERY_ADD, &ucCalTemp[ 3 ], dataLength + 1 , SMBUS_LAST_FRAME_NO_PEC ); for ( usTimeCon = 0; bSMBusRxCpltFlag == RESET; usTimeCon++ ) { if ( usTimeCon >= 0xFF00 ) return HAL_ERROR; } bSMBusRxCpltFlag = RESET; //HAL_Delay(5); //IF I don't wait in here, the data I can not use immediately. if (xPecCheck( ucCalTemp, ucCalTemp[dataLength + 3], dataLength + 3 ) == HAL_OK) { for(uint8_t i = 0; i < dataLength ; i++) { ucReceiveBuffer[i] = ucCalTemp[dataLength+2-i]; } return HAL_OK; } else return HAL_ERROR; } else if ( xDirection == write ) { //todo: need to add more function about it. } return HAL_ERROR; } void HAL_SMBUS_MasterTxCpltCallback( SMBUS_HandleTypeDef *hsmbus ) { if(hsmbus == &hsmbus1) { bSMBusTxCpltFlag = SET; } } // Data dont't save into RAM, but the flag had been changed. void HAL_SMBUS_MasterRxCpltCallback( SMBUS_HandleTypeDef *hsmbus ) { if(hsmbus == &hsmbus1) { bSMBusRxCpltFlag = SET; } }