/* ** Receives data over I2C bus */ void SetupI2CReception(int count) { I2CSetDataCount(SOC_I2C_0_REGS, count); I2CMasterControl(SOC_I2C_0_REGS, I2C_CFG_MST_RX | I2C_CFG_STOP); I2CMasterIntEnableEx(SOC_I2C_0_REGS, I2C_INT_DATA_READY | I2C_INT_STOP_CONDITION | I2C_INT_NO_ACK); I2CMasterStart(SOC_I2C_0_REGS); while(flag); flag = 1; }
/* ** Transmits data over I2C bus */ void SetupI2CTransmit2(int count) { I2CSetDataCount(SOC_I2C_0_REGS, count); I2CMasterControl(SOC_I2C_0_REGS, I2C_CFG_MST_TX | I2C_CFG_STOP); I2CMasterIntEnableEx(SOC_I2C_0_REGS, I2C_INT_TRANSMIT_READY | I2C_INT_STOP_CONDITION | I2C_INT_NO_ACK); I2CMasterStart(SOC_I2C_0_REGS); while(flag); flag = 1; }
/* ** Function to receive data from the Codec through I2C bus */ static void I2CCodecRcvBlocking(unsigned int baseAddr, unsigned int dataCnt) { txCompFlag = 1; dataIdx = 0; savedBase = baseAddr; I2CSetDataCount(baseAddr, dataCnt); I2CMasterControl(baseAddr, I2C_CFG_MST_RX | I2C_CFG_STOP); I2CMasterIntEnableEx(baseAddr, I2C_INT_DATA_READY | I2C_INT_STOP_CONDITION); I2CMasterStart(baseAddr); /* Wait till data is received fully */ while(txCompFlag); }
/* ** Function to send data through i2c */ static void I2CCodecSendBlocking(unsigned int baseAddr, unsigned int dataCnt) { txCompFlag = 1; dataIdx = 0; savedBase = baseAddr; I2CSetDataCount(baseAddr, dataCnt); I2CMasterControl(baseAddr, I2C_CFG_MST_TX | I2C_CFG_STOP); I2CMasterIntEnableEx(baseAddr, I2C_INT_TRANSMIT_READY | I2C_INT_STOP_CONDITION); I2CMasterStart(baseAddr); /* Wait till the data is sent */ while(txCompFlag); }
/* 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); }
/* ** Function to receive data from the Codec through I2C bus */ void I2C0RcvBlocking(unsigned int dataCnt) { txCompFlag = 1; dataIdx = 0; while(I2C0IsBusy()); I2CSetDataCount(SOC_I2C_0_REGS, dataCnt); I2CMasterControl(SOC_I2C_0_REGS, I2C_CFG_MST_RX | I2C_CFG_STOP); I2CMasterIntEnableEx(SOC_I2C_0_REGS, I2C_INT_DATA_READY | I2C_INT_STOP_CONDITION | I2C_INT_NO_ACK); I2CMasterStart(SOC_I2C_0_REGS); /* Wait till data is received fully */ while(txCompFlag); while(I2C0IsBusy()); }
/* ** Function to send data through i2c */ void I2C0SendBlocking(unsigned int dataCnt) { txCompFlag = 1; dataIdx = 0; while(I2C0IsBusy()); I2CSetDataCount(SOC_I2C_0_REGS, dataCnt); I2CMasterControl(SOC_I2C_0_REGS, I2C_CFG_MST_TX | I2C_CFG_STOP); I2CMasterIntEnableEx(SOC_I2C_0_REGS, I2C_INT_TRANSMIT_READY | I2C_INT_STOP_CONDITION | I2C_INT_NO_ACK); I2CMasterStart(SOC_I2C_0_REGS); /* Wait till the data is sent */ while(txCompFlag); while(I2C0IsBusy()); }
/* Clear status of all interrupts */ void CleanUpInterrupts(new_twi* TwiStruct) { if(TwiStruct->WithInterrupt)I2CMasterIntEnableEx(TwiStruct->BaseAddr, 0x7FF); I2CMasterIntClearEx(TwiStruct->BaseAddr, 0x7FF); if(TwiStruct->WithInterrupt)I2CMasterIntDisableEx(TwiStruct->BaseAddr, 0x7FF); }
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); } }