/* * $Id: IicComm.c,v 1.1 2011/07/08 03:43:59 megakiss Exp $ * $Revision: 1.1 $ * $DateTime: 2005/08/24 19:03:17 $ * $Change: 28028 $ * $Author: megakiss $ */ #include #include "dsthalcommon.h" #include "IicComm.h" #include "cl.h" extern UINT32 gdhlExtDbgLvl; //----------------------------------------------------------------------------------------------------------- // Function name : SendToAddress // Description : Send to Address creates the stream to send using OrenIicTransmit function // the user must follow the following instruction: // 1. Allocate a buffer with the size . // 2. Copy the binary data from the file to the buffer starting from cell index 1. // 3. The cell in index 0 will be used by the function SendToAddress so the user doesnt have to. // fill it with a value. // Return type : unsigned int - IIC_COM_SUCCESS for success other value indicates an error occured // Argument : unsigned char - ucSendAddress the target sub addres in the cascade device // Argument : unsigned char* pucSendLoad - A pointer the buffer that holds the data to send (the data starts from cell index 1) // Argument : unsigned int uiSize - The size of the stream of data //----------------------------------------------------------------------------------------------------------- unsigned int SendToAddress(unsigned char ucSendAddress, unsigned char* pucSendLoad, unsigned int uiSize) { unsigned int iStatus ; // inserting the address byte pucSendLoad[0] =(unsigned char)(ucSendAddress & 0xff); // downloading the data to the cascade device iStatus = OrenIicTransmit(CASCADE_WRITE_ADDRESS, pucSendLoad, uiSize+1); return (iStatus); } //----------------------------------------------------------------------------------------------------------- // Function name : SendToReceive // Description : Send a stream of data starting from the given reciver address. // Return type : unsigned int - Success or Failure // Argument : int iReceiver - Receiever number // Argument : unsigned char *pcSendLoad - pointer to a strean array with additional cell for the receiver address(cell number 0) // Argument : unsigned int iSendSize - The size of the data to send not including the empty receiver address cell(cell number 0) //----------------------------------------------------------------------------------------------------------- unsigned int SendToReceive(int iReceiver, unsigned char *pucSendLoad, unsigned int uiSendSize) { if(NULL == pucSendLoad) return IIC_COM_NULL_PARAMETER; switch (iReceiver) { case 0: pucSendLoad[0] = 0x10; break; case 1: pucSendLoad[0] = 0x12; break; case 2: pucSendLoad[0] = 0x14; break; case 3: pucSendLoad[0] = 0x16; break; case 4: pucSendLoad[0] = 0x18; break; case 5: pucSendLoad[0] = 0x1a; break; case 6: pucSendLoad[0] = 0x1c; break; case 7: pucSendLoad[0] = 0x1e; break; default: return IIC_COM_UNDEFINED_RECEIVE_NUMBER; } return OrenIicTransmit(CASCADE_WRITE_ADDRESS,pucSendLoad, uiSendSize + 1); } //----------------------------------------------------------------------------------------------------------- // Function name : SendHostControl // Description : this function firs sends the index to the ucTransmitIndex to TX idx // register and then sends the ucHostControlRegister to the host control register // Return type : unsigned int IIC_COM_SUCCESS for success other value indicates an error occured // Argument : unsigned char ucHostControlRegister the data to the host control register // Argument : unsigned char ucTransmitIndex the index number that will be sent to the TX idx register //----------------------------------------------------------------------------------------------------------- unsigned int SendHostControl(unsigned char ucHostControlRegister,unsigned char ucTransmitIndex) { static unsigned char ucaSend[4]; unsigned int uiRes; //Inserting the address of the TX idx register ucaSend[0] = 0x4; //Inserting the index for the host TX idx register ucaSend[1] = ucTransmitIndex; uiRes = OrenIicTransmit(CASCADE_WRITE_ADDRESS,ucaSend, 2); if(IIC_COM_SUCCESS != uiRes) return uiRes; //Inserting the address of the Host ctrl ucaSend[0] = 0x0; //Inserting the data for the host ctrl ucaSend[1] = ucHostControlRegister; return OrenIicTransmit(CASCADE_WRITE_ADDRESS,ucaSend, 2); } unsigned short ReceiveTransmitRegisterValue(int iTransmitIndex, int *piError) { unsigned short usReceivedWord; #if 0 unsigned long ulWait = 50000000; #endif if((iTransmitIndex < 0) || (iTransmitIndex > 31)){ *piError = (int)UNDEFINED_TRANSMIT_REGISTER_NUMBER; return 0; } //first send a host control message with the appropriate index number if(0 != SendHostControl(0x80,(unsigned char)iTransmitIndex)){ *piError = SEND_HOST_CONTROL_FUNCTION_FAILED; return 0; } OS_mDelay( 10 ); usReceivedWord = ReceiveWordOfData((unsigned int *)piError); if(IIC_COM_SUCCESS != *piError){ *piError = RECEIVE_WORD_OF_DATA_FUNCTION_FAILED; return 0; } *piError = IIC_COM_SUCCESS; return usReceivedWord; } unsigned int SendRunFlag() { unsigned char ucRunFlag[2]; int iTransStat; //Inserting the address ucRunFlag[0] = (unsigned char)0x7f; //Inserting the runflag ucRunFlag[1] = (unsigned char)0xff; // downloading the data to the DVR chip //------------------------------------- iTransStat = OrenIicTransmit(CASCADE_WRITE_ADDRESS,ucRunFlag, 2); // checking return iTransStat; } //----------------------------------------------------------------------------------------------------------- //iic dummy functions //----------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------- // Function name : OrenIicTransmit // Description : this is a dummy function that simulates a transmission of a sequence of bytes to the cascade device // This sequence of bytes should be transmitted to the cascade device by IIC communication protocol // Return type : IIC_COM_SUCCESS for success other value indicates an error occured // Argument : unsigned char ucAddress can be write or read address 0x2a (write) 0x2b (Read) // Argument : unsigned char* pucSendBuffer will hold the sequence of bytes that will be sent after the ucAddress (will hold sub address and data) // Argument : unsigned int uiNumberOfBytesToSend holds the number of bytes in pucSendBuffer //----------------------------------------------------------------------------------------------------------- unsigned int OrenIicTransmit(unsigned int ucAddress,unsigned char* pucSendBuffer,unsigned int uiNumberOfBytesToSend) { int err = 1; err = DHL_SYS_I2cWrite((unsigned char)ucAddress,1,pucSendBuffer[0],&pucSendBuffer[1],uiNumberOfBytesToSend-1); if ( err ) DHL_DbgPrintf(gdhlExtDbgLvl,DHLDBG_EXT, "ERROR: Line= %d.\n", __LINE__); return (unsigned int)(err ? SEND_HOST_CONTROL_FUNCTION_FAILED : IIC_COM_SUCCESS); } //----------------------------------------------------------------------------------------------------------- // Function name : ReceiveWordOfData // Description : this is a dummy function that illustrates the call to the function that reads a word of data from // the cascade device // Return type : unsigned short the word that was read from the device // Argument : unsigned int *puiError if the function succeeds the value of puiError is set to IIC_COM_SUCCESS //----------------------------------------------------------------------------------------------------------- unsigned short ReceiveWordOfData(unsigned int *puiError) { unsigned char data[2]; int ret; ret = DHL_SYS_I2cRead(CASCADE_WRITE_ADDRESS|1, 0, 0, data, 2); if ( ret ) { DHL_DbgPrintf(gdhlExtDbgLvl,DHLDBG_EXT, "ERROR: I2C Receive Error, Line=%d\n", __LINE__); *puiError = RECEIVE_WORD_OF_DATA_FUNCTION_FAILED; return (unsigned short)-1; } *puiError = IIC_COM_SUCCESS; return ( (data[1] << 8) + (data[0]) ); } //----------------------------------------------------------------------------------------------------------- // Function name : ReceiveByteOfData // Description : this is a dummy function that illustrates the call to the function that reads a byte of data from // the cascade device // Return type : unsigned char the byte that was read from the device // Argument : unsigned int *puiError if the function succeeds the value of puiError is set to IIC_COM_SUCCESS //----------------------------------------------------------------------------------------------------------- unsigned char ReceiveByteOfData(unsigned int *puiError) { unsigned char data[1]; int ret; ret = DHL_SYS_I2cRead(CASCADE_WRITE_ADDRESS|1, 0, 0, data, 1); if ( ret ) { DHL_DbgPrintf(gdhlExtDbgLvl,DHLDBG_EXT, "ERROR: I2C Receive Error, Line=%d\n", __LINE__); *puiError = RECEIVE_WORD_OF_DATA_FUNCTION_FAILED; return (unsigned char)-1; } *puiError = IIC_COM_SUCCESS; return data[0]; } //----------------------------------------------------------------------------------------------------------- // Function name : ReceiveByteOfDataFromAddress // Description : this is a dummy function that illustrates the call to the function that reads a byte of data from // the device in slave adddress ucAddress // Return type : unsigned char the byte that was read from the device // Argument : unsigned char ucSlaveAddress - the slave address of the device that we want to read from // Argument : unsigned int *puiError if the function succeeds the value of puiError is set to IIC_COM_SUCCESS //----------------------------------------------------------------------------------------------------------- unsigned char ReceiveByteOfDataFromAddress(unsigned int ucSlaveAddress,unsigned int *puiError) { unsigned char data[1]; int ret; ret = DHL_SYS_I2cRead(ucSlaveAddress|1, 0, 0, data, 1); if ( ret ) { DHL_DbgPrintf(gdhlExtDbgLvl,DHLDBG_EXT, "ERROR: I2C Receive Error, Line=%d\n", __LINE__); *puiError = RECEIVE_WORD_OF_DATA_FUNCTION_FAILED; return (unsigned char)-1; } *puiError = IIC_COM_SUCCESS; return data[0]; } // Function name : ReceiveVector // Description : retrives a vector data from the cascade devie // Return type : unsigned int IIC_COM_SUCCESS for success other value indicates an error occured // Argument : unsigned int uiVectorIdIndex can be 1-6 // Argument : unsigned short * pusVector buffer for the retrived vector // Argument : unsigned short usVectorLength number of elements in the vector buffer unsigned int ReceiveVector(unsigned int uiVectorIdIndex,unsigned short * pusVector,unsigned short usVectorLength) { unsigned char cSendRec[3]; unsigned int uiError,uiErrorCnt=0; unsigned short usReg23 = 0,usSend = 0,usLenCounter = 0,ulWait; int iRunLimit = 0,i; // int iStopComm = 0; // int iVectorError = 0; if(NULL == pusVector) return IIC_COM_NULL_PARAMETER; for (i = 0; i<(int)usVectorLength; i++) { pusVector[i] = 0; } //Start receiving the data while (usLenCounter < usVectorLength) { //Set the Vector ID in the appropriate place. add the index to bits 11..0 usSend = ((uiVectorIdIndex & 0xf) << 12); usSend |= (usLenCounter & 0xfff); cSendRec[1] = (char)(usSend >> 8); cSendRec[2] = (char)(usSend); uiError = SendToReceive(0, cSendRec, 2); if (IIC_COM_SUCCESS != uiError) return uiError; // send index = 23 uiError = SendHostControl(0x80, 23); //Check the next index to see if everything is OK if (IIC_COM_SUCCESS != uiError) return uiError; usReg23 = ReceiveWordOfData(&uiError); if (IIC_COM_SUCCESS != uiError) return uiError; //Check to see if the cascade added all the indexes if ((usReg23 & 0xfff) == usLenCounter) { //In case evrything is OK reset the error counter update the next index to read, // and copy the data to the file uiErrorCnt = 0; } else { ulWait = 1000; //wait a small delay while(ulWait > 0) ulWait -- ; //Increasethe error counter uiErrorCnt++; //In case we reached to 10 there is a problem in the coomunication if (10 == uiErrorCnt) return IIC_COM_RECEIVE_INDEX_FROM_REG_23_FAILED_FOR_10_TIMES; continue; } //Check if we need to read more than 8. //If more than 8 read the next 8 if not read whats left if ((int)(usVectorLength - usLenCounter) > 8 ) iRunLimit = 8; else iRunLimit = (int)(usVectorLength - usLenCounter); for (i=0; i