Development

Mote Runner:

General / I2C

This is the development section of the Waspmote Mote Runner platform. You can navigate through the examples with the left bar.

If you actually want to see examples about the original Waspmote platform, please go its specific development section.

» MR-General 05: I2C reading

This examples shows the use of the functions to control the i2c readings, reading some information of the RTC and printing it into the screen trough the USB.

Required Materials

1 x Waspmote PRO
1 x MiniUSB wire
1 x Battery

Notes

* Please, note that the output is in Hexadecimal
* Remember to connect the battery to Waspmote for proper operation.
* To compile this example: mrc --assembly=i2c-1.0 --system=waspmote --ref=logger-#.# I2CExample.java

Code

/**
 *  Copyright (C) 2013 Libelium Comunicaciones Distribuidas S.L.
 * http://www.libelium.com
 *
 * This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 3.0 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-3.0.html
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Design: David Gascón
 * 
 * @version 0.1
 * @author Anartz Nuin Jiménez, Diego López Pérez and Ignacio Ruiz Martín
 */
package com.libelium.examples.general;

import com.ibm.saguaro.logger.Logger;
import com.ibm.saguaro.system.DevCallback;
import com.ibm.saguaro.system.Device;
import com.ibm.saguaro.system.GPIO;
import com.ibm.saguaro.system.I2C;
import com.ibm.saguaro.system.Mote;
import com.ibm.saguaro.system.Util;
import com.ibm.saguaro.system.csr;
import com.ibm.waspmote.WASPMOTE;

public class I2CExample {
	
	static GPIO gpio = new GPIO();
	static I2C i2c = new I2C();
	private static byte RTC_I2C_ADDR= (byte) (0x68<<1);
	
	static{
		gpio.open();
		gpio.configureOutput(WASPMOTE.PIN_RTC_PW,GPIO.OUT_SET);
		
		i2c.open(I2C.BUS_0|I2C.SPEED_STD, RTC_I2C_ADDR, GPIO.NO_PIN, 0);
		byte[] cmd = new byte[2];
		cmd[0] = 0x00;
		cmd[1] = 0x02;
		
		//Write
		i2c.write(Device.ASAP,cmd,0,2,0);
		
		byte[] regToRead = new byte[2];
		//Reading syncrhonously
		i2c.transfer(regToRead, 0, 0, 2);
		
		int read = Util.get16(regToRead, 0);
		Logger.appendString(csr.s2b("I2C syncrhonous read: "));
		Logger.appendHexInt(read);
		Logger.flush(Mote.INFO);
		
		//Reading asyncrhonously
		i2c.setReadHandler(new DevCallback(null) {
			
			@Override
			public int invoke(int flags, byte[] data, int len, int info, long time) {
				return I2CExample.onReadDone(flags, data, len, info, time);
			}
		});
		i2c.read(Device.ASAP, 2, 0);
	}

	private static int onReadDone(int flags, byte[] data, int len, int info,
			long time) {
		byte[] regToRead = new byte[2];
		int read = Util.get16(regToRead, 0);
		
		Logger.appendString(csr.s2b("I2C asyncrhonous read: "));
		Logger.appendHexInt(read);
		Logger.flush(Mote.INFO);
		
		return 0;
	}
}

Output

category: log
evname: MAPP
id: 7d
time: 11:21:10.830'000
severity: 5
module: MAPP
logmsg: MAPP: I2C syncrhonous read: 0035

category: log
evname: MAPP
id: 7d
time: 11:21:10.830'000
severity: 5
module: MAPP
logmsg: MAPP: I2C asyncrhonous read: 0035

Quick Publish: