I2C Sensor Integration in Linux - Page 1
Forum section: electronics / general

author: BlogRahul
author_rank: Regular Contributor
timestamp: August 05, 2023, 01:35:23 pm
content: hello expertsI'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.

author: ledtester
author_rank: Super Contributor
timestamp: August 05, 2023, 03:00:44 pm
content: This video might help:Basics of I2C on Linux - Luca Ceresoli, Bootlin -- The Linux Foundationhttps://youtu.be/g9-wgdesvwAAlso, have a look at the lm75 device driver:https://github.com/torvalds/linux/blob/master/drivers/hwmon/lm75.cThe video discusses the driver for the PCA9570:https://github.com/torvalds/linux/blob/master/drivers/gpio/gpio-pca9570.cIt also talks about accessing the I2C bus from user space using the i2c-tools package. Presumably no device driver is needed.

author: nctnico
author_rank: Super Contributor
timestamp: August 05, 2023, 05:31:09 pm
content: 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.

author: zilp
author_rank: Frequent Contributor
timestamp: August 05, 2023, 07:43:12 pm
content: This should probably explain everything you need, as far as I²C is concerned:https://www.kernel.org/doc/Documentation/i2c/

author: rstofer
author_rank: Super Contributor
timestamp: August 07, 2023, 02:52:21 am
content: This starter kit has plenty of examples of I2C code:https://www.amazon.com/FREENOVE-Ultimate-Raspberry-558-Page-Detailed/dp/B06W54L7B5

author: DiTBho
author_rank: Super Contributor
timestamp: August 07, 2023, 06:07:10 am
content: Quote from: nctnico on August 05, 2023, 05:31:09 pmOr, 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.

author: RoGeorge
author_rank: Super Contributor
timestamp: August 07, 2023, 07:16:47 am
content: Playlist about how to write Linux drivers:Let's code a Linux DriverJohannes 4GNU_Linuxhttps://www.youtube.com/playlist?list=PLCGpd0Do5-I3b5TtyqeF1UdyD4C-S-dMa

author: BlogRahul
author_rank: Regular Contributor
timestamp: August 07, 2023, 12:55:22 pm
content: Quote from: DiTBho on August 07, 2023, 06:07:10 amThank 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?

author: DiTBho
author_rank: Super Contributor
timestamp: August 07, 2023, 01:24:01 pm
content: Quote from: BlogRahul on August 07, 2023, 12:55:22 pmfind one similar, clone it, add it to the Makefile, study it, and modify it.