EduardH
Posts: 46
Joined: Fri Sep 04, 2015 8:10 am

I2C on Rpi4 in C language

Sun May 09, 2021 7:58 am

Good morning,

I am looking for some code examples about using the I2C interface on a Rpi4 (or Rpi3B+). There are many examples wtih Python but I didn't find any in simple C language.

The ultimate goal is connecting a pico to the Rpi4. The code on the pico seems to be working. The pico shows up in i2cdetect -y 1 .

Sincerely,

Eduard

User avatar
joan
Posts: 15616
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C on Rpi4 in C language

Sun May 09, 2021 8:30 am

I suggest you choose a C GPIO library and look for I2C support. If you don't want to use a library I'd look at how the library implements the calls as a starting point.

User avatar
DougieLawson
Posts: 41297
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: I2C on Rpi4 in C language

Sun May 09, 2021 9:48 am

Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

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

Re: I2C on Rpi4 in C language

Sun May 09, 2021 5:47 pm

Please note that SMBus is a subset of I2C. Only use it if your slave device actually complies with the SMBus spec.

Kernel I2C docs - https://www.kernel.org/doc/html/v5.10/i ... rface.html
For generic I2C commands you want to look at ioctl I2C_RDWR. Set the slave address first with ioctl I2C_SLAVE.
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.

EduardH
Posts: 46
Joined: Fri Sep 04, 2015 8:10 am

Re: I2C on Rpi4 in C language

Tue May 11, 2021 7:28 pm

Good evening,

It took a lot of time figuring out why the Rpi4 master could write to the slave but not read. The slave is a raspberry pico. After all I changed the pico for an ordinary i2c integrated circuit. (a Philips TEA5767). From then on the bus was up and living.

There must be something wrong in the program I installed on the pico slave. Searching what goes wrong on the pico will be my next task.

Thanks a lot for Your help,

sincerely,

Eduard

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