freshinator
Posts: 5
Joined: Wed Mar 25, 2020 3:24 pm

Addressing 16 bit Register, Read/Write 16 bit into this Register

Fri Mar 27, 2020 8:39 am

Hello folks,

i already searched for simular Topics here but didn't get the right answer.
Hope it's okay to start a new Topic.

I'm trying to write 16-bit to an 16 bit Imager-Register via I2C form the Raspi.

I don't have any problems with I2Ctools.

As an Example:

i2cset -y 1 0x30 0x32 0x85 0x0F 0xA0 i

--> The Register 0x3285 is set with 0x0FA0

But as i have to set alot of Registers i just but all the command into a Shellscript.

i2cset -y 1 0x30 0x32 0x85 0x0F 0xA0 i
i2cset -y 1 0x30 0x35 0x27 0xF1 0xA8 i
.
.
.

But here is the Problem --> the delay between each Command is about 5 ms.
This is far to long for what i need it to.
So i thought i could go for a python script.

As an Example:
#! /usr/bin/python

import smbus
import time

bus = smbus.SMBus(1)

bus.write_byte_data(0x30, 0x3F91, 0x0D03)
bus.write_byte_data(0x30, 0x3502, 0x0808)


I read in other Topics, that Smbbus is not made for 16-bit Register.
I also know that I2C is not made for 16- bits only for 7/8 bit or 10 bit.
So Adressing should be 2x8bit, but the Manufacturer is not answering how he does that.

I'm a newbie to Programming things, so if you have ideas how i could realize it in otherways --> I'm always into it.
Maybe there is also an option of i2ctools to get the delay into microseconds.

Glad to hear all your Ideas.

Best Regards,

Andi

User avatar
DougieLawson
Posts: 37570
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Addressing 16 bit Register, Read/Write 16 bit into this Register

Fri Mar 27, 2020 8:53 am

Use write_i2c_block_data().
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

freshinator
Posts: 5
Joined: Wed Mar 25, 2020 3:24 pm

Re: Addressing 16 bit Register, Read/Write 16 bit into this Register

Fri Mar 27, 2020 11:01 am

Hey DougieLawson,

Thanks for the fast reply.

Ain't there the Problem, that i cannot address the 16 bit Register ?

Maybe you could clear this thing also for me :)

Stay healthy!

Best Regards,

Andi

User avatar
DougieLawson
Posts: 37570
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Addressing 16 bit Register, Read/Write 16 bit into this Register

Fri Mar 27, 2020 11:34 am

write_i2c_block_data() lets you send 1 to 32 bytes. if you have 16bits that's two bytes (the only hard part is byte ordering - you have to read your I2C device datasheet for that). It works much the same as the i2cset program you're using.

You'd see that in the source code for i2cset at https://fossies.org/linux/i2c-tools/tools/i2cset.c

Docs for the python stuff are at: https://pypi.org/project/smbus2/ Example 4 does what you want.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8076
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Addressing 16 bit Register, Read/Write 16 bit into this Register

Fri Mar 27, 2020 12:18 pm

If your device isn't an SMbus device, then don't use SMbus calls.
write_i2c_block_data doesn't do the right thing as it is still taking an SMbus register number as a parameter and inserting it into the I2C traffic.

i2c_rdwr is the correct call and is documented in https://pypi.org/project/smbus2/ with example under "Example 6: Dual i2c_rdwr".
Write two bytes, and then read as many as you fancy. Or write as many bytes as you want (probably 4).
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

freshinator
Posts: 5
Joined: Wed Mar 25, 2020 3:24 pm

Re: Addressing 16 bit Register, Read/Write 16 bit into this Register

Fri Mar 27, 2020 1:04 pm

hi 6by9,

thanks also for your reply.

I don't have the device here today, but Monday i can check that.
Could you please clarify what i have to do, so i can prepare my script for Monday?!

write = i2c_msg.write(80, [40, 50])

# Single transaction writing two bytes then read two at address 80

But to go back to my Example:

bus.write_byte_data(0x30, 0x3F91, 0x0D03)

There i would want to write to address 30 to Register 0x3F91 the value 0x0D03

so would it be like that :

write = i2c_msg.write(30,[3F,91],[0D03]) ?!

Thanks in advance
&
Stay Healthy,

Andi

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8076
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Addressing 16 bit Register, Read/Write 16 bit into this Register

Fri Mar 27, 2020 1:21 pm

write = i2c_msg.write(30,[3F,91,0D,03])
bus.i2c_rdwr(write)
write = i2c_msg.write(30,[35,02,08,08])
bus.i2c_rdwr(write)

You'll need to read your device's datasheet to see whether you need to swap the bytes around in either of those values. Quite frequently they are sent least significant byte first.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

freshinator
Posts: 5
Joined: Wed Mar 25, 2020 3:24 pm

Re: Addressing 16 bit Register, Read/Write 16 bit into this Register

Tue Mar 31, 2020 7:35 am

Hi 6by9 again,

sorry to distrub you again.
But actually none of my tries is working .
maybe you could help me again.


why is this one working :

i2cset -y 1 0x30 0x30 0x1A 0x10 0xD8 i

But this :

write = i2c_msg.write(30,[30,1A,10,D8])

Gives this Problem:

"1A" invalid Syntax

even if i change MSG and LSB, then it says:

"A1" is not defined.


Glad if you could help me again.

Best Regards,

Andi

User avatar
DougieLawson
Posts: 37570
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Addressing 16 bit Register, Read/Write 16 bit into this Register

Tue Mar 31, 2020 9:11 am

Python needs 0xDE (222 decimal) to define a hex constant.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “Interfacing (DSI, CSI, I2C, etc.)”