Ejemplo n.º 1
0
uint16_t read16(uint8_t address) {
	uint8_t err, pec;
	uint16_t ret = 0;

	i2c_start_wait(MLX90614_I2CADDR << 1);

	err = i2c_write(address); // send register address to read from
	if (err > 0) {
		return 0;
	}

	err = i2c_start((MLX90614_I2CADDR << 1) + I2C_READ);
	if (err > 0) {
		return 0;
	}
	
	ret = i2c_read_ack();
	ret |= i2c_read_ack() << 8;
	pec = i2c_read_nack();
	
	i2c_stop();

	uint8_t commArray[5] = {MLX90614_I2CADDR << 1,
		address,
		(MLX90614_I2CADDR << 1) + I2C_READ,
		ret & 0xFF,
		(ret >> 8) & 0xFF
	};

	uint8_t crc = crc8_ccitt(commArray, 5);

	if (pec != crc) { // PEC error
		return 0;
	}

	return ret;
}

int32_t readTemp(uint8_t reg) {
	uint16_t reading = read16(reg);
	
	if (reading == 0) {
		return -1;
	}
	
    int32_t temp = ((int32_t) reading) * 100;

    temp /= 50;
    temp -= 27315;

    return temp;
}
Ejemplo n.º 2
0
i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout)
{
  i2c_status_t status = i2c_start(address | I2C_READ, timeout);
  if (status) return status;

  for (uint16_t i = 0; i < (length-1); i++) {
    status = i2c_read_ack(timeout);
    if (status >= 0) {
      data[i] = status;
    } else {
      return status;
    }
  }

  status = i2c_read_nack(timeout);
  if (status >= 0 ) {
    data[(length-1)] = status;
  } else {
    return status;
  }

  status = i2c_stop(timeout);
  if (status) return status;

  return I2C_STATUS_SUCCESS;
}
Ejemplo n.º 3
0
/**
 * Reads the values of the RC channels from the motorcontrol unit
 *
 * @param channel_a Value of channel_a
 * @param channel_b Value of channel_b
 * @param channel_c Value of channel_c
 * @param channel_d Value of channel_d
 * @return If the communication was succesful
 */
uint8_t motorcontrol_get_rc_channels(uint8_t *channel_a, uint8_t *channel_b, uint8_t *channel_c, uint8_t *channel_d) {
#ifdef MOTORCONTROL_AVAILABLE
    if (i2c_start(I2C_ADDR_MOTORCONTROL + I2C_WRITE)) {
        i2c_write(MC_RC_CHANNEL_1);
        if (i2c_rep_start(I2C_ADDR_MOTORCONTROL + I2C_READ)) {
            *channel_a = i2c_read_ack();
            *channel_b = i2c_read_ack();
            *channel_c = i2c_read_ack();
            *channel_d = i2c_read_nak();
            i2c_stop();
            return 1;
        }
    }
#endif /* MOTORCONTROL_AVAILABLE */
    return 0;
}
Ejemplo n.º 4
0
i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout)
{
  i2c_status_t status = i2c_start(devaddr, timeout);
  if (status) return status;

  status = i2c_write(regaddr, timeout);
  if (status) return status;

  status = i2c_start(devaddr | 0x01, timeout);
  if (status) return status;

  for (uint16_t i = 0; i < (length-1); i++) {
    status = i2c_read_ack(timeout);
    if (status >= 0) {
      data[i] = status;
    } else {
      return status;
    }
  }

  status = i2c_read_nack(timeout);
  if (status >= 0 ) {
    data[(length-1)] = status;
  } else {
    return status;
  }

  status = i2c_stop(timeout);
  if (status) return status;

  return I2C_STATUS_SUCCESS;
}
Ejemplo n.º 5
0
void sensor_get(uint8_t channel, uint16_t* values)
{
	uint8_t i;
	mux_select(channel);
	i2c_start(SENSOR_READ);
	for(i = 0; i < 4; i++) {
		if (i < 3) {
			values[i] = i2c_read_ack();
			values[i] |= (i2c_read_ack() << 8);
		}
		else if (i == 3) {
			values[i] = i2c_read_ack();
			values[i] = (i2c_read_nack() << 8);
		}
	}
	i2c_stop();
}
Ejemplo n.º 6
0
/*
 * Function:  measureRH()
 * --------------------
 * Used to send the RH measurement command and read the result 
 */
void measureRH() {
    i2c_start();
    send_i2c_byte(SI7020ADR_W); // address +0 write
    send_i2c_byte(MEASURE_HUM_HOLD_CMD); //0xE5 =0b11100101 
    i2c_repeatedStart();
    send_i2c_byte(SI7020ADR_R); //adresss +1 read
    i2c_mIdleI2C1();
    hhLSB = i2c_read_ack();
    i2c_ack();
    i2c_mIdleI2C1();
    i2c_ack();
    hhMSB = i2c_read_ack();
    i2c_mIdleI2C1(); /////////////////////// maybe?
    i2c_nack();
    i2c_stop();

    humidityCode.b[0] = hhLSB;
    humidityCode.b[1] = hhMSB;
}
Ejemplo n.º 7
0
/*
 * Init function for the light sensor. the sensor has to be activated by 0x3 (see p. 14)
 * As integration time 402ms will be used (p.15)
 */
void initLightSensorI2C(void) {
	/* ENABLE DEVICE to set the configuration */
	i2c_start(LIGHTSENSOR_I2C, SLAVE_ADDRESS << 1, I2C_Direction_Transmitter); 		// start a transmission in Master transmitter mode
	i2c_write(LIGHTSENSOR_I2C, 0xA0); 																			// 10010000 - // Command Register (0x00)  CommandBit (0x80), Word protocol
	i2c_write(LIGHTSENSOR_I2C, 0x03); 																			// Power up the device (0x3)
	i2c_stop(LIGHTSENSOR_I2C); 																					// stop the transmission

	i2c_start(LIGHTSENSOR_I2C, SLAVE_ADDRESS << 1, I2C_Direction_Receiver); 			// start a transmission in Master transmitter mode
	received_data[0] = i2c_read_ack(LIGHTSENSOR_I2C); 												// 0x03 returns if the controller is active
	received_data[1] = i2c_read_nack(LIGHTSENSOR_I2C); 											// read one byte and request another byte
}
Ejemplo n.º 8
0
/*
 * Function:  measureT()
 * --------------------
 * Used to send the temperature measurement command and read the result 
 */
void measureT() {
    i2c_start();
    send_i2c_byte(SI7020ADR_W); // address +0 write
    send_i2c_byte(MEASURE_TEMP_HOLD_CMD); //0xE3 =0b11100011
    i2c_repeatedStart();
    //i2c_start(); 
    send_i2c_byte(SI7020ADR_R); //adresss +1 read
    i2c_mIdleI2C1();
    ttLSB = i2c_read_ack();
    i2c_ack();
    i2c_mIdleI2C1();
    i2c_ack();
    ttMSB = i2c_read_ack();
    i2c_mIdleI2C1(); /////////////////////// maybe?
    i2c_nack();
    i2c_stop();
 
    temperatureCode.b[0] = ttLSB;
    temperatureCode.b[1] = ttMSB;
}
Ejemplo n.º 9
0
/*
 * Function:  getLastTemp()
 * --------------------
 * Used to retrieve last temperature measurement, idea is to save some time on 
 * conversion-> because SI7020 does relative humidity(RH) measurement it also measures 
 * temperature. 
 */
void getLastTemp() {
    i2c_start();
    send_i2c_byte(SI7020ADR_W); // address +0 write
    send_i2c_byte(READ_LAST_TEMP_CMD);
    i2c_repeatedStart();
    //i2c_start();  
    send_i2c_byte(SI7020ADR_R); //adresss +1 read
    i2c_mIdleI2C1();
    ttLSB = i2c_read_ack();
    i2c_ack();
    i2c_mIdleI2C1();
    i2c_ack();
    ttMSB = i2c_read_ack();
    i2c_mIdleI2C1(); /////////////////////// maybe?
    i2c_nack();
    i2c_stop();

    temperatureCode.b[0] = ttLSB;
    temperatureCode.b[1] = ttMSB;
}
Ejemplo n.º 10
0
uint8_t i2c_receive(uint8_t address, uint8_t *data, uint16_t length) {
	if (i2c_start((uint8_t) (address | I2C_READ))) return 1;

	for (uint16_t i = 0; i < (length - 1); i++) {
		data[i] = i2c_read_ack();
	}
	data[(length - 1)] = i2c_read_nack();

	i2c_stop();

	return 0;
}
Ejemplo n.º 11
0
/**
 * Set the speed of the motors and reads the values of the RC channels from the motorcontrol unit
 *
 * @param motor_a Speed of motor 1
 * @param motor_b Speed of motor 2
 * @param motor_c Speed of motor 3
 * @param motor_d Speed of motor 4
 * @param channel_a Value of channel_a
 * @param channel_b Value of channel_b
 * @param channel_c Value of channel_c
 * @param channel_d Value of channel_d
 * @return If the communication was succesful
 */
uint8_t motorcontrol(uint8_t motor_a, uint8_t motor_b, uint8_t motor_c, uint8_t motor_d, uint8_t *channel_a, uint8_t *channel_b, uint8_t *channel_c, uint8_t *channel_d) {
#ifdef MOTORCONTROL_AVAILABLE
    if (i2c_start(I2C_ADDR_MOTORCONTROL + I2C_WRITE)) {
        /* Write the motor value */
        i2c_write(MC_MOTOR_1_SPEED);
        i2c_write(motor_a);
        i2c_write(motor_b);
        i2c_write(motor_c);
        i2c_write(motor_d);
        if (i2c_rep_start(I2C_ADDR_MOTORCONTROL + I2C_READ)) {
            /* Read the RC channels */
            *channel_a = i2c_read_ack();
            *channel_b = i2c_read_ack();
            *channel_c = i2c_read_ack();
            *channel_d = i2c_read_nak();
            i2c_stop();
            return 1;
        }
    }
#endif /* MOTORCONTROL_AVAILABLE */
    return 0;
}
Ejemplo n.º 12
0
uint8_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t *data, uint16_t length) {
	if (i2c_start(devaddr)) return 1;

	i2c_write(regaddr);

	if (i2c_start((uint8_t) (devaddr | 0x01))) return 1;

	for (uint16_t i = 0; i < (length - 1); i++) {
		data[i] = i2c_read_ack();
	}
	data[(length - 1)] = i2c_read_nack();

	i2c_stop();

	return 0;
}
Ejemplo n.º 13
0
uint16_t i2c_read_reg16(uint8_t address, uint8_t reg) {
    uint16_t result;
    address <<= 1;
    i2c_start();
    if (i2c_write(address) != 0) { // address in write mode 
        i2c_stop();
        return 0xffff;
    }
    i2c_write(reg);
    i2c_start();
    i2c_write(address|1); // address in read mode
    result = i2c_read_ack(); // read high byte
    result =result << 8;
    result |= i2c_read_noack(); // read low byte
    i2c_stop();
    return result;
}
Ejemplo n.º 14
0
static unsigned char i2c_write_byte(unsigned char data)  
{  
    
    int i;
    unsigned char state;  
  
    for (i=7; i>=0; i--) {  
        GPIOPIN_Set_Fast( GPIO_SCL, GPIO_I2C_SCL_LOW );
        GPIOPIN_Set_Fast( GPIO_SDA, data & (1<<i) ? GPIO_I2C_SDA_HIGH : GPIO_I2C_SDA_LOW );        //从高位到低位依次准备数据进行发送        
        delay(); 
        GPIOPIN_Set_Fast( GPIO_SCL, GPIO_I2C_SCL_HIGH );                    
        delay(); 
    }  
    state = i2c_read_ack();       
    return state;
    
}  
Ejemplo n.º 15
0
void cell_adcReadings(void)
{
	uint8_t c = cellAddress_MIN;
	//read adc results and store them to adcReadings array
	for (uint8_t x = 0; x < cellNumber; x++)
	{
		//Set i2c address pointer to adc readings memory
		i2c_start(c+I2C_WRITE);
		i2c_write(cellMemory_adc);
		i2c_stop();
		//Start reading adc information and write to array
		i2c_start(c+I2C_READ);
		adcReadings[x] = i2c_read_ack();
		adcReadings[x] |= (i2c_read_nack()<<8);
		i2c_stop();
		c+=2;
	}
}
Ejemplo n.º 16
0
int16_t mpu_read_accel_y(){
	uint16_t accel_y;
	int16_t ay;

	i2c_start(address,write);
	i2c_write(accel_y_H);

	i2c_start(address,read);

	accel_y = ((uint8_t)i2c_read_ack())<<8;
	accel_y |= i2c_read_nack();

	i2c_stop();
	
	ay = zweierkompliment(accel_y);

	return ay;
}
Ejemplo n.º 17
0
int16_t mpu_read_gyro_y(){
		uint16_t gyro_y;
		int16_t gy;

		i2c_start(address,write);
		i2c_write(gyro_y_H);

		i2c_start(address,read);

		gyro_y = ((uint8_t)i2c_read_ack())<<8;
		gyro_y |= i2c_read_nack();

		i2c_stop();
		
		gy = zweierkompliment(gyro_y);

		return gy;
 }
Ejemplo n.º 18
0
int16_t mpu_read_accel_z(){
	uint16_t accel_z;
	int16_t az;

	i2c_start(address,write);
	i2c_write(accel_z_H);

	i2c_start(address,read);

	accel_z = ((uint8_t)i2c_read_ack())<<8;
	accel_z |= i2c_read_nack();

	i2c_stop();
		  
	az = zweierkompliment(accel_z);

	return az;
}
Ejemplo n.º 19
0
 int16_t mpu_read_gyro_z(){
	  uint16_t gyro_z;
	  int16_t gz;

	  i2c_start(address,write);
	  i2c_write(gyro_z_H);

	  i2c_start(address,read);

	  gyro_z = ((uint8_t)i2c_read_ack())<<8;
	  gyro_z |= i2c_read_nack();

	  i2c_stop();
	  
	  gz = zweierkompliment(gyro_z);

	  return gz;
}
Ejemplo n.º 20
0
int16_t mpu_read_accel_x(){
	uint16_t accel_x;
	int16_t ax;

	i2c_start(address,write);
	i2c_write(accel_x_H);

	i2c_start(address,read);

	accel_x = ((uint8_t)i2c_read_ack())<<8;
	accel_x |= i2c_read_nack();

	i2c_stop();
	
	ax = zweierkompliment(accel_x);

	return ax;
}
Ejemplo n.º 21
0
 //read the low and and the high byte of the gyro in the mcu
 int16_t mpu_read_gyro_x(){

	uint16_t gyro_x;
	int16_t gx;

	i2c_start(address,write);
	i2c_write(gyro_x_H);

	i2c_start(address,read);

	gyro_x = ((uint8_t)i2c_read_ack())<<8;
	gyro_x |= i2c_read_nack();

	i2c_stop();

	gx = zweierkompliment(gyro_x);
	
	return gx;	
 }
Ejemplo n.º 22
0
float mpu__read_temp(){
	  uint16_t temp;
	  float temp_c;

	  i2c_start(address,write);
	  i2c_write(temp_H);

	  i2c_start(address,read);

	  temp = ((uint8_t)i2c_read_ack())<<8;
	  temp |= i2c_read_nack();

	  i2c_stop();

	  temp_c = temp / 340 + 36.53;
	  
	  return temp_c;

}
Ejemplo n.º 23
0
void collect_data_mpu(void) {
	i2c_start(MPU_6050_WRITE_ADDRESS);
	i2c_write(MPU_6050_REGISTER_ACCEL_XOUT_H);
	i2c_rep_start(MPU_6050_READ_ADDRESS);
	i2c_read_ack(&accelXH);
	i2c_read_ack(&accelXL);
	i2c_read_ack(&accelYH);
	i2c_read_ack(&accelYL);
	i2c_read_ack(&accelZH);
	i2c_read_ack(&accelZL);
	i2c_read_ack(&temperatureH);
	i2c_read_ack(&temperatureL);
	i2c_read_ack(&gyroXH);
	i2c_read_ack(&gyroXL);
	i2c_read_ack(&gyroYH);
	i2c_read_ack(&gyroYL);
	i2c_read_ack(&gyroZH);
	i2c_read_nack(&gyroZL);
	i2c_stop();
	accelX = (accelXH << 8) | accelXL;
	accelY = (accelYH << 8) | accelYL;
	accelZ = (accelZH << 8) | accelZL;
	temperature = (temperatureH << 8) | temperatureL;
	gyroX = (gyroXH << 8) | gyroXL;
	gyroY = (gyroYH << 8) | gyroYL;
	gyroZ = (gyroZH << 8) | gyroZL;
}
Ejemplo n.º 24
0
int main(void)
{
	
	//I2C設定
	i2c_init();
	_delay_ms(15);
	
	i2c_start(HMC5883L_WRITE);
	i2c_write(HMC5883L_CONFIG_A); 
	i2c_write(0x78); // 平均化8回 75Hz 通常測定
	i2c_stop();

	i2c_start(HMC5883L_WRITE);
	i2c_write(HMC5883L_CONFIG_B); 
	i2c_write(HMC5883L_GAIN);
	i2c_stop();

	i2c_start(HMC5883L_WRITE);
	i2c_write(HMC5883L_MODE); // set pointer to measurement mode
	i2c_write(0x00); // continous measurement
	i2c_stop();
	
	//入出力設定
	DDRB  =0b00010000; //SPI IR入力
	DDRD  =0b00001100; //トリガー IR電源 IR入力
	PORTD =0b00001000; //IR電源OFF
	
	
	//超音波関連
	
	//割り込み許可
	PCICR  =0b00000010;
	PCMSK1 =0b00001111;
	
	//タイマー
	TCCR0A = 0b00000000;
	TCCR0B = 0b00000101;
	
	//トリガー用タイマー初期化
	TCCR1A = 0b00000000;
	TCCR1B = 0b00000100;
	
	
	
	//SPI設定
	SPCR=0b11000000;
	SPDR=0;
	sei();
	_delay_ms(15);
    while (1) {
		int x_raw,y_raw;
		unsigned long ir[6]={0,0,0,0,0,0};
		float arg;
		//方位データ読み出し
		PORTD = 0b00000000;//IR電源ON
		i2c_start(HMC5883L_WRITE);
		i2c_write(0x03); 
		i2c_stop();

		i2c_start(HMC5883L_READ);
		
		data[X_H]=i2c_read_ack();
		data[X_L]=i2c_read_ack();
		data[Y_H]=i2c_read_ack();
		data[Y_L]=i2c_read_ack();
		data[Z_H]=i2c_read_ack();
		data[Z_L]=i2c_read_nack();
		
		x_raw=(data[X_H]<<8)+data[X_L];
		y_raw=(data[Z_H]<<8)+data[Z_L];
		
		x_raw= x_raw+X_OFFSET;
		y_raw= y_raw+Y_OFFSET;
		
		x_raw*= HMC5883L_M;
		y_raw*= HMC5883L_M;
		
		arg = atan2((double)x_raw,(double)y_raw);
		arg = arg/M_PI;
		data[ARG]=(char)(arg*(128));
	
		i2c_stop();
		
		for(int i=0;i<IR_CYCLE;i++){
			char d = PIND^0xFF;
			char b = PINB^0xFF;
			if(d & (1<<PIND4))ir[0]++;
			if(d & (1<<PIND5))ir[1]++;
			if(d & (1<<PIND6))ir[2]++;
			if(d & (1<<PIND7))ir[3]++;
			if(b & (1<<PINB0))ir[4]++;
			if(b & (1<<PINB1))ir[5]++;
			_delay_us(15);
		}
		PORTD = 0b00001000;//IR電源OFF
		data[IR0]=ir[0];
		data[IR1]=ir[1];
		data[IR2]=ir[2];
		data[IR3]=ir[3];
		data[IR4]=ir[4];
		data[IR5]=ir[5];
		
		int t =TCNT1L;//←よくわからないが入れると動く
		if(TCNT1H > 10 ){ //測距
			PORTD =0b00000100;
			_delay_us(1000);
			PORTD =0b00000000;
			TCNT1H =0;
			TCNT1L =0;
			TCNT0 =0;
		}
		
		_delay_ms(15);
		
    }
}
Ejemplo n.º 25
0
lc_err_t read_page(e2p_addr_t address, unsigned char *data, unsigned char size)
{
	register lc_err_t err_code;

	do {
		do {
			i2c_start();
			err_code = i2c_read_state_wait();
			if (err_code != I2C_MASTER_START && err_code != I2C_MASTER_REPSTART)
				break;

			i2c_slaW(0xa0);
			err_code = i2c_read_state_wait();
		} while (err_code == I2C_MASTER_SLAW_NACK);
		if (err_code != I2C_MASTER_SLAW_ACK)
			break;

		i2c_write(address >> 8);
		err_code = i2c_read_state_wait();
		if (err_code != I2C_MASTER_TXDATA_ACK)
			break;

		i2c_write(address);
		err_code = i2c_read_state_wait();
		if (err_code != I2C_MASTER_TXDATA_ACK)
			break;

		i2c_start();
		err_code = i2c_read_state_wait();
		if (err_code != I2C_MASTER_REPSTART)
			break;

		i2c_slaR(0xa0);
		err_code = i2c_read_state_wait();
		if (err_code != I2C_MASTER_SLAR_ACK)
			break;

		while (size > 1)
		{
			i2c_read_ack();
			err_code = i2c_read_state_wait();
			if (err_code != I2C_MASTER_RXDATA_ACK)
				break;

			*data++ = i2c_get_byte();
			--size;
		}
		if (err_code != I2C_MASTER_RXDATA_ACK)
			break;

		i2c_read_nack();
		err_code = i2c_read_state_wait();
		if (err_code != I2C_MASTER_RXDATA_NACK)
			break;

		*data = i2c_get_byte();
		err_code = 0;
	} while(0);

	i2c_stop();
	return err_code;
}
Ejemplo n.º 26
0
i2c_ack i2c_read_byte(int bus_id, char chip_addr, unsigned int sub_addr, char *buf, unsigned int size)
{
#ifdef FLY_VIDEO_BOARD_V3
    int ret = 0;
    //ret=i2c_api_do_recv(3,chip_addr, sub_addr , buf, size);
    ret = SOC_I2C_Rec(3, chip_addr, sub_addr , buf, size);
    //lidbg("\n************ i2c_read_byte =%d*******\n",ret);
    //if(ret==size){
    if(ret > 0)
    {
#ifdef  DEBUG_ACK
        return NACK;
#else
        return ACK;
#endif

    }
    else
    {
        return NACK;
    }
#else
    u8 i;
    mutex_lock(&io_i2c_lock);
    i2c_init();
    // start transmite
    i2c_begin();
    i2c_write_chip_addr(chip_addr, hkj_WRITE) ;
    if (i2c_read_ack () == NACK)
    {
        i2c_stop();
        i2c_free();
        lidbg("at read funtion :i2c_write_chip_addr(%.2x, %d) ; is not ACK\n", chip_addr, hkj_WRITE);
        mutex_unlock(&io_i2c_lock);
        return NACK;
    }

    //i2c_stop();
    // restart transmite
    //i2c_begin();

    // send message to mm_i2c device to transmite data
    i2c_write(sub_addr) ;
    if (i2c_read_ack () == NACK)
    {
        i2c_stop();
        i2c_free();
        lidbg("at read funtion :i2c_write (%.2x) is not ACK\n", sub_addr & 0xff);
        mutex_unlock(&io_i2c_lock);
        return NACK;
    }
    i2c_stop();

    // start transmite
    i2c_begin();
    i2c_write_chip_addr(chip_addr, hkj_READ) ;
    if (i2c_read_ack() == NACK)
    {
        i2c_stop();
        i2c_free();
        lidbg("at read funtion :i2c_write_chip_addr(%.2x, %d) ; is not ACK\n", sub_addr, hkj_READ);
        mutex_unlock(&io_i2c_lock);
        return NACK;
    }
    // transmite data
    for(i = 0; i < size; i++)
    {
        buf[i] = i2c_read();
        ( i == (size - 1) ) ? i2c_write_ask(NACK) : i2c_write_ask(ACK);
    }

    // stop transmite
    i2c_stop();
    i2c_free();
    mutex_unlock(&io_i2c_lock);
    return ACK;
#endif
}
Ejemplo n.º 27
0
i2c_ack i2c_write_byte(int bus_id, char chip_addr, char *buf, unsigned int size)
{
#ifdef FLY_VIDEO_BOARD_V3
    int ret_send = 0;
    ret_send = SOC_I2C_Send(3,  chip_addr, buf, size);
    // lidbg("\n************i2c_write_byte =%d*******\n",ret_send);
    if(ret_send > 0)
    {
#ifdef  DEBUG_ACK
        return NACK;
#else
        return ACK;
#endif
    }
    else
    {
        return NACK;
    }
#else
    u8 i;
    mutex_lock(&io_i2c_lock);
    //lidbg("i2c:write byte:addr =0x%.2x value=0x%.2x ",buf[0],buf[1]);
    i2c_init();

    // start transmite
    i2c_begin();

    i2c_write_chip_addr(chip_addr, hkj_WRITE) ;
    if (i2c_read_ack() == NACK)
    {
        i2c_stop();

        i2c_free();
        lidbg(" at write funtion: i2c_write_chip_addr(%.2x, %d) ; is not ACK\n", chip_addr, hkj_WRITE);
        mutex_unlock(&io_i2c_lock);
        return NACK;
    }


    //i2c_stop();
    // restart transmite
    //i2c_begin();

    // send message to mm_i2c device to transmite data
    i2c_write(buf[0]) ;
    if (i2c_read_ack() == NACK)
    {
        i2c_stop();

        i2c_free();
        lidbg(" at write funtion:i2c_write(%.2x) ;is not ACK\n", buf[0]);
        mutex_unlock(&io_i2c_lock);
        return NACK;
    }
    // transmite data
    for(i = 1; i < size; i++)
    {
        i2c_write(buf[i]);
        if( i2c_read_ack()  == NACK)
        {
            i2c_stop();
            i2c_free();
            lidbg(" at write funtion:i2c_write(%.2x) ;is not ACK\n", buf[i]);
            mutex_unlock(&io_i2c_lock);
            return NACK;
        }
    }

    // stop transmite
    i2c_stop();

    i2c_free();
    mutex_unlock(&io_i2c_lock);
    return ACK;
#endif
}
Ejemplo n.º 28
0
i2c_ack i2c_read_2byte(int bus_id, char chip_addr, unsigned int sub_addr, char *buf, unsigned int size)
{
#ifdef FLY_VIDEO_BOARD_V3
    int ret_rec = 0;
    ret_rec = SOC_I2C_Rec_2B_SubAddr(3, TC358746_I2C_ChipAdd, sub_addr, buf, size);
    //lidbg("\n************i2c_read_2byte  ret_rec=%d *****************\n",ret_rec);
    if(ret_rec > 0)
    {
        return ACK;
    }
    else
    {
        return NACK;
    }
#else
    u8 i;
    mutex_lock(&io_i2c_lock);
    i2c_init();
    // start transmite
    i2c_begin();//msg 0>>
    i2c_write_chip_addr(chip_addr, hkj_WRITE) ;
    if (i2c_read_ack () == NACK)
    {
        i2c_stop();
        i2c_free();
        lidbg(" chip_addr devices is not ACK------i2c_write_chip_addr----r--\r\n");
        mutex_unlock(&io_i2c_lock);
        return NACK;
    }

    //i2c_stop();
    // restart transmite


    // send message to mm_i2c device to transmite data
    i2c_write(sub_addr >> 8) ;
    if (i2c_read_ack () == NACK)
    {
        i2c_stop();
        i2c_free();
        lidbg(" chip_addr devices is not ACK-----subadder1-------\r\n");
        mutex_unlock(&io_i2c_lock);
        return NACK;
    }
    i2c_write(sub_addr) ;
    if (i2c_read_ack () == NACK)
    {
        i2c_stop();
        i2c_free();
        lidbg(" chip_addr devices is not ACK------subadder0------\r\n");
        mutex_unlock(&io_i2c_lock);
        return NACK;
    }
    i2c_stop();//msg 0<<

    // start transmite
    i2c_begin();//msg 1>>
    i2c_write_chip_addr(chip_addr, hkj_READ) ;
    if (i2c_read_ack () == NACK)
    {
        i2c_stop();

        i2c_free();
        lidbg(" chip_addr devices is not ACK----- i2c_write_chip_addr-------\r\n");
        mutex_unlock(&io_i2c_lock);
        return NACK;
    }
    // transmite data
    for(i = 0; i < size; i++)
    {
        buf[i] = i2c_read();
        ( i == (size - 1) ) ? i2c_write_ask(NACK) : i2c_write_ask(ACK);
    }

    // stop transmite
    i2c_stop();//msg 1<<

    i2c_free();
    mutex_unlock(&io_i2c_lock);
    return ACK;
#endif
}
Ejemplo n.º 29
0
i2c_ack i2c_write_2byte(int bus_id, char chip_addr, char *buf, unsigned int size)
{
#ifdef FLY_VIDEO_BOARD_V3
    int ret_send = 0;
    ret_send = SOC_I2C_Send(3, chip_addr, buf, size);
    //lidbg("\n*************i2c_write_2byt ret_send=%d**************\n",ret_send);
    if(ret_send > 0)
    {
        return ACK;
    }
    else
    {
        return NACK;
    }
#else
    u8 i;
    mutex_lock(&io_i2c_lock);
    i2c_init();

    // start transmite
    i2c_begin();//msg 0>>

    i2c_write_chip_addr(chip_addr, hkj_WRITE) ;
    if (i2c_read_ack() == NACK)
    {
        i2c_stop();

        i2c_free();
        lidbg(" chip_addr devices is not ACK--in-i2c_write_chip_addr(%.2x)----w-----\r\n", chip_addr);
        mutex_unlock(&io_i2c_lock);
        return NACK;
    }

    // send message to mm_i2c device to transmite data
    i2c_write(buf[0]) ;//subaddr MSB
    if (i2c_read_ack () == NACK)
    {
        i2c_stop();
        i2c_free();
        lidbg(" chip_addr devices is not ACK--in-i2c_write(sub[%d])=%.2x-----w----\r\n", 0, buf[0]);
        mutex_unlock(&io_i2c_lock);
        return NACK;
    }
    i2c_write(buf[1]) ;//subaddr
    if (i2c_read_ack () == NACK)
    {
        i2c_stop();
        i2c_free();
        lidbg("chip_addr devices is not ACK--in-i2c_write(sub[%d])=%.2x--w", 1, buf[1]);
        mutex_unlock(&io_i2c_lock);
        return NACK;
    }

    // transmite data
    for(i = 2; i < size; i++)
    {
        i2c_write(buf[i]);
        if( i2c_read_ack()  == NACK)
        {
            i2c_stop();
            i2c_free();
            lidbg(" chip_addr devices is not ACK--in-i2c_write(buf[%d])=%.2x--w----\r\n", (i - 2), buf[i]);
            mutex_unlock(&io_i2c_lock);
            return NACK;
        }
    }

    // stop transmite
    i2c_stop();//msg 0<<

    i2c_free();
    mutex_unlock(&io_i2c_lock);
    return ACK;
#endif
}
static unsigned char i2c_write_byte(unsigned char data)
{
	unsigned char i;

#ifdef _DRIVE_
	/* loop */
	for	(i = 0; i < 8; i++) {
		/* set SDA high or low depend on the data bit */
		if (data & 0x80)
			set_i2c_sda_PIN;
		else
			clr_i2c_sda_PIN;
		/* delay */
		i2c_delay(I2C_DELAY_UNIT << 0);

		data <<= 1;

		/* set SCL high */
		set_i2c_scl_PIN;
		/* delay */
		i2c_delay(I2C_DELAY_UNIT << 0);

		/* set SCL low */
		clr_i2c_scl_PIN;
		/* delay */
		i2c_delay(I2C_DELAY_UNIT << 0);
	}
#else
	int retry, retry_val = 10000000;

	//set_sda_output();

	for	(i = 0; i < 8; i++) {
		if (data & 0x80)
			set_sda_input();
		else
			set_sda_output();

		data <<= 1;

		i2c_delay(I2C_DELAY_UNIT << 0);
		set_scl_input();
		i2c_delay(I2C_DELAY_UNIT << 0);
		retry = retry_val;
		while (retry >= 0) 
		{
			if (get_i2c_scl_PIN)
				break;
			else
			{
				i2c_delay(I2C_DELAY_UNIT << 0);
				retry--;
			}
		}

		//if (retry != retry_val)
		if (retry < (retry_val-10000))
		{
			dPrint(N_TRACE,"i2c write_byte: retry = %d\n",retry);
		}
		set_scl_output();		
		i2c_delay(I2C_DELAY_UNIT << 0);
	}
	
#endif
	
	return i2c_read_ack();
}