pca9564.c
上传用户:caisangzi8
上传日期:2013-10-25
资源大小:15756k
文件大小:8k
源码类别:

DVD

开发平台:

C/C++

  1. #include "pca9564.h"
  2. #include "global.h"
  3. #include "regmap.h"
  4. /* Send a string of bytes to the I2C transmit buffer */
  5. int I2C_Slave_Send_Data(UINT8 *tx_data, UINT8 nbytes)
  6. {
  7.   int result = SUCCESS;
  8.   UINT8 i;
  9.   
  10.   /* Ensure that the message fits into the available tx buffer */
  11.   /* Otherwise signal an error */
  12.   if (nbytes <= I2C_Slave_Get_Tx_Buf_Avail())
  13.   {
  14.     for (i=0; i<nbytes; i++)
  15.     {
  16.       /* Ensure enough space to put the byte in */
  17.       if (I2C_Slave_Is_Tx_Buf_Full() == FALSE)
  18.       {
  19.         /* Copy the tx data byte in */
  20.         I2C_Slave_Tx_Buf_PutChar(tx_data[i]);
  21.       }
  22.       else
  23.       {
  24.         result = FAILURE;
  25.         break;
  26.       }
  27.     }
  28.   }
  29.   else
  30.   {
  31.     result = FAILURE;
  32.   }
  33.   
  34.   return result;
  35. }
  36. /* Read a string of bytes from the I2C receive buffer */
  37. int I2C_Slave_Read_Data(UINT8 *rx_data, UINT8 nbytes)
  38. {
  39.   int result = SUCCESS;
  40.   UINT8 i;  
  41.   
  42.   /* Check that receive buffer has at least nbytes */
  43.   /* Else don't receive the data at all */
  44.   /* HDI Interface : */
  45.   /* - Control Command : 4 bytes */
  46.   /* - System Command  : 4 bytes */
  47.   /* - Info Command    : 4 bytes */
  48.   /* - Set Command     : 37 bytes */
  49.   if (I2C_Slave_Get_Rx_Buf_Length() < nbytes)
  50.   {
  51.     return FAILURE;
  52.   }
  53.   
  54.   for (i=0; i<nbytes; i++)
  55.   {
  56.     if (I2C_Slave_Is_Rx_Buf_Empty() == FALSE)
  57.     {
  58.       *(rx_data + i) = I2C_Slave_Rx_Buf_GetChar();
  59.     }
  60.     else
  61.     {
  62.       result = FAILURE;
  63.       break;
  64.     }
  65.   }
  66.   
  67.   return result;
  68. }
  69. /* Check if I2C receive buffer is empty */
  70. UINT8 I2C_Slave_Is_Rx_Buf_Empty(void)
  71. {
  72.   if (I2C_Slave_Get_Rx_In_Ptr() != I2C_Slave_Get_Rx_Out_Ptr())
  73.   {
  74.     return FALSE;
  75.   }
  76.   else
  77.   {
  78.     return TRUE;
  79.   }
  80. }
  81. /* Check if I2C receive buffer is full */
  82. UINT8 I2C_Slave_Is_Rx_Buf_Full(void)
  83. {     
  84.   if ((I2C_Slave_Get_Rx_In_Ptr() + 1) == I2C_Slave_Get_Rx_Out_Ptr())
  85.   {  
  86.     return TRUE;
  87.   }
  88.   else
  89.   {
  90.     return FALSE;
  91.   }
  92. }
  93. /* Check if I2C transmit buffer is empty */
  94. UINT8 I2C_Slave_Is_Tx_Buf_Empty(void)
  95. {
  96.   if (I2C_Slave_Get_Tx_In_Ptr() != I2C_Slave_Get_Tx_Out_Ptr())
  97.   {
  98.     return FALSE;
  99.   }
  100.   else
  101.   {
  102.     return TRUE;
  103.   }
  104. }
  105. /* Check if I2C receive buffer is full */
  106. UINT8 I2C_Slave_Is_Tx_Buf_Full(void)
  107. {
  108.   UINT8 temp;
  109.   
  110.   temp = I2C_Slave_Get_Tx_In_Ptr() + 1;
  111.   
  112.   if (temp == MAX_I2C_SLAVE_TX_BUF_LEN)
  113.   {
  114.     temp = 0;
  115.   }
  116.   if (temp == I2C_Slave_Get_Tx_Out_Ptr())
  117.   {
  118.     return TRUE;
  119.   }
  120.   else
  121.   {
  122.     return FALSE;
  123.   }
  124. }
  125. /* Get Rx Buffer Empty Space */
  126. UINT8 I2C_Slave_Get_Rx_Buf_Avail(void)
  127. {
  128.   UINT8 space_avail = 0;
  129.   
  130.   space_avail = (UINT8)(MAX_I2C_SLAVE_RX_BUF_LEN - I2C_Slave_Get_Rx_Buf_Length() - 1);
  131.   
  132.   return space_avail;
  133. }
  134. /* Get Rx Buffer Empty Space */
  135. UINT8 I2C_Slave_Get_Rx_Buf_Length(void)
  136. {
  137.   UINT8 length = 0;
  138.   UINT8 in_ptr, out_ptr;  
  139.   
  140.   in_ptr  = I2C_Slave_Get_Rx_In_Ptr();
  141.   out_ptr = I2C_Slave_Get_Rx_Out_Ptr();
  142.   
  143.   if (in_ptr != out_ptr)
  144.   {
  145.     do
  146.     { 
  147.       out_ptr += 1;
  148.       if (out_ptr == MAX_I2C_SLAVE_RX_BUF_LEN)
  149.       {
  150.         out_ptr = 0;
  151.       }       
  152.       length = length + 1;
  153.     } while (in_ptr != out_ptr);
  154.   }
  155.   return length;
  156. }
  157. /* Get Tx Buffer Empty Space */
  158. UINT8 I2C_Slave_Get_Tx_Buf_Avail(void)
  159. {
  160.   UINT8 space_avail = MAX_I2C_SLAVE_TX_BUF_LEN;
  161.   
  162.   space_avail = (UINT8)(MAX_I2C_SLAVE_TX_BUF_LEN - I2C_Slave_Get_Tx_Buf_Length() - 1);
  163.   
  164.   return space_avail; 
  165. }
  166. /* Calculate the length of the buffer used */
  167. UINT8 I2C_Slave_Get_Tx_Buf_Length(void)
  168. {
  169.   UINT8 length = 0;
  170.   UINT8 in_ptr, out_ptr;  
  171.   
  172.   in_ptr  = I2C_Slave_Get_Tx_In_Ptr();
  173.   out_ptr = I2C_Slave_Get_Tx_Out_Ptr();
  174.   
  175.   if (in_ptr != out_ptr)
  176.   {
  177.     do
  178.     { 
  179.       out_ptr += 1;
  180.       if (out_ptr == MAX_I2C_SLAVE_TX_BUF_LEN)
  181.       {
  182.         out_ptr = 0;
  183.       } 
  184.       length = length + 1;
  185.     } while (in_ptr != out_ptr);
  186.   }
  187.   return length;
  188. }
  189. /* Get a data byte from the receive buffer */
  190. UINT8 I2C_Slave_Rx_Buf_GetChar(void)
  191. {
  192.   UINT8 c = 0;  // NULL Character
  193.   /* Check if receive buffer has something to retrieve */  
  194.   if (I2C_Slave_Is_Rx_Buf_Empty() == FALSE)
  195.   {  
  196.     I2C_Slave_Set_Rx_Out_Ptr(I2C_Slave_Get_Rx_Out_Ptr() + 1);
  197.     
  198.     /* Allow roll-over */
  199.     if (I2C_Slave_Get_Rx_Out_Ptr() > (MAX_I2C_SLAVE_RX_BUF_LEN-1))
  200.     {
  201.       I2C_Slave_Set_Rx_Out_Ptr(0);
  202.     }
  203.     /* Check if any character in the buffer */
  204.     c = *((BYTE *)(I2C_Slave_Rx_Buf_Ptr() + I2C_Slave_Get_Rx_Out_Ptr())); 
  205.   }
  206.   
  207.   return c;
  208. }
  209. /* Put a data byte to the transmit buffer */
  210. int I2C_Slave_Tx_Buf_PutChar(UINT8 c)
  211. {
  212.   int result = FAILURE;
  213.   
  214.   //if (c != 0)
  215.   {
  216.     /* Ensure enough space in receive buffer */
  217.     if (I2C_Slave_Is_Tx_Buf_Full() != TRUE)
  218.     {
  219.       I2C_Slave_Set_Tx_In_Ptr(I2C_Slave_Get_Tx_In_Ptr() + 1);
  220.       
  221.       /* Allow roll-over */
  222.       if (I2C_Slave_Get_Tx_In_Ptr() == MAX_I2C_SLAVE_TX_BUF_LEN)
  223.       {
  224.         I2C_Slave_Set_Tx_In_Ptr(0);
  225.       }
  226.       
  227.       /* Copy the data byte in */
  228.       *((UINT8 *)((UINT32)(I2C_Slave_Tx_Buf_Ptr() + I2C_Slave_Get_Tx_In_Ptr()))) = c;
  229.       result = SUCCESS;
  230.     }
  231.   }
  232.   return result;   
  233. }
  234. // Rx In/Out Pointers
  235. BYTE  I2C_Slave_Get_Rx_In_Ptr(void)
  236. {
  237.   return (BYTE)((regs0->iop_data[2]&0x00ff)>>0);//write by 6502
  238. }    
  239. BYTE  I2C_Slave_Get_Rx_Out_Ptr(void)
  240. {
  241.   return (BYTE)((regs0->iop_data[2]&0xff00)>>8);
  242. }    
  243. // Tx In/Out Pointers
  244. BYTE  I2C_Slave_Get_Tx_In_Ptr(void)
  245. {
  246.   return (BYTE)((regs0->iop_data[3]&0x00ff)>>0);
  247. }    
  248. BYTE  I2C_Slave_Get_Tx_Out_Ptr(void)
  249. {
  250.   return (BYTE)((regs0->iop_data[3]&0xff00)>>8);
  251. }
  252. // Increment Rx Out Buffer
  253. void  I2C_Slave_Set_Rx_Out_Ptr(UINT16 val)
  254. {  
  255.   UINT16 temp;  
  256.   temp = ((UINT16)(regs0->iop_data[2] & 0x00ff));
  257.   regs0->iop_data[2] = temp + (val*256);
  258. }
  259. // Increment Tx In Buffer
  260. void  I2C_Slave_Set_Tx_In_Ptr(UINT16 val)
  261.   UINT16 temp;
  262.   
  263.   temp = (UINT16)(regs0->iop_data[3] & 0xff00);
  264.   
  265.   regs0->iop_data[3] = (UINT16) (temp + val);
  266. }    
  267. // Return Receive Buffer Pointer Address
  268. UINT32 I2C_Slave_Rx_Buf_Ptr(void)
  269. {
  270.   return ((UINT32)(SDRAM_BASE_UNCACHED + regs0->iopya * 1024 + 0x588));
  271. }
  272. // Return Transmit Buffer Pointer Address
  273. UINT32 I2C_Slave_Tx_Buf_Ptr(void)
  274. {
  275.   //return ((UINT32)(SDRAM_BASE_UNCACHED + regs0->iopya * 1024 + 0x5C8));
  276.   return ((UINT32)(SDRAM_BASE_UNCACHED + regs0->iopya * 1024 + 0x608));
  277. }
  278. #if 0
  279. /* Get a data byte from the transmit buffer */
  280. UINT8 I2C_Slave_Tx_Buf_GetChar(void)
  281. {
  282.   UINT8 c = 0;  // NULL Character
  283.   
  284.   /* Check if transmit buffer has data to be retrieved */
  285.   if (I2C_Slave_Is_Tx_Buf_Empty() == FALSE)
  286.   {
  287.     //I2C_Slave_Get_Tx_Out_Ptr() = I2C_Slave_Get_Tx_Out_Ptr() + 1;
  288.     
  289.     
  290.     /* Allow roll-over */
  291.     //if (I2C_Slave_Get_Tx_Out_Ptr() > (MAX_I2C_SLAVE_TX_BUF_LEN-1))
  292.     //{
  293.     //  I2C_Slave_Get_Tx_Out_Ptr() = 0;
  294.     // }
  295.     /* Check if any character in the buffer */
  296.     //c = I2C_Slave_Tx_Buf[I2C_Slave_Get_Tx_Out_Ptr()];    
  297.     //c = *((UINT8 *)(SDRAM_BASE_UNCACHED + regs0->iopya * 1024 + 0x5C9));
  298.     c = *((UINT8 *)(SDRAM_BASE_UNCACHED + regs0->iopya * 1024 + 0x608));
  299.     
  300.   }  
  301.   
  302.   return c;
  303. }
  304. #endif
  305. #if 0
  306. /* Put a data byte to the receive buffer */
  307. int I2C_Slave_Rx_Buf_PutChar(UINT8 c)
  308. {
  309.   int result = FAILURE;
  310.   
  311.   if (c != 0)
  312.   {
  313.     /* Ensure enough space in receive buffer */
  314.     if (I2C_Slave_Is_Rx_Buf_Full() != TRUE)
  315.     {
  316.       I2C_Slave_Get_Rx_In_Ptr() = I2C_Slave_Get_Rx_In_Ptr() + 1;
  317.       
  318.       /* Allow roll-over */
  319.       if (I2C_Slave_Get_Rx_In_Ptr() > (MAX_I2C_SLAVE_RX_BUF_LEN-1))
  320.       {
  321.         I2C_Slave_Get_Rx_In_Ptr() = 0;
  322.       }
  323.       
  324.       /* Copy the data byte in */
  325.       I2C_Slave_Rx_Buf[I2C_Slave_Get_Rx_In_Ptr()] = c;
  326.       result = SUCCESS;
  327.     }
  328.   }
  329.   
  330.   return result;
  331. }
  332. #endif