BasicMicro - Forums

www.basicmicro.com
It is currently Mon May 21, 2012 12:16 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: I2C Slave mode on Nano...
PostPosted: Sun Oct 17, 2010 9:39 am 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 527
Recently for the heck of it, I ordered a Nano Dev board and also a Nano 40 and a Nano 18. I don't have any real plans for them, but thought it would be fun to play around.

One thing that I thought would be fun to try is to make the Nano be a slave to an Atom pro and maybe offload some work, like maybe monitoring RC signals and the like. For example maybe I use the Nano to monitor my hacked up RC receiver and provide the data back to the BAP through I2C. That is if the BAP does a read for register 0 it returns the current value for channel 0...

I am just starting to play around. I was just wondering if anyone has already played around with using the I2C in slave mode on any of the Nanos.

My guess is that I will need to setup the SCL/SDA pins as input and probably setup the appropriate SSP port values to initialize the I2C. Also it looks like I may need to setup an interrupt handler for when the a byte is received... I have not done an PIC programming before so this is all new to me.

Suggestions?

Thanks,
Kurt

P.S - Assuming I get this to work, maybe next will be to implement an HI2C for the BAP...


Top
 Profile  
 
 Post subject: Re: I2C Slave mode on Nano...
PostPosted: Sun Oct 17, 2010 7:03 pm 
Offline
Master

Joined: Sun Aug 17, 2008 5:26 pm
Posts: 798
Location: CA bay Area
I have seen this "I2C Slave" discussion come up before. The stock answer has been,"Studio does Master mode only, never a Slave." So I bet there are lots of people waiting to see your work on turning a Nano into a Slave and take direction from another uprocessor.

However, there is already a series of Microchip slave processors available, the lesser one being this:
http://ww1.microchip.com/downloads/en/DeviceDoc/21919e.pdf
This does either I2C or SPI, just drop a zero and add an "S" to the part number.
Then there is the next step up, the MCP23016:
http://ww1.microchip.com/downloads/en/DeviceDoc/20090C.pdf
This is I2C only.
I can't find a version supporting ADC, so let us know how how your work goes!
You could always use the hardware USART on any Atom part to do this support work. A simple back-and-forth system might be doable.
Take care.

_________________
kenjj
http://blog.basicmicro.com/
http://kjennejohn.wordpress.com/


Top
 Profile  
 
 Post subject: Re: I2C Slave mode on Nano...
PostPosted: Mon Oct 18, 2010 12:43 pm 
Offline
Master

Joined: Tue Jun 22, 2010 1:15 pm
Posts: 203
Hi Kurt,

There is "slave" code (C) available for the PIC used to implement the SRF08 (or one of them) sonar rangefinder. That's what I'm going to use for some of my slaves. Source code is probably on the arconame website

http://www.acroname.com/robotics/parts/R145-SRF08.html


Alan KM6VV

_________________
Visit:
http://groups.yahoo.com/group/SherlineCNC/
http://tech.groups.yahoo.com/group/HexapodRobotIK/


Top
 Profile  
 
 Post subject: Re: I2C Slave mode on Nano...
PostPosted: Mon Oct 18, 2010 1:12 pm 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 527
Thanks guys,

My guess is the SRF08 code is in master mode, that is the Pic is the master, where I would like to be the slave.
I have found some stuff up on the net for doing this in C using the underlying hardware. I will probably play with this and convert the interrupt function to ASM. I am new to pics so it may take me a little.

Maybe I will try to install MPLAB ... and compile the C code and hopefully see what asm it creates and work my way through it.

Again this is just a for fun background task, may have no real need for IE yet...

Kurt


Top
 Profile  
 
 Post subject: Re: I2C Slave mode on Nano...
PostPosted: Mon Oct 18, 2010 1:38 pm 
Offline
Master

Joined: Tue Jun 22, 2010 1:15 pm
Posts: 203
Kurt,

I was thinking of the code that runs ON the SRF module (slave code). but I can't find it just yet.

Alan KM6VV

_________________
Visit:
http://groups.yahoo.com/group/SherlineCNC/
http://tech.groups.yahoo.com/group/HexapodRobotIK/


Top
 Profile  
 
 Post subject: Re: I2C Slave mode on Nano...
PostPosted: Tue Oct 19, 2010 9:08 am 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 11:00 am
Posts: 903
Location: Temecula, CA
You can setup the i2c hardware on the Nano to be an I2C slave. If you have a C example you should be able to translate it to Basic pretty easily.

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: I2C Slave mode on Nano...
PostPosted: Tue Oct 19, 2010 9:13 am 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 527
Acidtech wrote:
You can setup the i2c hardware on the Nano to be an I2C slave. If you have a C example you should be able to translate it to Basic pretty easily.


Yep, the C version uses an interrupt to do everything...
Code:
void interrupt My_interrupt(void) {

  if (/*PIR1.*/SSPIF == 1){                // I2C Interrupt
    if (/*SSPCON.*/SSPOV==1){
      j=SSPBUF; // Read address to clear buffer
      /*SSPCON.*/SSPOV=0;
    }
    else {
      if (/*SSPSTAT.*/START==1 && /*SSPSTAT.*/RW==0 && /*SSPSTAT.*/DA==0 && /*SSPSTAT.*/BF==1){state=1;} // State 1: MASTER WRITE, LAST BYTE WAS AN ADDRESS
      if (/*SSPSTAT.*/START==1 && /*SSPSTAT.*/RW==0 && /*SSPSTAT.*/DA==1 && /*SSPSTAT.*/BF==1){state=2;} // State 2: MASTER WRITE, LAST BYTE WAS DATA
      if (/*SSPSTAT.*/START==1 && /*SSPSTAT.*/RW==1 && /*SSPSTAT.*/DA==0 && /*SSPSTAT.*/BF==0){state=3;} // State 3: MASTER READ, LAST BYTE WAS AN ADDRESS
      if (/*SSPSTAT.*/START==1 && /*SSPSTAT.*/RW==1 && /*SSPSTAT.*/DA==1 && /*SSPSTAT.*/BF==0){state=4;} // State 4: MASTER READ, LAST BYTE WAS DATA
      if (/*SSPSTAT.*/START==1 && /*SSPSTAT.*/DA==1 && /*SSPSTAT.*/BF==0 && /*SSPSTAT.*/RW==0 && /*SSPCON.*/CKP==1){state=5;} // State 5: MASTER NACK

      switch ( state ) {
        case 1:
          j=SSPBUF; // Read address to clear buffer
          if (/*SSPCON.*/SSPOV==1){ //overflow?
            /*SSPCON.*/SSPOV=0;
            j=SSPBUF; // Read address to clear buffer
          }
          break;

        case 2:
          rxbuffer=SSPBUF; // Read DATA from master.
          if (/*SSPCON.*/SSPOV==1){ //overflow?
            /*SSPCON.*/SSPOV=0;
            rxbuffer=SSPBUF; // Read address to clear buffer
          }
          break;

        case 3:
          /*SSPCON.*/CKP =0; // Hold the SCL line low
          tx_data++;
          SSPBUF = tx_data; // load buffer with data to be sent to master
          /*SSPCON.*/CKP =1; // Release the SCL line
          j=SSPBUF; // read to empty buffer
          break;

        case 4:
          /*SSPCON.*/CKP =0; // Hold the SCL line low
          SSPBUF = tx_data; // load buffer with data to be sent to master
          /*SSPCON.*/CKP =1; // Release the SCL line
          j=SSPBUF; // read to empty buffer
          break;

        case 5:
          break;

        default : break;
      }
       j=SSPBUF; //clear buffer
    }
    /*PIR1.*/SSPIF = 0;                    // reset SSP interrupt flag
  } //I2C interrupt end
}

So I did compile it in C, got the list file and am playing with it. Also still need to experiment. Not sure when it extracts the address byte if it is the address of the device or the data address that you should be reading or writing to. Also need to add additional stuff in, like if first byte of a write is a register number, save that and use that for the next read or write...

Kurt


Top
 Profile  
 
 Post subject: Re: I2C Slave mode on Nano...
PostPosted: Fri Oct 22, 2010 9:04 am 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 527
I am making some progress on this. :D I have all of the I2C code an an ISRASM. I attached the current code in case anyone else wishes to follow along and or better yet improve/fix it.

The code for saving bytes from the Atom Pro to the Nano appears to be working pretty well. So far I have only tested it to save one byte at a time... Here is a logic trace of a simple write:
Attachment:
Trace-Nano-I2C-write.jpg
Trace-Nano-I2C-write.jpg [ 16.2 KiB | Viewed 876 times ]


The code to query data from the Nano to the the pro works some of the times, especially if I only do one byte. If I do multiple bytes something gets confused, I am still playing around with the different state flags to see what works for what... Here is a trace of two reads done in a row:
Attachment:
Trace-Nano-I2c-Read.jpg
Trace-Nano-I2c-Read.jpg [ 16.64 KiB | Viewed 876 times ]


Not sure why it thinks it is trying to go to ID 47?

The simple Atom Pro test code is simply:
Code:
; simple test to loop I2c Outputs... to try to see what the Nano sees.
i2creg   var   byte
i2cval   var byte
i2cdatain var byte(4)

i2creg = 0
i2cval = 0

Main:
   toggle p12
   I2cout p0, p1, 0x42, [i2creg, i2cval]
   i2cval = i2cval + 1
   
   if i2creg >= 9 then
      i2creg = 0
      toggle p13   
      ; lets try reading several of them back...
      i2cout p0, p1, 0x42, [0]
      i2cin p0, p1, 0x42, [i2cdatain(0)]
      i2cin p0, p1, 0x42, [i2cdatain(1)]
;      i2cin p0, p1, 0x42, [i2cdatain(0), i2cdatain(1), i2cdatain(2), i2cdatain(3)]
      serout s_out, i9600, [hex i2cdatain(0), " ", hex i2cdatain(1), " ", hex i2cdatain(2), " ", hex i2cdatain(3), 13]
      pause 150
   else
      i2creg = i2creg + 1
   endif
   pause 25
   goto main

That's all for now. It may be a few days before I play some more with this, I am now rebuilding my hex robot...

Kurt

P.S - I forgot to mention, please forgive my programming here, this is the first time I ever looked at a PIC microprocessor. Took me a little while to figure out how to do things like address an array. Also not always sure how often I need to do bank selects...


Attachments:
File comment: Still a work in progress. Currently for Nano 18...
nano i2c slave test program.bas [11.92 KiB]
Downloaded 83 times
Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO