source: svn/trunk/zas_dstar/devices/Tuner/Oren_Cascade2B/IicComm.c @ 2

Last change on this file since 2 was 2, checked in by phkim, 11 years ago

1.phkim

  1. revision copy newcon3sk r27
File size: 13.0 KB
Line 
1/*
2 * $Id: IicComm.c,v 1.1 2011/07/08 03:43:59 megakiss Exp $
3 * $Revision: 1.1 $
4 * $DateTime: 2005/08/24 19:03:17 $
5 * $Change: 28028 $
6 * $Author: megakiss $
7 */
8
9#include <stdio.h>
10
11#include "dsthalcommon.h"
12#include "IicComm.h"
13#include "cl.h"
14
15extern UINT32 gdhlExtDbgLvl;
16
17//-----------------------------------------------------------------------------------------------------------
18// Function name        : SendToAddress
19// Description      : Send to Address creates the stream to send using OrenIicTransmit function
20//                                        the user must follow the following instruction:
21//                                        1. Allocate a buffer with the size <number of bytes to send + 1>.
22//                                        2. Copy the binary data from the file to the buffer starting from cell index 1.
23//                                        3. The cell in index 0 will be used by the function SendToAddress so the user doesnt have to.         
24//                                               fill it with a value.
25// Return type          : unsigned int - IIC_COM_SUCCESS for success other value indicates an error occured
26// Argument         : unsigned char  - ucSendAddress the target sub addres in the cascade device
27// Argument         : unsigned char* pucSendLoad - A pointer the buffer that holds the data to send (the data starts from cell index 1)
28// Argument         : unsigned int uiSize - The size of the stream of data
29//-----------------------------------------------------------------------------------------------------------
30unsigned int SendToAddress(unsigned char ucSendAddress, unsigned char* pucSendLoad, unsigned int uiSize)
31{
32        unsigned int iStatus ;
33
34    // inserting the address  byte
35    pucSendLoad[0] =(unsigned char)(ucSendAddress & 0xff);
36                                                                             
37    // downloading the data to the cascade device
38        iStatus = OrenIicTransmit(CASCADE_WRITE_ADDRESS, pucSendLoad, uiSize+1);
39
40    return (iStatus);
41}
42
43
44//-----------------------------------------------------------------------------------------------------------
45// Function name        : SendToReceive
46// Description      : Send a stream of data starting from the given reciver address.
47// Return type          : unsigned int - Success or Failure
48// Argument         : int iReceiver - Receiever number
49// Argument         : unsigned char *pcSendLoad - pointer to a strean array with additional cell for the receiver address(cell number 0)
50// Argument         : unsigned int iSendSize - The size of the data to send not including the empty receiver address cell(cell number 0)
51//-----------------------------------------------------------------------------------------------------------
52unsigned int SendToReceive(int iReceiver, unsigned char *pucSendLoad, unsigned int uiSendSize)
53{
54        if(NULL == pucSendLoad)
55                return IIC_COM_NULL_PARAMETER;
56
57        switch (iReceiver) {
58        case 0:
59                pucSendLoad[0] = 0x10;
60                break;
61        case 1:
62                pucSendLoad[0] = 0x12;
63                break;
64        case 2:
65                pucSendLoad[0] = 0x14;
66                break;
67        case 3:
68                pucSendLoad[0] = 0x16;
69                break;
70        case 4:
71                pucSendLoad[0] = 0x18;
72                break;
73        case 5:
74                pucSendLoad[0] = 0x1a;
75                break;
76        case 6:
77                pucSendLoad[0] = 0x1c;
78                break;
79        case 7:
80                pucSendLoad[0] = 0x1e;
81                break;
82        default:
83                return IIC_COM_UNDEFINED_RECEIVE_NUMBER;
84        }
85
86        return OrenIicTransmit(CASCADE_WRITE_ADDRESS,pucSendLoad, uiSendSize + 1);
87}
88
89
90//-----------------------------------------------------------------------------------------------------------
91// Function name        : SendHostControl
92// Description      : this function firs sends the index to the ucTransmitIndex to TX idx
93//                                        register and then sends the ucHostControlRegister to the host control register
94// Return type          : unsigned int IIC_COM_SUCCESS for success other value indicates an error occured 
95// Argument         : unsigned char ucHostControlRegister the data to the host control register
96// Argument         : unsigned char ucTransmitIndex the index number that will be sent to the TX idx register
97//-----------------------------------------------------------------------------------------------------------
98unsigned int SendHostControl(unsigned char ucHostControlRegister,unsigned char ucTransmitIndex)
99{
100        static unsigned char ucaSend[4];
101        unsigned int uiRes;
102        //Inserting the address of the TX idx register
103        ucaSend[0] = 0x4;
104        //Inserting the index for the host TX idx register
105    ucaSend[1] = ucTransmitIndex;
106        uiRes = OrenIicTransmit(CASCADE_WRITE_ADDRESS,ucaSend, 2);
107        if(IIC_COM_SUCCESS != uiRes)
108                return uiRes;
109
110        //Inserting the address of the Host ctrl
111        ucaSend[0] = 0x0;
112        //Inserting the data for the host ctrl
113    ucaSend[1] = ucHostControlRegister;
114        return OrenIicTransmit(CASCADE_WRITE_ADDRESS,ucaSend, 2);
115}
116
117
118unsigned short ReceiveTransmitRegisterValue(int iTransmitIndex, int *piError)
119{
120        unsigned short usReceivedWord;
121#if 0
122        unsigned long ulWait = 50000000;
123#endif
124       
125        if((iTransmitIndex < 0) || (iTransmitIndex > 31)){
126                *piError = (int)UNDEFINED_TRANSMIT_REGISTER_NUMBER;
127                return 0;
128        }
129        //first send a host control message with the appropriate index number
130        if(0 != SendHostControl(0x80,(unsigned char)iTransmitIndex)){
131                *piError = SEND_HOST_CONTROL_FUNCTION_FAILED;
132                return 0;
133        }
134
135        OS_mDelay( 10 );
136
137        usReceivedWord = ReceiveWordOfData((unsigned int *)piError);
138        if(IIC_COM_SUCCESS != *piError){
139                *piError =  RECEIVE_WORD_OF_DATA_FUNCTION_FAILED;
140                return 0;
141        }
142
143        *piError = IIC_COM_SUCCESS;
144        return usReceivedWord;
145}
146
147
148
149
150unsigned int SendRunFlag()
151{
152        unsigned char ucRunFlag[2];
153        int iTransStat;
154        //Inserting the address
155        ucRunFlag[0] = (unsigned char)0x7f;
156
157        //Inserting the runflag
158    ucRunFlag[1] = (unsigned char)0xff;
159                               
160    // downloading the data to the DVR chip
161    //-------------------------------------                                                                                     
162    iTransStat = OrenIicTransmit(CASCADE_WRITE_ADDRESS,ucRunFlag, 2);
163        // checking
164
165    return iTransStat;
166}
167
168
169
170//-----------------------------------------------------------------------------------------------------------
171//iic dummy functions
172//-----------------------------------------------------------------------------------------------------------
173
174//-----------------------------------------------------------------------------------------------------------
175// Function name        : OrenIicTransmit
176// Description      : this is a dummy function that simulates a transmission of a sequence of bytes to the cascade device
177//                                        This sequence of bytes should be transmitted to the cascade device by IIC communication protocol     
178// Return type          : IIC_COM_SUCCESS for success other value indicates an error occured
179// Argument         : unsigned char ucAddress can be write or read address 0x2a (write) 0x2b (Read)
180// Argument         : unsigned char* pucSendBuffer will hold the sequence of bytes that will be sent after the ucAddress (will hold sub address and data)
181// Argument         : unsigned int uiNumberOfBytesToSend holds the number of bytes in pucSendBuffer
182//-----------------------------------------------------------------------------------------------------------
183unsigned int OrenIicTransmit(unsigned int ucAddress,unsigned char* pucSendBuffer,unsigned int uiNumberOfBytesToSend)
184{
185        int err = 1;
186
187        err = DHL_SYS_I2cWrite((unsigned char)ucAddress,1,pucSendBuffer[0],&pucSendBuffer[1],uiNumberOfBytesToSend-1);
188        if ( err ) 
189                DHL_DbgPrintf(gdhlExtDbgLvl,DHLDBG_EXT, "ERROR: Line= %d.\n", __LINE__);
190       
191        return (unsigned int)(err ? SEND_HOST_CONTROL_FUNCTION_FAILED : IIC_COM_SUCCESS);
192}
193
194//-----------------------------------------------------------------------------------------------------------
195// Function name        : ReceiveWordOfData
196// Description      : this is a dummy function that illustrates the call to the function that reads a word of data from
197//                                        the cascade device
198// Return type          : unsigned short the word that was read from the device
199// Argument         : unsigned int *puiError if the function succeeds the value of puiError is set to IIC_COM_SUCCESS
200//-----------------------------------------------------------------------------------------------------------
201unsigned short ReceiveWordOfData(unsigned int *puiError)
202{
203        unsigned char data[2];
204        int ret;
205
206        ret = DHL_SYS_I2cRead(CASCADE_WRITE_ADDRESS|1, 0, 0, data, 2);
207    if ( ret ) {
208        DHL_DbgPrintf(gdhlExtDbgLvl,DHLDBG_EXT, "ERROR: I2C Receive Error, Line=%d\n", __LINE__);
209                *puiError = RECEIVE_WORD_OF_DATA_FUNCTION_FAILED;
210                return (unsigned short)-1;
211    }
212   
213    *puiError = IIC_COM_SUCCESS;
214    return ( (data[1] << 8) + (data[0]) );
215}
216
217//-----------------------------------------------------------------------------------------------------------
218// Function name        : ReceiveByteOfData
219// Description      : this is a dummy function that illustrates the call to the function that reads a byte of data from
220//                                        the cascade device
221// Return type          : unsigned char the byte that was read from the device
222// Argument         : unsigned int *puiError if the function succeeds the value of puiError is set to IIC_COM_SUCCESS
223//-----------------------------------------------------------------------------------------------------------
224unsigned char  ReceiveByteOfData(unsigned int *puiError)
225{
226        unsigned char data[1];
227        int ret;
228
229        ret = DHL_SYS_I2cRead(CASCADE_WRITE_ADDRESS|1, 0, 0, data, 1);
230    if ( ret ) {
231        DHL_DbgPrintf(gdhlExtDbgLvl,DHLDBG_EXT, "ERROR: I2C Receive Error, Line=%d\n", __LINE__);
232                *puiError = RECEIVE_WORD_OF_DATA_FUNCTION_FAILED;
233                return (unsigned char)-1;
234    }
235   
236    *puiError = IIC_COM_SUCCESS;
237    return data[0];
238}
239
240//-----------------------------------------------------------------------------------------------------------
241// Function name        :  ReceiveByteOfDataFromAddress
242// Description      :  this is a dummy function that illustrates the call to the function that reads a byte of data from
243//                                         the device in slave adddress ucAddress
244// Return type          : unsigned char the byte that was read from the device
245// Argument         : unsigned char ucSlaveAddress - the slave address of the device that we want to read from
246// Argument         : unsigned int *puiError if the function succeeds the value of puiError is set to IIC_COM_SUCCESS
247//-----------------------------------------------------------------------------------------------------------
248unsigned char  ReceiveByteOfDataFromAddress(unsigned int ucSlaveAddress,unsigned int *puiError)
249{
250        unsigned char data[1];
251        int ret;
252
253        ret = DHL_SYS_I2cRead(ucSlaveAddress|1, 0, 0, data, 1);
254    if ( ret ) {
255        DHL_DbgPrintf(gdhlExtDbgLvl,DHLDBG_EXT, "ERROR: I2C Receive Error, Line=%d\n", __LINE__);
256                *puiError = RECEIVE_WORD_OF_DATA_FUNCTION_FAILED;
257                return (unsigned char)-1;
258    }
259   
260    *puiError = IIC_COM_SUCCESS;
261    return data[0];
262}
263
264
265// Function name        : ReceiveVector
266// Description      : retrives a vector data from the cascade devie
267// Return type          : unsigned int  IIC_COM_SUCCESS for success other value indicates an error occured
268// Argument         : unsigned int uiVectorIdIndex can be 1-6
269// Argument         : unsigned short * pusVector buffer for the retrived vector
270// Argument         : unsigned short usVectorLength number of elements in the vector buffer
271unsigned int ReceiveVector(unsigned int uiVectorIdIndex,unsigned short * pusVector,unsigned short usVectorLength)
272{
273        unsigned char cSendRec[3];
274        unsigned int uiError,uiErrorCnt=0; 
275        unsigned short usReg23 = 0,usSend = 0,usLenCounter = 0,ulWait;
276        int iRunLimit = 0,i;
277//      int iStopComm = 0;
278//      int iVectorError = 0;
279       
280       
281        if(NULL == pusVector)
282                return IIC_COM_NULL_PARAMETER;
283
284        for (i = 0; i<(int)usVectorLength; i++) {
285                pusVector[i] = 0;
286        }
287
288        //Start receiving the data
289        while (usLenCounter < usVectorLength) {
290                //Set the Vector ID in the appropriate place. add the index to bits 11..0
291                usSend = ((uiVectorIdIndex & 0xf) << 12); 
292                usSend |= (usLenCounter & 0xfff);
293
294                cSendRec[1] = (char)(usSend >> 8);
295                cSendRec[2] = (char)(usSend);
296                uiError = SendToReceive(0, cSendRec, 2);
297                if (IIC_COM_SUCCESS != uiError) 
298                        return uiError;
299               
300                // send index = 23
301                uiError = SendHostControl(0x80, 23); //Check the next index to see if everything is OK
302                if (IIC_COM_SUCCESS != uiError) 
303                        return uiError;
304
305                usReg23 = ReceiveWordOfData(&uiError); 
306                if (IIC_COM_SUCCESS != uiError) 
307                        return uiError;
308
309                //Check to see if the cascade added all the indexes
310                if ((usReg23 & 0xfff) == usLenCounter) {
311                        //In case evrything is OK reset the error counter update the next index to read,
312                        // and copy the data to the file
313                        uiErrorCnt = 0; 
314                }
315                else {
316                        ulWait = 1000;
317                        //wait a small delay
318                        while(ulWait > 0)
319                                ulWait -- ;
320
321                        //Increasethe error counter
322                        uiErrorCnt++;
323                        //In case we reached to 10 there is a problem in the coomunication
324                        if (10 == uiErrorCnt) 
325                                return IIC_COM_RECEIVE_INDEX_FROM_REG_23_FAILED_FOR_10_TIMES;
326                       
327                        continue;
328                }
329
330
331                //Check if we need to read more than 8.
332                //If more than 8 read the next 8 if not read whats left
333                if ((int)(usVectorLength - usLenCounter) > 8 )
334                        iRunLimit = 8;
335                else 
336                        iRunLimit = (int)(usVectorLength - usLenCounter);
337
338               
339                for (i=0; i<iRunLimit; i++) {
340                    pusVector[usLenCounter] = ReceiveWordOfData(&uiError);     
341                        if (IIC_COM_SUCCESS != uiError) 
342                                return uiError;
343                       
344                        usLenCounter++;
345                }
346       
347        }
348       
349
350
351        //Stop sending vector
352        cSendRec[1] = 0;
353        cSendRec[2] = 0;
354        uiError = SendToReceive(0, cSendRec, 2);
355        if (IIC_COM_SUCCESS != uiError) 
356                return uiError;
357        else
358                return IIC_COM_SUCCESS;
359}
360
Note: See TracBrowser for help on using the repository browser.