Пример #1
0
void  SSP_isr(VOID) 
{
   state = i2c_isr_state ();

   IF ( (state == 0) || (state == 0x80))
   i2c_read () ;

   IF (state >= 0x80)
      i2c_write (buffer[0]) ;

   else IF (state > 0)
Пример #2
0
void ssp_interupt ()
{
  led_spp_on;
  state = i2c_isr_state();

 
  if(state < 0x80) //Master is sending data

  {

    if(state == 0)

    {
       buffer_pos = i2c_read();
    }

    if(state == 1)  //First received byte is address

    {

      buffer_pos = i2c_read();

     

    }

    if(state == 2)  //Second received byte is data

    {
 buffer_pos = i2c_read();
    

    }

  }

  if(state == 0x80)  //Master is requesting data

  {

    i2c_write (buffer_adc);

  }
led_spp_off;
}
Пример #3
0
void  SSP_isr(void) 
{
      state = i2c_isr_state();

      if((state== 0 ) || (state== 0x80)){
        i2c_read();
      }

      if(state >= 0x80){
         //response
      }

      else if(state > 0){
         buffer[state - 1] = i2c_read();
         if(buffer[state - 1]=='\0')
         {
            hasCommand= TRUE;
         }
      }

}
Пример #4
0
void ssp_interrupt()
{
   int state;
   int inByte;


   // Output of i2c_isr_state()
   //
   // 0         - Address match received with R/W bit clear, perform i2c_read( )
   //             to read the I2C address.
   // 1-0x7F    - Master has written data; i2c_read() will immediately return the data
   // 0x80      - Address match received with R/W bit set; perform i2c_read( ) to read
   //             the I2C address, and use i2c_write( ) to pre-load the transmit buffer
   //             for the next transaction (next I2C read performed by master will read
   //             this byte).
   // 0x81-0xFF - Transmission completed and acknowledged; respond with i2c_write() to
   //             pre-load the transmit buffer for the next transation (the next I2C
   //             read performed by master will read this byte).

   state = i2c_isr_state();

   //////////////////////////////////////////////////////////////////
   //  Address match with bit 0 clear (master write to slave)
   //////////////////////////////////////////////////////////////////
   if (state == 0) {
      i2c_read();  // flush the device address in the rx buffer
      

      // Fix any errors in the I2C communication
      // if wrong state -> there must have been an error in the i2c comm
      if (slaveState1 != READY_FOR_ADDRESS) {
         // clear the error flag registers and re-enable the i2c bus
         SSPEN = 0;   // disable i2c
         SSPOV = 0;   // clear the receive overflow flag
         WCOL = 0;    // clear the write collision flag

         SSPEN = 1;   // re-enable i2c

         slaveState1 = READY_FOR_ADDRESS; // reset the state
   

         return;

      }
   
      // if no error -> move forward to the next state
      slaveState1=READY_FOR_CMD;     //device moves into command mode

   }

   //////////////////////////////////////////////////////////////////
   //  a byte has been recieved from the Master
   //////////////////////////////////////////////////////////////////

   else if (state < 0x80) {

         inByte=i2c_read();

         switch (slaveState1) {

            case READY_FOR_CMD:

               switch (inByte) {
               
                  case CMD_RECEIVE_A_BYTE_FROM_MASTER:
                     slaveState1=READY_FOR_NUMBER;  
                     break;
                  case CMD_SEND_TWO_BYTES_TO_MASTER:
                     slaveState1=READY_TO_SEND; 
                     break;

                  // reset the state if received an
                  // unknown command
                  default:
                     slaveState1 = READY_FOR_ADDRESS;
                     break;
               }

               break;

            case READY_FOR_NUMBER:
               valFromMaster=inByte;
               slaveState1=READY_FOR_ADDRESS;
               break;

            // reset the state if unknown state is detected
            default:
               slaveState1=READY_FOR_ADDRESS;
               break;

         }

   }

   //////////////////////////////////////////////////////////////////
   //  Address match with bit 0 set (slave write to master)
   //////////////////////////////////////////////////////////////////

   else if (state == 0x80) {


      i2c_read();  // flush the address byte
                     
      if (slaveState1 == READY_TO_SEND) {

            i2c_write(0x61);
            slaveState1=READY_TO_SEND_BYTE_2;
      }

   }

   //////////////////////////////////////////////////////////////////
   //  Master is ready to receive the next byte from slave
   //////////////////////////////////////////////////////////////////

   else if (state > 0x80) {

      if (slaveState1 == READY_TO_SEND_BYTE_2) {
            //i2c_write(222);
           
            char buf [32];
            memset(buf, 0, 32);
            
            vTemp = vTemp/100;
            vHumid = vHumid/100;
            
            //PC changed /100 from 1000 1:21
            vSonic = vSonic/16;
            vSonic = vSonic/100;
            vSonic = 6-vSonic; 
            
            //vTemp = 3.2;
            sprintf(buf, "T=%.1f&H=%.1f&S=%.2f", vTemp, vHumid, vSonic);
            int len = 20;//strlen(buf);
            int i;
            for(i = 0; i < len; i += 1)
            {
               i2c_write(buf[i]);
            }
            i2c_write('\0');
            i2c_write('\0');
            slaveState1=READY_FOR_ADDRESS;
            sentCount++;
      }
   }


}
Пример #5
0
void  ssp2_isr(void)
{
	int state;

	state = i2c_isr_state(I2C_Slave);
    if((state==0) || (state== 0x80))														//I2C address byte read and ignore
        i2c_read(I2C_Slave);
    if(state==1){									    									//if state == 1 then this is the word address (Host Command written) 
		rcv_buffer[state - 1] = i2c_read(I2C_Slave);
		switch(rcv_buffer[0]){
			case 0x01:																		//Hard reset of board.  Power cycle all except CMC microController
			{	
				printf("\r\n3715 I2C Hard Reset 0x%x\n", rcv_buffer[0]);					
				SystemReset(1);
				break;
			}
			case 0x02:																		//Back end reset of 3219 and 3218
			{	
				printf("\r\n3715 I2C Reset Back End 0x%x\n", rcv_buffer[0]);
				output_low(RST3219n);													
				delay_ms(200);
				output_high(RST3219n);													
				break;
			}
			default: 																		//Host issued command that doesn't exist..do nothing
			{
				break;
			}
		}
	}
	if(state >= 0x80){
		if (I2CReadEnable) {
		//This is disabled at the moment.  3715 not supporting clock stretching so read function not working.																	
	    	switch(rcv_buffer[0]){   
		 		case 0xA1:																	//Master asking for status data...write out status byte LSB first (2 bytes)
				{
					i2c_write(I2C_Slave, ((StatByte >> ((state-0x80)*8)) & 0xff));
					break;
				}
				case 0xA2:																	//Master asking for temp data...write out status byte LSB first (7 bytes)
				{
					i2c_write(I2C_Slave,temp_buffer[state-0x80]);							//This will write out 7 bytes.  Temp format is signed 8 bits
					break;
				}
				case 0xA3:																	//Master asking for voltage data...write out status byte LSB first (13 bytes)
				{
					i2c_write(I2C_Slave,voltage_buffer[state-0x80]);						//This will write out 13 bytes.  Voltage format is .02V/LSB
					break;
				}
				case 0xA4:																	//Master asking for current data...write out status byte LSB first (13 bytes)
				{
					i2c_write(I2C_Slave,current_buffer[state-0x80]);						//This will write out 13 bytes of the voltage across the lower fet.  
					break;																	//User must know RDSon to calculate current..very low accuracy
				}	
				default:
				{
					i2c_write(I2C_Slave, 0x00);												//default response.  Host asked for command that doesn't exist
					break;
				}
			}
		} else printf("\r\nI2C read from micro disabled");
	}