/* ----- file I2CHard.c66 ----- */



/* Use it for interfacing the devices connected to the I2C-bus */
/* The functions of this file must be adapted to the underlaying hardware */



#include "MISC.H"
#include <REG167.H>



/****************************************************************************/
/* realizes the generic I2C-bus delay                                       */
/* Programmed as timing loop for maximum speed of                           */
/* 80C166 (40MHz,16-bit,zero wait-state)                                    */
/* The I2C-bus does not need a time-out condition, so                       */
/* slowing down the 80C166 makes no problem                                 */

void I2CDelay(void) {
   BYTE  Count;                        /* loop-counting variable            */



   for(Count = 0; Count < 25; Count++);
}

/****************************************************************************/
/* set SCL-line to desired state (SCL= port-pin P2.1)                       */

void I2CPutSCL(BYTE State) {



   if(!State) {
      P2 &= 0xFFFD;                    /* set SCL-line to ZERO              */
      DP2 |= 0x0002;                   /* set port-pin SCL for output mode  */
                                       /* enable output AFTER setting of    */
                                       /* correct level to avoid '1'-level  */
                                       /* spikes                            */
   }
   else {
      DP2 &= 0xFFFD;                   /* set port-pin SCL for input mode   */
                                       /* this is possible because of the   */
                                       /* external pull-up resistor !       */
      P2 |= 0x0002;                    /* set SCL-line to ONE               */
   }
}

/****************************************************************************/
/* read state of  SCL-line (SCL= port-pin P2.1)                             */
/* WARNING: This function should be used only if SCL-line is in High-level !*/
/* (This function is for used only for realizing the Slow-Down-Mode)        */

BYTE I2CGetSCL(void) {



   DP2 &= 0xFFFD;                      /* set port-pin SCL for input mode   */

   if((P2 & 0x0002))
      return(1);                       /* read SCL-line status              */
   else
      return(0);
}

/****************************************************************************/
/* set SDA-line to desired state (SDA= port-pin P2.2) */

void I2CPutSDA(BYTE State) {



   if(!State) {
      P2 &= 0xFFFB;                    /* set SDA-line to ZERO              */
      DP2 |= 0x0004;                   /* set port-pin SDA for output mode  */
                                       /* enable output AFTER setting of    */
                                       /* correct level to avoid '1'-level  */
                                       /* spikes                            */
   }
   else {
      DP2 &= 0xFFFB;                   /* set port-pin SDA for input mode   */
                                       /* this is possible because of the   */
                                       /* external pull-up resistor !       */
      P2 |= 0x0004;                    /* set SDA-line to ONE               */
   }
}


/****************************************************************************/
/* read state of SDA-line (SDA= port-pin P2.2)                              */

BYTE I2CGetSDA(void) {



   DP2 &= 0xFFFB;                      /* set port-pin SDA for input mode   */

   if((P2 & 0x0004))
      return(1);                       /* read SDA-line status              */
   else
      return(0);
}

/****************************************************************************/

/* ----- end I2C_HARD.C66 ----- */