/* Sends command to the I2C module and waits for it to be processed. */ static enum i2c_ack_type i2c_command(struct i2c_state *c, uint32_t cmd) { I2CMasterIntClear(c->base); I2CMasterTimeoutSet(c->base, 0x20); /* 5 ms @ 100 KHz */ I2CMasterControl(c->base, cmd); unsigned long mcs; while ((MAP_I2CMasterIntStatusEx(c->base, 0) & (I2C_MRIS_RIS | I2C_MRIS_CLKRIS)) == 0) { mcs = HWREG(c->base + I2C_O_MCS); dprintf(("busy mcs %lx cnt %lx\n", mcs, HWREG(c->base + I2C_O_MCLKOCNT))); } I2CMasterIntClearEx(c->base, I2C_MRIS_RIS | I2C_MRIS_CLKRIS); mcs = HWREG(c->base + I2C_O_MCS); dprintf(("mcs %lx\n", mcs)); if ((mcs & (I2C_MCS_ARBLST | I2C_MCS_ACK | I2C_MCS_ADRACK | I2C_MCS_CLKTO)) == 0) { return I2C_ACK; } else { /* This does not actually put STOP condition on the bus (bit 0 = 0), * but resets the error condition in the module. */ I2CMasterControl(c->base, I2C_MASTER_CMD_BURST_SEND_ERROR_STOP); return I2C_NAK; } }
int main(void) { MAP_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |SYSCTL_XTAL_12MHZ); //50MHZ // // Enable peripherals to operate when CPU is in sleep. // MAP_SysCtlPeripheralClockGating(true); // // Configure SysTick to occur 1000 times per second, to use as a time // reference. Enable SysTick to generate interrupts. // MAP_SysTickPeriodSet(MAP_SysCtlClockGet() / SYSTICKS_PER_SECOND); MAP_SysTickIntEnable(); MAP_SysTickEnable(); // // Get the current processor clock frequency. // ulClockMS = MAP_SysCtlClockGet() / (3 * 1000); // init Serial Comm initSerialComm(230400); // init SSI0 in slave mode initSPIComm(); #ifdef DEBUG UARTprintf("Setting up PID\n"); #endif initCarPID(); #ifdef DEBUG UARTprintf("done\n"); #endif #ifdef DEBUG UARTprintf("Setting up PWM ... \n"); #endif configurePWM(); configureGPIO(); #ifdef DEBUG UARTprintf("done\n"); #endif #ifdef DEBUG UARTprintf("Setting up Servo ... \n"); #endif servo_init(); servo_setPosition(90); #ifdef DEBUG UARTprintf("done\n"); #endif #ifdef DEBUG UARTprintf("Starting QEI..."); #endif encoder_init(); #ifdef DEBUG UARTprintf("done\n"); #endif #ifdef USE_I2C #ifdef DEBUG UARTprintf("Setting up I2C\n"); #endif //I2C MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); MAP_GPIOPinTypeI2C(GPIO_PORTB_AHB_BASE,GPIO_PIN_2 | GPIO_PIN_3); MAP_I2CMasterInitExpClk(I2C0_MASTER_BASE,SysCtlClockGet(),true); //false = 100khz , true = 400khz I2CMasterTimeoutSet(I2C0_MASTER_BASE, 1000); #ifdef DEBUG UARTprintf("done\n"); #endif #endif #ifdef USE_I2C #ifdef USE_INA226 #ifdef DEBUG UARTprintf("Setting up INA226\n"); #endif initINA226(); #ifdef DEBUG UARTprintf("done\n"); #endif #endif #endif while (1) { } }
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); I2CMasterInitExpClk( I2C3_MASTER_BASE, SysCtlClockGet(), false); SysCtlDelay(10000); I2CMasterTimeoutSet(I2C3_MASTER_BASE, 0x7d); I2CMasterSlaveAddrSet(I2C3_MASTER_BASE, I2C_SLAVE_ADDRESS, false); I2CMasterIntEnableEx(I2C3_MASTER_BASE, I2C_MASTER_INT_TIMEOUT|I2C_MASTER_INT_DATA); IntEnable(INT_I2C3); IntMasterEnable(); while(1) { i2c_flag=0; //Let's send various challenges to our slave data.op1++; data.op2+=2; switch(data.op1 % 4) { case 0: data.op = op_add; break; case 1: data.op = op_mult; break; case 2: data.op = op_div; break; case 3: data.op = op_subst; break; } what_we_re_doing = sending; sent_bytes = 0; //Start to send the full structure I2CMasterSlaveAddrSet(I2C3_MASTER_BASE, I2C_SLAVE_ADDRESS, false); I2CMasterDataPut(I2C3_MASTER_BASE, *((unsigned char *)&data)); I2CMasterControl(I2C3_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START); while(!i2c_flag); if (i2c_flag == 2) { //I2C Failure continue; } else if (i2c_flag == 1) { //I2C Success } what_we_re_doing = receiving; received_bytes = 0; //Start to receive the full structure I2CMasterSlaveAddrSet(I2C3_MASTER_BASE, I2C_SLAVE_ADDRESS, true); I2CMasterControl(I2C3_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); while(!i2c_flag); if (i2c_flag == 2) { //I2C Failure continue; } else if (i2c_flag == 1) { //I2C Success } SysCtlDelay(SysCtlClockGet()/100); } }