Shop OBEX P1 Docs P2 Docs Learn Events
i2c Slave and COG RAM Access — Parallax Forums

i2c Slave and COG RAM Access

EricGarlicEricGarlic Posts: 41
edited 2008-11-12 00:10 in Propeller 1
Hello from the UK

I would like to use a propeller as an i2c Slave. The propeller then receives data written to it and then performs actions depending on the data that it receives. (I'm relatively new to propeller so any response should be couched in terms suitable for the hard of understanding).

The only i2c Slave object in existing is Hippy's :

forums.parallax.com/forums/default.aspx?f=25&m=263375
[noparse][[/noparse]url]

I can kick off the i2c Slave object in a cog. I can write to that slave (using another propeller and existing i2c master objects). How do I access the data accepted by the slave cog (I guess I'm asking how do I access COG ram data).

Any help appreciated.

Eric

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-10-24 19:22
    You can't access cog ram from another cog. What you have to do is to copy the data to the shared hub ram where other cogs can access it. Look at existing I/O drivers included with the Propeller Tool and from the Propeller Object Exchange. They all do this. The PS/2 keyboard driver is one example.
  • hippyhippy Posts: 1,981
    edited 2008-10-24 19:26
    It comes down to where the Cog acting as the I2C Slave puts the data it receives; this can either be in Cog memory or in Hub memory. If it's in Hub Memory it's simply case of reading it and using it from other Cogs / Spin.

    If it's in Cog memory you'll need a mechanism to interact with the Cog and have it place its data in Hub memory so it can be accessed by other Cogs. This is going to interfere with the I2C Bus handling itself so it's not recommended. Having the I2C Slave Cog store its data in Hub is probably the best solution.

    Added : Looking at the demo code you will find this to start a 16Kx8 RAM, based at $4000 in hub memory ...

    i2c_Slave.Start( SCL_PIN, SDA_PIN, DEVICE_ID, $4000, 16*1024 )

    You can use @MyArray to supply an array base address instead of $4000, and the array size can be any power of two, so ...

    CON
      SIZE = 1 * 1024 
    VAR
      byte MyArray[noparse][[/noparse] SIZE ]
    PUB Main
      i2c_Slave.Start( SCL_PIN, SDA_PIN, DEVICE_ID, @MyArray, SIZE )
      
    
    



    The AiChip_I2cSlave_Slave object uses 1KB of Cog regardless so it's necessary to delete the two lines at ":SaveByteToHub" and the two lines at ":LoadByteFromHub".

    It's been a while since I used or looked at the code so apologies if I've remembered anything wrong. Shout if you need any further help.

    Post Edited (hippy) : 10/24/2008 8:12:38 PM GMT
  • EricGarlicEricGarlic Posts: 41
    edited 2008-10-24 20:24
    Mike & Hippy, thank you for your prompt replies. I'll give it go. I do struggle understanding how to pass data around (VARS/CONS/@ and even @@/Byte[noparse][[/noparse]xx] etc), between cogs and objects and objects in Cogs. I guess this answer is in there somewhere. Hippy I would really appreciate it if you could find the time to document your i2c objects. I've thrashed around with them but with limited luck when it came to anything other than running your demo. Again, thank you for the prompt reply.
  • EricGarlicEricGarlic Posts: 41
    edited 2008-10-24 21:03
    Cheers Hippy - made me feel better because I thought that was how you access HUB data between cogs; not so good about having to change assembly lines - but I deleted as you said.

    No joy. Here is my Master and Slave code respectively, in case I've done something daft.

    Additional (I've re-edit (cut-n-paste) but the code is not looking so good when I paste onto the forum - sorry)

    I've got to put the nippers to bed now so a reply isn't urgent.


    'Master_1 I2C experiment SpinStudio Board 
    
    CON
    
      _CLKMODE      = XTAL1 + PLL16x
      _XINFREQ      = 5_000_000
    
      SDA_PIN       = 29
      SCL_PIN       = 28
    
      DEVICE_ID     = $E4
    
                           
    
    OBJ
         i2cObject : "i2cObject" 
    
    Pub Main | temp, i
      i2cObject.init(SDA_PIN,SCL_PIN,False)  'Initialise main i2c
      WaitCnt( CLKFREQ/100 + CNT )
      i2cObject.start
      WaitCnt( CLKFREQ/100 + CNT ) 
      dira[noparse][[/noparse]0..7]~~        'LEDs on 0 & 1 showed me that the repeat cycle is working
    
    Repeat
      repeat i from 1 to 500
          outa~
          i2cObject.Writelocation(DEVICE_ID,i,99,8,8)  'writeLocation(deviceAddress, deviceRegister,
          !outa[noparse][[/noparse]0]                                     'i2cDataValue, addressbits,databits).  99 is value to slave
          WaitCnt( CLKFREQ + CNT )
          !outa
           
          temp := i2cobject.readlocation(Device_ID,i,8,8)     'deviceAddress, deviceRegister, addressbits, databits
          If Temp == 99
            outa~~           'LED on Pin 3 tells me If managed to read the slace
            WaitCnt(CLKFREQ*2 + CNT )
    
    





    'Slave_1 Experiment  Using Demo Board
    
    
    CON
    
      _CLKMODE      = XTAL1 + PLL16x
      _XINFREQ      = 5_000_000
    
      SDA_PIN       = 1
      SCL_PIN       = 0
    
      ID_1     = $E4
      SIZE = 1 * 1024
    
        
    Var
    
    Byte Temp
    byte MyArray[noparse][[/noparse] SIZE ] 
    Obj
    
      i2c_Slave1    : "AiChip_I2cSlave_Slave_006"
    
      
    Pub Main
      Dira[noparse][[/noparse]16..23]~~
    
    
      if ID_1 => 0
        i2c_Slave1.Start( SCL_PIN, SDA_PIN, ID_1, @MyArray, size )
        WaitCnt( CLKFREQ / 1000 * 100 + CNT )
    
    Monitor
    
    Pub Monitor | i
      repeat
        !outa[noparse][[/noparse]16]    'lights an LED to say I'm cycling through the code
        repeat i from 1 to size
          Temp := MyArray[i]
          if temp == 99  '99 is the value I'm trying to write to the slave
            outa[noparse][[/noparse]17..22]~~    'LEDs on 17..22 tell me that I have received data on the slave
            waitcnt(clkfreq*5 +cnt)
          else
            outa[noparse][[/noparse]17..22]~    
    
    [/i]
    

    Post Edited (EricGarlic) : 10/24/2008 9:10:44 PM GMT
  • SeariderSearider Posts: 290
    edited 2008-11-09 20:04
    Eric,

    did you ever get this figured out? I am working on the same thing and would love to avoid any wheel inventing.

    thx

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

    Searider
  • EricGarlicEricGarlic Posts: 41
    edited 2008-11-10 23:57
    Hi ya,

    Nope. Too busy at work and with the kids to get back to the bench. Give it go and post your findings. Good luck

    Eric
  • SeariderSearider Posts: 290
    edited 2008-11-11 01:01
    I am using the Full-Duplex Serial object using one wire. This seems to be very simple and I got it working in less than 30 min.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

    Searider
  • EricGarlicEricGarlic Posts: 41
    edited 2008-11-11 19:40
    Good on yeh. But was the communication I2C or serial? Full Duplex chip-to-chip is fine. I2C MasterProp-to-SlaveProp is another matter. I'm cool with Propeller as an I2C master device, but not as a slave device.

    Eric
  • SeariderSearider Posts: 290
    edited 2008-11-12 00:10
    When I moved to the FullDuplex Serial object I quit working on the I2C. It looks like I can get what I wanted (Prop to Prop) quicker and easier. The vale that I2C had was the ability to have multiple devices with addressability. I don't need that right now.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

    Searider
Sign In or Register to comment.