hal_pwm.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /**
  2. * @file hal_pwm.h
  3. * @author Xiao, Lifan (xiaolf6@midea.com)
  4. * @brief
  5. * @version 0.1
  6. * @date 2021-08-09
  7. *
  8. * @copyright Copyright (c) 2021
  9. *
  10. * @details The interface of PWM module
  11. *
  12. */
  13. #ifndef _HAL_PWM_H_
  14. #define _HAL_PWM_H_
  15. #include "hal_config.h"
  16. /****************************************************************
  17. *
  18. * Standard Interface
  19. *
  20. * *************************************************************/
  21. /********************/
  22. /* Type definations */
  23. /********************/
  24. /**
  25. * @brief The PWM wave type
  26. * @note Four type of PWM wave is supported, as
  27. *
  28. * RightAligned:
  29. * ┌──────
  30. * │
  31. * │
  32. * ──────┘
  33. *
  34. * LeftAligned:
  35. * ──────┐
  36. * │
  37. * │
  38. * └──────
  39. *
  40. * CenterAligned:
  41. * ┌───┐
  42. * │ │
  43. * │ │
  44. * ────┘ └────
  45. *
  46. * CenterAlignedInverse:
  47. * ────┐ ┌────
  48. * │ │
  49. * │ │
  50. * └───┘
  51. *
  52. */
  53. typedef enum
  54. {
  55. RightAligned,
  56. LeftAligned,
  57. CenterAligned,
  58. CenterAlignedInverse,
  59. EdgeAlignedAutoReverse,
  60. #if HAL_PWM_SUPPORT_CUSTOM_VECTOR_TYPE
  61. Custom,
  62. #endif
  63. } Hal_Pwm_WaveModeEnum;
  64. typedef enum
  65. {
  66. VectorTypeUp,
  67. VectorTypeDown,
  68. VectorTypeUpDown,
  69. VectorTypeDownUp,
  70. } Hal_Pwm_VectorType;
  71. /******************************/
  72. /* Global Interface Variables */
  73. /******************************/
  74. /* !!!Important!!! */
  75. /* Interface Variable Should Not Be Modified By Application */
  76. /**
  77. * @brief The clock of the pwm module (in unit Hz)
  78. *
  79. */
  80. extern uint32_t Hal_Pwm_Clock;
  81. /**
  82. * @brief The period of the pwm module (in unit Ticks)
  83. *
  84. */
  85. extern uint32_t Hal_Pwm_PeriodTicks;
  86. /**
  87. * @brief The period of the pwm module (in unit us)
  88. *
  89. */
  90. extern uint32_t Hal_Pwm_PeriodUs;
  91. /**
  92. * @brief Avaliable pwm channel number
  93. *
  94. */
  95. extern const uint32_t Hal_Pwm_ChannelNum;
  96. /**
  97. * @brief Actual phase duty cycle (in unit ticks)
  98. *
  99. */
  100. extern uint32_t *Hal_Pwm_ActualDutyCycleTicks;
  101. /**
  102. * @brief Drive pwm output status
  103. *
  104. */
  105. extern int8_t Hal_Pwm_DriveOutputEnable;
  106. /*************************/
  107. /* Global Interface Hook */
  108. /*************************/
  109. /**
  110. * @brief Function hook called at Zero Interrupt
  111. *
  112. */
  113. extern void (*Hal_Pwm_ZeroIsrHook)();
  114. /**
  115. * @brief Function hook called at Period Interrupt
  116. *
  117. */
  118. extern void (*Hal_Pwm_PeriodIsrHook)();
  119. /*****************************************/
  120. /* Application Layer Interface Functions */
  121. /*****************************************/
  122. /**
  123. * @brief Set the duty cycle for all channel (in q15 format)
  124. *
  125. * @param da Pointer to the list of duty cycles
  126. * @param channels The number of channel to set duty cycle
  127. *
  128. * @note If current PWM mode is EdgeAlignedAutoReverse, the modulation mode will reverse after calling this function
  129. */
  130. void Hal_Pwm_SetDutyCycle(int32_t *da, int8_t channels);
  131. /**
  132. * @brief Set the duty cycle for specified channel (in q15 format)
  133. *
  134. * @param duty duty cycle
  135. * @param chindex The index of channel to set duty cycle
  136. *
  137. * @note Calling this function will not affect PWM mode
  138. */
  139. void Hal_Pwm_SetChannelDutyCycle(int32_t duty, int8_t chindex);
  140. /**
  141. * @brief Set the pwm period (in ticks)
  142. *
  143. * @param ticks Pwm period
  144. */
  145. void Hal_Pwm_SetPeriod(int32_t ticks);
  146. /**
  147. * @brief Set the Wave Mode
  148. *
  149. * @param mode Target mode
  150. */
  151. void Hal_Pwm_SetWaveMode(Hal_Pwm_WaveModeEnum mode);
  152. /**
  153. * @brief Set the pwm output deadband
  154. *
  155. * @param ticks Deadband length
  156. */
  157. void Hal_Pwm_SetDeadband(int32_t ticks);
  158. /**
  159. * @brief Enable drive pwm immediately
  160. *
  161. */
  162. void Hal_Pwm_EnableDriveOutput();
  163. /**
  164. * @brief Disable drive pwm output immediately
  165. *
  166. */
  167. void Hal_Pwm_DisableDriveOutput();
  168. /**
  169. * @brief Enable channel pwm immediately
  170. *
  171. * @param ch_index Channel index
  172. */
  173. void Hal_Pwm_EnableChannelOutput(uint8_t ch_index);
  174. /**
  175. * @brief Disable channel pwm immediately
  176. *
  177. * @param ch_index Channel index
  178. */
  179. void Hal_Pwm_DisableChannelOutput(uint8_t ch_index);
  180. /**
  181. * @brief Enable standard hooks, function address for enable, 0 for disable
  182. *
  183. * @param zeroHook Zero interrupt hook address
  184. * @param prdHook Period interrupt hook address
  185. */
  186. void Hal_Pwm_EnableStdHooks(void *zeroHook, void *prdHook);
  187. /**************************************/
  188. /* Platform Layer Interface Functions */
  189. /**************************************/
  190. /**
  191. * @brief Init pwm module
  192. *
  193. */
  194. void Hal_Pwm_Init();
  195. #if HAL_PWM_SUPPORT_CUSTOM_VECTOR_TYPE
  196. /****************************************************************
  197. *
  198. * Extended Interface
  199. * Support Custom Modulation Like TSPWM
  200. * Different Vector Type On Different Channel
  201. *
  202. * *************************************************************/
  203. void Hal_Pwm_Ext_SetCustomVectorType(Hal_Pwm_VectorType *type, uint8_t channels);
  204. #endif
  205. #if HAL_PWM_SUPPORT_SINGLE_RESISTOR_SAMPLING
  206. /****************************************************************
  207. *
  208. * Extended Interface
  209. * Support Single Resistor Sampling
  210. *
  211. * *************************************************************/
  212. /*************************/
  213. /* Global Interface Hook */
  214. /*************************/
  215. /**
  216. * @brief Function hook for pwm sampling trigger
  217. *
  218. */
  219. void (*Hal_Pwm_Ext_SamplingTriggerHook)();
  220. /*****************************************/
  221. /* Application Layer Interface Functions */
  222. /*****************************************/
  223. /**
  224. * @brief Set the duty cycle of the extra channel, affect the
  225. * sampling trigger moment
  226. *
  227. * @param dutycycle Duty cycle in q15 format
  228. */
  229. void Hal_Pwm_Ext_SetSamplingDutyCycle(uint32_t dutycycle);
  230. /**************************************/
  231. /* Platform Layer Interface Functions */
  232. /**************************************/
  233. /**
  234. * @brief Extra init process to support single resistor sampling
  235. * @note
  236. * An extra channel will be used to trigger sampling at attribute time.
  237. * The counter of extra channel always runs counting up mode for
  238. * controlling the sampling trigger time
  239. *
  240. */
  241. void Hal_Pwm_Ext_Init();
  242. #endif
  243. #if HAL_PWM_SUPPORT_COMPLEMENTARY_PULSE_GENERATION
  244. #endif
  245. #if HAL_PWM_SUPPORT_HALF_PULSE_GENERATION
  246. #endif
  247. #endif