Arduino I2C port to Spark Wire

My second post! This is exhilarating. I admittedly know very little about I2C. However, I’m using a library to interface to the MMA8452 Accelerometer. I know that the library utilizes pull ups, does the Spark I2C library support them?

byte readRegister(uint8_t address)
{
	byte data;
	
	i2cSendStart();
	i2cWaitForComplete();
	
	i2cSendByte((MMA8452_ADDRESS<<1));	// write 0xB4
	i2cWaitForComplete();
	
	i2cSendByte(address);	// write register address
	i2cWaitForComplete();
	
	i2cSendStart();
	
	i2cSendByte((MMA8452_ADDRESS<<1)|0x01);	// write 0xB5
	i2cWaitForComplete();
	i2cReceiveByte(TRUE);
	i2cWaitForComplete();
	
	data = i2cGetReceivedByte();	// Get MSB result
	i2cWaitForComplete();
	i2cSendStop();
	
	cbi(TWCR, TWEN);	// Disable TWI
	sbi(TWCR, TWEN);	// Enable TWI
	
	return data;
}


   void eHealthClass::readRegisters(byte address, int i, byte * dest)
{
	i2cSendStart();
	i2cWaitForComplete();
	
	i2cSendByte((MMA8452_ADDRESS<<1));	// write 0xB4
	i2cWaitForComplete();
	
	i2cSendByte(address);	// write register address
	i2cWaitForComplete();
	
	i2cSendStart();
	i2cSendByte((MMA8452_ADDRESS<<1)|0x01);	// write 0xB5
	i2cWaitForComplete();
	
	for (int j=0; j<i; j++) {
		i2cReceiveByte(TRUE);
		i2cWaitForComplete();
		dest[j] = i2cGetReceivedByte();	// Get MSB result
	}
	
	i2cWaitForComplete();
	i2cSendStop();
	
	cbi(TWCR, TWEN);// Disable TWI
	sbi(TWCR, TWEN);// Enable TWI
}

The writeRegister function looks like this:

    void writeRegister(unsigned char address, unsigned char data)
{
	i2cSendStart();
	i2cWaitForComplete();  
	i2cSendByte((MMA8452_ADDRESS<<1));// write 0xB4
	i2cWaitForComplete();  
	i2cSendByte(address);// write register address
	i2cWaitForComplete();  
	i2cSendByte(data);
	i2cWaitForComplete();  
	i2cSendStop();
}

Perhaps I’m in over my head with this one! I just really don’t understand much about I2C. Do all of these I2C commands have equivalents in the Spark Wire library? That being said, is it possible to develop libraries for Spark? Or does everything have to be on one page? Or, would it be simpler to just have a small arduino talk to the accelerometer and then talk to the Spark over I2C. I’ve written a lot of code around those two functions, so if they don’t have replacements, thats my only other option.

Thanks for any help! It’s been a blast.

Is there a problem that you are running into with this code? I2C does require a pullup resistor on each pin and as far as I know, this is expected to already exist when you start making calls to the I2C functions (the library won’t configure your pins for you). You can probably set the I2C pins to output and configure them to use the internal pullups (check the pinMode function in the API docs), or you could just put your pullups external to the Core on your breadboard (around 4.7K is probably good).

I do not know about creating your own library in the Spark webIDE but I am sure you could use the Arduino ‘Wire’ library (#include <Wire.h>), which implements the I2C interface. Your functions would be slightly different

/* Write a byte to a register over I2C */
Wire.beginTransmission(deviceAddress);
Wire.write(addr);  // send the address to write
Wire.write(data);  // send the data to write
myerr = Wire.endTransmission();  // capture any error codes

/* Read numReads sequential bytes from I2C address */
Wire.beginTransmission(deviceAddress);
Wire.write(addr);  // send the address to start reading from
Wire.endTransmission();
Wire.requestFrom(deviceAddress, numReads);  // tell it how many bytes you want
data = Wire.read()  // get the data

These are the kinda of things that making a wrapper function would be very useful for. I hope this helps.