#ifndef __MULLEI2C_H__
#define __MULLEI2C_H__
-#define UQ_MULLE_SOFTI2C_BATTERY_RTC "SoftI2CMasterC.Battery_RTC"
+#define UQ_MULLE_SOFTWAREI2C_2 "UQ_MULLE_SOFTWAREI2C_2"
#endif // __MULLEI2C_H__
--- /dev/null
+/*
+ * Copyright (c) 2009 Communication Group and Eislab at
+ * Lulea University of Technology
+ *
+ * Contact: Laurynas Riliskis, LTU
+ * Mail: laurynas.riliskis@ltu.se
+ * All rights reserved.
+ *
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ * - Neither the name of Communication Group at Lulea University of Technology
+ * nor the names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD
+ * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 2006 Stanford University.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ * - Neither the name of the Stanford University nor the names of
+ * its contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD
+ * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @author Philip Levis
+ */
+
+/**
+ * The configuration that takes a I2C bus with 7-bit addressing
+ * and turns it into a shared abstraction.
+ *
+ * @author Henrik Makitaavola <henrik.makitaavola@gmail.com>
+ */
+
+#include "MulleI2C.h"
+#include "I2C.h"
+generic configuration SharedI2CPacketC(char resourceName[])
+{
+ provides interface Resource[uint8_t client];
+ provides interface I2CPacket<TI2CBasicAddr>[uint8_t client];
+ uses interface I2CPacket<TI2CBasicAddr> as SubPacket;
+}
+implementation
+{
+ components new FcfsArbiterC(resourceName) as Arbiter;
+ components new SharedI2CPacketP() as I2C;
+
+ Resource = I2C.Resource;
+ I2CPacket = I2C.I2CPacket;
+
+ I2C.SubResource -> Arbiter;
+ I2C.SubPacket = SubPacket;
+}
+
--- /dev/null
+/*
+ * Copyright (c) 2009 Communication Group and Eislab at
+ * Lulea University of Technology
+ *
+ * Contact: Laurynas Riliskis, LTU
+ * Mail: laurynas.riliskis@ltu.se
+ * All rights reserved.
+ *
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ * - Neither the name of Communication Group at Lulea University of Technology
+ * nor the names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD
+ * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 2006 Stanford University.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ * - Neither the name of the Stanford University nor the names of
+ * its contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD
+ * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @author Philip Levis
+ */
+
+/**
+ * The module implements the logic when creating a I2C bus with 7-bit addressing
+ * into a shared abstraction.
+ *
+ * @author Henrik Makitaavola <henrik.makitaavola@gmail.com>
+ */
+#include "I2C.h"
+generic module SharedI2CPacketP()
+{
+ provides interface Resource[uint8_t client];
+ provides interface I2CPacket<TI2CBasicAddr>[uint8_t client];
+ uses interface Resource as SubResource[uint8_t];
+ uses interface I2CPacket<TI2CBasicAddr> as SubPacket;
+}
+implementation
+{
+ enum
+ {
+ NO_CLIENT = 0xff
+ };
+
+ uint8_t currentClient = NO_CLIENT;
+
+ async command error_t Resource.request[uint8_t id]()
+ {
+ return call SubResource.request[id]();
+ }
+
+ async command error_t Resource.immediateRequest[uint8_t id]()
+ {
+ error_t rval = call SubResource.immediateRequest[id]();
+ if (rval == SUCCESS)
+ {
+ atomic currentClient = id;
+ }
+ return rval;
+ }
+
+ event void SubResource.granted[uint8_t id]()
+ {
+ atomic currentClient = id;
+ signal Resource.granted[id]();
+ }
+
+ async command error_t Resource.release[uint8_t id]()
+ {
+ return call SubResource.release[id]();
+ }
+
+ async command bool Resource.isOwner[uint8_t id]()
+ {
+ return call SubResource.isOwner[id]();
+ }
+
+ async command error_t I2CPacket.write[uint8_t id](i2c_flags_t flags,
+ uint16_t addr,
+ uint8_t len,
+ uint8_t* data)
+ {
+ atomic
+ {
+ if (currentClient != id)
+ {
+ return FAIL;
+ }
+ }
+ return call SubPacket.write(flags, addr, len, data);
+ }
+
+ async command error_t I2CPacket.read[uint8_t id](i2c_flags_t flags,
+ uint16_t addr,
+ uint8_t len,
+ uint8_t* data)
+ {
+ atomic
+ {
+ if (currentClient != id)
+ {
+ return FAIL;
+ }
+ }
+ return call SubPacket.read(flags, addr, len, data);
+ }
+
+ default event void Resource.granted[uint8_t id]() {}
+
+ async event void SubPacket.readDone(
+ error_t error, uint16_t addr, uint8_t length, uint8_t* data)
+ {
+ signal I2CPacket.readDone[currentClient](error, addr, length, data);
+ }
+
+ async event void SubPacket.writeDone(
+ error_t error, uint16_t addr, uint8_t length, uint8_t* data)
+ {
+ signal I2CPacket.writeDone[currentClient]( error, addr, length, data);
+ }
+
+ default async event void I2CPacket.readDone[uint8_t id](
+ error_t error, uint16_t addr, uint8_t length, uint8_t* data) { }
+
+ default async event void I2CPacket.writeDone[uint8_t id](
+ error_t error, uint16_t addr, uint8_t length, uint8_t* data) { }
+
+}
+++ /dev/null
-/*
- * Copyright (c) 2009 Communication Group and Eislab at
- * Lulea University of Technology
- *
- * Contact: Laurynas Riliskis, LTU
- * Mail: laurynas.riliskis@ltu.se
- * All rights reserved.
- *
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the
- * distribution.
- * - Neither the name of Communication Group at Lulea University of Technology
- * nor the names of its contributors may be used to endorse or promote
- * products derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD
- * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- * OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/**
- * The basic client abstraction of the software I2C on Mulle used
- * by the RTC and battery monitor.
- * The device drivers should instantiate one of these to ensure
- * exclusive access to the I2C bus.
- *
- * @author Henrik Makitaavola <henrik.makitaavola@gmail.com>
- */
-
-#include "MulleI2C.h"
-#include "I2C.h"
-generic configuration SoftI2CBatteryMonitorRTCC()
-{
- provides interface Resource;
- provides interface I2CPacket<TI2CBasicAddr>;
-}
-implementation
-{
- enum
- {
- CLIENT_ID = unique(UQ_MULLE_SOFTI2C_BATTERY_RTC),
- };
-
- components SoftI2CBatteryMonitorRTCP as I2C;
- Resource = I2C.Resource[CLIENT_ID];
- I2CPacket = I2C.I2CPacket[CLIENT_ID];
-}
+++ /dev/null
-/*
- * Copyright (c) 2009 Communication Group and Eislab at
- * Lulea University of Technology
- *
- * Contact: Laurynas Riliskis, LTU
- * Mail: laurynas.riliskis@ltu.se
- * All rights reserved.
- *
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the
- * distribution.
- * - Neither the name of Communication Group at Lulea University of Technology
- * nor the names of its contributors may be used to endorse or promote
- * products derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD
- * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- * OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/**
- * The configuration that takes the underlying software I2C driver
- * on Mulle and turns it into a shared abstraction for the RTC and battery
- * monitor.
- *
- * @author Henrik Makitaavola <henrik.makitaavola@gmail.com>
- */
-
-#include "MulleI2C.h"
-#include "I2C.h"
-configuration SoftI2CBatteryMonitorRTCP
-{
- provides interface Resource[uint8_t client];
- provides interface I2CPacket<TI2CBasicAddr>[uint8_t client];
-}
-implementation
-{
- components new SoftI2CMasterP(UQ_MULLE_SOFTI2C_BATTERY_RTC) as I2C,
- new SoftI2CBusP(),
- HplM16c62pGeneralIOC as IOs;
-
- // Init the software I2C bus
- SoftI2CBusP.I2CClk -> IOs.PortP71;
- SoftI2CBusP.I2CData -> IOs.PortP70;
- SoftI2CBusP.I2CCtrl -> IOs.PortP75;
-
- I2C.SoftI2CBus -> SoftI2CBusP;
-
- Resource = I2C.Resource;
- I2CPacket = I2C.I2CPacket;
-}
-
+++ /dev/null
-/*
- * Copyright (c) 2009 Communication Group and Eislab at
- * Lulea University of Technology
- *
- * Contact: Laurynas Riliskis, LTU
- * Mail: laurynas.riliskis@ltu.se
- * All rights reserved.
- *
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the
- * distribution.
- * - Neither the name of Communication Group at Lulea University of Technology
- * nor the names of its contributors may be used to endorse or promote
- * products derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD
- * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- * OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/**
- * Interface for a software I2C bus.
- *
- * @author Henrik Makitaavola <henrik.makitaavola@gmail.com>
- */
-interface SoftI2CBus
-{
- /*
- * Initializes bus default state.
- */
- async command void init();
-
- /*
- * Turn the bus off.
- */
- async command void off();
-
- /*
- * Generates a start condition on the bus.
- */
- async command void start();
-
- /*
- * Generates a stop condition.
- */
- async command void stop();
-
- /*
- * Restarts a I2C bus transaction.
- */
- async command void restart();
-
- /*
- * Reads a byte from the I2C bus.
- *
- * @param ack If true ack the read byte else nack.
- * @return A byte from the bus.
- */
- async command uint8_t readByte(bool ack);
-
- /*
- * Writes a byte on th I2C bus.
- * Send the data( or address) C and wait for acknowledge after finishing
- * sending it. Nonacknowledge sets ACK=0 and normal sending sets ACK=1.
- *
- * @param byte the byte to write.
- */
- async command void writeByte(uint8_t byte);
-
- /*
- * Master sends the ACK (LowLevel), working as a master-receiver.
- */
- async command void masterAck();
-
- /*
- * Master sends the NACK (HighLevel), working as a master-receiver.
- */
- async command void masterNack();
-}
+++ /dev/null
-/*
- * Copyright (c) 2009 Communication Group and Eislab at
- * Lulea University of Technology
- *
- * Contact: Laurynas Riliskis, LTU
- * Mail: laurynas.riliskis@ltu.se
- * All rights reserved.
- *
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the
- * distribution.
- * - Neither the name of Communication Group at Lulea University of Technology
- * nor the names of its contributors may be used to endorse or promote
- * products derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD
- * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- * OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/**
- * Mulle specific implementation of a software I2C bus.
- *
- * @author Gong Liang
- * @author Henrik Makitaavola <henrik.makitaavola@gmail.com>
- */
-generic module SoftI2CBusP()
-{
- provides interface SoftI2CBus as I2C;
-
- uses interface GeneralIO as I2CClk;
- uses interface GeneralIO as I2CData;
- uses interface GeneralIO as I2CCtrl;
-}
-implementation
-{
- /***************************************************************************************/
- // Name:idle(), transition(),halfTransition()
- // I2C-bus can be transferred at rates of up to 100 kbit/s in the Standard-mode
- // 10MHz main clock frequency
- // The Baud rate of IIC bus communication can be adjusted by altering the loop times.
- /***************************************************************************************/
- // TODO(henrik) This must be altered to support different main clock speeds.
- void idle(void)
- {
- volatile long int i;
- for(i=0;i<1000;i++)
- {
- asm("nop");
- }
- }
-
- void transition(void)
- {
- volatile int i;
- for(i=0;i<100;i++)
- {
- asm("nop");
- }
- }
-
- void halfTransition(void)
- {
- volatile int i;
- for(i=0;i<50;i++)
- {
- asm("nop");
- }
- }
-
-
- async command void I2C.init()
- {
- call I2CData.makeOutput();
- call I2CClk.makeOutput();
- call I2CCtrl.makeOutput();
- call I2CCtrl.set();
- call I2CData.set(); // drive bus high (default)
- call I2CClk.set(); // drive bus high (default)
- }
-
- async command void I2C.off()
- {
- // TODO(henrik): Exactly what should be set if I2C bus should be turned off?
- call I2CData.makeOutput();
- call I2CClk.makeOutput();
- call I2CCtrl.makeOutput();
- call I2CCtrl.clr();
- call I2CClk.clr();
- call I2CData.clr();
- }
-
- async command void I2C.start()
- {
- atomic
- {
- call I2CData.makeOutput();
- call I2CClk.clr();
- call I2CData.set();
- idle();
- call I2CClk.set();
- idle();
- call I2CData.clr();
- idle();
- call I2CClk.clr();
- idle();
- }
- }
-
- async command void I2C.stop()
- {
- atomic
- {
- call I2CData.makeOutput();
-
- call I2CData.set();
- call I2CClk.clr();
- idle();
- call I2CData.clr();
- idle();
- call I2CClk.set();
- idle();
- call I2CData.set();
- idle();
- }
- }
-
- async command void I2C.restart()
- {
- atomic
- {
- call I2CClk.clr();
- call I2CData.set();
- call I2CClk.set();
- idle();
- call I2CData.clr();
- idle();
- call I2CClk.clr();
- }
- }
-
- async command uint8_t I2C.readByte(bool ack)
- {
- uint8_t retChar;
- uint8_t bitCnt;
-
- atomic
- {
- call I2CData.makeInput();
-
- retChar = 0;
-
- for (bitCnt=0; bitCnt<8; ++bitCnt)
- {
- asm("nop");
- call I2CClk.clr();
- transition();
-
- call I2CClk.set(); // Validate the RevData on the SDA2 line
- halfTransition();
- retChar<<=1; // Push each received bit toward MSB
- if(call I2CData.get() == 1) retChar+=1; // Read the bit on SDA2 line
- halfTransition();
- }
-
- call I2CClk.clr();
- if (ack)
- {
- call I2C.masterAck();
- }
- else
- {
- call I2C.masterNack();
- }
- }
- return retChar;
- }
-
- async command void I2C.writeByte(uint8_t byte)
- {
- uint8_t bitCnt;
-
- atomic
- {
- call I2CData.makeOutput();
-
- for (bitCnt=0; bitCnt<8; ++bitCnt)
- {
- if ((byte<<bitCnt) & 0x80)
- {
- call I2CData.set(); // MSB sending logic
- }
- else
- {
- call I2CData.clr();
- }
- transition(); // Use this function to adjust the baut rate of I2C bus
-
- call I2CClk.set(); //Set SCL2 high to inform slave of receiving sth.
- transition(); // Greater than 4¦Ìs
- call I2CClk.clr();
- }
-
-
- call I2CData.set(); // Release SDA2 for ACK-waiting after sending 8 bits
- transition();
-
- call I2CClk.set();
- transition();
- call I2CClk.clr();
-
- idle();
- }
- }
-
- async command void I2C.masterAck()
- {
- atomic
- {
- call I2CData.makeOutput();
- call I2CClk.clr();
- call I2CData.clr(); // Low for ack
- transition();
- call I2CClk.set();
- transition();
- call I2CClk.clr();
-
- call I2CData.makeInput();
- }
- }
-
- async command void I2C.masterNack()
- {
- atomic
- {
- call I2CData.makeOutput();
- call I2CClk.clr();
-
- call I2CData.set(); // High for nack
- transition();
- call I2CClk.set();
- transition();
- call I2CClk.clr();
-
- call I2CData.makeInput();
- }
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2009 Communication Group and Eislab at
- * Lulea University of Technology
- *
- * Contact: Laurynas Riliskis, LTU
- * Mail: laurynas.riliskis@ltu.se
- * All rights reserved.
- *
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the
- * distribution.
- * - Neither the name of Communication Group at Lulea University of Technology
- * nor the names of its contributors may be used to endorse or promote
- * products derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD
- * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- * OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/*
- * Copyright (c) 2006 Stanford University.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the
- * distribution.
- * - Neither the name of the Stanford University nor the names of
- * its contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD
- * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- * OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * @author Philip Levis
- */
-
-/**
- * The configuration that takes the underlying software I2C on Mulle
- * and turns it into a shared abstraction.
- *
- * @author Henrik Makitaavola <henrik.makitaavola@gmail.com>
- */
-#include "I2C.h"
-generic module SoftI2CMasterImplP()
-{
- provides interface Resource[uint8_t client];
- provides interface I2CPacket<TI2CBasicAddr>[uint8_t client];
- uses interface Resource as SubResource[uint8_t];
- uses interface I2CPacket<TI2CBasicAddr> as SubPacket;
-}
-implementation
-{
- enum
- {
- NO_CLIENT = 0xff
- };
-
- uint8_t currentClient = NO_CLIENT;
-
- async command error_t Resource.request[uint8_t id]()
- {
- return call SubResource.request[id]();
- }
-
- async command error_t Resource.immediateRequest[uint8_t id]()
- {
- error_t rval = call SubResource.immediateRequest[id]();
- if (rval == SUCCESS)
- {
- atomic currentClient = id;
- }
- return rval;
- }
-
- event void SubResource.granted[uint8_t id]()
- {
- atomic currentClient = id;
- signal Resource.granted[id]();
- }
-
- async command error_t Resource.release[uint8_t id]()
- {
- return call SubResource.release[id]();
- }
-
- async command bool Resource.isOwner[uint8_t id]()
- {
- return call SubResource.isOwner[id]();
- }
-
- async command error_t I2CPacket.write[uint8_t id](i2c_flags_t flags,
- uint16_t addr,
- uint8_t len,
- uint8_t* data)
- {
- atomic
- {
- if (currentClient != id)
- {
- return FAIL;
- }
- }
- return call SubPacket.write(flags, addr, len, data);
- }
-
- async command error_t I2CPacket.read[uint8_t id](i2c_flags_t flags,
- uint16_t addr,
- uint8_t len,
- uint8_t* data)
- {
- atomic
- {
- if (currentClient != id)
- {
- return FAIL;
- }
- }
- return call SubPacket.read(flags, addr, len, data);
- }
-
- default event void Resource.granted[uint8_t id]() {}
-
- async event void SubPacket.readDone(
- error_t error, uint16_t addr, uint8_t length, uint8_t* data)
- {
- signal I2CPacket.readDone[currentClient](error, addr, length, data);
- }
-
- async event void SubPacket.writeDone(
- error_t error, uint16_t addr, uint8_t length, uint8_t* data)
- {
- signal I2CPacket.writeDone[currentClient]( error, addr, length, data);
- }
-
- default async event void I2CPacket.readDone[uint8_t id](
- error_t error, uint16_t addr, uint8_t length, uint8_t* data) { }
-
- default async event void I2CPacket.writeDone[uint8_t id](
- error_t error, uint16_t addr, uint8_t length, uint8_t* data) { }
-
-}
+++ /dev/null
-/*
- * Copyright (c) 2009 Communication Group and Eislab at
- * Lulea University of Technology
- *
- * Contact: Laurynas Riliskis, LTU
- * Mail: laurynas.riliskis@ltu.se
- * All rights reserved.
- *
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the
- * distribution.
- * - Neither the name of Communication Group at Lulea University of Technology
- * nor the names of its contributors may be used to endorse or promote
- * products derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD
- * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- * OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/*
- * Copyright (c) 2006 Stanford University.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the
- * distribution.
- * - Neither the name of the Stanford University nor the names of
- * its contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD
- * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- * OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * @author Philip Levis
- */
-
-/**
- * The configuration that takes the underlying software I2C driver
- * Mulle and turns it into a shared abstraction.
- *
- * @author Henrik Makitaavola <henrik.makitaavola@gmail.com>
- */
-
-#include "MulleI2C.h"
-#include "I2C.h"
-generic configuration SoftI2CMasterP(char resourceName[])
-{
- provides interface Resource[uint8_t client];
- provides interface I2CPacket<TI2CBasicAddr>[uint8_t client];
- uses interface SoftI2CBus;
-}
-implementation
-{
- components new FcfsArbiterC(resourceName) as Arbiter;
- components new AsyncPowerManagerP() as Power;
- components new SoftI2CMasterImplP() as I2C;
- components new SoftI2CMasterPacketP() as Master;
-
- Resource = I2C;
- I2CPacket = I2C;
-
- I2C.SubResource -> Arbiter;
- I2C.SubPacket -> Master;
-
- Power.AsyncStdControl -> Master;
- Power.ResourceDefaultOwner -> Arbiter;
-
- Master.I2C = SoftI2CBus;
-}
-
+++ /dev/null
-/*
- * Copyright (c) 2009 Communication Group and Eislab at
- * Lulea University of Technology
- *
- * Contact: Laurynas Riliskis, LTU
- * Mail: laurynas.riliskis@ltu.se
- * All rights reserved.
- *
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the
- * distribution.
- * - Neither the name of Communication Group at Lulea University of Technology
- * nor the names of its contributors may be used to endorse or promote
- * products derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD
- * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- * OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/*
- * Copyright (c) 2006 Stanford University.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the
- * distribution.
- * - Neither the name of the Stanford University nor the names of
- * its contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD
- * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- * OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * @author Philip Levis
- */
-
-/**
- * This driver implements an software I2C Master controller.
- *
- * @author Henrik Makitaavola <henrik.makitaavola@gmail.com>
- */
-#include "I2C.h"
-generic module SoftI2CMasterPacketP()
-{
- provides interface AsyncStdControl;
- provides interface I2CPacket<TI2CBasicAddr>;
-
- uses interface SoftI2CBus as I2C;
-}
-implementation
-{
- enum
- {
- I2C_OFF = 0,
- I2C_IDLE = 1,
- I2C_BUSY = 2,
- } soft_i2c_state_t;
-
- uint8_t state = I2C_OFF;
- uint16_t _addr;
- uint8_t _len;
- uint8_t* _data;
- error_t _error;
-
- task void writeDoneTask()
- {
- atomic
- {
- state = I2C_IDLE;
- signal I2CPacket.writeDone( _error, _addr, _len, _data);
- }
- }
-
- task void readDoneTask()
- {
- atomic
- {
- state = I2C_IDLE;
- signal I2CPacket.readDone( _error, _addr, _len, _data);
- }
- }
-
- async command error_t AsyncStdControl.start()
- {
- atomic
- {
- if (state == I2C_OFF)
- {
- call I2C.init();
- state = I2C_IDLE;
- return SUCCESS;
- }
- else
- {
- return FAIL;
- }
- }
- }
-
- async command error_t AsyncStdControl.stop()
- {
- atomic
- {
- if (state == I2C_IDLE)
- {
- call I2C.off();
- state = I2C_OFF;
- return SUCCESS;
- }
- else
- {
- return FAIL;
- }
- }
- }
-
- async command error_t I2CPacket.read(i2c_flags_t flags,
- uint16_t addr,
- uint8_t len,
- uint8_t* data)
- {
- int i;
- atomic
- {
- if (state == I2C_IDLE)
- {
- state = I2C_BUSY;
- }
- else if (state == I2C_OFF)
- {
-
- return EOFF;
- }
- else
- {
-
- return EBUSY;
- }
- }
- atomic
- {
- if (len < 1) // A 0-length packet with no start and no stop....
- {
- state = I2C_IDLE;
- return FAIL;
- }
-
- if (flags & I2C_START)
- {
- call I2C.start();
- call I2C.writeByte(addr+1);
- }
-
- // Read the information.
- for (i = 0; i < len-1; ++i)
- {
- data[i] = call I2C.readByte(true);
- }
- data[len-1] = call I2C.readByte(I2C_ACK_END);
- if (flags & I2C_STOP)
- {
- call I2C.stop();
- }
-
- _error = SUCCESS;
- _addr = addr;
- _len = len;
- _data = data;
- }
- post readDoneTask();
-
- return SUCCESS;
- }
-
- async command error_t I2CPacket.write(i2c_flags_t flags,
- uint16_t addr,
- uint8_t len,
- uint8_t* data)
- {
- int i;
-
- atomic
- {
- if (state == I2C_IDLE)
- {
- state = I2C_BUSY;
- }
- else if (state == I2C_OFF)
- {
- return EOFF;
- }
- else
- {
-
- return EBUSY;
- }
- }
- atomic
- {
- if (len < 1) // A 0-length packet with no start and no stop....
- {
- state = I2C_IDLE;
- return FAIL;
- }
- if (flags & I2C_START)
- {
- call I2C.start();
- call I2C.writeByte(addr);
- }
-
- // Send information.
- for (i = 0; i < len; ++i)
- {
- call I2C.writeByte(data[i]);
- }
-
- if (flags & I2C_STOP)
- {
- call I2C.stop();
- }
-
- _data = data;
- _addr = addr;
- _len = len;
- _error = SUCCESS;
- }
- post writeDoneTask();
- return SUCCESS;
- }
-
-
-
-}
--- /dev/null
+/*
+ * Copyright (c) 2009 Communication Group and Eislab at
+ * Lulea University of Technology
+ *
+ * Contact: Laurynas Riliskis, LTU
+ * Mail: laurynas.riliskis@ltu.se
+ * All rights reserved.
+ *
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ * - Neither the name of Communication Group at Lulea University of Technology
+ * nor the names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD
+ * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * The basic client abstraction of the I2C nr 2 on Mulle used
+ * by the RTC and battery monitor and the possibilty to be connected
+ * from the external connections.
+ * The device drivers should instantiate this configuration to ensure
+ * exclusive access to the I2C bus.
+ *
+ * @author Henrik Makitaavola <henrik.makitaavola@gmail.com>
+ */
+
+#include "MulleI2C.h"
+#include "I2C.h"
+generic configuration SoftwareI2C2C()
+{
+ provides interface Resource;
+ provides interface I2CPacket<TI2CBasicAddr>;
+}
+implementation
+{
+ enum
+ {
+ CLIENT_ID = unique(UQ_MULLE_SOFTWAREI2C_2),
+ };
+
+ components SoftwareI2C2P as I2C;
+ Resource = I2C.Resource[CLIENT_ID];
+ I2CPacket = I2C.I2CPacket[CLIENT_ID];
+}
--- /dev/null
+/*
+ * Copyright (c) 2009 Communication Group and Eislab at
+ * Lulea University of Technology
+ *
+ * Contact: Laurynas Riliskis, LTU
+ * Mail: laurynas.riliskis@ltu.se
+ * All rights reserved.
+ *
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ * - Neither the name of Communication Group at Lulea University of Technology
+ * nor the names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD
+ * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * Initilizes the pullups on the I2C bus marked as nr 2 on Mulle.
+ *
+ * @author Henrik Makitaavola <henrik.makitaavola@gmail.com>
+ */
+ module SoftwareI2C2InitP
+ {
+ provides interface Init;
+ uses interface GeneralIO as Pullup;
+ }
+ implementation
+ {
+ command error_t Init.init()
+ {
+ call Pullup.makeOutput();
+ call Pullup.set();
+ }
+ }
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2009 Communication Group and Eislab at
+ * Lulea University of Technology
+ *
+ * Contact: Laurynas Riliskis, LTU
+ * Mail: laurynas.riliskis@ltu.se
+ * All rights reserved.
+ *
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ * - Neither the name of Communication Group at Lulea University of Technology
+ * nor the names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD
+ * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * The wiring of the I2C bus nr 2 on Mulle and creation of it into a
+ * shared abstraction.
+ *
+ * @author Henrik Makitaavola <henrik.makitaavola@gmail.com>
+ */
+
+#include "MulleI2C.h"
+#include "I2C.h"
+configuration SoftwareI2C2P
+{
+ provides interface Resource[uint8_t client];
+ provides interface I2CPacket<TI2CBasicAddr>[uint8_t client];
+}
+implementation
+{
+ components new SoftwareI2CPacketC(1000),
+ HplM16c62pGeneralIOC as IOs,
+ BusyWaitMicroC,
+ new SharedI2CPacketC(UQ_MULLE_SOFTWAREI2C_2),
+ SoftwareI2C2InitP,
+ PlatformP;
+
+ // Wire the software I2C bus
+ SoftwareI2CPacketC.SCL -> IOs.PortP71;
+ SoftwareI2CPacketC.SDA -> IOs.PortP70;
+ SoftwareI2CPacketC.BusyWait -> BusyWaitMicroC;
+
+ Resource = SharedI2CPacketC;
+ I2CPacket = SharedI2CPacketC.I2CPacket;
+ SharedI2CPacketC -> SoftwareI2CPacketC.I2CPacket;
+
+ // Init the bus
+ SoftwareI2C2InitP.Pullup -> IOs.PortP75;
+ PlatformP.SubInit -> SoftwareI2C2InitP;
+}
+
--- /dev/null
+/*
+ * Copyright (c) 2009 Communication Group and Eislab at
+ * Lulea University of Technology
+ *
+ * Contact: Laurynas Riliskis, LTU
+ * Mail: laurynas.riliskis@ltu.se
+ * All rights reserved.
+ *
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ * - Neither the name of Communication Group at Lulea University of Technology
+ * nor the names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD
+ * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "I2C.h"
+
+/**
+ * This module implements a software I2CPacket with 7-bit addressing.
+ * The SDA and SCL pins must have pull-up resistors.
+ *
+ * This code was written with help from the I2C Wikipedia page:
+ * http://en.wikipedia.org/wiki/I%C2%B2C
+ *
+ * @param speed The number of micro seconds
+ * @author Henrik Makitaavola <henrik.makitaavola@gmail.com>
+ */
+generic module SoftwareI2CPacketC(int speed)
+{
+ provides interface I2CPacket<TI2CBasicAddr>;
+
+ uses interface GeneralIO as SDA;
+ uses interface GeneralIO as SCL;
+ uses interface BusyWait<TMicro, uint16_t>;
+}
+implementation
+{
+ enum
+ {
+ S_IDLE,
+ S_BUSY,
+ };
+
+ uint8_t m_state = S_IDLE;
+ uint16_t m_addr;
+ uint8_t m_length;
+ uint8_t* m_data;
+ error_t m_error;
+ bool m_read;
+
+ uint8_t READSDA()
+ {
+ call SDA.makeInput();
+ return call SDA.get();
+ }
+
+ uint8_t READSCL()
+ {
+ call SCL.makeInput();
+ return call SCL.get();
+ }
+
+ void CLRSCL()
+ {
+ call SCL.clr();
+ call SCL.makeOutput();
+ }
+
+ void CLRSDA()
+ {
+ call SDA.clr();
+ call SDA.makeOutput();
+ }
+
+ void i2cDelay(uint16_t u) {
+ call BusyWait.wait(u);
+ }
+
+ uint8_t i2cReadBit(void)
+ {
+ uint8_t bit;
+
+ /* lets the slave drive data */
+ READSDA();
+ i2cDelay(speed/2);
+ /* Clock stretching */
+ while (READSCL() == 0);
+ /* SCL is high, now data is valid */
+ bit = READSDA();
+ i2cDelay(speed/2);
+ CLRSCL();
+ return bit;
+ }
+
+ error_t i2cWriteBit(bool bit)
+ {
+ if (bit)
+ READSDA();
+ else
+ CLRSDA();
+ i2cDelay(speed/2);
+ /* Clock stretching */
+ while (READSCL() == 0);
+ /* SCL is high, now data is valid */
+ /* check that nobody is driving SDA */
+ if (bit && READSDA() == 0)
+ return FAIL;
+ i2cDelay(speed/2);
+ CLRSCL();
+ return SUCCESS;
+ }
+
+ error_t i2cStartCond(void)
+ {
+ if (READSDA() == 0)
+ return FAIL;
+ /* SCL is high, set SDA from 1 to 0 */
+ CLRSDA();
+ i2cDelay(speed/2);
+ CLRSCL();
+ return SUCCESS;
+ }
+
+ error_t i2cStopCond(void)
+ {
+ /* set SDA to 0 */
+ CLRSDA();
+ i2cDelay(speed/2);
+ /* Clock stretching */
+ while (READSCL() == 0);
+ /* SCL is high, set SDA from 0 to 1 */
+ if (READSDA() == 0)
+ return FAIL;
+ i2cDelay(speed/2);
+ return SUCCESS;
+ }
+
+ error_t i2cTx(uint8_t byte)
+ {
+ uint8_t bit;
+ uint8_t ack;
+ error_t error = SUCCESS;
+
+ for (bit = 0; bit < 8; bit++) {
+ error = ecombine(error, i2cWriteBit(byte & 0x80));
+ byte <<= 1;
+ }
+
+ // The ack bit is 0 for success
+ if (!i2cReadBit())
+ {
+ return ecombine(error, SUCCESS);
+ }
+ else
+ {
+ return FAIL;
+ }
+ }
+
+ uint8_t i2cRx (bool nack)
+ {
+ uint8_t byte = 0;
+ uint8_t bit;
+
+ for (bit = 0; bit < 8; bit++) {
+ byte <<= 1;
+ byte |= i2cReadBit();
+ }
+ i2cWriteBit(nack);
+ return byte;
+ }
+
+ task void signalTask()
+ {
+ uint16_t addr;
+ uint8_t length;
+ uint8_t* data;
+ error_t error;
+ bool read;
+ atomic
+ {
+ addr = m_addr;
+ length = m_length;
+ data = m_data;
+ error = m_error;
+ m_state = S_IDLE;
+ read = m_read;
+ }
+ if (read)
+ {
+ signal I2CPacket.readDone(error, addr, length, data);
+ }
+ else
+ {
+ signal I2CPacket.writeDone(error, addr, length, data);
+ }
+ }
+
+ async command error_t I2CPacket.read(i2c_flags_t flags, uint16_t addr, uint8_t length, uint8_t* data)
+ {
+ uint8_t i;
+ error_t error = SUCCESS;
+
+ // Both I2C_STOP and I2C_ACK_END flags are not allowed at the same time.
+ if ((flags & I2C_STOP) && (flags & I2C_ACK_END))
+ {
+ return EINVAL;
+ }
+
+ atomic
+ {
+ if (m_state == S_IDLE)
+ {
+ m_state = S_BUSY;
+ }
+ else
+ {
+ return EBUSY;
+ }
+ }
+ atomic
+ {
+ if (flags & I2C_START)
+ {
+ error = ecombine(error, i2cStartCond());
+ error = ecombine(error, i2cTx(addr+1));
+ }
+
+ // Only read data from the device if length is >0.
+ // TODO(henrik): Should a data length of 0 be a invalid input?
+ if (length > 0)
+ {
+ // Read the data from the device.
+ for (i = 0; i < length-1; ++i)
+ {
+ data[i] = i2cRx(false);
+ }
+ if (flags & I2C_ACK_END)
+ {
+ data[length-1] = i2cRx(false);
+ }
+ else
+ {
+ data[length-1] = i2cRx(true);
+ }
+ }
+ if (flags & I2C_STOP)
+ {
+ error = ecombine(error, i2cStopCond());
+ }
+
+ m_error = error;
+ m_addr = addr;
+ m_length = length;
+ m_data = data;
+ m_read = true;
+ }
+ post signalTask();
+
+ return SUCCESS;
+ }
+
+ async command error_t I2CPacket.write(i2c_flags_t flags, uint16_t addr, uint8_t length, uint8_t* data)
+ {
+ uint8_t i;
+ error_t error = SUCCESS;
+
+ atomic
+ {
+ if (m_state == S_IDLE)
+ {
+ m_state = S_BUSY;
+ }
+ else
+ {
+ return EBUSY;
+ }
+ }
+ atomic
+ {
+ if (flags & I2C_START)
+ {
+ error = ecombine(error, i2cStartCond());
+ }
+
+ i2cTx(addr);
+
+ // Send the data to the device.
+ for (i = 0; i < length; ++i)
+ {
+ error = ecombine(error, i2cTx(data[i]));
+ }
+
+ if (flags & I2C_STOP)
+ {
+ error = ecombine(error, i2cStopCond());
+ }
+
+ m_error = error;
+ m_addr = addr;
+ m_length = length;
+ m_data = data;
+ m_read = false;
+ }
+ post signalTask();
+ return SUCCESS;
+ }
+}
\ No newline at end of file