Example: tourism industry

How to avoid writing kernel drivers - elinux.org

How to avoid writing kernel driversChris SimmondsEmbedded Linux Conference Europe 2018 How to avoid writing kernel drivers1 Copyright 2011-2018, 2net LtdLicenseThese slides are available under a Creative Commons Attribution-ShareAlike license. You can read the fulltext of the license are free to copy, distribute, display, and perform the work make derivative works make commercial use of the workUnder 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 onlyunder a license identical to this one ( include this page exactly as it is) For any reuse or distribution, you must make clear to others the license terms of this workHow to avoid writing kernel drivers2 Copyright 2011-2018, 2net LtdAb

A note about device trees • Even though you are writing userspace drivers, you still need to make sure that the hardware is accessible to the kernel • On ARM based systems, this may mean changing the device tree or adding a device tree overlay (which is outside the scope of this talk)

Tags:

  Writing

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of How to avoid writing kernel drivers - elinux.org

1 How to avoid writing kernel driversChris SimmondsEmbedded Linux Conference Europe 2018 How to avoid writing kernel drivers1 Copyright 2011-2018, 2net LtdLicenseThese slides are available under a Creative Commons Attribution-ShareAlike license. You can read the fulltext of the license are free to copy, distribute, display, and perform the work make derivative works make commercial use of the workUnder 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 onlyunder a license identical to this one ( include this page exactly as it is)

2 For any reuse or distribution, you must make clear to others the license terms of this workHow to avoid writing kernel drivers2 Copyright 2011-2018, 2net LtdAbout Chris Simmonds Consultant and trainer Author ofMastering Embedded Linux Programming Working with embedded Linux since 1999 Android since 2009 Speaker at many conferences and workshops"Looking after the Inner Penguin" blog to avoid writing kernel drivers3 Copyright 2011-2018, 2net LtdAgenda Device drivers in kernel space Device drivers in user space Some examples: GPIO PWM I2 CHow to avoid writing kernel drivers4 Copyright 2011-2018, 2net LtdConventional device driver modelUserspaceSystem call handlerGeneric servicesDevice driversHardwareApplicationC libraryinterruptsLinux kernelHow to avoid writing kernel drivers5 Copyright 2011-2018, 2net LtdHow applications interact device drivers In Linux, everything is a file1 Applications interact with drivers via POSIX functions open(2), read(2),write(2), ioctl(2), etc Two main types of interface:1.

3 Device nodes in/dev For example, the serial driver,ttyS. Device nodes are named/dev/ttyS0,/ Driver attributes, exported viasysfs For example/sys/class/gpio1 Except network interfaces, which are socketsHow to avoid writing kernel drivers6 Copyright 2011-2018, 2net LtdUserspace drivers writing kernel device drivers can be difficult Luckily, there are generic drivers that that allow you to write most of thecode in userspace We will look at three GPIO PWM I2 CHow to avoid writing kernel drivers7 Copyright 2011-2018, 2net LtdA note about device trees Even though you are writing userspace drivers .

4 You still need to makesure that the hardware is accessible to the kernel On ARM based systems, this may mean changing the device tree oradding a device tree overlay (which is outside the scope of this talk)How to avoid writing kernel drivers8 Copyright 2011-2018, 2net LtdGPIO: General Purpose Input/Output Pins that can be configured as inputs or outputs As outputs: used to control LEDs, relays, control chip selects, etc. As inputs: used to read a switch or button state, etc. some GPIO hardware can generate an interrupt when the input changesHow to avoid writing kernel drivers9 Copyright 2011-2018, 2net LtdTwo userspace drivers !

5 Gpiolib1: old, but scriptable interface using sysfs gpio-cdev: new, higher performance method using character devicenodes/dev/gpiochip*1it snota libraryHow to avoid writing kernel drivers10 Copyright 2011-2018, 2net LtdThe gpiolib sysfs interface GPIO pins grouped into registers, namedgpiochipNN Each pin is assigned a number from 0 to XXX# ls /sys/class/gpio/export gpiochip0 gpiochip32 gpiochip64 gpiochip96 unexportThis device has 4 gpio chipseach with 32 pinsWrite to thisfile to exporta GPIO pinto user spaceWrite to thisfile to unexporta GPIO pinto user spaceHow to avoid writing kernel drivers11 Copyright 2011-2018, 2net LtdInside a gpiochip# /sys/class/gpio/gpiochip0base device label ngpio power subsystem ueventThe number of GPIO pins (32)A lable to identify the chip(gpiochip0)

6 The starting GPIO number (0)How to avoid writing kernel drivers12 Copyright 2011-2018, 2net LtdExporting a GPIO pin# echo 42 > /sys/class/gpio/export# ls /sys/class/gpioexport gpio42 gpiochip0 gpiochip32 gpiochip64 gpiochip96 unexportIf the export is successful, a newdirectory is createdHow to avoid writing kernel drivers13 Copyright 2011-2018, 2net LtdInputs and outputs# ls /sys/class/gpio/gpio42active_low device direction edge power subsystem uevent valueSet to 1 to invertinput and ouputSet direction bywriting "out" or"in". Default "in" The logic level of thepin.

7 Change the levelof outputs by writing "0" or "1"How to avoid writing kernel drivers14 Copyright 2011-2018, 2net LtdInterrupts If the GPIO can generate interrupts, the fileedgecan be used tocontrol interrupt handling edge = ["none", "rising", "falling","both"] For example, to make GPIO60 interrupt on a falling edge: echo falling > /sys/class/gpio/gpio60/edge To wait for an interrupt, use the poll(2) functionHow to avoid writing kernel drivers15 Copyright 2011-2018, 2net LtdThe gpio-cdev interface One device node per GPIO register named/dev/gpiochip* Access the GPIO pins usingioctl(2)

8 Advantages Naming scheme gpiochip/pin rather than uniform but opaque namespace from 0 to XXX Multiple pin transitions in single function call without glitches More robust handling of interruptsHow to avoid writing kernel drivers16 Copyright 2011-2018, 2net Ltdgpio-cdev example 1/2/** Demonstrate using gpio cdev to output a single bit* On a BeagleBone Black, GPIO1_21 is user LED 1*/#include < >#include < >#include < >#include < >#include < >#include < >#include < >int main(void){int f;int ret;struct gpiohandle_request req;struct gpiohandle_data data.}

9 How to avoid writing kernel drivers17 Copyright 2011-2018, 2net Ltdgpio-cdev example 2/2f = open("/dev/gpiochip1", O_RDONLY); [0] = 21; = GPIOHANDLE_REQUEST_OUTPUT; /* Request as output * [0] = 0;strcpy( , "gpio-output"); /* up to 31 characters * = 1.

10 Ret = ioctl(f, GPIO_GET_LINEHANDLE_IOCTL, /* Note that there is a new file descriptor in to handle theGPIO lines * [0] = 1;ret = ioctl( , GPIOHANDLE_SET_LINE_VALUES_IOCTL, close(f);return 0.))


Related search queries