[Tinyos-help] What's wrong with this code for I2C communication
Sagar Bijwe
sag.bijwe at gmail.com
Thu Jun 12 04:14:26 PDT 2008
Hi,
I have written following code for the master
module TestI2CM {
provides {
interface StdControl;
}
uses {
interface Timer2<TMilli> as Timer;
interface Timer2<TMilli> as TimerConflict;
interface LocalTime<T32khz>;
interface Leds;
interface SPSend;
interface Debug;
interface ResourceCmd;
interface MSP430I2CPacket ;
interface StdControl as I2CControl;
interface MSP430I2C ;
interface MSP430I2CEvents;
}
}
implementation {
void dbg_msg(char *str, ...) {
#include<stdio.h>
#include<stdarg.h>
#include<string.h>
va_list ap;
char buf1[TOSH_DATA_LENGTH - 4];
va_start(ap, str);
sprintf(buf1,"%d:",TOS_LOCAL_ADDRESS);
vsprintf(buf1+2,str,ap);
va_end(ap);
call Debug.print(buf1);
}
char str[100];
uint32_t counter=0;
sp_message_t m_spmsg;
TOS_Msg m_tosmsg;
uint8_t m_size;
uint8_t k1=111;
uint8_t flag=1;
uint8_t *send_data_ptr;
uint8_t *receive_data_ptr;
uint8_t send_data;
uint8_t receive_data;
uint16_t I2C_ADDRESS;
uint8_t length;
event void SPSend.sendDone(sp_message_t* spmsg, sp_message_flags_t flags,
sp_error_t error) {
}
event void MSP430I2CPacket.readPacketDone(uint16_t _addr, uint8_t
_length, uint8_t* _data, result_t _success) { }
event void MSP430I2CPacket.writePacketDone(uint16_t _addr, uint8_t
_length, uint8_t* _data, result_t _success) {
call Leds.redToggle();
call ResourceCmd.release();
call ResourceCmd.request( RESOURCE_NONE );
}
event void ResourceCmd.granted(uint8_t rh) {
call MSP430I2CPacket.writePacket(rh,I2C_ADDRESS, length, send_data_ptr);
}
task void send_cont()
{
call ResourceCmd.request( RESOURCE_NONE );
}
event void Timer.fired() {
//call ResourceCmd.request( RESOURCE_NONE );
//call Timer.startPeriodic( 1000 );
}
event void TimerConflict.fired() {
// sendMessage();
}
command result_t StdControl.init() {
call I2CControl.init();
call MSP430I2C.enable();
call MSP430I2C.setModeMaster();
call MSP430I2C.enable();
call MSP430I2C.setModeMaster();
I2C_ADDRESS = 0x004d; // address of the
length =1;
send_data=7;
receive_data=0;
send_data_ptr= &send_data;
receive_data_ptr=&receive_data;
return SUCCESS; }
command result_t StdControl.start() {
dbg_msg("Booted");
post send_cont();
return SUCCESS;
}
command result_t StdControl.stop() {
call I2CControl.stop();
dbg_msg("Stopped");
call Leds.redOff();
call Leds.yellowOff();
call Leds.greenOff();
return SUCCESS; }
async event void MSP430I2CEvents.arbitrationLost() {}
async event void MSP430I2CEvents.noAck(){}
async event void MSP430I2CEvents.ownAddr(){}
async event void MSP430I2CEvents.readyRegAccess(){}
async event void MSP430I2CEvents.readyRxData(){}
async event void MSP430I2CEvents.readyTxData(){}
async event void MSP430I2CEvents.generalCall(){}
async event void MSP430I2CEvents.startRecv(){}
}
Configuration file looks like this
configuration TestI2C {
}
implementation {
components SPC,DebugM,GenericComm as Comm,
LedsC
, Counter32khzC
, new TimerMilliC()
, new TimerMilliC() as TimerConflictC
, Main
, TestI2CM
, MSP430I2CM
,new I2CResourceC() as I2CResource
,HPLUSART0M
;
Main.StdControl -> TestI2CM;
TestI2CM.I2CControl-> MSP430I2CM;
TestI2CM.MSP430I2C-> MSP430I2CM;
TestI2CM.MSP430I2CEvents-> MSP430I2CM;
MSP430I2CM.USARTControl->HPLUSART0M ;
MSP430I2CM.HPLI2CInterrupt->HPLUSART0M ;
TestI2CM.MSP430I2CPacket-> MSP430I2CM;
TestI2CM.ResourceCmd->I2CResource;
TestI2CM.Timer -> TimerMilliC;
TestI2CM.TimerConflict -> TimerConflictC;
TestI2CM.Leds -> LedsC;
TestI2CM.Debug-> DebugM.Debug;
TestI2CM.LocalTime -> Counter32khzC;
DebugM.SendMsg -> Comm.SendMsg[8];
TestI2CM.SPSend -> SPC.SPSend[1];
}
I am not getting the desired ouput. The master fails after sending 3 to 4
times. I need to connect another mote at other end that too a master. How
can arbitration be held over the I2C . How can i modify the code to work
both as master & slave??
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://www.millennium.berkeley.edu/pipermail/tinyos-help/attachments/20080612/adf4d154/attachment-0001.htm
More information about the Tinyos-help
mailing list