multi_rtimer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 CRITICAL_SECTION_BEGIN( ) register rt_base_t level; level = rt_hw_interrupt_disable()
  38. /*!
  39. * Ends critical section
  40. */
  41. #define 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. #endif
  53. /*!
  54. * \brief Timer object description
  55. */
  56. typedef struct TimerEvent_s
  57. {
  58. uint32_t Timestamp; //! Current timer value
  59. uint32_t ReloadValue; //! Timer delay value
  60. bool IsStarted; //! Is the timer currently running
  61. bool IsNext2Expire; //! Is the next timer to expire
  62. void ( *Callback )( void ); //! Timer IRQ callback function
  63. void *Context; //! User defined data object pointer to pass back
  64. struct TimerEvent_s *Next; //! Pointer to the next Timer object.
  65. }timer_event_t;
  66. /*!
  67. * \brief Timer time variable definition
  68. */
  69. #ifndef TimerTime_t
  70. typedef uint32_t TimerTime_t;
  71. #define TIMERTIME_T_MAX ( ( uint32_t )~0 )
  72. #endif
  73. /*!
  74. * \brief Initializes the timer object
  75. *
  76. * \remark rtimer_set_value function must be called before starting the timer.
  77. * this function initializes timestamp and reload value at 0.
  78. *
  79. * \param [IN] obj Structure containing the timer object parameters
  80. * \param [IN] callback Function callback called at the end of the timeout
  81. */
  82. void rtimer_init( timer_event_t *obj, void ( *callback )( void ) );
  83. /*!
  84. * \brief Starts and adds the timer object to the list of timer events
  85. *
  86. * \param [IN] obj Structure containing the timer object parameters
  87. */
  88. void rtimer_start( timer_event_t *obj );
  89. /*!
  90. * \brief Stops and removes the timer object from the list of timer events
  91. *
  92. * \param [IN] obj Structure containing the timer object parameters
  93. */
  94. void rtimer_stop( timer_event_t *obj );
  95. /*!
  96. * \brief Resets the timer object
  97. *
  98. * \param [IN] obj Structure containing the timer object parameters
  99. */
  100. void rtimer_reset( timer_event_t *obj );
  101. /*!
  102. * \brief Set timer new timeout value
  103. *
  104. * \param [IN] obj Structure containing the timer object parameters
  105. * \param [IN] value New timer timeout value
  106. */
  107. void rtimer_set_value( timer_event_t *obj, uint32_t value );
  108. /*!
  109. * \brief Read the current time
  110. *
  111. * \retval time returns current time
  112. */
  113. TimerTime_t rtimer_get_current_time( void );
  114. /*!
  115. * \brief Computes the temperature compensation for a period of time on a
  116. * specific temperature.
  117. *
  118. * \param [IN] period Time period to compensate
  119. * \param [IN] temperature Current temperature
  120. *
  121. * \retval Compensated time period
  122. */
  123. TimerTime_t rtimer_temp_compensation( TimerTime_t period, float temperature );
  124. /*!
  125. * Timer IRQ event handler
  126. */
  127. void rtimer_irq_handler( void );
  128. /*!
  129. * \brief Processes pending timer events
  130. */
  131. void rtimer_process( void );
  132. #ifdef __cplusplus
  133. }
  134. #endif
  135. #endif // __RTIMER_H__