status_t I2C_MasterTransferBlocking(I2C_Type *base, i2c_master_transfer_t *xfer) { status_t result = kStatus_Success; uint32_t subaddress; uint8_t subaddrBuf[4]; int i; assert(xfer); /* If repeated start is requested, send repeated start. */ if (!(xfer->flags & kI2C_TransferNoStartFlag)) { if (xfer->subaddressSize) { result = I2C_MasterStart(base, xfer->slaveAddress, kI2C_Write); if (result == kStatus_Success) { /* Prepare subaddress transmit buffer, most significant byte is stored at the lowest address */ subaddress = xfer->subaddress; for (i = xfer->subaddressSize - 1; i >= 0; i--) { subaddrBuf[i] = subaddress & 0xff; subaddress >>= 8; } /* Send subaddress. */ result = I2C_MasterWriteBlocking(base, subaddrBuf, xfer->subaddressSize, kI2C_TransferNoStopFlag); if ((result == kStatus_Success) && (xfer->direction == kI2C_Read)) { result = I2C_MasterRepeatedStart(base, xfer->slaveAddress, xfer->direction); } } }
int i2c_byte_write(i2c_t *obj, int data) { if (I2C_MasterWriteBlocking(i2c_addrs[obj->instance], (uint8_t *)(&data), 1) == kStatus_Success) { return 1; } return 0; }