driver_lpwm.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * @File name : driver_lpwm.c
  3. * @Author : Bluetrum IOT Team
  4. * @Date : 2023-03-06
  5. * @Description : This file provides functions to manage the most functionalities
  6. * of the LPWM peripheral.
  7. *
  8. * Copyright (c) by Bluetrum, All Rights Reserved.
  9. */
  10. #include "driver_lpwm.h"
  11. /**
  12. * @brief Initializes the LPWM peripheral according to the specified
  13. * parameters in the lpwm_init_struct.
  14. * @param lpwm_init_struct: pointer to a lpwm_base_init_typedef structure that
  15. * contains the configuration information for the specified LPWM peripheral.
  16. * @retval None
  17. */
  18. void lpwm_init(lpwm_base_init_typedef *lpwm_base_init_struct)
  19. {
  20. /* Disable peripheral before config */
  21. LPWM->start_con &= ~LPWMSTCON_LPWM_EN;
  22. /* Configure mode */
  23. LPWM->start_con &= ~LPWMSTCON_BREATH_MODE;
  24. LPWM->start_con |= lpwm_base_init_struct->mode & LPWMSTCON_BREATH_MODE;
  25. /* Configure period and duty of normal mode or first duty of breath mode */
  26. if ((lpwm_base_init_struct->mode & LPWMSTCON_BREATH_MODE) != LPWM_MODE_NORMAL) {
  27. LPWM->start_con &= ~LPWMSTCON_LPWM_DIR;
  28. LPWM->start_con |= lpwm_base_init_struct->breath_start_dir & LPWMSTCON_LPWM_DIR;
  29. LPWM->cnt_max_con &= ~LPWMCMCON_BRT_CNTMAX;
  30. LPWM->cnt_max_con |= (lpwm_base_init_struct->period * LPWMCMCON_BRT_CNTMAX_0) & LPWMCMCON_BRT_CNTMAX;
  31. LPWM->start_con &= ~LPWMSTCON_ST_DUTY;
  32. LPWM->start_con |= (lpwm_base_init_struct->first_duty * LPWMSTCON_ST_DUTY_0) & LPWMSTCON_ST_DUTY;
  33. } else {
  34. LPWM->cnt_max_con &= ~LPWMCMCON_SWT_CNTMAX;
  35. LPWM->cnt_max_con |= (lpwm_base_init_struct->period * LPWMCMCON_SWT_CNTMAX_0) & LPWMCMCON_SWT_CNTMAX;
  36. LPWM->switch_con &= ~LPWMSWCON_DUTY_ON;
  37. LPWM->switch_con |= (lpwm_base_init_struct->first_duty * LPWMSWCON_DUTY_ON_0) & LPWMSWCON_DUTY_ON;
  38. }
  39. }
  40. /**
  41. * @brief De-initialize the specified LPWM peripheral.
  42. * @retval None
  43. */
  44. void lpwm_deinit(void)
  45. {
  46. u8 i;
  47. volatile uint32_t *lpwm_con_base = &(LPWM->stageA_con);
  48. for (i = 0; i < 6; i++) {
  49. lpwm_con_base[i] &= ~LPWMxCON_EN;
  50. }
  51. LPWM->stageHL_con &= ~(LPWMHLCON_L_EN | LPWMHLCON_H_EN);
  52. LPWM->start_con = 0;
  53. clk_gate1_cmd(CLK_GATE1_LPWM, CLK_EN);
  54. }
  55. /**
  56. * @brief Configure the LPWM stage that needs to be enabled or disabled.
  57. * @param stage: the stage that need to config.
  58. * @param stage_init_struct: pointer to a lpwm_stage_init_typedef structure that
  59. * contains the configuration information for the specified LPWM peripheral.
  60. * @param state: state of the stage of LPWM peripheral.
  61. * This parameter can be: ENABLE or DISABLE.
  62. * @retval None
  63. */
  64. void lpwm_stage_config(LPWM_STAGE_TYPEDEF stage, lpwm_stage_init_typedef *stage_init_struct, FUNCTIONAL_STATE state)
  65. {
  66. uint8_t i;
  67. volatile uint32_t *lpwm_con_base = &(LPWM->stageA_con);
  68. /* Configure A..F LPWM stage register */
  69. for (i = 0; i < 6; i++) {
  70. if (stage & (1 << i)) {
  71. if (state != DISABLE) {
  72. lpwm_con_base[i] &= ~LPWMxCON_EN;
  73. lpwm_con_base[i] &= ~LPWMxCON_REPEAT;
  74. lpwm_con_base[i] |= (stage_init_struct->repeat * LPWMxCON_REPEAT_0) & LPWMxCON_REPEAT;
  75. lpwm_con_base[i] &= ~LPWMxCON_STEP;
  76. lpwm_con_base[i] |= (stage_init_struct->step * LPWMxCON_STEP_0) & LPWMxCON_STEP;
  77. lpwm_con_base[i] &= ~LPWMxCON_ST_DUTY;
  78. lpwm_con_base[i] |= (stage_init_struct->start_duty * LPWMxCON_ST_DUTY_0) & LPWMxCON_ST_DUTY;
  79. lpwm_con_base[i] |= LPWMxCON_EN;
  80. } else {
  81. lpwm_con_base[i] &= ~LPWMxCON_EN;
  82. }
  83. }
  84. }
  85. /* Configure max hold time */
  86. if (stage & LPWM_STAGE_L) {
  87. if (state != DISABLE) {
  88. LPWM->stageHL_con &= ~LPWMHLCON_L_EN;
  89. LPWM->stageHL_con &= ~LPWMHLCON_L_REPEAT;
  90. LPWM->stageHL_con |= (stage_init_struct->repeat * LPWMHLCON_L_REPEAT_0) & LPWMHLCON_L_REPEAT;
  91. LPWM->stageHL_con |= LPWMHLCON_L_EN;
  92. } else {
  93. LPWM->stageHL_con &= ~LPWMHLCON_L_EN;
  94. }
  95. }
  96. /* Configure min hold time */
  97. if (stage & LPWM_STAGE_H) {
  98. if (state != DISABLE) {
  99. LPWM->stageHL_con &= ~LPWMHLCON_H_EN;
  100. LPWM->stageHL_con &= ~LPWMHLCON_H_REPEAT;
  101. LPWM->stageHL_con |= (stage_init_struct->repeat * LPWMHLCON_H_REPEAT_0) & LPWMHLCON_H_REPEAT;
  102. LPWM->stageHL_con |= LPWMHLCON_H_EN;
  103. } else {
  104. LPWM->stageHL_con &= ~LPWMHLCON_H_EN;
  105. }
  106. }
  107. }
  108. /**
  109. * @brief Enable or disable LPWM peripheral.
  110. * @param state: State of LPWM peripheral.
  111. * This parameter can be: ENABLE or DISABLE.
  112. * @retval None
  113. */
  114. void lpwm_cmd(FUNCTIONAL_STATE state)
  115. {
  116. if (state != DISABLE) {
  117. LPWM->start_con |= LPWMSTCON_LPWM_EN;
  118. } else {
  119. LPWM->start_con &= ~LPWMSTCON_LPWM_EN;
  120. }
  121. }
  122. /**
  123. * @brief Get the LPWM flag state.
  124. * @param flag_type: specifies the flag to get.
  125. * @retval state of the specified flag (SET or RESET)
  126. */
  127. FLAG_STATE lpwm_get_flag(LPWM_FLAG_TYPEDEF flag_type)
  128. {
  129. uint32_t pending_bit = 0;
  130. if (flag_type & LPWM_FLAG_L_STAGE) {
  131. pending_bit |= LPWMPND_LPWM_PND;
  132. }
  133. if ((LPWM->pend & pending_bit) != RESET) {
  134. return SET;
  135. } else {
  136. return RESET;
  137. }
  138. }
  139. /**
  140. * @brief Clear LPWM pending flag.
  141. * @param flag_type: specifies the flag to clear.
  142. * this parameter can be one of the following values:
  143. * @arg LPWM_FLAG_L_STAGE: stage L begin pending.
  144. * @retval None
  145. */
  146. void lpwm_clear_flag(LPWM_FLAG_TYPEDEF flag_type)
  147. {
  148. uint32_t pending_bit = 0;
  149. if (flag_type & LPWM_FLAG_L_STAGE) {
  150. pending_bit |= LPWMPND_LPWM_PND;
  151. }
  152. LPWM->pend |= pending_bit;
  153. }