//Initialize as a master void TwoWire::begin(void) { if(i2cModule == NOT_ACTIVE) { i2cModule = BOOST_PACK_WIRE; } ROM_SysCtlPeripheralEnable(g_uli2cPeriph[i2cModule]); //Configure GPIO pins for I2C operation ROM_GPIOPinConfigure(g_uli2cConfig[i2cModule][0]); ROM_GPIOPinConfigure(g_uli2cConfig[i2cModule][1]); ROM_GPIOPinTypeI2C(g_uli2cBase[i2cModule], g_uli2cSDAPins[i2cModule]); ROM_GPIOPinTypeI2CSCL(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); ROM_I2CMasterInitExpClk(MASTER_BASE, F_CPU, speedMode);//Bus speed if(speedMode==I2C_SPEED_FASTMODE_PLUS)//Force 1Mhz { uint32_t ui32TPR = ((F_CPU + (2 * 10 * 1000000l) - 1) / (2 * 10 * 1000000l)) - 1; HWREG(MASTER_BASE + I2C_O_MTPR) = ui32TPR; } //force a stop condition if(!ROM_GPIOPinRead(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule])) forceStop(); //Handle any startup issues by pulsing SCL if(ROM_I2CMasterBusBusy(MASTER_BASE) || ROM_I2CMasterErr(MASTER_BASE) || !ROM_GPIOPinRead(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule])) { uint8_t doI = 0; ROM_GPIOPinTypeGPIOOutput(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); unsigned long mask = 0; do { for(unsigned long i = 0; i < 10 ; i++) { if(speedMode==I2C_SPEED_FASTMODE_PLUS) ROM_SysCtlDelay(F_CPU/1000000/3);//1000Hz=desired frequency, delay iteration=3 cycles else if(speedMode==I2C_SPEED_FASTMODE) ROM_SysCtlDelay(F_CPU/400000/3);//400Hz=desired frequency, delay iteration=3 cycles else ROM_SysCtlDelay(F_CPU/100000/3);//100Hz=desired frequency, delay iteration=3 cycles mask = (i%2) ? g_uli2cSCLPins[i2cModule] : 0; ROM_GPIOPinWrite(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule], mask); } doI++; } while(ROM_I2CMasterBusBusy(MASTER_BASE) && doI < 100); ROM_GPIOPinTypeI2CSCL(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); if(!ROM_GPIOPinRead(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule])) forceStop(); } }
//Initialize as a slave void TwoWire::begin(uint8_t address) { if(i2cModule == NOT_ACTIVE) { i2cModule = BOOST_PACK_WIRE; } ROM_SysCtlPeripheralEnable(g_uli2cPeriph[i2cModule]); ROM_GPIOPinConfigure(g_uli2cConfig[i2cModule][0]); ROM_GPIOPinConfigure(g_uli2cConfig[i2cModule][1]); ROM_GPIOPinTypeI2C(g_uli2cBase[i2cModule], g_uli2cSDAPins[i2cModule]); ROM_GPIOPinTypeI2CSCL(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); slaveAddress = address; //Enable slave interrupts ROM_IntEnable(g_uli2cInt[i2cModule]); I2CSlaveIntEnableEx(SLAVE_BASE, I2C_SLAVE_INT_DATA | I2C_SLAVE_INT_STOP); HWREG(SLAVE_BASE + I2C_O_SICR) = I2C_SICR_DATAIC | I2C_SICR_STARTIC | I2C_SICR_STOPIC; //Setup as a slave device ROM_I2CMasterDisable(MASTER_BASE); I2CSlaveEnable(SLAVE_BASE); I2CSlaveInit(SLAVE_BASE, address); ROM_IntMasterEnable(); }
void TwoWire::forceStop(void) { //force a stop to release the bus ROM_GPIOPinTypeGPIOOutput(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule] | g_uli2cSDAPins[i2cModule]); ROM_GPIOPinWrite(g_uli2cBase[i2cModule], g_uli2cSDAPins[i2cModule], 0); ROM_GPIOPinWrite(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule], g_uli2cSCLPins[i2cModule]); ROM_GPIOPinWrite(g_uli2cBase[i2cModule], g_uli2cSDAPins[i2cModule], g_uli2cSDAPins[i2cModule]); ROM_GPIOPinTypeI2C(g_uli2cBase[i2cModule], g_uli2cSDAPins[i2cModule]); ROM_GPIOPinTypeI2CSCL(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); //reset I2C controller //without resetting the I2C controller, the I2C module will //bring the bus back to it's erroneous state ROM_SysCtlPeripheralReset(g_uli2cPeriph[i2cModule]); while(!ROM_SysCtlPeripheralReady(g_uli2cPeriph[i2cModule])); ROM_I2CMasterInitExpClk(MASTER_BASE, F_CPU, speedMode);//Bus speed if(speedMode==I2C_SPEED_FASTMODE_PLUS)//Force 1Mhz { uint32_t ui32TPR = ((F_CPU + (2 * 10 * 1000000l) - 1) / (2 * 10 * 1000000l)) - 1; HWREG(MASTER_BASE + I2C_O_MTPR) = ui32TPR; } }
//Initialize as a master void TwoWire::begin(void) { pinMode(RED_LED, OUTPUT); if(i2cModule == NOT_ACTIVE) { i2cModule = BOOST_PACK_WIRE; } ROM_SysCtlPeripheralEnable(g_uli2cPeriph[i2cModule]); //Configure GPIO pins for I2C operation ROM_GPIOPinConfigure(g_uli2cConfig[i2cModule][0]); ROM_GPIOPinConfigure(g_uli2cConfig[i2cModule][1]); ROM_GPIOPinTypeI2C(g_uli2cBase[i2cModule], g_uli2cSDAPins[i2cModule]); ROM_GPIOPinTypeI2CSCL(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); ROM_I2CMasterInitExpClk(MASTER_BASE, F_CPU, false);//max bus speed=400kHz for gyroscope //force a stop condition if(!ROM_GPIOPinRead(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule])) forceStop(); //Handle any startup issues by pulsing SCL if(ROM_I2CMasterBusBusy(MASTER_BASE) || ROM_I2CMasterErr(MASTER_BASE) || !ROM_GPIOPinRead(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule])){ uint8_t doI = 0; ROM_GPIOPinTypeGPIOOutput(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); unsigned long mask = 0; do{ for(unsigned long i = 0; i < 10 ; i++) { ROM_SysCtlDelay(F_CPU/100000/3);//100Hz=desired frequency, delay iteration=3 cycles mask = (i%2) ? g_uli2cSCLPins[i2cModule] : 0; ROM_GPIOPinWrite(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule], mask); } doI++; }while(ROM_I2CMasterBusBusy(MASTER_BASE) && doI < 100); ROM_GPIOPinTypeI2CSCL(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); if(!ROM_GPIOPinRead(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule])) forceStop(); } }
void ConfigureI2C3(void){ // Enable peripherals used by I2C ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3); // Setup GPIO ROM_GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); ROM_GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1); // Set GPIO D0 and D1 as SCL and SDA ROM_GPIOPinConfigure(GPIO_PD0_I2C3SCL); ROM_GPIOPinConfigure(GPIO_PD1_I2C3SDA); // Initialize as master - 'true' for fastmode, 'false' for regular ROM_I2CMasterInitExpClk(I2C3_BASE, ROM_SysCtlClockGet(), true); }
void TwoWire::forceStop(void) { //force a stop to release the bus ROM_GPIOPinTypeGPIOOutput(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule] | g_uli2cSDAPins[i2cModule]); ROM_GPIOPinWrite(g_uli2cBase[i2cModule], g_uli2cSDAPins[i2cModule], 0); ROM_GPIOPinWrite(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule], g_uli2cSCLPins[i2cModule]); ROM_GPIOPinWrite(g_uli2cBase[i2cModule], g_uli2cSDAPins[i2cModule], g_uli2cSDAPins[i2cModule]); ROM_GPIOPinTypeI2C(g_uli2cBase[i2cModule], g_uli2cSDAPins[i2cModule]); ROM_GPIOPinTypeI2CSCL(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); //reset I2C controller //without resetting the I2C controller, the I2C module will //bring the bus back to it's erroneous state ROM_SysCtlPeripheralReset(g_uli2cPeriph[i2cModule]); while(!ROM_SysCtlPeripheralReady(g_uli2cPeriph[i2cModule])); ROM_I2CMasterInitExpClk(MASTER_BASE, F_CPU, false); }
void tm4c123_i2c_initialize(void) { uint32_t count; tm4c123_i2c_device.state = I2C_STATE_IDLE; ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); /* Disable I2C3 */ I2C3->MCR = I2C_MCR_MFE; ROM_GPIOPinTypeGPIOOutputOD(GPIOD_BASE, GPIO_PIN_0); ROM_GPIOPinTypeGPIOInput(GPIOD_BASE, GPIO_PIN_1); /* Delay for 10usec */ armv7m_udelay(10); /* If SDA is tied low by a slave, issue clock pulses till it releases * the bus. */ for (count = 0; count < 128; count++) { if (GPIOD->DATA & GPIO_PIN_1) { break; } /* Set SCL to L */ armv7m_bitband_peripheral_write(&GPIOD->DATA, 0, 0); armv7m_udelay(5); /* Set SCL to H */ armv7m_bitband_peripheral_write(&GPIOD->DATA, 0, 1); armv7m_udelay(5); } ROM_GPIOPinTypeGPIOOutput(GPIOD_BASE, GPIO_PIN_1); armv7m_bitband_peripheral_write(&GPIOD->DATA, 0, 1); armv7m_bitband_peripheral_write(&GPIOD->DATA, 1, 1); armv7m_udelay(5); /* Now SCL is H and SDA is H, so generate a STOP condition. */ /* Set SCL to L */ armv7m_bitband_peripheral_write(&GPIOD->DATA, 0, 0); armv7m_udelay(5); /* Set SDA to L */ armv7m_bitband_peripheral_write(&GPIOD->DATA, 1, 0); armv7m_udelay(5); /* Set SCL to H */ armv7m_bitband_peripheral_write(&GPIOD->DATA, 0, 1); armv7m_udelay(5); /* Set SDA to H */ armv7m_bitband_peripheral_write(&GPIOD->DATA, 1, 1); armv7m_udelay(5); /* After this recovery, switch to regular I2C mode. */ /* Enable pin PD0 for I2C3 I2C3SCL */ ROM_GPIOPinConfigure(GPIO_PD0_I2C3SCL); ROM_GPIOPinTypeI2CSCL(GPIOD_BASE, GPIO_PIN_0); /* Enable pin PD1 for I2C3 I2C3SDA */ ROM_GPIOPinConfigure(GPIO_PD1_I2C3SDA); ROM_GPIOPinTypeI2C(GPIOD_BASE, GPIO_PIN_1); tm4c123_i2c_reset(); NVIC_SetPriority(I2C3_IRQn, 3); NVIC_EnableIRQ(I2C3_IRQn); }
//***************************************************************************** // // Main 'C' Language entry point. // //***************************************************************************** int main(void) { const uint8_t bufLength=10; char inputBuf[1]; // // Configure the system frequency. // g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480), 120000000); // // Configure the device pins for this board. // PinoutSet(false, false); // // Initialize the UART. // ConfigureUART(); // // Print the welcome message to the terminal. // UARTprintf("\033[2J\033[H"); UARTprintf("SHT21 Example\n"); // // The I2C7 peripheral must be enabled before use. // // Note: For BoosterPack 2 interface use I2C8. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C8); //ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // // Configure the pin muxing for I2C7 functions on port D0 and D1. // This step is not necessary if your part does not support pin muxing. // // Note: For BoosterPack 2 interface use PA2 and PA3. // ROM_GPIOPinConfigure(GPIO_PA2_I2C8SCL); ROM_GPIOPinConfigure(GPIO_PA3_I2C8SDA); // // Select the I2C function for these pins. This function will also // configure the GPIO pins pins for I2C operation, setting them to // open-drain operation with weak pull-ups. Consult the data sheet // to see which functions are allocated per pin. // // Note: For BoosterPack 2 interface use PA2 and PA3. // ROM_GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_2); ROM_GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_3); // // Keep only some parts of the systems running while in sleep mode. // GPIOE is for the ISL29023 interrupt pin. // UART0 is the virtual serial port. // I2C7 is the I2C interface to the ISL29023. // // Note: For BoosterPack 2 change this to I2C8. // ROM_SysCtlPeripheralClockGating(true); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_GPIOE); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_UART0); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_I2C8); // // Enable interrupts to the processor. // ROM_IntMasterEnable(); // // Initialize I2C7 peripheral. // // Note: For BoosterPack 2 use I2C8. // I2CMInit(&g_sI2CInst, I2C8_BASE, INT_I2C8, 0xff, 0xff, g_ui32SysClock); // // Turn on D2 to show we are starting an I2C transaction with the sensor. // This is turned off in the application callback. // LEDWrite(CLP_D1 | CLP_D2 , CLP_D2); // // Initialize the SHT21. // SHT21Init(&g_sSHT21Inst, &g_sI2CInst, SHT21_I2C_ADDRESS, SHT21AppCallback, &g_sSHT21Inst); // // Initialize the TMP006 // TMP006Init(&g_sTMP006Inst, &g_sI2CInst, TMP006_I2C_ADDRESS, TMP006AppCallback, &g_sTMP006Inst); // // Wait for the I2C transactions to complete before moving forward. // SHT21AppI2CWait(__FILE__, __LINE__); // // Delay for 20 milliseconds for SHT21 reset to complete itself. // Datasheet says reset can take as long 15 milliseconds. // ROM_SysCtlDelay(g_ui32SysClock / (50 * 3)); UARTprintf("Menu:\n"); UARTprintf("h for Humidity \n"); UARTprintf("t for Temperature \n"); // // Loop Forever. // while(1) { if(UARTgets(inputBuf,bufLength)){ if(inputBuf[0]=='h'){ UARTprintf("Sensing Humidity Data:\n"); printHumidityData(); } else if(inputBuf[0]=='t'){ UARTprintf("Sensing Temperature Data:\n"); printTemperatureData(); } } } }
void MPU6050_Init(void){ ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); ROM_SysCtlDelay(3); ROM_GPIOPinConfigure(GPIO_PB2_I2C0SCL); ROM_GPIOPinConfigure(GPIO_PB3_I2C0SDA); ROM_GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); ROM_GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); //ROM_GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_0); //GPIOIntEnable(GPIO_PORTB_BASE, GPIO_PIN_0); //ROM_GPIOIntTypeSet(GPIO_PORTB_BASE, GPIO_PIN_0, GPIO_FALLING_EDGE); //ROM_IntEnable(INT_GPIOB); ROM_I2CMasterEnable(I2C0_BASE); I2CMInit(&sI2CInst, I2C0_BASE, INT_I2C0, 0xff, 0xff, ROM_SysCtlClockGet()); g_bMPU6050Done = false; MPU6050Init(&sMPU6050, &sI2CInst, 0x68, MPU6050_Callback, 0); while(!g_bMPU6050Done); g_bMPU6050Done = false; MPU6050ReadModifyWrite(&sMPU6050, MPU6050_O_PWR_MGMT_1, ~MPU6050_PWR_MGMT_1_SLEEP, 0, MPU6050_Callback, 0);//从Sleep模式中激活设备,否则无法写入寄存器 while(!g_bMPU6050Done); // Configure the MPU6050 for +/- 4 g accelerometer range. // g_bMPU6050Done = false; //先读取寄存器的值,再与上Mask,然后再或上Value,将得到的值写入寄存器 MPU6050ReadModifyWrite(&sMPU6050, MPU6050_O_PWR_MGMT_1, ~MPU6050_PWR_MGMT_1_CLKSEL_M, MPU6050_PWR_MGMT_1_CLKSEL_XG, MPU6050_Callback, 0); while(!g_bMPU6050Done); g_bMPU6050Done = false; MPU6050ReadModifyWrite(&sMPU6050, MPU6050_O_ACCEL_CONFIG, ~MPU6050_ACCEL_CONFIG_AFS_SEL_M, MPU6050_ACCEL_CONFIG_AFS_SEL_4G, MPU6050_Callback, 0); while(!g_bMPU6050Done); g_bMPU6050Done = false; MPU6050ReadModifyWrite(&sMPU6050, MPU6050_O_CONFIG, ~MPU6050_CONFIG_DLPF_CFG_M, MPU6050_CONFIG_DLPF_CFG_44_42, MPU6050_Callback, 0); while(!g_bMPU6050Done); g_bMPU6050Done = false; MPU6050ReadModifyWrite(&sMPU6050, MPU6050_O_GYRO_CONFIG, ~MPU6050_GYRO_CONFIG_FS_SEL_M, MPU6050_GYRO_CONFIG_FS_SEL_2000, MPU6050_Callback, 0); while(!g_bMPU6050Done); MPU6050Read(&sMPU6050,MPU6050_O_WHO_AM_I,&deviceID,1,MPU6050_Callback,0);//MPU-60X0设备号默认0x68 ROM_SysCtlDelay(ROM_SysCtlClockGet()/(3*1000)); MPU6050Read(&sMPU6050,MPU6050_O_PWR_MGMT_1,&PowerInfo,1,MPU6050_Callback,0);//若读到的值是0x00,则设备处于激活状态。0x40则为休眠状态 }