core_hwtimer.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #ifndef __CORE_HWTIMER_H__
  22. #define __CORE_HWTIMER_H__
  23. /**
  24. * @brief 硬件定时器按照周期和占空比设置成PWM并启动
  25. *
  26. * @param HWTimerID 硬件定时器ID
  27. * @param Period 周期,最大24M,
  28. * @param Duty 占空比,0.1%,只有50KHz以下才能达到这个精度,500KHz到1%
  29. * @param PulseNum 总共需要的脉冲,0表示一直运行
  30. */
  31. void HWTimer_SetPWM(uint8_t HWTimerID, uint32_t Period, uint16_t Duty, uint32_t PulseNum);
  32. /**
  33. * @brief 硬件定时器按照高低电平分辨率设置成PWM并启动,时钟48M
  34. *
  35. * @param HWTimerID 硬件定时器ID
  36. * @param HighCnt 高电平时钟周期数
  37. * @param LowCnt 低电平时钟周期数
  38. * @param IsOnePulse 只触发一次脉冲
  39. */
  40. void HWTimer_StartPWM(uint8_t HWTimerID, uint32_t HighCnt, uint32_t LowCnt, uint8_t IsOnePulse);
  41. /**
  42. * @brief 硬件定时器关闭
  43. *
  44. * @param HWTimerID 硬件定时器ID
  45. */
  46. void HWTimer_Stop(uint8_t HWTimerID);
  47. /**
  48. * @brief 获取上次被停止的IOqueue的信息
  49. *
  50. * @param HWTimerID 硬件定时器ID
  51. * @param Repeat 已经重复次数
  52. * @param Count 一次循环中已经操作步骤,可能为0
  53. */
  54. void HWTimer_GetResultOperationInfo(uint8_t HWTimerID, uint32_t *Repeat, uint32_t *Count);
  55. /**
  56. * @brief 初始化IO操作序列,会完全清除之前保留的操作序列
  57. *
  58. * @param HWTimerID 硬件定时器ID
  59. * @param nCount 操作步骤总数
  60. * @param Repeat 重复次数
  61. * @param CmdDoneCB 全部完成后回调函数
  62. * @param pCmdDoneParam 回调函数的用户参数
  63. *
  64. */
  65. void HWTimer_InitOperationQueue(uint8_t HWTimerID, uint32_t nCount, uint32_t Repeat, CBFuncEx_t CmdDoneCB, void *pCmdDoneParam);
  66. /**
  67. * @brief 加入IO操作序列
  68. *
  69. * @param HWTimerID 硬件定时器ID
  70. * @param pCmd IO操作
  71. */
  72. void HWTimer_AddOperation(uint8_t HWTimerID, OPQueue_CmdStruct *pCmd);
  73. /**
  74. * @brief 启动IO操作序列
  75. *
  76. * @param HWTimerID 硬件定时器ID
  77. */
  78. void HWTimer_StartOperationQueue(uint8_t HWTimerID);
  79. /**
  80. * @brief 清空IO操作序列,需要重新AddOperation,但是不需要init
  81. *
  82. * @param HWTimerID 硬件定时器ID
  83. */
  84. void HWTimer_ClearOperationQueue(uint8_t HWTimerID);
  85. /**
  86. * @brief 完全释放IO操作序列,下次使用需要重新init,必须在HWTimer_CheckOperationQueueDone确认完成的情况下调用
  87. *
  88. * @param HWTimerID 硬件定时器ID
  89. */
  90. void HWTimer_FreeOperationQueue(uint8_t HWTimerID);
  91. /**
  92. * @brief 在OP_QUEUE_CMD_CB时,需要在CB里调用本函数来安全的结束序列操作,只能在OP_QUEUE_CMD_CB时调用
  93. *
  94. * @param HWTimerID 硬件定时器ID
  95. */
  96. void HWTimer_AddEndCmdInOperationQueue(uint8_t HWTimerID);
  97. /**
  98. * @brief 检查IO操作序列是否完成
  99. *
  100. * @param HWTimerID 硬件定时器ID
  101. * @return =0未完成,其他已完成
  102. */
  103. uint8_t HWTimer_CheckOperationQueueDone(uint8_t HWTimerID);
  104. /**
  105. * @brief 获取IO操作序列中捕获状态
  106. *
  107. * @param HWTimerID 硬件定时器ID
  108. * @param CB 捕获回调函数
  109. * @param CB 捕获回调函数的用户参数
  110. * @return 捕获次数
  111. */
  112. uint32_t HWTimer_GetOperationQueueCaptureResult(uint8_t HWTimerID, CBFuncEx_t CB, void *pParam);
  113. uint32_t HWTimer_GetOperationQueueLen(uint8_t HWTimerID);
  114. OPQueue_CmdStruct *HWTimer_GetOperationQueue(uint8_t HWTimerID);
  115. #endif