ftimer_tacho.h 8.0 KB

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