Making an I2C slave device.

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
dgoadby
Posts: 6
Joined: Sat Jan 24, 2015 11:25 am

Making an I2C slave device.

Post by dgoadby » Sun Jan 25, 2015 12:19 pm

I want to make a low-speed I2C device that takes various analogue readings and which can be read by an Rpi which is on my network. Taking readings and putting them into a data structure is the easy part and I have that working already. Now I need task that will respond to the I2C commands. The task really needs respond to an I2C read command addressed to this device and return the packed data. Ideally this needs to be interrupt driven.

We do have interrupts but I'm blowed if I can see how I create a task that will respond to an I2C command. I could monitor the SDA pin and do a sort of "reverse bit-bang" but that would surely overload this little critter when other I2C commands are being processed by other devices.

Can I get an interrupt from the I2C state machine so I can at least do something like this?

Code: Select all

Global
   value-list

I2C Int handler
   read target address
   if not "me" then return
   get command byte 
   process-command (ie return my current value-list)
  send results to I2C master
return

Main 
   init I2C (including our address)
   init value-list

   do forever
     get analogue values
     process values and store in value-list
     blink activity led
     wait 1 second
I'm used to PIC chips where I would probably have done this in C but I am a stranger to STM devices and, besides, I am a big python fan. So, am I asking too much or is this part of the "wish list"?

David Goadby, North Wales.

Post Reply