multi_rtimer.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*!
  2. * \file multi_rtimer.h
  3. *
  4. * \brief Timer objects and scheduling management implementation
  5. *
  6. * \copyright Revised BSD License, see section \ref LICENSE.
  7. *
  8. * \code
  9. * ______ _
  10. * / _____) _ | |
  11. * ( (____ _____ ____ _| |_ _____ ____| |__
  12. * \____ \| ___ | (_ _) ___ |/ ___) _ \
  13. * _____) ) ____| | | || |_| ____( (___| | | |
  14. * (______/|_____)_|_|_| \__)_____)\____)_| |_|
  15. * (C)2013-2017 Semtech
  16. *
  17. * \endcode
  18. *
  19. * \author Miguel Luis ( Semtech )
  20. *
  21. * \author Gregory Cristian ( Semtech )
  22. *
  23. * \author Forest-Rain
  24. */
  25. #ifndef __RTIMER_H__
  26. #define __RTIMER_H__
  27. #ifdef __cplusplus
  28. extern "C"
  29. {
  30. #endif
  31. #include <stddef.h>
  32. #include <stdbool.h>
  33. #include <stdint.h>
  34. /*!
  35. * Begins critical section
  36. */
  37. #define MULTI_RTIMER_CRITICAL_SECTION_BEGIN( ) register rt_base_t level; level = rt_hw_interrupt_disable()
  38. /*!
  39. * Ends critical section
  40. */
  41. #define MULTI_RTIMER_CRITICAL_SECTION_END( ) rt_hw_interrupt_enable(level)
  42. #if 1
  43. /*!
  44. * LoRaWAN Timer API
  45. */
  46. #define TimerInit rtimer_init
  47. #define TimerStart rtimer_start
  48. #define TimerStop rtimer_stop
  49. #define TimerReset rtimer_reset
  50. #define TimerSetValue rtimer_set_value
  51. #define TimerGetCurrentTime rtimer_get_current_time
  52. #define TimerGetElapsedTime rtimer_get_elapsed_time
  53. #define TimerEvent_t timer_event_t
  54. #endif
  55. /*!
  56. * \brief Timer object description
  57. */
  58. typedef struct TimerEvent_s
  59. {
  60. uint32_t Timestamp; //! Current timer value
  61. uint32_t ReloadValue; //! Timer delay value
  62. bool IsStarted; //! Is the timer currently running
  63. bool IsNext2Expire; //! Is the next timer to expire
  64. void ( *Callback )( void ); //! Timer IRQ callback function
  65. void *Context; //! User defined data object pointer to pass back
  66. struct TimerEvent_s *Next; //! Pointer to the next Timer object.
  67. }timer_event_t;
  68. /*!
  69. * \brief Timer time variable definition
  70. */
  71. #ifndef TimerTime_t
  72. typedef uint32_t TimerTime_t;
  73. #define TIMERTIME_T_MAX ( ( uint32_t )~0 )
  74. #endif
  75. /*!
  76. * \brief Initializes the timer object
  77. *
  78. * \remark rtimer_set_value function must be called before starting the timer.
  79. * this function initializes timestamp and reload value at 0.
  80. *
  81. * \param [IN] obj Structure containing the timer object parameters
  82. * \param [IN] callback Function callback called at the end of the timeout
  83. */
  84. void rtimer_init( timer_event_t *obj, void ( *callback )( void ) );
  85. /*!
  86. * \brief Starts and adds the timer object to the list of timer events
  87. *
  88. * \param [IN] obj Structure containing the timer object parameters
  89. */
  90. void rtimer_start( timer_event_t *obj );
  91. /*!
  92. * \brief Stops and removes the timer object from the list of timer events
  93. *
  94. * \param [IN] obj Structure containing the timer object parameters
  95. */
  96. void rtimer_stop( timer_event_t *obj );
  97. /*!
  98. * \brief Resets the timer object
  99. *
  100. * \param [IN] obj Structure containing the timer object parameters
  101. */
  102. void rtimer_reset( timer_event_t *obj );
  103. /*!
  104. * \brief Set timer new timeout value
  105. *
  106. * \param [IN] obj Structure containing the timer object parameters
  107. * \param [IN] value New timer timeout value
  108. */
  109. void rtimer_set_value( timer_event_t *obj, uint32_t value );
  110. /*!
  111. * \brief Read the current time
  112. *
  113. * \retval time returns current time
  114. */
  115. TimerTime_t rtimer_get_current_time( void );
  116. /*!
  117. * \brief Return the Time elapsed since a fix moment in Time
  118. *
  119. * \remark TimerGetElapsedTime will return 0 for argument 0.
  120. *
  121. * \param [IN] past fix moment in Time
  122. * \retval time returns elapsed time
  123. */
  124. TimerTime_t rtimer_get_elapsed_time( TimerTime_t past );
  125. /*!
  126. * \brief Computes the temperature compensation for a period of time on a
  127. * specific temperature.
  128. *
  129. * \param [IN] period Time period to compensate
  130. * \param [IN] temperature Current temperature
  131. *
  132. * \retval Compensated time period
  133. */
  134. TimerTime_t rtimer_temp_compensation( TimerTime_t period, float temperature );
  135. /*!
  136. * Timer IRQ event handler
  137. */
  138. void rtimer_irq_handler( void );
  139. /*!
  140. * \brief Processes pending timer events
  141. */
  142. void rtimer_process( void );
  143. #ifdef __cplusplus
  144. }
  145. #endif
  146. #endif // __RTIMER_H__