void i2c_init(void) { bool status; // Enable peripheral except in deep sleep modes (e.g. LPM1, LPM2, LPM3) SysCtrlPeripheralEnable(I2C_PERIPHERAL); SysCtrlPeripheralSleepEnable(I2C_PERIPHERAL); SysCtrlPeripheralDeepSleepDisable(I2C_PERIPHERAL); // Reset peripheral previous to configuring it SysCtrlPeripheralReset(I2C_PERIPHERAL); // Configure the SCL pin GPIOPinTypeI2C(I2C_BASE, I2C_SCL); IOCPinConfigPeriphInput(I2C_BASE, I2C_SCL, IOC_I2CMSSCL); IOCPinConfigPeriphOutput(I2C_BASE, I2C_SCL, IOC_MUX_OUT_SEL_I2C_CMSSCL); // Configure the SDA pin GPIOPinTypeI2C(I2C_BASE, I2C_SDA); IOCPinConfigPeriphInput(I2C_BASE, I2C_SDA, IOC_I2CMSSDA); IOCPinConfigPeriphOutput(I2C_BASE, I2C_SDA, IOC_MUX_OUT_SEL_I2C_CMSSDA); // Configure the I2C clock status = (I2C_BAUDRATE == 400000 ? true : false); I2CMasterInitExpClk(SysCtrlClockGet(), status); // Enable the I2C module as master I2CMasterEnable(); }
/* * ======== 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; } }
void i2c_init() { // Trying to get around this damn I2C lockup issue GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_0); GPIOPinTypeGPIOInput(GPIO_PORTG_BASE, GPIO_PIN_1); while(!GPIOPinRead(GPIO_PORTG_BASE, GPIO_PIN_1)) { // Toggle the clock at 100kHz until the slave releases SDA GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_0, GPIO_PIN_0); cheapDelay(400); GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_0, ~GPIO_PIN_0); cheapDelay(400); } // Initialize the I2C channel the sensor is connected to SysCtlPeripheralEnable( SYSCTL_PERIPH_I2C1 ); GPIOPinConfigure( GPIO_PG0_I2C1SCL ); GPIOPinConfigure( GPIO_PG1_I2C1SDA ); GPIOPinTypeI2C( GPIO_PORTG_BASE, GPIO_PIN_0 | GPIO_PIN_1 ); // Set the clock (false = "slow" = 100kbps) I2CMasterInitExpClk( I2C1_MASTER_BASE, SysCtlClockGet(), false ); I2CMasterEnable( I2C1_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; }
/* 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); }
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 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(); }
void InitializeI2C(){ SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3); I2CMasterInitExpClk(I2C0_MASTER_BASE, SysCtlClockGet(), false); }
//***************************************************************************** // //! Initialization the I2C bus module. //! //! \param bus specifies the i2c bus data structure. //! //! This function initialization the I2C hardware bus, it will open the i2c peripheral //! and config the I2C gpio //! //! \return 0 is OK. // //***************************************************************************** int ipmi_i2c_bus_init(ipmi_i2c_bus *bus) { // // Check the arguments. // ASSERT(bus->sys_peripheral); ASSERT(bus->i2c_scl_periph); ASSERT(bus->i2c_sda_periph); ASSERT(bus->i2c_scl_gpio_port); ASSERT(bus->i2c_sda_gpio_port); ASSERT(bus->i2c_scl_gpio_pin); ASSERT(bus->i2c_sda_gpio_pin); ASSERT(bus->i2c_hw_master_base); // 初始化链表结构 INIT_LIST_HEAD(&bus->list); // 初始化系统硬件外设 SysCtlPeripheralEnable(bus->sys_peripheral); // 初始化SCL管脚硬件外设 SysCtlPeripheralEnable(bus->i2c_scl_periph); if (bus->i2c_scl_gpio_mux) GPIOPinConfigure(bus->i2c_scl_gpio_mux); GPIOPinTypeI2C(bus->i2c_scl_gpio_port, bus->i2c_scl_gpio_pin); // 初始化SDA管脚硬件外设 SysCtlPeripheralEnable(bus->i2c_sda_periph); if (bus->i2c_sda_gpio_mux) GPIOPinConfigure(bus->i2c_sda_gpio_mux); GPIOPinTypeI2C(bus->i2c_sda_gpio_port, bus->i2c_sda_gpio_pin); // 设备使能,开中断 I2CMasterInit(bus->i2c_hw_master_base, false); if (bus->i2c_int) { //I2CIntRegister(bus->i2c_hw_master_base); IntEnable(bus->i2c_int); I2CMasterIntEnable(bus->i2c_hw_master_base); } I2CMasterEnable(bus->i2c_hw_master_base); list_add(&bus->list, &ipmi_i2c_bus_root.head); ipmi_i2c_bus_root.count++; return 0; }
/******** I2C_Init ********************************************************** // Initialize I2C port // Input: I2C0 or I2C1 // Output: none // ------------------------------------------------------------------------*/ void I2C_Init ( I2C_Port i2c ) { unsigned long i2c_gpio_base, i2c_per_base, i2c_pins, i2c_master; if ( I2C_ON(i2c) ) { // return if the port is already initialize return; } if ( i2c == I2C1 ) { // we did not use I2C1 in this project return; } // Determine with i2c port to use if ( i2c ) { i2c_gpio_base = I2C1_GPIO_BASE; i2c_per_base = I2C1_PER_BASE; i2c_pins = I2C1_SCL | I2C1_SDA; i2c_master = I2C1_MASTER_BASE; // The I2C peripheral must be enabled for use. SysCtlPeripheralEnable ( SYSCTL_PERIPH_I2C1 ); } else { i2c_gpio_base = I2C0_GPIO_BASE; i2c_per_base = I2C0_PER_BASE; i2c_pins = I2C0_SCL | I2C0_SDA; i2c_master = I2C0_MASTER_BASE; SysCtlPeripheralEnable ( SYSCTL_PERIPH_I2C0 ); } // GPIO port needs to be enabled so these pins can be used. SysCtlPeripheralEnable ( i2c_per_base ); // Configure the GPIO settings for the SSI pins. GPIOPinTypeI2C ( i2c_gpio_base, i2c_pins ); // Enable and initialize the I2C master module. 100kpbs data rate I2CMasterInitExpClk ( i2c_master, SysCtlClockGet(), true ); // update status bit I2C_POWER_ON ( i2c ); }
/* * ======== 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(); }
void main(void) { unsigned char temp_data[10] = "00.0 C \n\n\r"; // Temp format to be edited by read unsigned short int i = 0; // general counter // Setup the I2C see lab 7 *************************************************************************************** SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); //setup clock SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); // Enable I2C hardware SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // Enable Pin hardware GPIOPinConfigure(GPIO_PB3_I2C0SDA); // Configure GPIO pin for I2C Data line GPIOPinConfigure(GPIO_PB2_I2C0SCL); // Configure GPIO Pin for I2C clock line GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3); // Set Pin Type GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_2, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);// SDA MUST BE STD GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_3, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD); // SCL MUST BE OPEN DRAIN I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false); // The False sets the controller to 100kHz communication I2CMasterSlaveAddrSet(I2C0_BASE, TEMP_ADDR, true); // false means transmit //**************************************************************************************************************** // Setup the UART see lab 6 ************************************************************************************** SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); // Enable UART hardware SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // Enable Pin hardware GPIOPinConfigure(GPIO_PA0_U0RX); // Configure GPIO pin for UART RX line GPIOPinConfigure(GPIO_PA1_U0TX); // Configure GPIO Pin for UART TX line GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); // Set Pins for UART UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200, // Configure UART to 8N1 at 115200bps (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); //**************************************************************************************************************** Print_header(); // Print Header while(1){ Read_temp(temp_data); // Read Data from Temp Sensor SysCtlDelay(6000000); // Delay for(i=0;i<10;i++){ // Loop to print out data string UARTCharPut(UART0_BASE, temp_data[i]); } } }
void init_pins() { //PORT A - SDHC Card SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); //set output: SD_SCK, SD_CS, SD_MOSI GPIOSetOutput(GPIO_PORTA_BASE, GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_5); //Set Input: SD_MISO GPIOSetInput(GPIO_PORTA_BASE, GPIO_PIN_4); //Initialize pins: SD_SCK <- LOW, SD_CS <- HIGH, SD_MOSI <- LOW GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_5, GPIO_PIN_3); //Set SD_CLK, SD_MISO, SD_MOSI as SSI pins GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_2 | GPIO_PIN_4 | GPIO_PIN_5); //Port B - RTC //Enable Port B SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); //Set pin type as I2C GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3); //Port D - LCD //TODO: Initialize LCD pins //Port E - SSI1 , VS0_BSYNC, VS0_RESET, VS0_DREQ, VS0_CS SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); //Set Output: VS_CLK, VS0_CS, VS_MOSI, VS0_BSYNC, VS0_RESET GPIOSetOutput(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_6); //Set Input: VS_MISO, VS0_DREQ GPIOSetInput(GPIO_PORTE_BASE, GPIO_PIN_2 | GPIO_PIN_5); //Initialize Pins: VS_CLK <- LOW, VS0_CS <- HIGH, VS_MOSI <- LOW, VS0_BSYNC <- HIGH, VS0_RESET <- LOW, GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_6, GPIO_PIN_1 | GPIO_PIN_4); //Set VS_CLK, VS_MOSI, VS_MISO as SSI pins GPIOPinTypeSSI(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_2 | GPIO_PIN_3); //Port F -VS1_BSYNC, VS1_RESET, VS1_DREQ, VS1_CS SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); //Set Output: VS1_RESET, VS1_BSYNC, VS1_CS GPIOSetOutput(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_2 | GPIO_PIN_3); //Set Input: VS1_DREQ GPIOSetInput(GPIO_PORTF_BASE, GPIO_PIN_1); //Initialize Pins: VS0_RESET <- LOW, VS0_BSYNC <- HIGH, VS1_CS <- HIGH GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_2 | GPIO_PIN_3, GPIO_PIN_2 | GPIO_PIN_3); return; }
/* 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. // SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); //Setup Mux GPIOPinConfigure(GPIO_PB2_I2C0SCL); GPIOPinConfigure(GPIO_PB3_I2C0SDA); GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3); //Set up direction //Reconfigure for correct operation GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_2, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_3, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD); // // Configure the I2C SCL and SDA pins for I2C operation. // //GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); //GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); // // Initialize the I2C master. // I2CMasterInitExpClk(I2C0_MASTER_BASE, SysCtlClockGet(), true); SysCtlDelay(10000); // delay mandatory here - otherwise portion of SlaveAddrSet() lost! // Register interrupt handler // or we could just edit the startup.c file //I2CIntRegister(I2C0_MASTER_BASE,I2C0IntHandler); // // Enable the I2C interrupt. // IntEnable(INT_I2C0); // already done via I2CIntRegister I2CMasterIntEnableEx(I2C0_MASTER_BASE,I2C_MASTER_INT_DATA | I2C_MASTER_INT_TIMEOUT); // // Enable the I2C master interrupt. // I2CMasterIntEnable(I2C0_MASTER_BASE); }
//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(); } }
static void prvSetupHardware( void ) { /* Setup the PLL. */ SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ ); /* Enable the I2C used to read the pot. */ SysCtlPeripheralEnable( SYSCTL_PERIPH_I2C ); SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOB ); GPIOPinTypeI2C( GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3 ); /* Initialize the I2C master. */ I2CMasterInit( I2C_MASTER_BASE, pdFALSE ); /* Enable the I2C master interrupt. */ I2CMasterIntEnable( I2C_MASTER_BASE ); IntEnable( INT_I2C ); /* Initialise the hardware used to talk to the LED's. */ vParTestInitialise(); }
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"); }
// * imu_init ***************************************************************** // * Setup pins and I2C interface to communicate with IMU (Pololu MinIMU-9). * // * * // * NOTE: Also called internally by imu_i2c_abort_transaction during reset * // * to recover from NAK/error. * // * * // * Portions for initialization of softi2c library copied/modified from * // * "soft_i2c_atmel.c" example code. * // **************************************************************************** void imu_init(void) { // setup pins for I2C-------------------- SysCtlPeripheralEnable(IMU_PORT); // enable GPIO port for IMU SysCtlPeripheralEnable(IMU_TIM); // enable timer to use for I2C bit timing SysCtlPeripheralReset(IMU_TIM); // reset timer to clear any previous configuration GPIOPinTypeI2C(IMU_PORT_BASE, IMU_PINS); // configure pins for I2C memset(&g_sI2C, 0, sizeof(g_sI2C)); // clear record SoftI2CCallbackSet(&g_sI2C, imu_SoftI2CCallback); // set callback function SoftI2CSCLGPIOSet(&g_sI2C, IMU_PORT_BASE, IMU_SCL); // set SCL pin SoftI2CSDAGPIOSet(&g_sI2C, IMU_PORT_BASE, IMU_SDA); // set SDA pin SoftI2CInit(&g_sI2C); // initialize software I2C driver SoftI2CIntEnable(&g_sI2C); // enable callback from softi2c // configure timer interrupt // Note, (interrupt rate)/4 = SCL frequency TimerConfigure(IMU_TIM_BASE, TIMER_CFG_32_BIT_PER); // configure timer for 32b operation TimerLoadSet(IMU_TIM_BASE, TIMER_A, SysCtlClockGet() / IMU_TIM_INTRATE); // configure divider to yield desired interrupt rate TimerIntEnable(IMU_TIM_BASE, TIMER_TIMA_TIMEOUT); // enable timer wrap interrupt TimerEnable(IMU_TIM_BASE, TIMER_A); // enable timer to start counting IntEnable(IMU_TIM_INT_VECT); // enable interrupt vector in NVIC }
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); }
//***************************************************************************** // //! Initialize the OLED display. //! //! \param bFast is a boolean that is \e true if the I2C interface should be //! run at 400 kbps and \e false if it should be run at 100 kbps. //! //! This function initializes the I2C interface to the OLED display and //! configures the SSD0303 controller on the panel. //! //! This function is contained in <tt>osram96x16.c</tt>, with //! <tt>osram96x16.h</tt> containing the API definition for use by //! applications. //! //! \return None. // //***************************************************************************** void OSRAMInit(tBoolean bFast) { unsigned long ulIdx; // // Enable the I2C and GPIO port B blocks as they are needed by this driver. // SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // // Configure the I2C SCL and SDA pins for I2C operation. // GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3); // // Initialize the I2C master. // I2CMasterInitExpClk(I2C_MASTER_BASE, SysCtlClockGet(), bFast); // // Compute the inter-byte delay for the SSD0303 controller. This delay is // dependent upon the I2C bus clock rate; the slower the clock the longer // the delay required. // // The derivation of this formula is based on a measured delay of // OSRAMDelay(1700) for a 100 kHz I2C bus with the CPU running at 50 MHz // (referred to as C). To scale this to the delay for a different CPU // speed (since this is just a CPU-based delay loop) is: // // f(CPU) // C * ---------- // 50,000,000 // // To then scale this to the actual I2C rate (since it won't always be // precisely 100 kHz): // // f(CPU) 100,000 // C * ---------- * ------- // 50,000,000 f(I2C) // // This equation will give the inter-byte delay required for any // configuration of the I2C master. But, as arranged it is impossible to // directly compute in 32-bit arithmetic (without loosing a lot of // accuracy). So, the equation is simplified. // // Since f(I2C) is generated by dividing down from f(CPU), replace it with // the equivalent (where TPR is the value programmed into the Master Timer // Period Register of the I2C master, with the 1 added back): // // 100,000 // f(CPU) ------- // C * ---------- * f(CPU) // 50,000,000 ------------ // 2 * 10 * TPR // // Inverting the dividend in the last term: // // f(CPU) 100,000 * 2 * 10 * TPR // C * ---------- * ---------------------- // 50,000,000 f(CPU) // // The f(CPU) now cancels out. // // 100,000 * 2 * 10 * TPR // C * ---------------------- // 50,000,000 // // Since there are no clock frequencies left in the equation, this equation // also works for 400 kHz bus operation as well, since the 100,000 in the // numerator becomes 400,000 but C is 1/4, which cancel out each other. // Reducing the constants gives: // // TPR TPR TPR // C * --- = 1700 * --- = 340 * --- = 68 * TPR // 25 25 5 // // Note that the constant C is actually a bit larger than it needs to be in // order to provide some safety margin. // g_ulDelay = 68 * (HWREG(I2C_MASTER_BASE + I2C_MASTER_O_TPR) + 1); // // Initialize the SSD0303 controller. Loop through the initialization // sequence doing a single I2C transfer for each command. // for(ulIdx = 0; ulIdx < sizeof(g_pucOSRAMInit); ulIdx += g_pucOSRAMInit[ulIdx] + 1) { // // Send this command. // OSRAMWriteFirst(g_pucOSRAMInit[ulIdx + 1]); OSRAMWriteArray(g_pucOSRAMInit + ulIdx + 2, g_pucOSRAMInit[ulIdx] - 2); OSRAMWriteFinal(g_pucOSRAMInit[ulIdx + g_pucOSRAMInit[ulIdx]]); } // // Clear the frame buffer. // OSRAMClear(); }
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; }
//***************************************************************************** // //! Initialize the OLED display. //! //! \param bFast is a boolean that is \e true if the I2C interface should be //! run at 400 kbps and \e false if it should be run at 100 kbps. //! //! This function initializes the I2C interface to the OLED display and //! configures the SSD0303 or SSD1300 controller on the panel. //! //! \return None. // //***************************************************************************** void Display96x16x1Init(tBoolean bFast) { unsigned long ulIdx; // // Enable the I2C and GPIO port B blocks as they are needed by this driver. // SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); #if (!(defined OSRAM_ONLY) && !(defined RIT_ONLY)) // // Read SysCtl DID1 register to determine whether this is an older board // with the OSRAM display or a newer one with the RIT model. // g_ucDisplayIsRIT = (HWREG(SYSCTL_DID1) & (1 << 12)) ? 1 : 0; // // Set the correct number of non-displayed columns given the display type // we are using. // g_ucColumnAdjust = g_ucDisplayIsRIT ? 4 : 36; #endif // // If using the RIT display, we need to enable power by pulling PD7 high. // #ifndef OSRAM_ONLY #ifndef RIT_ONLY if(g_ucDisplayIsRIT) { #endif SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_7); GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_7, GPIO_PIN_7); #ifndef RIT_ONLY } #endif #endif // // Configure the I2C SCL and SDA pins for I2C operation. // GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3); // // Initialize the I2C master. // I2CMasterInitExpClk(I2C0_MASTER_BASE, SysCtlClockGet(), bFast); // // Initialize the display controller. Loop through the initialization // sequence doing a single I2C transfer for each command. // for(ulIdx = 0; ulIdx < SIZE_INIT_CMDS; ulIdx += g_pucDisplayInit[ulIdx] + 1) { // // Send this command. // Display96x16x1WriteFirst(g_pucDisplayInit[ulIdx + 1]); Display96x16x1WriteArray(g_pucDisplayInit + ulIdx + 2, g_pucDisplayInit[ulIdx] - 2); Display96x16x1WriteFinal(g_pucDisplayInit[ulIdx + g_pucDisplayInit[ulIdx]]); } // // Clear the frame buffer. // Display96x16x1Clear(); }
/********************************************************************************************** * 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); */ } }
//***************************************************************************** // Defines IPMI Test Task //***************************************************************************** void ipmi_cmd_test_task(void *args) { #if 0 // SPI0 master to FPGA test { uint8_t spibuf[3] = {0}; uint8_t i; while (1) { #if 1 for (i = 0; i < 15; i++) { spibuf[0] = 0x00; spibuf[1] = i; spibuf[2] = 0xa0 + i; SPI_spi0_xfer(spibuf, 3, 0, 0); DEBUG("write_spi data=0x%x\r\n", spibuf[2]); } OSTimeDlyHMSM(0, 0, 1, 0); #endif for (i = 0; i < 15; i++) { spibuf[0] = 0x80; spibuf[1] = i; spibuf[2] = 0; SPI_spi0_xfer(spibuf, 2, &spibuf[2], 1); DEBUG("read_spi data=0x%x\r\n", spibuf[2]); } OSTimeDlyHMSM(0, 0, 3, 0); } } #endif #if 0 { #define IPMB_SELF_ADDR (0x71) #define SLAVE_ADDR (0x73) // 定义从机地址 #if 0 SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); // 使能I2C模块 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // 使能I2C管脚所在的GPIO模块 GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3); // 配置相关管脚为I2C收发功能 I2CSlaveInit(I2C0_SLAVE_BASE, SLAVE_ADDR); // I2C从机模块初始化 I2CSlaveIntEnable(I2C0_SLAVE_BASE); // 使能I2C从机模块中断 IntEnable(INT_I2C0); // 使能I2C中断 I2CSlaveEnable(I2C0_SLAVE_BASE); // 使能I2C从机 #endif { unsigned char ulDataTx[128] = {0}; unsigned char i; unsigned long error; struct ipmi_req *req; ulDataTx[0] = IPMI_FRAME_CHAR[0]; ulDataTx[1] = IPMI_FRAME_CHAR[1]; ulDataTx[2] = IPMI_FRAME_CHAR[2]; req = (struct ipmi_req *)&ulDataTx[3]; req->msg.data_len = 7; req->msg.rs_sa = SLAVE_ADDR; req->msg.netfn = 0x06; req->msg.rs_lun = 0x0; req->msg.checksum1 = 0xff; req->msg.rq_sa = IPMB_SELF_ADDR; req->msg.rq_lun = 0x0; req->msg.rq_seq = 0x01; req->msg.cmd = 0x01; while (1) { OSTimeDlyHMSM(0, 0, 5, 0); DEBUG("netfn=0x%x cmd=0x%x rs_sa=0x%x rq_sa=0x%x ", req->msg.netfn, req->msg.cmd, req->msg.rs_sa, req->msg.rq_sa); error = I2C_dev_write(I2C0_MASTER_BASE, SLAVE_ADDR, 0, 1, 20, &ulDataTx[0]); DEBUG("error=0x%x\r\n", error); //SysCtlDelay(10000000); } } } #endif #if 0 // test for eeprom uint8_t val[32] = { 0 }; uint8_t i; uint32_t error, addr; I2C_DEVICE at24xx_dev; I2C_i2c1_slave_dev_init(&at24xx_dev, 0x50, 2); #if 0 // set val to i for (i = 0; i < 32; i++) { val[i] = i; } // write to eeprom I2C_i2c1_slave_dev_set(&at24xx_dev, 0x1800, (uint8_t*)&val[0], 32); error = I2C_i2c1_master_write(&at24xx_dev); if (error) { DEBUG("write error=0x%x\r\n", error); } else { DEBUG("write to eeprom ok\r\n"); } OSTimeDlyHMSM(0, 0, 1, 0); #endif #if 0 // clear eeprom error = at24xx_clear(0x1800, 0x800); if (error) { DEBUG("clear error=0x%x\r\n", error); } else { DEBUG("clear OK\r\n"); } #endif // read eeprom for (addr = 0x1800; addr < 0x2000; addr += 32) { // clear val for (i = 0; i < 32; i++) { val[i] = 0; } // read from eeprom error = at24xx_read(addr, val, 32); //I2C_i2c1_slave_dev_set(&at24xx_dev, addr, (uint8_t*)&val[0], 32); //error = I2C_i2c1_master_read(&at24xx_dev); if (error) { DEBUG("read error=0x%x\r\n", error); } else { // show the read for (i = 0; i < 32; i++) { if (i % 8 == 0) { DEBUG("\r\n0x%04x ", addr+i); } DEBUG("0x%02x ", val[i]); } } } while (1) { OSTimeDlyHMSM(0, 0, 1, 0); } #endif #if 0 // test for i2c1/ipmb I2C_DEVICE ipmb_dev; uint32_t error; //uint8_t addr = 0; uint8_t buffer[4] = {0x00, 0x00, 0x00, 0x00}; I2C_i2c1_slave_dev_init(&ipmb_dev, 0x41, 1); #if 1 while (1) { // write to i2c0 //addr = (addr + 1) & 0xff; I2C_i2c1_slave_dev_set(&ipmb_dev, 0x00, (uint8_t*)&buffer[0], 2); error = I2C_i2c1_master_read(&ipmb_dev); if (error) { DEBUG("i2c0 error=0x%x\r\n", error); } else { DEBUG("ina230 cfg=0x%x\r\n", (buffer[0] << 8) | buffer[1]); } I2C_i2c1_slave_dev_set(&ipmb_dev, 0x01, (uint8_t*)&buffer[0], 2); error = I2C_i2c1_master_read(&ipmb_dev); if (error) { DEBUG("i2c0 error=0x%x\r\n", error); } else { DEBUG("ina230 shunt=0x%x\r\n", (buffer[0] << 8) | buffer[1]); } I2C_i2c1_slave_dev_set(&ipmb_dev, 0x02, (uint8_t*)&buffer[0], 2); error = I2C_i2c1_master_read(&ipmb_dev); if (error) { DEBUG("i2c0 error=0x%x\r\n", error); } else { DEBUG("ina230 bus=0x%x\r\n", (buffer[0] << 8) | buffer[1]); } buffer[0] = 0x2e; buffer[1] = 0xcf; I2C_i2c1_slave_dev_set(&ipmb_dev, 0x05, (uint8_t*)&buffer[0], 2); error = I2C_i2c1_master_write(&ipmb_dev); if (error) { DEBUG("i2c0 error=0x%x\r\n", error); } else { DEBUG("ina230 cal=0x%x\r\n", (buffer[0] << 8) | buffer[1]); } I2C_i2c1_slave_dev_set(&ipmb_dev, 0x04, (uint8_t*)&buffer[0], 2); error = I2C_i2c1_master_read(&ipmb_dev); if (error) { DEBUG("i2c0 error=0x%x\r\n", error); } else { DEBUG("ina230 cur=0x%x\r\n", (buffer[0] << 8) | buffer[1]); } I2C_i2c1_slave_dev_set(&ipmb_dev, 0x03, (uint8_t*)&buffer[0], 2); error = I2C_i2c1_master_read(&ipmb_dev); if (error) { DEBUG("i2c0 error=0x%x\r\n", error); } else { DEBUG("ina230 pow=0x%x\r\n", (buffer[0] << 8) | buffer[1]); } OSTimeDlyHMSM(0, 0, 3, 0); } #endif #if 0 while (1) { // read device id temp_val[0] = temp_val[1] = 0; I2C_i2c1_slave_dev_set(&adt7470_dev, 0x3d, (uint8_t*)&temp_val[0], 1); error = I2C_i2c1_master_read(&adt7470_dev); if (error) { DEBUG("error=0x%x\r\n", error); } else { DEBUG("device id=0x%x\t", temp_val[0]); } OSTimeDlyHMSM(0, 0, 1, 0); DEBUG("\r\n"); } #endif #endif #if 1 // test empty while (1) { OSTimeDlyHMSM(0, 0, 1, 0); } #endif }
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; }
//***************************************************************************** // // 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]); } } }