TimerP.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (c) 2016-2017, Texas Instruments Incorporated
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of Texas Instruments Incorporated nor the names of
  17. * its contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /** ============================================================================
  33. * @file TimerP.h
  34. *
  35. * @brief Timer interface for the RTOS Porting Interface
  36. *
  37. * The TimerP module can be used to create a timer (that is, to mark a timer
  38. * for use) and configure it to call a tickFxn when the timer expires.
  39. *
  40. * The timer can be configured as a one-shot or a continuous mode timer.
  41. * The period can be specified in timer counts or microseconds.
  42. * The timer interrupt always uses the Hwi dispatcher. The Timer tickFxn runs
  43. * in the context of a Hwi thread. The Timer module automatically creates a
  44. * Hwi instance for the timer interrupt.
  45. * ============================================================================
  46. */
  47. #ifndef ti_osal_TimerP__include
  48. #define ti_osal_TimerP__include
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. #include <stdint.h>
  53. #include <stdbool.h>
  54. #include <stddef.h>
  55. /*!
  56. * Const used to specify any timer
  57. */
  58. #define TimerP_ANY (~(uint32_t)0u)
  59. /*!
  60. * Max value of Timer period for PeriodType_COUNTS
  61. */
  62. #define TimerP_MAX_PERIOD ((uint32_t)0xffffffffu)
  63. /*!
  64. * Use default values as provided by the OSAL package
  65. */
  66. #define TimerP_USE_DEFAULT (-(int32_t)1)
  67. /*!
  68. * @brief Frequency-in-hertz struct
  69. */
  70. typedef struct TimerP_FreqHz_s {
  71. uint32_t hi; /*!< most significant 32-bits of frequency */
  72. uint32_t lo; /*!< least significant 32-bits of frequency */
  73. } TimerP_FreqHz;
  74. /*!
  75. * @brief Status codes for TimerP APIs
  76. */
  77. typedef enum TimerP_Status_e {
  78. TimerP_OK = 0,
  79. TimerP_FAILURE = (-(int32_t) 1),
  80. TimerP_NOT_AVAILABLE = (-(int32_t) 2),
  81. TimerP_BAD_INT_NUM = (-(int32_t) 3),
  82. TimerP_ISR_HOOK_ERR = (-(int32_t) 4)
  83. } TimerP_Status;
  84. /*!
  85. * @brief Timer period units for TimerP APIs
  86. */
  87. typedef enum TimerP_PeriodType_e {
  88. TimerP_PeriodType_MICROSECS, /*!< timer period is in micro seconds */
  89. TimerP_PeriodType_COUNTS /*!< timer period is in counts */
  90. } TimerP_PeriodType;
  91. /*!
  92. * @brief Timer runtime modes for TimerP APIs
  93. */
  94. typedef enum TimerP_RunMode_e {
  95. TimerP_RunMode_CONTINUOUS, /*!< timer is periodic and runs continuously */
  96. TimerP_RunMode_ONESHOT /*!< timer runs for a single period values and stops */
  97. } TimerP_RunMode;
  98. /*!
  99. * @brief Timer start modes for TimerP APIs
  100. */
  101. typedef enum TimerP_StartMode_e {
  102. TimerP_StartMode_AUTO, /*!< timer starts automatically after create*/
  103. TimerP_StartMode_USER /*!< timer will be started by the user */
  104. }TimerP_StartMode;
  105. /*!
  106. * @brief Timer mode for 64 bit timers (KeyStone devices)
  107. * @note this is not applicable for non KeyStone devices such as AM572x etc
  108. */
  109. typedef enum TimerP_Timer64Mode_e {
  110. TimerP_Timer64Mode_64BITGPTIMER,
  111. TimerP_Timer64Mode_UNCHAINED,
  112. TimerP_Timer64Mode_CHAINED
  113. } TimerP_Timer64Mode;
  114. /*!
  115. * @brief Timer half when 64 bit timer is split into two 32 bit timer (Keystone devices)
  116. * @note this is not applicable for non KeyStone devices such as AM572x etc
  117. */
  118. typedef enum TimerP_Timer64Half_e {
  119. TimerP_Timer64Half_LOWER,
  120. TimerP_Timer64Half_UPPER,
  121. TimerP_Timer64Half_DEFAULT
  122. }TimerP_Timer64Half;
  123. /*!
  124. * @brief Opaque client reference to an instance of a TimerP
  125. *
  126. * A TimerP_Handle returned from the ::TimerP_create represents that instance.
  127. * and then is used in the other instance based functions (e.g. ::TimerP_start,
  128. * ::TimerP_stop, etc.).
  129. */
  130. typedef void *TimerP_Handle;
  131. /*!
  132. * @brief Prototype for a TimerP function.
  133. */
  134. typedef void (*TimerP_Fxn)(uintptr_t arg);
  135. /*!
  136. * @brief Basic TimerP Parameters
  137. *
  138. * Structure that contains the parameters passed into ::TimerP_create
  139. * when creating a TimerP instance. The ::TimerP_Params_init function should
  140. * be used to initialize the fields to default values before the application sets
  141. * the fields manually. The TimerP default parameters are noted in
  142. * TimerP_Params_init.
  143. */
  144. typedef struct TimerP_Params_s {
  145. char *name; /*!< Name of the timer instance. Memory must
  146. persist for the life of the clock instance.
  147. This can be used for debugging purposes, or
  148. set to NULL if not needed. */
  149. uint32_t periodType; /*!< Period type, default micro seconds */
  150. int32_t extfreqLo; /*!< least siginificant 32-bits of ext frequency
  151. set to 0 to use internal clk freq */
  152. int32_t extfreqHi; /*!< most siginificant 32-bits of ext frequency
  153. set to 0 to use internal clk freq */
  154. int32_t intfreqLo; /*!< least siginificant 32-bits of int frequency
  155. set to 0 to use default internal clk freq */
  156. int32_t intfreqHi; /*!< most siginificant 32-bits of int frequency
  157. set to 0 to use default internal clk freq */
  158. uint32_t startMode; /*!< timer start mode */
  159. uint32_t runMode; /*!< timer run mode */
  160. uint32_t period; /*!< Period of a tick */
  161. TimerP_Timer64Mode timerMode; /*!< timer mode for 64bit timer */
  162. TimerP_Timer64Half timerHalf; /*!< timer half for 64bit timer */
  163. int32_t intNum; /*!< Hwi Interrupt number to be used by Timer */
  164. #if defined (_TMS320C6X)
  165. int32_t eventId; /*!< Hwi event Id to be used by the Timer */
  166. #endif
  167. void* arg; /*!< Argument passed into the timer function. */
  168. } TimerP_Params;
  169. /*!
  170. * @brief Function to create a timer object.
  171. *
  172. * @param timerId Timer Id
  173. *
  174. * @param tickFxn Function that runs upon timer expiry
  175. *
  176. * @param params Pointer to the instance configuration parameters. NULL
  177. * denotes to use the default parameters. The TimerP default
  178. * parameters are noted in ::TimerP_Params_init.
  179. *
  180. * @return A TimerP_Handle on success or a NULL on an error. This handle can
  181. * be passed to TimerP_start()
  182. */
  183. extern TimerP_Handle TimerP_create(int32_t id,
  184. TimerP_Fxn tickFxn,
  185. TimerP_Params *params);
  186. /*!
  187. * @brief Function to delete a timer.
  188. *
  189. * @param handle A TimerP_Handle returned from ::TimerP_create
  190. *
  191. * @return Status of the function.
  192. * - TimerP_OK: Deleted the timer instance
  193. * - TimerP_FAILURE: Timed out waiting to delete the timer object.
  194. */
  195. extern TimerP_Status TimerP_delete(TimerP_Handle handle);
  196. /*!
  197. * @brief Function to set timer period specified in micro seconds
  198. * A best-effort method will be used to set the period register.
  199. * There might be a slight rounding error based on resolution of
  200. * timer period register. If the timer frequency cannot support
  201. * the requested period, i.e. the timer period register cannot
  202. * support the requested period, then this function returns false.
  203. * TimerP_setPeriodMicroSecs() invokes TimerP_stop() prior to setting
  204. * the period and leaves the timer in the stopped state.
  205. *
  206. * @param handle A TimerP_Handle returned from ::TimerP_create
  207. *
  208. * @param microsecs time in micro seconds
  209. *
  210. * @return Status of the function.
  211. * - TimerP_OK: Deleted the timer instance
  212. * - TimerP_FAILURE: Timed out waiting to delete the timer object.
  213. */
  214. extern TimerP_Status TimerP_setPeriodMicroSecs(TimerP_Handle handle, uint32_t microsecs);
  215. /*!
  216. * @brief Initialize params structure to default values.
  217. *
  218. * The default parameters are:
  219. * - name: NULL
  220. * - arg: 0
  221. *
  222. * @param params Pointer to the instance configuration parameters.
  223. */
  224. extern void TimerP_Params_init(TimerP_Params *params);
  225. /*!
  226. * @brief Function to start a timer.
  227. *
  228. * @param handle A TimerP_Handle returned from ::TimerP_create
  229. *
  230. * @return Status of the functions
  231. * - TimerP_OK: Scheduled the timer function successfully
  232. * - TimerP_FAILURE: The API failed.
  233. */
  234. extern TimerP_Status TimerP_start(TimerP_Handle handle);
  235. /*!
  236. * @brief Function to stop a timer.
  237. *
  238. * @param handle A TimerP_Handle returned from ::TimerP_create
  239. *
  240. * @return Status of the functions
  241. * - TimerP_OK: Scheduled the timer function successfully
  242. * - TimerP_FAILURE: The API failed.
  243. */
  244. extern TimerP_Status TimerP_stop(TimerP_Handle handle);
  245. /*!
  246. * @brief Function to clear the interrupt of the timer.
  247. *
  248. * @param handle A TimerP_Handle returned from ::TimerP_create
  249. *
  250. * @return Status of the function
  251. * - TimerP_OK: Clear the interrupt of the timer function successfully.
  252. *
  253. */
  254. extern TimerP_Status TimerP_ClearInterrupt(TimerP_Handle handle);
  255. #ifdef __cplusplus
  256. }
  257. #endif
  258. #endif /* ti_osal_ClockP__include */