Exemplo n.º 1
0
int16_t i2cm_rcv(uint8_t slav_adress)
{
	int16_t data=-1;

	TWCR =(1<<TWINT) | (1<<TWEN)  | (1<<TWSTA);
	while(!( TWCR & (1<<TWINT) ) );// wait for SLA+R ack
	
	TWDR = (slav_adress << 1) | 1 ; /* addr=1 + read */ 
	TWCR =	(1<<TWINT) | (1<<TWEN) ;
	while(! ( TWCR & (1<<TWINT) ) );

	if(TW_STATUS == TW_MR_SLA_ACK) { 
		TWCR = 	(1<<TWINT) | (1<<TWEN) ;
		while(! ( TWCR & (1<<TWINT) ) );
		
		if(TW_STATUS == TW_MR_DATA_NACK) { /* nack because last byte */
		data = TWDR;
		}
		TWCR =  (1<<TWINT) | (1<<TWEN) | (1<<TWSTO) ;//transmit stop condition
		wait_4cyc(100);
		}
		else {
		  TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO) ;// transmit stop condition
		  wait_4cyc(100);
		}
	return data;
}
Exemplo n.º 2
0
void recv(void)
{
	uint8_t i;
	uint16_t c=0;
	uint16_t d=0;
	data_Z();
	clk_Z();
	if (read_data())
		printf("burp\r\n");
/* 	wait_4cyc(WAIT_KBD_CYCLE4); */
/* 	wait_4cyc(WAIT_KBD_CYCLE4); */
/* 	wait_4cyc(WAIT_KBD_CYCLE4); */
	clk_0();
	wait_4cyc(WAIT_KBD_CYCLE4);
	
	
	for (i=0; i<8 ; i++) {
		if (read_data()) 
			c |= 1 << i;
		clk_Z();
		wait_4cyc(WAIT_KBD_CYCLE4);
		if (read_data()) 
			d |= 1 << i;
		clk_0();
		wait_4cyc(WAIT_KBD_CYCLE4);
	}

	// parite
	clk_Z();
	wait_4cyc(WAIT_KBD_CYCLE4);
	clk_0();
	wait_4cyc(WAIT_KBD_CYCLE4);

	// ack
	clk_Z();
	data_0();
	wait_4cyc(WAIT_KBD_CYCLE4);
	clk_0();
	wait_4cyc(WAIT_KBD_CYCLE4);

	// stop
	clk_Z();
	data_Z();
	wait_4cyc(WAIT_KBD_CYCLE4);
	clk_0();
	wait_4cyc(WAIT_KBD_CYCLE4);
	clk_Z();
	
	printf("%x\r\n", c);
	printf("%x\r\n", d);
	wait_4cyc(2*WAIT_KBD_CYCLE4);
	while(1);
}
Exemplo n.º 3
0
// Receive a frame from an address
// Slave must respect the i2c_ryder protocol : first byte tells if there are
// data to transmit or not
// Return number of read bytes
uint8_t i2cm_rcv(uint8_t slave_addr, uint8_t n, uint8_t* data)
{
  int i;

  I2C_START();

  // Slave address + Read bit (1)
  I2C_SEND((slave_addr<<1)+1);

  // No ACK, no data
  if( TW_STATUS != TW_MR_SLA_ACK )
  {
    I2C_STOP();
    wait_4cyc(100);
    return 0;
  }

  // First byte: is there something to transmit?
  I2C_ACK();
  if( TWDR == 0 )
  {
    I2C_NACK();
    I2C_STOP();
    wait_4cyc(100);
    return 0;
  }

  // Read all data
  for( i=0; i<n; i++ )
  {
    I2C_ACK();
    data[i] = TWDR;
  }

  I2C_NACK();
		
  I2C_STOP();
  wait_4cyc(100);

  return 1;
}
Exemplo n.º 4
0
// Send a n-byte data frame to an address
void i2cm_send(uint8_t slave_addr, uint8_t n, const uint8_t* data)
{
  int i;
  I2C_START();

  // Slave address + Write bit (0)
  I2C_SEND(slave_addr<<1);

  // Data
  for( i=0; i<n; i++ )
  {
    I2C_SEND(data[i]);
  }

  // Stop and wait
  I2C_STOP();
  wait_4cyc(100);
}
Exemplo n.º 5
0
// Receive one data byte from an address, -1 on error
int i2cm_rcv_single(uint8_t slave_addr)
{
  I2C_START();

  // Slave address + Read bit (1)
  I2C_SEND((slave_addr<<1)+1);

  // No ACK, no data
  if( TW_STATUS != TW_MR_SLA_ACK )
  {
    I2C_STOP();
    wait_4cyc(100);
    return -1;
  }

  // First byte: is there something to transmit?
  I2C_ACK();
  return TWDR;
}
Exemplo n.º 6
0
void i2cm_send(uint8_t adress, uint8_t data)
{
	TWCR = (1<<TWINT) | (1<<TWEN)  | (1<<TWSTA);
	while(! ( TWCR & (1<<TWINT) ) );
	

	TWDR = (adress << 1) | 0  ; /* addr=1 + write */ 
	TWCR = (1<<TWINT) | (1<<TWEN) ;
	while(! ( TWCR & (1<<TWINT) ) );//wait for ack
	

	TWDR = data;
	TWCR = (1<<TWINT) | (1<<TWEN) ;// send data
	while(! ( TWCR & (1<<TWINT) ) );// wait for ack


	TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO) ;
	wait_4cyc(100);
}