/* * 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 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); }
/** * \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)); }
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 ); }
void I2C_Init0(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. 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. I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false); //Thrasher - Why was this here? //clear I2C FIFOs //HWREG(I2C0_BASE + I2C_O_FIFOCTL) = 80008000; }
/** * \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); }
//***************************************************************************** // // Configures the I2C1 port as a master for connection to the Gyro // //***************************************************************************** void Accel_Configure(void) { // Enable and initialize the I2C1 master module. Use the system clock for // the I2C1 module. The last parameter sets the I2C data transfer rate: // false = 100kbps, true = 400kbps I2CMasterInitExpClk(I2C1_BASE, SysCtlClockGet(), false); }
/* ** 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); }
/******** 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 ); }
void I2CSetup(void) { I2CPinMuxSetup(0); /* Put i2c in reset/disabled state */ I2CMasterDisable(SOC_I2C_0_REGS); /* Configure i2c bus speed to 100khz */ I2CMasterInitExpClk(SOC_I2C_0_REGS, 24000000, 8000000, 100000); /* Set i2c slave address */ I2CMasterSlaveAddrSet(SOC_I2C_0_REGS, 0x48); ConfigureAINTCIntI2C(); }
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]); } } }
/* 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); }
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); }
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); }
//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(); } }
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); }
/** * \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 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 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); }
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; }
//***************************************************************************** // // Configure the I2C0 master and slave and connect them using loopback mode. // //***************************************************************************** int main(void) { unsigned long ulDataTx[NUM_I2C_DATA]; unsigned long ulDataRx[NUM_I2C_DATA]; unsigned long ulindex; // // Set the clocking to run directly from the external crystal/oscillator. // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the // crystal on your board. // SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // // The I2C0 peripheral must be enabled before use. // SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); // // For this example I2C0 is used with PortB[3:2]. The actual port and // pins used may be different on your part, consult the data sheet for // more information. GPIO port B needs to be enabled so these pins can // be used. // TODO: change this to whichever GPIO port you are using. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // // Configure the pin muxing for I2C0 functions on port B2 and B3. // This step is not necessary if your part does not support pin muxing. // TODO: change this to select the port/pin you are using. // GPIOPinConfigure(GPIO_PB2_I2C0SCL); GPIOPinConfigure(GPIO_PB3_I2C0SDA); // // Select the I2C function for these pins. This function will also // configure the GPIO pins pins for I2C operation, setting them to // open-drain operation with weak pull-ups. Consult the data sheet // to see which functions are allocated per pin. // TODO: change this to select the port/pin you are using. // GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3); // // Enable loopback mode. Loopback mode is a built in feature that is // useful for debugging I2C operations. It internally connects the I2C // master and slave terminals, which effectively let's you send data as // a master and receive data as a slave. // NOTE: For external I2C operation you will need to use external pullups // that are stronger than the internal pullups. Refer to the datasheet for // more information. // HWREG(I2C0_MASTER_BASE + I2C_O_MCR) |= 0x01; // // Enable and initialize the I2C0 master module. Use the system clock for // the I2C0 module. The last parameter sets the I2C data transfer rate. // If false the data rate is set to 100kbps and if true the data rate will // be set to 400kbps. For this example we will use a data rate of 100kbps. // I2CMasterInitExpClk(I2C0_MASTER_BASE, SysCtlClockGet(), false); // // Enable the I2C0 slave module. This module is enabled only for testing // purposes. It does not need to be enabled for proper operation of the // I2Cx master module. // I2CSlaveEnable(I2C0_SLAVE_BASE); // // Set the slave address to SLAVE_ADDRESS. In loopback mode, it's an // arbitrary 7-bit number (set in a macro above) that is sent to the // I2CMasterSlaveAddrSet function. // I2CSlaveInit(I2C0_SLAVE_BASE, SLAVE_ADDRESS); // // Tell the master module what address it will place on the bus when // communicating with the slave. Set the address to SLAVE_ADDRESS // (as set in the slave module). The receive parameter is set to false // which indicates the I2C Master is initiating a writes to the slave. If // true, that would indicate that the I2C Master is initiating reads from // the slave. // I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, SLAVE_ADDRESS, false); // // Set up the serial console to use for displaying messages. This is // just for this example program and is not needed for I2C operation. // InitConsole(); // // Display the example setup on the console. // UARTprintf("I2C Loopback Example ->"); UARTprintf("\n Module = I2C0"); UARTprintf("\n Mode = Single Send/Receive"); UARTprintf("\n Rate = 100kbps\n\n"); // // Initalize the data to send. // ulDataTx[0] = 'I'; ulDataTx[1] = '2'; ulDataTx[2] = 'C'; // // Initalize the receive buffer. // for(ulindex = 0; ulindex < NUM_I2C_DATA; ulindex++) { ulDataRx[ulindex] = 0; } // // Indicate the direction of the data. // UARTprintf("Tranferring from: Master -> Slave\n"); // // Send 3 peices of I2C data from the master to the slave. // for(ulindex = 0; ulindex < NUM_I2C_DATA; ulindex++) { // // Display the data that the I2C0 master is transferring. // UARTprintf(" Sending: '%c' . . . ", ulDataTx[ulindex]); // // Place the data to be sent in the data register // I2CMasterDataPut(I2C0_MASTER_BASE, ulDataTx[ulindex]); // // Initiate send of data from the master. Since the loopback // mode is enabled, the master and slave units are connected // allowing us to receive the same data that we sent out. // I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND); // // Wait until the slave has received and acknowledged the data. // while(!(I2CSlaveStatus(I2C0_SLAVE_BASE) & I2C_SCSR_RREQ)) { } // // Read the data from the slave. // ulDataRx[ulindex] = I2CSlaveDataGet(I2C0_SLAVE_BASE); // // Wait until master module is done transferring. // while(I2CMasterBusy(I2C0_MASTER_BASE)) { } // // Display the data that the slave has received. // UARTprintf("Received: '%c'\n", ulDataRx[ulindex]); } // // Reset receive buffer. // for(ulindex = 0; ulindex < NUM_I2C_DATA; ulindex++) { ulDataRx[ulindex] = 0; } // // Indicate the direction of the data. // UARTprintf("\n\nTranferring from: Slave -> Master\n"); // // Modifiy the data direction to true, so that seeing the address will // indicate that the I2C Master is initiating a read from the slave. // I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, SLAVE_ADDRESS, true); // // Do a dummy receive to make sure you don't get junk on the first receive. // I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); // // Dummy acknowledge and wait for the receive request from the master. // This is done to clear any flags that should not be set. // while(!(I2CSlaveStatus(I2C0_SLAVE_BASE) & I2C_SLAVE_ACT_TREQ)) { } for(ulindex = 0; ulindex < NUM_I2C_DATA; ulindex++) { // // Display the data that I2C0 slave module is transferring. // UARTprintf(" Sending: '%c' . . . ", ulDataTx[ulindex]); // // Place the data to be sent in the data register // I2CSlaveDataPut(I2C0_SLAVE_BASE, ulDataTx[ulindex]); // // Tell the master to read data. // I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); // // Wait until the slave is done sending data. // while(!(I2CSlaveStatus(I2C0_SLAVE_BASE) & I2C_SLAVE_ACT_TREQ)) { } // // Read the data from the master. // ulDataRx[ulindex] = I2CMasterDataGet(I2C0_MASTER_BASE); // // Display the data that the slave has received. // UARTprintf("Received: '%c'\n", ulDataRx[ulindex]); } // // Tell the user that the test is done. // UARTprintf("\nDone.\n\n"); // // Return no errors // return(0); }
int main(void) { unsigned long ulPeriod; unsigned long ulDelay; unsigned long recvData = 11; volatile int status = 0; char buffer[40]; unsigned int bytesdRead; FRESULT res; FIL fileobj; SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ); //Initialize pins init_pins(); //Initialize peripherals init_periph(); I2CMasterInitExpClk(I2C0_MASTER_BASE, SysCtlClockGet(), false);//false for 100kHz mode //0x68 is the 7-bit address of the DS1307 I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, 0x68, false);//true for a read action I2CMasterDataPut(I2C0_MASTER_BASE, 0x10); //first Date/Time Register I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND); while(I2CMasterBusy(I2C0_MASTER_BASE)){} I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); while(I2CMasterBusy(I2C0_MASTER_BASE)){} recvData = I2CMasterDataGet(I2C0_MASTER_BASE); while(I2CMasterBusy(I2C0_MASTER_BASE)){} fs_mount(); if(disk_status(0) == 0) { status++; } else { status--; } if (status == 1) { res = f_open(&fileobj, "config.txt", FA_OPEN_EXISTING | FA_READ); res = f_read(&fileobj, buffer, 6, &bytesdRead); } SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_0); ulPeriod = SysCtlClockGet() / 10; ulDelay = ((ulPeriod / 2) / 3) - 4 ; while(1) { // Turn on the LED GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0x01); // Delay for a bit SysCtlDelay(ulDelay); // Turn off the LED GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0x00); // Delay for a bit SysCtlDelay(ulDelay); } }
//***************************************************************************** // //! 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(); }
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 *********************************************************************************************/ 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); */ } }