第一部分
int _I2CWrite(VL53L1_DEV Dev, uint8_t pdata, uint32_t count) {
int status;
int i2c_time_out = I2C_TIME_OUT_BASE+ count I2C_TIME_OUT_BYTE;
status = HAL_I2C_Master_Transmit(Dev->I2cHandle, Dev->I2cDevAddr, pdata, count, i2c_time_out);
if (status) {//VL6180x_ErrLog("I2C error 0x%x %d len", dev->I2cAddr, len);//XNUCLEO6180XA1_I2C1_Init(&hi2c1);
}
return status;
}
int _I2CRead(VL53L1_DEV Dev, uint8_t pdata, uint32_t count) {
int status;
int i2c_time_out = I2C_TIME_OUT_BASE+ count I2C_TIME_OUT_BYTE;
status = HAL_I2C_Master_Receive(Dev->I2cHandle, Dev->I2cDevAddr|1, pdata, count, i2c_time_out);
if (status) {//VL6180x_ErrLog("I2C error 0x%x %d len", dev->I2cAddr, len);//XNUCLEO6180XA1_I2C1_Init(&hi2c1);
}
return status;
}
其中HAL_I2C_Master_Transmit的定义为:
HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
{
uint32_t tickstart = 0x00U;
/* Init tickstart for timeout management*/
tickstart = HAL_GetTick();
if(hi2c->State == HAL_I2C_STATE_READY)
{
/* Wait until BUSY flag is reset */
if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
{
return HAL_BUSY;
}
/* Process Locked */
__HAL_LOCK(hi2c);/* Check if the I2C is already enabled */
if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
{/* Enable I2C peripheral */__HAL_I2C_ENABLE(hi2c);
}/* Disable Pos */
hi2c->Instance->CR1 &= ~I2C_CR1_POS;hi2c->State = HAL_I2C_STATE_BUSY_TX;
hi2c->Mode = HAL_I2C_MODE_MASTER;
hi2c->ErrorCode = HAL_I2C_ERROR_NONE;/* Prepare transfer parameters */
hi2c->pBuffPtr = pData;
hi2c->XferCount = Size;
hi2c->XferOptions = I2C_NO_OPTION_FRAME;
hi2c->XferSize = hi2c->XferCount;/* Send Slave Address */
if(I2C_MasterRequestWrite(hi2c, DevAddress, Timeout, tickstart) != HAL_OK)
{if(hi2c->ErrorCode == HAL_I2C_ERROR_AF){/* Process Unlocked */__HAL_UNLOCK(hi2c);return HAL_ERROR;}else{/* Process Unlocked */__HAL_UNLOCK(hi2c);return HAL_TIMEOUT;}
}/* Clear ADDR flag */
__HAL_I2C_CLEAR_ADDRFLAG(hi2c);while(hi2c->XferSize > 0U)
{/* Wait until TXE flag is set */if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK){if(hi2c->ErrorCode == HAL_I2C_ERROR_AF){/* Generate Stop */hi2c->Instance->CR1 |= I2C_CR1_STOP;return HAL_ERROR;}else{return HAL_TIMEOUT;}}/* Write data to DR */hi2c->Instance->DR = (*hi2c->pBuffPtr++);hi2c->XferCount--;hi2c->XferSize--;if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U)){/* Write data to DR */hi2c->Instance->DR = (*hi2c->pBuffPtr++);hi2c->XferCount--;hi2c->XferSize--;}/* Wait until BTF flag is set */if(I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK){if(hi2c->ErrorCode == HAL_I2C_ERROR_AF){/* Generate Stop */hi2c->Instance->CR1 |= I2C_CR1_STOP;return HAL_ERROR;}else{return HAL_TIMEOUT;}}
}/* Generate Stop */
hi2c->Instance->CR1 |= I2C_CR1_STOP;hi2c->State = HAL_I2C_STATE_READY;
hi2c->Mode = HAL_I2C_MODE_NONE;/* Process Unlocked */
__HAL_UNLOCK(hi2c);return HAL_OK;
}
else
{
return HAL_BUSY;
}
}
如何将其用
typedef struct
{
uint32 scl_pin; // 用于记录对应的引脚编号
uint32 sda_pin; // 用于记录对应的引脚编号
uint8 addr; // 器件地址 七位地址模式
uint32 delay; // 模拟 IIC 软延时时长 0:1370KHz 10:1020KHz 20:757KHz 30: 633KHz 40: 532Khz 50: 448KHz 60: 395KHz 70: 359KHz 80: 324KHz 100: 268KHz 1000:32KHz
void *iic_scl; // 记录 SCL 端口地址
void *iic_sda; // 记录 SDA 端口地址
}soft_iic_info_struct;
static uint8 soft_iic_send_data (soft_iic_info_struct *soft_iic_obj, const uint8 data)
{
uint8 temp = 0x80;
while(temp)
{
gpio_set_level(soft_iic_obj->sda_pin, data & temp);
temp >>= 1;
soft_iic_delay(soft_iic_obj->delay);soft_iic_gpio_high_scl(); // SCL 拉高soft_iic_delay(soft_iic_obj->delay);soft_iic_gpio_low_scl(); // SCL 拉低
}
return ((soft_iic_wait_ack(soft_iic_obj) == 1) ? 0 : 1 );
}
替换掉?并能配合VL53L1_Error VL53L1_WrByte(VL53L1_DEV Dev, uint16_t index, uint8_t data) {
VL53L1_Error Status = VL53L1_ERROR_NONE;
int32_t status_int;
_I2CBuffer[0] = index>>8;_I2CBuffer[1] = index&0xFF;_I2CBuffer[2] = data;VL53L1_GetI2cBus();status_int = _I2CWrite(Dev, _I2CBuffer, 3);if (status_int != 0) {Status = VL53L1_ERROR_CONTROL_INTERFACE;}VL53L1_PutI2cBus();return Status;
}
VL53L1_Error VL53L1_WrWord(VL53L1_DEV Dev, uint16_t index, uint16_t data) {
VL53L1_Error Status = VL53L1_ERROR_NONE;
int32_t status_int;
_I2CBuffer[0] = index>>8;_I2CBuffer[1] = index&0xFF;_I2CBuffer[2] = data >> 8;_I2CBuffer[3] = data & 0x00FF;VL53L1_GetI2cBus();status_int = _I2CWrite(Dev, _I2CBuffer, 4);if (status_int != 0) {Status = VL53L1_ERROR_CONTROL_INTERFACE;}VL53L1_PutI2cBus();return Status;
}
这几个函数进行使用?