pulse_reader.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #pragma once
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <furi_hal_gpio.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #define PULSE_READER_NO_EDGE (0xFFFFFFFFUL)
  10. #define PULSE_READER_LOST_EDGE (0xFFFFFFFEUL)
  11. #define F_TIM2 (64000000UL)
  12. /**
  13. * unit of the edge durations to return
  14. */
  15. typedef enum {
  16. PulseReaderUnit64MHz,
  17. PulseReaderUnitPicosecond,
  18. PulseReaderUnitNanosecond,
  19. PulseReaderUnitMicrosecond,
  20. } PulseReaderUnit;
  21. /* using an anonymous type */
  22. typedef struct PulseReader PulseReader;
  23. /** Allocate a PulseReader object
  24. *
  25. * Allocates memory for a ringbuffer and initalizes the object
  26. *
  27. * @param[in] gpio the GPIO to use. will get configured as input.
  28. * @param[in] size number of edges to buffer
  29. */
  30. PulseReader* pulse_reader_alloc(const GpioPin* gpio, uint32_t size);
  31. /** Free a PulseReader object
  32. *
  33. * Frees all memory of the given object
  34. *
  35. * @param[in] signal previously allocated PulseReader object.
  36. */
  37. void pulse_reader_free(PulseReader* signal);
  38. /** Start signal capturing
  39. *
  40. * Initializes DMA1, TIM2 and DMAMUX_REQ_GEN_0 to automatically capture timer values.
  41. * Ensure that interrupts are always enabled, as the used EXTI line is handled as one.
  42. *
  43. * @param[in] signal previously allocated PulseReader object.
  44. */
  45. void pulse_reader_start(PulseReader* signal);
  46. /** Stop signal capturing
  47. *
  48. * Frees DMA1, TIM2 and DMAMUX_REQ_GEN_0
  49. *
  50. * @param[in] signal previously allocated PulseReader object.
  51. */
  52. void pulse_reader_stop(PulseReader* signal);
  53. /** Recevie a sample from ringbuffer
  54. *
  55. * Waits for the specified time until a new edge gets detected.
  56. * If not configured otherwise, the pulse duration will be in picosecond resolution.
  57. * If a bittime was configured, the return value will contain the properly rounded
  58. * number of bit times measured.
  59. *
  60. * @param[in] signal previously allocated PulseReader object.
  61. * @param[in] timeout_us time to wait for a signal [µs]
  62. *
  63. * @returns the scaled value of the pulse duration
  64. */
  65. uint32_t pulse_reader_receive(PulseReader* signal, int timeout_us);
  66. /** Get available samples
  67. *
  68. * Get the number of available samples in the ringbuffer
  69. *
  70. * @param[in] signal previously allocated PulseReader object.
  71. *
  72. * @returns the number of samples in buffer
  73. */
  74. uint32_t pulse_reader_samples(PulseReader* signal);
  75. /** Set timebase
  76. *
  77. * Set the timebase to be used when returning pulse duration.
  78. *
  79. * @param[in] signal previously allocated PulseReader object.
  80. * @param[in] unit PulseReaderUnit64MHz or PulseReaderUnitPicosecond
  81. */
  82. void pulse_reader_set_timebase(PulseReader* signal, PulseReaderUnit unit);
  83. /** Set bit time
  84. *
  85. * Set the number of timebase units per bit.
  86. * When set, the pulse_reader_receive() will return an already rounded
  87. * bit count value instead of the raw duration.
  88. *
  89. * Set to 1 to return duration again.
  90. *
  91. * @param[in] signal previously allocated PulseReader object.
  92. * @param[in] bit_time
  93. */
  94. void pulse_reader_set_bittime(PulseReader* signal, uint32_t bit_time);
  95. /** Set GPIO pull direction
  96. *
  97. * Some GPIOs need pulldown, others don't. By default the
  98. * pull direction is GpioPullNo.
  99. *
  100. * @param[in] signal previously allocated PulseReader object.
  101. * @param[in] pull GPIO pull direction
  102. */
  103. void pulse_reader_set_pull(PulseReader* signal, GpioPull pull);
  104. #ifdef __cplusplus
  105. }
  106. #endif