loragw_sx1302_timestamp.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. / _____) _ | |
  3. ( (____ _____ ____ _| |_ _____ ____| |__
  4. \____ \| ___ | (_ _) ___ |/ ___) _ \
  5. _____) ) ____| | | || |_| ____( (___| | | |
  6. (______/|_____)_|_|_| \__)_____)\____)_| |_|
  7. (C)2019 Semtech
  8. Description:
  9. SX1302 timestamp counter Hardware Abstraction Layer
  10. Handles the conversion of a 32-bits 32MHz counter into a 32-bits 1 MHz counter.
  11. This modules MUST be called regularly by the application to maintain counter
  12. wrapping handling for conversion in 1MHz counter.
  13. Provides function to compute the correction to be applied to the received
  14. timestamp for demodulation processing time.
  15. License: Revised BSD License, see LICENSE.TXT file include in the project
  16. */
  17. #ifndef _LORAGW_SX1302_TIMESTAMP_H
  18. #define _LORAGW_SX1302_TIMESTAMP_H
  19. /* -------------------------------------------------------------------------- */
  20. /* --- DEPENDANCIES --------------------------------------------------------- */
  21. #include <stdint.h> /* C99 types*/
  22. #include <stdbool.h> /* boolean type */
  23. #include "loragw_hal.h"
  24. #include "loragw_sx1302.h"
  25. #include "config.h" /* library configuration options (dynamically generated) */
  26. /* -------------------------------------------------------------------------- */
  27. /* --- PUBLIC CONSTANTS ----------------------------------------------------- */
  28. /* -------------------------------------------------------------------------- */
  29. /* --- PUBLIC MACROS -------------------------------------------------------- */
  30. /* -------------------------------------------------------------------------- */
  31. /* --- PUBLIC TYPES --------------------------------------------------------- */
  32. /**
  33. @struct timestamp_counter_s
  34. @brief context to maintain the internal counters (inst and pps trig) rollover status
  35. */
  36. struct timestamp_info_s {
  37. uint32_t counter_us_27bits_ref; /* reference value (last read) */
  38. uint8_t counter_us_27bits_wrap; /* rollover/wrap status */
  39. };
  40. typedef struct timestamp_counter_s {
  41. struct timestamp_info_s inst; /* holds current reference of the instantaneous counter */
  42. struct timestamp_info_s pps; /* holds current reference of the pps-trigged counter */
  43. } timestamp_counter_t;
  44. /* -------------------------------------------------------------------------- */
  45. /* --- PUBLIC FUNCTIONS ----------------------------------------------------- */
  46. /**
  47. @brief Initialize the timestamp_counter instance
  48. @param self Pointer to the counter handler
  49. @return N/A
  50. */
  51. void timestamp_counter_new(timestamp_counter_t * self);
  52. /**
  53. @brief Reset the timestamp_counter instance
  54. @param self Pointer to the counter handler
  55. @return N/A
  56. */
  57. void timestamp_counter_delete(timestamp_counter_t * self);
  58. /**
  59. @brief Update the counter wrapping status based on given current counter
  60. @param self Pointer to the counter handler
  61. @param pps Current value of the pps counter to be used for the update
  62. @param cnt Current value of the freerun counter to be used for the update
  63. @return N/A
  64. */
  65. void timestamp_counter_update(timestamp_counter_t * self, uint32_t pps, uint32_t cnt);
  66. /**
  67. @brief Convert the 27-bits counter given by the SX1302 to a 32-bits counter which wraps on a uint32_t.
  68. @param self Pointer to the counter handler
  69. @param pps Set to true to expand the counter based on the PPS trig wrapping status
  70. @param cnt_us The 27-bits counter to be expanded
  71. @return the 32-bits counter
  72. */
  73. uint32_t timestamp_counter_expand(timestamp_counter_t * self, bool pps, uint32_t cnt_us);
  74. /**
  75. @brief Convert the 27-bits packet timestamp to a 32-bits counter which wraps on a uint32_t.
  76. @param self Pointer to the counter handler
  77. @param cnt_us The packet 27-bits counter to be expanded
  78. @return the 32-bits counter
  79. */
  80. uint32_t timestamp_pkt_expand(timestamp_counter_t * self, uint32_t cnt_us);
  81. /**
  82. @brief Reads the SX1302 internal counter register, and return the 32-bits 1 MHz counter
  83. @param self Pointer to the counter handler
  84. @param pps Current value of the freerun counter
  85. @param pps Current value of the PPS counter
  86. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  87. */
  88. int timestamp_counter_get(timestamp_counter_t * self, uint32_t * inst, uint32_t * pps);
  89. /**
  90. @brief Get the correction to applied to the LoRa packet timestamp (count_us)
  91. @param context gateway configuration context
  92. @param bandwidth modulation bandwidth
  93. @param datarate modulation datarate
  94. @param coderate modulation coding rate
  95. @param crc_en indicates if CRC is enabled or disabled
  96. @param payload_length payload length
  97. @param dft_peak_mode DFT peak mode configuration of the modem
  98. @return The correction to be applied to the packet timestamp, in microseconds
  99. */
  100. int32_t timestamp_counter_correction(lgw_context_t * context, uint8_t bandwidth, uint8_t datarate, uint8_t coderate, bool crc_en, uint8_t payload_length, sx1302_rx_dft_peak_mode_t dft_peak_mode);
  101. /**
  102. @brief Configure the SX1302 to output legacy timestamp or precision timestamp
  103. @note Legacy timestamp gives a timestamp latched at the end of the packet
  104. @note Precision timestamp gives a timestamp latched at the end of the header
  105. @note and additionally supplies metrics every N symbols troughout the payload.
  106. @param enable_precision_ts A boolean to enable precision timestamp output.
  107. @param max_ts_metrics The number of timestamp metrics to be returned when precision timestamp is enabled
  108. @param nb_symbols The sampling rate of timestamp metrics
  109. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  110. */
  111. int timestamp_counter_mode(bool ftime_enable);
  112. /**
  113. @brief Compute a precise timestamp (fine timestamp) based on given coarse timestamp, metrics given by sx1302 and current GW xtal drift
  114. @param ts_metrics_nb The number of timestamp metrics given in ts_metrics array
  115. @param ts_metrics An array containing timestamp metrics to compute fine timestamp
  116. @param pkt_coarse_tmst The packet coarse timestamp
  117. @param sf packet spreading factor, used to shift timestamp from end of header to end of preamble
  118. @param if_freq_hz the IF frequency, to take into account DC noth delay
  119. @param result_ftime A pointer to store the resulting fine timestamp
  120. @return 0 if success, -1 otherwise
  121. */
  122. int precise_timestamp_calculate(uint8_t ts_metrics_nb, const int8_t * ts_metrics, uint32_t pkt_coarse_tmst, uint8_t sf, int32_t if_freq_hz, double pkt_freq_error, uint32_t * result_ftime);
  123. #endif
  124. /* --- EOF ------------------------------------------------------------------ */