Hello, ADS1015!

Tux and his Gibson

images

His main hobby is playing guitar, especially electric guitar which makes the sound of the guitar to be heard from the Earth to the Moon. Tux told me that the first guitar which performed analog-to-digital conversion was a Gibson guitar in 2002.

Link to the Tux image





An analog-to-digital converter takes an analog signal as input and outputs a digital signal. See the below image to fully understand the difference between these two types of signals. images

Link to the Analog to Digital Signal

ADS1015 sensor

In the past weeks our goal was to develop a driver for an ADC sensor, the ADS1118. By looking through the existing ADC drivers in the Linux Kernel source we found out that there is a converter with similar characteristics and that converter is ADS1015.

The main difference between ADS1118 and ADS1015 is the serial communication interface. The first one is on SPI (serial peripheral interface) and the second one is on I2C interface.

The challenge was to split the code for ADS1118 into a core file and an I2C file and to add the SPI file. As you may guess, the core file contains the common functions used to read from channels and to convert the signal into digital values. The SPI and I2C files, are used for the communication between the sensor and the adaptor or microcontroller.

Compile and configure the Linux Kernel for Raspberry Pi

For checking if the I2C communication is still working after the split, I used a Raspberry Pi. If you have a Raspberry Pi and want to a connect a sensor from linux/drivers/iio, follow these steps:

Download the from the official site.
wget https://downloads.raspberrypi.org/raspbian_latest

Write the Raspbian image to the SD card using Etcher.

Download the kernel source for Raspberry.
git clone --depth=1 https://github.com/raspberrypi/linux

Make the default configuration.
make bcm2709_defconfig

Compile the kernel.
make -j4 zImage modules dtbs

Install the modules.
sudo make modules_install

Copy the files to /boot.
sudo cp arch/arm/boot/dts/*.dtb /boot/ sudo cp arch/arm/boot/dts/overlays/*.dtb* /boot/overlays/ sudo cp arch/arm/boot/zImage /boot/kernel7.img

Reboot the Raspberry

Connect the pins

Before connecting the sensor, be sure that you have the right tools. Install the i2c-tools and check the pins.
sudo apt-get install i2c-tools sudo i2cdetect -y 1

Make the following connections from ADS1015 to your Raspberry Pi:
* VDD to 3v3 Power (pin 1)
* SDA to Data (pin 3)
* SCL to Clock (pin 5)
* GND to Ground (pin 6)

The Raspberry’s pins and ADS1015’s pins can be found in the below images.

images images

Link to ADS1015
Link to Raspberry’s Pins

Measure the voltage

Now that we have the wire connections, create a new IIO device with the command:

echo 0x48 > /sys/bus/i2c/devices/i2c-1/new_device

After the sensor initialization, we can start measuring the voltage.

For example, connect a wire from the Raspberry’s Pin 1 to the ADS1015’s A0 pin and check the /sys/bus/iio/devices/iio:device0/in_voltage0_raw file.

cat /sys/bus/iio/devices/iio:device0/in_voltage0_raw

Another example is to connect a wire from Raspberry’s Pin 6 to the ADS1015’s A0 pin and check the same file. You will find that the voltage is 0 because A0 is wired to ground.

Results

We managed to check the two examples after splitting the ADS1015 into the 2 files: core, i2c.

The next challenge is to check if the ADS1118 implementation is working. This ADC sensor has a SPI interface. See you soon!

Start the Adeventure: The Setup Part

Intro

images

Our wise friend, will introduce us the world of Linux Kernel. Check below the piece of wisdom from Yoda Tux.

Good coding vibe needs a good working environment. So, our setup, let’s see how to do. There are several simple steps and, take them one by one, will we. Hihihi!

Link to the Yoda Tux image


The source code

Download the kernel source tree of Industrial I/O:

git clone https://www.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git

The default config

The next step is to configure the kernel. As a good practice, use the default configuration and later add or remove kernel modules. To create the configuration file, run the following command:

make defconfig

The configuration is based on the architecture type of the host system (eg: x86, powerpc, sparc64). The available architectures are to the iio/arch path.

Customize the kernel config

As the I/O subsystem is not part of the default configuration, we have to add the module using the command:

make menuconfig

This command opens the menu from the image, which is not friendly, but easy to use. Navigate with the arrows keys, press Y to include a module, press N to exclude a module and M to create a module from that feature/driver.

images

To search for any feature or module, just press the / (the slash key from the keyboard) and type the name.

images

At this moment, we are looking for the Industrial I/O Subsystem. Let’s type IIO and press Enter. The result looks like the below image.

images

Each results from the list has a corresponding number between parentheses. Go to the feature by pressing the indicated number.

In this situation, the Industrial I/O subsystem has the number (1), but it depends on other 3 features: RTC_DRV_HID_SENSOR_TIME RTC_CLASS USB_HID

The first one is marked with [=n], which means that it is not selected to be part of the kernel configuration. Adding this features includes automatically the Industrial I/O module in the kernel. So, let’s search for the RTC_DRV_HID_SENSOR_TIME and include it. Further, search again for the IIO. Check if the feature is marked with [=y].

Save the new kernel configuration by pressing the save button. Keep the default name of the configuration file which is .config. You can check the existence of this file in the iio folder (the root folder of the source code).

Compile the source code

The next step is to compile the kernel. It is recommended command to use all the processing units in order to finish the compiling fast. In the next command, the nproc gives the maximum number of the processor units available on your hardware. So, take a break, have some coffee or tea and come back later.

make -j `nproc`

In case you still want to use the computer during the processing time, replace the nproc with a number lower than the maximum units.

In case a feature was marked as module in the menuconfig, the module (a file with he .ko extension) will be available when the compiling is finished.

The kernek image

After the compiling is done, the kernel image could be found on the path:

iio/arch/your_architecture/boot/bzImage

In my case, the path is iio/arch/x86_64/boot/bzImage because the kernel was compiled for a 64bit Intel architecture.

Qemu setup

The Qemu is an easy and fast tool to create a virtual machine. Follow the tutorial to build a qemu with a custom kernel (link) and use the kernel image that you have just compiled.

My piece of advice is to run the qemu as in the tutorial, with

-nographic -append "console=ttyS0"

These two parameters let the qemu to run without opening a new windows and to show the output in the working terminal.

Possible issue:

The qemu is on 32bit, but the kernel is compiled on 64bit. Check in the menu config if the first option matches the type of the qemu architecture. images

Time to work

In case you encounter a problem with the tutorial, send me an email to

<georgiana.chelu93 AT SPAMFREE gmail DOT com>

and as Yoda Tux would say:

Patience you must have, my young padawan.

Hello From the Tux Side

Hello!

I am Georgiana and I am currently pursuing a Master’s Degree in Advanced Network Security at the University Politehnica of Bucharest. In addition to this, I am glad to say that I was accepted to the Outreachy Round 15. The project that I was selected for aims to build an Industrial I/O Driver.

This journey started 3 years ago when I applied to Outreachy for the first time. Back then, my lack of experience did not stop me to give it a try. The result was not the one I was expected and my confidence faded. Thus, the following years, my main focus was on building a solid knowledge in the C programming language, Git and Linux Kernel. Now, I am happy that I will write my first driver for the IIO Linux Kernel module, under the guidance of Daniel Băluță and Alison Schofield. I own them lot of thanks for their time and their support.

The community played an important role by helping me during the application period. The mentors had an incredible patience for our silly mistakes and their responses revealed professionalism, passion and attention to details. Regarding my contribution to Linux Kernel, my struggle in the past was the Git tool. But this time, I discovered the beauty of using this version control system. Now, I am ready to level up my knowledge and confidence.

images

So, let the adventure of IIO Drivers begin! Tux will join me in this journey and help me during the future challenges. He will help me through debugging sessions (as rubber duck) or will make things happen (as a wizard would do).

Wizard Tux image source.