SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Downloaden Sie, um offline zu lesen
How to avoid writing device drivers
for embedded Linux
Embedded World 2016
How to avoid writing device drivers for embedded Linux 1 Copyright © 2011-2016, 2net Ltd
License
These slides are available under a Creative Commons Attribution-ShareAlike 3.0
license. You can read the full text of the license here
http://creativecommons.org/licenses/by-sa/3.0/legalcode
You are free to
• copy, distribute, display, and perform the work
• make derivative works
• make commercial use of the work
Under the following conditions
• Attribution: you must give the original author credit
• Share Alike: if you alter, transform, or build upon this work, you may distribute
the resulting work only under a license identical to this one (i.e. include this
page exactly as it is)
• For any reuse or distribution, you must make clear to others the license terms of
this work
How to avoid writing device drivers for embedded Linux 2 Copyright © 2011-2016, 2net Ltd
About Chris Simmonds
• Consultant and trainer
• Author of Mastering Embedded Linux
Programming
• Working with embedded Linux since 1999
• Android since 2009
• Speaker at many conferences and
workshops
"Looking after the Inner Penguin" blog at http://2net.co.uk/
https://uk.linkedin.com/in/chrisdsimmonds/
https://google.com/+chrissimmonds
How to avoid writing device drivers for embedded Linux 3 Copyright © 2011-2016, 2net Ltd
Conventional device driver model
User
space
System call handler
Generic services
Device drivers
Hardware
Application
C library
interrupts
Linux
kernel
How to avoid writing device drivers for embedded Linux 4 Copyright © 2011-2016, 2net Ltd
How applications call device drivers
• In Linux, everything is a file 1
• Applications interact with drivers via POSIX functions
open(2), read(2), write(2), ioctl(2), etc
• There are two types of interface
• 1. Device nodes in /dev
• The serial driver, ttyS is an example
• Device nodes are named /dev/ttyS0, /dev/ttyS1 ...
• 2. Driver attributes, exported via sysfs
• For example /sys/class/gpio
1Except network interfaces, which are sockets
How to avoid writing device drivers for embedded Linux 5 Copyright © 2011-2016, 2net Ltd
Userspace drivers
• Writing kernel device drivers can be difficult
• Luckily, there are generic drivers that that allow you to
write most of the code in userspace
• We will look at three
• GPIO
• PWM
• I2C
• Note: applications will need read/write permissions
for the files. Consequently, they usually have to run
as user root
How to avoid writing device drivers for embedded Linux 6 Copyright © 2011-2016, 2net Ltd
/sys/class/gpio
# ls /sys/class/gpio/
export gpiochip0 gpiochip32 gpiochip64 gpiochip96 unexport
This device has 4 gpio chips
each with 32 pins
Write to this
file to export
a GPIO pin
to user space
Write to this
file to unexport
a GPIO pin
to user space
How to avoid writing device drivers for embedded Linux 7 Copyright © 2011-2016, 2net Ltd
gpiochip
# /sys/class/gpio/gpiochip0
base device label ngpio power subsystem uevent
The number of GPIO pins (32)
A lable to identify the chip
(gpiochip0)
The starting GPIO number (0)
How to avoid writing device drivers for embedded Linux 8 Copyright © 2011-2016, 2net Ltd
Exporting a GPIO pin
# echo 42 > /sys/class/gpio/export
# ls /sys/class/gpio
export gpio42 gpiochip0 gpiochip32 gpiochip64 gpiochip96 unexport
If the export is successful, a new
directory is created
How to avoid writing device drivers for embedded Linux 9 Copyright © 2011-2016, 2net Ltd
Inputs and outputs
# ls /sys/class/gpio/gpio42
active_low device direction edge power subsystem uevent value
Set to 1 to invert
input and ouput
Set direction by
writing "out" or
"in". Default "in"
The logic level of the
pin. Change the level
of outputs by writing
"0" or "1"
How to avoid writing device drivers for embedded Linux 10 Copyright © 2011-2016, 2net Ltd
Interrupts
• If the GPIO can generate interrupts, the file edge can
be used to control interrupt handling
• edge = ["none", "rising", "falling","both]
• For example, to make GPIO60 interrupt on falling
edge:
• echo falling > /sys/class/gpio/gpio60/edge
• To wait for an interrupt, use the poll(2) function
• Example on next slide
How to avoid writing device drivers for embedded Linux 11 Copyright © 2011-2016, 2net Ltd
GPIO interrupt code example
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
int main (int argc, char *argv[])
{
int f;
struct pollfd poll_fds [1];
int ret;
char value[4];
f = open("/sys/class/gpio/gpio60/value", O_RDONLY);
poll_fds[0].fd = f;
poll_fds[0].events = POLLPRI | POLLERR;
while (1) {
if (poll(poll_fds, 1, -1) > 0) {
read(f, &value, sizeof(value));
printf("Interrupt! value=%cn", value[0]);
}
}
}
Code: https://github.com/csimmonds/userspace-io-ew2016
How to avoid writing device drivers for embedded Linux 12 Copyright © 2011-2016, 2net Ltd
PWM
# echo 6 > /sys/class/pwm/export
# ls /sys/class/pwm
export pwm6 pwmchip0 pwmchip2 pwmchip3 pwmchip5 pwmchip7 unexport
If the export is successful, a new
directory is created
# ls /sys/class/pwm/pwm6/
device duty_ns period_ns polarity power run subsystem uevent
period_ns
duty_ns
How to avoid writing device drivers for embedded Linux 13 Copyright © 2011-2016, 2net Ltd
I2C
• Device nodes, one per I2C bus controller:
# ls -l /dev/i2c*
crw-rw---T 1 root i2c 89, 0 Jan 1 2000 /dev/i2c-0
crw-rw---T 1 root i2c 89, 1 Jan 1 2000 /dev/i2c-1
• Some functions are implemented using ioctl(2), using
commands and structures defined in
usr/include/linux/i2c-dev.h
How to avoid writing device drivers for embedded Linux 14 Copyright © 2011-2016, 2net Ltd
i2c-utils
• Command-line tools for interacting with I2C devices
• i2cdetect - list I2C adapters and probe bus
• i2cget - read data from an I2C device
• i2cset - write data to an I2C device
How to avoid writing device drivers for embedded Linux 15 Copyright © 2011-2016, 2net Ltd
i2cdetect
• i2cdetect - list i2c adapters and probe bus
• Example: detect devices on bus 1 (/dev/i2c-1)
# i2cdetect -y -r 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- 39 -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- UU UU UU UU -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
UU = device already handled by kernel driver
0x39 = device discovered at address 0x39
How to avoid writing device drivers for embedded Linux 16 Copyright © 2011-2016, 2net Ltd
i2cget/i2cset
• i2cget <bus> <chip> <register>: read data from an I2C
device
• Example: read register 0x8a from device at 0x39
# i2cget -y 1 0x39 0x8a
0x50
• i2cset <bus> <chip> <register>: writedata to an I2C
device
• Example: Write 0x03 to register 0x80:
# i2cset -y 1 0x39 0x80 3
How to avoid writing device drivers for embedded Linux 17 Copyright © 2011-2016, 2net Ltd
I2C code example
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
int main(int argc, char **argv)
int f;
char buf[4];
f = open("/dev/i2c-1", O_RDWR);
ioctl(f, I2C_SLAVE, 0x39) < 0) {
buf[0] = 0x8a; /* Chip ID register */
write(f, buf, 1);
read(f, buf, 1);
printf("ID 0x%xn", buf [0]);
}
Code: https://github.com/csimmonds/userspace-io-ew2016
How to avoid writing device drivers for embedded Linux 18 Copyright © 2011-2016, 2net Ltd
Other examples
• SPI: access SPI devices via device nodes
/dev/spidev*
• USB: access USB devices via libusb
• User defined I/O: UIO
• Generic kernel driver that allows you to write
userspace drivers
• access device registers and handle interrupts from
userspace
How to avoid writing device drivers for embedded Linux 19 Copyright © 2011-2016, 2net Ltd
Delving deeper
• This is an excerpt from my Fast track to embedded
Linux class
• If you would like to discover more about the power of
embedded Linux, visit
http://www.2net.co.uk/training.html and enquire
about training classes for your company
• 2net training is available world-wide
• Also, my book, Mastering Embedded Linux
Programming, covers the topics discused here in
much greater detail
How to avoid writing device drivers for embedded Linux 20 Copyright © 2011-2016, 2net Ltd

Weitere ähnliche Inhalte

Was ist angesagt?

Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Jian-Hong Pan
 
Understanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicUnderstanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicJoseph Lu
 
用Raspberry Pi 學Linux I2C Driver
用Raspberry Pi 學Linux I2C Driver用Raspberry Pi 學Linux I2C Driver
用Raspberry Pi 學Linux I2C Driver艾鍗科技
 
Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionGene Chang
 
Prerequisite knowledge for shared memory concurrency
Prerequisite knowledge for shared memory concurrencyPrerequisite knowledge for shared memory concurrency
Prerequisite knowledge for shared memory concurrencyViller Hsiao
 
Slab Allocator in Linux Kernel
Slab Allocator in Linux KernelSlab Allocator in Linux Kernel
Slab Allocator in Linux KernelAdrian Huang
 
Marco Cavallini @ LinuxLab 2018 : Workshop Yocto Project, an automatic genera...
Marco Cavallini @ LinuxLab 2018 : Workshop Yocto Project, an automatic genera...Marco Cavallini @ LinuxLab 2018 : Workshop Yocto Project, an automatic genera...
Marco Cavallini @ LinuxLab 2018 : Workshop Yocto Project, an automatic genera...Marco Cavallini
 
Read-only rootfs: theory and practice
Read-only rootfs: theory and practiceRead-only rootfs: theory and practice
Read-only rootfs: theory and practiceChris Simmonds
 
Linux Performance Tools
Linux Performance ToolsLinux Performance Tools
Linux Performance ToolsBrendan Gregg
 
Kernel_Crash_Dump_Analysis
Kernel_Crash_Dump_AnalysisKernel_Crash_Dump_Analysis
Kernel_Crash_Dump_AnalysisBuland Singh
 
Yocto project and open embedded training
Yocto project and open embedded trainingYocto project and open embedded training
Yocto project and open embedded trainingH Ming
 
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is bootedVmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is bootedAdrian Huang
 

Was ist angesagt? (20)

USB Drivers
USB DriversUSB Drivers
USB Drivers
 
Toolchain
ToolchainToolchain
Toolchain
 
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021
 
Understanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicUnderstanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panic
 
Memory model
Memory modelMemory model
Memory model
 
Linux-Internals-and-Networking
Linux-Internals-and-NetworkingLinux-Internals-and-Networking
Linux-Internals-and-Networking
 
用Raspberry Pi 學Linux I2C Driver
用Raspberry Pi 學Linux I2C Driver用Raspberry Pi 學Linux I2C Driver
用Raspberry Pi 學Linux I2C Driver
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introduction
 
Prerequisite knowledge for shared memory concurrency
Prerequisite knowledge for shared memory concurrencyPrerequisite knowledge for shared memory concurrency
Prerequisite knowledge for shared memory concurrency
 
U-Boot - An universal bootloader
U-Boot - An universal bootloader U-Boot - An universal bootloader
U-Boot - An universal bootloader
 
Video Drivers
Video DriversVideo Drivers
Video Drivers
 
Slab Allocator in Linux Kernel
Slab Allocator in Linux KernelSlab Allocator in Linux Kernel
Slab Allocator in Linux Kernel
 
Marco Cavallini @ LinuxLab 2018 : Workshop Yocto Project, an automatic genera...
Marco Cavallini @ LinuxLab 2018 : Workshop Yocto Project, an automatic genera...Marco Cavallini @ LinuxLab 2018 : Workshop Yocto Project, an automatic genera...
Marco Cavallini @ LinuxLab 2018 : Workshop Yocto Project, an automatic genera...
 
Block Drivers
Block DriversBlock Drivers
Block Drivers
 
Read-only rootfs: theory and practice
Read-only rootfs: theory and practiceRead-only rootfs: theory and practice
Read-only rootfs: theory and practice
 
Linux Performance Tools
Linux Performance ToolsLinux Performance Tools
Linux Performance Tools
 
Kernel_Crash_Dump_Analysis
Kernel_Crash_Dump_AnalysisKernel_Crash_Dump_Analysis
Kernel_Crash_Dump_Analysis
 
Yocto project and open embedded training
Yocto project and open embedded trainingYocto project and open embedded training
Yocto project and open embedded training
 
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is bootedVmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
 

Andere mochten auch

Software update for IoT Embedded World 2017
Software update for IoT Embedded World 2017Software update for IoT Embedded World 2017
Software update for IoT Embedded World 2017Chris Simmonds
 
Software update for IoT: the current state of play
Software update for IoT: the current state of playSoftware update for IoT: the current state of play
Software update for IoT: the current state of playChris Simmonds
 
Linux field-update-2015
Linux field-update-2015Linux field-update-2015
Linux field-update-2015Chris Simmonds
 
The end of embedded Linux (as we know it)
The end of embedded Linux (as we know it)The end of embedded Linux (as we know it)
The end of embedded Linux (as we know it)Chris Simmonds
 
10 ways hardware engineers can make software integration easier
10 ways hardware engineers can make software integration easier10 ways hardware engineers can make software integration easier
10 ways hardware engineers can make software integration easierChris Simmonds
 
Device Drivers
Device DriversDevice Drivers
Device DriversSuhas S R
 
Booting Android: bootloaders, fastboot and boot images
Booting Android: bootloaders, fastboot and boot imagesBooting Android: bootloaders, fastboot and boot images
Booting Android: bootloaders, fastboot and boot imagesChris Simmonds
 
A timeline for embedded Linux
A timeline for embedded LinuxA timeline for embedded Linux
A timeline for embedded LinuxChris Simmonds
 
OTT in Azerbaijan - Project Brief
OTT in Azerbaijan - Project BriefOTT in Azerbaijan - Project Brief
OTT in Azerbaijan - Project BriefFarhad Shahrivar
 
Embedded linux barco-20121001
Embedded linux barco-20121001Embedded linux barco-20121001
Embedded linux barco-20121001Marc Leeman
 
Assistencia geologica
Assistencia geologicaAssistencia geologica
Assistencia geologicacrom68
 
The move from a hardware centric design to a software centric design: GStream...
The move from a hardware centric design to a software centric design: GStream...The move from a hardware centric design to a software centric design: GStream...
The move from a hardware centric design to a software centric design: GStream...Marc Leeman
 
Ensoft dvb 1
Ensoft dvb 1Ensoft dvb 1
Ensoft dvb 1sarge
 
sasikumarj_resume
sasikumarj_resumesasikumarj_resume
sasikumarj_resumeSasi Kumar
 
Capria no_video_ship_detection_with_dvbt_software_defined_passive_radar
 Capria no_video_ship_detection_with_dvbt_software_defined_passive_radar Capria no_video_ship_detection_with_dvbt_software_defined_passive_radar
Capria no_video_ship_detection_with_dvbt_software_defined_passive_radargrssieee
 
Buildin a small linux kernel
Buildin a small linux kernelBuildin a small linux kernel
Buildin a small linux kerneltrx2001
 
Standard java coding convention
Standard java coding conventionStandard java coding convention
Standard java coding conventionTam Thanh
 

Andere mochten auch (20)

Software update for IoT Embedded World 2017
Software update for IoT Embedded World 2017Software update for IoT Embedded World 2017
Software update for IoT Embedded World 2017
 
Software update for IoT: the current state of play
Software update for IoT: the current state of playSoftware update for IoT: the current state of play
Software update for IoT: the current state of play
 
Linux field-update-2015
Linux field-update-2015Linux field-update-2015
Linux field-update-2015
 
The end of embedded Linux (as we know it)
The end of embedded Linux (as we know it)The end of embedded Linux (as we know it)
The end of embedded Linux (as we know it)
 
10 ways hardware engineers can make software integration easier
10 ways hardware engineers can make software integration easier10 ways hardware engineers can make software integration easier
10 ways hardware engineers can make software integration easier
 
Device Drivers
Device DriversDevice Drivers
Device Drivers
 
Booting Android: bootloaders, fastboot and boot images
Booting Android: bootloaders, fastboot and boot imagesBooting Android: bootloaders, fastboot and boot images
Booting Android: bootloaders, fastboot and boot images
 
A timeline for embedded Linux
A timeline for embedded LinuxA timeline for embedded Linux
A timeline for embedded Linux
 
OTT in Azerbaijan - Project Brief
OTT in Azerbaijan - Project BriefOTT in Azerbaijan - Project Brief
OTT in Azerbaijan - Project Brief
 
How To Handle An IRD Audit - Atainz
How To Handle An IRD Audit - AtainzHow To Handle An IRD Audit - Atainz
How To Handle An IRD Audit - Atainz
 
Embedded linux barco-20121001
Embedded linux barco-20121001Embedded linux barco-20121001
Embedded linux barco-20121001
 
Assistencia geologica
Assistencia geologicaAssistencia geologica
Assistencia geologica
 
The move from a hardware centric design to a software centric design: GStream...
The move from a hardware centric design to a software centric design: GStream...The move from a hardware centric design to a software centric design: GStream...
The move from a hardware centric design to a software centric design: GStream...
 
Linux Workshop , Day 3
Linux Workshop , Day 3Linux Workshop , Day 3
Linux Workshop , Day 3
 
Ensoft dvb 1
Ensoft dvb 1Ensoft dvb 1
Ensoft dvb 1
 
sasikumarj_resume
sasikumarj_resumesasikumarj_resume
sasikumarj_resume
 
DVB-T/H Solution
DVB-T/H  SolutionDVB-T/H  Solution
DVB-T/H Solution
 
Capria no_video_ship_detection_with_dvbt_software_defined_passive_radar
 Capria no_video_ship_detection_with_dvbt_software_defined_passive_radar Capria no_video_ship_detection_with_dvbt_software_defined_passive_radar
Capria no_video_ship_detection_with_dvbt_software_defined_passive_radar
 
Buildin a small linux kernel
Buildin a small linux kernelBuildin a small linux kernel
Buildin a small linux kernel
 
Standard java coding convention
Standard java coding conventionStandard java coding convention
Standard java coding convention
 

Ähnlich wie Userspace drivers-2016

Lab Handson: Power your Creations with Intel Edison!
Lab Handson: Power your Creations with Intel Edison!Lab Handson: Power your Creations with Intel Edison!
Lab Handson: Power your Creations with Intel Edison!Codemotion
 
IoT Getting Started with Intel® IoT Devkit
IoT Getting Started with Intel® IoT DevkitIoT Getting Started with Intel® IoT Devkit
IoT Getting Started with Intel® IoT DevkitVasily Ryzhonkov
 
Srikanth_PILLI_CV_latest
Srikanth_PILLI_CV_latestSrikanth_PILLI_CV_latest
Srikanth_PILLI_CV_latestSrikanth Pilli
 
Why the yocto project for my io t project elc_edinburgh_2018
Why the yocto project for my io t project elc_edinburgh_2018Why the yocto project for my io t project elc_edinburgh_2018
Why the yocto project for my io t project elc_edinburgh_2018Mender.io
 
BKK16-211 Internet of Tiny Linux (io tl)- Status and Progress
BKK16-211 Internet of Tiny Linux (io tl)- Status and ProgressBKK16-211 Internet of Tiny Linux (io tl)- Status and Progress
BKK16-211 Internet of Tiny Linux (io tl)- Status and ProgressLinaro
 
Getting started with Intel IoT Developer Kit
Getting started with Intel IoT Developer KitGetting started with Intel IoT Developer Kit
Getting started with Intel IoT Developer KitSulamita Garcia
 
Introduction to Arduino.pptx
Introduction to Arduino.pptxIntroduction to Arduino.pptx
Introduction to Arduino.pptxAkshat Bijronia
 
Tac Presentation October 72014- Raspberry PI
Tac Presentation October 72014- Raspberry PITac Presentation October 72014- Raspberry PI
Tac Presentation October 72014- Raspberry PICliff Samuels Jr.
 
pcDuino Presentation at SparkFun
pcDuino Presentation at SparkFunpcDuino Presentation at SparkFun
pcDuino Presentation at SparkFunJingfeng Liu
 
Unit III ARM Interface and ARM Programming
Unit III ARM Interface and ARM Programming Unit III ARM Interface and ARM Programming
Unit III ARM Interface and ARM Programming Dr. Pankaj Zope
 
Deploy STM32 family on Zephyr - SFO17-102
Deploy STM32 family on Zephyr - SFO17-102Deploy STM32 family on Zephyr - SFO17-102
Deploy STM32 family on Zephyr - SFO17-102Linaro
 
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...First Steps Developing Embedded Applications using Heterogeneous Multi-core P...
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...Toradex
 
Meet the Experts: InfluxDB Product Update
Meet the Experts: InfluxDB Product UpdateMeet the Experts: InfluxDB Product Update
Meet the Experts: InfluxDB Product UpdateInfluxData
 
HKG15-300: Art's Quick Compiler: An unofficial overview
HKG15-300: Art's Quick Compiler: An unofficial overviewHKG15-300: Art's Quick Compiler: An unofficial overview
HKG15-300: Art's Quick Compiler: An unofficial overviewLinaro
 

Ähnlich wie Userspace drivers-2016 (20)

Lab Handson: Power your Creations with Intel Edison!
Lab Handson: Power your Creations with Intel Edison!Lab Handson: Power your Creations with Intel Edison!
Lab Handson: Power your Creations with Intel Edison!
 
Intel galileo
Intel galileoIntel galileo
Intel galileo
 
IoT Getting Started with Intel® IoT Devkit
IoT Getting Started with Intel® IoT DevkitIoT Getting Started with Intel® IoT Devkit
IoT Getting Started with Intel® IoT Devkit
 
Начало работы с Intel IoT Dev Kit
Начало работы с Intel IoT Dev KitНачало работы с Intel IoT Dev Kit
Начало работы с Intel IoT Dev Kit
 
Hardware hacking
Hardware hackingHardware hacking
Hardware hacking
 
Srikanth_PILLI_CV_latest
Srikanth_PILLI_CV_latestSrikanth_PILLI_CV_latest
Srikanth_PILLI_CV_latest
 
STM -32
STM -32STM -32
STM -32
 
learning STM -32
learning STM -32 learning STM -32
learning STM -32
 
Why the yocto project for my io t project elc_edinburgh_2018
Why the yocto project for my io t project elc_edinburgh_2018Why the yocto project for my io t project elc_edinburgh_2018
Why the yocto project for my io t project elc_edinburgh_2018
 
BKK16-211 Internet of Tiny Linux (io tl)- Status and Progress
BKK16-211 Internet of Tiny Linux (io tl)- Status and ProgressBKK16-211 Internet of Tiny Linux (io tl)- Status and Progress
BKK16-211 Internet of Tiny Linux (io tl)- Status and Progress
 
Getting started with Intel IoT Developer Kit
Getting started with Intel IoT Developer KitGetting started with Intel IoT Developer Kit
Getting started with Intel IoT Developer Kit
 
Introduction to Arduino.pptx
Introduction to Arduino.pptxIntroduction to Arduino.pptx
Introduction to Arduino.pptx
 
Tac Presentation October 72014- Raspberry PI
Tac Presentation October 72014- Raspberry PITac Presentation October 72014- Raspberry PI
Tac Presentation October 72014- Raspberry PI
 
pcDuino Presentation at SparkFun
pcDuino Presentation at SparkFunpcDuino Presentation at SparkFun
pcDuino Presentation at SparkFun
 
Unit III ARM Interface and ARM Programming
Unit III ARM Interface and ARM Programming Unit III ARM Interface and ARM Programming
Unit III ARM Interface and ARM Programming
 
Deploy STM32 family on Zephyr - SFO17-102
Deploy STM32 family on Zephyr - SFO17-102Deploy STM32 family on Zephyr - SFO17-102
Deploy STM32 family on Zephyr - SFO17-102
 
Introduction of Arduino Uno
Introduction of Arduino UnoIntroduction of Arduino Uno
Introduction of Arduino Uno
 
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...First Steps Developing Embedded Applications using Heterogeneous Multi-core P...
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...
 
Meet the Experts: InfluxDB Product Update
Meet the Experts: InfluxDB Product UpdateMeet the Experts: InfluxDB Product Update
Meet the Experts: InfluxDB Product Update
 
HKG15-300: Art's Quick Compiler: An unofficial overview
HKG15-300: Art's Quick Compiler: An unofficial overviewHKG15-300: Art's Quick Compiler: An unofficial overview
HKG15-300: Art's Quick Compiler: An unofficial overview
 

Mehr von Chris Simmonds

Debugging embedded devices using GDB
Debugging embedded devices using GDBDebugging embedded devices using GDB
Debugging embedded devices using GDBChris Simmonds
 
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?Chris Simmonds
 
Embedded Linux Quick Start Guide v1.5
Embedded Linux Quick Start Guide v1.5Embedded Linux Quick Start Guide v1.5
Embedded Linux Quick Start Guide v1.5Chris Simmonds
 
Running Android on the Raspberry Pi: Android Pie meets Raspberry Pi
Running Android on the Raspberry Pi: Android Pie meets Raspberry PiRunning Android on the Raspberry Pi: Android Pie meets Raspberry Pi
Running Android on the Raspberry Pi: Android Pie meets Raspberry PiChris Simmonds
 
Reducing the boot time of Linux devices
Reducing the boot time of Linux devicesReducing the boot time of Linux devices
Reducing the boot time of Linux devicesChris Simmonds
 
Android rpi-csimmonds-fosdem-2019
Android rpi-csimmonds-fosdem-2019Android rpi-csimmonds-fosdem-2019
Android rpi-csimmonds-fosdem-2019Chris Simmonds
 
Reducing boot time in embedded Linux
Reducing boot time in embedded LinuxReducing boot time in embedded Linux
Reducing boot time in embedded LinuxChris Simmonds
 
Linux power management: are you doing it right?
Linux power management: are you doing it right?Linux power management: are you doing it right?
Linux power management: are you doing it right?Chris Simmonds
 
Embedded Android: Android beyond the smartphone
Embedded Android: Android beyond the smartphoneEmbedded Android: Android beyond the smartphone
Embedded Android: Android beyond the smartphoneChris Simmonds
 
Android beyond the smartphone
Android beyond the smartphoneAndroid beyond the smartphone
Android beyond the smartphoneChris Simmonds
 
Tuning Android for low RAM
Tuning Android for low RAMTuning Android for low RAM
Tuning Android for low RAMChris Simmonds
 
The Android graphics path, in depth
The Android graphics path, in depthThe Android graphics path, in depth
The Android graphics path, in depthChris Simmonds
 

Mehr von Chris Simmonds (12)

Debugging embedded devices using GDB
Debugging embedded devices using GDBDebugging embedded devices using GDB
Debugging embedded devices using GDB
 
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?
 
Embedded Linux Quick Start Guide v1.5
Embedded Linux Quick Start Guide v1.5Embedded Linux Quick Start Guide v1.5
Embedded Linux Quick Start Guide v1.5
 
Running Android on the Raspberry Pi: Android Pie meets Raspberry Pi
Running Android on the Raspberry Pi: Android Pie meets Raspberry PiRunning Android on the Raspberry Pi: Android Pie meets Raspberry Pi
Running Android on the Raspberry Pi: Android Pie meets Raspberry Pi
 
Reducing the boot time of Linux devices
Reducing the boot time of Linux devicesReducing the boot time of Linux devices
Reducing the boot time of Linux devices
 
Android rpi-csimmonds-fosdem-2019
Android rpi-csimmonds-fosdem-2019Android rpi-csimmonds-fosdem-2019
Android rpi-csimmonds-fosdem-2019
 
Reducing boot time in embedded Linux
Reducing boot time in embedded LinuxReducing boot time in embedded Linux
Reducing boot time in embedded Linux
 
Linux power management: are you doing it right?
Linux power management: are you doing it right?Linux power management: are you doing it right?
Linux power management: are you doing it right?
 
Embedded Android: Android beyond the smartphone
Embedded Android: Android beyond the smartphoneEmbedded Android: Android beyond the smartphone
Embedded Android: Android beyond the smartphone
 
Android beyond the smartphone
Android beyond the smartphoneAndroid beyond the smartphone
Android beyond the smartphone
 
Tuning Android for low RAM
Tuning Android for low RAMTuning Android for low RAM
Tuning Android for low RAM
 
The Android graphics path, in depth
The Android graphics path, in depthThe Android graphics path, in depth
The Android graphics path, in depth
 

Kürzlich hochgeladen

Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 

Kürzlich hochgeladen (20)

Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 

Userspace drivers-2016

  • 1. How to avoid writing device drivers for embedded Linux Embedded World 2016 How to avoid writing device drivers for embedded Linux 1 Copyright © 2011-2016, 2net Ltd
  • 2. License These slides are available under a Creative Commons Attribution-ShareAlike 3.0 license. You can read the full text of the license here http://creativecommons.org/licenses/by-sa/3.0/legalcode You are free to • copy, distribute, display, and perform the work • make derivative works • make commercial use of the work Under the following conditions • Attribution: you must give the original author credit • Share Alike: if you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one (i.e. include this page exactly as it is) • For any reuse or distribution, you must make clear to others the license terms of this work How to avoid writing device drivers for embedded Linux 2 Copyright © 2011-2016, 2net Ltd
  • 3. About Chris Simmonds • Consultant and trainer • Author of Mastering Embedded Linux Programming • Working with embedded Linux since 1999 • Android since 2009 • Speaker at many conferences and workshops "Looking after the Inner Penguin" blog at http://2net.co.uk/ https://uk.linkedin.com/in/chrisdsimmonds/ https://google.com/+chrissimmonds How to avoid writing device drivers for embedded Linux 3 Copyright © 2011-2016, 2net Ltd
  • 4. Conventional device driver model User space System call handler Generic services Device drivers Hardware Application C library interrupts Linux kernel How to avoid writing device drivers for embedded Linux 4 Copyright © 2011-2016, 2net Ltd
  • 5. How applications call device drivers • In Linux, everything is a file 1 • Applications interact with drivers via POSIX functions open(2), read(2), write(2), ioctl(2), etc • There are two types of interface • 1. Device nodes in /dev • The serial driver, ttyS is an example • Device nodes are named /dev/ttyS0, /dev/ttyS1 ... • 2. Driver attributes, exported via sysfs • For example /sys/class/gpio 1Except network interfaces, which are sockets How to avoid writing device drivers for embedded Linux 5 Copyright © 2011-2016, 2net Ltd
  • 6. Userspace drivers • Writing kernel device drivers can be difficult • Luckily, there are generic drivers that that allow you to write most of the code in userspace • We will look at three • GPIO • PWM • I2C • Note: applications will need read/write permissions for the files. Consequently, they usually have to run as user root How to avoid writing device drivers for embedded Linux 6 Copyright © 2011-2016, 2net Ltd
  • 7. /sys/class/gpio # ls /sys/class/gpio/ export gpiochip0 gpiochip32 gpiochip64 gpiochip96 unexport This device has 4 gpio chips each with 32 pins Write to this file to export a GPIO pin to user space Write to this file to unexport a GPIO pin to user space How to avoid writing device drivers for embedded Linux 7 Copyright © 2011-2016, 2net Ltd
  • 8. gpiochip # /sys/class/gpio/gpiochip0 base device label ngpio power subsystem uevent The number of GPIO pins (32) A lable to identify the chip (gpiochip0) The starting GPIO number (0) How to avoid writing device drivers for embedded Linux 8 Copyright © 2011-2016, 2net Ltd
  • 9. Exporting a GPIO pin # echo 42 > /sys/class/gpio/export # ls /sys/class/gpio export gpio42 gpiochip0 gpiochip32 gpiochip64 gpiochip96 unexport If the export is successful, a new directory is created How to avoid writing device drivers for embedded Linux 9 Copyright © 2011-2016, 2net Ltd
  • 10. Inputs and outputs # ls /sys/class/gpio/gpio42 active_low device direction edge power subsystem uevent value Set to 1 to invert input and ouput Set direction by writing "out" or "in". Default "in" The logic level of the pin. Change the level of outputs by writing "0" or "1" How to avoid writing device drivers for embedded Linux 10 Copyright © 2011-2016, 2net Ltd
  • 11. Interrupts • If the GPIO can generate interrupts, the file edge can be used to control interrupt handling • edge = ["none", "rising", "falling","both] • For example, to make GPIO60 interrupt on falling edge: • echo falling > /sys/class/gpio/gpio60/edge • To wait for an interrupt, use the poll(2) function • Example on next slide How to avoid writing device drivers for embedded Linux 11 Copyright © 2011-2016, 2net Ltd
  • 12. GPIO interrupt code example #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <poll.h> int main (int argc, char *argv[]) { int f; struct pollfd poll_fds [1]; int ret; char value[4]; f = open("/sys/class/gpio/gpio60/value", O_RDONLY); poll_fds[0].fd = f; poll_fds[0].events = POLLPRI | POLLERR; while (1) { if (poll(poll_fds, 1, -1) > 0) { read(f, &value, sizeof(value)); printf("Interrupt! value=%cn", value[0]); } } } Code: https://github.com/csimmonds/userspace-io-ew2016 How to avoid writing device drivers for embedded Linux 12 Copyright © 2011-2016, 2net Ltd
  • 13. PWM # echo 6 > /sys/class/pwm/export # ls /sys/class/pwm export pwm6 pwmchip0 pwmchip2 pwmchip3 pwmchip5 pwmchip7 unexport If the export is successful, a new directory is created # ls /sys/class/pwm/pwm6/ device duty_ns period_ns polarity power run subsystem uevent period_ns duty_ns How to avoid writing device drivers for embedded Linux 13 Copyright © 2011-2016, 2net Ltd
  • 14. I2C • Device nodes, one per I2C bus controller: # ls -l /dev/i2c* crw-rw---T 1 root i2c 89, 0 Jan 1 2000 /dev/i2c-0 crw-rw---T 1 root i2c 89, 1 Jan 1 2000 /dev/i2c-1 • Some functions are implemented using ioctl(2), using commands and structures defined in usr/include/linux/i2c-dev.h How to avoid writing device drivers for embedded Linux 14 Copyright © 2011-2016, 2net Ltd
  • 15. i2c-utils • Command-line tools for interacting with I2C devices • i2cdetect - list I2C adapters and probe bus • i2cget - read data from an I2C device • i2cset - write data to an I2C device How to avoid writing device drivers for embedded Linux 15 Copyright © 2011-2016, 2net Ltd
  • 16. i2cdetect • i2cdetect - list i2c adapters and probe bus • Example: detect devices on bus 1 (/dev/i2c-1) # i2cdetect -y -r 1 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- 39 -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- UU UU UU UU -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- -- UU = device already handled by kernel driver 0x39 = device discovered at address 0x39 How to avoid writing device drivers for embedded Linux 16 Copyright © 2011-2016, 2net Ltd
  • 17. i2cget/i2cset • i2cget <bus> <chip> <register>: read data from an I2C device • Example: read register 0x8a from device at 0x39 # i2cget -y 1 0x39 0x8a 0x50 • i2cset <bus> <chip> <register>: writedata to an I2C device • Example: Write 0x03 to register 0x80: # i2cset -y 1 0x39 0x80 3 How to avoid writing device drivers for embedded Linux 17 Copyright © 2011-2016, 2net Ltd
  • 18. I2C code example #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/ioctl.h> #include <linux/i2c-dev.h> int main(int argc, char **argv) int f; char buf[4]; f = open("/dev/i2c-1", O_RDWR); ioctl(f, I2C_SLAVE, 0x39) < 0) { buf[0] = 0x8a; /* Chip ID register */ write(f, buf, 1); read(f, buf, 1); printf("ID 0x%xn", buf [0]); } Code: https://github.com/csimmonds/userspace-io-ew2016 How to avoid writing device drivers for embedded Linux 18 Copyright © 2011-2016, 2net Ltd
  • 19. Other examples • SPI: access SPI devices via device nodes /dev/spidev* • USB: access USB devices via libusb • User defined I/O: UIO • Generic kernel driver that allows you to write userspace drivers • access device registers and handle interrupts from userspace How to avoid writing device drivers for embedded Linux 19 Copyright © 2011-2016, 2net Ltd
  • 20. Delving deeper • This is an excerpt from my Fast track to embedded Linux class • If you would like to discover more about the power of embedded Linux, visit http://www.2net.co.uk/training.html and enquire about training classes for your company • 2net training is available world-wide • Also, my book, Mastering Embedded Linux Programming, covers the topics discused here in much greater detail How to avoid writing device drivers for embedded Linux 20 Copyright © 2011-2016, 2net Ltd