/** * \brief Sets up the EEPROM I2C interface * * \param slaveAddr Slave Address of the EEPROM * * \return None. */ void EEPROMI2CSetUp(unsigned int slaveAddr) { /* Configuring system clocks for I2C0 instance. */ I2C0ModuleClkConfig(); /* Performing Pin Multiplexing for I2C0. */ I2C0PinMux(); /* Put i2c in reset/disabled state */ I2CMasterDisable(I2C_BASE_ADDR); I2CSoftReset(I2C_BASE_ADDR); /* Configure i2c bus speed to 100khz */ I2CMasterInitExpClk(I2C_BASE_ADDR, 48000000, 24000000, 100000); /* Set i2c slave address */ I2CMasterSlaveAddrSet(I2C_BASE_ADDR, slaveAddr); /* Disable all I2C interrupts */ I2CMasterIntDisableEx(I2C_BASE_ADDR, 0xFFFFFFFF); /* Bring I2C module out of reset */ I2CMasterEnable(I2C_BASE_ADDR); while(!I2CSystemStatusGet(I2C_BASE_ADDR)); }
//***************************************************************************** // //! Initializes the I2C master block //! //! \param ui32I2CClk is the rate of the clock supplied to the I2C module. //! \param bFast set up for fast data transfers //! //! This function initializes operation of the I2C master block. Upon //! successful initialization of the I2C block, this functionhas set the //! bus speed for the master, and has enabled the I2C master block. //! //! If the parameter \e bFast is \b true, then the master block will be set up //! to transfer data at 400 kbps; otherwise, it will be set up to transfer data //! at 100 kbps. //! //! The peripheral clock will be the same as the processor clock. This will be //! the value returned by SysCtrlClockGet(), or it can be explicitly hardcoded //! if it is constant and known (to save the code/execution overhead of a call //! to SysCtrlClockGet()). //! //! \return None // //***************************************************************************** void I2CMasterInitExpClk(uint32_t ui32I2CClk, bool bFast) { uint32_t ui32SCLFreq; uint32_t ui32TPR; // // Must enable the device before doing anything else. // I2CMasterEnable(); // // Get the desired SCL speed. // if(bFast == true) { ui32SCLFreq = 400000; } else { ui32SCLFreq = 100000; } // // Compute the clock divider that achieves the fastest speed less than or // equal to the desired speed. The numerator is biased to favor a larger // clock divider so that the resulting clock is always less than or equal // to the desired clock, never greater. // ui32TPR = ((ui32I2CClk + (2 * 10 * ui32SCLFreq) - 1) / (2 * 10 * ui32SCLFreq)) - 1; HWREG(I2CM_TPR) = ui32TPR; }
//***************************************************************************** //! Initializes the I2C Master block. //! //! \param ulBase is the base address of the I2C Master module. //! \param ulI2CClk is the rate of the clock supplied to the I2C module. //! \param bFast set up for fast data transfers //! //! This function initializes operation of the I2C Master block. Upon //! successful initialization of the I2C block, this function will have set the //! bus speed for the master, and will have enabled the I2C Master block. //! //! If the parameter \e bFast is \b true, then the master block will be set up //! to transfer data at 400 kbps; otherwise, it will be set up to transfer data //! at 100 kbps. //! //! The peripheral clock will be the same as the processor clock. This will be //! the value returned by SysCtlClockGet(), or it can be explicitly hard coded //! if it is constant and known (to save the code/execution overhead of a call //! to SysCtlClockGet()). //! //! This function replaces the original I2CMasterInit() API and performs the //! same actions. A macro is provided in <tt>i2c.h</tt> to map the original //! API to this API. //! //! \return None. //***************************************************************************** void I2CMasterInitExpClk(unsigned long ulBase, unsigned long ulI2CClk, tBoolean bFast) { unsigned long ulSCLFreq; unsigned long ulTPR; // Check the arguments. ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE)); // Must enable the device before doing anything else. I2CMasterEnable(ulBase); // Get the desired SCL speed. if(bFast == true) { ulSCLFreq = 400000; } else { ulSCLFreq = 100000; } // Compute the clock divider that achieves the fastest speed less than or // equal to the desired speed. The numerator is biased to favor a larger // clock divider so that the resulting clock is always less than or equal // to the desired clock, never greater. ulTPR = ((ulI2CClk + (2 * 10 * ulSCLFreq) - 1) / (2 * 10 * ulSCLFreq)) - 1; HWREG(ulBase + I2C_O_MTPR) = ulTPR; }
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(); }
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 ); }
/** * \brief Initializes the I2C interface for a codec * * \param baseAddr Base Address of the I2C Module Registers which * is used for the codec * intCh Channel Number where the I2C ISR to be registered * slaveAddr Slave Address of the codec * * Note: This API enables the system interrupt for the given I2C module only. * It does not do any pin multiplexing or global interrupt enabling. * This shall be called only after AINTC initialization. * * \return None. * **/ void I2CCodecIfInit(unsigned int baseAddr, unsigned int intCh, unsigned int slaveAddr) { unsigned int sysIntNum = 0; /* Put i2c in reset/disabled state */ I2CMasterDisable(baseAddr); /* Configure i2c bus speed to 100khz */ I2CMasterInitExpClk(baseAddr, 24000000, 8000000, 100000); /* Set i2c slave address */ I2CMasterSlaveAddrSet(baseAddr, slaveAddr); I2CMasterEnable(baseAddr); /* ** Setup the interrupt in AINTC for the i2c module. ** If another instance is to be added, this shall include ** checking for the other instance base address also. */ if(SOC_I2C_0_REGS == baseAddr) { #ifdef _TMS320C6X sysIntNum = SYS_INT_I2C0_INT; #else sysIntNum = SYS_INT_I2CINT0; #endif } I2CCodecIntSetup(sysIntNum, intCh); }
/* ** Initializes the I2C interface for a slave */ void I2C0IfConfig(unsigned int slaveAddr, unsigned int speed) { /* Put i2c in reset/disabled state */ I2CMasterDisable(SOC_I2C_0_REGS); /* Configure i2c bus speed to 100khz */ I2CMasterInitExpClk(SOC_I2C_0_REGS, 24000000, 8000000, speed); /* Set i2c slave address */ I2CMasterSlaveAddrSet(SOC_I2C_0_REGS, slaveAddr); I2CMasterEnable(SOC_I2C_0_REGS); }
//***************************************************************************** // //! Initializes the I2C Master block. //! //! \param ulBase is the base address of the I2C Master module. //! \param ulI2CClk is the rate of the clock supplied to the I2C module. //! \param bFast set up for fast data transfers. //! //! This function initializes operation of the I2C Master block by configuring //! the bus speed for the master and enabling the I2C Master block. //! //! If the parameter \e bFast is \b true, then the master block is set up to //! transfer data at 400 Kbps; otherwise, it is set up to transfer data at //! 100 Kbps. If Fast Mode Plus (1 Mbps) is desired, software should manually //! write the I2CMTPR after calling this function. For High Speed (3.4 Mbps) //! mode, a specific command is used to switch to the faster clocks after the //! initial communication with the slave is done at either 100 Kbps or //! 400 Kbps. //! //! The peripheral clock is the same as the processor clock. This value is //! returned by SysCtlClockGet(), or it can be explicitly hard coded if it is //! constant and known (to save the code/execution overhead of a call to //! SysCtlClockGet()). //! //! This function replaces the original I2CMasterInit() API and performs the //! same actions. A macro is provided in <tt>i2c.h</tt> to map the original //! API to this API. //! //! \return None. // //***************************************************************************** void I2CMasterInitExpClk(unsigned long ulBase, unsigned long ulI2CClk, tBoolean bFast) { unsigned long ulSCLFreq; unsigned long ulTPR; // // Check the arguments. // ASSERT(I2CMasterBaseValid(ulBase)); // // Must enable the device before doing anything else. // I2CMasterEnable(ulBase); // // Get the desired SCL speed. // if(bFast == true) { ulSCLFreq = 400000; } else { ulSCLFreq = 100000; } // // Compute the clock divider that achieves the fastest speed less than or // equal to the desired speed. The numerator is biased to favor a larger // clock divider so that the resulting clock is always less than or equal // to the desired clock, never greater. // ulTPR = ((ulI2CClk + (2 * 10 * ulSCLFreq) - 1) / (2 * 10 * ulSCLFreq)) - 1; HWREG(ulBase + I2C_O_MTPR) = ulTPR; // // Check to see if this I2C peripheral is High-Speed enabled. If yes, also // choose the fastest speed that is less than or equal to 3.4 Mbps. // if(HWREG(ulBase + I2C_O_PP) & I2C_PP_HS) { ulTPR = ((ulI2CClk + (2 * 3 * 3400000) - 1) / (2 * 3 * 3400000)) - 1; HWREG(ulBase + I2C_O_MTPR) = I2C_MTPR_HS | ulTPR; } }
//***************************************************************************** // //! 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; }
void I2CController::setup(GPIOPin sda, GPIOPin scl, speed_t speed, bool doEnableInterrupts, uint32_t defaultTimeout) { RecursiveMutexGuard guard(&_lock); _sda = sda; _scl = scl; _defaultTimeout = defaultTimeout; MAP_SysCtlPeripheralEnable(_periph); SysCtlPeripheralReset(_periph); _sda.enablePeripheral(); if (_base == I2C0_MASTER_BASE) { _sda.mapAsI2C0SDA(); } else if (_base == I2C1_MASTER_BASE) { _sda.mapAsI2C1SDA(); } else { while(1) { /* we should never get here! */ } } _sda.configure(GPIOPin::I2C); _scl.enablePeripheral(); if (_base == I2C0_MASTER_BASE) { _scl.mapAsI2C0SCL(); } else if (_base == I2C1_MASTER_BASE) { _scl.mapAsI2C1SCL(); } else { while(1) { /* we should never get here! */ } } _scl.configure(GPIOPin::I2CSCL); I2CMasterInitExpClk(_base, MAP_SysCtlClockGet(), (speed==speed_400kBit) ? 1 : 0); I2CMasterEnable(_base); if (doEnableInterrupts) enableInterrupts(true, true); // Do a dummy receive to make sure you don't get junk on the first receive. I2CMasterControl(_base, I2C_MASTER_CMD_SINGLE_RECEIVE); waitFinish(1); }
static void SetupI2C(void) { I2C0_BASE = vmm_find_iomap("SOC_I2C_0_REGS"); /* Enable the clock for I2C0 */ I2C0ModuleClkConfig(); I2CPinMuxSetup(0); /* Put i2c in reset/disabled state */ I2CMasterDisable(I2C0_BASE); /* Disable auto Idle functionality */ I2CAutoIdleDisable(I2C0_BASE); /* Configure i2c bus speed to 100khz */ I2CMasterInitExpClk(I2C0_BASE, 48000000, 12000000, 100000); /* Set i2c slave address */ I2CMasterSlaveAddrSet(I2C0_BASE, I2C_SLAVE_ADDR); /* Bring I2C out of reset */ I2CMasterEnable(I2C0_BASE); }
/** * \brief Initializes the I2C interface for a codec * * \param baseAddr Base Address of the I2C Module Registers which * is used for the codec * slaveAddr Slave Address of the codec * * Note: This API enables the system interrupt for the given I2C module only. * It does not do any pin multiplexing or global interrupt enabling. * This shall be called only after AINTC initialization. * * \return None. * **/ void I2CCodecIfInit(unsigned int baseAddr, unsigned int slaveAddr) { /* Put i2c in reset/disabled state */ I2CMasterDisable(baseAddr); /* Disable the auto idle functionality */ I2CAutoIdleDisable(baseAddr); /* Configure i2c bus speed to 100khz */ I2CMasterInitExpClk(SOC_I2C_1_REGS, 48000000, 12000000, 100000); /* Set i2c slave address */ I2CMasterSlaveAddrSet(baseAddr, slaveAddr); I2CMasterEnable(baseAddr); IntRegister(SYS_INT_I2C1INT, I2CCodecIsr); IntPrioritySet(SYS_INT_I2C1INT, 0, AINTC_HOSTINT_ROUTE_IRQ); IntSystemEnable(SYS_INT_I2C1INT); }
void I2CController::setup(GPIOPin sda, GPIOPin scl, speed_t speed) { MutexGuard guard(&_lock); _sda = sda; _scl = scl; ROM_SysCtlPeripheralEnable(_periph); SysCtlPeripheralReset(_periph); _sda.enablePeripheral(); if (_base == I2C0_MASTER_BASE) { _sda.mapAsI2C0SDA(); } else if (_base == I2C1_MASTER_BASE) { _sda.mapAsI2C1SDA(); } else { while(1) { /* we should never get here! */ } } _sda.configure(GPIOPin::I2C); _scl.enablePeripheral(); if (_base == I2C0_MASTER_BASE) { _scl.mapAsI2C0SCL(); } else if (_base == I2C1_MASTER_BASE) { _scl.mapAsI2C1SCL(); } else { while(1) { /* we should never get here! */ } } _scl.configure(GPIOPin::I2CSCL); I2CMasterInitExpClk(_base, ROM_SysCtlClockGet(), (speed==speed_400kBit) ? 1 : 0); I2CMasterEnable(_base); // Do a dummy receive to make sure you don't get junk on the first receive. I2CMasterControl(_base, I2C_MASTER_CMD_SINGLE_RECEIVE); while(I2CMasterBusy(_base)); }
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"); }
/* * ======== I2CCC26XX_hwInit ======== * This functions initializes the I2C hardware module. * * @pre Function assumes that the I2C handle is pointing to a hardware * module which has already been opened. */ static void I2CCC26XX_initHw(I2C_Handle handle) { I2CCC26XX_Object *object; I2CCC26XX_HWAttrs const *hwAttrs; Types_FreqHz freq; /* Get the pointer to the object and hwAttrs */ object = handle->object; hwAttrs = handle->hwAttrs; /* Set the I2C configuration */ BIOS_getCpuFreq(&freq); I2CMasterInitExpClk(hwAttrs->baseAddr, freq.lo, bitRate[object->bitRate]); /* Clear any pending interrupts */ I2CMasterIntClear(hwAttrs->baseAddr); /* Enable the I2C Master for operation */ I2CMasterEnable(hwAttrs->baseAddr); /* Unmask I2C interrupts */ I2CMasterIntEnable(hwAttrs->baseAddr); }
int main(void) { // Clock (80MHz) SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN); // GPIO SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1); GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0); // UART SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200, (UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE | UART_CONFIG_WLEN_8)); UARTEnable(UART0_BASE); // I2C SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); GPIOPinConfigure(GPIO_PB2_I2C0SCL); GPIOPinConfigure(GPIO_PB3_I2C0SDA); GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false); I2CMasterEnable(I2C0_BASE); // Scan for addresses GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1); printf("Scan started\r\n"); for(i=0; i<255; i++) { I2CMasterSlaveAddrSet(I2C0_BASE, i>>1, false); I2CMasterDataPut(I2C0_BASE, 0); I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND); SysCtlDelay(SysCtlClockGet()/10000); if (I2CMasterErr(I2C0_BASE) == I2C_MASTER_ERR_NONE) { printf("x%02X\r\n", i); } else { //printf(" N\r\n"); } } printf("\r\nScan complete\r\n\r\n"); GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0); while(1); }
/********************************************************************************************** * 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); */ } }