Society of Robots - Robot Forum

Software => Software => Topic started by: hassan.iftikhar on October 11, 2011, 01:08:07 AM

Title: I2C tx not responding (axon2 with CM02)
Post by: hassan.iftikhar on October 11, 2011, 01:08:07 AM
Hi all,

I am interfacing CM02 with axon02
CM02 and RF02 from devantech
http://www.robot-electronics.co.uk/htm/rf04tech.htm (http://www.robot-electronics.co.uk/htm/rf04tech.htm)
http://www.robot-electronics.co.uk/htm/cm02tech.htm (http://www.robot-electronics.co.uk/htm/cm02tech.htm)

here is my data flow
computer <--over usb--> RF02   ((rf wireless link))    CM02 <--over I2C--> AXON2
axon is in slave mode.

my problem is that axon2 rxhandler for i2c is responding but when master (rf02) i2c ask to send some ack or data axon2 dosent responds.

here is code for axon

#define BUF_SIZE 30
cBuffer txBuffer, rxBuffer;
unsigned char tx[BUF_SIZE], rx[BUF_SIZE], ary[BUF_SIZE];

void rxHandler(cBuffer* data)
{
   unsigned char dataByte;
   int i = 0;
   rprintfInit(USB_ACTIVATE);
   rprintf("rxHandler: ");
   while(bufferGet(&rxBuffer, &dataByte))
   {
      rprintf( "byte: %c", dataByte);      
      if(i >= BUF_SIZE) break;
   }
   bufferPut(&txBuffer, 'a');
   bufferPut(&txBuffer, 'c');
   bufferPut(&txBuffer, 'k');
   rprintf("\n");
}

void txHandler(cBuffer* data)
{
   rprintfInit(USB_ACTIVATE);
   rprintf("txHandler:\n");
}

// This routine is called once only and allows you to do any initialisation
// Dont use any 'clock' functions here - use 'delay' functions instead
void appInitHardware(void){
   // Set up the hardware UART to xmit the results
   uartInit(USB_UART, SERIAL_BAUD);
   // Set rprintf to go to output
   rprintfInit(USB_ACTIVATE);
   i2cSlaveInit(I2C_ADDRESS, FALSE, &rxBuffer, &txBuffer);
   bufferInit(&txBuffer, tx, sizeof(tx));
   i2cSlaveSetTransmitHandler(&txHandler);
   bufferInit(&rxBuffer, rx, sizeof(rx));
   i2cSlaveSetReceiveHandler(&rxHandler);
   led_off();
}

TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
      
   rprintf("\nAxon initiated.\n\n");
   rprintfInit( marqueeGetWriter(&marquee) );
   marqueeSetCharDelay(&marquee,600000);//set character delay time in us
   marqueeSetEndDelay(&marquee,0);//set as non-repeating

   return 0;
}

// This routine is called repeatedly - its your main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart){
   return 2000;
}



I have just written a simple program for windows that act like hyperterminal and I am pretty much sure that there is no problem at that side (but if someone asks I can post the code).

Either it is mis configuration of axon2 or I have not connected it properly.

I have straight a way attached I2C wires of CM02 with axon2 not sure on that.

Any help is really appreciated.

Thanks.
Title: Re: I2C tx not responding (axon2 with CM02)
Post by: Webbot on October 14, 2011, 08:38:46 AM
After a quick look....
You should only place data into the transmit buffer from within the 'txHandler' routine (since the buffer is zapped out before it is called).
In your 'rxHandler' you are placing 3 bytes into the txBuffer, which then get thrown away. Since your 'txHandler' puts nothing into the txBuffer then nothing gets sent back to the caller.

Remove these lines from 'rxHandler':
 
Code: [Select]
bufferPut(&txBuffer, 'a');
   bufferPut(&txBuffer, 'c');
   bufferPut(&txBuffer, 'k');

And put them into 'txHandler' like this:
Code: [Select]
  bufferPut(data, 'a');
   bufferPut(data, 'c');
   bufferPut(data, 'k');

Note how it uses the 'data' parameter rather than your 'txBuffer'. Although they will be the same then its just good practice - because if you change the name of the buffer that you pass to i2cSlaveInit then it means you don't have to remember to change it in your 'txHandler'. The same thing applies to your 'rxHandler' ie use the 'data' parameter rather than '&rxBuffer'


Title: Re: I2C tx not responding (axon2 with CM02)
Post by: hassan.iftikhar on October 18, 2011, 08:39:35 PM
thanks for the reply,

I have tried it with that scheme too but still it dosent get called,

actually when master sends data to axon rxhandler is called and it prints data through serial (for testing)
but when master asks for data it dosent responds neither master recv any data. Kindly have a deep look at the code (its really very short) and let me know it there is any setup error here.
See below


#include "sys/axon2.h"
#include "rprintf.h"
#include "hardware.h"
#include "i2cBus.h"

#define BUF_SIZE 30
cBuffer txBuffer, rxBuffer;
unsigned char tx[BUF_SIZE], rx[BUF_SIZE], ary[BUF_SIZE];

void rxHandler(cBuffer* data)
{
   unsigned char dataByte;
   rprintf("R\n");
   while(bufferGet(data, &dataByte))
   {
      rprintf( "%d\n",dataByte);
   }
}

void txHandler(cBuffer* data)
{
   bufferPut(data, 0x01);
   bufferPut(data, 0x02);
   bufferPut(data, 0x03);
   
   rprintf("T\n");
}

void appInitHardware(void){

   uartInit(USB_UART, SERIAL_BAUD);

   // Set rprintf to go to output
   rprintfInit(USB_ACTIVATE);

   i2cSlaveInit(I2C_ADDRESS, TRUE, &rxBuffer, &txBuffer);
   bufferInit(&txBuffer, tx, sizeof(tx));
   i2cSlaveSetTransmitHandler(&txHandler);
   bufferInit(&rxBuffer, rx, sizeof(rx));
   i2cSlaveSetReceiveHandler(&rxHandler);

   led_off();
}

TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
   
   rprintf("\nAxon initiated.\n\n");

   return 0;
}

// This routine is called repeatedly - its your main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart){
   
   return 2000;
}
Title: Re: I2C tx not responding (axon2 with CM02)
Post by: Webbot on October 19, 2011, 09:12:26 AM
Seems to work for me. Perhaps its the master that is doing something weird.
What does your code look like on the master?
Title: Re: I2C tx not responding (axon2 with CM02)
Post by: hassan.iftikhar on October 19, 2011, 11:48:17 AM

thanks for replying webbot!

actually master is CM02 from robot-electronics.com
http://www.robot-electronics.co.uk/htm/cm02tech.htm (http://www.robot-electronics.co.uk/htm/cm02tech.htm)

it only has instructions for the pc programming side
the workflow is like this

PC <--serial link via usb--> RF02   (((rf wireless link)))   CM02<--I2C link-->AXON2

I can only send instruction to rf02 rest is with cm02

it says(reference http://www.robot-electronics.co.uk/htm/cm02tech.htm (http://www.robot-electronics.co.uk/htm/cm02tech.htm))

Reading directly from any I2C device
This is similar to writing, except that you should add 1 to the device
address to make it an odd number. To read from an SRF08 at address 0xE0, you
would use 0xE1 as the address. (When the address goes out on the I2C bus,
its the 1 in the lowest bit position that indicates a read cycle is
happening). The maximum number of data bytes requested should not exceed 76
so as not to overflow the CM02's internal buffer. Here is an example of
reading the two byte bearing from the CMPS03 compass module:

I2C_CMD     CPMS03 I2C address + Read bit     CMPS03 bearing register     
Number of bytes to read
0x55     0xC1     0x02     0x02

The CM02 will perform the read operation on the I2C bus and send two bytes
back to the PC - high byte first. The PC should wait for both bytes to be
returned (timing out after 500mS) before proceeding with the next
transaction.

---------------------------------

Now here is my pc side code



        memset(command, 0, 50);
        getWrireDataComm(command);
        WriteFile(rfPort, &command, 10, &write, NULL);
        ReadFile(axonPort, &command, 200, &read, NULL);
//axon prints that it recieved data through serial link
        dumpbuf(command, "AXON says", read);
        Sleep(1000);

        memset(command, 0, 50);
        getReadDataComm(command);
        WriteFile(rfPort, &command, 4, &write, NULL);
        memset(command, 0, 50);
//only garbage is read like 0xff and 0x01
//it should reply with 01, 02, 03 as bufferPut(data, 0x01);bufferPut(data, 0x02);bufferPut(data, 0x03); in axon txhandler
        ReadFile(rfPort, &command, 10, &read, NULL);
        dumpbuf(command, "RF says", read);
        ReadFile(axonPort, &command, 200, &read, NULL);
//axon dosent print any thing as txhandler in axon was never called
//it should print "T" as rprintf("T\n");
        dumpbuf(command, "AXON says", read);


void getWrireDataComm(BYTE command[])
{
   command[0] = 0x55;
   command[1] = 0xC0;   // axon i2c address
   command[2] = 0x00;  // mode register
   command[3] = 0x06;   // data byte count
   command[4] = 'H';   // data byte count
   command[5] = 'A';   // data byte count
   command[6] = 'S';   // data byte count
   command[7] = 'S';   // data byte count
   command[8] = 'A';   // data byte count
   command[9] = 'N';   // data byte count
}

void getReadDataComm(BYTE command[])
{
   command[0] = 0x55;
   command[1] = 0xC1;   // axon i2c address + 1 bit for read command
   command[2] = 0x00;  // mode register
   command[3] = 0x03;   // data bytes to read
}


Again thanks for the help webbot!

Regards,

Title: Re: I2C tx not responding (axon2 with CM02)
Post by: Webbot on October 20, 2011, 07:48:30 AM
Your code looks ok (not that I know much about the CM02).

Have you got it working correctly when talking to another I2C device (like a sensor of some kind).

Alternatively you could try adding some debug into WebbotLib.....
Look in the file i2c/i2c_slave.c
You will see the interrupt handler...
Code: [Select]
// I2C (TWI) interrupt service routine
ISR(SIG_2WIRE_SERIAL)
{
// read status bits
uint8_t status = inb(TWSR) & TWSR_STATUS_MASK;
uint8_t byte;

switch(status)
{
        --- etc ---


You could dump out the status values by adding one line ie
Code: [Select]
// I2C (TWI) interrupt service routine
ISR(SIG_2WIRE_SERIAL)
{
// read status bits
uint8_t status = inb(TWSR) & TWSR_STATUS_MASK;
uint8_t byte;
        rprintf("i2c=0x%x\n"); // dump out value
switch(status)
{
        --- etc ---

You will also need to '#include "../rprintf.h" at the top of the file.

Then recompile WebbotLib by running 'make' in the WebbotLib folder. Then recompile your project. You should now see a log of the status calls to wherever you have set rprintf to go to.
Title: Re: I2C tx not responding (axon2 with CM02)
Post by: hassan.iftikhar on October 23, 2011, 10:50:49 PM
I have changed the code and redirected the rprintf to usart and recompiled but when some data is send axon goes to halt and when master asks for data axon goes to restarting.

after removing the dump I notices some thing intresting
the format of CM02 command for writing on I2C devise is

source: http://www.robot-electronics.co.uk/htm/cm02tech.htm (http://www.robot-electronics.co.uk/htm/cm02tech.htm)

I2C_CMD    address    mode-reg    data-byte-count              data
0x55              0x0C          0x00                    0x01                    0xAA

now in axon data received is always
mode-reg and data like above case

00 and 0A will be received

but data is received completely

for reading the format is

I2C_CMD     address + Read bit      bearing register             Number of bytes to read

so in bearing register I always send 00 may be this is the problem
my packet format

0x55      axon_iic_addr+1    00     02

but garbage is always received like FF FF.

Let me know your thoughts.

Thanks!


Title: Re: I2C tx not responding (axon2 with CM02)
Post by: Webbot on October 25, 2011, 07:17:23 AM
The only thing I can suggest is to try changing this:
Code: [Select]
   i2cSlaveInit(I2C_ADDRESS, TRUE, &rxBuffer, &txBuffer);
   bufferInit(&txBuffer, tx, sizeof(tx));
   i2cSlaveSetTransmitHandler(&txHandler);
   bufferInit(&rxBuffer, rx, sizeof(rx));
   i2cSlaveSetReceiveHandler(&rxHandler);
into this:
Code: [Select]
   bufferInit(&txBuffer, tx, sizeof(tx));
   i2cSlaveSetTransmitHandler(&txHandler);
   bufferInit(&rxBuffer, rx, sizeof(rx));
   i2cSlaveSetReceiveHandler(&rxHandler);
   i2cSlaveInit(I2C_ADDRESS, TRUE, &rxBuffer, &txBuffer);
ie make sure the buffers etc are setup before activating the I2C slave
Title: Re: I2C tx not responding (axon2 with CM02)
Post by: hassan.iftikhar on October 25, 2011, 03:28:29 PM
thanks for the reply webboot

here is the new development

when I try to read from the slave (axon)
and send packet

   command[0] = 0x55;
   command[1] = 0xC1;   // axon i2c address + 1 bit for read command
   command[2] = 0x00;  // mode register
   command[3] = 0x02;   // data bytes to read

then instead of txhandler which should be called as slave should now transmit
the rx handler is called again
I am sending the odd address but axon translate it as a data read called
I have tried cm02(master) with another i2c slave cmps03( from devantech) and it is working fine i.e. master is perfectly reading fine data from slave (cmps03)
but in case of axon again rxhandler instead of txhandler.

Thanks
Title: Re: I2C tx not responding (axon2 with CM02)
Post by: Webbot on October 25, 2011, 03:59:32 PM
Quote
then instead of txhandler which should be called as slave should now transmit the rx handler is called again
I am sending the odd address but axon translate it as a data read called

Yep - I can believe that. A 'read register(s)' call is normally broken down into the following i2c protocol:
1. Start
2. 'Write' the start register number (in your case 0)
3. Repeated Start
4. Do one 'read' for each register value required (in your case 2)

The rxHandler would be called by step 2 with the register value 0 (which is probably what you are seeing), and then the return values should be written out.

I've created a test bed with 2 x boards. The first board (i2c master) has a cmps03 device added to it and prints out the values. Adding a real CMPS03 device works fine.

Next I replace the CMPS03 with a second board (i2c slave) using your code and everything still works (except the compass bearing is fixed by the hard coded values you return).

So I'm not sure what other help I can give as its the cm02 board thats giving the problem somehow - since I dont have one then its hard for me to debug what it's doing.
Title: Re: I2C tx not responding (axon2 with CM02)
Post by: hassan.iftikhar on October 25, 2011, 10:16:37 PM
Thanks for the help and time webboot I really appreciate that

I guess my 150pounds are wasted just due to someone's carelessness

anyway can you please recommend me some wireless device (that you have used and works fine with axon2)
requirements:
- should be attached to pc using usb serial
- the other transceiver can be connected to axon and should use serial (I am tired of i2c)
- should have reasonable range
- you have used it and recommend it :)

thanks!
Title: Re: I2C tx not responding (axon2 with CM02)
Post by: Admin on October 25, 2011, 10:34:41 PM
an option:
http://www.societyofrobots.com/axon_mote/ (http://www.societyofrobots.com/axon_mote/)

bluetooth will also work fine:
http://www.societyofrobots.com/electronics_bluetooth_robot.shtml (http://www.societyofrobots.com/electronics_bluetooth_robot.shtml)