bool i2cRead(uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t* buf) { uint32_t timeout = I2C_DEFAULT_TIMEOUT; addr = addr_ << 1; reg = reg_; writing = 0; reading = 1; read_p = buf; write_p = buf; bytes = len; busy = 1; error = false; if (!I2Cx) return false; if (!(I2Cx->CR2 & I2C_IT_EVT)) { // if we are restarting the driver if (!(I2Cx->CR1 & 0x0100)) { // ensure sending a start while (I2Cx->CR1 & 0x0200 && --timeout > 0) { ; } // wait for any stop to finish sending if (timeout == 0) return i2cHandleHardwareFailure(); I2C_GenerateSTART(I2Cx, ENABLE); // send the start for the new job } I2C_ITConfig(I2Cx, I2C_IT_EVT | I2C_IT_ERR, ENABLE); // allow the interrupts to fire off again } timeout = I2C_DEFAULT_TIMEOUT; while (busy && --timeout > 0) { ; } if (timeout == 0) return i2cHandleHardwareFailure(); return !error; }
bool i2cWriteBuffer(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len_, uint8_t *data) { if (device == I2CINVALID) return false; uint32_t timeout = I2C_DEFAULT_TIMEOUT; I2C_TypeDef *I2Cx; I2Cx = i2cHardwareMap[device].dev; i2cState_t *state; state = &(i2cState[device]); state->addr = addr_ << 1; state->reg = reg_; state->writing = 1; state->reading = 0; state->write_p = data; state->read_p = data; state->bytes = len_; state->busy = 1; state->error = false; if (!(I2Cx->CR2 & I2C_IT_EVT)) { // if we are restarting the driver if (!(I2Cx->CR1 & I2C_CR1_START)) { // ensure sending a start while (I2Cx->CR1 & I2C_CR1_STOP && --timeout > 0) {; } // wait for any stop to finish sending if (timeout == 0) return i2cHandleHardwareFailure(device); I2C_GenerateSTART(I2Cx, ENABLE); // send the start for the new job } I2C_ITConfig(I2Cx, I2C_IT_EVT | I2C_IT_ERR, ENABLE); // allow the interrupts to fire off again } timeout = I2C_DEFAULT_TIMEOUT; while (state->busy && --timeout > 0) {; } if (timeout == 0) return i2cHandleHardwareFailure(device); return !(state->error); }
bool i2cRead(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t* buf) { HAL_StatusTypeDef status; if(reg_ == 0xFF) status = HAL_I2C_Master_Receive(&i2cHandle[device].Handle,addr_ << 1,buf, len, I2C_DEFAULT_TIMEOUT); else status = HAL_I2C_Mem_Read(&i2cHandle[device].Handle,addr_ << 1, reg_, I2C_MEMADD_SIZE_8BIT,buf, len, I2C_DEFAULT_TIMEOUT); if(status != HAL_OK) return i2cHandleHardwareFailure(device); return true; }
bool i2cWriteBuffer(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len_, uint8_t *data) { HAL_StatusTypeDef status; if(reg_ == 0xFF) status = HAL_I2C_Master_Transmit(&i2cHandle[device].Handle,addr_ << 1,data, len_, I2C_DEFAULT_TIMEOUT); else status = HAL_I2C_Mem_Write(&i2cHandle[device].Handle,addr_ << 1, reg_, I2C_MEMADD_SIZE_8BIT,data, len_, I2C_DEFAULT_TIMEOUT); if(status != HAL_OK) return i2cHandleHardwareFailure(device); return true; }
bool i2cWriteBuffer(uint8_t addr_, uint8_t reg_, uint8_t len_, uint8_t *data, I2CDevice bus) { I2C_TypeDef *I2Cx = i2cHardwareMap[bus].dev; uint32_t timeout = I2C_DEFAULT_TIMEOUT; addr = addr_ << 1; reg = reg_; writing = 1; reading = 0; write_p = data; read_p = data; bytes = len_; busy = 1; error = false; if (!I2Cx) return false; if (!(I2Cx->CR2 & I2C_IT_EVT)) { // if we are restarting the driver if (!(I2Cx->CR1 & 0x0100)) { // ensure sending a start while (I2Cx->CR1 & 0x0200 && --timeout > 0) { ; } // wait for any stop to finish sending if (timeout == 0) { return i2cHandleHardwareFailure(bus); } I2C_GenerateSTART(I2Cx, ENABLE); // send the start for the new job } I2C_ITConfig(I2Cx, I2C_IT_EVT | I2C_IT_ERR, ENABLE); // allow the interrupts to fire off again } timeout = I2C_DEFAULT_TIMEOUT; while (busy && --timeout > 0) { ; } if (timeout == 0) { return i2cHandleHardwareFailure(bus); } return !error; }
bool i2cRead(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t* buf) { if (device == I2CINVALID || device > I2CDEV_COUNT) { return false; } I2C_HandleTypeDef *pHandle = &i2cDevice[device].handle; if (!pHandle->Instance) { return false; } HAL_StatusTypeDef status; if (reg_ == 0xFF) status = HAL_I2C_Master_Receive(pHandle ,addr_ << 1, buf, len, I2C_DEFAULT_TIMEOUT); else status = HAL_I2C_Mem_Read(pHandle, addr_ << 1, reg_, I2C_MEMADD_SIZE_8BIT,buf, len, I2C_DEFAULT_TIMEOUT); if (status != HAL_OK) return i2cHandleHardwareFailure(device); return true; }