CyU3PReturnStatus_t SensorRead2B2( uint8_t slaveAddr, uint8_t highAddr, uint8_t lowAddr, uint8_t RegAdd, uint8_t *buf) { CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS; CyU3PI2cPreamble_t preamble; if ((slaveAddr != SENSOR_ADDR_RD) && (slaveAddr != I2C_MEMORY_ADDR_RD)) { CyU3PDebugPrint(4, "I2C Slave address is not valid!\n"); return 1; } preamble.buffer[0] = slaveAddr & I2C_SLAVEADDR_MASK; /* Mask out the transfer type bit. */ preamble.buffer[1] = highAddr; //highAddr; preamble.buffer[2] = lowAddr; //lowAddr; preamble.length = 3; preamble.ctrlMask = 0x0000; /* Send start bit after third byte of preamble. */ buf[0] = RegAdd; #ifdef DbgInfo CyU3PDebugPrint(4, "sensor read2B(0) %d %d %d\r\n", lowAddr, buf[0], buf[1]); //additional debug #endif #if 0 apiRetStatus = CyU3PI2cTransmitBytes(&preamble, buf, 1, 0); //send command block to prepare for reading /*** test I2C bus ready ****/ if(apiRetStatus != CY_U3P_SUCCESS){ CyU3PDebugPrint(4, "sensor read2B(T) %d %d %d\r\n", apiRetStatus, buf[0], buf[1]); } #ifdef DbgInfo CyU3PDebugPrint(4, "sensor read2B(1) %d %d %d\r\n", lowAddr, buf[0], buf[1]); //additional debug #endif SensorI2CAccessDelay(apiRetStatus); preamble.buffer[0] = slaveAddr; preamble.length = 1; preamble.ctrlMask = 0x0000; #endif apiRetStatus = CyU3PI2cReceiveBytes(&preamble, buf, 1, 0);//send data block read one byte /*** test I2C bus ready ****/ if(apiRetStatus != CY_U3P_SUCCESS){ CyU3PDebugPrint(4, "sensor read2B(R) %d %d %d\r\n", apiRetStatus, buf[0], buf[1]); } SensorI2CAccessDelay(apiRetStatus); #ifdef DbgInfo CyU3PDebugPrint(4, "sensor read2B(2) %d %d %d\r\n", apiRetStatus, buf[0], buf[1]); //additional debug #endif return apiRetStatus; }
CyU3PReturnStatus_t SensorRead(uint8_t slaveAddr, uint8_t highAddr, uint8_t lowAddr, uint8_t count, uint8_t *buf) { CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS; CyU3PI2cPreamble_t preamble; /* Validate the parameters. */ if ((slaveAddr != SENSOR_ADDR_RD) && (slaveAddr != I2C_MEMORY_ADDR_RD)) { CyU3PDebugPrint(4, "I2C Slave address is not valid!\n"); return 1; } if (count > 64) { CyU3PDebugPrint(4, "ERROR: SensorWrite count > 64\n"); return 1; } preamble.buffer[0] = slaveAddr & I2C_SLAVEADDR_MASK; /* Mask out the transfer type bit. */ preamble.buffer[1] = 0x55;//highAddr; preamble.buffer[2] = 0xaa;//lowAddr; preamble.buffer[3] = slaveAddr; preamble.length = 4; preamble.ctrlMask = 0x0004; /* Send start bit after third byte of preamble. */ apiRetStatus = CyU3PI2cReceiveBytes(&preamble, buf, count, 0); SensorI2CAccessDelay(apiRetStatus); return apiRetStatus; }
CyU3PReturnStatus_t SensorWrite(uint8_t slaveAddr, uint8_t highAddr, uint8_t lowAddr, uint8_t count, uint8_t *buf) { CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS; CyU3PI2cPreamble_t preamble; /* Validate the I2C slave address. */ if ((slaveAddr != SENSOR_ADDR_WR) && (slaveAddr != I2C_MEMORY_ADDR_WR)) { CyU3PDebugPrint(4, "I2C Slave address is not valid!\n"); return 1; } if (count > 64) { CyU3PDebugPrint(4, "ERROR: SensorWrite count > 64\n"); return 1; } /* Set up the I2C control parameters and invoke the write API. */ preamble.buffer[0] = slaveAddr; preamble.buffer[1] = 0xab;//highAddr; preamble.buffer[2] = 0xcd;//lowAddr; preamble.length = 3; preamble.ctrlMask = 0x0000; apiRetStatus = CyU3PI2cTransmitBytes(&preamble, buf, count, 0); SensorI2CAccessDelay(apiRetStatus); return apiRetStatus; }
/* Write to an I2C slave with two bytes of data. */ CyU3PReturnStatus_t SensorWrite2B( uint8_t slaveAddr, uint8_t highAddr, uint8_t lowAddr, uint8_t highData, uint8_t lowData) { CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS; CyU3PI2cPreamble_t preamble; uint8_t buf[2]; /* Validate the I2C slave address. */ if ((slaveAddr != SENSOR_ADDR_WR) && (slaveAddr != I2C_MEMORY_ADDR_WR)) { CyU3PDebugPrint(4, "I2C Slave address is not valid!\n"); return 1; } preamble.buffer[0] = slaveAddr; /************** command block ***************************************/ preamble.buffer[1] = highAddr; preamble.buffer[2] = lowAddr; preamble.ctrlMask = 0x0000; preamble.length = 3; /* Three byte preamble. */ buf[0] = highData; apiRetStatus = CyU3PI2cTransmitBytes(&preamble, buf, 1, 0); #ifdef DbgInfo CyU3PDebugPrint(4, "sensor write2B(0) %d %d %d\r\n", lowAddr, buf[0], lowData); //additional debug #endif SensorI2CAccessDelay(apiRetStatus); buf[0] = lowData; /****************** data block *****************************************/ preamble.ctrlMask = 0x0000; preamble.length = 1; apiRetStatus = CyU3PI2cTransmitBytes(&preamble, buf, 1, 0); #ifdef DbgInfo CyU3PDebugPrint(4, "sensor write2B(1) %d %d %d\r\n", lowAddr, buf[0], buf[1]); //additional debug #endif /* Set the parameters for the I2C API access and then call the write API. */ SensorI2CAccessDelay(apiRetStatus); return apiRetStatus; }
// Write to an I2C slave with two bytes of data. static CyU3PReturnStatus_t SensorWrite2B(uint8_t slaveAddress, uint8_t highAddress, uint8_t lowAddress, uint8_t highData, uint8_t lowData) { CyU3PReturnStatus_t api_status = CY_U3P_SUCCESS; CyU3PI2cPreamble_t preamble; uint8_t buf[2]; // Validate the I2C slave address. if (slaveAddress != SENSOR_WRITE_ADDRESS) { CyU3PDebugPrint(4, "I2C Slave address is not valid!\r\n"); return 1; } // Set the parameters for the I2C API access and then call the write API. preamble.buffer[0] = slaveAddress; preamble.buffer[1] = highAddress; preamble.buffer[2] = lowAddress; preamble.length = 3; // Three byte preamble. preamble.ctrlMask = 0x0000; // No additional start and stop bits. buf[0] = highData; buf[1] = lowData; api_status = CyU3PI2cTransmitBytes(&preamble, buf, 2, 0); SensorI2CAccessDelay(api_status); if (api_status == CY_U3P_SUCCESS) { CyU3PDebugPrint (4, "Write Camera REG address = 0x%x%x%x%x data = 0x%x%x%x%x\r\n", highAddress >> 4, highAddress & 0x0f, lowAddress >> 4, lowAddress & 0x0f, highData >> 4, highData & 0x0f, lowData >> 4, lowData & 0x0f); } else {