コード例 #1
0
ファイル: i2c.cpp プロジェクト: brucetsao/RoBoIO
RBAPI(bool) i2c_Init2(unsigned baseaddr, unsigned devs, int i2c0irq, int i2c1irq) {
    int  i;
    
    if (I2C_ioSection != -1)
	{
        err_SetMsg(ERROR_I2C_INUSE, "I2C lib was already opened");
		return false;
	}
	if ((I2C_ioSection = io_Init()) == -1) return false;

	//NOTE: base address should be selected carefully to avoid conflicts with other devices!
	if (baseaddr != 0xffff)
	    i2c_SetBaseAddress(baseaddr);
	else
	{
        if (i2c_SetDefaultBaseAddress() == 0x0000) i2c_SetBaseAddress(0xfb00);
    }

    #ifdef ROBOIO
        switch (roboio_GetRBVer())
        {
            case RB_100b1:
            case RB_100b2:
            case RB_100b3:
            case RB_100:
            case RB_100RD:
            case RB_110:
            case RB_050:
                devs = devs & I2C_USEMODULE0;
                i2c1irq = I2CIRQ_DISABLE;
                break;
            default:
                devs = 0;
                i2c0irq = i2c1irq = I2CIRQ_DISABLE;
                break;
        }
    #endif
    i2c_SetIRQ(i2c0irq, i2c1irq);

    I2C_swMode[0] = I2C_swMode[1] = I2CSW_DISABLE;
    I2C_action[0] = I2C_action[1] = I2CACT_DISABLE;
    for (i=0; i<2; i++)
    {
        if ((i == 0) && ((devs & I2C_USEMODULE0) == 0)) continue;
        if ((i == 1) && ((devs & I2C_USEMODULE1) == 0)) continue;
        I2C_action[i] = I2CACT_IDLE;

        //switch GPIO/I2C pins into GPIO pins
        OLD_I2CGPIO3FLAG[i] = read_sb_reg(SB_IPFCTRL3_REG) & OLD_I2CGPIO3MASK[i]; //backup GPIO/I2C switch flag
        write_sb_reg(SB_IPFCTRL3_REG, read_sb_reg(SB_IPFCTRL3_REG) & (~OLD_I2CGPIO3MASK[i]));

        //send START & STOP signal to reset I2C devices
        OLD_I2CGPIO3DIR[i] = io_inpb(GPIO3_DIR)  & (0x03 << (i*2+4)); //backup GPIO3 DIR
        OLD_I2CGPIO3VAL[i] = io_inpb(GPIO3_DATA) & (0x03 << (i*2+4)); //backup GPIO3 VAL

        //set_pins(i, 1, 1); delay_ms(1); //SCL = 1, SDA = 1; START
        //set_pins(i, 1, 0); delay_ms(1); //SCL = 1, SDA = 0
        //set_pins(i, 0, 0); delay_ms(1); //SCL = 0, SDA = 0
        //Note: if we send the above START, some I2C sensors, such as ADI ADXL345, may fail to respond

        //set_pins(i, 0, 0); delay_ms(1); //SCL = 0, SDA = 0; STOP
        //Note: if we perform the line above, some I2C sensors, such as MEMSIC MXC6202, may fail to respond
        
        //set_pins(i, 1, 0); delay_ms(1); //SCL = 1, SDA = 0
        //set_pins(i, 1, 1); delay_ms(1); //SCL = 1, SDA = 1
        //Note: the above lines work for all sensors we tested, but we disable them to avoid unexpected sensor behaviors

        if (i2c_Reset(i) == false)  // assume the status of GPIO/I2C pins are GPIO "IN" or "OUT 1"
        {
            i2c_Close();
            err_SetMsg(ERROR_I2C_INITFAIL, "can't reset the I2C modules");
		    return false;
        }

        i2c_DisableINT(i, I2CINT_ALL);
        i2c_ClearSTAT(i, I2CSTAT_ALL);

        //Remarks: for DX ver.2, we must disable the noise filter to ensure that 3.3Mbps works in high-speed mode
        i2c_DisableNoiseFilter(i);
        //i2c_EnableNoiseFilter(i);
        i2c_DisableStandardHSM(i); //SCL open-drain in high-speed mode
        //i2c_EnableStandardHSM(i);
    
        i2c_SetSpeed(i, I2CMODE_AUTO, 100000L); //default 100Kbps

        i2cslave_SetAddr(i, 0x7f); //set slave address 0x7F by default (change this if it collide with external I2C devices)
        i2cslave_EnableACK(i);

        //switch GPIO pins into I2C SCL,SDA pins
        //Remarks: Vortex86DX's H/W I2C has an issue here; if you call i2c_Reset() in case GPIO/SCL pin = GPIO out 0,
        //         then, whenever you switch GPIO/SCL pin to SCL pin, the SCL pin always first send the 10 reset dummy clocks
        write_sb_reg(SB_IPFCTRL3_REG, read_sb_reg(SB_IPFCTRL3_REG) | OLD_I2CGPIO3MASK[i]);
    }

    return true;
}
コード例 #2
0
ファイル: i2c.cpp プロジェクト: pongraczi/roboio
RBAPI(bool) i2c_Initialize2(unsigned devs, int i2c0irq, int i2c1irq) {
    int  i;
    
    if (I2C_ioSection != -1)
	{
        err_SetMsg(ERROR_I2C_INUSE, "I2C lib was already opened");
		return false;
	}
	if ((I2C_ioSection = io_Init()) == -1) return false;

    if (i2c_SetDefaultBaseAddress() == 0x0000) i2c_SetBaseAddress(0xfb00);
    i2c_SetIRQ(i2c0irq, i2c1irq);
    
    I2C_action[0] = I2C_action[1] = I2CACT_DISABLE;
    for (i=0; i<2; i++)
    {
        if ((i == 0) && ((devs & I2C_USEMODULE0) == 0)) continue;
        if ((i == 1) && ((devs & I2C_USEMODULE1) == 0)) continue;
        I2C_action[i] = I2CACT_IDLE;

        //switch GPIO/I2C pins into GPIO pins
        OLD_I2CGPIO3FLAG[i] = read_sb_reg(SB_IPFCTRL3_REG) & OLD_I2CGPIO3MASK[i]; //backup GPIO/I2C switch flag
        write_sb_reg(SB_IPFCTRL3_REG, read_sb_reg(SB_IPFCTRL3_REG) & (~OLD_I2CGPIO3MASK[i]));

        //send START & STOP signal to reset I2C devices
        OLD_I2CGPIO3DIR[i] = io_inpb(0x9b) & (0x03 << (i*2+4)); //backup GPIO3 DIR
        OLD_I2CGPIO3VAL[i] = io_inpb(0x7b) & (0x03 << (i*2+4)); //backup GPIO3 VAL

        //set_pins(i, 1, 1); delay_ms(1); //SCL = 1, SDA = 1; START
        //set_pins(i, 1, 0); delay_ms(1); //SCL = 1, SDA = 0
        //set_pins(i, 0, 0); delay_ms(1); //SCL = 0, SDA = 0
        //Note: if we send the above START, some I2C sensors, such as ADI ADXL345, may fail to respond

        //set_pins(i, 0, 0); delay_ms(1); //SCL = 0, SDA = 0; STOP
        //Note: if we perform the line above, some I2C sensors, such as MEMSIC MXC6202, may fail to respond
        set_pins(i, 1, 0); delay_ms(1); //SCL = 1, SDA = 0
        set_pins(i, 1, 1); delay_ms(1); //SCL = 1, SDA = 1

        if (i2c_Reset(i) == false)
        {
            i2c_Close();
            err_SetMsg(ERROR_I2C_INITFAIL, "can't reset the I2C modules");
		    return false;
        }

        i2c_DisableINT(i, I2CINT_ALL);
        i2c_ClearSTAT(i, I2CSTAT_ALL);

        //Remarks: for DX ver.2, we must disable the noise filter to ensure that 3.3Mbps works in high-speed mode
        i2c_DisableNoiseFilter(i);
        //i2c_EnableNoiseFilter(i);
        i2c_DisableStandardHSM(i); //SCL open-drain in high-speed mode
        //i2c_EnableStandardHSM(i);
    
        i2c_SetSpeed(i, I2CMODE_AUTO, 100000L); //default 100Kbps

        i2cslave_SetAddr(i, 0x7f); //set slave address 0x7F by default (change this if it collide with external I2C devices)
        i2cslave_EnableACK(i);

        //switch GPIO pins into I2C SCL,SDA pins
        //Remarks: Vortex86DX's H/W I2C has an issue here; if you call i2c_Reset() in case GPIO/SCL pin = GPIO out 0,
        //         then, whenever you switch GPIO/SCL pin to SCL pin, the SCL pin always first send the 10 reset dummy clocks
        write_sb_reg(SB_IPFCTRL3_REG, read_sb_reg(SB_IPFCTRL3_REG) | OLD_I2CGPIO3MASK[i]);
    }

    return true;
}