/* * ======== EK_TM4C123GXL_initI2C ======== */ void EK_TM4C123GXL_initI2C(void) { /* I2C1 Init */ /* Enable the peripheral */ SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1); /* Configure the appropriate pins to be I2C instead of GPIO. */ GPIOPinConfigure(GPIO_PA6_I2C1SCL); GPIOPinConfigure(GPIO_PA7_I2C1SDA); GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6); GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_7); /* I2C3 Init */ /* Enable the peripheral */ SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3); /* Configure the appropriate pins to be I2C instead of GPIO. */ GPIOPinConfigure(GPIO_PD0_I2C3SCL); GPIOPinConfigure(GPIO_PD1_I2C3SDA); GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1); /* * These GPIOs are connected to PD0 and PD1 and need to be brought into a * GPIO input state so they don't interfere with I2C communications. */ GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_6); GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_7); I2C_init(); }
/* * Functions */ void I2CInit(uint32_t i2c_base, uint32_t i2c_speed) { // Initialize GPIO and I2C depending on chosen port switch (i2c_base) { case I2C0_BASE: SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); GPIOPinConfigure(GPIO_PB2_I2C0SCL); GPIOPinConfigure(GPIO_PB3_I2C0SDA); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), i2c_speed); break; case I2C1_BASE: SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6); GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_7); GPIOPinConfigure(GPIO_PA6_I2C1SCL); GPIOPinConfigure(GPIO_PA7_I2C1SDA); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1); I2CMasterInitExpClk(I2C1_BASE, SysCtlClockGet(), i2c_speed); break; case I2C2_BASE: SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); GPIOPinTypeI2CSCL(GPIO_PORTE_BASE, GPIO_PIN_4); GPIOPinTypeI2C(GPIO_PORTE_BASE, GPIO_PIN_5); GPIOPinConfigure(GPIO_PE4_I2C2SCL); GPIOPinConfigure(GPIO_PE5_I2C2SDA); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C2); I2CMasterInitExpClk(I2C2_BASE, SysCtlClockGet(), i2c_speed); break; case I2C3_BASE: SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1); GPIOPinConfigure(GPIO_PD0_I2C3SCL); GPIOPinConfigure(GPIO_PD1_I2C3SDA); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3); I2CMasterInitExpClk(I2C3_BASE, SysCtlClockGet(), i2c_speed); break; } }
/* Set up I2C pins and clock slow/fast rate400 true = 400KHz, false 100KHz */ void MasterI2C0Init(int rate400) { //I2CMasterEnable(I2C0_MASTER_BASE); // causes fault // // Enable the I2C and GPIO port B blocks as they are needed by this driver. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // // Configure the I2C SCL and SDA pins for I2C operation. // GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); GPIOPinConfigure(GPIO_PB2_I2C0SCL); GPIOPinConfigure(GPIO_PB3_I2C0SDA); // // Initialize the I2C master. // ROM_I2CMasterInitExpClk(I2C0_MASTER_BASE, SysCtlClockGet(), rate400); // Register interrupt handler // or we could just edit the startup.c file //I2CIntRegister(I2C0_MASTER_BASE,I2C0IntHandler); // // Enable the I2C interrupt. // ROM_IntEnable(INT_I2C0); // already done via I2CIntRegister // // Enable the I2C master interrupt. // ROM_I2CMasterIntEnable(I2C0_MASTER_BASE); }
void i2c_0_init() { //enable I2C module 0 ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); //reset module ROM_SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0); //enable GPIO peripheral that contains I2C 0 // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // Configure the pin muxing for I2C0 functions on port B2 and B3. ROM_GPIOPinConfigure(GPIO_PIN_2); ROM_GPIOPinConfigure(GPIO_PIN_3); // Select the I2C function for these pins. GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); // Enable and initialize the I2C0 master module. Use the system clock for // the I2C0 module. The last parameter sets the I2C data transfer rate. // If false the data rate is set to 100kbps and if true the data rate will // be set to 400kbps. ROM_I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), true); //clear I2C FIFOs // HWREG(I2C0_BASE + I2C_O_FIFOCTL) = 80008000; }
//Initialize as a slave void TwoWire::begin(uint8_t address) { if(i2cModule == NOT_ACTIVE) { i2cModule = BOOST_PACK_WIRE; } SysCtlPeripheralEnable(g_uli2cPeriph[i2cModule]); GPIOPinConfigure(g_uli2cConfig[i2cModule][0]); GPIOPinConfigure(g_uli2cConfig[i2cModule][1]); GPIOPinTypeI2C(g_uli2cBase[i2cModule], g_uli2cSDAPins[i2cModule]); GPIOPinTypeI2CSCL(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); slaveAddress = address; //Enable slave interrupts 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 I2CMasterDisable(MASTER_BASE); I2CSlaveEnable(SLAVE_BASE); I2CSlaveInit(SLAVE_BASE, address); IntMasterEnable(); }
int main(void) { SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN); SysCtlDelay(10000); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1); GPIOPinConfigure(GPIO_PD0_I2C3SCL); GPIOPinConfigure(GPIO_PD1_I2C3SDA); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3); I2CSlaveInit(I2C3_SLAVE_BASE, I2C_SLAVE_ADDRESS); I2CSlaveAddressSet(I2C3_SLAVE_BASE, I2C_SLAVE_ADDRESS, 0); I2CSlaveIntEnableEx(I2C3_SLAVE_BASE, I2C_SLAVE_INT_START|I2C_SLAVE_INT_STOP|I2C_SLAVE_INT_DATA); I2CSlaveEnable(I2C3_SLAVE_BASE); IntEnable(INT_I2C3); IntMasterEnable(); while(1) { SysCtlDelay(100000); } }
//Initialize as a master void TwoWire::begin(void) { if(i2cModule == NOT_ACTIVE) { i2cModule = BOOST_PACK_WIRE; } SysCtlPeripheralEnable(g_uli2cPeriph[i2cModule]); //Configure GPIO pins for I2C operation GPIOPinConfigure(g_uli2cConfig[i2cModule][0]); GPIOPinConfigure(g_uli2cConfig[i2cModule][1]); GPIOPinTypeI2C(g_uli2cBase[i2cModule], g_uli2cSDAPins[i2cModule]); GPIOPinTypeI2CSCL(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); I2CMasterInitExpClk(MASTER_BASE, F_CPU, false);//max bus speed=400kHz for gyroscope //force a stop condition if(!GPIOPinRead(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule])) forceStop(); //Handle any startup issues by pulsing SCL if(I2CMasterBusBusy(MASTER_BASE) || I2CMasterErr(MASTER_BASE) || !GPIOPinRead(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule])){ uint8_t doI = 0; GPIOPinTypeGPIOOutput(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); unsigned long mask = 0; do{ for(unsigned long i = 0; i < 10 ; i++) { SysCtlDelay(F_CPU/100000/3);//100Hz=desired frequency, delay iteration=3 cycles mask = (i%2) ? g_uli2cSCLPins[i2cModule] : 0; GPIOPinWrite(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule], mask); } doI++; }while(I2CMasterBusBusy(MASTER_BASE) && doI < 100); GPIOPinTypeI2CSCL(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); if(!GPIOPinRead(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule])) forceStop(); } }
//***************************************************************************** // //! Initializes and enables the specified I2C block. //! //! \param ACCEL_I2C_MASTER_BASE is the I2C peripheral to be used. //! \param ulI2CSpeed defines the normal (100kbps) or fast (400kbps) I2C mode. //! //! This function enables the specified I2C block and sets it up to run at //! the either 100kbps or 400kbps. If the \e ulI2CSpeed is false, the I2C will //! run at 100kbps and if true, then the I2C will run at 400kbps. The //! \e ACCEL_I2C_MASTER_BASE parameter can be one of the following values: //! //! \return None. // //***************************************************************************** void i2c_Config(void) { // I2C1 is used with PortA[7:6]. SysCtlPeripheralEnable(ACCEL_I2C_PHERIPHERAL); SysCtlPeripheralEnable(ACCEL_PHERIPHERAL); GPIOPinConfigure(ACCEL_SCL_PIN_CONF); GPIOPinTypeI2CSCL(ACCEL_I2C_PORT, ACCEL_SCL_PIN); GPIOPinConfigure(ACCEL_SDA_PIN_CONF); GPIOPinTypeI2CSCL(ACCEL_I2C_PORT, ACCEL_SDA_PIN); // GPIOPinTypeI2CSCL(ACCEL_I2C_PORT, ACCEL_SCL_PIN); // ROM_GPIOPinTypeI2C(ACCEL_I2C_PORT,ACCEL_SDA_PIN ); // // ROM_GPIOPinConfigure(ACCEL_SCL_PIN_CONF); // ROM_GPIOPinConfigure(ACCEL_SDA_PIN_CONF); // // ROM_SysCtlPeripheralEnable(ACCEL_I2C_PHERIPHERAL); // ROM_I2CMasterInitExpClk(ACCEL_I2C_MASTER_BASE, SysCtlClockGet(), 1); // 1 : 400Khz, 0 : 100Khz }
void Init_I2C(void) { ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); ROM_GPIOPinConfigure(GPIO_PB2_I2C0SCL); ROM_GPIOPinConfigure(GPIO_PB3_I2C0SDA); ROM_GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); ROM_GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_2, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD); ROM_GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_3, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD); ROM_I2CMasterInitExpClk(I2C0_BASE,ROM_SysCtlClockGet(),true); SysCtlDelay(500000); ROM_I2CMasterEnable(I2C0_BASE); }
/* * ======== EKS_LM4F232_initI2C ======== */ Void EKS_LM4F232_initI2C(Void) { /* I2C0 Init */ /* Enable the peripheral */ SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); /* Configure the appropriate pins to be I2C instead of GPIO. */ GPIOPinConfigure(GPIO_PB2_I2C0SCL); GPIOPinConfigure(GPIO_PB3_I2C0SDA); GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); /* I2C2 Init */ /* Enable the peripheral */ SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C2); /* Configure the appropriate pins to be I2C instead of GPIO. */ GPIOPinConfigure(GPIO_PF6_I2C2SCL); GPIOPinConfigure(GPIO_PF7_I2C2SDA); GPIOPinTypeI2CSCL(GPIO_PORTF_BASE, GPIO_PIN_6); GPIOPinTypeI2C(GPIO_PORTF_BASE, GPIO_PIN_7); I2C_init(); }
//***************************************************************************** // //! Initializes and enables the specified I2C block. //! //! \param I2C_PORT is the I2C peripheral to be used. //! \param ulI2CSpeed defines the normal (100kbps) or fast (400kbps) I2C mode. //! //! This function enables the specified I2C block and sets it up to run at //! the either 100kbps or 400kbps. If the \e ulI2CSpeed is false, the I2C will //! run at 100kbps and if true, then the I2C will run at 400kbps. The //! \e I2C_PORT parameter can be one of the following values: //! //! \return None. // //***************************************************************************** void SetupI2C(void) { ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6); ROM_GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_7); ROM_GPIOPinConfigure(GPIO_PA6_I2C1SCL); ROM_GPIOPinConfigure(GPIO_PA7_I2C1SDA); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1); ROM_I2CMasterInitExpClk(I2C_PORT, SysCtlClockGet(), 1); // 1 : 400Khz, 0 : 100Khz }
void TwoWire::forceStop(void) { //force a stop to release the bus GPIOPinTypeGPIOOutput(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule] | g_uli2cSDAPins[i2cModule]); GPIOPinWrite(g_uli2cBase[i2cModule], g_uli2cSDAPins[i2cModule], 0); GPIOPinWrite(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule], g_uli2cSCLPins[i2cModule]); GPIOPinWrite(g_uli2cBase[i2cModule], g_uli2cSDAPins[i2cModule], g_uli2cSDAPins[i2cModule]); GPIOPinTypeI2C(g_uli2cBase[i2cModule], g_uli2cSDAPins[i2cModule]); 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 SysCtlPeripheralReset(g_uli2cPeriph[i2cModule]); while(!SysCtlPeripheralReady(g_uli2cPeriph[i2cModule])); I2CMasterInitExpClk(MASTER_BASE, F_CPU, false); }
void i2cman_init() { //Enable I2C0 which by default uses PortB[3:2] for SDA and SCL respectively SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0); GPIOPinConfigure(GPIO_PB2_I2C0SCL); GPIOPinConfigure(GPIO_PB3_I2C0SDA); GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); I2CMasterInitExpClk(I2C_PORT, SysCtlClockGet(), false); I2CMasterGlitchFilterConfigSet(I2C_PORT, I2C_MASTER_GLITCH_FILTER_32); ROM_I2CMasterIntEnable(I2C_PORT); IntEnable(INT_I2C0); }
void IIC0Init(uint8_t report) { SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); // Enable IIC0 clock SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // Enable IIC0 IO GPIOPinConfigure(GPIO_PB2_I2C0SCL); GPIOPinConfigure(GPIO_PB3_I2C0SDA); GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); // 语句的最后一个参数是用来设定数据传输速率的。 // false表示传输速率是100kbps,true则意味着传输速率是400kbps。 //此处使用的是100kbps的传输速率 I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false); I2CMasterEnable(I2C0_BASE); if(report) UARTprintf("IIC0初始化完成!\r\n"); }
void initI2C0(void) { SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); //reset I2C module SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0); //enable GPIO peripheral that contains I2C SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // Configure the pin muxing for I2C0 functions on port B2 and B3. GPIOPinConfigure(GPIO_PB2_I2C0SCL); GPIOPinConfigure(GPIO_PB3_I2C0SDA); // Select the I2C function for these pins. GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); // Enable and initialize the I2C0 master module. I2CMasterInitExpClk(I2C0_BASE, 80000000, true); //clear I2C FIFOs HWREG(I2C0_BASE + I2C_O_FIFOCTL) = 80008000; }
void initI2C(void) { uint8_t ui8Mask; SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOH); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C7); GPIOPinConfigure(GPIO_PD0_I2C7SCL); GPIOPinConfigure(GPIO_PD1_I2C7SDA); GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1); // // Configure and Enable the GPIO interrupt. Used for INT signal from the // ISL29023 // GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_5); GPIOIntEnable(GPIO_PORTE_BASE, GPIO_PIN_5); GPIOIntTypeSet(GPIO_PORTE_BASE, GPIO_PIN_5, GPIO_FALLING_EDGE); IntEnable(INT_GPIOE); I2CMInit(&g_sI2CInst, I2C7_BASE, INT_I2C7, 0xff, 0xff, g_SysClock); // // Initialize the SHT21. // SHT21Init(&g_sSHT21Inst, &g_sI2CInst, SHT21_I2C_ADDRESS, SHT21AppCallback, &g_sSHT21Inst); SysCtlDelay(g_SysClock / (100 * 3)); // // Initialize the TMP006 // TMP006Init(&g_sTMP006Inst, &g_sI2CInst, TMP006_I2C_ADDRESS, TMP006AppCallback, &g_sTMP006Inst); SysCtlDelay(g_SysClock / (100 * 3)); // // Initialize the BMP180. // BMP180Init(&g_sBMP180Inst, &g_sI2CInst, BMP180_I2C_ADDRESS, BMP180AppCallback, &g_sBMP180Inst); SysCtlDelay(g_SysClock / (100 * 3)); IntPrioritySet(INT_I2C7, 0x00); IntPrioritySet(INT_GPIOM, 0x00); IntPrioritySet(INT_TIMER0A, 0x80); IntPrioritySet(INT_TIMER1A, 0x40); IntPrioritySet(INT_GPIOE, 0x80); IntPrioritySet(INT_UART0, 0x80); // // Initialize the ISL29023 Driver. // ISL29023Init(&g_sISL29023Inst, &g_sI2CInst, ISL29023_I2C_ADDRESS, ISL29023AppCallback, &g_sISL29023Inst); SysCtlDelay(g_SysClock / (100 * 3)); // // Configure the ISL29023 to measure ambient light continuously. Set a 8 // sample persistence before the INT pin is asserted. Clears the INT flag. // Persistence setting of 8 is sufficient to ignore camera flashes. // ui8Mask = (ISL29023_CMD_I_OP_MODE_M | ISL29023_CMD_I_INT_PERSIST_M | ISL29023_CMD_I_INT_FLAG_M); ISL29023ReadModifyWrite(&g_sISL29023Inst, ISL29023_O_CMD_I, ~ui8Mask, (ISL29023_CMD_I_OP_MODE_ALS_CONT | ISL29023_CMD_I_INT_PERSIST_8), ISL29023AppCallback, &g_sISL29023Inst); // // Configure the upper threshold to 80% of maximum value // g_sISL29023Inst.pui8Data[1] = 0xCC; g_sISL29023Inst.pui8Data[2] = 0xCC; ISL29023Write(&g_sISL29023Inst, ISL29023_O_INT_HT_LSB, g_sISL29023Inst.pui8Data, 2, ISL29023AppCallback, &g_sISL29023Inst); // // Wait for transaction to complete // SysCtlDelay(g_SysClock / (100 * 3)); // // Configure the lower threshold to 20% of maximum value // g_sISL29023Inst.pui8Data[1] = 0x33; g_sISL29023Inst.pui8Data[2] = 0x33; ISL29023Write(&g_sISL29023Inst, ISL29023_O_INT_LT_LSB, g_sISL29023Inst.pui8Data, 2, ISL29023AppCallback, &g_sISL29023Inst); // // Wait for transaction to complete // SysCtlDelay(g_SysClock / (100 * 3)); // // Write the command to start a humidity measurement. // SHT21Write(&g_sSHT21Inst, SHT21_CMD_MEAS_RH, g_sSHT21Inst.pui8Data, 0, SHT21AppCallback, &g_sSHT21Inst); // // Wait for transaction to complete // SysCtlDelay(g_SysClock / (100 * 3)); // // Initialize the MPU9150 Driver. // MPU9150Init(&g_sMPU9150Inst, &g_sI2CInst, MPU9150_I2C_ADDRESS, MPU9150AppCallback, &g_sMPU9150Inst); SysCtlDelay(g_SysClock / (100 * 3)); // // Write application specifice sensor configuration such as filter settings // and sensor range settings. // g_sMPU9150Inst.pui8Data[0] = MPU9150_CONFIG_DLPF_CFG_94_98; g_sMPU9150Inst.pui8Data[1] = MPU9150_GYRO_CONFIG_FS_SEL_250; g_sMPU9150Inst.pui8Data[2] = (MPU9150_ACCEL_CONFIG_ACCEL_HPF_5HZ | MPU9150_ACCEL_CONFIG_AFS_SEL_2G); MPU9150Write(&g_sMPU9150Inst, MPU9150_O_CONFIG, g_sMPU9150Inst.pui8Data, 3, MPU9150AppCallback, &g_sMPU9150Inst); SysCtlDelay(g_SysClock / (100 * 3)); // // Initialize the DCM system. 50 hz sample rate. // accel weight = .2, gyro weight = .8, mag weight = .2 // CompDCMInit(&g_sCompDCMInst, 1.0f / 50.0f, 0.2f, 0.6f, 0.2f); SysCtlDelay(g_SysClock / (100 * 3)); ui32CompDCMStarted = 0; // // Print the basic outline of our data table. Done once and then kept as we // print only the data. // // CLI_Write("\033[2J\033[H"); // CLI_Write("MPU9150 9-Axis Simple Data Application Example\n\r\n\r"); // CLI_Write("\033[20GX\033[31G|\033[43GY\033[54G|\033[66GZ\n\r\n\r"); // CLI_Write("Accel\033[8G|\033[31G|\033[54G|\n\r\n\r"); // CLI_Write("Gyro\033[8G|\033[31G|\033[54G|\n\r\n\r"); // CLI_Write("Mag\033[8G|\033[31G|\033[54G|\n\r\n\r"); // CLI_Write("\n\033[20GRoll\033[31G|\033[43GPitch\033[54G|\033[66GYaw\n\r\n\r"); // CLI_Write("Eulers\033[8G|\033[31G|\033[54G|\n\r\n\r"); // CLI_Write("\n\033[17GQ1\033[26G|\033[35GQ2\033[44G|\033[53GQ3\033[62G|" // "\033[71GQ4\n\r\n\r"); // CLI_Write("Q\033[8G|\033[26G|\033[44G|\033[62G|\n\r\n\r"); TimerEnable(TIMER1_BASE, TIMER_A); }
int getAccelValue() { short dataX; short dataY; short dataZ; char printVal[10]; char chPwrCtlReg = 0x2D; char chX0Addr = 0x32; char chY0Addr = 0x34; char chZ0Addr = 0x36; char rgchReadAccl[] = { 0, 0, 0 }; char rgchWriteAccl[] = { 0, 0 }; char rgchReadAccl2[] = { 0, 0, 0 }; char rgchReadAccl3[] = { 0, 0, 0 }; int xDirThreshPos = 50; int xDirThreshNeg = -50; bool fDir = true; bool fClearOled = true; /* * If applicable, reset OLED */ if(fClearOled == true) { //Enable I2C Peripheral SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0); //Set I2C GPIO pins GPIOPinTypeI2C(I2CSDAPort, I2CSDA_PIN); GPIOPinTypeI2CSCL(I2CSCLPort, I2CSCL_PIN); GPIOPinConfigure(I2CSCL); GPIOPinConfigure(I2CSDA); //Setup I2C I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false); //Initialize the Accelerometer GPIOPinTypeGPIOInput(ACCL_INT2Port, ACCL_INT2); rgchWriteAccl[0] = chPwrCtlReg; rgchWriteAccl[1] = 1 << 3; // sets Accl in measurement mode I2CGenTransmit(rgchWriteAccl, 1, WRITE, ACCLADDR); } rgchReadAccl[0] = chX0Addr; rgchReadAccl2[0] = chY0Addr; rgchReadAccl3[0] = chZ0Addr; I2CGenTransmit(rgchReadAccl, 2, READ, ACCLADDR); I2CGenTransmit(rgchReadAccl2, 2, READ, ACCLADDR); I2CGenTransmit(rgchReadAccl3, 2, READ, ACCLADDR); dataX = (rgchReadAccl[2] << 8) | rgchReadAccl[1]; dataY = (rgchReadAccl2[2] << 8) | rgchReadAccl2[1]; dataZ = (rgchReadAccl3[2] << 8) | rgchReadAccl2[1]; return (int)dataY; }
//***************************************************************************** // // Main 'C' Language entry point. // //***************************************************************************** int main(void) { float fTemperature, fPressure, fAltitude; int32_t i32IntegerPart; int32_t i32FractionPart; // // Setup the system clock to run at 40 MHz from PLL with crystal reference // ROM_SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN); // // Initialize the UART. // ConfigureUART(); // // Print the welcome message to the terminal. // UARTprintf("\033[2JBMP180 Example\n"); // // Set the color to a white approximation. // g_pui32Colors[RED] = 0x8000; g_pui32Colors[BLUE] = 0x8000; g_pui32Colors[GREEN] = 0x8000; // // Initialize RGB driver. Use a default intensity and blink rate. // RGBInit(0); RGBColorSet(g_pui32Colors); RGBIntensitySet(0.5f); RGBEnable(); // // The I2C3 peripheral must be enabled before use. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); // // Configure the pin muxing for I2C3 functions on port D0 and D1. // This step is not necessary if your part does not support pin muxing. // ROM_GPIOPinConfigure(GPIO_PD0_I2C3SCL); ROM_GPIOPinConfigure(GPIO_PD1_I2C3SDA); // // 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. // GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); ROM_GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1); // // Initialize the GPIO for the LED. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1); ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0x00); // // Enable interrupts to the processor. // ROM_IntMasterEnable(); // // Initialize the I2C3 peripheral. // I2CMInit(&g_sI2CInst, I2C3_BASE, INT_I2C3, 0xff, 0xff, ROM_SysCtlClockGet()); // // Initialize the BMP180. // BMP180Init(&g_sBMP180Inst, &g_sI2CInst, BMP180_I2C_ADDRESS, BMP180AppCallback, &g_sBMP180Inst); // // Wait for initialization callback to indicate reset request is complete. // while(g_vui8DataFlag == 0) { // // Wait for I2C Transactions to complete. // } // // Reset the data ready flag // g_vui8DataFlag = 0; // // Enable the system ticks at 10 Hz. // ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / (10 * 3)); ROM_SysTickIntEnable(); ROM_SysTickEnable(); // // After all the init and config we start blink the LED // RGBBlinkRateSet(1.0f); // // Begin the data collection and printing. Loop Forever. // while(1) { // // Read the data from the BMP180 over I2C. This command starts a // temperature measurement. Then polls until temperature is ready. // Then automatically starts a pressure measurement and polls for that // to complete. When both measurement are complete and in the local // buffer then the application callback is called from the I2C // interrupt context. Polling is done on I2C interrupts allowing // processor to continue doing other tasks as needed. // BMP180DataRead(&g_sBMP180Inst, BMP180AppCallback, &g_sBMP180Inst); while(g_vui8DataFlag == 0) { // // Wait for the new data set to be available. // } // // Reset the data ready flag. // g_vui8DataFlag = 0; // // Get a local copy of the latest temperature data in float format. // BMP180DataTemperatureGetFloat(&g_sBMP180Inst, &fTemperature); // // Convert the floats to an integer part and fraction part for easy // print. // i32IntegerPart = (int32_t) fTemperature; i32FractionPart =(int32_t) (fTemperature * 1000.0f); i32FractionPart = i32FractionPart - (i32IntegerPart * 1000); if(i32FractionPart < 0) { i32FractionPart *= -1; } // // Print temperature with three digits of decimal precision. // UARTprintf("Temperature %3d.%03d\t\t", i32IntegerPart, i32FractionPart); // // Get a local copy of the latest air pressure data in float format. // BMP180DataPressureGetFloat(&g_sBMP180Inst, &fPressure); // // Convert the floats to an integer part and fraction part for easy // print. // i32IntegerPart = (int32_t) fPressure; i32FractionPart =(int32_t) (fPressure * 1000.0f); i32FractionPart = i32FractionPart - (i32IntegerPart * 1000); if(i32FractionPart < 0) { i32FractionPart *= -1; } // // Print Pressure with three digits of decimal precision. // UARTprintf("Pressure %3d.%03d\t\t", i32IntegerPart, i32FractionPart); // // Calculate the altitude. // fAltitude = 44330.0f * (1.0f - powf(fPressure / 101325.0f, 1.0f / 5.255f)); // // Convert the floats to an integer part and fraction part for easy // print. // i32IntegerPart = (int32_t) fAltitude; i32FractionPart =(int32_t) (fAltitude * 1000.0f); i32FractionPart = i32FractionPart - (i32IntegerPart * 1000); if(i32FractionPart < 0) { i32FractionPart *= -1; } // // Print altitude with three digits of decimal precision. // UARTprintf("Altitude %3d.%03d", i32IntegerPart, i32FractionPart); // // Print new line. // UARTprintf("\n"); // // Delay to keep printing speed reasonable. About 100 milliseconds. // ROM_SysCtlDelay(ROM_SysCtlClockGet() / (10 * 3)); }//while end }
//***************************************************************************** // // Main 'C' Language entry point. // //***************************************************************************** int main(void) { float fAmbient; int32_t i32IntegerPart, i32FractionPart; uint8_t ui8Mask; // // Setup the system clock to run at 40 Mhz from PLL with crystal reference // ROM_SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN); // // Enable the peripherals used by this example. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); // // Initialize the UART and its pins. // ConfigureUART(); // // Print the welcome message to the terminal. // UARTprintf("\033[2JISL29023 Example\n"); // // Set the color to a white approximation. // g_pui32Colors[RED] = 0x8000; g_pui32Colors[BLUE] = 0x8000; g_pui32Colors[GREEN] = 0x8000; // // Initialize RGB driver. Use a default intensity and blink rate. // RGBInit(0); RGBColorSet(g_pui32Colors); RGBIntensitySet(0.5f); RGBEnable(); // // The I2C3 peripheral must be enabled before use. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); // // Configure the pin muxing for I2C3 functions on port D0 and D1. // This step is not necessary if your part does not support pin muxing. // ROM_GPIOPinConfigure(GPIO_PD0_I2C3SCL); ROM_GPIOPinConfigure(GPIO_PD1_I2C3SDA); // // 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. // GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); ROM_GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1); // // Configure and Enable the GPIO interrupt. Used for INT signal from the // ISL29023 // ROM_GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_5); GPIOIntEnable(GPIO_PORTE_BASE, GPIO_PIN_5); ROM_GPIOIntTypeSet(GPIO_PORTE_BASE, GPIO_PIN_5, GPIO_FALLING_EDGE); ROM_IntEnable(INT_GPIOE); // // 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 // TIMER0, TIMER1 and WTIMER5 are used by the RGB driver // I2C3 is the I2C interface to the ISL29023 // ROM_SysCtlPeripheralClockGating(true); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_GPIOE); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_UART0); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_TIMER0); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_TIMER1); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_I2C3); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_WTIMER5); // // Configure desired interrupt priorities. Setting the I2C interrupt to be // of more priority than SysTick and the GPIO interrupt means those // interrupt routines can use the I2CM_DRV Application context does not use // I2CM_DRV API and GPIO and SysTick are at the same priority level. This // prevents re-entrancy problems with I2CM_DRV but keeps the MCU in sleep // state as much as possible. UART is at least priority so it can operate // in the background. // ROM_IntPrioritySet(INT_I2C3, 0x00); ROM_IntPrioritySet(FAULT_SYSTICK, 0x40); ROM_IntPrioritySet(INT_GPIOE, 0x80); ROM_IntPrioritySet(INT_UART0, 0x80); // // Enable interrupts to the processor. // ROM_IntMasterEnable(); // // Initialize I2C3 peripheral. // I2CMInit(&g_sI2CInst, I2C3_BASE, INT_I2C3, 0xff, 0xff, ROM_SysCtlClockGet()); // // Initialize the ISL29023 Driver. // ISL29023Init(&g_sISL29023Inst, &g_sI2CInst, ISL29023_I2C_ADDRESS, ISL29023AppCallback, &g_sISL29023Inst); // // Wait for transaction to complete // ISL29023AppI2CWait(__FILE__, __LINE__); // // Configure the ISL29023 to measure ambient light continuously. Set a 8 // sample persistence before the INT pin is asserted. Clears the INT flag. // Persistence setting of 8 is sufficient to ignore camera flashes. // ui8Mask = (ISL29023_CMD_I_OP_MODE_M | ISL29023_CMD_I_INT_PERSIST_M | ISL29023_CMD_I_INT_FLAG_M); ISL29023ReadModifyWrite(&g_sISL29023Inst, ISL29023_O_CMD_I, ~ui8Mask, (ISL29023_CMD_I_OP_MODE_ALS_CONT | ISL29023_CMD_I_INT_PERSIST_8), ISL29023AppCallback, &g_sISL29023Inst); // // Wait for transaction to complete // ISL29023AppI2CWait(__FILE__, __LINE__); // // Configure the upper threshold to 80% of maximum value // g_sISL29023Inst.pui8Data[1] = 0xCC; g_sISL29023Inst.pui8Data[2] = 0xCC; ISL29023Write(&g_sISL29023Inst, ISL29023_O_INT_HT_LSB, g_sISL29023Inst.pui8Data, 2, ISL29023AppCallback, &g_sISL29023Inst); // // Wait for transaction to complete // ISL29023AppI2CWait(__FILE__, __LINE__); // // Configure the lower threshold to 20% of maximum value // g_sISL29023Inst.pui8Data[1] = 0x33; g_sISL29023Inst.pui8Data[2] = 0x33; ISL29023Write(&g_sISL29023Inst, ISL29023_O_INT_LT_LSB, g_sISL29023Inst.pui8Data, 2, ISL29023AppCallback, &g_sISL29023Inst); // // Wait for transaction to complete // ISL29023AppI2CWait(__FILE__, __LINE__); // //Configure and enable SysTick Timer // ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / SYSTICKS_PER_SECOND); ROM_SysTickIntEnable(); ROM_SysTickEnable(); // // After all the init and config we start blink the LED // RGBBlinkRateSet(1.0f); // // Loop Forever // while(1) { ROM_SysCtlSleep(); if(g_vui8DataFlag) { g_vui8DataFlag = 0; // // Get a local floating point copy of the latest light data // ISL29023DataLightVisibleGetFloat(&g_sISL29023Inst, &fAmbient); // // Perform the conversion from float to a printable set of integers // i32IntegerPart = (int32_t)fAmbient; i32FractionPart = (int32_t)(fAmbient * 1000.0f); i32FractionPart = i32FractionPart - (i32IntegerPart * 1000); if(i32FractionPart < 0) { i32FractionPart *= -1; } // // Print the temperature as integer and fraction parts. // UARTprintf("Visible Lux: %3d.%03d\n", i32IntegerPart, i32FractionPart); // // Check if the intensity of light has crossed a threshold. If so // then adjust range of sensor readings to track intensity. // if(g_vui8IntensityFlag) { // // Disable the low priority interrupts leaving only the I2C // interrupt enabled. // ROM_IntPriorityMaskSet(0x40); // // Reset the intensity trigger flag. // g_vui8IntensityFlag = 0; // // Adjust the lux range. // ISL29023AppAdjustRange(&g_sISL29023Inst); // // Now we must manually clear the flag in the ISL29023 // register. // ISL29023Read(&g_sISL29023Inst, ISL29023_O_CMD_I, g_sISL29023Inst.pui8Data, 1, ISL29023AppCallback, &g_sISL29023Inst); // // Wait for transaction to complete // ISL29023AppI2CWait(__FILE__, __LINE__); // // Disable priority masking so all interrupts are enabled. // ROM_IntPriorityMaskSet(0); } } } }
void main (void) { SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3); ROM_SysCtlClockSet (SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN); IntMasterEnable(); ConfigureUART(); ConfigureGPRS(); float fAccel[3]; tMPU9150 sMPU9150; UARTprintf("Point 0\n"); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); ROM_GPIOPinConfigure(GPIO_PD0_I2C3SCL); ROM_GPIOPinConfigure(GPIO_PD1_I2C3SDA); // // 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. // GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); ROM_GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1); // // Initialize the MPU9150. This code assumes that the I2C master instance // has already been initialized. // I2CMInit(&sI2CInst, I2C3_BASE, INT_I2C3, 0xff, 0xff, ROM_SysCtlClockGet()); GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 4); UARTprintf("Point 2\n"); g_bMPU9150Done = false; MPU9150Init(&sMPU9150, &sI2CInst, 0x68, MPU9150Callback, 0); while(!g_bMPU9150Done) { } // // Configure the MPU9150 for +/- 4 g accelerometer range. // UARTprintf("Point 3\n"); g_bMPU9150Done = false; // MPU9150ReadModifyWrite(&sMPU9150, MPU9150_O_ACCEL_CONFIG, // ~MPU9150_ACCEL_CONFIG_AFS_SEL_M, // MPU9150_ACCEL_CONFIG_AFS_SEL_16G, MPU9150Callback, // 0); sMPU9150.pui8Data[0] = MPU9150_CONFIG_DLPF_CFG_94_98; sMPU9150.pui8Data[1] = MPU9150_GYRO_CONFIG_FS_SEL_250; sMPU9150.pui8Data[2] = (MPU9150_ACCEL_CONFIG_ACCEL_HPF_5HZ | MPU9150_ACCEL_CONFIG_AFS_SEL_16G); MPU9150Write(&sMPU9150, MPU9150_O_CONFIG, sMPU9150.pui8Data, 3, MPU9150Callback, 0); // while(1){} while(!g_bMPU9150Done) { } // // Loop forever reading data from the MPU9150. Typically, this process // would be done in the background, but for the purposes of this example, // it is shown in an infinite loop. // int count=0; int prev_count=-100; int is_acc=0; float imp[3]; int j=0; for(;j<2;j++) imp[j]=0; float curr_avg=0; while(1) { GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 8); // // Request another reading from the MPU9150. // g_bMPU9150Done = false; if(!MPU9150DataRead(&sMPU9150, MPU9150Callback, 0)) continue; while(!g_bMPU9150Done) { } // // Get the new accelerometer, gyroscope, and magnetometer readings. // float backup[3]; int l=0; if(count!=0)for(;l<3;l++)backup[l]=fAccel[l]; MPU9150DataAccelGetFloat(&sMPU9150, &fAccel[0], &fAccel[1], &fAccel[2]); // MPU9150DataGyroGetFloat(&sMPU9150, &fGyro[0], &fGyro[1], &fGyro[2]); // MPU9150DataMagnetoGetFloat(&sMPU9150, &fMagneto[0], &fMagneto[1], // &fMagneto[2]); // float factor = 0.0011970964; // UARTprintf("%d %d %d ",(int)(fMagneto[0]*1000), (int)(fMagneto[1]*1000),(int)(fMagneto[2]*1000)); // UARTprintf("Accel %d %d %d \n",(int)(fAccel[0]*1000), (int)(fAccel[1]*1000),(int)(fAccel[2]*1000)); // if(count ==0)UARTprintf("\n"); // UARTprintf("Gyro %d %d %d \n",(int)(fGyro[0]*1000), (int)(fGyro[1]*1000),(int)(fGyro[2]*1000)); // int iter; // for(iter=0;iter<6;iter++) // UARTprintf("%f %f %f \n",fAccel[0]*factor, fAccel[1] *factor,fAccel[2]*factor ); float temp = check_acc(fAccel, backup); curr_avg = curr_avg + (temp - curr_avg)/(count +1); if(is_acc && count< prev_count+ 400) { int j=0; for(;j<2;j++)imp[j]+=(fAccel[j] - backup[j]); } if(is_acc && count == prev_count + 400) { is_acc = 0; UARTprintf("Impulse %d %d %d \n",(int)(imp[0]*1000), (int)(imp[1]*1000),(int)(imp[2]*1000)); int side = 0; int sign=0; int j=0, max_imp = 0; for(;j<2;j++) if(imp[j]*imp[j] > max_imp) { max_imp = imp[j]*imp[j]; side = j; sign = imp[j] > 0 ? 1 : -1; } send_accident_data(side, sign); j=0; for(;j<2;j++)imp[j]=0; } if(count!=0 && temp >= thres && count >= prev_count + 400) { UARTprintf("Accel %d %d %d ",(int)(fAccel[0]*1000), (int)(fAccel[1]*1000),(int)(fAccel[2]*1000)); prev_count = count; is_acc=1; } count++; if(count % 5000 ==0) UARTprintf("Driver stats : %d\r\n", (int)(curr_avg)); if(count % 50000 == 0) { int rating = driver_rating(curr_avg); // UARTprintf("Sending data to server\r\n"); memset(command,0,200); strcpy(command,"AT+HTTPPARA=\"URL\",\"embedded-roshanroshan.rhcloud.com/add/Driver_rating="); itoa(rating, command+strlen(command)); strcpy(command+strlen(command), "\""); send_AT_command(command,NULL); send_AT_command("AT+HTTPACTION=0",NULL); count=0; curr_avg = 0; } GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0); // SysCtlDelay(5000000); // // Do something with the new accelerometer, gyroscope, and magnetometer // readings. // } }
//***************************************************************************** // //! Initializes and enables the specified I2C block. //! //! \param ulI2CBase is the I2C peripheral to be used. //! \param ulI2CSpeed defines the normal (100kbps) or fast (400kbps) I2C mode. //! //! This function enables the specified I2C block and sets it up to run at //! the either 100kbps or 400kbps. If the \e ulI2CSpeed is false, the I2C will //! run at 100kbps and if true, then the I2C will run at 400kbps. The //! \e ulI2CBase parameter can be one of the following values: //! //! - \b I2C0_MASTER_BASE //! - \b I2C1_MASTER_BASE //! - \b I2C2_MASTER_BASE //! - \b I2C3_MASTER_BASE //! //! \return None. // //***************************************************************************** void I2CSetup(unsigned long ulI2CBase, unsigned long ulI2CSpeed) { // // Check the arguments. // ASSERT(I2CMasterBaseValid(ulI2CBase)); ASSERT((ulI2CSpeed == true) || (ulI2CSpeed == false)); switch (ulI2CBase) { // I2C_PERIPH_0 case I2C0_MASTER_BASE: // // I2C0 is used with PortB[3:2]. The actual port and // pins used may be different on your part, consult the data sheet for // more information. GPIO port B needs to be enabled so these pins can // be used. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // // Select the I2C function for these pins. This function will also // configure the GPIO 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. // GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); // special I2CSCL treatment for M4F devices GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); // // Configure the pin muxing for I2C0 functions on port B2 and B3. // This step is not necessary if your part does not support pin muxing. // GPIOPinConfigure(GPIO_PB2_I2C0SCL); GPIOPinConfigure(GPIO_PB3_I2C0SDA); // // The I2C0 peripheral must be enabled before use. // SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); // // Enable and initialize the I2C0 master module. // I2CMasterInitExpClk(I2C0_MASTER_BASE, SysCtlClockGet(), ulI2CSpeed); break; // I2C_PERIPH_1 case I2C1_MASTER_BASE: // // I2C1 is used with PortA[7:6]. The actual port and // pins used may be different on your part, consult the data sheet for // more information. GPIO port A needs to be enabled so these pins can // be used. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // // Select the I2C function for these pins. This function will also // configure the GPIO 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. // GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6); // special I2CSCL treatment for M4F devices GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_7); // // Configure the pin muxing for I2C1 functions on port A6 and A7. // This step is not necessary if your part does not support pin muxing. // GPIOPinConfigure(GPIO_PA6_I2C1SCL); GPIOPinConfigure(GPIO_PA7_I2C1SDA); // // The I2C1 peripheral must be enabled before use. // SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1); // // Enable and initialize the I2C1 master module. // I2CMasterInitExpClk(I2C1_MASTER_BASE, SysCtlClockGet(), ulI2CSpeed); break; // I2C_PERIPH_2 case I2C2_MASTER_BASE: // // I2C2 is used with PortE[5:4]. The actual port and // pins used may be different on your part, consult the data sheet for // more information. GPIO port E needs to be enabled so these pins can // be used. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); // // Select the I2C function for these pins. This function will also // configure the GPIO 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. // GPIOPinTypeI2CSCL(GPIO_PORTE_BASE, GPIO_PIN_4); // special I2CSCL treatment for M4F devices GPIOPinTypeI2C(GPIO_PORTE_BASE, GPIO_PIN_5); // // Configure the pin muxing for I2C2 functions on port E4 and E5. // This step is not necessary if your part does not support pin muxing. // GPIOPinConfigure(GPIO_PE4_I2C2SCL); GPIOPinConfigure(GPIO_PE5_I2C2SDA); // // The I2C2 peripheral must be enabled before use. // SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C2); // // Enable and initialize the I2C2 master module. // I2CMasterInitExpClk(I2C2_MASTER_BASE, SysCtlClockGet(), ulI2CSpeed); break; // I2C_PERIPH_3 case I2C3_MASTER_BASE: // // I2C3 is used with PortD[1:0]. The actual port and // pins used may be different on your part, consult the data sheet for // more information. GPIO port D needs to be enabled so these pins can // be used. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); // // Select the I2C function for these pins. This function will also // configure the GPIO 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. // GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); // special I2CSCL treatment for M4F devices GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1); // // Configure the pin muxing for I2C2 functions on port D0 and D1. // This step is not necessary if your part does not support pin muxing. // GPIOPinConfigure(GPIO_PD0_I2C3SCL); GPIOPinConfigure(GPIO_PD1_I2C3SDA); // // The I2C3 peripheral must be enabled before use. // SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3); // // Enable and initialize the I2C3 master module. // I2CMasterInitExpClk(I2C3_MASTER_BASE, SysCtlClockGet(), ulI2CSpeed); break; } }
//***************************************************************************** // // Main application entry point. // //***************************************************************************** int main(void) { int_fast32_t i32IPart[16], i32FPart[16]; uint_fast32_t ui32Idx, ui32CompDCMStarted; float pfData[16]; float *pfAccel, *pfGyro, *pfMag, *pfEulers, *pfQuaternion; // // Initialize convenience pointers that clean up and clarify the code // meaning. We want all the data in a single contiguous array so that // we can make our pretty printing easier later. // pfAccel = pfData; pfGyro = pfData + 3; pfMag = pfData + 6; pfEulers = pfData + 9; pfQuaternion = pfData + 12; // // Setup the system clock to run at 40 Mhz from PLL with crystal reference // SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN); // // Enable port B used for motion interrupt. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // // Initialize the UART. // ConfigureUART(); // // Print the welcome message to the terminal. // UARTprintf("\033[2JMPU9150 Raw Example\n"); // // Set the color to a purple approximation. // g_pui32Colors[RED] = 0x8000; g_pui32Colors[BLUE] = 0x8000; g_pui32Colors[GREEN] = 0x0000; // // Initialize RGB driver. // RGBInit(0); RGBColorSet(g_pui32Colors); RGBIntensitySet(0.5f); RGBEnable(); // // The I2C3 peripheral must be enabled before use. // SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); // // Configure the pin muxing for I2C3 functions on port D0 and D1. // GPIOPinConfigure(GPIO_PD0_I2C3SCL); GPIOPinConfigure(GPIO_PD1_I2C3SDA); // // 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. // GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1); // // Configure and Enable the GPIO interrupt. Used for INT signal from the // MPU9150 // GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_2); GPIOIntEnable(GPIO_PORTB_BASE, GPIO_PIN_2); GPIOIntTypeSet(GPIO_PORTB_BASE, GPIO_PIN_2, GPIO_FALLING_EDGE); IntEnable(INT_GPIOB); // // Keep only some parts of the systems running while in sleep mode. // GPIOB is for the MPU9150 interrupt pin. // UART0 is the virtual serial port // TIMER0, TIMER1 and WTIMER5 are used by the RGB driver // I2C3 is the I2C interface to the ISL29023 // SysCtlPeripheralClockGating(true); SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_GPIOB); SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_UART0); SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_TIMER0); SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_TIMER1); SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_I2C3); SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_WTIMER5); // // Enable interrupts to the processor. // IntMasterEnable(); // // Initialize I2C3 peripheral. // I2CMInit(&g_sI2CInst, I2C3_BASE, INT_I2C3, 0xff, 0xff, SysCtlClockGet()); // // Initialize the MPU9150 Driver. // MPU9150Init(&g_sMPU9150Inst, &g_sI2CInst, MPU9150_I2C_ADDRESS, MPU9150AppCallback, &g_sMPU9150Inst); // // Wait for transaction to complete // MPU9150AppI2CWait(__FILE__, __LINE__); // // Write application specifice sensor configuration such as filter settings // and sensor range settings. // g_sMPU9150Inst.pui8Data[0] = MPU9150_CONFIG_DLPF_CFG_94_98; g_sMPU9150Inst.pui8Data[1] = MPU9150_GYRO_CONFIG_FS_SEL_250; g_sMPU9150Inst.pui8Data[2] = (MPU9150_ACCEL_CONFIG_ACCEL_HPF_5HZ | MPU9150_ACCEL_CONFIG_AFS_SEL_2G); MPU9150Write(&g_sMPU9150Inst, MPU9150_O_CONFIG, g_sMPU9150Inst.pui8Data, 3, MPU9150AppCallback, &g_sMPU9150Inst); // // Wait for transaction to complete // MPU9150AppI2CWait(__FILE__, __LINE__); // // Configure the data ready interrupt pin output of the MPU9150. // g_sMPU9150Inst.pui8Data[0] = MPU9150_INT_PIN_CFG_INT_LEVEL | MPU9150_INT_PIN_CFG_INT_RD_CLEAR | MPU9150_INT_PIN_CFG_LATCH_INT_EN; g_sMPU9150Inst.pui8Data[1] = MPU9150_INT_ENABLE_DATA_RDY_EN; MPU9150Write(&g_sMPU9150Inst, MPU9150_O_INT_PIN_CFG, g_sMPU9150Inst.pui8Data, 2, MPU9150AppCallback, &g_sMPU9150Inst); // // Wait for transaction to complete // MPU9150AppI2CWait(__FILE__, __LINE__); // // Initialize the DCM system. 50 hz sample rate. // accel weight = .2, gyro weight = .8, mag weight = .2 // CompDCMInit(&g_sCompDCMInst, 1.0f / 50.0f, 0.2f, 0.6f, 0.2f); UARTprintf("\033[2J\033[H"); UARTprintf("MPU9150 9-Axis Simple Data Application Example\n\n"); UARTprintf("\033[20GX\033[31G|\033[43GY\033[54G|\033[66GZ\n\n"); UARTprintf("Accel\033[8G|\033[31G|\033[54G|\n\n"); UARTprintf("Gyro\033[8G|\033[31G|\033[54G|\n\n"); UARTprintf("Mag\033[8G|\033[31G|\033[54G|\n\n"); UARTprintf("\n\033[20GRoll\033[31G|\033[43GPitch\033[54G|\033[66GYaw\n\n"); UARTprintf("Eulers\033[8G|\033[31G|\033[54G|\n\n"); UARTprintf("\n\033[17GQ1\033[26G|\033[35GQ2\033[44G|\033[53GQ3\033[62G|" "\033[71GQ4\n\n"); UARTprintf("Q\033[8G|\033[26G|\033[44G|\033[62G|\n\n"); // // Enable blinking indicates config finished successfully // RGBBlinkRateSet(1.0f); ui32CompDCMStarted = 0; while(1) { // // Go to sleep mode while waiting for data ready. // while(!g_vui8I2CDoneFlag) { SysCtlSleep(); } // // Clear the flag // g_vui8I2CDoneFlag = 0; // // Get floating point version of the Accel Data in m/s^2. // MPU9150DataAccelGetFloat(&g_sMPU9150Inst, pfAccel, pfAccel + 1, pfAccel + 2); // // Get floating point version of angular velocities in rad/sec // MPU9150DataGyroGetFloat(&g_sMPU9150Inst, pfGyro, pfGyro + 1, pfGyro + 2); // // Get floating point version of magnetic fields strength in tesla // MPU9150DataMagnetoGetFloat(&g_sMPU9150Inst, pfMag, pfMag + 1, pfMag + 2); // // Check if this is our first data ever. // if(ui32CompDCMStarted == 0) { // // Set flag indicating that DCM is started. // Perform the seeding of the DCM with the first data set. // ui32CompDCMStarted = 1; CompDCMMagnetoUpdate(&g_sCompDCMInst, pfMag[0], pfMag[1], pfMag[2]); CompDCMAccelUpdate(&g_sCompDCMInst, pfAccel[0], pfAccel[1], pfAccel[2]); CompDCMGyroUpdate(&g_sCompDCMInst, pfGyro[0], pfGyro[1], pfGyro[2]); CompDCMStart(&g_sCompDCMInst); } else { // // DCM Is already started. Perform the incremental update. // CompDCMMagnetoUpdate(&g_sCompDCMInst, pfMag[0], pfMag[1], pfMag[2]); CompDCMAccelUpdate(&g_sCompDCMInst, pfAccel[0], pfAccel[1], pfAccel[2]); CompDCMGyroUpdate(&g_sCompDCMInst, -pfGyro[0], -pfGyro[1], -pfGyro[2]); CompDCMUpdate(&g_sCompDCMInst); } // // Increment the skip counter. Skip counter is used so we do not // overflow the UART with data. // g_ui32PrintSkipCounter++; if(g_ui32PrintSkipCounter >= PRINT_SKIP_COUNT) { // // Reset skip counter. // g_ui32PrintSkipCounter = 0; // // Get Euler data. (Roll Pitch Yaw) // CompDCMComputeEulers(&g_sCompDCMInst, pfEulers, pfEulers + 1, pfEulers + 2); // // Get Quaternions. // CompDCMComputeQuaternion(&g_sCompDCMInst, pfQuaternion); // // convert mag data to micro-tesla for better human interpretation. // pfMag[0] *= 1e6; pfMag[1] *= 1e6; pfMag[2] *= 1e6; // // Convert Eulers to degrees. 180/PI = 57.29... // Convert Yaw to 0 to 360 to approximate compass headings. // pfEulers[0] *= 57.295779513082320876798154814105f; pfEulers[1] *= 57.295779513082320876798154814105f; pfEulers[2] *= 57.295779513082320876798154814105f; if(pfEulers[2] < 0) { pfEulers[2] += 360.0f; } // // Now drop back to using the data as a single array for the // purpose of decomposing the float into a integer part and a // fraction (decimal) part. // for(ui32Idx = 0; ui32Idx < 16; ui32Idx++) { // // Conver float value to a integer truncating the decimal part. // i32IPart[ui32Idx] = (int32_t) pfData[ui32Idx]; // // Multiply by 1000 to preserve first three decimal values. // Truncates at the 3rd decimal place. // i32FPart[ui32Idx] = (int32_t) (pfData[ui32Idx] * 1000.0f); // // Subtract off the integer part from this newly formed decimal // part. // i32FPart[ui32Idx] = i32FPart[ui32Idx] - (i32IPart[ui32Idx] * 1000); // // make the decimal part a positive number for display. // if(i32FPart[ui32Idx] < 0) { i32FPart[ui32Idx] *= -1; } } // // Print the acceleration numbers in the table. // UARTprintf("\033[5;17H%3d.%03d", i32IPart[0], i32FPart[0]); UARTprintf("\033[5;40H%3d.%03d", i32IPart[1], i32FPart[1]); UARTprintf("\033[5;63H%3d.%03d", i32IPart[2], i32FPart[2]); // // Print the angular velocities in the table. // UARTprintf("\033[7;17H%3d.%03d", i32IPart[3], i32FPart[3]); UARTprintf("\033[7;40H%3d.%03d", i32IPart[4], i32FPart[4]); UARTprintf("\033[7;63H%3d.%03d", i32IPart[5], i32FPart[5]); // // Print the magnetic data in the table. // UARTprintf("\033[9;17H%3d.%03d", i32IPart[6], i32FPart[6]); UARTprintf("\033[9;40H%3d.%03d", i32IPart[7], i32FPart[7]); UARTprintf("\033[9;63H%3d.%03d", i32IPart[8], i32FPart[8]); // // Print the Eulers in a table. // UARTprintf("\033[14;17H%3d.%03d", i32IPart[9], i32FPart[9]); UARTprintf("\033[14;40H%3d.%03d", i32IPart[10], i32FPart[10]); UARTprintf("\033[14;63H%3d.%03d", i32IPart[11], i32FPart[11]); // // Print the quaternions in a table format. // UARTprintf("\033[19;14H%3d.%03d", i32IPart[12], i32FPart[12]); UARTprintf("\033[19;32H%3d.%03d", i32IPart[13], i32FPart[13]); UARTprintf("\033[19;50H%3d.%03d", i32IPart[14], i32FPart[14]); UARTprintf("\033[19;68H%3d.%03d", i32IPart[15], i32FPart[15]); } } }
int getAccelY(){ short dataX; short dataY; short dataZ; char printVal[10]; char chPwrCtlReg = 0x2D; char chX0Addr = 0x32; char chY0Addr = 0x34; char chZ0Addr = 0x36; char rgchReadAccl[] = { 0, 0, 0 }; char rgchWriteAccl[] = { 0, 0 }; char rgchReadAccl2[] = { 0, 0, 0 }; char rgchReadAccl3[] = { 0, 0, 0 }; /*int xcoRocketCur = xcoRocketStart; int ycoRocketCur = ycoRocketStart; int xcoExhstCur = xcoExhstStart; int ycoExhstCur = ycoExhstStart; int xDirThreshPos = 50; int xDirThreshNeg = -50; bool fDir = true;*/ /* * Enable I2C Peripheral */ SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0); /* * Set I2C GPIO pins */ GPIOPinTypeI2C(I2CSDAPort, I2CSDA_PIN); GPIOPinTypeI2CSCL(I2CSCLPort, I2CSCL_PIN); GPIOPinConfigure(I2CSCL); GPIOPinConfigure(I2CSDA); /* * Setup I2C */ I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false); /* Initialize the Accelerometer * */ GPIOPinTypeGPIOInput(ACCL_INT2Port, ACCL_INT2); rgchWriteAccl[0] = chPwrCtlReg; rgchWriteAccl[1] = 1 << 3; // sets Accl in measurement mode I2CGenTransmit(rgchWriteAccl, 1, WRITE, ACCLADDR); /* * Loop and check for movement until switches * change */ /* * Read the X data register */ rgchReadAccl[0] = chX0Addr; rgchReadAccl2[0] = chY0Addr; rgchReadAccl3[0] = chZ0Addr; I2CGenTransmit(rgchReadAccl, 2, READ, ACCLADDR); I2CGenTransmit(rgchReadAccl2, 2, READ, ACCLADDR); I2CGenTransmit(rgchReadAccl3, 2, READ, ACCLADDR); dataX = (rgchReadAccl[2] << 8) | rgchReadAccl[1]; dataY = (rgchReadAccl2[2] << 8) | rgchReadAccl2[1]; dataZ = (rgchReadAccl3[2] << 8) | rgchReadAccl2[1]; return dataY; }
//***************************************************************************** // // Initialize the I2C, MPU9150 and Gesture systems. // //***************************************************************************** void MotionInit(void) { // // Enable port B used for motion interrupt. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // // The I2C3 peripheral must be enabled before use. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); // // Configure the pin muxing for I2C3 functions on port D0 and D1. // ROM_GPIOPinConfigure(GPIO_PD0_I2C3SCL); ROM_GPIOPinConfigure(GPIO_PD1_I2C3SDA); // // 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. // GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); ROM_GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1); // // Configure and Enable the GPIO interrupt. Used for INT signal from the // MPU9150 // ROM_GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_2); GPIOIntEnable(GPIO_PORTB_BASE, GPIO_PIN_2); ROM_GPIOIntTypeSet(GPIO_PORTB_BASE, GPIO_PIN_2, GPIO_FALLING_EDGE); ROM_IntEnable(INT_GPIOB); // // Enable interrupts to the processor. // ROM_IntMasterEnable(); // // Initialize I2C3 peripheral. // I2CMInit(&g_sI2CInst, I2C3_BASE, INT_I2C3, 0xff, 0xff, ROM_SysCtlClockGet()); // // Set the motion state to initializing. // g_ui8MotionState = MOTION_STATE_INIT; // // Initialize the MPU9150 Driver. // MPU9150Init(&g_sMPU9150Inst, &g_sI2CInst, MPU9150_I2C_ADDRESS, MotionCallback, &g_sMPU9150Inst); // // Wait for transaction to complete // MotionI2CWait(__FILE__, __LINE__); // // Write application specifice sensor configuration such as filter settings // and sensor range settings. // g_sMPU9150Inst.pui8Data[0] = MPU9150_CONFIG_DLPF_CFG_94_98; g_sMPU9150Inst.pui8Data[1] = MPU9150_GYRO_CONFIG_FS_SEL_250; g_sMPU9150Inst.pui8Data[2] = (MPU9150_ACCEL_CONFIG_ACCEL_HPF_5HZ | MPU9150_ACCEL_CONFIG_AFS_SEL_2G); MPU9150Write(&g_sMPU9150Inst, MPU9150_O_CONFIG, g_sMPU9150Inst.pui8Data, 3, MotionCallback, &g_sMPU9150Inst); // // Wait for transaction to complete // MotionI2CWait(__FILE__, __LINE__); // // Configure the data ready interrupt pin output of the MPU9150. // g_sMPU9150Inst.pui8Data[0] = (MPU9150_INT_PIN_CFG_INT_LEVEL | MPU9150_INT_PIN_CFG_INT_RD_CLEAR | MPU9150_INT_PIN_CFG_LATCH_INT_EN); g_sMPU9150Inst.pui8Data[1] = MPU9150_INT_ENABLE_DATA_RDY_EN; MPU9150Write(&g_sMPU9150Inst, MPU9150_O_INT_PIN_CFG, g_sMPU9150Inst.pui8Data, 2, MotionCallback, &g_sMPU9150Inst); // // Wait for transaction to complete // MotionI2CWait(__FILE__, __LINE__); // // Initialize the DCM system. // CompDCMInit(&g_sCompDCMInst, 1.0f / ((float) MOTION_SAMPLE_FREQ_HZ), DCM_ACCEL_WEIGHT, DCM_GYRO_WEIGHT, DCM_MAG_WEIGHT); // // Initialize the gesture instance and establish a initial state estimate. // GestureInit(&g_sGestureInst, g_pfInitProb, g_ppfPath, g_ppfTransitionProb, g_ppfEmitProb, GESTURE_PATH_LENGTH, GESTURE_NUM_STATES, GESTURE_STATE_IDLE); }
/** Initializes Ports/Pins: sets direction, interrupts, pullup/pulldown resistors etc. */ void portInit() { /* Port A * PA0 U0Rx (Debug UART) * PA1 U0Tx (Debug UART) * PA2 Bit-Bang I2C SDA * PA3 Bit-Bang I2C SCL * PA4 BoosterPack RGB LED - Green * PA5 Module SS & MRDY * PA6 BoosterPack RGB LED - Red * PA7 Module SRDY */ SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); PERIPHERAL_ENABLE_DELAY(); /* Configure UART pins */ GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); /* Outputs */ GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6); GPIOPadConfigSet(GPIO_PORTA_BASE, GPIO_PIN_4 | GPIO_PIN_6, GPIO_STRENGTH_4MA, GPIO_PIN_TYPE_STD); // For LEDs /* Inputs */ GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_7); /* Bit-bang I2C */ //GPIODirModeSet(GPIO_PORTA_BASE, GPIO_PIN_2 | GPIO_PIN_3, GPIO_DIR_MODE_OUT); //SDA & SCL //GPIOPadConfigSet(GPIO_PORTA_BASE, GPIO_PIN_2 | GPIO_PIN_3, GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_OD); //SDA & SCL open-drain //GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2 | GPIO_PIN_3, GPIO_PIN_2 | GPIO_PIN_3); //Set SDA & SCL high /* Port B * PB0 * PB1 * PB2 BoosterPack RGB LED - Blue * PB3 * PB4 Module SCLK SSI2CLK * PB5 LED0 * PB6 Module MISO SSI2Rx * PB7 Module MOSI SSI2Tx */ SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); //FOR STATUS LED PERIPHERAL_ENABLE_DELAY(); /* SSI2 Configuration for Module SPI */ SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI2); PERIPHERAL_ENABLE_DELAY(); GPIOPinConfigure(GPIO_PB4_SSI2CLK); GPIOPinConfigure(GPIO_PB6_SSI2RX); GPIOPinConfigure(GPIO_PB7_SSI2TX); GPIOPinTypeSSI(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_6 | GPIO_PIN_7); /* Outputs */ GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_5); GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_5, GPIO_STRENGTH_4MA, GPIO_PIN_TYPE_STD); /* Port C * PC0 (JTAG TCK/SWCLK) * PC1 (JTAG TMS/SWDIO) * PC2 (JTAG TDI) * PC3 (JTAG TDIO/SWO) * PC4 * PC5 * PC6 * PC7 */ // Note: no initialization needed for Port C required; using default configuration /* Port D * PD0 I2C3SCL * PD1 I2C3SDA * PD2 * PD3 * PD4 (USB D-) * PD5 (USB D+) * PD6 * PD7 (USB VBUS) */ SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); //FOR STATUS LED PERIPHERAL_ENABLE_DELAY(); /* I2C Configuration */ SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3); //requires 5 clock cycles to initialize PERIPHERAL_ENABLE_DELAY(); GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); //NOTE: Only required for blizzard (LM4F) parts GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1); GPIOPinConfigure(GPIO_PD0_I2C3SCL); GPIOPinConfigure(GPIO_PD1_I2C3SDA); #ifdef TIVA I2CMasterInitExpClk(I2C3_BASE, SysCtlClockGet(), false); //FALSE = 100kbps #else I2CMasterInitExpClk(I2C3_MASTER_BASE, SysCtlClockGet(), false); //FALSE = 100kbps #endif SysCtlDelay(10000); //otherwise portion of SlaveAddrSet() lost - only for blizzard /* Port E - note this port is only 6 pins * PE0 Module Reset * PE1 * PE2 * PE3 * PE4 Switch S2 on BoosterPack - note has external 47k pullup on BoosterPack * PE5 Current Sensor analog input */ SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); PERIPHERAL_ENABLE_DELAY(); SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); PERIPHERAL_ENABLE_DELAY(); /* Outputs */ GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_0); /* Inputs */ GPIOPadConfigSet(GPIO_PORTE_BASE, GPIO_PIN_4 , GPIO_STRENGTH_4MA, GPIO_PIN_TYPE_STD); GPIODirModeSet(GPIO_PORTE_BASE, GPIO_PIN_4, GPIO_DIR_MODE_IN); GPIOIntTypeSet(GPIO_PORTE_BASE, GPIO_PIN_4, GPIO_FALLING_EDGE); // Make button a falling-edge triggered interrupt #ifdef TIVA GPIOIntEnable(GPIO_PORTE_BASE, GPIO_PIN_4); // Enable the interrupt on this pin GPIOIntClear(GPIO_PORTE_BASE, GPIO_PIN_4); //Clear interrupts #else GPIOPinIntEnable(GPIO_PORTE_BASE, GPIO_PIN_4); // Enable the interrupt on this pin GPIOPinIntClear(GPIO_PORTE_BASE, GPIO_PIN_4); //Clear interrupts #endif IntEnable(INT_GPIOE);//enable interrupt 18 /* ADC Inputs */ GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_5); RADIO_OFF(); SPI_SS_CLEAR(); /* Port F * PF0 Switch SW2 on Stellaris LaunchPad * PF1 RGB LED on Stellaris LaunchPad - Red * PF2 RGB LED on Stellaris LaunchPad - Green * PF3 RGB LED on Stellaris LaunchPad - Blue * PF4 Switch SW1 on Stellaris LaunchPad */ SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); PERIPHERAL_ENABLE_DELAY(); /* Outputs */ // Note: PWM Outputs for LEDs are initialized separately if they are being used /* Inputs */ // Unlock PF0 so we can change it to a GPIO input. see ButtonsInit() in buttons.c in example qs-rgb // Once we have enabled (unlocked) the commit register then re-lock it // to prevent further changes. PF0 is muxed with NMI thus a special case. #ifdef TIVA HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY; #else HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY_DD; #endif HWREG(GPIO_PORTF_BASE + GPIO_O_CR) |= 0x01; HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = 0; GPIODirModeSet(GPIO_PORTF_BASE, ALL_BUTTONS, GPIO_DIR_MODE_IN); GPIOPadConfigSet(GPIO_PORTF_BASE, ALL_BUTTONS, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); GPIOIntTypeSet(GPIO_PORTF_BASE, ALL_BUTTONS, GPIO_FALLING_EDGE); // Make button a falling-edge triggered interrupt #ifdef TIVA GPIOIntEnable(GPIO_PORTF_BASE, ALL_BUTTONS); // Enable the interrupt on this pin GPIOIntClear(GPIO_PORTF_BASE, ALL_BUTTONS); //Clear interrupts #else GPIOPinIntEnable(GPIO_PORTF_BASE, ALL_BUTTONS); // Enable the interrupt on this pin GPIOPinIntClear(GPIO_PORTF_BASE, ALL_BUTTONS); //Clear interrupts #endif IntEnable(INT_GPIOF); }
//***************************************************************************** // // Configure the I2C0 master and slave and connect them using loopback mode. // //***************************************************************************** int main(void) { uint32_t ui32DataTx; // // 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); // // The I2C0 peripheral must be enabled before use. // SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); // // For this example I2C0 is used with PortB[3:2]. The actual port and // pins used may be different on your part, consult the data sheet for // more information. GPIO port B needs to be enabled so these pins can // be used. // TODO: change this to whichever GPIO port you are using. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // // Configure the pin muxing for I2C0 functions on port B2 and B3. // This step is not necessary if your part does not support pin muxing. // TODO: change this to select the port/pin you are using. // GPIOPinConfigure(GPIO_PB2_I2C0SCL); GPIOPinConfigure(GPIO_PB3_I2C0SDA); // // 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. // TODO: change this to select the port/pin you are using. // GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); // // Enable loopback mode. Loopback mode is a built in feature that helps // for debug the I2Cx module. It internally connects the I2C master and // slave terminals, which effectively lets you send data as a master and // receive data as a slave. NOTE: For external I2C operation you will need // to use external pull-ups that are faster than the internal pull-ups. // Refer to the datasheet for more information. // HWREG(I2C0_BASE + I2C_O_MCR) |= 0x01; // // Enable the I2C0 interrupt on the processor (NVIC). // IntEnable(INT_I2C0); // // Configure and turn on the I2C0 slave interrupt. The I2CSlaveIntEnableEx() // gives you the ability to only enable specific interrupts. For this case // we are only interrupting when the slave device receives data. // I2CSlaveIntEnableEx(I2C0_BASE, I2C_SLAVE_INT_DATA); // // Enable and initialize the I2C0 master module. Use the system clock for // the I2C0 module. The last parameter sets the I2C data transfer rate. // If false the data rate is set to 100kbps and if true the data rate will // be set to 400kbps. For this example we will use a data rate of 100kbps. // I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false); // // Enable the I2C0 slave module. // I2CSlaveEnable(I2C0_BASE); // // Set the slave address to SLAVE_ADDRESS. In loopback mode, it's an // arbitrary 7-bit number (set in a macro above) that is sent to the // I2CMasterSlaveAddrSet function. // I2CSlaveInit(I2C0_BASE, SLAVE_ADDRESS); // // Tell the master module what address it will place on the bus when // communicating with the slave. Set the address to SLAVE_ADDRESS // (as set in the slave module). The receive parameter is set to false // which indicates the I2C Master is initiating a writes to the slave. If // true, that would indicate that the I2C Master is initiating reads from // the slave. // I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false); // // Set up the serial console to use for displaying messages. This is just // for this example program and is not needed for proper I2C operation. // InitConsole(); // // Enable interrupts to the processor. // IntMasterEnable(); // // Display the example setup on the console. // UARTprintf("I2C Slave Interrupt Example ->"); UARTprintf("\n Module = I2C0"); UARTprintf("\n Mode = Receive interrupt on the Slave module"); UARTprintf("\n Rate = 100kbps\n\n"); // // Initialize the data to send. // ui32DataTx = 'I'; // // Indicate the direction of the data. // UARTprintf("Transferring from: Master -> Slave\n"); // // Display the data that I2C0 is transferring. // UARTprintf(" Sending: '%c'", ui32DataTx); // // Place the data to be sent in the data register. // I2CMasterDataPut(I2C0_BASE, ui32DataTx); // // Initiate send of single piece of data from the master. Since the // loopback mode is enabled, the Master and Slave units are connected // allowing us to receive the same data that we sent out. // I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND); // // Wait for interrupt to occur. // while(!g_bIntFlag) { } // // Display that interrupt was received. // UARTprintf("\n Slave Interrupt Received!\n"); // // Display the data that the slave has received. // UARTprintf(" Received: '%c'\n\n", g_ui32DataRx); // // Loop forever. // while(1) { } }
//***************************************************************************** // // Main 'C' Language entry point. // //***************************************************************************** int main(void) { float fAmbient, fObject; int_fast32_t i32IntegerPart; int_fast32_t i32FractionPart; // // Setup the system clock to run at 40 Mhz from PLL with crystal reference // ROM_SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN); // // Enable the peripherals used by this example. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); // // Initialize the UART. // ConfigureUART(); // // Print the welcome message to the terminal. // UARTprintf("\033[2J\033[1;1HTMP006 Example\n"); // // Setup the color of the RGB LED. // g_pui32Colors[RED] = 0; g_pui32Colors[BLUE] = 0xFFFF; g_pui32Colors[GREEN] = 0; // // Initialize the RGB Driver and start RGB blink operation. // RGBInit(0); RGBColorSet(g_pui32Colors); RGBIntensitySet(0.5f); RGBEnable(); // // The I2C3 peripheral must be enabled before use. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); // // Configure the pin muxing for I2C3 functions on port D0 and D1. // This step is not necessary if your part does not support pin muxing. // ROM_GPIOPinConfigure(GPIO_PD0_I2C3SCL); ROM_GPIOPinConfigure(GPIO_PD1_I2C3SDA); // // 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. // GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); ROM_GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1); // // Configure and Enable the GPIO interrupt. Used for DRDY from the TMP006 // ROM_GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_0); GPIOIntEnable(GPIO_PORTE_BASE, GPIO_PIN_0); ROM_GPIOIntTypeSet(GPIO_PORTE_BASE, GPIO_PIN_0, GPIO_FALLING_EDGE); ROM_IntEnable(INT_GPIOE); // // Keep only some parts of the systems running while in sleep mode. // GPIOE is for the TMP006 data ready interrupt. // UART0 is the virtual serial port // TIMER0, TIMER1 and WTIMER5 are used by the RGB driver // I2C3 is the I2C interface to the TMP006 // ROM_SysCtlPeripheralClockGating(true); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_GPIOE); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_UART0); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_TIMER0); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_TIMER1); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_I2C3); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_WTIMER5); // // Enable interrupts to the processor. // ROM_IntMasterEnable(); // // Initialize I2C3 peripheral. // I2CMInit(&g_sI2CInst, I2C3_BASE, INT_I2C3, 0xff, 0xff, SysCtlClockGet()); // // Initialize the TMP006 // TMP006Init(&g_sTMP006Inst, &g_sI2CInst, TMP006_I2C_ADDRESS, TMP006AppCallback, &g_sTMP006Inst); // // Put the processor to sleep while we wait for the I2C driver to // indicate that the transaction is complete. // while((g_vui8DataFlag == 0) && (g_vui8ErrorFlag == 0)) { ROM_SysCtlSleep(); } // // If an error occurred call the error handler immediately. // if(g_vui8ErrorFlag) { TMP006AppErrorHandler(__FILE__, __LINE__); } // // clear the data flag for next use. // g_vui8DataFlag = 0; // // Delay for 10 milliseconds for TMP006 reset to complete. // Not explicitly required. Datasheet does not say how long a reset takes. // ROM_SysCtlDelay(ROM_SysCtlClockGet() / (100 * 3)); // // Enable the DRDY pin indication that a conversion is in progress. // TMP006ReadModifyWrite(&g_sTMP006Inst, TMP006_O_CONFIG, ~TMP006_CONFIG_EN_DRDY_PIN_M, TMP006_CONFIG_EN_DRDY_PIN, TMP006AppCallback, &g_sTMP006Inst); // // Wait for the DRDY enable I2C transaction to complete. // while((g_vui8DataFlag == 0) && (g_vui8ErrorFlag == 0)) { ROM_SysCtlSleep(); } // // If an error occurred call the error handler immediately. // if(g_vui8ErrorFlag) { TMP006AppErrorHandler(__FILE__, __LINE__); } // // clear the data flag for next use. // g_vui8DataFlag = 0; // // Last thing before the loop start blinking to show we got this far and // the tmp006 is setup and ready for auto measure // RGBBlinkRateSet(1.0f); // // Loop Forever // while(1) { // // Put the processor to sleep while we wait for the TMP006 to // signal that data is ready. Also continue to sleep while I2C // transactions get the raw data from the TMP006 // while((g_vui8DataFlag == 0) && (g_vui8ErrorFlag == 0)) { ROM_SysCtlSleep(); } // // If an error occurred call the error handler immediately. // if(g_vui8ErrorFlag) { TMP006AppErrorHandler(__FILE__, __LINE__); } // // Reset the flag // g_vui8DataFlag = 0; // // Get a local copy of the latest data in float format. // TMP006DataTemperatureGetFloat(&g_sTMP006Inst, &fAmbient, &fObject); // // Convert the floating point ambient temperature to an integer part // and fraction part for easy printing. // i32IntegerPart = (int32_t)fAmbient; i32FractionPart = (int32_t)(fAmbient * 1000.0f); i32FractionPart = i32FractionPart - (i32IntegerPart * 1000); if(i32FractionPart < 0) { i32FractionPart *= -1; } UARTprintf("Ambient %3d.%03d\t", i32IntegerPart, i32FractionPart); // // Convert the floating point ambient temperature to an integer part // and fraction part for easy printing. // i32IntegerPart = (int32_t)fObject; i32FractionPart = (int32_t)(fObject * 1000.0f); i32FractionPart = i32FractionPart - (i32IntegerPart * 1000); if(i32FractionPart < 0) { i32FractionPart *= -1; } UARTprintf("Object %3d.%03d\n", i32IntegerPart, i32FractionPart); } }
/********************************************************************************************** * Main *********************************************************************************************/ void main(void) { //After tomorrow's test flight. FOR THE LOVE OF GOD MOVE THESE INITALIZATIONS TO FUNCTIONS /********************************************************************************************** * Local Variables *********************************************************************************************/ unsigned long ultrasonic = 0; // Enable lazy stacking for interrupt handlers. This allows floating-point // instructions to be used within interrupt handlers, but at the expense of // extra stack usage. FPULazyStackingEnable(); //Set the clock speed to 80MHz aka max speed SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); /*unsigned long test[2]; test[0] = 180; test[1] = 10; short bob[1]; bob[0] = ((char)test[0]<<8)|(char)test[1]; float jimmy = (short)(((char)test[0]<<8)|(char)test[1]); jimmy /= 26;*/ /********************************************************************************************** * Peripheral Initialization Awake *********************************************************************************************/ SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); //Turn on GPIO communication on F pins for switches SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); //Turn on GPIO for ADC SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); //Turn on GPIO for the PWM comms SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); //Turn on GPIO for LED test SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); //Turn on GPIO for UART SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); //Turn on Timer for PWM SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); //Turn on Timer for PWM SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2); //Turn on Timer for PWM SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER3); //Turn on Timer for PWM SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1); //Turn on I2C communication I2C slot 0 SysCtlPeripheralEnable(SYSCTL_PERIPH_UART2); //Turn on the UART com SysCtlPeripheralEnable(SYSCTL_PERIPH_WDOG); //Turn on the watchdog timer. This is a risky idea but I think it's for the best. /********************************************************************************************** * Peripheral Initialization Sleep *********************************************************************************************/ /*SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_GPIOF); //This sets what peripherals are still enabled in sleep mode while UART would be nice, it would require the clock operate at full speed which is :P SysCtlPeripheralSleepDisable(SYSCTL_PERIPH_GPIOB); SysCtlPeripheralSleepDisable(SYSCTL_PERIPH_GPIOA); SysCtlPeripheralSleepDisable(SYSCTL_PERIPH_TIMER0); SysCtlPeripheralSleepDisable(SYSCTL_PERIPH_TIMER1); SysCtlPeripheralSleepDisable(SYSCTL_PERIPH_TIMER2); SysCtlPeripheralSleepDisable(SYSCTL_PERIPH_TIMER3); SysCtlPeripheralSleepDisable(SYSCTL_PERIPH_SSI0); SysCtlPeripheralSleepDisable(SYSCTL_PERIPH_I2C1); SysCtlPeripheralSleepDisable(SYSCTL_PERIPH_ADC); SysCtlPeripheralClockGating(true); //I'm not sure about this one maybe remove it */ /********************************************************************************************** * PWM Initialization *********************************************************************************************/ SysCtlDelay((SysCtlClockGet() / (1000 * 3))*100); //This shouldn't be needed will test to remove //PWM pin Setup //PWM 0 on GPIO PB6, PWM 1 on pin 4... etc GPIOPinConfigure(GPIO_PB6_T0CCP0); //Pitch - yaw + GPIOPinConfigure(GPIO_PB4_T1CCP0); //Pitch + yaw + GPIOPinConfigure(GPIO_PB0_T2CCP0); //Roll - yaw - GPIOPinConfigure(GPIO_PB2_T3CCP0); //Roll + yaw - GPIOPinTypeTimer(GPIO_PORTB_BASE, (GPIO_PIN_6|GPIO_PIN_4|GPIO_PIN_0|GPIO_PIN_2)); //Prescale the timers so they are slow enough to work with the ESC TimerPrescaleSet(TIMER0_BASE,TIMER_A,2); TimerPrescaleSet(TIMER1_BASE,TIMER_A,2); TimerPrescaleSet(TIMER2_BASE,TIMER_A,2); TimerPrescaleSet(TIMER3_BASE,TIMER_A,2); //Basic LED Out Test Not sure why this is here look into This just turns on an LED that I don't have plugged in. should remove later GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_3); GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,0xFF); //GPIOPinTypeGPIOOutputOD(GPIO_PORTB_BASE,GPIO_PIN_0); //Timers Setup for PWM and the load for the countdown TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PWM); TimerLoadSet(TIMER0_BASE, TIMER_A, ulPeriod -1); TimerConfigure(TIMER1_BASE, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PWM); TimerLoadSet(TIMER1_BASE, TIMER_A, ulPeriod -1); TimerConfigure(TIMER2_BASE, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PWM); TimerLoadSet(TIMER2_BASE, TIMER_A, (ulPeriod -1)); TimerConfigure(TIMER3_BASE, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PWM); TimerLoadSet(TIMER3_BASE, TIMER_A, ulPeriod -1); //TimerPrescaleSet(TIMER2_BASE, TIMER_A, extender1); //TimerLoadSet(TIMER2_BASE, TIMER_A, period1); //Set the match which is when the thing will pull high TimerMatchSet(TIMER0_BASE, TIMER_A, 254); //Duty cycle = (1-%desired)*1000 note this means this number is percent low not percent high TimerMatchSet(TIMER1_BASE, TIMER_A, 254); TimerMatchSet(TIMER2_BASE, TIMER_A, 254); TimerMatchSet(TIMER3_BASE, TIMER_A, 254); //TimerPrescaleMatchSet(TIMER2_BASE, TIMER_A, extender2); //TimerMatchSet(TIMER2_BASE, TIMER_A, period2); //Enable the timers TimerEnable(TIMER0_BASE, TIMER_A); TimerEnable(TIMER1_BASE, TIMER_A); TimerEnable(TIMER2_BASE, TIMER_A); TimerEnable(TIMER3_BASE, TIMER_A); /********************************************************************************************** * onboard Chip interrupt Initialization *********************************************************************************************/ //These two buttons are used to reset the bluetooth module in case of disconnection GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3); //RGB LED's GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,0x00); HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY_DD; //Sw1 (PF4) is unaviable unless you make it only a GPIOF input via these commands HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0x1; GPIOPinTypeGPIOInput(GPIO_PORTF_BASE,GPIO_PIN_0|GPIO_PIN_4); //Onboard buttons (PF0=Sw2,PF4=Sw1 GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_0|GPIO_PIN_4, GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU); //This will make the buttons falling edge (a press pulls them low) //void (*functionPtr)(void) = &onBoardInteruptHandle; GPIOPortIntRegister(GPIO_PORTF_BASE, onBoardInteruptHandle); //set function to handle interupt GPIOIntTypeSet(GPIO_PORTF_BASE,GPIO_PIN_0|GPIO_PIN_4,GPIO_FALLING_EDGE); //Set the interrupt as falling edge GPIOPinIntEnable(GPIO_PORTF_BASE,GPIO_PIN_0|GPIO_PIN_4); //Enable the interrupt //IntMasterEnable(); IntEnable(INT_GPIOF); /********************************************************************************************** * UART Initialization *********************************************************************************************/ //Unlock PD7 HWREG(GPIO_PORTD_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY_DD; HWREG(GPIO_PORTD_BASE + GPIO_O_CR) = 0x80; GPIOPinConfigure(GPIO_PD7_U2TX); //Set PD7 as TX GPIOPinTypeUART(GPIO_PORTD_BASE, GPIO_PIN_7); GPIOPinConfigure(GPIO_PD6_U2RX); //Set PD6 as RX GPIOPinTypeUART(GPIO_PORTD_BASE, GPIO_PIN_6); UARTConfigSetExpClk(UART2_BASE,SysCtlClockGet(),115200,UART_CONFIG_WLEN_8|UART_CONFIG_STOP_ONE|UART_CONFIG_PAR_NONE); //I believe the Xbee defaults to no parity I do know it's 9600 baud though, changed to 115200 for bluetooth reasons UARTFIFOLevelSet(UART2_BASE,UART_FIFO_TX1_8,UART_FIFO_RX1_8); //Set's how big the fifo needs to be in order to call the interrupt handler, 2byte UARTIntRegister(UART2_BASE,Uart2IntHandler); //Regiester the interrupt handler UARTIntClear(UART2_BASE, UART_INT_TX | UART_INT_RX); //Clear the interrupt UARTIntEnable(UART2_BASE, UART_INT_TX | UART_INT_RX); //Enable the interrupt to trigger on both TX and RX event's. Could possibly remove TX UARTEnable(UART2_BASE); //Enable UART IntEnable(INT_UART2); //Second way to enable handler not sure if needed using anyway /********************************************************************************************** * I2C Initialization *********************************************************************************************/ //Serious credit to the man who made the Arduino version of this. he gave me addresses and equations. Sadly Arduino obfuscates what really is happening //Link posted on blog page //gyro address = 0x68 not 0x69 GPIOPinConfigure(GPIO_PA7_I2C1SDA); GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_7); //Set GPA7 as SDA GPIOPinConfigure(GPIO_PA6_I2C1SCL); GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6); //Set GPA6 as SCL I2CMasterInitExpClk(I2C1_MASTER_BASE,SysCtlClockGet(),false); //I think it operates at 100kbps I2CMasterEnable(I2C1_MASTER_BASE); //Initalize the accelerometer Address = 0x53 GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,0x02); UARTSend(0xAB); I2CTransmit(0x53,0x2D,0x00); I2CTransmit(0x53,0x2D,0x10); I2CTransmit(0x53,0x2D,0x08); //Initalize the gyroscope Address = 0x68 I2CTransmit(0x68,0x3E,0x00); I2CTransmit(0x68,0x15,0x07); I2CTransmit(0x68,0x16,0x1E); I2CTransmit(0x68,0x17,0x00); UARTSend(0xAC); /********************************************************************************************** * SysTick Initialization *********************************************************************************************/ SysTickIntRegister(SysTickIntHandler); SysTickIntEnable(); SysTickPeriodSet((SysCtlClockGet() / (1000 * 3))*timeBetweenCalculations); //This sets the period for the delay. the last num is the num of milliseconds SysTickEnable(); /********************************************************************************************** * Watchdog Initialization *********************************************************************************************/ WatchdogReloadSet(WATCHDOG_BASE, 0xFEEFEEFF); //Set the timer for a reset WatchdogIntRegister(WATCHDOG_BASE,WatchdogIntHandler); //Enable interrupt WatchdogIntClear(WATCHDOG_BASE); WatchdogIntEnable(WATCHDOG_BASE); WatchdogEnable(WATCHDOG_BASE); //Enable the actual timer IntEnable(INT_WATCHDOG); /********************************************************************************************** * Preflight motor inialization maybe not necessary not going to test *********************************************************************************************/ PWMSet(TIMER0_BASE,998); PWMSet(TIMER1_BASE,998); PWMSet(TIMER2_BASE,998); PWMSet(TIMER3_BASE,998); recievedCommands[0]=253; SysCtlDelay((SysCtlClockGet() / (1000 * 3))*100); //Very important to ensure motor see's a start high (998 makes 0 sense but it works so shhhh don't tell anyone) GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,0x06); while(1){ WatchdogReloadSet(WATCHDOG_BASE, 0xFEEFEEFF); //Feed the dog a new time //UARTSend(recievedCommands[0]); //SysCtlDelay(50000); //Set 4 PWM Outputs //Get Acc data I2CRead(0x53,0x32,6,quadAcc); //Address blah blah 2 for each axis rawAccToG(quadAcc,RwAcc); /*GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,0x04); //Blue //Get Gyro data /************************************** Gyro ITG-3200 I2C registers: temp MSB = 1B, temp LSB = 1C x axis MSB = 1D, x axis LSB = 1E y axis MSB = 1F, y axis LSB = 20 z axis MSB = 21, z axis LSB = 22 *************************************/ I2CRead(0x68,0x1B,8,quadGyro); //Address blah blah 2 for each axis + 2 for temperature. why. because why not rawGyroToDegsec(quadGyro,Gyro_ds); //GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,0x02); //Red //Get the actual angles in XYZ. Store them in RwEst //getInclination(RwAcc, RwEst, RwGyro, Gyro_ds, Awz); //After this function is called RwEst will hold the roll pitch and yaw //RwEst will be returned in PITCH, ROLL, YAW 0, 1, 2 remember this order very important. Little obvious but yaw is worthless /*if(RwEst[1]>0.5){ GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,0x06); //Red Blue, Correct data read in }else{ GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,0x0A); //Red Green, The correct data is not there }*/ /*GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,0x06); //Red Blue, Correct data read in float test=RwAcc[0]*100; //These two commands work char temp = (char)test; //UARTSend((char)(RwAcc[0])*100); //This one does not UARTSend(temp); //UARTSend((char)(RwAcc[1])*100); UARTSend(0xAA); SysCtlDelay((SysCtlClockGet() / (1000 * 3))*1); */ } }
void initsensorhub(void) { // // Enable port B used for motion interrupt. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // // Initialize the UART. // ConfigureUART(); // // Print the welcome message to the terminal. // UARTprintf("\033[2JMPU9150 Raw Example\n"); // // Set the color to a purple approximation. // g_pui32Colors[RED] = 0x8000; g_pui32Colors[BLUE] = 0x8000; g_pui32Colors[GREEN] = 0x0000; // // Initialize RGB driver. // RGBInit(0); RGBColorSet(g_pui32Colors); RGBIntensitySet(0.5f); RGBEnable(); // // The I2C3 peripheral must be enabled before use. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); // // Configure the pin muxing for I2C3 functions on port D0 and D1. // ROM_GPIOPinConfigure(GPIO_PD0_I2C3SCL); ROM_GPIOPinConfigure(GPIO_PD1_I2C3SDA); // // 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. // GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); ROM_GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1); // // Configure and Enable the GPIO interrupt. Used for INT signal from the // MPU9150 // ROM_GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_2); GPIOIntEnable(GPIO_PORTB_BASE, GPIO_PIN_2); ROM_GPIOIntTypeSet(GPIO_PORTB_BASE, GPIO_PIN_2, GPIO_FALLING_EDGE); ROM_IntEnable(INT_GPIOB); // // Keep only some parts of the systems running while in sleep mode. // GPIOB is for the MPU9150 interrupt pin. // UART0 is the virtual serial port // TIMER0, TIMER1 and WTIMER5 are used by the RGB driver // I2C3 is the I2C interface to the ISL29023 // ROM_SysCtlPeripheralClockGating(true); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_GPIOB); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_UART0); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_TIMER0); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_TIMER1); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_I2C3); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_WTIMER5); // // Enable interrupts to the processor. // ROM_IntMasterEnable(); // // Initialize I2C3 peripheral. // I2CMInit(&g_sI2CInst, I2C3_BASE, INT_I2C3, 0xff, 0xff, ROM_SysCtlClockGet()); // // Initialize the MPU9150 Driver. // MPU9150Init(&g_sMPU9150Inst, &g_sI2CInst, MPU9150_I2C_ADDRESS, MPU9150AppCallback, &g_sMPU9150Inst); // // Wait for transaction to complete // MPU9150AppI2CWait(__FILE__, __LINE__); // // Write application specifice sensor configuration such as filter settings // and sensor range settings. // g_sMPU9150Inst.pui8Data[0] = MPU9150_CONFIG_DLPF_CFG_94_98; g_sMPU9150Inst.pui8Data[1] = MPU9150_GYRO_CONFIG_FS_SEL_250; g_sMPU9150Inst.pui8Data[2] = (MPU9150_ACCEL_CONFIG_ACCEL_HPF_5HZ | MPU9150_ACCEL_CONFIG_AFS_SEL_2G); MPU9150Write(&g_sMPU9150Inst, MPU9150_O_CONFIG, g_sMPU9150Inst.pui8Data, 3, MPU9150AppCallback, &g_sMPU9150Inst); // // Wait for transaction to complete // MPU9150AppI2CWait(__FILE__, __LINE__); // // Configure the data ready interrupt pin output of the MPU9150. // g_sMPU9150Inst.pui8Data[0] = MPU9150_INT_PIN_CFG_INT_LEVEL | MPU9150_INT_PIN_CFG_INT_RD_CLEAR | MPU9150_INT_PIN_CFG_LATCH_INT_EN; g_sMPU9150Inst.pui8Data[1] = MPU9150_INT_ENABLE_DATA_RDY_EN; MPU9150Write(&g_sMPU9150Inst, MPU9150_O_INT_PIN_CFG, g_sMPU9150Inst.pui8Data, 2, MPU9150AppCallback, &g_sMPU9150Inst); // // Wait for transaction to complete // MPU9150AppI2CWait(__FILE__, __LINE__); // // Initialize the DCM system. 50 hz sample rate. // accel weight = .2, gyro weight = .8, mag weight = .2 // CompDCMInit(&g_sCompDCMInst, 1.0f / 50.0f, 0.2f, 0.6f, 0.2f); UARTprintf("\033[2J\033[H"); UARTprintf("MPU9150 9-Axis Simple Data Application Example\n\n"); UARTprintf("\033[20GX\033[31G|\033[43GY\033[54G|\033[66GZ\n\n"); UARTprintf("Accel\033[8G|\033[31G|\033[54G|\n\n"); UARTprintf("Gyro\033[8G|\033[31G|\033[54G|\n\n"); UARTprintf("Mag\033[8G|\033[31G|\033[54G|\n\n"); UARTprintf("\n\033[20GRoll\033[31G|\033[43GPitch\033[54G|\033[66GYaw\n\n"); UARTprintf("Eulers\033[8G|\033[31G|\033[54G|\n\n"); UARTprintf("\n\033[17GQ1\033[26G|\033[35GQ2\033[44G|\033[53GQ3\033[62G|" "\033[71GQ4\n\n"); UARTprintf("Q\033[8G|\033[26G|\033[44G|\033[62G|\n\n"); // // Enable blinking indicates config finished successfully // RGBBlinkRateSet(1.0f); // // Initialize convenience pointers that clean up and clarify the code // meaning. We want all the data in a single contiguous array so that // we can make our pretty printing easier later. // pfAccel = pfData; pfGyro = pfData + 3; pfMag = pfData + 6; pfEulers = pfData + 9; pfQuaternion = pfData + 12; }
void readSensorData(GameState* state) { short dataX; short dataY; short dataZ; char printVal[10]; char chPwrCtlReg = 0x2D; char chX0Addr = 0x32; char chY0Addr = 0x34; char chZ0Addr = 0x36; char rgchReadAccl[] = { 0, 0, 0 }; char rgchWriteAccl[] = { 0, 0 }; char rgchReadAccl2[] = { 0, 0, 0 }; char rgchReadAccl3[] = { 0, 0, 0 }; int xDirThreshPos = 50; int xDirThreshNeg = -50; bool fDir = true; if(state->accelInitialized == 0){ /* * Enable I2C Peripheral */ SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0); /* * Set I2C GPIO pins */ GPIOPinTypeI2C(I2CSDAPort, I2CSDA_PIN); GPIOPinTypeI2CSCL(I2CSCLPort, I2CSCL_PIN); GPIOPinConfigure(I2CSCL); GPIOPinConfigure(I2CSDA); /* * Setup I2C */ I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false); /* Initialize the Accelerometer * */ GPIOPinTypeGPIOInput(ACCL_INT2Port, ACCL_INT2); rgchWriteAccl[0] = chPwrCtlReg; rgchWriteAccl[1] = 1 << 3; // sets Accl in measurement mode I2CGenTransmit(rgchWriteAccl, 1, WRITE, ACCLADDR); state->accelInitialized = 1; } rgchReadAccl[0] = chX0Addr; rgchReadAccl2[0] = chY0Addr; rgchReadAccl3[0] = chZ0Addr; I2CGenTransmit(rgchReadAccl, 2, READ, ACCLADDR); I2CGenTransmit(rgchReadAccl2, 2, READ, ACCLADDR); I2CGenTransmit(rgchReadAccl3, 2, READ, ACCLADDR); dataX = (rgchReadAccl[2] << 8) | rgchReadAccl[1]; dataY = (rgchReadAccl2[2] << 8) | rgchReadAccl2[1]; dataZ = (rgchReadAccl3[2] << 8) | rgchReadAccl2[1]; state->accelY = dataY; }