mac_time.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * Copyright (c) 2016 - 2020 Nordic Semiconductor ASA and Luxoft Global Operations Gmbh.
  3. *
  4. * All Rights Reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice, this
  11. * list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form, except as embedded into a Nordic
  14. * Semiconductor ASA integrated circuit in a product or a software update for
  15. * such product, must reproduce the above copyright notice, this list of
  16. * conditions and the following disclaimer in the documentation and/or other
  17. * materials provided with the distribution.
  18. *
  19. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * 4. This software, with or without modification, must only be used with a
  24. * Nordic Semiconductor ASA integrated circuit.
  25. *
  26. * 5. Any software provided in binary form under this license must not be reverse
  27. * engineered, decompiled, modified and/or disassembled.
  28. *
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  31. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  33. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  34. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  36. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  39. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. */
  42. #ifndef MAC_TIME_H_INCLUDED
  43. #define MAC_TIME_H_INCLUDED
  44. #include <stdint.h>
  45. #include "sys_time.h"
  46. #include "hal_timer.h"
  47. #include "hal_timer_critical.h"
  48. #include "sys_debug.h"
  49. /** @file
  50. * The MAC Time module declares some useful macros/types and routines that deal with the MAC
  51. * timer.
  52. *
  53. * @defgroup mac_time MAC Time API
  54. * @ingroup mac_15_4
  55. * @{
  56. * @brief Module to declare MAC Time API.
  57. * @details The MAC Time module declares some useful macros/types and routines that deal with the MAC
  58. * timer. More specifically, some convertion routines such as mac_timestamp_from_systime(),
  59. * mac_time_from_us(), and mac_time_to_us() are declared here.
  60. */
  61. /**@brief This mask shall always be used after any mathematical operation on
  62. * mac_time_t to avoid overflow.
  63. */
  64. #define MAC_TIME_MASK 0xFFFFFFULL
  65. /**@brief Type of MAC time in symbols. */
  66. typedef uint32_t mac_time_t;
  67. /**@brief Type is used to save timestamps with microsecond precision. */
  68. typedef uint32_t mac_timestamp_t;
  69. /**@brief Gets timestamp from system time.
  70. *
  71. * @param[in] time_us System time.
  72. *
  73. * @return Time in us but smaller type size.
  74. */
  75. static inline mac_timestamp_t mac_timestamp_from_systime(sys_time_t time_us)
  76. {
  77. return (mac_timestamp_t)time_us;
  78. }
  79. /**@brief Converts microseconds to symbol time.
  80. *
  81. * @details Symbol time is measured in PHY Symbol Periods (16 us).
  82. *
  83. * @param[in] time_us Time in microseconds.
  84. *
  85. * @return Time in PHY Symbol Periods (16 us).
  86. */
  87. static inline mac_time_t mac_time_from_us(sys_time_t time_us)
  88. {
  89. return (mac_time_t)((time_us >> 4ull) & MAC_TIME_MASK);
  90. }
  91. /**@brief Converts symbol time to microseconds.
  92. *
  93. * @details Symbol time is measured in PHY Symbol Periods (16 us).
  94. *
  95. * @param[in] time_symbol Time in PHY Symbol Periods (16 us).
  96. *
  97. * @return Time in microseconds.
  98. */
  99. static inline sys_time_t mac_time_to_us(mac_time_t time_symbol)
  100. {
  101. return time_symbol << 4u;
  102. }
  103. /**@brief Starts the critical MAC timer.
  104. *
  105. * @details The callback function of the critical MAC timer will be called from
  106. * the timer's interrupt routine. Only one critical MAC timer can run
  107. * at the same time.
  108. *
  109. * @warning This is internal MAC functionality, needed for the realtime channel access.
  110. * This function must not be used by other modules.
  111. *
  112. * @param[in] interval_us Interval in microseconds, after which the callback
  113. * function will be called.
  114. * @param[in] callback Callback function to be called after the specified inteval.
  115. */
  116. static inline void mac_timer_critical_start(sys_time_t interval_us, void (* callback)(void))
  117. {
  118. // Make sure interval_us fits into 32 bits, since hardware critical timer is 32 bit.
  119. ASSERT(interval_us < (1ULL << 32));
  120. hal_timer_critical_start((uint32_t)interval_us, callback);
  121. }
  122. /**@brief Stops the critical MAC timer.
  123. *
  124. * @details After critical MAC timer is stopped with this function, its callback will not be called.
  125. *
  126. * @warning This is internal MAC functionality, needed for the realtime channel access.
  127. * This function must not be used by other modules.
  128. */
  129. static inline void mac_timer_critical_stop(void)
  130. {
  131. hal_timer_critical_stop();
  132. }
  133. /** @} */
  134. #endif // MAC_TIME_H_INCLUDED