machine.I2C and the stop condition
-
- Posts: 14
- Joined: Sun Dec 13, 2015 5:31 pm
machine.I2C and the stop condition
I am trying to communicate with the impedance converter AD5933 through I2C. However, it seems that Analog Devices have adopted their own flavour of block reading and writing, cf. the datasheet p.27-29.
Using the machine.I2C library, I have not had success reading and writing blocks. I have checked, and the *_mem* functions do not yield the correct result with this device.
Cf. the docs, it should be possible to construct the calls the AD5933 specifies, however, I need to control the stop-condtion, which seems to be unavailable? Is this the case, or did I miss something obvious? I'm using a pyboard v1.1 with firmware v1.9.2.
Using the machine.I2C library, I have not had success reading and writing blocks. I have checked, and the *_mem* functions do not yield the correct result with this device.
Cf. the docs, it should be possible to construct the calls the AD5933 specifies, however, I need to control the stop-condtion, which seems to be unavailable? Is this the case, or did I miss something obvious? I'm using a pyboard v1.1 with firmware v1.9.2.
-
- Posts: 14
- Joined: Sun Dec 13, 2015 5:31 pm
Re: machine.I2C and the stop condition
I have implemented a solution where I read and write a single byte at a time. This works for now, but for a later version I would like to read and write larger blocks in one go, so I am still interested in this topic
Re: machine.I2C and the stop condition
If you look at the documentation (http://docs.micropython.org/en/latest/p ... operations) you will see that you have full control over what gets sent and received. You shouldn't have any problems constructing the necessary transactions. Perhaps you should show us your code if it doesn't work as you expect it. Best write a small example program that demonstrates the issue.
Re: machine.I2C and the stop condition
Looking at the descriptions of the commands, they look like perfectly normal and standard I2C communication. You should be able to do it with a writeto with stop=False, followed by a readfrom.
Re: machine.I2C and the stop condition
It should work if you drop the "stop=" from your command, and just put False in there.
-
- Posts: 14
- Joined: Sun Dec 13, 2015 5:31 pm
Re: machine.I2C and the stop condition
Dear @deshipu,
Thanks for commenting on this. I see that using false as a third and keyword-less argument works! Is the documentation wrong, or do I not get the finer points of how it is documented?
Thank you for your time! See working code at the bottom.
Using primitive operations
Not supported on my pyboard.
Thanks for commenting on this. I see that using false as a third and keyword-less argument works! Is the documentation wrong, or do I not get the finer points of how it is documented?
Thank you for your time! See working code at the bottom.
Using primitive operations
Not supported on my pyboard.
Code: Select all
from machine import I2C
I2C_ADDR = 0x0D
i2c = I2C(1, freq=100000)
set_pointer = bytearray(2)
set_pointer[0] = 0b10110000 # Set pointer command code
set_pointer[1] = 0x82 # Register address, to start writing at
write_ba = bytearray(5)
write_ba[0] = 0b10100000 # Block write command code
write_ba[1] = 3 # Number of bytes to write
write_ba[2] = 0x02 # First data byte
write_ba[3] = 0x03 # Second data byte
write_ba[4] = 0x04 # Third data byte
read_ba = bytearray(2)
read_ba[0] = 0b10100001 # Block read command code
read_ba[1] = 3 # Number of bytes to read
def test_i2c():
# WRITE BLOCK
# Set pointer
i2c.writeto(I2C_ADDR, set_pointer)
# Write
i2c.writeto(I2C_ADDR, write_ba)
# READ BLOCK
# Set pointer
i2c.writeto(I2C_ADDR, set_pointer, True)
# Set read block instruction with no stop condition
i2c.writeto(I2C_ADDR, read_ba, False)
# Read
i2c.readfrom(I2C_ADDR, 3)
Re: machine.I2C and the stop condition
The problem is that in normal Python, you can always specify arguments as keywords — so the documentation tools used don't give any way of marking if an argument is positional or keyword. However, MicroPython had to cut some corners, and most of its functions can only ever take positional arguments — that saves memory for not having to store their names.
-
- Posts: 14
- Joined: Sun Dec 13, 2015 5:31 pm
Re: machine.I2C and the stop condition
Ahh! Great explanation – will keep that in mind when reading the docs
Thanks again. I have now built my very rudimentary AD5933 driver. When it has been expanded and tested, I will share it .
Thanks again. I have now built my very rudimentary AD5933 driver. When it has been expanded and tested, I will share it .
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: machine.I2C and the stop condition
@NielsClausen That looks like an intriguing chip but I'm struggling to understand its purpose. As far as I can see it drives an external load with a sinewave and acquires a sample set of the load current. It then performs a DFT on the samples. Given that a linear load will produce only a single point in the frequency domain I assume its purpose is to analyse nonlinear loads. What information about the load can you expect to deduce from the frequency domain data?
Is there an idiot's guide anywhere which might enlighten me about these devices?
Incidentally the point made by @deshipu applies to functions written in C. If you write your own functions in MicroPython, normal Python calling conventions apply: you can optionally specify positional arguments by name in the calling code.
Is there an idiot's guide anywhere which might enlighten me about these devices?
Incidentally the point made by @deshipu applies to functions written in C. If you write your own functions in MicroPython, normal Python calling conventions apply: you can optionally specify positional arguments by name in the calling code.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 14
- Joined: Sun Dec 13, 2015 5:31 pm
Re: machine.I2C and the stop condition
The AD5933 measures the complex impedance of the load. I cannot disclose what I use it for specifically in this case, but one use-case is laid out on p. 32 of the datasheet.
I like the chip, as it has almost everything on-board, with minimal post-processing required.
I like the chip, as it has almost everything on-board, with minimal post-processing required.