ftimer_tacho.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: ftimer_tacho.h
  15. * Date: 2022-02-10 14:53:42
  16. * LastEditTime: 2022-02-18 09:09:43
  17. * Description:  This file is for user ftimer_tacho API definition
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. * 1.0 liushengming 2022/02/18 first commit
  23. */
  24. #ifndef FTIMER_TACHO_H
  25. #define FTIMER_TACHO_H
  26. #include "ftypes.h"
  27. #include "fdebug.h"
  28. #include "ferror_code.h"
  29. #ifdef __cplusplus
  30. extern "C"
  31. {
  32. #endif
  33. #define FTIMER_TACHO_SUCCESS FT_SUCCESS
  34. #define FTIMER_TACHO_ERR_INVAL_PARM FT_MAKE_ERRCODE(ErrModBsp, ErrBspTimer, 1)
  35. #define FTIMER_TACHO_ERR_NOT_READY FT_MAKE_ERRCODE(ErrModBsp, ErrBspTimer, 2)
  36. #define FTIMER_TACHO_ERR_INIT_FAILED FT_MAKE_ERRCODE(ErrModBsp, ErrBspTimer, 3)
  37. #define FTIMER_TACHO_ERR_NOT_SUPPORT FT_MAKE_ERRCODE(ErrModBsp, ErrBspTimer, 4)
  38. #define FTIMER_TACHO_ERR_INVAL_STATE FT_MAKE_ERRCODE(ErrModBsp, ErrBspTimer, 5)
  39. #define FTIMER_TACHO_ERR_IS_READ FT_MAKE_ERRCODE(ErrModBsp, ErrBspTimer, 6)
  40. #define FTIMER_TACHO_ERR_ABORT FT_MAKE_ERRCODE(ErrModBsp, ErrBspTimer, 7)
  41. #define FTIMER_TACHO_ERR_FAILED FT_MAKE_ERRCODE(ErrModBsp, ErrBspTimer, 8)
  42. #define FTIMER_DEBUG_TAG "TIMER"
  43. #define FTIMER_ERROR(format, ...) FT_DEBUG_PRINT_E(FTIMER_DEBUG_TAG, format, ##__VA_ARGS__)
  44. #define FTIMER_INFO(format, ...) FT_DEBUG_PRINT_I(FTIMER_DEBUG_TAG, format, ##__VA_ARGS__)
  45. #define FTIMER_DEBUG(format, ...) FT_DEBUG_PRINT_D(FTIMER_DEBUG_TAG, format, ##__VA_ARGS__)
  46. #define FTACHO_DEBUG_TAG "TACHO"
  47. #define FTACHO_ERROR(format, ...) FT_DEBUG_PRINT_E(FTACHO_DEBUG_TAG, format, ##__VA_ARGS__)
  48. #define FTACHO_INFO(format, ...) FT_DEBUG_PRINT_I(FTACHO_DEBUG_TAG, format, ##__VA_ARGS__)
  49. #define FTACHO_DEBUG(format, ...) FT_DEBUG_PRINT_D(FTACHO_DEBUG_TAG, format, ##__VA_ARGS__)
  50. typedef enum
  51. {
  52. /*TimerTacho mode */
  53. FTIMER_WORK_MODE_TIMER = 0,
  54. FTIMER_WORK_MODE_TACHO,
  55. FTIMER_WORK_MODE_CAPTURE
  56. } FTimerTachoModeType;
  57. typedef enum
  58. {
  59. /*Timer count mode*/
  60. FTIMER_FREE_RUN = 0,
  61. FTIMER_RESTART
  62. } FTimerCntModeType;
  63. typedef enum
  64. {
  65. FTIMER_32_BITS = 0,
  66. FTIMER_64_BITS
  67. } FTimerBitsType;
  68. typedef enum
  69. {
  70. FTIMER_ONCE_CMP = 0,
  71. FTIMER_CYC_CMP
  72. } FTimerCmpType;
  73. typedef enum
  74. {
  75. FTACHO_EVENT_OVER = 0, /*tacho超速事件*/
  76. FTACHO_EVENT_UNDER, /*tacho低速事件*/
  77. FTIMER_EVENT_ROLL_OVER, /*计数器翻转事件*/
  78. FTIMER_EVENT_ONCE_CMP, /*单次定时输出事件*/
  79. FTIMER_EVENT_CYC_CMP, /*重复定时输出事件*/
  80. FTACHO_EVENT_CAPTURE, /*tacho输入捕获事件*/
  81. FMAX_TIMER_TACHO_EVENT
  82. } FTimerTachoEventType;
  83. typedef enum
  84. {
  85. FTACHO_FALLING_EDGE = 0, /*下降沿触发模式*/
  86. FTACHO_RISING_EDGE, /*上升沿触发模式*/
  87. FTACHO_DOUBLE_EDGE /*双边沿触发模式*/
  88. } FTachoEdgeType;
  89. typedef enum
  90. {
  91. FTACHO_JITTER_LEVEL0 = 0, /*消抖等级*/
  92. FTACHO_JITTER_LEVEL1,
  93. FTACHO_JITTER_LEVEL2,
  94. FTACHO_JITTER_LEVEL3,
  95. } FTachoJitterLevelType;
  96. typedef struct
  97. {
  98. u32 id; /* id of timer tacho */
  99. char name[12]; /* instance name */
  100. u32 irq_priority; /* intr priority */
  101. u32 work_mode; /* timer/tacho/capture mode */
  102. /* for timer function */
  103. u32 timer_mode; /* free-run/restart */
  104. u32 timer_bits; /* 32/64 bits */
  105. u32 cmp_type; /* once/cycle cmp */
  106. boolean clear_cnt; /* clear timer counts */
  107. boolean force_load; /* start count from start val */
  108. /* for tacho function */
  109. u32 edge_mode; /* rising/falling/double */
  110. u32 jitter_level; /* jitter level */
  111. u32 plus_num; /* plus_num of period to calculate rpm */
  112. u32 captue_cnt; /* in capture mode, when cnt reach this val, intr asserted */
  113. } FTimerTachoConfig;
  114. typedef void (*FTimerEventHandler)(void *instance_p);
  115. typedef struct
  116. {
  117. FTimerTachoConfig config; /* Current active configs */
  118. boolean isready; /* Device is initialized and ready */
  119. FTimerEventHandler evt_handlers[FMAX_TIMER_TACHO_EVENT];/* event handler for interrupt */
  120. } FTimerTachoCtrl;
  121. /* Time & Tacho API */
  122. /*将控制器复位*/
  123. FError FTimerSoftwareReset(FTimerTachoCtrl *instance_p);
  124. /*获取中断设置*/
  125. u32 FTimerGetInterruptMask(FTimerTachoCtrl *instance_p);
  126. /*设置中断,根据不同的intrType,将对于的中断mask置位*/
  127. void FTimerSetInterruptMask(FTimerTachoCtrl *instance_p,
  128. FTimerTachoEventType intrType,
  129. boolean enable);
  130. /*启动timer_tacho外设,根据不同的功能,开启使能位*/
  131. FError FTimerStart(FTimerTachoCtrl *instance_p);
  132. /*停止timer外设,根据不同的功能,关闭使能位,计数值停止并冻结*/
  133. FError FTimerStop(FTimerTachoCtrl *instance_p);
  134. /*用于timer 与 tacho-capture两种模式的切换,切换需要失能和清除计数器*/
  135. FError FTimerSwithMode(FTimerTachoCtrl *instance_p, FTimerTachoConfig *new_config_p);
  136. /* 注册中断事件处理回调函数 */
  137. void FTimerRegisterEvtCallback(FTimerTachoCtrl *instance_p,
  138. FTimerTachoEventType evt,
  139. FTimerEventHandler callback);
  140. /*打印寄存器信息*/
  141. FError FTimeSettingDump(const FTimerTachoCtrl *instance_p);
  142. /*TimerTacho中断处理函数,如果注册回调函数,则跳转到回调函数*/
  143. void FTimerTachoIntrHandler(s32 vector, void *param);
  144. /*根据工作模式和状态设置相应的中断*/
  145. void FTimerTachoSetIntr(FTimerTachoCtrl *instance_p);
  146. /* Timer API */
  147. /*完成TimerTacho驱动实例的初始化,使之在就绪状态*/
  148. FError FTimerInit(FTimerTachoCtrl *instance_p, const FTimerTachoConfig *config_p);
  149. /*获取Timer驱动的默认配置参数*/
  150. void FTimerGetDefConfig(u32 timer_id, FTimerTachoConfig *config_p);
  151. /*设置32位计数模式下,计数器的compare的值,达到此值,如果开启中断,则开启中断*/
  152. FError FTimerSetPeriod32(FTimerTachoCtrl *instance_p, u32 ticks);
  153. /*设置64位计数模式下,计数器的compare的值,达到此值,如果开启中断,则开启中断*/
  154. FError FTimerSetPeriod64(FTimerTachoCtrl *instance_p, u64 ticks);
  155. /*32位模式下,获取计数器当前计数值*/
  156. u32 FTimerGetCurCnt32(FTimerTachoCtrl *instance_p);
  157. /*64位模式下,获取计数器当前计数值*/
  158. u64 FTimerGetCurCnt64(FTimerTachoCtrl *instance_p);
  159. /*设置计数器初始值*/
  160. FError FTimerSetStartVal(FTimerTachoCtrl *instance_p, u32 cnt);
  161. /*完成Timer驱动实例去使能,清零实例数据*/
  162. void FTimerDeInit(FTimerTachoCtrl *instance_p);
  163. /* Tacho API */
  164. /*完成Tacho驱动实例的初始化,使之在就绪状态*/
  165. FError FTachoInit(FTimerTachoCtrl *instance_p, const FTimerTachoConfig *config_p);
  166. /*获取Tacho驱动的默认配置参数*/
  167. void FTachoGetDefConfig(u32 tacho_id, FTimerTachoConfig *config_p);
  168. /*配置tach转速周期= pulse_num*/
  169. void FTachoSetCntPeriod(FTimerTachoCtrl *instance_p, u32 ticks);
  170. /*预设的 tach 最大值*/
  171. void FTachoSetOverLimit(FTimerTachoCtrl *instance_p, u32 overLim);
  172. /*预设的 tach 最小值*/
  173. void FTachoSetUnderLimit(FTimerTachoCtrl *instance_p, u32 underLim);
  174. /*根据预设采样周期的值,来获取风扇的转速值*/
  175. FError FTachoGetFanRPM(FTimerTachoCtrl *instance_p, u32 *rpm);
  176. /*获取capture模式下tacho输入脉冲的个数*/
  177. u32 FTachoGetCaptureCnt(FTimerTachoCtrl *instance_p);
  178. /*完成Tacho驱动实例去使能,清零实例数据*/
  179. void FTachoDeInit(FTimerTachoCtrl *instance_p);
  180. #ifdef __cplusplus
  181. }
  182. #endif
  183. #endif // !