Author Topic: I2C Sensor Integration in Linux  (Read 3052 times)

0 Members and 1 Guest are viewing this topic.

Offline BlogRahulTopic starter

  • Regular Contributor
  • *
  • Posts: 75
  • Country: in
I2C Sensor Integration in Linux
« on: August 05, 2023, 01:35:23 pm »
hello experts

I'm facing a bit of confusion while trying to grasp the concept of interfacing an I2C temperature sensor (such as the LM75) with a Linux system. I have experience with bare-metal coding and interfacing I2C devices directly with microcontrollers by configuring registers and such. However, the transition to Linux has left me a bit perplexed.

As I understand it, when working with Linux, the process involves developing a kernel-space driver to facilitate communication with the sensor, as well as creating a user-space application to interact with the driver. I've seen mentions of accessing the kernel source code, developing the driver, and writing the user application, but the details are a bit hazy.

To put things into perspective, let's take the example of the LM75 temperature sensor. In a bare-metal scenario, I'd usually set up registers and handle I2C transactions directly. However, in a Linux environment, I'm not entirely sure about the low-level coding involved.

I'd greatly appreciate it if anyone could shed some light on this topic. How does one approach the low-level coding for I2C in Linux development? What are the key steps involved in creating the kernel-space driver and the user-space application?   

Please note that this is a hypothetical question aimed at understanding the concept better. I'm particularly interested in gaining insights into how the low-level coding for I2C is tackled in Linux development. Your guidance and expertise would be immensely valuable!

Thank you in advance for sharing your knowledge and experiences.
 

Online ledtester

  • Super Contributor
  • ***
  • Posts: 3505
  • Country: us
Re: I2C Sensor Integration in Linux
« Reply #1 on: August 05, 2023, 03:00:44 pm »
This video might help:

Basics of I2C on Linux - Luca Ceresoli, Bootlin -- The Linux Foundation
https://youtu.be/g9-wgdesvwA

Also, have a look at the lm75 device driver:

https://github.com/torvalds/linux/blob/master/drivers/hwmon/lm75.c

The video discusses the driver for the PCA9570:

https://github.com/torvalds/linux/blob/master/drivers/gpio/gpio-pca9570.c

It also talks about accessing the I2C bus from user space using the i2c-tools package. Presumably no device driver is needed.


« Last Edit: August 05, 2023, 03:08:06 pm by ledtester »
 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 28871
  • Country: nl
    • NCT Developments
Re: I2C Sensor Integration in Linux
« Reply #2 on: August 05, 2023, 05:31:09 pm »
The Linux kernel has I2C drivers built-in so no need to write your own driver. Unless you are using unknown  hardware.  There is also a bit bang driver that can use gpio pins for I2C. The kernel will manage access to several devices on the bus so several processes can use the same I2C bus.

At the application side you'll need to open the I2C device snd use ioctl calls to select de I2C addresses  you want to access. It is not very complicated.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline zilp

  • Frequent Contributor
  • **
  • Posts: 351
  • Country: de
Re: I2C Sensor Integration in Linux
« Reply #3 on: August 05, 2023, 07:43:12 pm »
This should probably explain everything you need, as far as I²C is concerned:

https://www.kernel.org/doc/Documentation/i2c/
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9988
  • Country: us
Re: I2C Sensor Integration in Linux
« Reply #4 on: August 07, 2023, 02:52:21 am »
 

Offline DiTBho

  • Super Contributor
  • ***
  • Posts: 4637
  • Country: gb
Re: I2C Sensor Integration in Linux
« Reply #5 on: August 07, 2023, 06:07:10 am »
The Linux kernel has I2C drivers built-in so no need to write your own driver. Unless you are using unknown  hardware.

Or, unless you are using hardware which doesn't have any physical i2c adapter, and you need to implement it someway.

e.g. my PPC4xx doesn't have any GPIO, doesn't have any i2c, there are two latch ports, you can use them like GPIOs, but you have to implement a little kernel support, then a bit bang driver that can mimic i2c.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline RoGeorge

  • Super Contributor
  • ***
  • Posts: 7344
  • Country: ro
Re: I2C Sensor Integration in Linux
« Reply #6 on: August 07, 2023, 07:16:47 am »
Playlist about how to write Linux drivers:

Let's code a Linux Driver
Johannes 4GNU_Linux
https://www.youtube.com/playlist?list=PLCGpd0Do5-I3b5TtyqeF1UdyD4C-S-dMa

Offline BlogRahulTopic starter

  • Regular Contributor
  • *
  • Posts: 75
  • Country: in
Re: I2C Sensor Integration in Linux
« Reply #7 on: August 07, 2023, 12:55:22 pm »
The Linux kernel has I2C drivers built-in so no need to write your own driver. Unless you are using unknown  hardware.

Or, unless you are using hardware which doesn't have any physical i2c adapter, and you need to implement it someway.

e.g. my PPC4xx doesn't have any GPIO, doesn't have any i2c, there are two latch ports, you can use them like GPIOs, but you have to implement a little kernel support, then a bit bang driver that can mimic i2c.

Thank you for sharing that insight about the built-in I2C drivers in the Linux kernel – However, I'm particularly intrigued by the idea of creating a driver for an unsupported I2C device.

Imagine we're dealing with such a scenario – an I2C device without an existing driver. Given the vast structure of the Linux kernel with its various folders and files like arch, mm, fs, lib, and more, where would you suggest starting the process of developing a new device driver?
 

Offline DiTBho

  • Super Contributor
  • ***
  • Posts: 4637
  • Country: gb
Re: I2C Sensor Integration in Linux
« Reply #8 on: August 07, 2023, 01:24:01 pm »
where would you suggest starting the process of developing a new device driver?

find one similar, clone it, add it to the Makefile, study it, and modify it.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf