//UINT32 MxL_I2C_Read(UINT8 DeviceAddr, UINT8 Addr, UINT8* mData) UINT32 MxL_I2C_Read(MxL5007_TunerConfigS* myTuner, UINT8 Addr, UINT8* mData) { TUNER_MODULE *pTuner; I2C_BRIDGE_MODULE *pI2cBridge; unsigned char DeviceAddr; unsigned char Buffer[LEN_2_BYTE]; // Get tuner module and I2C bridge. pTuner = myTuner->pTuner; pI2cBridge = pTuner->pI2cBridge; // Get tuner device address. pTuner->GetDeviceAddr(pTuner, &DeviceAddr); // Set tuner register reading address. // Note: The I2C format of tuner register reading address setting is as follows: // start_bit + (DeviceAddr | writing_bit) + 0xfb + RegReadingAddr + stop_bit Buffer[0] = (unsigned char)MXL5007T_I2C_READING_CONST; Buffer[1] = (unsigned char)Addr; if(pI2cBridge->ForwardI2cWritingCmd(pI2cBridge, DeviceAddr, Buffer, LEN_2_BYTE) != FUNCTION_SUCCESS) goto error_status_set_tuner_register_reading_address; // Get tuner register bytes. // Note: The I2C format of tuner register byte getting is as follows: // start_bit + (DeviceAddr | reading_bit) + reading_byte + stop_bit if(pI2cBridge->ForwardI2cReadingCmd(pI2cBridge, DeviceAddr, Buffer, LEN_1_BYTE) != FUNCTION_SUCCESS) goto error_status_get_tuner_registers; *mData = (UINT8)Buffer[0]; return MxL_OK; error_status_get_tuner_registers: error_status_set_tuner_register_reading_address: return MxL_ERR_OTHERS; }
//UINT32 MxL_I2C_Write(UINT8 DeviceAddr, UINT8* pArray, UINT32 count) UINT32 MxL_I2C_Write(MxL5007_TunerConfigS* myTuner, UINT8* pArray, UINT32 count) { TUNER_MODULE *pTuner; BASE_INTERFACE_MODULE *pBaseInterface; I2C_BRIDGE_MODULE *pI2cBridge; unsigned char DeviceAddr; unsigned long WritingByteNumMax; unsigned long i; unsigned char Buffer[I2C_BUFFER_LEN]; unsigned long WritingIndex; unsigned char *pData; unsigned long DataLen; // Get tuner module, base interface, and I2C bridge. pTuner = myTuner->pTuner; pBaseInterface = pTuner->pBaseInterface; pI2cBridge = pTuner->pI2cBridge; // Get tuner device address. pTuner->GetDeviceAddr(pTuner, &DeviceAddr); // Get writing byte and byte number. pData = (unsigned char *)pArray; DataLen = (unsigned long)count; // Calculate MxL5007T maximum writing byte number. // Note: MxL5007T maximum writing byte number must be a multiple of 2. WritingByteNumMax = pBaseInterface->I2cWritingByteNumMax; WritingByteNumMax = ((WritingByteNumMax % 2) == 0) ? WritingByteNumMax : (WritingByteNumMax - 1); // Set register bytes. // Note: The 2 kind of I2C formats of MxL5007T is described as follows: // 1. start_bit + (device_addr | writing_bit) + (register_addr + writing_byte) * n + stop_bit // ... // start_bit + (device_addr | writing_bit) + (register_addr + writing_byte) * m + stop_bit // 2. start_bit + (device_addr | writing_bit) + 0xff + stop_bit for(i = 0, WritingIndex = 0; i < DataLen; i++, WritingIndex++) { // Put data into buffer. Buffer[WritingIndex] = pData[i]; // If writing buffer is full or put data into buffer completely, send the I2C writing command with buffer. if( (WritingIndex == (WritingByteNumMax - 1)) || (i == (DataLen - 1)) ) { if(pI2cBridge->ForwardI2cWritingCmd(pI2cBridge, DeviceAddr, Buffer, (WritingIndex + LEN_1_BYTE)) != FUNCTION_SUCCESS) goto error_status_set_tuner_registers; WritingIndex = -1; } } return MxL_OK; error_status_set_tuner_registers: return MxL_ERR_OTHERS; }