loragw_aux.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. / _____) _ | |
  3. ( (____ _____ ____ _| |_ _____ ____| |__
  4. \____ \| ___ | (_ _) ___ |/ ___) _ \
  5. _____) ) ____| | | || |_| ____( (___| | | |
  6. (______/|_____)_|_|_| \__)_____)\____)_| |_|
  7. (C)2019 Semtech
  8. Description:
  9. LoRa concentrator HAL common auxiliary functions
  10. License: Revised BSD License, see LICENSE.TXT file include in the project
  11. */
  12. #ifndef _LORAGW_AUX_H
  13. #define _LORAGW_AUX_H
  14. /* -------------------------------------------------------------------------- */
  15. /* --- DEPENDANCIES --------------------------------------------------------- */
  16. #include <stdint.h> /* C99 types */
  17. #include <stdbool.h> /* bool type */
  18. #include <sys/time.h> /* gettimeofday, structtimeval */
  19. #include "config.h" /* library configuration options (dynamically generated) */
  20. /* -------------------------------------------------------------------------- */
  21. /* --- PUBLIC CONSTANTS ----------------------------------------------------- */
  22. #define DEBUG_PERF 0 /* Debug timing performances: level [0..4] */
  23. /* -------------------------------------------------------------------------- */
  24. /* --- PUBLIC MACROS -------------------------------------------------------- */
  25. #define MIN(a,b) (((a)<(b))?(a):(b))
  26. #define MAX(a,b) (((a)>(b))?(a):(b))
  27. /**
  28. @brief Get a particular bit value from a byte
  29. @param b [in] Any byte from which we want a bit value
  30. @param p [in] Position of the bit in the byte [0..7]
  31. @param n [in] Number of bits we want to get
  32. @return The value corresponding the requested bits
  33. */
  34. #define TAKE_N_BITS_FROM(b, p, n) (((b) >> (p)) & ((1 << (n)) - 1))
  35. /**
  36. @brief Substract struct timeval values
  37. @param a [in] struct timeval a
  38. @param b [in] struct timeval b
  39. @param b [out] struct timeval resulting from (a - b)
  40. */
  41. #define TIMER_SUB(a, b, result) \
  42. do { \
  43. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  44. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  45. if ((result)->tv_usec < 0) { \
  46. --(result)->tv_sec; \
  47. (result)->tv_usec += 1000000; \
  48. } \
  49. } while (0)
  50. /* -------------------------------------------------------------------------- */
  51. /* --- PUBLIC FUNCTIONS PROTOTYPES ------------------------------------------ */
  52. /**
  53. @brief Wait for a certain time (millisecond accuracy)
  54. @param t number of milliseconds to wait.
  55. */
  56. void wait_ms(unsigned long t);
  57. /**
  58. @brief Wait for a certain time (microsencond accuracy)
  59. @param t number of microseconds to wait.
  60. */
  61. void wait_us(unsigned long t);
  62. /**
  63. @brief Calculate the time on air of a LoRa packet in microseconds
  64. @param bw packet bandwidth
  65. @param sf packet spreading factor
  66. @param cr packet coding rate
  67. @param n_symbol_preamble packet preamble length (number of symbols)
  68. @param no_header true if packet has no header
  69. @param no_crc true if packet has no CRC
  70. @param size packet size in bytes
  71. @param nb_symbols pointer to return the total number of symbols in packet
  72. @param nb_symbols_payload pointer to return the number of symbols in packet payload
  73. @param t_symbol_us pointer to return the duration of a symbol in microseconds
  74. @return the packet time on air in microseconds
  75. */
  76. uint32_t lora_packet_time_on_air( const uint8_t bw,
  77. const uint8_t sf,
  78. const uint8_t cr,
  79. const uint16_t n_symbol_preamble,
  80. const bool no_header,
  81. const bool no_crc,
  82. const uint8_t size,
  83. double * nb_symbols,
  84. uint32_t * nb_symbols_payload,
  85. uint16_t * t_symbol_us);
  86. /**
  87. @brief Record the current time, for measure start
  88. @param tm Pointer to the current time value
  89. */
  90. void _meas_time_start(struct timeval *tm);
  91. /**
  92. @brief Measure the ellapsed time since given time
  93. @param debug_level debug print debug level to be used
  94. @param start_time start time of the measure to be used
  95. @param str string to be used for debug print
  96. */
  97. void _meas_time_stop(int debug_level, struct timeval start_time, const char *str);
  98. /**
  99. @brief Get the current time for later timeout check
  100. @param start contains the current time to be used as start time for timeout
  101. */
  102. void timeout_start(struct timeval * start);
  103. /**
  104. @brief Check if the given timeout time in milliseconds has ellapsed compared to start time
  105. @param start reference start time
  106. @param timeout_ms the timeout duration in milliseconds
  107. @return -1 if the timeout has exceeded, 0 otherwise
  108. */
  109. int timeout_check(struct timeval start, uint32_t timeout_ms);
  110. #endif
  111. /* --- EOF ------------------------------------------------------------------ */