SlideShare uma empresa Scribd logo
1 de 28
Baixar para ler offline
IIO, a new kernel subsystem
IIO, a new kernel
subsystem
Maxime Ripard
Free Electrons
c Copyright 2009-2012, Free Electrons.
Creative Commons BY-SA 3.0 license.
Latest update: February 14, 2012.
Document sources, updates and translations:
Corrections, suggestions, contributions and translations are welcome!
Embedded Linux
Developers
Free Electrons
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 1
Outline
IIO, a new kernel subsystem
What is IIO ?
Definition
Current state in the kernel
Getting started
IIO device
IIO channels
Going further : Hardware triggers and buffers
Hardware Triggers
Buffers
Useful Resources
Conclusion
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 3
Maxime Ripard
Embedded Linux engineer and trainer at Free Electrons since
2011
Embedded Linux development: kernel and driver development,
system integration, boot time and power consumption
optimization, etc.
Embedded Linux, Embedded Android and driver development
training, with materials freely available under a Creative
Commons license.
http://free-electrons.com
Contributor to Buildroot, a simple open-source embedded
build system
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 4
Outline
IIO, a new kernel subsystem
What is IIO ?
Definition
Current state in the kernel
Getting started
IIO device
IIO channels
Going further : Hardware triggers and buffers
Hardware Triggers
Buffers
Useful Resources
Conclusion
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 5
IIO
A subsystem for Analog to Digital Converters (ADCs) and
related hardwares (accelerometers, light sensors, gyroscopes),
but also DACs
Can be used on ADCs ranging from a SoC ADC to 100M
samples/sec industrial ADCs
Until recently, mostly focused on user-space abstraction with
no in-kernel API for other drivers
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 6
Current state in the kernel
Developed since 2009 by Jonathan Cameron
Being developed in the staging/ directory until it comes to
an high quality code and a mature API
It is now moving out of staging, one step at a time: first,
basic features, then the support for advanced IIO features.
Already has a lot of different hardware supports and drivers
for them, mostly from Analog Devices Inc, but also drivers for
Texas Instruments, Atmel, etc.
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 7
Outline
IIO, a new kernel subsystem
What is IIO ?
Definition
Current state in the kernel
Getting started
IIO device
IIO channels
Going further : Hardware triggers and buffers
Hardware Triggers
Buffers
Useful Resources
Conclusion
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 8
iio dev structure
Main structure of all IIO drivers
Holds informations about the device and the driver, such as :
How much channels are available on the device ?
What modes can the device operate in ?
What hooks are available for this driver ?
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 9
Allocate a new device
struct iio_dev *idev = iio_allocate_device)
sizeof(struct at91_adc_state));
Allocates a struct iio dev, along with the private data of your
driver
Does all the basic initialisation
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 10
Capture modes
idev->modes = INDIO_DIRECT_MODE;
Defines the mode of operations available for this device, to choose
between :
INDIO DIRECT MODE the device can operate using software
triggers
INDIO BUFFER TRIGGERED the device can use hardware
triggers
INDIO BUFFER HARDWARE the device has a hardware buffer
INDIO ALL BUFFER MODES union of the two above
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 11
IIO infos
static const struct iio_info at91_adc_info = {
.driver_module = THIS_MODULE,
.read_raw = &at91_adc_read_raw,
};
idev->info = &at91_adc_info;
Used to declare the hooks the core can use for this device
Lot of hooks available corresponding to interactions the user
can make through sysfs.
read_raw for example is called to request a value from the
driver. A bitmask allows us to know more precisely which type
of value is requested, and for which channel if needed. It can
be for example either the scale used to convert value returned
to volts or the value in itself.
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 12
Basic design of the driver
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 13
iio channels declaration
struct iio_chan_spec *chan = kmalloc(sizeof(struct iio_chan_spec),
GFP_KERNEL);
chan->type = IIO_VOLTAGE;
chan->indexed = 1;
chan->channel = 1;
chan->scan_type.sign = ’u’;
chan->scan_type.realbits = 10;
chan->scan_type.storagebits = 16;
chan->info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT;
idev->channels = chan;
idev->num_channels = 1;
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 14
Register the device
iio_device_register(idev);
this is sufficient to have a basic IIO device driver
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 15
Userspace API
If we look at /sys/bus/iio/devices/iio:deviceX, we should
have:
$ ls /sys/bus/iio/devices/iio:deviceX
in_voltage0_raw
in_voltage_scale
name
$
reading in voltage0 raw calls the read raw hook, with the
mask set to 0, and the chan argument set with the
iio chan spec structure corresponding to the the channel 0
reading in voltage scale calls the read raw hook, with the
mask set to IIO CHAN INFO SCALE
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 16
Outline
IIO, a new kernel subsystem
What is IIO ?
Definition
Current state in the kernel
Getting started
IIO device
IIO channels
Going further : Hardware triggers and buffers
Hardware Triggers
Buffers
Useful Resources
Conclusion
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 17
Hardware Triggers
IIO exposes API so that we can :
declare any given number of triggers
choose which channels we want enabled for conversions
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 18
Kernel API
static const struct iio_trigger_ops at91_adc_trigger_ops = {
.owner = THIS_MODULE,
.set_trigger_state = &at91_adc_configure_trigger,
};
struct iio_trigger *trig = iio_allocate_trigger("foo");
trig->ops = &at91_adc_trigger_ops;
iio_trigger_register(trig)
Once again, we have hooks to declare
These hooks are this time triggers-specific, so that we can
have a function called when the trigger state changes.
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 19
Poll Function
function triggered at each conversion
called through the functions iio_trigger_poll or
iio_trigger_poll_chained
basically, its job is to feed retrieve data from the device and
feed them into the buffer
IIO uses the IRQ model, so the poll function has the same
prototype than any other IRQ handler.
idev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
&at91_adc_trigger_handler,
IRQF_ONESHOT,
idev,
"foo");
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 20
Buffers
doesn’t make much sense to have triggered captures without
a buffer
Buffers and triggers are closely tied together in IIO
2 types of buffers in IIO
One relies on kfifo
The other one is a ring buffer
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 21
Allocate a buffer
in the latest code, if you want to use the IIO ring buffer, boiler
plate code has been added so it’s pretty straightforward.
ret = iio_sw_rb_simple_setup(idev,
&iio_pollfunc_store_time,
&at91_adc_trigger_handler);
Allocate the buffer, allocates the poll function, declares the
device as supporting triggered capture, register the buffer
against the core, etc
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 22
Userspace API 1/3: Set up the capture
If we look at /sys/bus/iio/devices/, we should now have in
addition to iio:deviceX:
$ ls /sys/bus/iio/devices/
iio:device0
trigger0
$ ls /sys/bus/iio/devices/iio:device0
buffer
scan_elements
trigger
$ ls /sys/bus/iio/devices/iio:device0/buffer
enabled length
$ ls /sys/bus/iio/devices/iio:device0/scan_elements
in_voltage0_en
in_voltage0_index
in_voltage0_type
$ ls /sys/bus/iio/devices/trigger0
name
$
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 23
Userspace API 2/3: Start and stop a capture
$ echo 1 > 
/sys/bus/iio/devices/iio:device0/scan_elements/in_voltage0_en
$ echo "foo" > 
/sys/bus/iio/devices/iio:device0/trigger/current_trigger
$ echo 100 > /sys/bus/iio/devices/iio:device0/buffer/length
$ echo 1 > /sys/bus/iio/devices/iio:device0/buffer/enable
$ echo 0 > /sys/bus/iio/devices/iio:device0/buffer/enable
$ echo "" > 
/sys/bus/iio/devices/iio:device0/trigger/current_trigger
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 24
Userspace API 3/3: Get the capture results
IIO also exposes a character device to get the converted
values: /dev/iio:deviceX
You just have to read in it to get data
Data are organized by chunks
Example: You have 4 channels, plus a timestamp one. All are
enabled except channel 2.
Channels Timestamp
Index 0 1 3
Size in bits 16 16 16 64
There is a program that provides a basic implementation and
a good way to test your driver in the IIO documentation
directory: drivers/staging/iio/Documentation/
generic_buffer.c
# ./generic-buffer -n at91_adc -t at91_adc-dev0-external
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 25
Outline
IIO, a new kernel subsystem
What is IIO ?
Definition
Current state in the kernel
Getting started
IIO device
IIO channels
Going further : Hardware triggers and buffers
Hardware Triggers
Buffers
Useful Resources
Conclusion
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 26
Useful Resources
drivers/staging/iio/Documentation
drivers/staging/iio/iio_simple_dummy.c
http://www.ohwr.org/projects/zio/wiki/Iio
http://www.at91.com/linux4sam/bin/view/Linux4SAM/
IioAdcDriver
linux-iio@vger.kernel.org
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 27
Outline
IIO, a new kernel subsystem
What is IIO ?
Definition
Current state in the kernel
Getting started
IIO device
IIO channels
Going further : Hardware triggers and buffers
Hardware Triggers
Buffers
Useful Resources
Conclusion
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 28
Conclusion
IIO is a nice subsystem to add ADCs and the like support
Still under heavy development, but also really opens to
changes and feedback
Yet reliable enough to be used in production
Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 29

Mais conteúdo relacionado

Mais procurados

Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequence
Houcheng Lin
 

Mais procurados (20)

Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
 
U Boot or Universal Bootloader
U Boot or Universal BootloaderU Boot or Universal Bootloader
U Boot or Universal Bootloader
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequence
 
Ubuntu
UbuntuUbuntu
Ubuntu
 
Block Drivers
Block DriversBlock Drivers
Block Drivers
 
Linux Kernel Tour
Linux Kernel TourLinux Kernel Tour
Linux Kernel Tour
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
Linux Serial Driver
Linux Serial DriverLinux Serial Driver
Linux Serial Driver
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
 
Linux Boot Process
Linux Boot ProcessLinux Boot Process
Linux Boot Process
 
Spi drivers
Spi driversSpi drivers
Spi drivers
 
Debian or Yocto Project? Which is the best for your Embedded Linux project?
Debian or Yocto Project? Which is the best for your Embedded Linux project?Debian or Yocto Project? Which is the best for your Embedded Linux project?
Debian or Yocto Project? Which is the best for your Embedded Linux project?
 
SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 
Linux booting Process
Linux booting ProcessLinux booting Process
Linux booting Process
 
Making Linux do Hard Real-time
Making Linux do Hard Real-timeMaking Linux do Hard Real-time
Making Linux do Hard Real-time
 
Power optimization for Android apps
Power optimization for Android appsPower optimization for Android apps
Power optimization for Android apps
 
Linux Initialization Process (1)
Linux Initialization Process (1)Linux Initialization Process (1)
Linux Initialization Process (1)
 
Run Qt on Linux embedded systems using Yocto
Run Qt on Linux embedded systems using YoctoRun Qt on Linux embedded systems using Yocto
Run Qt on Linux embedded systems using Yocto
 
Architecture Of The Linux Kernel
Architecture Of The Linux KernelArchitecture Of The Linux Kernel
Architecture Of The Linux Kernel
 
Board Bringup
Board BringupBoard Bringup
Board Bringup
 

Semelhante a 127 iio a-new-subsystem

Os Grossupdated
Os GrossupdatedOs Grossupdated
Os Grossupdated
oscon2007
 
How to Choose a Software Update Mechanism for Embedded Linux Devices
How to Choose a Software Update Mechanism for Embedded Linux DevicesHow to Choose a Software Update Mechanism for Embedded Linux Devices
How to Choose a Software Update Mechanism for Embedded Linux Devices
Leon Anavi
 
OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons
OWF12/PAUG Conf Days Android system development, maxime ripard, free electronsOWF12/PAUG Conf Days Android system development, maxime ripard, free electrons
OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons
Paris Open Source Summit
 
180 nm Tape out experience using Open POWER ISA
180 nm Tape out experience using Open POWER ISA180 nm Tape out experience using Open POWER ISA
180 nm Tape out experience using Open POWER ISA
Ganesan Narayanasamy
 
Petazzoni device-tree-dummies
Petazzoni device-tree-dummiesPetazzoni device-tree-dummies
Petazzoni device-tree-dummies
Alex Cherny
 

Semelhante a 127 iio a-new-subsystem (20)

Inria Tech Talk : RIOT, l'OS libre pour vos objets connectés #IoT
Inria Tech Talk : RIOT, l'OS libre pour vos objets connectés #IoTInria Tech Talk : RIOT, l'OS libre pour vos objets connectés #IoT
Inria Tech Talk : RIOT, l'OS libre pour vos objets connectés #IoT
 
Os Grossupdated
Os GrossupdatedOs Grossupdated
Os Grossupdated
 
TDC2016SP - Trilha Linux Embarcado
TDC2016SP - Trilha Linux EmbarcadoTDC2016SP - Trilha Linux Embarcado
TDC2016SP - Trilha Linux Embarcado
 
How to Choose a Software Update Mechanism for Embedded Linux Devices
How to Choose a Software Update Mechanism for Embedded Linux DevicesHow to Choose a Software Update Mechanism for Embedded Linux Devices
How to Choose a Software Update Mechanism for Embedded Linux Devices
 
Linux day 2016 Yocto Project
Linux day 2016 Yocto ProjectLinux day 2016 Yocto Project
Linux day 2016 Yocto Project
 
Kernel Recipes 2014 - Supporting a new ARM platform: the Allwinner example
Kernel Recipes 2014 - Supporting a new ARM platform: the Allwinner exampleKernel Recipes 2014 - Supporting a new ARM platform: the Allwinner example
Kernel Recipes 2014 - Supporting a new ARM platform: the Allwinner example
 
Kernel Recipes 2013 - ARM support in the Linux kernel
Kernel Recipes 2013 - ARM support in the Linux kernelKernel Recipes 2013 - ARM support in the Linux kernel
Kernel Recipes 2013 - ARM support in the Linux kernel
 
Kernel Recipes 2013 - Easy rootfs using Buildroot
Kernel Recipes 2013 - Easy rootfs using BuildrootKernel Recipes 2013 - Easy rootfs using Buildroot
Kernel Recipes 2013 - Easy rootfs using Buildroot
 
Userspace drivers-2016
Userspace drivers-2016Userspace drivers-2016
Userspace drivers-2016
 
OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons
OWF12/PAUG Conf Days Android system development, maxime ripard, free electronsOWF12/PAUG Conf Days Android system development, maxime ripard, free electrons
OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons
 
Kotlin Basics & Introduction to Jetpack Compose.pptx
Kotlin Basics & Introduction to Jetpack Compose.pptxKotlin Basics & Introduction to Jetpack Compose.pptx
Kotlin Basics & Introduction to Jetpack Compose.pptx
 
180 nm Tape out experience using Open POWER ISA
180 nm Tape out experience using Open POWER ISA180 nm Tape out experience using Open POWER ISA
180 nm Tape out experience using Open POWER ISA
 
Iit roorkee 2021
Iit roorkee 2021Iit roorkee 2021
Iit roorkee 2021
 
Quick and Easy Device Drivers for Embedded Linux Using UIO
Quick and Easy Device Drivers for Embedded Linux Using UIOQuick and Easy Device Drivers for Embedded Linux Using UIO
Quick and Easy Device Drivers for Embedded Linux Using UIO
 
The Fundamentals of Internet of Everything Connectivity
The Fundamentals of Internet of Everything ConnectivityThe Fundamentals of Internet of Everything Connectivity
The Fundamentals of Internet of Everything Connectivity
 
Linux internals v4
Linux internals v4Linux internals v4
Linux internals v4
 
Petazzoni device-tree-dummies
Petazzoni device-tree-dummiesPetazzoni device-tree-dummies
Petazzoni device-tree-dummies
 
Implementation of Cmos Camera Device Driver and Wifi Technology on S3c2440 Us...
Implementation of Cmos Camera Device Driver and Wifi Technology on S3c2440 Us...Implementation of Cmos Camera Device Driver and Wifi Technology on S3c2440 Us...
Implementation of Cmos Camera Device Driver and Wifi Technology on S3c2440 Us...
 
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B KuteUnit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
 
Software update for embedded systems
Software update for embedded systemsSoftware update for embedded systems
Software update for embedded systems
 

Último

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Último (20)

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 

127 iio a-new-subsystem

  • 1. IIO, a new kernel subsystem IIO, a new kernel subsystem Maxime Ripard Free Electrons c Copyright 2009-2012, Free Electrons. Creative Commons BY-SA 3.0 license. Latest update: February 14, 2012. Document sources, updates and translations: Corrections, suggestions, contributions and translations are welcome! Embedded Linux Developers Free Electrons Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 1
  • 2. Outline IIO, a new kernel subsystem What is IIO ? Definition Current state in the kernel Getting started IIO device IIO channels Going further : Hardware triggers and buffers Hardware Triggers Buffers Useful Resources Conclusion Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 3
  • 3. Maxime Ripard Embedded Linux engineer and trainer at Free Electrons since 2011 Embedded Linux development: kernel and driver development, system integration, boot time and power consumption optimization, etc. Embedded Linux, Embedded Android and driver development training, with materials freely available under a Creative Commons license. http://free-electrons.com Contributor to Buildroot, a simple open-source embedded build system Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 4
  • 4. Outline IIO, a new kernel subsystem What is IIO ? Definition Current state in the kernel Getting started IIO device IIO channels Going further : Hardware triggers and buffers Hardware Triggers Buffers Useful Resources Conclusion Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 5
  • 5. IIO A subsystem for Analog to Digital Converters (ADCs) and related hardwares (accelerometers, light sensors, gyroscopes), but also DACs Can be used on ADCs ranging from a SoC ADC to 100M samples/sec industrial ADCs Until recently, mostly focused on user-space abstraction with no in-kernel API for other drivers Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 6
  • 6. Current state in the kernel Developed since 2009 by Jonathan Cameron Being developed in the staging/ directory until it comes to an high quality code and a mature API It is now moving out of staging, one step at a time: first, basic features, then the support for advanced IIO features. Already has a lot of different hardware supports and drivers for them, mostly from Analog Devices Inc, but also drivers for Texas Instruments, Atmel, etc. Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 7
  • 7. Outline IIO, a new kernel subsystem What is IIO ? Definition Current state in the kernel Getting started IIO device IIO channels Going further : Hardware triggers and buffers Hardware Triggers Buffers Useful Resources Conclusion Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 8
  • 8. iio dev structure Main structure of all IIO drivers Holds informations about the device and the driver, such as : How much channels are available on the device ? What modes can the device operate in ? What hooks are available for this driver ? Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 9
  • 9. Allocate a new device struct iio_dev *idev = iio_allocate_device) sizeof(struct at91_adc_state)); Allocates a struct iio dev, along with the private data of your driver Does all the basic initialisation Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 10
  • 10. Capture modes idev->modes = INDIO_DIRECT_MODE; Defines the mode of operations available for this device, to choose between : INDIO DIRECT MODE the device can operate using software triggers INDIO BUFFER TRIGGERED the device can use hardware triggers INDIO BUFFER HARDWARE the device has a hardware buffer INDIO ALL BUFFER MODES union of the two above Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 11
  • 11. IIO infos static const struct iio_info at91_adc_info = { .driver_module = THIS_MODULE, .read_raw = &at91_adc_read_raw, }; idev->info = &at91_adc_info; Used to declare the hooks the core can use for this device Lot of hooks available corresponding to interactions the user can make through sysfs. read_raw for example is called to request a value from the driver. A bitmask allows us to know more precisely which type of value is requested, and for which channel if needed. It can be for example either the scale used to convert value returned to volts or the value in itself. Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 12
  • 12. Basic design of the driver Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 13
  • 13. iio channels declaration struct iio_chan_spec *chan = kmalloc(sizeof(struct iio_chan_spec), GFP_KERNEL); chan->type = IIO_VOLTAGE; chan->indexed = 1; chan->channel = 1; chan->scan_type.sign = ’u’; chan->scan_type.realbits = 10; chan->scan_type.storagebits = 16; chan->info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT; idev->channels = chan; idev->num_channels = 1; Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 14
  • 14. Register the device iio_device_register(idev); this is sufficient to have a basic IIO device driver Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 15
  • 15. Userspace API If we look at /sys/bus/iio/devices/iio:deviceX, we should have: $ ls /sys/bus/iio/devices/iio:deviceX in_voltage0_raw in_voltage_scale name $ reading in voltage0 raw calls the read raw hook, with the mask set to 0, and the chan argument set with the iio chan spec structure corresponding to the the channel 0 reading in voltage scale calls the read raw hook, with the mask set to IIO CHAN INFO SCALE Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 16
  • 16. Outline IIO, a new kernel subsystem What is IIO ? Definition Current state in the kernel Getting started IIO device IIO channels Going further : Hardware triggers and buffers Hardware Triggers Buffers Useful Resources Conclusion Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 17
  • 17. Hardware Triggers IIO exposes API so that we can : declare any given number of triggers choose which channels we want enabled for conversions Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 18
  • 18. Kernel API static const struct iio_trigger_ops at91_adc_trigger_ops = { .owner = THIS_MODULE, .set_trigger_state = &at91_adc_configure_trigger, }; struct iio_trigger *trig = iio_allocate_trigger("foo"); trig->ops = &at91_adc_trigger_ops; iio_trigger_register(trig) Once again, we have hooks to declare These hooks are this time triggers-specific, so that we can have a function called when the trigger state changes. Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 19
  • 19. Poll Function function triggered at each conversion called through the functions iio_trigger_poll or iio_trigger_poll_chained basically, its job is to feed retrieve data from the device and feed them into the buffer IIO uses the IRQ model, so the poll function has the same prototype than any other IRQ handler. idev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time, &at91_adc_trigger_handler, IRQF_ONESHOT, idev, "foo"); Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 20
  • 20. Buffers doesn’t make much sense to have triggered captures without a buffer Buffers and triggers are closely tied together in IIO 2 types of buffers in IIO One relies on kfifo The other one is a ring buffer Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 21
  • 21. Allocate a buffer in the latest code, if you want to use the IIO ring buffer, boiler plate code has been added so it’s pretty straightforward. ret = iio_sw_rb_simple_setup(idev, &iio_pollfunc_store_time, &at91_adc_trigger_handler); Allocate the buffer, allocates the poll function, declares the device as supporting triggered capture, register the buffer against the core, etc Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 22
  • 22. Userspace API 1/3: Set up the capture If we look at /sys/bus/iio/devices/, we should now have in addition to iio:deviceX: $ ls /sys/bus/iio/devices/ iio:device0 trigger0 $ ls /sys/bus/iio/devices/iio:device0 buffer scan_elements trigger $ ls /sys/bus/iio/devices/iio:device0/buffer enabled length $ ls /sys/bus/iio/devices/iio:device0/scan_elements in_voltage0_en in_voltage0_index in_voltage0_type $ ls /sys/bus/iio/devices/trigger0 name $ Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 23
  • 23. Userspace API 2/3: Start and stop a capture $ echo 1 > /sys/bus/iio/devices/iio:device0/scan_elements/in_voltage0_en $ echo "foo" > /sys/bus/iio/devices/iio:device0/trigger/current_trigger $ echo 100 > /sys/bus/iio/devices/iio:device0/buffer/length $ echo 1 > /sys/bus/iio/devices/iio:device0/buffer/enable $ echo 0 > /sys/bus/iio/devices/iio:device0/buffer/enable $ echo "" > /sys/bus/iio/devices/iio:device0/trigger/current_trigger Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 24
  • 24. Userspace API 3/3: Get the capture results IIO also exposes a character device to get the converted values: /dev/iio:deviceX You just have to read in it to get data Data are organized by chunks Example: You have 4 channels, plus a timestamp one. All are enabled except channel 2. Channels Timestamp Index 0 1 3 Size in bits 16 16 16 64 There is a program that provides a basic implementation and a good way to test your driver in the IIO documentation directory: drivers/staging/iio/Documentation/ generic_buffer.c # ./generic-buffer -n at91_adc -t at91_adc-dev0-external Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 25
  • 25. Outline IIO, a new kernel subsystem What is IIO ? Definition Current state in the kernel Getting started IIO device IIO channels Going further : Hardware triggers and buffers Hardware Triggers Buffers Useful Resources Conclusion Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 26
  • 27. Outline IIO, a new kernel subsystem What is IIO ? Definition Current state in the kernel Getting started IIO device IIO channels Going further : Hardware triggers and buffers Hardware Triggers Buffers Useful Resources Conclusion Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 28
  • 28. Conclusion IIO is a nice subsystem to add ADCs and the like support Still under heavy development, but also really opens to changes and feedback Yet reliable enough to be used in production Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 29