triggered-buffers.rst 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. =================
  2. Triggered Buffers
  3. =================
  4. Now that we know what buffers and triggers are let's see how they work together.
  5. IIO triggered buffer setup
  6. ==========================
  7. * :c:func:`iio_triggered_buffer_setup` — Setup triggered buffer and pollfunc
  8. * :c:func:`iio_triggered_buffer_cleanup` — Free resources allocated by
  9. :c:func:`iio_triggered_buffer_setup`
  10. * struct iio_buffer_setup_ops — buffer setup related callbacks
  11. A typical triggered buffer setup looks like this::
  12. const struct iio_buffer_setup_ops sensor_buffer_setup_ops = {
  13. .preenable = sensor_buffer_preenable,
  14. .postenable = sensor_buffer_postenable,
  15. .postdisable = sensor_buffer_postdisable,
  16. .predisable = sensor_buffer_predisable,
  17. };
  18. irqreturn_t sensor_iio_pollfunc(int irq, void *p)
  19. {
  20. pf->timestamp = iio_get_time_ns((struct indio_dev *)p);
  21. return IRQ_WAKE_THREAD;
  22. }
  23. irqreturn_t sensor_trigger_handler(int irq, void *p)
  24. {
  25. u16 buf[8];
  26. int i = 0;
  27. /* read data for each active channel */
  28. for_each_set_bit(bit, active_scan_mask, masklength)
  29. buf[i++] = sensor_get_data(bit)
  30. iio_push_to_buffers_with_timestamp(indio_dev, buf, timestamp);
  31. iio_trigger_notify_done(trigger);
  32. return IRQ_HANDLED;
  33. }
  34. /* setup triggered buffer, usually in probe function */
  35. iio_triggered_buffer_setup(indio_dev, sensor_iio_polfunc,
  36. sensor_trigger_handler,
  37. sensor_buffer_setup_ops);
  38. The important things to notice here are:
  39. * :c:type:`iio_buffer_setup_ops`, the buffer setup functions to be called at
  40. predefined points in the buffer configuration sequence (e.g. before enable,
  41. after disable). If not specified, the IIO core uses the default
  42. iio_triggered_buffer_setup_ops.
  43. * **sensor_iio_pollfunc**, the function that will be used as top half of poll
  44. function. It should do as little processing as possible, because it runs in
  45. interrupt context. The most common operation is recording of the current
  46. timestamp and for this reason one can use the IIO core defined
  47. :c:func:`iio_pollfunc_store_time` function.
  48. * **sensor_trigger_handler**, the function that will be used as bottom half of
  49. the poll function. This runs in the context of a kernel thread and all the
  50. processing takes place here. It usually reads data from the device and
  51. stores it in the internal buffer together with the timestamp recorded in the
  52. top half.
  53. More details
  54. ============
  55. .. kernel-doc:: drivers/iio/buffer/industrialio-triggered-buffer.c