コード例 #1
0
ファイル: mpu6050.cpp プロジェクト: danhil/copter
BOOL MPU6050::I2CInit()
{
    INT32 actualClock;
    BOOL Success = TRUE;

    // Added but not tested yet. This is not included in example from Microchip.
    //However it is included in the following example:
    //https://gobotronics.wordpress.com/2010/12/09/i2c-eeprom-pic32/
    //I2CConfigure(this->i2cBusId, I2C_ENABLE_SLAVE_CLOCK_STRETCHING | I2C_ENABLE_HIGH_SPEED);
    I2CConfigure(this->i2cBusId, I2C_ENABLE_HIGH_SPEED);
    
    // Set the I2C baudrate
    actualClock = I2CSetFrequency( this->i2cBusId, GetPeripheralClock(), I2C_CLOCK_FREQ );
    if ( abs(actualClock-I2C_CLOCK_FREQ) > I2C_CLOCK_FREQ/10 )
    {
        //DBPRINTF("Error: I2C1 clock frequency (%u) error exceeds 10%%.\n", (unsigned)actualClock);
        sprintf(filename, "Error: I2C1 clock frequency (%u) error exceeds 10%%.\n", (unsigned)actualClock );
        Success = FALSE;
    }
    else
    {
        sprintf(filename, "I2CInit(): I2C1 clock frequency OK.\n");
    }

    // this function locks the application! Why??!!
    //putsUART1( filename );

    // Enable the I2C bus
    I2CEnable( this->i2cBusId, TRUE );

    return Success;
}
コード例 #2
0
ファイル: i2c.c プロジェクト: expertsleepersltd/disting
void ConfigureCodec()
{
    I2CConfigure( EEPROM_I2C_BUS, 0 );
    I2CSetFrequency( EEPROM_I2C_BUS, GetPeripheralClock(), I2C_CLOCK_FREQ );
    I2CEnable( EEPROM_I2C_BUS, TRUE );
    //
    UINT8               i2cData[10];
    I2C_7_BIT_ADDRESS   SlaveAddress;

    // Initialize the data buffer
    I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, 0x46, I2C_WRITE);
    i2cData[0] = SlaveAddress.byte;
    i2cData[1] = 0x40;              // register 64
    i2cData[2] = 0xC0;              // turn off power save

    SendPacket( i2cData, 3 );

    i2cData[1] = 73;                // register 73
    i2cData[2] = 0x0C;              // inverted phase, no HPF

    SendPacket( i2cData, 3 );

    //
    I2CEnable( EEPROM_I2C_BUS, FALSE );
}
コード例 #3
0
ファイル: i2c.c プロジェクト: andrade824/NoiseBlaster32
/**
 * Initialize the I2C
 */
void InitI2C()
{
    //I2C Pin Config
    mPORTBSetPinsDigitalOut(I2C_SCL_Pin | I2C_SDA_Pin);

    I2CEnable(I2C1, FALSE);

    //Soft reset I2C Bus by pulsing the clock line 10 times
    mPORTBSetBits(I2C_SCL_Pin | I2C_SDA_Pin);
    unsigned int i;
    unsigned int wait;
    for (i = 0; i < 20; i++) {
        for (wait = 0; wait < 20; wait++);
        mPORTBToggleBits(I2C_SCL_Pin);
    }
    mPORTBSetBits(I2C_SCL_Pin | I2C_SDA_Pin);

    // Configure Various I2C Options
    //!!!!! - Slew rate control off(High speed mode enabled), If enabled, RA0 and RA1 fail to work, see silicon errata (Microchip Hardware Bugs)
    I2CConfigure(I2C1, I2C_ENABLE_SLAVE_CLOCK_STRETCHING | I2C_ENABLE_HIGH_SPEED);
    // Set the I2C baud rate
    int I2C_actualClock = I2CSetFrequency(I2C1, SYS_FREQ, I2C_Clock);
    UART_SendString("I2C Clock: ");
    UART_SendInt(I2C_actualClock);
    UART_SendString(" Hz\n\r");
    // Enable the I2C bus
    I2CEnable(I2C1, TRUE);

    while (!I2CTransmitterIsReady(I2C1));

    // configure the interrupt priority for the I2C peripheral
    //INTSetVectorPriority(INT_I2C_1_VECTOR,INT_PRIORITY_LEVEL_3);
    //INTClearFlag(INT_I2C1);
    //INTEnable(INT_I2C1,INT_ENABLED);
}
コード例 #4
0
/**
 * \brief initialize an I2C interface using given config
 *
 * \param[in] hal - opaque ptr to HAL data
 * \param[in] cfg - interface configuration
 *
 * \return ATCA_STATUS
 */
ATCA_STATUS hal_i2c_init(void *hal, ATCAIfaceCfg *cfg)
{
    int bus = cfg->atcai2c.bus; // 0-based logical bus number
    int i;
    ATCAHAL_t *phal = (ATCAHAL_t*)hal;

    if (i2c_bus_ref_ct == 0)    // power up state, no i2c buses will have been used

    for (i = 0; i < MAX_I2C_BUSES; i++)
        i2c_hal_data[i] = NULL;

    i2c_bus_ref_ct++;   // total across buses

    if (bus >= 0 && bus < MAX_I2C_BUSES) 
    {
        //// if this is the first time this bus and interface has been created, do the physical work of enabling it
        if (i2c_hal_data[bus] == NULL) 
        {
            i2c_hal_data[bus] = malloc(sizeof(ATCAI2CMaster_t));
            i2c_hal_data[bus]->ref_ct = 1;  // buses are shared, this is the first instance

            switch (bus) 
            {
//            case 0:
//                i2c_hal_data[bus]->id = I2C0;
//                break;
            case 1:
                i2c_hal_data[bus]->id = I2C1;
                break;
//            case 2:
//                i2c_hal_data[bus]->id = I2C2;
//                break;
            case 3:
                i2c_hal_data[bus]->id = I2C3;
                break;
            }
            
            // Set the I2C baudrate
            I2CSetFrequency(i2c_hal_data[bus]->id, GetPeripheralClock(), cfg->atcai2c.baud);

            // Enable the I2C bus
            I2CEnable(i2c_hal_data[bus]->id, TRUE);
            
            // store this for use during the release phase
            i2c_hal_data[bus]->bus_index = bus;
        }
        else 
        {
            // otherwise, another interface already initialized the bus, so this interface will share it and any different
            // cfg parameters will be ignored...first one to initialize this sets the configuration
            i2c_hal_data[bus]->ref_ct++;
        }

        phal->hal_data = i2c_hal_data[bus];

        return ATCA_SUCCESS;
    }

    return ATCA_COMM_FAIL;
}
コード例 #5
0
ファイル: I2C.c プロジェクト: sdajani/sdp
void I2C_init(I2C_MODULE I2C_ID, uint32_t I2C_clockFreq) {

    // Configure Various I2C Options
    I2CConfigure(I2C_ID, I2C_EN);

    // Set Desired Operation Frequency
    I2CSetFrequency(I2C_ID, Board_GetPBClock(), I2C_clockFreq);
}
コード例 #6
0
void initI2C()
{
	// Set the I2C baudrate.
    I2CSetFrequency(LCD_I2C_BUS, GetPeripheralClock(), I2C_CLOCK_FREQ);

	// Enable the I2C bus.
    I2CEnable(LCD_I2C_BUS, TRUE);
}
コード例 #7
0
void I2C_Initialize( I2C_MODULE id, UINT32 i2cClock, UINT16 address )
{
        // Configure Various I2C Options
        I2CConfigure(id, I2C_ENABLE_SLAVE_CLOCK_STRETCHING | I2C_ENABLE_HIGH_SPEED );
        
        // Set Desired Operation Frequency
        UINT32 actualClock = I2CSetFrequency(id, GetPeripheralClock(), i2cClock);
        if ( abs(actualClock-i2cClock) > i2cClock/10 )
        {
            DEBUG_FUNK("  Clock frequency (%d) error exceeds 10\%\r\n", actualClock);
        }
コード例 #8
0
ファイル: main.c プロジェクト: conorpp/school
/*
 Initializes the accelerometer and it's I2C channel
 */
void initI2CAccel(Accel *a, I2C_MODULE i2c){
    a->read = 0x3b;                     // read accel device value
    a->write = 0x3a;                    // write accel device value

    a->I2C = i2c;                       // I2C channel for accel device

    I2CSetFrequency(a->I2C,             //100 Hz
                    10*1000*1000, 100);
    I2CEnable(a->I2C, 1);               // turn on
    accelWriteReg(a, 0x2d, 0x8);        // set to measure mode
}
コード例 #9
0
ファイル: i2c1.c プロジェクト: colinlewis/SFC_Firmware
void I2C1init(void) {
  // (72M / (2*100kHz)) - 2 = 358
  I2C1BRG = 358;
  
  i2c1QueueRd = 0;
  i2c1QueueWr = 0;
  i2c1Mode = I2C1_MODE_IDLE;
  I2CSetFrequency(I2C1, 72000000L, 90000L);
  I2CEnable(I2C1, TRUE);
  // enable interrupt
  IEC0 |= 0x80000000;
  // priority1
  IPC6 |= 0x400;
} // I2C1init()
コード例 #10
0
/**
 *  @brief  Initialize i2c module:
 *  I2C clock is BRG
 *  If mode parameter is SLAVE, uses address to set slave address for the module
 *  Enable module
 *  @param[in]  I2C_MODULE i2cnum (use I2C1 otherwise is necessary to modify the functions)
 *  @param[in]  i2cmode mode (MASTER or SLAVE)
 *  @param[in]  BYTE address for SLAVE mode
 *  @return     none
 */
void i2c_init(I2C_MODULE i2cnum, i2cmode mode, BYTE address) {
	//enabling i2c module doesnt need changing port
	//direction/value etc, and is not a pin muxed peripheral
	I2CConfigure ( i2cnum, I2C_ENABLE_SLAVE_CLOCK_STRETCHING);
	I2CSetFrequency ( i2cnum, GetPeripheralClock(), BRG);

	if(mode == SLAVE)
	{
		//address mask is set to 0
		I2CSetSlaveAddress ( i2cnum, address&0x7f, 0, I2C_USE_7BIT_ADDRESS );
	}

	I2CEnable(i2cnum, TRUE);
}
コード例 #11
0
/**
 * \brief method to change the bus speed of I2C
 *
 * \param[in] iface  interface on which to change bus speed
 * \param[in] speed  baud rate (typically 100000 or 400000)
 */
void change_i2c_speed(ATCAIface iface, uint32_t speed)
{
	ATCAIfaceCfg *cfg = atgetifacecfg(iface);
	int bus = cfg->atcai2c.bus;
    
    // Disable the I2C bus
    I2CEnable(i2c_hal_data[bus]->id, FALSE);
    
    // Set the I2C baudrate
    I2CSetFrequency(i2c_hal_data[bus]->id, GetPeripheralClock(), speed);
    
    // Enable the I2C bus
    I2CEnable(i2c_hal_data[bus]->id, TRUE);
}
コード例 #12
0
ファイル: i2c_mpu.c プロジェクト: varesa/SerialAcc.X
void I2C_init() {
    int actualClock;

    I2CConfigure(I2C_DEV, 0);

    actualClock = I2CSetFrequency(I2C_DEV, GetPeripheralClock(), I2C_SPEED);
    if(abs(actualClock-I2C_SPEED) > I2C_SPEED*0.1) {
        appendBuffer("ERROR: Could not set I2C clock\r\n");
    } else {
        appendBuffer("INFO: I2C Clock set\r\n");
    }

    I2CEnable(I2C_DEV, TRUE);
    appendBuffer("INFO: I2C Enabled\r\n");

    /*if(!StartTransfer(FALSE)) {
        while(1);
    }
    
    if(!TransmitOneByte((0x68 << 1) & 0b11111110)) {
        while(1);
    }

    if(!I2CByteWasAcknowledged(I2C_DEV)) {
        while(1);
    }

    TransmitOneByte(0x75);


    if(!I2CByteWasAcknowledged(I2C_DEV)) {
        while(1);
    }

    if(!StartTransfer(TRUE)) {
        while(1);
    }
    TransmitOneByte((0x68 << 1) | 0b00000001);

    if(!I2CByteWasAcknowledged(I2C_DEV)) {
        while(1);
    }

    I2CReceiverEnable(I2C_DEV, TRUE);
    while(!I2CReceivedDataIsAvailable(I2C_DEV));
    I2CAcknowledgeByte(I2C_DEV, FALSE);
    I2CGetByte(I2C_DEV);
    StopTransfer();*/
}
コード例 #13
0
ファイル: iic.c プロジェクト: dancollins/danos_pic
void i2c_init(void) {
    I2C_2.moduleName = I2C2;

    I2C_2.state = IDLE;

    I2CConfigure(I2C2, I2C_ENABLE_HIGH_SPEED);
    I2CSetFrequency(I2C2, GetPeripheralClock(), 400000); // 400KHz
    I2CEnable(I2C2, TRUE);

    // Interrupts
    IEC1bits.I2C2MIE = 1; // Enable I2C2 master interrupts
    IEC1bits.I2C2BIE = 1; // Enable I2C2 bues error interrupts
    IPC8bits.I2C2IP = 4; // Priority level 4
    IPC8bits.I2C2IS = 2; // Subpriority level 2
}
コード例 #14
0
/************************************************************************************************** 
  Function: 
    BOOL I2CShared_Init(const I2C_MODULE i2c, const UINT peripheral_clock_speed, const UINT i2c_speed)
  
  Authors(s):
    mkobit
  
  Summary: 
    Enables an I2C module with the given speed
  
  Description: 
    Configures I2C module for normal use and sets the speed if it can
  
  Preconditions: 
    I2C ports not being used for anything
  
  Parameters: 
    const I2C_MODULE i2c - I2C module to be used for initialization
    const UINT peripheral_clock_speed - clock speed of peripheral bus
    const UINT i2c_speed - target speed of I2C module
  
  Returns: 
    TRUE - successfully enabled I2C on this module
    FALSE - clock frequency is too fast
  
  Example: 
    <code>
    I2CShared_Init(I2C1, 40000000L, 300000)
    </code>
  
  Conditions at Exit: 
    I2C module (i2c) configured for use as an I2C bus
  
**************************************************************************************************/
BOOL I2CShared_Init(const I2C_MODULE i2c, const UINT peripheral_clock_speed, const UINT i2c_speed) {
  UINT actualClock;
  I2CConfigure(i2c, 0);

  // Check clock rate for peripheral bus
  actualClock = I2CSetFrequency(i2c, peripheral_clock_speed, i2c_speed);
  if (actualClock - i2c_speed > i2c_speed / 10) {
    ////printf("I2CShared_Init: Error, I2C clock frequency (%u) error exceeds 10%%n\n", actualClock);
    return FALSE;
  }
  TIMEOUT = peripheral_clock_speed / 10;

  I2CShared_ResetBus(i2c);
  
  return TRUE;
}
コード例 #15
0
int gestic_open(gestic_t *gestic)
{
    GESTIC_ASSERT(gestic);
    GESTIC_ASSERT(gestic->io.device);

    /* Maybe power up */

    gestic->io.connected = 1;
    gestic->io.I2cSlaveAddr.address = gestic->io.device(GestICDev_GetI2CAddress);
    gestic->io.I2cPort = gestic->io.device(GestICDev_GetI2CPort);

    gestic->io.device(GestICDev_TsLine_Init);
    gestic->io.device(GestICDev_Reset_Init);

    I2CEnable(gestic->io.I2cPort, FALSE);
    I2CConfigure(gestic->io.I2cPort, I2C_ENABLE_SLAVE_CLOCK_STRETCHING | I2C_ENABLE_HIGH_SPEED);
    //I2CSetFrequency(gestic->io.I2cPort, 40*1000*1000, 400*1000);
    I2CSetFrequency(gestic->io.I2cPort, 40*1000*1000, 25*1000);

    I2CEnable(gestic->io.I2cPort, TRUE);
    return GESTIC_NO_ERROR;
}
コード例 #16
0
void i2cMasterInitialize(I2cBus* i2cBus) {
    OutputStream* outputStream = getDebugOutputStreamLogger();
    appendString(outputStream, "Initializing I2C ...");

    // Avoid more than one initialization
    if (i2cBus->initialized) {
        printI2cBus(outputStream, i2cBus);
        appendCRLF(outputStream);
        appendString(getDebugOutputStreamLogger(), "\n!!! ALREADY INITIALIZED !!!\n");
        return;
    }
    i2cBus->initialized = true;
    #define I2C_BRG     0xC6    // 100khz for PIC32
    // Configure I2C for 7 bit address mode
    #define I2C_CON     I2C_ON

    i2cBus->config = I2C_CON;

    if (i2cBus == NULL) {
        OpenI2C1(
                 // Configure I2C for 7 bit address mode.
                 I2C_CON,
                 // 100khz for PIC32.
                 I2C_BRG);
    }
    else {
        I2C_MODULE i2cModule = getI2C_MODULE(i2cBus->port);
        I2CConfigure(i2cModule, I2C_ON);
        I2CSetFrequency(i2cModule, GetPeripheralClock(), 100000L);
    }

    WaitI2C(i2cBus);
    
    // Indicates that it's ok !
    appendString(outputStream, "OK\n"); 
    printI2cBus(outputStream, i2cBus);
    appendCRLF(outputStream);
}
コード例 #17
0
ファイル: NU32_I2C.c プロジェクト: adwilson10/quad_pic32
//Turn on I2C Module and calibrate for standard operation.
void I2Cinitialize(void){

    I2CSetFrequency(I2C1, SYS_FREQ, I2C_CLOCK_FREQ);
    I2CEnable(I2C1, TRUE);
}
コード例 #18
0
ファイル: main.c プロジェクト: cdauchess/VTMDAQ_EXPERIMENTAL
//the next few functions are general for i2c
//all these functions were stolen from Kralick_Lab4
void initI2CBus(I2C_MODULE id, int pBusFrq, int i2cFrq){
    I2CConfigure(id, 0);
    I2CSetFrequency(id, pBusFrq, i2cFrq);
    I2CEnable(id, TRUE);
}
コード例 #19
0
int main(void)
{
    UINT8               i2cData[10];
    I2C_7_BIT_ADDRESS   SlaveAddress;
    int                 Index;
    int                 DataSz;
    UINT32              actualClock;
    BOOL                Acknowledged;
    BOOL                Success = TRUE;
    UINT8               i2cbyte;


    // Initialize debug messages (when supported)
    DBINIT();

    // Set the I2C baudrate
    actualClock = I2CSetFrequency(EEPROM_I2C_BUS, GetPeripheralClock(), I2C_CLOCK_FREQ);
    if ( abs(actualClock-I2C_CLOCK_FREQ) > I2C_CLOCK_FREQ/10 )
    {
        DBPRINTF("Error: I2C1 clock frequency (%u) error exceeds 10%%.\n", (unsigned)actualClock);
    }

    // Enable the I2C bus
    I2CEnable(EEPROM_I2C_BUS, TRUE);
    

    //
    // Send the data to EEPROM to program one location
    //

    // Initialize the data buffer
    I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, EEPROM_ADDRESS, I2C_WRITE);
    i2cData[0] = SlaveAddress.byte;
    i2cData[1] = 0x05;              // EEPROM location to program (high address byte)
    i2cData[2] = 0x40;              // EEPROM location to program (low address byte)
    i2cData[3] = 0xAA;              // Data to write
    DataSz = 4;

    // Start the transfer to write data to the EEPROM
    if( !StartTransfer(FALSE) )
    {
        while(1);
    }

    // Transmit all data
    Index = 0;
    while( Success && (Index < DataSz) )
    {
        // Transmit a byte
        if (TransmitOneByte(i2cData[Index]))
        {
            // Advance to the next byte
            Index++;

            // Verify that the byte was acknowledged
            if(!I2CByteWasAcknowledged(EEPROM_I2C_BUS))
            {
                DBPRINTF("Error: Sent byte was not acknowledged\n");
                Success = FALSE;
            }
        }
        else
        {
            Success = FALSE;
        }
    }

    // End the transfer (hang here if an error occured)
    StopTransfer();
    if(!Success)
    {
        while(1);
    }


    // Wait for EEPROM to complete write process, by polling the ack status.
    Acknowledged = FALSE;
    do
    {
        // Start the transfer to address the EEPROM
        if( !StartTransfer(FALSE) )
        {
            while(1);
        }
        
        // Transmit just the EEPROM's address
        if (TransmitOneByte(SlaveAddress.byte))
        {
            // Check to see if the byte was acknowledged
            Acknowledged = I2CByteWasAcknowledged(EEPROM_I2C_BUS);
        }
        else
        {
            Success = FALSE;
        }

        // End the transfer (stop here if an error occured)
        StopTransfer();
        if(!Success)
        {
            while(1);
        }

    } while (Acknowledged != TRUE);


    //
    // Read the data back from the EEPROM.
    //

    // Initialize the data buffer
    I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, EEPROM_ADDRESS, I2C_WRITE);
    i2cData[0] = SlaveAddress.byte;
    i2cData[1] = 0x05;              // EEPROM location to read (high address byte)
    i2cData[2] = 0x40;              // EEPROM location to read (low address byte)
    DataSz = 3;
    
    // Start the transfer to read the EEPROM.
    if( !StartTransfer(FALSE) )
    {
        while(1);
    }
    
    // Address the EEPROM.
    Index = 0;
    while( Success & (Index < DataSz) )
    {
        // Transmit a byte
        if (TransmitOneByte(i2cData[Index]))
        {
            // Advance to the next byte
            Index++;
        }
        else
        {
            Success = FALSE;
        }

        // Verify that the byte was acknowledged
        if(!I2CByteWasAcknowledged(EEPROM_I2C_BUS))
        {
            DBPRINTF("Error: Sent byte was not acknowledged\n");
            Success = FALSE;
        }
    }

    // Restart and send the EEPROM's internal address to switch to a read transfer
    if(Success)
    {
        // Send a Repeated Started condition
        if( !StartTransfer(TRUE) )
        {
            while(1);
        }

        // Transmit the address with the READ bit set
        I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, EEPROM_ADDRESS, I2C_READ);
        if (TransmitOneByte(SlaveAddress.byte))
        {
            // Verify that the byte was acknowledged
            if(!I2CByteWasAcknowledged(EEPROM_I2C_BUS))
            {
                DBPRINTF("Error: Sent byte was not acknowledged\n");
                Success = FALSE;
            }
        }
        else
        {
            Success = FALSE;
        }
    }

    // Read the data from the desired address
    if(Success)
    {
        if(I2CReceiverEnable(EEPROM_I2C_BUS, TRUE) == I2C_RECEIVE_OVERFLOW)
        {
            DBPRINTF("Error: I2C Receive Overflow\n");
            Success = FALSE;
        }
        else
        {
            while(!I2CReceivedDataIsAvailable(EEPROM_I2C_BUS));
            i2cbyte = I2CGetByte(EEPROM_I2C_BUS);
        }

    }

    // End the transfer (stop here if an error occured)
    StopTransfer();
    if(!Success)
    {
        while(1);
    }


    // Validate the data read
    if( i2cbyte != 0xAA )
    {
        DBPRINTF("Error: Verify failed\n");
    }
    else
    {
        DBPRINTF("Success\n");
    }

    // Example complete
    while(1);
}