uint16_t rdwr_vcon_pot(uint8_t *val, CyBool_t write) { uint16_t status; CyU3PI2cPreamble_t preamble; preamble.length = 1; uint8_t addr = UXN1330_VCON_POT; if (!write) { addr |= 1; // read } preamble.buffer[0] = addr; preamble.ctrlMask = 0x0000; if (write) { status = CyU3PI2cTransmitBytes(&preamble, val, 1, 1); } else { status = CyU3PI2cReceiveBytes(&preamble, val, 1, 1); } if(status) { log_debug ( "vcon_pot CyU3PI2c status: %d\n", status ); return status; /* Wait for the write to complete. */ preamble.length = 1; status = CyU3PI2cWaitForAck(&preamble, 200); if(status) { log_error( "CyU3PI2cWaitForAck fail %d\n", status ); return status; } } return 0; }
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; }
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; }
// 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 {
CyBool_t handle_serial_num(uint8_t bReqType, uint16_t wLength) { CyU3PI2cPreamble_t preamble; uint32_t reg_addr = FX3_PROM_SERIALNUM0_0; uint8_t dev_addr = 0x50; uint8_t size = 17; // size of prom uint16_t status; if (wLength != 16) { log_error("Bad length=%d \n", wLength); return CY_U3P_ERROR_BAD_ARGUMENT; } switch(bReqType) { case 0xC0: preamble.length = 4; preamble.buffer[0] = m24xx_get_dev_addr(dev_addr, reg_addr, size, 0); preamble.buffer[1] = (uint8_t)(reg_addr >> 8); preamble.buffer[2] = (uint8_t)(reg_addr & 0xFF); preamble.buffer[3] = m24xx_get_dev_addr(dev_addr, reg_addr, size, 1); preamble.ctrlMask = 0x0004; status = CyU3PI2cReceiveBytes (&preamble, gSerialNum, 16, 1); if(status) { log_error("Error reading serial num from prom (%d)\n", status); return CyFalse; } status = CyU3PUsbSendEP0Data(16, gSerialNum); if(status) { log_error("Error Sending serial num to EP0 (%d)\n", status); return CyFalse; } return CyTrue; case 0x40: status = CyU3PUsbGetEP0Data(wLength, gSerialNum, 0); if(status) { log_error("Error getting serial num from EP0 (%d)\n", status); return status; } preamble.length = 3; preamble.buffer[0] = m24xx_get_dev_addr(dev_addr, reg_addr, size, 0); preamble.buffer[1] = (uint8_t)(reg_addr >> 8); preamble.buffer[2] = (uint8_t)(reg_addr & 0xFF); preamble.ctrlMask = 0x0000; status = CyU3PI2cTransmitBytes(&preamble, gSerialNum, 16, 1); if(status) { log_error("Error writing serial num to I2C (%d)\n", status); return CyFalse; } /* Wait for the write to complete. */ preamble.length = 1; status = CyU3PI2cWaitForAck(&preamble, 200); if(status) { log_error("Error waiting for i2c ACK after writing serial num (%d)\n", status); return CyFalse; } /* An additional delay seems to be required after receiving an ACK. */ CyU3PThreadSleep (1); return CyTrue; default: log_error("Bad ReqType=%d \n", bReqType); return CY_U3P_ERROR_BAD_ARGUMENT; } }