示例#1
0
void portableCommonAckI2C(I2cBusConnection* i2cBusConnection) {
    I2cBus* i2cBus = i2cBusConnection->i2cBus;

    if (i2cBus == NULL) {
        AckI2C1();
    }
    else {
        I2C_MODULE i2cModule = getI2C_MODULE(i2cBus->port);
        I2CAcknowledgeByte(i2cModule, true);
    }
}
示例#2
0
void I2C_ReadFromReg_Burst(uint8_t address, uint8_t deviceRegister, uint8_t* data, uint8_t burstNum)
{
	// Assert the start condition
	StartI2C1();
	while (I2C1CONbits.SEN);

	// Send the 7-bit device address
	MasterWriteI2C1(address << 1);
	while (I2C1STATbits.TBF); // 8 clock cycles
	while (!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
	IFS1bits.MI2C1IF = 0; // Clear interrupt flag

	// Send the register address
	MasterWriteI2C1(deviceRegister);
	while (I2C1STATbits.TBF); // 8 clock cycles
	while (!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
	IFS1bits.MI2C1IF = 0; // Clear interrupt flag

	// Start a new I2C transaction
	RestartI2C1();
	while (I2C1CONbits.RSEN);

	// Send the second address
	MasterWriteI2C1((address << 1) + 1);
	while (I2C1STATbits.TBF); // 8 clock cycles
	while (!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
	IFS1bits.MI2C1IF = 0; // Clear interrupt flag

	// Read the data
	data[0] = MasterReadI2C1();

	if (burstNum > 1) {
                uint8_t i;
		for (i = 1; i < burstNum; i++) {
			AckI2C1();
			while (I2C1CONbits.ACKEN == 1);

			data[i] = MasterReadI2C1();
		}
	}

	// No need to ack reception of this data
	NotAckI2C1();
	while (I2C1CONbits.ACKEN == 1);

	// Stop the I2C transaction
	StopI2C1();
	while (I2C1CONbits.PEN);

	// Go idle on the bus
	IdleI2C1();
}
int RcvData2(unsigned int address){
    rcv = 0;
    StartI2C1();				//Send line start condition
	IdleI2C1();			        //Wait to complete
	MasterWriteI2C1((address << 1) | 1);	//Write out slave address OR 1 (read command)
	IdleI2C1();				//Wait to complete
	rcv = MasterReadI2C1()<<8;		//Read in a value   
    AckI2C1();
    rcv |= MasterReadI2C1();    
	StopI2C1();				//Send line stop condition
	IdleI2C1();				//Wait co complete
	return rcv;				//Return read value    
}
int8_t I2C1dev_readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data) {
    count_accel = 0;

    // S
    IdleI2C1();
    StartI2C1();

    // Device write address
    IdleI2C1();
    WriteI2C1(devAddr << 1 | 0x00);

    // Register address
    IdleI2C1();
    WriteI2C1(regAddr);

    // R
    IdleI2C1();
    RestartI2C1();

    // Device read address
    IdleI2C1();
    WriteI2C1(devAddr << 1 | 0x01);

    for (count_accel = 0; count_accel < length; count_accel++) {
        // Data byte
        IdleI2C1();
        data[count_accel] = ReadI2C1();

        if (count_accel == length - 1) {
            // NACK
            IdleI2C1();
            NotAckI2C1();
        } else {
            // ACK
            IdleI2C1();
            AckI2C1();
        }
    }

    // P
    IdleI2C1();
    StopI2C1();

    return count_accel;
}
uint8_t
mti_tcn75a_get_temperature(int32_t *result)
{
	uint16_t tmp;
	uint8_t rc;
	uint8_t tmp_lsb = 4;
	uint8_t tmp_msb = 2;

	/* start */
	StartI2C1();

	/* send slave address (write) */
	rc = WriteI2C1(0b10010000);
	if (rc != 0x00) {
		rc = CH_ERROR_I2C_SLAVE_ADDRESS;
		goto out;
	}

	/* set ambient temperature pointer */
	rc = WriteI2C1(0b00000000);

	/* reset the bus */
	RestartI2C1();

	/* send slave address (read) */
	WriteI2C1(0b10010001);

	/* read the temperature */
	tmp_msb = ReadI2C1();
	AckI2C1();
	tmp_lsb = ReadI2C1();
	NotAckI2C1();

	/* stop */
	StopI2C1();

	/* format result */
	*result = (((int32_t) tmp_msb) << 16) + (((int32_t) tmp_lsb) << 8);
out:
	return rc;
}