/*
;**********************************************************************
* Try and get an iic slave to work 
*
* Connect 16F886 to IIC bus and send commands to it.
* 
* [ raspberry pi ]===[ levelshifter ]====[ PIC16F886 ]
* In the raspberry pi you can use these commands
*
* i2cdetect -y 1
* i2cset -y 1 0x08 3 3 w
* i2cget -y 1 0x08 3 w
*
*
;**********************************************************************
There are a number of C compilers. 
HI TECH for 10F,12F and 16 chips
MCC - 18F
These have been replaced by the xc range

C:\Program Files (x86)\HI-TECH Software\PICC\9.70\samples\i2c
C:\Program Files (x86)\HI-TECH Software\PICC\9.70\include

Try out HI-TECH for 16F886 chips. 
;**********************************************************************
*/


/*
	; Apply IIC start
	; SCL on RB4
	; SDA on RB1

	; Apply IIC start
    ; on PIC16F886
	; SCL on RC3
	; SDA on RC4

	; SCL ___/--\___
	; SDA _/------\_____

LEDs on RB0 to RB3

Serial Analyser connector on PIC16F886 board 
  TX
  5V
  GND
  SDA
  SCL
  RX

*/

#include <htc.h>
	__CONFIG( DEBUGDIS & WDTDIS & BORDIS & PWRTEN  & XT & MCLREN & LVPDIS );
void iicSlave( void );

char iic_slave_addr;
char iic_slave_data;

char b 			= 0x55;
char counter 	= 0x0;
char iic_state 	= 0x0;

/* *********************************** */

#define SCLt TRISC3
#define SDAt TRISC4
#define SCLp PORTC3
#define SDAp PORTC4

#define SCL 3
#define SDA 4

// call back functions read / write 
char iicSlaveRead( void ){
  //return counter and increment	
  return counter++;
}

char iicSlaveNACK( void ){
  // fig 13.8  
  return counter;
}


void iicSlaveWrite( char data ){
  // this needs a state machine
  // memory chips often have the first byte or two as address
  // start,addr,data,data,stop
  // 
  iic_state++; 
  if ( iic_state == 1 ){
    //counter = data;
  }
  return;
}

void iicSlave( void ){
  SSPOV = 0;
  WCOL  = 0;
  if ( START ){ 
  } 
    if ( ! DA ){ // ADDR
      iic_state == 0;
      if ( BF ){
	    // the read below is required.
	    NOP();		
	    NOP();		
	    NOP();		
	    NOP();		
        iic_slave_addr = SSPBUF;
	    NOP();		
	    NOP();		
	    NOP();		
	    NOP();		
      } 
      // write
      // I was getting a 0x22 on reads. this is the Address shifted one bit to the left
      // I am guessing that it is runt data
      // if the R bit is set, send a byte  
	  if ( RW ){
	    NOP();		
		SSPBUF = iicSlaveRead();
		NOP();
        CKP = 1;
	  }   
      SSPIF = 0;
	  NOP();

      return;
    } else { // data
	   NOP();
	   NOP();
	   NOP();
	   NOP();

       if ( BF ){
		 NOP();
	     NOP();
	     NOP();
		 NOP();
	     iic_slave_data = SSPBUF;
         iicSlaveWrite( iic_slave_data );
         PORTB = iic_slave_data;
		 NOP();
       }

	   if ( RW ){  // write
        NOP();        
		if ( CKP ){
		  // NACK - fig 13.8  
		  iicSlaveNACK( );
		} else {
          SSPBUF = iicSlaveRead() ;
		/* 
		really need to check ACK before incrementing counter 
		DATA sheet does not say how to test for NACK for slave.
		CKP is set to LOW if last byte was ACK, else CKP remains at 1 in 
		diagram 13.8
		This seems to work		
		*/
		
        NOP();
        CKP = 1;
      }
      SSPIF = 0;
	  NOP();
    }
    if ( STOP ){
      // reset iic 
    SSPCON = 0x0;
    SSPADD = 0x10;
    SSPCON = 0x36;
	CKP = 1;
    SSPIF = 0;
    SSPOV = 0;
    return;
    }
  SSPIF = 0;
  SSPOV = 0;
  }
}

void iic_init(void){
//	; Set address to 0x10
    TRISC |=  (1<<SCL | 1<<SDA );
    SSPCON = 0x0;
    SSPADD = 0x10;
    SSPCON = 0x36;

    //
    // enable clock stretching
    // 
    SSPOV = 0;
    WCOL  = 0;
	SEN = 0;
    CKP = 1;
	return;
}

void main( void ){
  // char b=0x55;
  char 	RAMTRIS,	//			; this is a baseline part so I have to create my 
		Flag,		//		; contains misc flags
		TMRlast,
		TMRnew,
		IPlast,
		IPnew,
		IPevents;

  #if defined(_10F204) || defined(_10F206)
  CMCON0 = 0x70;
  #endif

  OPTION = 0b01000011 ; //1:32TMR0 prescaler: 1E-6 x 256 x 256, weak pullups disabled
  TRISB  = 0xF0;
  PORTB  = 0x55;
  IPevents =0;

  // init 
  iic_init();
  
  while ( 1==1 ){
  
    if ( SSPIF ){
      iicSlave();
      //SSPIF = 0;
    }

    TMRlast	= TMRnew;
    TMRnew	= TMR0;
    IPevents |= ( TMRlast ^ TMRnew )& 0xF0;
    IPlast  = IPnew;
    IPnew   = PORTB;
    IPevents |= ( IPlast ^ IPnew ) & 0x0F;

    if ( IPevents & 0x80 ){ 
     // PORTB = b;
      b++;
      IPevents &= ~0x80;
    }
  }
}
