IIO, a new kernel subsystemIIO, a new kernelsubsystemMaxime RipardFree Electronsc Copyright 2009-2012, Free Electrons.Crea...
OutlineIIO, a new kernel subsystemWhat is IIO ?DefinitionCurrent state in the kernelGetting startedIIO deviceIIO channelsGo...
Maxime RipardEmbedded Linux engineer and trainer at Free Electrons since2011Embedded Linux development: kernel and driver ...
OutlineIIO, a new kernel subsystemWhat is IIO ?DefinitionCurrent state in the kernelGetting startedIIO deviceIIO channelsGo...
IIOA subsystem for Analog to Digital Converters (ADCs) andrelated hardwares (accelerometers, light sensors, gyroscopes),bu...
Current state in the kernelDeveloped since 2009 by Jonathan CameronBeing developed in the staging/ directory until it come...
OutlineIIO, a new kernel subsystemWhat is IIO ?DefinitionCurrent state in the kernelGetting startedIIO deviceIIO channelsGo...
iio dev structureMain structure of all IIO driversHolds informations about the device and the driver, such as :How much ch...
Allocate a new devicestruct iio_dev *idev = iio_allocate_device)sizeof(struct at91_adc_state));Allocates a struct iio dev,...
Capture modesidev->modes = INDIO_DIRECT_MODE;Defines the mode of operations available for this device, to choosebetween :IN...
IIO infosstatic const struct iio_info at91_adc_info = {.driver_module = THIS_MODULE,.read_raw = &at91_adc_read_raw,};idev-...
Basic design of the driverFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support...
iio channels declarationstruct iio_chan_spec *chan = kmalloc(sizeof(struct iio_chan_spec),GFP_KERNEL);chan->type = IIO_VOL...
Register the deviceiio_device_register(idev);this is sufficient to have a basic IIO device driverFree Electrons. Kernel, dri...
Userspace APIIf we look at /sys/bus/iio/devices/iio:deviceX, we shouldhave:$ ls /sys/bus/iio/devices/iio:deviceXin_voltage...
OutlineIIO, a new kernel subsystemWhat is IIO ?DefinitionCurrent state in the kernelGetting startedIIO deviceIIO channelsGo...
Hardware TriggersIIO exposes API so that we can :declare any given number of triggerschoose which channels we want enabled...
Kernel APIstatic const struct iio_trigger_ops at91_adc_trigger_ops = {.owner = THIS_MODULE,.set_trigger_state = &at91_adc_...
Poll Functionfunction triggered at each conversioncalled through the functions iio_trigger_poll oriio_trigger_poll_chained...
Buffersdoesn’t make much sense to have triggered captures withouta bufferBuffers and triggers are closely tied together in II...
Allocate a bufferin the latest code, if you want to use the IIO ring buffer, boilerplate code has been added so it’s pretty ...
Userspace API 1/3: Set up the captureIf we look at /sys/bus/iio/devices/, we should now have inaddition to iio:deviceX:$ l...
Userspace API 2/3: Start and stop a capture$ echo 1 > /sys/bus/iio/devices/iio:device0/scan_elements/in_voltage0_en$ echo ...
Userspace API 3/3: Get the capture resultsIIO also exposes a character device to get the convertedvalues: /dev/iio:deviceX...
OutlineIIO, a new kernel subsystemWhat is IIO ?DefinitionCurrent state in the kernelGetting startedIIO deviceIIO channelsGo...
Useful Resourcesdrivers/staging/iio/Documentationdrivers/staging/iio/iio_simple_dummy.chttp://www.ohwr.org/projects/zio/wi...
OutlineIIO, a new kernel subsystemWhat is IIO ?DefinitionCurrent state in the kernelGetting startedIIO deviceIIO channelsGo...
ConclusionIIO is a nice subsystem to add ADCs and the like supportStill under heavy development, but also really opens toc...
Upcoming SlideShare
Loading in...5
×

127 iio a-new-subsystem

1,514

Published on

Published in: Education, Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,514
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
39
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

127 iio a-new-subsystem

  1. 1. IIO, a new kernel subsystemIIO, a new kernelsubsystemMaxime RipardFree Electronsc 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 LinuxDevelopersFree ElectronsFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 1
  2. 2. OutlineIIO, a new kernel subsystemWhat is IIO ?DefinitionCurrent state in the kernelGetting startedIIO deviceIIO channelsGoing further : Hardware triggers and buffersHardware TriggersBuffersUseful ResourcesConclusionFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 3
  3. 3. Maxime RipardEmbedded Linux engineer and trainer at Free Electrons since2011Embedded Linux development: kernel and driver development,system integration, boot time and power consumptionoptimization, etc.Embedded Linux, Embedded Android and driver developmenttraining, with materials freely available under a CreativeCommons license.http://free-electrons.comContributor to Buildroot, a simple open-source embeddedbuild systemFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 4
  4. 4. OutlineIIO, a new kernel subsystemWhat is IIO ?DefinitionCurrent state in the kernelGetting startedIIO deviceIIO channelsGoing further : Hardware triggers and buffersHardware TriggersBuffersUseful ResourcesConclusionFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 5
  5. 5. IIOA subsystem for Analog to Digital Converters (ADCs) andrelated hardwares (accelerometers, light sensors, gyroscopes),but also DACsCan be used on ADCs ranging from a SoC ADC to 100Msamples/sec industrial ADCsUntil recently, mostly focused on user-space abstraction withno in-kernel API for other driversFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 6
  6. 6. Current state in the kernelDeveloped since 2009 by Jonathan CameronBeing developed in the staging/ directory until it comes toan high quality code and a mature APIIt 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 driversfor them, mostly from Analog Devices Inc, but also drivers forTexas Instruments, Atmel, etc.Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 7
  7. 7. OutlineIIO, a new kernel subsystemWhat is IIO ?DefinitionCurrent state in the kernelGetting startedIIO deviceIIO channelsGoing further : Hardware triggers and buffersHardware TriggersBuffersUseful ResourcesConclusionFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 8
  8. 8. iio dev structureMain structure of all IIO driversHolds 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. 9. Allocate a new devicestruct iio_dev *idev = iio_allocate_device)sizeof(struct at91_adc_state));Allocates a struct iio dev, along with the private data of yourdriverDoes all the basic initialisationFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 10
  10. 10. Capture modesidev->modes = INDIO_DIRECT_MODE;Defines the mode of operations available for this device, to choosebetween :INDIO DIRECT MODE the device can operate using softwaretriggersINDIO BUFFER TRIGGERED the device can use hardwaretriggersINDIO BUFFER HARDWARE the device has a hardware bufferINDIO ALL BUFFER MODES union of the two aboveFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 11
  11. 11. IIO infosstatic 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 deviceLot of hooks available corresponding to interactions the usercan make through sysfs.read_raw for example is called to request a value from thedriver. A bitmask allows us to know more precisely which typeof value is requested, and for which channel if needed. It canbe for example either the scale used to convert value returnedto volts or the value in itself.Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 12
  12. 12. Basic design of the driverFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 13
  13. 13. iio channels declarationstruct 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. 14. Register the deviceiio_device_register(idev);this is sufficient to have a basic IIO device driverFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 15
  15. 15. Userspace APIIf we look at /sys/bus/iio/devices/iio:deviceX, we shouldhave:$ ls /sys/bus/iio/devices/iio:deviceXin_voltage0_rawin_voltage_scalename$reading in voltage0 raw calls the read raw hook, with themask set to 0, and the chan argument set with theiio chan spec structure corresponding to the the channel 0reading in voltage scale calls the read raw hook, with themask set to IIO CHAN INFO SCALEFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 16
  16. 16. OutlineIIO, a new kernel subsystemWhat is IIO ?DefinitionCurrent state in the kernelGetting startedIIO deviceIIO channelsGoing further : Hardware triggers and buffersHardware TriggersBuffersUseful ResourcesConclusionFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 17
  17. 17. Hardware TriggersIIO exposes API so that we can :declare any given number of triggerschoose which channels we want enabled for conversionsFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 18
  18. 18. Kernel APIstatic 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 declareThese hooks are this time triggers-specific, so that we canhave 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. 19. Poll Functionfunction triggered at each conversioncalled through the functions iio_trigger_poll oriio_trigger_poll_chainedbasically, its job is to feed retrieve data from the device andfeed them into the bufferIIO uses the IRQ model, so the poll function has the sameprototype 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. 20. Buffersdoesn’t make much sense to have triggered captures withouta bufferBuffers and triggers are closely tied together in IIO2 types of buffers in IIOOne relies on kfifoThe other one is a ring bufferFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 21
  21. 21. Allocate a bufferin the latest code, if you want to use the IIO ring buffer, boilerplate 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 thedevice as supporting triggered capture, register the bufferagainst the core, etcFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 22
  22. 22. Userspace API 1/3: Set up the captureIf we look at /sys/bus/iio/devices/, we should now have inaddition to iio:deviceX:$ ls /sys/bus/iio/devices/iio:device0trigger0$ ls /sys/bus/iio/devices/iio:device0bufferscan_elementstrigger$ ls /sys/bus/iio/devices/iio:device0/bufferenabled length$ ls /sys/bus/iio/devices/iio:device0/scan_elementsin_voltage0_enin_voltage0_indexin_voltage0_type$ ls /sys/bus/iio/devices/trigger0name$Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 23
  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_triggerFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 24
  24. 24. Userspace API 3/3: Get the capture resultsIIO also exposes a character device to get the convertedvalues: /dev/iio:deviceXYou just have to read in it to get dataData are organized by chunksExample: You have 4 channels, plus a timestamp one. All areenabled except channel 2.Channels TimestampIndex 0 1 3Size in bits 16 16 16 64There is a program that provides a basic implementation anda good way to test your driver in the IIO documentationdirectory: drivers/staging/iio/Documentation/generic_buffer.c# ./generic-buffer -n at91_adc -t at91_adc-dev0-externalFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 25
  25. 25. OutlineIIO, a new kernel subsystemWhat is IIO ?DefinitionCurrent state in the kernelGetting startedIIO deviceIIO channelsGoing further : Hardware triggers and buffersHardware TriggersBuffersUseful ResourcesConclusionFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 26
  26. 26. Useful Resourcesdrivers/staging/iio/Documentationdrivers/staging/iio/iio_simple_dummy.chttp://www.ohwr.org/projects/zio/wiki/Iiohttp://www.at91.com/linux4sam/bin/view/Linux4SAM/IioAdcDriverlinux-iio@vger.kernel.orgFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 27
  27. 27. OutlineIIO, a new kernel subsystemWhat is IIO ?DefinitionCurrent state in the kernelGetting startedIIO deviceIIO channelsGoing further : Hardware triggers and buffersHardware TriggersBuffersUseful ResourcesConclusionFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 28
  28. 28. ConclusionIIO is a nice subsystem to add ADCs and the like supportStill under heavy development, but also really opens tochanges and feedbackYet reliable enough to be used in productionFree Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 29
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×