iio_devbuf.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =============================
  3. Industrial IIO device buffers
  4. =============================
  5. 1. Overview
  6. ===========
  7. The Industrial I/O core offers a way for continuous data capture based on a
  8. trigger source. Multiple data channels can be read at once from
  9. ``/dev/iio:deviceX`` character device node, thus reducing the CPU load.
  10. Devices with buffer support feature an additional sub-directory in the
  11. ``/sys/bus/iio/devices/iio:deviceX/`` directory hierarchy, called bufferY, where
  12. Y defaults to 0, for devices with a single buffer.
  13. 2. Buffer attributes
  14. ====================
  15. An IIO buffer has an associated attributes directory under
  16. ``/sys/bus/iio/iio:deviceX/bufferY/``. The attributes are described below.
  17. ``length``
  18. ----------
  19. Read / Write attribute which states the total number of data samples (capacity)
  20. that can be stored by the buffer.
  21. ``enable``
  22. ----------
  23. Read / Write attribute which starts / stops the buffer capture. This file should
  24. be written last, after length and selection of scan elements. Writing a non-zero
  25. value may result in an error, such as EINVAL, if, for example, an unsupported
  26. combination of channels is given.
  27. ``watermark``
  28. -------------
  29. Read / Write positive integer attribute specifying the maximum number of scan
  30. elements to wait for.
  31. Poll will block until the watermark is reached.
  32. Blocking read will wait until the minimum between the requested read amount or
  33. the low watermark is available.
  34. Non-blocking read will retrieve the available samples from the buffer even if
  35. there are less samples than the watermark level. This allows the application to
  36. block on poll with a timeout and read the available samples after the timeout
  37. expires and thus have a maximum delay guarantee.
  38. Data available
  39. --------------
  40. Read-only attribute indicating the bytes of data available in the buffer. In the
  41. case of an output buffer, this indicates the amount of empty space available to
  42. write data to. In the case of an input buffer, this indicates the amount of data
  43. available for reading.
  44. Scan elements
  45. -------------
  46. The meta information associated with a channel data placed in a buffer is called
  47. a scan element. The scan elements attributes are presented below.
  48. **_en**
  49. Read / Write attribute used for enabling a channel. If and only if its value
  50. is non-zero, then a triggered capture will contain data samples for this
  51. channel.
  52. **_index**
  53. Read-only unsigned integer attribute specifying the position of the channel in
  54. the buffer. Note these are not dependent on what is enabled and may not be
  55. contiguous. Thus for userspace to establish the full layout these must be used
  56. in conjunction with all _en attributes to establish which channels are present,
  57. and the relevant _type attributes to establish the data storage format.
  58. **_type**
  59. Read-only attribute containing the description of the scan element data storage
  60. within the buffer and hence the form in which it is read from userspace. Format
  61. is [be|le]:[s|u]bits/storagebits[Xrepeat][>>shift], where:
  62. - **be** or **le** specifies big or little-endian.
  63. - **s** or **u** specifies if signed (2's complement) or unsigned.
  64. - **bits** is the number of valid data bits.
  65. - **storagebits** is the number of bits (after padding) that it occupies in the
  66. buffer.
  67. - **repeat** specifies the number of bits/storagebits repetitions. When the
  68. repeat element is 0 or 1, then the repeat value is omitted.
  69. - **shift** if specified, is the shift that needs to be applied prior to
  70. masking out unused bits.
  71. For example, a driver for a 3-axis accelerometer with 12-bit resolution where
  72. data is stored in two 8-bit registers is as follows::
  73. 7 6 5 4 3 2 1 0
  74. +---+---+---+---+---+---+---+---+
  75. |D3 |D2 |D1 |D0 | X | X | X | X | (LOW byte, address 0x06)
  76. +---+---+---+---+---+---+---+---+
  77. 7 6 5 4 3 2 1 0
  78. +---+---+---+---+---+---+---+---+
  79. |D11|D10|D9 |D8 |D7 |D6 |D5 |D4 | (HIGH byte, address 0x07)
  80. +---+---+---+---+---+---+---+---+
  81. will have the following scan element type for each axis:
  82. .. code-block:: bash
  83. $ cat /sys/bus/iio/devices/iio:device0/buffer0/in_accel_y_type
  84. le:s12/16>>4
  85. A userspace application will interpret data samples read from the buffer as
  86. two-byte little-endian signed data, that needs a 4 bits right shift before
  87. masking out the 12 valid bits of data.
  88. It is also worth mentioning that the data in the buffer will be naturally
  89. aligned, so the userspace application has to handle the buffers accordingly.
  90. Take for example, a driver with four channels with the following description:
  91. - channel0: index: 0, type: be:u16/16>>0
  92. - channel1: index: 1, type: be:u32/32>>0
  93. - channel2: index: 2, type: be:u32/32>>0
  94. - channel3: index: 3, type: be:u64/64>>0
  95. If all channels are enabled, the data will be aligned in the buffer as follows::
  96. 0-1 2 3 4-7 8-11 12 13 14 15 16-23 -> buffer byte number
  97. +-----+---+---+-----+-----+---+---+---+---+-----+
  98. |CHN_0|PAD|PAD|CHN_1|CHN_2|PAD|PAD|PAD|PAD|CHN_3| -> buffer content
  99. +-----+---+---+-----+-----+---+---+---+---+-----+
  100. If only channel0 and channel3 are enabled, the data will be aligned in the
  101. buffer as follows::
  102. 0-1 2 3 4 5 6 7 8-15 -> buffer byte number
  103. +-----+---+---+---+---+---+---+-----+
  104. |CHN_0|PAD|PAD|PAD|PAD|PAD|PAD|CHN_3| -> buffer content
  105. +-----+---+---+---+---+---+---+-----+
  106. Typically the buffered data is found in raw format (unscaled with no offset
  107. applied), however there are corner cases in which the buffered data may be found
  108. in a processed form. Please note that these corner cases are not addressed by
  109. this documentation.
  110. Please see ``Documentation/ABI/testing/sysfs-bus-iio`` for a complete
  111. description of the attributes.