1
0

hpm_pwm_drv.h 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_PWM_DRV_H
  8. #define HPM_PWM_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_pwm_regs.h"
  11. #include "hpm_soc_feature.h"
  12. /**
  13. * @brief PWM driver APIs
  14. * @defgroup pwm_interface PWM driver APIs
  15. * @ingroup motor_interfaces
  16. * @{
  17. *
  18. */
  19. #define PWM_UNLOCK_KEY (0xB0382607UL)
  20. /* IRQ enable bit mask */
  21. #define PWM_IRQ_FAULT PWM_IRQEN_FAULTIRQE_MASK
  22. #define PWM_IRQ_EX_RELOAD PWM_IRQEN_XRLDIRQE_MASK
  23. #define PWM_IRQ_HALF_RELOAD PWM_IRQEN_HALFRLDIRQE_MASK
  24. #define PWM_IRQ_RELOAD PWM_IRQEN_RLDIRQE_MASK
  25. #define PWM_IRQ_CMP(x) PWM_IRQEN_CMPIRQEX_SET((1 << x))
  26. /* PWM force output mask */
  27. #define PWM_FORCE_OUTPUT(pwm_index, force_output) \
  28. (force_output << (pwm_index << 1))
  29. #define PWM_DUTY_CYCLE_FP_MAX ((1U << 24) - 1)
  30. /**
  31. * @brief pwm trigger mode
  32. *
  33. */
  34. typedef enum pwm_counter_type {
  35. pwm_counter_type_capture_rising_edge, /**< rising edge trigger*/
  36. pwm_counter_type_capture_falling_edge, /**< falling edge trigger*/
  37. } pwm_counter_type_t;
  38. /**
  39. * @brief pwm cmp mode
  40. *
  41. */
  42. typedef enum pwm_cmp_mode {
  43. pwm_cmp_mode_output_compare = 0, /**< output compare*/
  44. pwm_cmp_mode_input_capture = 1, /**< input compare*/
  45. } pwm_cmp_mode_t;
  46. /**
  47. * @brief update time of the shadow register
  48. *
  49. */
  50. typedef enum pwm_register_update {
  51. pwm_shadow_register_update_on_shlk = 0, /**< after software set shlk bit of shlk register*/
  52. pwm_shadow_register_update_on_modify = 1, /**< immediately after the register being modified*/
  53. pwm_shadow_register_update_on_hw_event = 2, /**< after hardware event assert*/
  54. pwm_shadow_register_update_on_sh_synci = 3, /**< after SHSYNCI assert */
  55. } pwm_shadow_register_update_trigger_t;
  56. /**
  57. * @brief configure the state of channel 0-7 outputs when the forced output is in effect
  58. *
  59. */
  60. typedef enum pwm_fault_mode {
  61. pwm_fault_mode_force_output_0 = 0, /**< fault forced output logic 0 */
  62. pwm_fault_mode_force_output_1 = 1, /**< fault forced output logic 1 */
  63. pwm_fault_mode_force_output_highz = 2, /**< turn off output, pin becomes high resistance */
  64. } pwm_fault_mode_t;
  65. /**
  66. * @brief select when to recover PWM output after fault
  67. *
  68. */
  69. typedef enum pwm_fault_recovery_trigger {
  70. pwm_fault_recovery_immediately = 0, /**< immediately*/
  71. pwm_fault_recovery_on_reload = 1, /**< after pwm timer counter reload time*/
  72. pwm_fault_recovery_on_hw_event = 2, /**< after hardware event assert*/
  73. pwm_fault_recovery_on_fault_clear = 3, /**< after software write faultclr bit in GCR register*/
  74. } pwm_fault_recovery_trigger_t;
  75. /**
  76. * @brief fault input signal
  77. *
  78. */
  79. typedef enum pwm_fault_source {
  80. pwm_fault_source_internal_0 = PWM_GCR_FAULTI0EN_MASK, /**< FAULTI0 */
  81. pwm_fault_source_internal_1 = PWM_GCR_FAULTI1EN_MASK, /**< FAULTI1 */
  82. pwm_fault_source_internal_2 = PWM_GCR_FAULTI2EN_MASK, /**< FAULTI2 */
  83. pwm_fault_source_internal_3 = PWM_GCR_FAULTI3EN_MASK, /**< FAULTI3 */
  84. pwm_fault_source_external_0 = PWM_GCR_FAULTE0EN_MASK, /**< EXFAULTI0 */
  85. pwm_fault_source_external_1 = PWM_GCR_FAULTE1EN_MASK, /**< EXFAULTI1 */
  86. pwm_fault_source_debug = PWM_GCR_DEBUGFAULT_MASK, /**< Debug fault */
  87. } pwm_fault_source_t;
  88. /**
  89. * @brief Select sources for force output
  90. *
  91. */
  92. typedef enum pwm_force_source {
  93. pwm_force_source_force_input = 0, /**< force output is enabled when FRCI assert */
  94. pwm_force_source_software = 1, /**< force output is enabled by software write swfrc to 1 */
  95. } pwm_force_source_t;
  96. /**
  97. * @brief select when the FRCMD shadow register will be loaded to its work register
  98. *
  99. */
  100. typedef enum pwm_force_cmd_timing {
  101. pwm_force_immediately = 0, /**< after software set shlk bit of shlk register */
  102. pwm_force_at_reload = 1, /**< immediately after the register being modified */
  103. pwm_force_at_synci = 2, /**< after hardware event assert */
  104. pwm_force_none = 3, /**< after SHSYNCI assert */
  105. } pwm_force_cmd_timing_t;
  106. /**
  107. * @brief pwm output type
  108. *
  109. */
  110. typedef enum pwm_output_type {
  111. pwm_output_0 = 0, /**< output 0 */
  112. pwm_output_1 = 1, /**< output 1 */
  113. pwm_output_high_z = 2, /**< output */
  114. pwm_output_no_force = 3,
  115. } pwm_output_type_t;
  116. /**
  117. * @brief pwm compare config
  118. *
  119. */
  120. typedef struct pwm_cmp_config {
  121. uint32_t cmp; /**< compare value */
  122. bool enable_ex_cmp; /**< enable extended compare value */
  123. #if defined(PWM_SOC_HRPWM_SUPPORT) && PWM_SOC_HRPWM_SUPPORT
  124. bool enable_hrcmp; /**< enable high precision pwm */
  125. #endif
  126. uint8_t mode; /**< compare work mode: pwm_cmp_mode_output_compare or pwm_cmp_mode_input_capture */
  127. uint8_t update_trigger; /**< compare configuration update trigger */
  128. uint8_t ex_cmp; /**< extended compare value */
  129. uint8_t half_clock_cmp; /**< half clock compare value*/
  130. uint8_t jitter_cmp; /**< jitter compare value */
  131. #if defined(PWM_SOC_HRPWM_SUPPORT) && PWM_SOC_HRPWM_SUPPORT
  132. uint8_t hrcmp; /**< high precision pwm */
  133. #endif
  134. } pwm_cmp_config_t;
  135. /**
  136. * @brief pwm output channel config
  137. *
  138. */
  139. typedef struct pwm_output_channel {
  140. uint8_t cmp_start_index; /**< output channel compare start index */
  141. uint8_t cmp_end_index; /**< output channel compare end index */
  142. bool invert_output; /**< invert output */
  143. } pwm_output_channel_t;
  144. /**
  145. * @brief pwm fault source config
  146. *
  147. */
  148. typedef struct pwm_fault_source_config {
  149. uint32_t source_mask; /**< fault source mask*/
  150. bool fault_recover_at_rising_edge; /**< recover fault at rising edge */
  151. bool fault_external_0_active_low; /**< active external fault0 by low */
  152. bool fault_external_1_active_low; /**< active external fault1 by low */
  153. uint8_t fault_output_recovery_trigger; /**< fault output recoverty trigger */
  154. } pwm_fault_source_config_t;
  155. /**
  156. * @brief pwm config data
  157. *
  158. */
  159. typedef struct pwm_config {
  160. #if defined(PWM_SOC_HRPWM_SUPPORT) && PWM_SOC_HRPWM_SUPPORT
  161. bool hrpwm_update_mode; /**< mode one or zero, HR CMP update timing */
  162. #endif
  163. bool enable_output; /**< enable pwm output */
  164. bool invert_output; /**< invert pwm output level */
  165. uint8_t update_trigger; /**< pwm config update trigger */
  166. uint8_t fault_mode; /**< fault mode */
  167. uint8_t fault_recovery_trigger; /**< fault recoverty trigger */
  168. uint8_t force_source; /**< fault source */
  169. uint32_t dead_zone_in_half_cycle; /**< dead zone in half cycle*/
  170. } pwm_config_t;
  171. /**
  172. * @brief pair pwm config
  173. *
  174. */
  175. typedef struct pwm_pair_config {
  176. pwm_config_t pwm[2]; /**< pwm config data */
  177. } pwm_pair_config_t;
  178. #ifdef __cplusplus
  179. extern "C" {
  180. #endif
  181. /**
  182. * @brief pwm deinitialize function
  183. *
  184. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  185. *
  186. */
  187. static inline void pwm_deinit(PWM_Type *pwm_x)
  188. {
  189. pwm_x->IRQEN = 0;
  190. pwm_x->DMAEN = 0;
  191. pwm_x->SR |= pwm_x->SR;
  192. pwm_x->STA = 0;
  193. pwm_x->RLD = PWM_RLD_RLD_MASK;
  194. for (uint8_t i = 0; i < PWM_SOC_CMP_MAX_COUNT; i++) {
  195. pwm_x->CMP[i] = PWM_CMP_CMP_MASK;
  196. pwm_x->CMPCFG[i] = 0;
  197. pwm_x->CHCFG[i] = PWM_CHCFG_CMPSELEND_SET(PWM_SOC_CMP_MAX_COUNT - 1) | PWM_CHCFG_CMPSELBEG_SET(PWM_SOC_CMP_MAX_COUNT - 1);
  198. }
  199. pwm_x->FRCMD = 0;
  200. pwm_x->GCR = 0;
  201. pwm_x->SHCR = 0;
  202. #if defined(PWM_SOC_HRPWM_SUPPORT) && PWM_SOC_HRPWM_SUPPORT
  203. pwm_x->HRPWM_CFG = 0;
  204. #endif
  205. for (uint8_t i = 0; i < PWM_SOC_OUTPUT_TO_PWM_MAX_COUNT; i++) {
  206. pwm_x->PWMCFG[i] = 0;
  207. }
  208. }
  209. /**
  210. * @brief issue all shawdow register
  211. *
  212. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  213. */
  214. static inline void pwm_issue_shadow_register_lock_event(PWM_Type *pwm_x)
  215. {
  216. if (pwm_x->SHCR & PWM_SHCR_SHLKEN_MASK) {
  217. /*
  218. * if lock shadow register has been enabled in SHCR, it has to set
  219. * the lock bit twice to issue shadow register lock event.
  220. */
  221. pwm_x->SHLK = PWM_SHLK_SHLK_MASK;
  222. }
  223. pwm_x->SHLK = PWM_SHLK_SHLK_MASK;
  224. }
  225. /**
  226. * @brief lock all shawdow register
  227. *
  228. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  229. */
  230. static inline void pwm_shadow_register_lock(PWM_Type *pwm_x)
  231. {
  232. pwm_x->SHCR |= PWM_SHCR_SHLKEN_MASK;
  233. pwm_x->SHLK = PWM_SHLK_SHLK_MASK;
  234. }
  235. /**
  236. * @brief unlock all shadow register
  237. *
  238. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  239. */
  240. static inline void pwm_shadow_register_unlock(PWM_Type *pwm_x)
  241. {
  242. pwm_x->UNLK = PWM_UNLOCK_KEY;
  243. }
  244. /**
  245. * @brief set counter start value and extended start value
  246. *
  247. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  248. * @param[in] ex_start pwm timer counter extended start value
  249. * @param[in] start pwm timer counter start value
  250. */
  251. static inline void pwm_set_start_count(PWM_Type *pwm_x,
  252. uint8_t ex_start,
  253. uint32_t start)
  254. {
  255. pwm_x->STA = PWM_STA_XSTA_SET(ex_start)
  256. | PWM_STA_STA_SET(start);
  257. }
  258. #if defined(PWM_SOC_HRPWM_SUPPORT) && PWM_SOC_HRPWM_SUPPORT
  259. /**
  260. * @brief set hrpwm counter start value
  261. *
  262. * @param pwm_x PWM base address, HPM_PWMx(x=0..n)
  263. * @param start pwm timer counter start value
  264. */
  265. static inline void pwm_set_hrpwm_start_count(PWM_Type *pwm_x,
  266. uint32_t start)
  267. {
  268. pwm_x->STA_HRPWM = PWM_STA_HRPWM_STA_SET(start);
  269. }
  270. #endif
  271. /**
  272. * @brief set the reload value
  273. *
  274. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  275. * @param[in] ex_reload pwm timer counter extended reload value
  276. * @param[in] reload pwm timer counter reload value
  277. */
  278. static inline void pwm_set_reload(PWM_Type *pwm_x,
  279. uint8_t ex_reload,
  280. uint32_t reload)
  281. {
  282. pwm_shadow_register_unlock(pwm_x);
  283. pwm_x->RLD = PWM_RLD_XRLD_SET(ex_reload)
  284. | PWM_RLD_RLD_SET(reload);
  285. }
  286. #if defined(PWM_SOC_HRPWM_SUPPORT) && PWM_SOC_HRPWM_SUPPORT
  287. /**
  288. * @brief set the hr pwm reload value
  289. *
  290. * @param pwm_x PWM base address, HPM_PWMx(x=0..n)
  291. * @param hr_reload pwm timer counter hrpwm reload value
  292. * @param reload pwm timer counter reload value
  293. */
  294. static inline void pwm_set_hrpwm_reload(PWM_Type *pwm_x,
  295. uint16_t hrpwm_reload,
  296. uint32_t reload)
  297. {
  298. pwm_shadow_register_unlock(pwm_x);
  299. pwm_x->RLD_HRPWM = PWM_RLD_HRPWM_RLD_HR_SET(hrpwm_reload)
  300. | PWM_RLD_HRPWM_RLD_SET(reload);
  301. }
  302. #endif
  303. /**
  304. * @brief clear pwm status register
  305. *
  306. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  307. * @param[in] mask :
  308. * @arg PWM_IRQ_FAULT: fault condition flag
  309. * @arg PWM_IRQ_EX_RELOAD : extended reload flag, this flag set when xcnt count to xrld value or when SYNCI assert
  310. * @arg PWM_IRQ_HALF_RELOAD: half reload flag, this flag set when cnt count to rld/2
  311. * @arg PWM_IRQ_RELOAD: reload flag, this flag set when cnt count to rld value or when SYNCI assert
  312. * @arg PWM_IRQ_CMP(x)(x=0...n): comparator output compare or input capture flag
  313. */
  314. static inline void pwm_clear_status(PWM_Type *pwm_x, uint32_t mask)
  315. {
  316. pwm_x->SR |= mask;
  317. }
  318. #if defined(PWM_SOC_TIMER_RESET_SUPPORT) && PWM_SOC_TIMER_RESET_SUPPORT
  319. /**
  320. * @brief Reset timer and extension timer
  321. *
  322. * @param pwm_x PWM base address, HPM_PWMx(x=0..n)
  323. */
  324. static inline void pwm_timer_reset(PWM_Type *pwm_x)
  325. {
  326. pwm_x->GCR = ((pwm_x->GCR & ~(PWM_GCR_TIMERRESET_MASK)) | PWM_GCR_TIMERRESET_SET(1));
  327. }
  328. #endif
  329. /**
  330. * @brief get pwm status register
  331. *
  332. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  333. * @retval uint32_t SR register value
  334. */
  335. static inline uint32_t pwm_get_status(PWM_Type *pwm_x)
  336. {
  337. return pwm_x->SR;
  338. }
  339. /**
  340. * @brief disable pwm irq
  341. *
  342. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  343. * @param[in] mask :
  344. * @arg PWM_IRQ_FAULT: fault condition interrupt enable
  345. * @arg PWM_IRQ_EX_RELOAD: extended reload flag interrupt enable
  346. * @arg PWM_IRQ_HALF_RELOAD: half reload flag interrupt enable
  347. * @arg PWM_IRQ_RELOAD: reload flag interrupt enable
  348. * @arg PWM_IRQ_CMP(x)(x=0...n): comparator output compare or input capture flag interrupt enable
  349. */
  350. static inline void pwm_disable_irq(PWM_Type *pwm_x, uint32_t mask)
  351. {
  352. pwm_x->IRQEN &= ~mask;
  353. }
  354. /**
  355. * @brief enable pwm irq
  356. *
  357. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  358. * @param[in] mask :
  359. * @arg PWM_IRQ_FAULT: fault condition interrupt enable
  360. * @arg PWM_IRQ_EX_RELOAD: extended reload flag interrupt enable
  361. * @arg PWM_IRQ_HALF_RELOAD: half reload flag interrupt enable
  362. * @arg PWM_IRQ_RELOAD: reload flag interrupt enable
  363. * @arg PWM_IRQ_CMP(x)(x=0...n): comparator output compare or input capture flag interrupt enable
  364. */
  365. static inline void pwm_enable_irq(PWM_Type *pwm_x, uint32_t mask)
  366. {
  367. pwm_x->IRQEN |= mask;
  368. }
  369. /**
  370. * @brief disable pwm dma request
  371. *
  372. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  373. * @param[in] mask :
  374. * @arg PWM_IRQ_FAULT: fault condition DMA request enable
  375. * @arg PWM_IRQ_EX_RELOAD: extended reload flag DMA request enable
  376. * @arg PWM_IRQ_HALF_RELOAD: half reload flag DMA request enable
  377. * @arg PWM_IRQ_RELOAD: reload flag DMA request enable
  378. * @arg PWM_IRQ_CMP(x)(x=0...n): comparator output compare or input capture flag DMA request enable
  379. *
  380. */
  381. static inline void pwm_disable_dma_request(PWM_Type *pwm_x, uint32_t mask)
  382. {
  383. pwm_x->DMAEN &= ~mask;
  384. }
  385. /**
  386. * @brief enable pwm dma request
  387. *
  388. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  389. * @param[in] mask :
  390. * @arg PWM_IRQ_FAULT: fault condition DMA request enable
  391. * @arg PWM_IRQ_EX_RELOAD: extended reload flag DMA request enable
  392. * @arg PWM_IRQ_HALF_RELOAD: half reload flag DMA request enable
  393. * @arg PWM_IRQ_RELOAD: reload flag DMA request enable
  394. * @arg PWM_IRQ_CMP(x)(x=0...n): comparator output compare or input capture flag DMA request enable
  395. *
  396. */
  397. static inline void pwm_enable_dma_request(PWM_Type *pwm_x, uint32_t mask)
  398. {
  399. pwm_x->DMAEN |= mask;
  400. }
  401. /**
  402. * @brief set target cmp as hardware event to trigger force cmd output
  403. *
  404. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  405. * @param[in] target_cmp_index cmp index select one of the cmp as hardware event time to load FRCMD shadow registers [0-23]
  406. */
  407. static inline void pwm_set_force_cmd_shadow_register_hwevent(PWM_Type *pwm_x,
  408. uint8_t target_cmp_index)
  409. {
  410. pwm_x->SHCR = ((pwm_x->SHCR & ~(PWM_SHCR_FRCSHDWSEL_MASK))
  411. | PWM_SHCR_FRCSHDWSEL_SET(target_cmp_index));
  412. }
  413. /**
  414. * @note if trigger is not set to hardware event, target_cmp_index can be
  415. * passed with any value
  416. *
  417. */
  418. /**
  419. * @brief set shadow register control register
  420. *
  421. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  422. * @param[in] trigger select when the counter related shadow registers @ref pwm_shadow_register_update_trigger_t
  423. * @param[in] target_cmp_index select one of the comparators as hardware event (0..(PWM_SOC_CMP_MAX_COUNT-1))
  424. */
  425. static inline void pwm_set_load_counter_shadow_register_trigger(PWM_Type *pwm_x,
  426. pwm_shadow_register_update_trigger_t trigger,
  427. uint8_t target_cmp_index)
  428. {
  429. if (trigger == pwm_shadow_register_update_on_hw_event) {
  430. pwm_x->SHCR = ((pwm_x->SHCR & ~(PWM_SHCR_CNTSHDWSEL_MASK
  431. | PWM_SHCR_CNTSHDWUPT_MASK))
  432. | PWM_SHCR_CNTSHDWSEL_SET(target_cmp_index)
  433. | PWM_SHCR_CNTSHDWUPT_SET(trigger));
  434. } else {
  435. pwm_x->SHCR = ((pwm_x->SHCR & ~(PWM_SHCR_CNTSHDWUPT_MASK))
  436. | PWM_SHCR_CNTSHDWUPT_SET(trigger));
  437. }
  438. }
  439. /**
  440. * @brief Configure input capture cmp to trigger shadow register updates
  441. *
  442. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  443. * @param[in] index cmp index (0..(PWM_SOC_CMP_MAX_COUNT-1))
  444. * @param[in] is_falling_edge which edge is used as shadow register hardware load event
  445. * @arg 1- falling edge
  446. * @arg 0- rising edge
  447. */
  448. static inline void pwm_load_cmp_shadow_on_capture(PWM_Type *pwm_x,
  449. uint8_t index,
  450. bool is_falling_edge)
  451. {
  452. pwm_x->CMPCFG[index] |= PWM_CMPCFG_CMPMODE_MASK;
  453. pwm_x->GCR = ((pwm_x->GCR & ~(PWM_GCR_CMPSHDWSEL_MASK | PWM_GCR_HWSHDWEDG_MASK))
  454. | PWM_GCR_CMPSHDWSEL_SET(index)
  455. | PWM_GCR_HWSHDWEDG_SET(is_falling_edge));
  456. }
  457. #if defined(PWM_SOC_SHADOW_TRIG_SUPPORT) && PWM_SOC_SHADOW_TRIG_SUPPORT
  458. /**
  459. * @brief RLD, STA shadow registers take effect at the reload point
  460. *
  461. * @param pwm_x pwm_x PWM base address, HPM_PWMx(x=0..n)
  462. * @param is_enable true or false
  463. */
  464. static inline void pwm_set_cnt_shadow_trig_reload(PWM_Type *pwm_x, bool is_enable)
  465. {
  466. pwm_x->SHCR = ((pwm_x->SHCR & ~PWM_SHCR_CNT_UPDATE_RELOAD_MASK)
  467. | PWM_SHCR_CNT_UPDATE_RELOAD_SET(is_enable));
  468. }
  469. /**
  470. * @brief Set the timer shadow register to update the trigger edge
  471. *
  472. * @param[in] pwm_x pwm_x PWM base address, HPM_PWMx(x=0..n)
  473. * @param[in] is_falling_edge which edge is used as shadow register hardware load event
  474. * @arg 1- falling edge
  475. * @arg 0- rising edge
  476. */
  477. static inline void pwm_set_cnt_shadow_trig_edge(PWM_Type *pwm_x,
  478. bool is_falling_edge)
  479. {
  480. pwm_x->SHCR = ((pwm_x->SHCR & ~PWM_SHCR_CNT_UPDATE_EDGE_MASK)
  481. | PWM_SHCR_CNT_UPDATE_EDGE_SET(is_falling_edge));
  482. }
  483. /**
  484. * @brief Set the force output shadow register to update the trigger edge
  485. *
  486. * @param[in] pwm_x pwm_x PWM base address, HPM_PWMx(x=0..n)
  487. * @param[in] is_falling_edge which edge is used as shadow register hardware load event
  488. * @arg 1- falling edge
  489. * @arg 0- rising edge
  490. */
  491. static inline void pwm_set_force_shadow_trig_edge(PWM_Type *pwm_x,
  492. bool is_falling_edge)
  493. {
  494. pwm_x->SHCR = ((pwm_x->SHCR & ~PWM_SHCR_FORCE_UPDATE_EDGE_MASK)
  495. | PWM_SHCR_FORCE_UPDATE_EDGE_SET(is_falling_edge));
  496. }
  497. #endif
  498. /**
  499. * @brief disable pwn cmp half clock
  500. *
  501. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  502. * @param[in] index cmp index (0..(PWM_SOC_CMP_MAX_COUNT-1))
  503. */
  504. static inline void pwm_cmp_disable_half_clock(PWM_Type *pwm_x, uint8_t index)
  505. {
  506. pwm_x->CMP[index] &= ~PWM_CMP_CMPHLF_MASK;
  507. }
  508. /**
  509. * @brief enable pwm cmp half clock
  510. *
  511. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  512. * @param[in] index cmp index (0..(PWM_SOC_CMP_MAX_COUNT-1))
  513. */
  514. static inline void pwm_cmp_enable_half_clock(PWM_Type *pwm_x, uint8_t index)
  515. {
  516. pwm_x->CMP[index] |= PWM_CMP_CMPHLF_MASK;
  517. }
  518. /**
  519. * @brief update pwm cmp jitter counter compare value
  520. *
  521. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  522. * @param[in] index cmp index (0..(PWM_SOC_CMP_MAX_COUNT-1))
  523. * @param[in] jitter jitter value
  524. */
  525. static inline void pwm_cmp_update_jitter_value(PWM_Type *pwm_x, uint8_t index, uint8_t jitter)
  526. {
  527. pwm_x->CMP[index] = (pwm_x->CMP[index] & ~PWM_CMP_CMPJIT_MASK) | PWM_CMP_CMPJIT_SET(jitter);
  528. }
  529. /**
  530. * @brief update pwm cmp value
  531. *
  532. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  533. * @param[in] index cmp index (0..(PWM_SOC_CMP_MAX_COUNT-1))
  534. * @param[in] cmp clock counter compare value
  535. * @param[in] ex_cmp extended counter compare value
  536. */
  537. static inline void pwm_cmp_update_cmp_value(PWM_Type *pwm_x, uint8_t index,
  538. uint32_t cmp, uint16_t ex_cmp)
  539. {
  540. pwm_x->CMP[index] = (pwm_x->CMP[index] & ~(PWM_CMP_CMP_MASK | PWM_CMP_XCMP_MASK))
  541. | PWM_CMP_CMP_SET(cmp) | PWM_CMP_XCMP_SET(ex_cmp);
  542. }
  543. #if defined(PWM_SOC_HRPWM_SUPPORT) && PWM_SOC_HRPWM_SUPPORT
  544. /**
  545. * @brief update high-precision cmp value
  546. *
  547. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  548. * @param[in] index cmp index (0..(PWM_SOC_CMP_MAX_COUNT-1))
  549. * @param[in] cmp clock counter compare value
  550. * @param[in] hrcmp high-precision pwm
  551. */
  552. static inline void pwm_cmp_update_hrcmp_value(PWM_Type *pwm_x, uint8_t index,
  553. uint32_t cmp, uint16_t hrcmp)
  554. {
  555. pwm_x->CMP_HRPWM[index] = (pwm_x->CMP_HRPWM[index] & ~(PWM_CMP_HRPWM_CMP_MASK | PWM_CMP_HRPWM_CMP_HR_MASK))
  556. | PWM_CMP_HRPWM_CMP_SET(cmp) | PWM_CMP_HRPWM_CMP_HR_SET(hrcmp);
  557. }
  558. #endif
  559. /**
  560. * @brief Forced update of pwm cmp register value, cmp content guaranteed accurate by user
  561. *
  562. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  563. * @param[in] index cmp index (0..(PWM_SOC_CMP_MAX_COUNT-1))
  564. * @param[in] cmp cmp register data
  565. */
  566. static inline void pwm_cmp_force_value(PWM_Type *pwm_x, uint8_t index, uint32_t cmp)
  567. {
  568. pwm_x->CMP[index] = cmp;
  569. }
  570. /**
  571. * @brief config pwm cmp
  572. *
  573. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  574. * @param[in] index cmp index (0..(PWM_SOC_CMP_MAX_COUNT-1))
  575. * @param[in] config @ref pwm_cmp_config_t
  576. */
  577. static inline void pwm_config_cmp(PWM_Type *pwm_x, uint8_t index, pwm_cmp_config_t *config)
  578. {
  579. pwm_shadow_register_unlock(pwm_x);
  580. if (config->mode == pwm_cmp_mode_output_compare) {
  581. #if defined(PWM_SOC_HRPWM_SUPPORT) && PWM_SOC_HRPWM_SUPPORT
  582. if (config->enable_hrcmp) {
  583. pwm_x->CMPCFG[index] = PWM_CMPCFG_CMPSHDWUPT_SET(config->update_trigger);
  584. pwm_x->CMP[index] = PWM_CMP_HRPWM_CMP_SET(config->cmp)
  585. | PWM_CMP_HRPWM_CMP_HR_SET(config->hrcmp);
  586. } else {
  587. #endif
  588. pwm_x->CMPCFG[index] = (config->enable_ex_cmp ? PWM_CMPCFG_XCNTCMPEN_MASK : 0)
  589. | PWM_CMPCFG_CMPSHDWUPT_SET(config->update_trigger);
  590. pwm_x->CMP[index] = PWM_CMP_CMP_SET(config->cmp)
  591. | PWM_CMP_XCMP_SET(config->ex_cmp)
  592. | PWM_CMP_CMPHLF_SET(config->half_clock_cmp)
  593. | PWM_CMP_CMPJIT_SET(config->jitter_cmp);
  594. #if defined(PWM_SOC_HRPWM_SUPPORT) && PWM_SOC_HRPWM_SUPPORT
  595. }
  596. #endif
  597. } else {
  598. pwm_x->CMPCFG[index] |= PWM_CMPCFG_CMPMODE_MASK;
  599. }
  600. }
  601. /**
  602. * @brief config pwm output channel
  603. *
  604. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  605. * @param[in] index channel index (0..(PWM_SOC_PWM_MAX_COUNT-1))
  606. * @param[in] config @ref pwm_output_channel_t
  607. */
  608. static inline void pwm_config_output_channel(PWM_Type *pwm_x, uint8_t index, pwm_output_channel_t *config)
  609. {
  610. pwm_x->CHCFG[index] = PWM_CHCFG_CMPSELBEG_SET(config->cmp_start_index)
  611. | PWM_CHCFG_CMPSELEND_SET(config->cmp_end_index)
  612. | PWM_CHCFG_OUTPOL_SET(config->invert_output);
  613. }
  614. /**
  615. * @brief config pwm fault source
  616. *
  617. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  618. * @param[in] config @ref pwm_fault_source_config_t
  619. */
  620. static inline void pwm_config_fault_source(PWM_Type *pwm_x, pwm_fault_source_config_t *config)
  621. {
  622. pwm_x->GCR = (pwm_x->GCR & ~(PWM_GCR_FAULTI0EN_MASK | PWM_GCR_FAULTI1EN_MASK
  623. | PWM_GCR_FAULTI2EN_MASK | PWM_GCR_FAULTI3EN_MASK
  624. | PWM_GCR_FAULTE0EN_MASK | PWM_GCR_FAULTE1EN_MASK
  625. | PWM_GCR_FAULTRECEDG_MASK | PWM_GCR_FAULTEXPOL_MASK
  626. | PWM_GCR_FAULTRECHWSEL_MASK))
  627. | config->source_mask
  628. | PWM_GCR_FAULTEXPOL_SET((config->fault_external_0_active_low ? 0x1 : 0) | (config->fault_external_1_active_low ? 0x2 : 0))
  629. | PWM_GCR_FAULTRECEDG_SET(config->fault_recover_at_rising_edge)
  630. | PWM_GCR_FAULTRECHWSEL_SET(config->fault_output_recovery_trigger);
  631. }
  632. /**
  633. * @brief clear pwm fault status
  634. *
  635. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  636. */
  637. static inline void pwm_clear_fault(PWM_Type *pwm_x)
  638. {
  639. pwm_x->GCR |= PWM_GCR_FAULTCLR_MASK;
  640. pwm_x->GCR &= ~PWM_GCR_FAULTCLR_MASK;
  641. }
  642. /**
  643. * @brief stop the pwm timer counter
  644. *
  645. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  646. */
  647. static inline void pwm_stop_counter(PWM_Type *pwm_x)
  648. {
  649. pwm_x->GCR &= ~PWM_GCR_CEN_MASK;
  650. }
  651. /**
  652. * @brief start pwm timer counter
  653. *
  654. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  655. */
  656. static inline void pwm_start_counter(PWM_Type *pwm_x)
  657. {
  658. pwm_x->GCR |= PWM_GCR_CEN_MASK;
  659. }
  660. /**
  661. * @brief enable software force
  662. *
  663. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  664. */
  665. static inline void pwm_enable_sw_force(PWM_Type *pwm_x)
  666. {
  667. pwm_x->GCR |= PWM_GCR_SWFRC_MASK;
  668. }
  669. /**
  670. * @brief disable software force , force will take effect
  671. *
  672. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  673. */
  674. static inline void pwm_disable_sw_force(PWM_Type *pwm_x)
  675. {
  676. pwm_x->GCR &= ~PWM_GCR_SWFRC_MASK;
  677. }
  678. /**
  679. * @brief enable pwm reload value by synci
  680. *
  681. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  682. */
  683. static inline void pwm_enable_reload_at_synci(PWM_Type *pwm_x)
  684. {
  685. pwm_x->GCR |= PWM_GCR_XRLDSYNCEN_MASK | PWM_GCR_RLDSYNCEN_MASK;
  686. }
  687. /**
  688. * @brief disable pwm output
  689. *
  690. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  691. * @param[in] index pwm index (0..(PWM_SOC_CMP_MAX_COUNT-1))
  692. */
  693. static inline void pwm_disable_output(PWM_Type *pwm_x, uint8_t index)
  694. {
  695. pwm_x->PWMCFG[index] &= ~PWM_PWMCFG_OEN_MASK;
  696. }
  697. /**
  698. * @brief enable pwm output
  699. *
  700. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  701. * @param[in] index pwm index (0..(PWM_SOC_CMP_MAX_COUNT-1))
  702. */
  703. static inline void pwm_enable_output(PWM_Type *pwm_x, uint8_t index)
  704. {
  705. pwm_x->PWMCFG[index] |= PWM_PWMCFG_OEN_MASK;
  706. }
  707. /**
  708. * @brief config pwm force output level per output channel
  709. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  710. * @param[in] output_mask PWM output channel force level, set it using the macro
  711. * @arg PWM_FORCE_OUTPUT(pwm_index, force_output)
  712. */
  713. static inline void pwm_set_force_output(PWM_Type *pwm_x, uint32_t output_mask)
  714. {
  715. pwm_x->FRCMD = PWM_FRCMD_FRCMD_SET(output_mask);
  716. }
  717. /**
  718. * @brief config pwm force polarity
  719. *
  720. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  721. * @param[in] polarity polarity of input pwm_force
  722. * @arg 1- active low
  723. * @arg 0- active high
  724. */
  725. static inline void pwm_config_force_polarity(PWM_Type *pwm_x, bool polarity)
  726. {
  727. pwm_x->GCR = (pwm_x->GCR & ~(PWM_GCR_FRCPOL_MASK)) | PWM_GCR_FRCPOL_SET(polarity);
  728. }
  729. /**
  730. * @brief config the force effective time
  731. *
  732. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  733. * @param[in] timing @ref pwm_force_cmd_timing_t
  734. */
  735. static inline void pwm_config_force_cmd_timing(PWM_Type *pwm_x, pwm_force_cmd_timing_t timing)
  736. {
  737. pwm_x->GCR = (pwm_x->GCR & ~(PWM_GCR_FRCTIME_MASK)) | PWM_GCR_FRCTIME_SET(timing);
  738. }
  739. /**
  740. * @brief enable pwm sw force output
  741. *
  742. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  743. * @param[in] index pwm cmp index (0..(PWM_SOC_CMP_MAX_COUNT-1))
  744. */
  745. static inline void pwm_enable_pwm_sw_force_output(PWM_Type *pwm_x, uint8_t index)
  746. {
  747. pwm_x->PWMCFG[index] |= PWM_PWMCFG_OEN_MASK | PWM_PWMCFG_FRCSRCSEL_MASK
  748. | PWM_PWMCFG_FRCSHDWUPT_SET(pwm_shadow_register_update_on_modify);
  749. }
  750. /**
  751. * @brief disable pwm sw force output
  752. *
  753. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  754. * @param[in] index pwm cmp index (0..(PWM_SOC_CMP_MAX_COUNT-1))
  755. */
  756. static inline void pwm_disable_pwm_sw_force_output(PWM_Type *pwm_x, uint8_t index)
  757. {
  758. pwm_x->PWMCFG[index] &= ~PWM_PWMCFG_FRCSRCSEL_MASK;
  759. }
  760. /**
  761. * @brief config PWM channel configure registe
  762. *
  763. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  764. * @param[in] index pwm channel index (0..(PWM_SOC_PWM_MAX_COUNT-1))
  765. * @param[in] config @ref pwm_config_t
  766. * @param[in] enable_pair_mode enable pair mode
  767. * @arg 1- PWM output is in pair mode
  768. * @arg 0- PWM output is in indepandent mode
  769. */
  770. static inline void pwm_config_pwm(PWM_Type *pwm_x, uint8_t index,
  771. pwm_config_t *config, bool enable_pair_mode)
  772. {
  773. pwm_x->PWMCFG[index] = PWM_PWMCFG_OEN_SET(config->enable_output)
  774. | PWM_PWMCFG_FRCSHDWUPT_SET(config->update_trigger)
  775. | PWM_PWMCFG_FAULTMODE_SET(config->fault_mode)
  776. | PWM_PWMCFG_FAULTRECTIME_SET(config->fault_recovery_trigger)
  777. | PWM_PWMCFG_FRCSRCSEL_SET(config->force_source)
  778. | PWM_PWMCFG_PAIR_SET(enable_pair_mode)
  779. #if defined(PWM_SOC_HRPWM_SUPPORT) && PWM_SOC_HRPWM_SUPPORT
  780. | PWM_PWMCFG_HR_UPDATE_MODE_SET(config->hrpwm_update_mode)
  781. #endif
  782. | PWM_PWMCFG_DEADAREA_SET(config->dead_zone_in_half_cycle);
  783. }
  784. /**
  785. * @brief getting the counter reload value for a pwm timer
  786. *
  787. * @param pwm_x PWM base address, HPM_PWMx(x=0..n)
  788. * @retval pwm reload value
  789. */
  790. static inline uint32_t pwm_get_reload_val(PWM_Type *pwm_x)
  791. {
  792. return PWM_RLD_RLD_GET(pwm_x->RLD);
  793. }
  794. /**
  795. * @brief getting the extended counter reload value for a pwm timer
  796. *
  797. * @param pwm_x PWM base address, HPM_PWMx(x=0..n)
  798. * @retval pwm extended reload value
  799. */
  800. static inline uint32_t pwm_get_ex_reload_val(PWM_Type *pwm_x)
  801. {
  802. return PWM_RLD_XRLD_GET(pwm_x->RLD);
  803. }
  804. /**
  805. * @brief getting the value of the pwm counter
  806. *
  807. * @param pwm_x PWM base address, HPM_PWMx(x=0..n)
  808. * @retval pwm counter value
  809. */
  810. static inline uint32_t pwm_get_counter_val(PWM_Type *pwm_x)
  811. {
  812. return PWM_CNT_CNT_GET(pwm_x->CNT);
  813. }
  814. /**
  815. * @brief getting the value of the pwm extended counter
  816. *
  817. * @param pwm_x PWM base address, HPM_PWMx(x=0..n)
  818. * @retval pwm counter value
  819. */
  820. static inline uint32_t pwm_get_ex_counter_val(PWM_Type *pwm_x)
  821. {
  822. return PWM_CNT_XCNT_GET(pwm_x->CNT);
  823. }
  824. /**
  825. * @brief pwm load cmp shadow on match
  826. *
  827. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  828. * @param[in] index cmp index (0..(PWM_SOC_CMP_MAX_COUNT-1))
  829. * @param[in] config @ref pwm_cmp_config_t
  830. * @retval status_invalid_argument or status_success
  831. */
  832. hpm_stat_t pwm_load_cmp_shadow_on_match(PWM_Type *pwm_x,
  833. uint8_t index,
  834. pwm_cmp_config_t *config);
  835. /**
  836. * @brief pwm get captured count
  837. *
  838. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  839. * @param[out] buf count value
  840. * @param[in] counter @ref pwm_counter_type_t
  841. * @param[in] start_index start capture index (0..(PWM_SOC_CMP_MAX_COUNT-1))
  842. * @param[in] num capture num (1..PWM_SOC_CMP_MAX_COUNT)
  843. */
  844. void pwm_get_captured_count(PWM_Type *pwm_x, uint32_t *buf, pwm_counter_type_t counter, uint8_t start_index, uint8_t num);
  845. /**
  846. * @brief get default cmp config
  847. *
  848. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  849. * @param[out] config @ref pwm_cmp_config_t
  850. */
  851. void pwm_get_default_cmp_config(PWM_Type *pwm_x, pwm_cmp_config_t *config);
  852. /**
  853. * @brief get default output channel config
  854. *
  855. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  856. * @param[out] config @ref pwm_output_channel_t
  857. */
  858. void pwm_get_default_output_channel_config(PWM_Type *pwm_x, pwm_output_channel_t *config);
  859. /**
  860. * @brief get default pwm config
  861. *
  862. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  863. * @param[out] config @ref pwm_config_t
  864. */
  865. void pwm_get_default_pwm_config(PWM_Type *pwm_x, pwm_config_t *config);
  866. /**
  867. * @brief get default pwm pair config
  868. *
  869. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  870. * @param[out] config @ref pwm_pair_config_t
  871. */
  872. void pwm_get_default_pwm_pair_config(PWM_Type *pwm_x, pwm_pair_config_t *config);
  873. /**
  874. * @brief setup waveform
  875. *
  876. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  877. * @param[in] pwm_index pwm channel index (0..(PWM_SOC_PWM_MAX_COUNT-1))
  878. * @param[in] pwm_config @ref pwm_config_t
  879. * @param[in] cmp_start_index pwm cmp index (0..(PWM_SOC_PWM_MAX_COUNT-1))
  880. * @param[in] cmp @ref pwm_cmp_config_t
  881. * @param[in] cmp_num cmp num (1..PWM_SOC_CMP_MAX_COUNT), cmp[cmp_num-1] must not overflow
  882. * @retval hpm_stat_t
  883. */
  884. hpm_stat_t pwm_setup_waveform(PWM_Type *pwm_x,
  885. uint8_t pwm_index, pwm_config_t *pwm_config,
  886. uint8_t cmp_start_index, pwm_cmp_config_t *cmp, uint8_t cmp_num);
  887. /**
  888. * @brief setup pwm waveform in pair
  889. *
  890. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  891. * @param[in] pwm_index pwm channel index (0..(PWM_SOC_PWM_MAX_COUNT-1))
  892. * @param[in] pwm_pair_config @ref pwm_pair_config_t
  893. * @param[in] cmp_start_index pwm cmp index (0..(PWM_SOC_PWM_MAX_COUNT-1))
  894. * @param[in] cmp @ref pwm_cmp_config_t
  895. * @param[in] cmp_num cmp num (1..PWM_SOC_CMP_MAX_COUNT), cmp[cmp_num-1] must not overflow
  896. * @retval hpm_stat_t @ref status_invalid_argument or @ref status_success
  897. */
  898. hpm_stat_t pwm_setup_waveform_in_pair(PWM_Type *pwm_x,
  899. uint8_t pwm_index, pwm_pair_config_t *pwm_pair_config,
  900. uint8_t cmp_start_index, pwm_cmp_config_t *cmp, uint8_t cmp_num);
  901. /**
  902. * @brief update raw compare value for edge aligned waveform
  903. *
  904. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  905. * @param[in] cmp_index index of cmp to be adjusted (0..(PWM_SOC_PWM_MAX_COUNT-1))
  906. * @param[in] target_cmp target compare value
  907. * @retval hpm_stat_t @ref status_invalid_argument or @ref status_success
  908. */
  909. hpm_stat_t pwm_update_raw_cmp_edge_aligned(PWM_Type *pwm_x, uint8_t cmp_index,
  910. uint32_t target_cmp);
  911. /**
  912. * @brief update raw compare value for central aligned waveform
  913. *
  914. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  915. * @param[in] cmp1_index index of cmp1 to be adjusted (cmp1_index must be even number)
  916. * @param[in] cmp2_index index of cmp2 to be adjusted (cmp2_index must be odd number)
  917. * @param[in] target_cmp1 target compare value for cmp1
  918. * @param[in] target_cmp2 target compare value for cmp2
  919. * @retval hpm_stat_t @ref status_invalid_argument or @ref status_success cmp1_index
  920. */
  921. hpm_stat_t pwm_update_raw_cmp_central_aligned(PWM_Type *pwm_x, uint8_t cmp1_index,
  922. uint8_t cmp2_index, uint32_t target_cmp1, uint32_t target_cmp2);
  923. #if defined(PWM_SOC_HRPWM_SUPPORT) && PWM_SOC_HRPWM_SUPPORT
  924. /**
  925. * @brief recovery hrpwm output
  926. *
  927. * @param pwm_x @ref PWM_Type PWM base address
  928. */
  929. static inline void pwm_recovery_hrpwm_output(PWM_Type *pwm_x)
  930. {
  931. pwm_x->HRPWM_CFG |= PWM_HRPWM_CFG_CAL_SW_EN_MASK;
  932. pwm_x->ANA_CFG0 |= PWM_ANA_CFG0_CAL_SW_TRIG_H_MASK;
  933. pwm_x->ANA_CFG0 &= ~PWM_ANA_CFG0_CAL_SW_TRIG_H_MASK;
  934. pwm_x->ANA_CFG0 |= PWM_ANA_CFG0_CAL_SW_TRIG_H_MASK;
  935. pwm_x->ANA_CFG0 &= ~PWM_ANA_CFG0_CAL_SW_TRIG_H_MASK;
  936. pwm_x->HRPWM_CFG &= ~PWM_HRPWM_CFG_CAL_SW_EN_MASK;
  937. }
  938. /**
  939. * @brief Enable high-precision pwm
  940. *
  941. * @param[in] pwm_x @ref PWM_Type PWM base address
  942. */
  943. static inline void pwm_enable_hrpwm(PWM_Type *pwm_x)
  944. {
  945. pwm_x->GCR = (pwm_x->GCR & ~(PWM_GCR_HR_PWM_EN_MASK)) | PWM_GCR_HR_PWM_EN_SET(1);
  946. }
  947. /**
  948. * @brief Disable high-precision pwm
  949. *
  950. * @param[in] pwm_x @ref PWM_Type PWM base address
  951. */
  952. static inline void pwm_disable_hrpwm(PWM_Type *pwm_x)
  953. {
  954. pwm_x->GCR = pwm_x->GCR & ~(PWM_GCR_HR_PWM_EN_MASK);
  955. }
  956. /**
  957. * @brief Calibrate all channels of hrpwm
  958. *
  959. * @param[in] pwm_x @ref PWM_Type PWM base address
  960. */
  961. static inline void pwm_cal_hrpwm_start(PWM_Type *pwm_x)
  962. {
  963. pwm_x->HRPWM_CFG |= PWM_HRPWM_CFG_CAL_START_MASK;
  964. }
  965. /**
  966. * @brief Calibrate specified hrpwm channels
  967. *
  968. * @param[in] pwm_x @ref PWM_Type PWM base address
  969. * @param[in] chn Channel number
  970. */
  971. static inline void pwm_cal_hrpwm_chn_start(PWM_Type *pwm_x, uint8_t chn)
  972. {
  973. pwm_x->HRPWM_CFG |= PWM_HRPWM_CFG_CAL_START_SET(chn);
  974. }
  975. /**
  976. * @brief Wait for the completion of calibration of the specified channel of high-precision PWM, blocking
  977. *
  978. * @param[in] pwm_x @ref PWM_Type PWM base address
  979. * @param[in] chn Channel number
  980. */
  981. static inline void pwm_cal_hrpwm_chn_wait(PWM_Type *pwm_x, uint8_t chn)
  982. {
  983. while (PWM_ANASTS_CALON_GET(pwm_x->ANASTS[chn])) {
  984. };
  985. }
  986. /**
  987. * @brief get calibration status
  988. *
  989. * @param[in] pwm_x pwm_x @ref PWM_Type PWM base address
  990. * @param[in] chn Channel number
  991. * @return uint32_t finished will be set zero.
  992. */
  993. static inline uint32_t pwm_get_cal_hrpwm_status(PWM_Type *pwm_x, uint8_t chn)
  994. {
  995. return PWM_ANASTS_CALON_GET(pwm_x->ANASTS[chn]);
  996. }
  997. /**
  998. * @brief getting the counter reload value for hrpwm counter
  999. *
  1000. * @param pwm_x pwm_x @ref PWM_Type PWM base address
  1001. * @return uint32_t hrpwm reload
  1002. */
  1003. static inline uint32_t pwm_get_hrpwm_reload_val(PWM_Type *pwm_x)
  1004. {
  1005. return PWM_RLD_HRPWM_RLD_GET(pwm_x->RLD_HRPWM);
  1006. }
  1007. /**
  1008. * @brief getting the counter reload value for hrpwm hr counter
  1009. *
  1010. * @param pwm_x pwm_x @ref PWM_Type PWM base address
  1011. * @return uint32_t hrpwm hr reload
  1012. */
  1013. static inline uint32_t pwm_get_hrpwm_hr_reload_val(PWM_Type *pwm_x)
  1014. {
  1015. return PWM_RLD_HRPWM_RLD_HR_GET(pwm_x->RLD_HRPWM);
  1016. }
  1017. /**
  1018. * @brief update raw high-precision compare value for edge aligned waveform
  1019. *
  1020. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  1021. * @param[in] cmp_index index of cmp to be adjusted (0..(PWM_SOC_PWM_MAX_COUNT-1))
  1022. * @param[in] target_cmp target compare value
  1023. * @param[in] target_hrcmp target high-precision compare value
  1024. * @return hpm_stat_t
  1025. */
  1026. hpm_stat_t pwm_update_raw_hrcmp_edge_aligned(PWM_Type *pwm_x, uint8_t cmp_index, uint32_t target_cmp,
  1027. uint16_t target_hrcmp);
  1028. /**
  1029. * @brief update raw high-precision compare value for central aligned waveform
  1030. *
  1031. * @param[in] pwm_x PWM base address, HPM_PWMx(x=0..n)
  1032. * @param[in] cmp1_index index of cmp1 to be adjusted (cmp1_index must be even number)
  1033. * @param[in] cmp2_index index of cmp2 to be adjusted (cmp2_index must be odd number)
  1034. * @param[in] target_cmp1 target compare value for cmp1
  1035. * @param[in] target_cmp2 target compare value for cmp2
  1036. * @param[in] target_hrcmp1 target high-precision compare value for cmp1
  1037. * @param[in] target_hrcmp2 target high-precision compare value for cmp2
  1038. * @return hpm_stat_t
  1039. */
  1040. hpm_stat_t pwm_update_raw_hrcmp_central_aligned(PWM_Type *pwm_x, uint8_t cmp1_index,
  1041. uint8_t cmp2_index, uint32_t target_cmp1, uint32_t target_cmp2,
  1042. uint16_t target_hrcmp1, uint16_t target_hrcmp2);
  1043. #endif
  1044. #ifdef __cplusplus
  1045. }
  1046. #endif
  1047. /**
  1048. * @}
  1049. */
  1050. #endif /* HPM_PWM_DRV_H */