HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c,uint16_tDevAddress,uint8_t*pData,
uint16_tSize,uint32_tTimeout);
HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c,uint16_tDevAddress,uint8_t*pData,
uint16_tSize,uint32_tTimeout);
HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c,uint16_tDevAddress,uint16_tMemAddress,
uint16_tMemAddSize,uint8_t*pData,uint16_tSize,uint32_tTimeout);
HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c,uint16_tDevAddress,uint16_tMemAddress,
uint16_tMemAddSize,uint8_t*pData,uint16_tSize,uint32_tTimeout);
static int i2cMasterReadDevice(inti2cNum,uint8_tdevAddr,uint8_t*data,intdataSize) {
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) != RESET); // 等待I2C空闲状态
I2C_GenerateSTART(I2C1, ENABLE); // 发送START信号
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); // 等待I2C进入主模式
I2C_Send7bitAddress(I2C1, devAddr, I2C_Direction_Receiver); // 系统控制读写位
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); // 等待进入I2C接收模式
I2C_AcknowledgeConfig(I2C1, ENABLE);
for (int i = 0; i < dataSize - 1; i++) {
*data = I2C_ReceiveData(I2C1);
data++;
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == SET);
}
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == RESET) {
I2C_AcknowledgeConfig(I2C1, DISABLE);
}
*data = I2C_ReceiveData(I2C1);
I2C_GenerateSTOP(I2C1, ENABLE);
return 0;
}
static int i2cMasterWriteDevice(inti2cNum,uint8_tdevAddr,uint8_t*data,intdataSize) {
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) != RESET);
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, devAddr, I2C_Direction_Transmitter); // 系统控制读写位
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
for (int i = 0; i < dataSize - 1; i++) {
I2C_SendData(I2C1, *data);
data++;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
if (I2C_GetFlagStatus(I2C1, I2C_FLAG_TXE) != RESET) {
I2C_SendData(I2C1, data);
}
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1, ENABLE);
return 0;
}
static int i2cMasterReadMemory(inti2cNum,uint8_tdevAddr,uint16_t*wRegData,intwRegDataSize,uint8_t*rData,intrDataSize) {
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) != RESET);
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, devAddr, I2C_Direction_Transmitter); // 系统控制读写位
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
for (int i = 0; i < wRegDataSize; i++) {
I2C_SendData(I2C1, *wRegData);
wRegData++;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, 0XA0, I2C_Direction_Receiver);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
for (int i = 0; i < rDataSize - 1; i++) {
*rData = I2C_ReceiveData(I2C1);
rData++;
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == SET);
}
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == RESET) {
I2C_AcknowledgeConfig(I2C1, DISABLE);
}
*rData = I2C_ReceiveData(I2C1);
I2C_GenerateSTOP(I2C1, ENABLE);
return 0;
}
static int i2cMasterWriteMemory(inti2cNum,uint8_tdevAddr,uint16_t*wRegData,intwRegDataSize,uint8_t*wData,intwDataSize) {
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) != RESET);
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, devAddr, I2C_Direction_Transmitter); // 系统控制读写位
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
for (int i = 0; i < wRegDataSize; i++) {
I2C_SendData(I2C1, *wRegData);
wRegData++;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
for (int i = 0; i < wDataSize - 1; i++) {
I2C_SendData(I2C1, *wData);
wData++;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
if (I2C_GetFlagStatus(I2C1, I2C_FLAG_TXE) != RESET) {
I2C_SendData(I2C1, wData);
}
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1, ENABLE);
return 0;
}