core_hwtimer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 IsOnePulse 只触发一次脉冲
  30. */
  31. void HWTimer_SetPWM(uint8_t HWTimerID, uint32_t Period, uint16_t Duty, uint8_t IsOnePulse);
  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 初始化IO操作序列,会完全清除之前保留的操作序列
  49. *
  50. * @param HWTimerID 硬件定时器ID
  51. * @param nCount 操作步骤总数
  52. * @param Repeat 重复次数
  53. * @param InputByte IO读取的次数,注意每8次1个byte,不足8个也算1个byte
  54. * @param CmdDoneCB 全部完成后回调函数
  55. * @param pCmdDoneParam 回调函数的用户参数
  56. *
  57. */
  58. void HWTimer_InitOperationQueue(uint8_t HWTimerID, uint32_t nCount, uint32_t Repeat, uint32_t InputByte, CBFuncEx_t CmdDoneCB, void *pCmdDoneParam);
  59. /**
  60. * @brief 加入IO操作序列
  61. *
  62. * @param HWTimerID 硬件定时器ID
  63. * @param pCmd IO操作
  64. */
  65. void HWTimer_AddOperation(uint8_t HWTimerID, OPQueue_CmdStruct *pCmd);
  66. /**
  67. * @brief 启动IO操作序列
  68. *
  69. * @param HWTimerID 硬件定时器ID
  70. */
  71. void HWTimer_StartOperationQueue(uint8_t HWTimerID);
  72. /**
  73. * @brief 清空IO操作序列,需要重新AddOperation,但是不需要init
  74. *
  75. * @param HWTimerID 硬件定时器ID
  76. */
  77. void HWTimer_ClearOperationQueue(uint8_t HWTimerID);
  78. /**
  79. * @brief 完全释放IO操作序列,下次使用需要重新init,必须在HWTimer_CheckOperationQueueDone确认完成的情况下调用
  80. *
  81. * @param HWTimerID 硬件定时器ID
  82. */
  83. void HWTimer_FreeOperationQueue(uint8_t HWTimerID);
  84. /**
  85. * @brief 在OP_QUEUE_CMD_CB时,需要在CB里调用本函数来安全的结束序列操作,只能在OP_QUEUE_CMD_CB时调用
  86. *
  87. * @param HWTimerID 硬件定时器ID
  88. */
  89. void HWTimer_AddEndCmdInOperationQueue(uint8_t HWTimerID);
  90. /**
  91. * @brief 检查IO操作序列是否完成
  92. *
  93. * @param HWTimerID 硬件定时器ID
  94. * @return =0未完成,其他已完成
  95. */
  96. uint8_t HWTimer_CheckOperationQueueDone(uint8_t HWTimerID);
  97. /**
  98. * @brief 获取IO操作序列中IO读取的值
  99. *
  100. * @param HWTimerID 硬件定时器ID
  101. * @param Value copy的空间
  102. * @return 获取的byte数,和init时InputByte一致
  103. */
  104. int HWTimer_GetOperationQueueInputResult(uint8_t HWTimerID, uint8_t *Value);
  105. /**
  106. * @brief 获取IO操作序列中捕获状态
  107. *
  108. * @param HWTimerID 硬件定时器ID
  109. * @param CB 捕获回调函数
  110. * @param CB 捕获回调函数的用户参数
  111. * @return 捕获次数
  112. */
  113. uint32_t HWTimer_GetOperationQueueCaptureResult(uint8_t HWTimerID, CBFuncEx_t CB, void *pParam);
  114. #endif