Beispiel #1
0
// * imu_init *****************************************************************
// * Setup pins and I2C interface to communicate with IMU (Pololu MinIMU-9).  *
// *                                                                          *
// * NOTE: Also called internally by imu_i2c_abort_transaction during reset   *
// *       to recover from NAK/error.                                         *
// *                                                                          *
// * Portions for initialization of softi2c library copied/modified from      *
// * "soft_i2c_atmel.c" example code.                                         *
// ****************************************************************************
void imu_init(void)
{
                                                          // setup pins for I2C--------------------
    SysCtlPeripheralEnable(IMU_PORT);                     // enable GPIO port for IMU
    SysCtlPeripheralEnable(IMU_TIM);                      // enable timer to use for I2C bit timing
    SysCtlPeripheralReset(IMU_TIM);                       // reset timer to clear any previous configuration
    GPIOPinTypeI2C(IMU_PORT_BASE, IMU_PINS);              // configure pins for I2C
    memset(&g_sI2C, 0, sizeof(g_sI2C));                   // clear record
    SoftI2CCallbackSet(&g_sI2C, imu_SoftI2CCallback);     // set callback function
    SoftI2CSCLGPIOSet(&g_sI2C, IMU_PORT_BASE, IMU_SCL);   // set SCL pin
    SoftI2CSDAGPIOSet(&g_sI2C, IMU_PORT_BASE, IMU_SDA);   // set SDA pin
    SoftI2CInit(&g_sI2C);                                 // initialize software I2C driver
    SoftI2CIntEnable(&g_sI2C);                            // enable callback from softi2c
    
                                                          // configure timer interrupt
                                                          // Note, (interrupt rate)/4 = SCL frequency
    TimerConfigure(IMU_TIM_BASE, TIMER_CFG_32_BIT_PER);   // configure timer for 32b operation
    TimerLoadSet(IMU_TIM_BASE, TIMER_A, SysCtlClockGet() / IMU_TIM_INTRATE);
                                                          // configure divider to yield desired interrupt rate
    TimerIntEnable(IMU_TIM_BASE, TIMER_TIMA_TIMEOUT);     // enable timer wrap interrupt
    TimerEnable(IMU_TIM_BASE, TIMER_A);                   // enable timer to start counting
    IntEnable(IMU_TIM_INT_VECT);                          // enable interrupt vector in NVIC
  
}
Beispiel #2
0
//*****************************************************************************
//
// This example demonstrates the use of the SoftI2C module to read and write an
// Atmel AT24C08A EEPROM.
//
//*****************************************************************************
int
main(void)
{
    uint8_t pui8Data[16];
    uint32_t ui32Idx;

    //
    // Set the clocking to run directly from the external crystal/oscillator.
    // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
    // crystal on your board.
    //
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_16MHZ);

    //
    // For this example, PortB[3:2] are used for the SoftI2C pins.  GPIO port B
    // needs to be enabled so these pins can be used.
    // TODO: change this to whichever GPIO port(s) you are using.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

    //
    // For this example, Timer0 is used for the SoftI2C time base.  This timer
    // needs to be enabled before it can be used.
    // TODO: change this to whichever timer you are using.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);

    //
    // Configure the appropriate pins to be I2C instead of GPIO.
    //
    GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3);

    //
    // Initialize the SoftI2C module, including the assignment of GPIO pins.
    // TODO: change this to whichever GPIO pins you are using.
    //
    memset(&g_sI2C, 0, sizeof(g_sI2C));
    SoftI2CCallbackSet(&g_sI2C, SoftI2CCallback);
    SoftI2CSCLGPIOSet(&g_sI2C, GPIO_PORTB_BASE, GPIO_PIN_2);
    SoftI2CSDAGPIOSet(&g_sI2C, GPIO_PORTB_BASE, GPIO_PIN_3);
    SoftI2CInit(&g_sI2C);

    //
    // Enable the SoftI2C interrupt.
    //
    SoftI2CIntEnable(&g_sI2C);

    //
    // Configure the timer to generate an interrupt at a rate of 40 KHz.  This
    // will result in a I2C rate of 10 KHz.
    // TODO: change this to whichever timer you are using.
    // TODO: change this to whichever I2C rate you require.
    //
    TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
    TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet() / 40000);
    TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    TimerEnable(TIMER0_BASE, TIMER_A);

    //
    // Enable the timer interrupt.
    // TODO: change this to whichever timer interrupt you are using.
    //
    IntEnable(INT_TIMER0A);

    //
    // Set up the serial console to use for displaying messages.  This is
    // just for this example program and is not needed for SoftI2C operation.
    //
    InitConsole();

    //
    // Display the example setup on the console.
    //
    UARTprintf("SoftI2C Atmel AT24C08A example\n");

    //
    // Write a data=address pattern into the first 16 bytes of the Atmel
    // device.
    //
    UARTprintf("Write:");
    for(ui32Idx = 0; ui32Idx < 16; ui32Idx++)
    {
        pui8Data[ui32Idx] = ui32Idx;
        UARTprintf(" %02x", pui8Data[ui32Idx]);
    }
    UARTprintf("\n");
    AtmelWrite(pui8Data, 0, 16);

    //
    // Read back the first 16 bytes of the Atmel device.
    //
    AtmelRead(pui8Data, 0, 16);
    UARTprintf("Read :");
    for(ui32Idx = 0; ui32Idx < 16; ui32Idx++)
    {
        UARTprintf(" %02x", pui8Data[ui32Idx]);
    }
    UARTprintf("\n");

    //
    // Tell the user that the test is done.
    //
    UARTprintf("Done.\n\n");

    //
    // Return no errors.
    //
    return(0);
}