//Initialize as a master void TwoWire::begin(void) { if(i2cModule == NOT_ACTIVE) { i2cModule = BOOST_PACK_WIRE; } ROM_SysCtlPeripheralEnable(g_uli2cPeriph[i2cModule]); //Configure GPIO pins for I2C operation ROM_GPIOPinConfigure(g_uli2cConfig[i2cModule][0]); ROM_GPIOPinConfigure(g_uli2cConfig[i2cModule][1]); ROM_GPIOPinTypeI2C(g_uli2cBase[i2cModule], g_uli2cSDAPins[i2cModule]); ROM_GPIOPinTypeI2CSCL(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); ROM_I2CMasterInitExpClk(MASTER_BASE, F_CPU, speedMode);//Bus speed if(speedMode==I2C_SPEED_FASTMODE_PLUS)//Force 1Mhz { uint32_t ui32TPR = ((F_CPU + (2 * 10 * 1000000l) - 1) / (2 * 10 * 1000000l)) - 1; HWREG(MASTER_BASE + I2C_O_MTPR) = ui32TPR; } //force a stop condition if(!ROM_GPIOPinRead(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule])) forceStop(); //Handle any startup issues by pulsing SCL if(ROM_I2CMasterBusBusy(MASTER_BASE) || ROM_I2CMasterErr(MASTER_BASE) || !ROM_GPIOPinRead(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule])) { uint8_t doI = 0; ROM_GPIOPinTypeGPIOOutput(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); unsigned long mask = 0; do { for(unsigned long i = 0; i < 10 ; i++) { if(speedMode==I2C_SPEED_FASTMODE_PLUS) ROM_SysCtlDelay(F_CPU/1000000/3);//1000Hz=desired frequency, delay iteration=3 cycles else if(speedMode==I2C_SPEED_FASTMODE) ROM_SysCtlDelay(F_CPU/400000/3);//400Hz=desired frequency, delay iteration=3 cycles else ROM_SysCtlDelay(F_CPU/100000/3);//100Hz=desired frequency, delay iteration=3 cycles mask = (i%2) ? g_uli2cSCLPins[i2cModule] : 0; ROM_GPIOPinWrite(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule], mask); } doI++; } while(ROM_I2CMasterBusBusy(MASTER_BASE) && doI < 100); ROM_GPIOPinTypeI2CSCL(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); if(!ROM_GPIOPinRead(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule])) forceStop(); } }
//Initialize as a master void TwoWire::begin(void) { pinMode(RED_LED, OUTPUT); if(i2cModule == NOT_ACTIVE) { i2cModule = BOOST_PACK_WIRE; } ROM_SysCtlPeripheralEnable(g_uli2cPeriph[i2cModule]); //Configure GPIO pins for I2C operation ROM_GPIOPinConfigure(g_uli2cConfig[i2cModule][0]); ROM_GPIOPinConfigure(g_uli2cConfig[i2cModule][1]); ROM_GPIOPinTypeI2C(g_uli2cBase[i2cModule], g_uli2cSDAPins[i2cModule]); ROM_GPIOPinTypeI2CSCL(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); ROM_I2CMasterInitExpClk(MASTER_BASE, F_CPU, false);//max bus speed=400kHz for gyroscope //force a stop condition if(!ROM_GPIOPinRead(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule])) forceStop(); //Handle any startup issues by pulsing SCL if(ROM_I2CMasterBusBusy(MASTER_BASE) || ROM_I2CMasterErr(MASTER_BASE) || !ROM_GPIOPinRead(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule])){ uint8_t doI = 0; ROM_GPIOPinTypeGPIOOutput(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); unsigned long mask = 0; do{ for(unsigned long i = 0; i < 10 ; i++) { ROM_SysCtlDelay(F_CPU/100000/3);//100Hz=desired frequency, delay iteration=3 cycles mask = (i%2) ? g_uli2cSCLPins[i2cModule] : 0; ROM_GPIOPinWrite(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule], mask); } doI++; }while(ROM_I2CMasterBusBusy(MASTER_BASE) && doI < 100); ROM_GPIOPinTypeI2CSCL(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); if(!ROM_GPIOPinRead(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule])) forceStop(); } }
uint8_t TwoWire::endTransmission(uint8_t sendStop) { uint8_t error = I2C_MASTER_ERR_NONE; if(TX_BUFFER_EMPTY) return 0; //Wait for any previous transaction to complete while(ROM_I2CMasterBusBusy(MASTER_BASE)); while(ROM_I2CMasterBusy(MASTER_BASE)); //Select which slave we are requesting data from //false indicates we are writing to the slave ROM_I2CMasterSlaveAddrSet(MASTER_BASE, txAddress, false); while(ROM_I2CMasterBusy(MASTER_BASE)); unsigned long cmd = RUN_BIT | START_BIT; error = sendTxData(cmd,txBuffer[txReadIndex]); txReadIndex = (txReadIndex + 1) % BUFFER_LENGTH; if(error) return error; while(!TX_BUFFER_EMPTY) { error = sendTxData(RUN_BIT,txBuffer[txReadIndex]); txReadIndex = (txReadIndex + 1) % BUFFER_LENGTH; if(error) return getError(error); } if(txWriteRomQuantity!=0) // Hey we have Rom Block data to transmit! { while(txWriteRomIndex != txWriteRomQuantity) { uint8_t c = *PtrTxRomBuffer++; error = sendTxData(RUN_BIT,c); txWriteRomIndex++; if(error) return getError(error); } txWriteRomQuantity=0; // Clear to disable further tx } if(sendStop) { while(ROM_I2CMasterBusy(MASTER_BASE)); HWREG(MASTER_BASE + I2C_O_MCS) = STOP_BIT; while(ROM_I2CMasterBusy(MASTER_BASE)); currentState = IDLE; } else { currentState = MASTER_TX; } // indicate that we are done transmitting transmitting = 0; return error; }
uint8_t TwoWire::getRxData(unsigned long cmd) { if (currentState == IDLE) while(ROM_I2CMasterBusBusy(MASTER_BASE)); HWREG(MASTER_BASE + I2C_O_MCS) = cmd; while(ROM_I2CMasterBusy(MASTER_BASE)); uint8_t error = ROM_I2CMasterErr(MASTER_BASE); if (error != I2C_MASTER_ERR_NONE) { ROM_I2CMasterControl(MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP); } else { while(ROM_I2CMasterBusy(MASTER_BASE)); rxBuffer[rxWriteIndex] = ROM_I2CMasterDataGet(MASTER_BASE); rxWriteIndex = (rxWriteIndex + 1) % BUFFER_LENGTH; } return error; }
uint8_t TwoWire::getRxData(unsigned long cmd) { if (currentState == IDLE) while(ROM_I2CMasterBusBusy(MASTER_BASE)); HWREG(MASTER_BASE + I2C_O_MCS) = cmd; while(ROM_I2CMasterBusy(MASTER_BASE)); uint8_t error = ROM_I2CMasterErr(MASTER_BASE); if (error != I2C_MASTER_ERR_NONE) { ROM_I2CMasterControl(MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP); } else { if(speedMode==I2C_SPEED_STANDARD) //Fast modes works without delay (Tested on stellaris&GY-80) delayMicroseconds(SLOWMODE_DELAYUS); rxBuffer[rxWriteIndex] = ROM_I2CMasterDataGet(MASTER_BASE); rxWriteIndex = (rxWriteIndex + 1) % BUFFER_LENGTH; } return error; }
uint8_t TwoWire::endTransmission(uint8_t sendStop) { uint8_t error = I2C_MASTER_ERR_NONE; digitalWrite(RED_LED, HIGH); if(TX_BUFFER_EMPTY) return 0; //Wait for any previous transaction to complete while(ROM_I2CMasterBusBusy(MASTER_BASE)); // Here it hangs sometimes! digitalWrite(RED_LED, LOW); while(ROM_I2CMasterBusy(MASTER_BASE)); //Select which slave we are requesting data from //false indicates we are writing to the slave ROM_I2CMasterSlaveAddrSet(MASTER_BASE, txAddress, false); while(ROM_I2CMasterBusy(MASTER_BASE)); unsigned long cmd = RUN_BIT | START_BIT; error = sendTxData(cmd,txBuffer[txReadIndex]); txReadIndex = (txReadIndex + 1) % BUFFER_LENGTH; if(error) return error; while(!TX_BUFFER_EMPTY) { error = sendTxData(RUN_BIT,txBuffer[txReadIndex]); txReadIndex = (txReadIndex + 1) % BUFFER_LENGTH; if(error) return getError(error); } if(sendStop) { while(ROM_I2CMasterBusy(MASTER_BASE)); HWREG(MASTER_BASE + I2C_O_MCS) = STOP_BIT; while(ROM_I2CMasterBusy(MASTER_BASE)); currentState = IDLE; } else { currentState = MASTER_TX; } // indicate that we are done transmitting transmitting = 0; return error; }