Page 1 of 1

I2C Question

Posted: Sat Oct 16, 2021 11:32 pm
by Andrew1234
I want to use my pyboard v1.1 to control an audio codec, the ADAU1361.
www.analog.com/adau1361
The ADAU1361 has a PLL register at address 0x4002 that takes 6 bytes (48-bit register that must be written with a single continuous write)

My test code:
from machine import I2C
i2c = I2C('X')
i2.scan() # result is 56, or 0x38, which is the correct I2C address of the codec
i2c.writeto_mem(56, 0x4002, bytearray([0,1,0,0,20,3]), addrsize=16)
i2c.readfrom_mem(56, 0x4002, 6, addrsize=16)
b'\x00\x01\x00\x00\x14\x01'

I expected to see:
b'\x00\x01\x00\x00\x20\x03'

Can anyone anyone comment on what I may be doing wrong or failing to understand?

Re: I2C Question

Posted: Sun Oct 17, 2021 10:14 am
by Roberthh
decimal 20 is hex 14. The data you wrote conmtained decimal 20, and that was read back and shown as \x14. If you want to have \x20 in the register, you have to writeit as decimal 20 or hex 0x20.

Re: I2C Question

Posted: Sun Oct 17, 2021 10:55 am
by Andrew1234
Thanks for the reply!
The six bytes I meant to write are these (with decimal in parentheses)
00000000 - (0)
00000001 - (1)
00000000 - (0)
00000000 - (0)
00100000 - (32)
00000011 - (3)

My corrected code still has an output that I don't understand:
from machine import I2C
i2c = I2C('X')
i2.scan() # result is 56, or 0x38, which is the correct I2C address of the codec
i2c.writeto_mem(56, 0x4002, bytearray([0,1,0,0,32,3]), addrsize=16)
i2c.readfrom_mem(56, 0x4002, 6, addrsize=16)
b'\x00\x01\x00\x00 \x03'

Note the space between the 4th and 5th hex values.. ?
I expected to see 6 bytes returned:
b'\x00\x01\x00\x00\x20\x03'

BTW, can the writeto_mem function accept a hex format instead of a bytearray? I've tried to use hex but get an error. I cannot find the place in the document where the format of the arguments is described. I found the bytearray format recommended in the forum.

Regards
Andy

Re: I2C Question

Posted: Sun Oct 17, 2021 11:01 am
by Roberthh
Note the space between the 4th and 5th hex values..
That's the way, 0x20 is printed. When Python prints bytes objects, it shows printable bytes as such, and non-printable as \xnn. Since \x20 is printable, the character is shown, which in this case is a blanc.

Re: I2C Question

Posted: Sun Oct 17, 2021 11:19 am
by Andrew1234
Okay I'm starting to see now -- you mean the ascii printable characters
Would you say that I'm formatting this in the best way possible? My preference would be to input hex values in the function call, and get hex values in the return value. Maybe that isn't possible..

Regards
Andy