drv_pwm.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*********************************************************************
  2. * INCLUDES
  3. */
  4. #include "sdk_common.h"
  5. #include "SEGGER_RTT.h"
  6. #include "usr_config.h"
  7. #include "exception.h"
  8. #include "system.h"
  9. #include "nrf_gpio.h"
  10. #include "drv_pwm.h"
  11. /*********************************************************************
  12. * DEFINITIONS
  13. */
  14. #define PWM_INSTANCE 0
  15. /*********************************************************************
  16. * LOCAL VARIABLES
  17. */
  18. static nrf_drv_pwm_t m_pwm = NRF_DRV_PWM_INSTANCE(PWM_INSTANCE);
  19. static nrf_drv_pwm_config_t m_config =
  20. {
  21. .output_pins =
  22. {
  23. NRF_DRV_PWM_PIN_NOT_USED, // channel 0
  24. NRF_DRV_PWM_PIN_NOT_USED, // channel 1
  25. NRF_DRV_PWM_PIN_NOT_USED, // channel 2
  26. NRF_DRV_PWM_PIN_NOT_USED // channel 3
  27. },
  28. .irq_priority = PWM0_IRQ_PRIORITY,
  29. .base_clock = NRF_PWM_CLK_125kHz,
  30. .count_mode = NRF_PWM_MODE_UP,
  31. .top_value = 15612,
  32. .load_mode = NRF_PWM_LOAD_INDIVIDUAL,
  33. .step_mode = NRF_PWM_STEP_AUTO
  34. };
  35. static nrfx_pwm_handler_t m_pwm_callback = NULL;
  36. //static void bsp_pwm_init_process(void)
  37. //{
  38. //// if(Except_TxError(EXCEPT_PWM_INIT,"bsp_pwm_init_error\r\n") == 0)
  39. //// {
  40. //// Process_Stop(bsp_pwm_init_process);
  41. //// }
  42. //}
  43. /*********************************************************************
  44. * LOCAL FUNCTIONS
  45. */
  46. /**
  47. @brief PWM 初始化
  48. @prama 无
  49. @return 无
  50. */
  51. void PWM_Initialize(void)
  52. {
  53. nrf_drv_pwm_init(&m_pwm, &m_config, m_pwm_callback);
  54. }
  55. /**
  56. @brief PWM 的未初始化
  57. @param 无
  58. @return 无
  59. */
  60. void PWM_UnInitialize(void)
  61. {
  62. nrfx_pwm_uninit(&m_pwm);
  63. }
  64. ///**
  65. // @brief PWM 唤醒操作
  66. // @prama t - [in] 唤醒时间
  67. // @return 无
  68. //*/
  69. //static void cb_pwmWakeup(uint32_t t)
  70. //{
  71. // Pwm_init();
  72. //}
  73. ///**
  74. // @brief PWM 睡眠操作
  75. // @prama t - [in] 睡眠时间
  76. // @return 无
  77. //*/
  78. //static void cb_pwmSleep(uint32_t t)
  79. //{
  80. // nrfx_pwm_uninit(&m_pwm);
  81. //}
  82. /**
  83. @brief 设置PWM的通道的加载模式
  84. @param load_mode -[in] 加载模式
  85. @return 无
  86. */
  87. static void PWM_SetChannelsLoadMode(nrf_pwm_dec_load_t load_mode)
  88. {
  89. m_config.load_mode = load_mode;
  90. }
  91. /*********************************************************************
  92. * PUBLIC FUNCTIONS
  93. */
  94. /**
  95. @brief 设置PWM的引脚通道
  96. @param p_pins -[in] 输入的引脚通道
  97. @return 无
  98. */
  99. void PWM_SetChannels(uint32_t channel_1, uint32_t channel_2, uint32_t channel_3, uint32_t channel_4)
  100. {
  101. m_config.output_pins[0] = channel_1;
  102. m_config.output_pins[1] = channel_2;
  103. m_config.output_pins[2] = channel_3;
  104. m_config.output_pins[3] = channel_4;
  105. }
  106. /**
  107. @brief 设置PWM的中断回调函数
  108. @param pwm_callback -[in] 中断函数的地址
  109. @param irq_priority -[in] 中断函数的优先级
  110. @return 无
  111. */
  112. void PWM_SetIRQ(nrfx_pwm_handler_t pwm_callback, uint8_t irq_priority)
  113. {
  114. m_pwm_callback = pwm_callback;
  115. m_config.irq_priority = irq_priority;
  116. }
  117. /**
  118. @brief 设置PWM的基础时钟
  119. @param clock -[in] 时钟频率
  120. @return 无
  121. */
  122. void PWM_SetBaseClock(nrf_pwm_clk_t clock)
  123. {
  124. m_config.base_clock = clock;
  125. }
  126. /**
  127. @brief 设置PWM的计数模式
  128. @param count_mode -[in] 计数模式
  129. @return 无
  130. */
  131. void PWM_SetCountMode(nrf_pwm_mode_t count_mode)
  132. {
  133. m_config.count_mode = count_mode;
  134. }
  135. /**
  136. @brief 设置PWM的最大的占空比阈值
  137. @param Max_duty_cycle_value -[in] 最大的占空比阈值,根据频率决定。
  138. @return 无
  139. */
  140. void PWM_SetDutyCycleThreshold(uint16_t Max_duty_cycle_value)
  141. {
  142. m_config.top_value = Max_duty_cycle_value;
  143. }
  144. /**
  145. @brief 设置PWM的序列的推进模式,定义下一个cycle的进行方式。
  146. @param step_mode -[in] 推进模式
  147. @return 无
  148. */
  149. void PWM_SetSequenceStepMode(nrf_pwm_dec_step_t step_mode)
  150. {
  151. m_config.step_mode = step_mode;
  152. }
  153. /**
  154. @brief 设置一个独立通道序列
  155. @param p_seqValues -[in] 指向一个独立通道序列
  156. @param seqValues_length - [in] 序列的大小
  157. @param seqValues_repeats -[in] 序列的每个占空比重复的次数
  158. @param seqValues_end_delay - [in] 每个序列中,最后一个占空比重复的次数
  159. @return 无
  160. */
  161. nrf_pwm_sequence_t* PWM_SetIndSequence(pwm_values_individual_t *p_seqValues, uint16_t seqValues_length, uint32_t seqValues_repeats, uint32_t seqValues_end_delay)
  162. {
  163. static nrf_pwm_sequence_t m_sequence;
  164. if(m_config.load_mode != NRF_PWM_LOAD_INDIVIDUAL)
  165. {
  166. PWM_SetChannelsLoadMode(NRF_PWM_LOAD_INDIVIDUAL);
  167. PWM_UnInitialize();
  168. PWM_Initialize();
  169. }
  170. m_sequence.values.p_individual = (nrf_pwm_values_individual_t*)p_seqValues;
  171. m_sequence.length = seqValues_length;
  172. m_sequence.repeats = seqValues_repeats;
  173. m_sequence.end_delay = seqValues_end_delay;
  174. return &m_sequence;
  175. }
  176. /**
  177. @brief 设置一个共用通道序列
  178. @param p_seqValues -[in] 指向一个共用通道序列
  179. @param seqValues_length - [in] 序列的大小
  180. @param seqValues_repeats -[in] 序列的每个占空比重复的次数
  181. @param seqValues_end_delay - [in] 每个序列中,最后一个占空比重复的次数
  182. @return 无
  183. */
  184. nrf_pwm_sequence_t* PWM_SetComSequence(pwm_values_common_t *p_seqValues, uint16_t seqValues_length, uint32_t seqValues_repeats, uint32_t seqValues_end_delay)
  185. {
  186. static nrf_pwm_sequence_t m_sequence;
  187. if(m_config.load_mode != NRF_PWM_LOAD_COMMON)
  188. {
  189. PWM_SetChannelsLoadMode(NRF_PWM_LOAD_COMMON);
  190. PWM_UnInitialize();
  191. PWM_Initialize();
  192. }
  193. m_sequence.values.p_common = (nrf_pwm_values_common_t*)p_seqValues;
  194. m_sequence.length = seqValues_length;
  195. m_sequence.repeats = seqValues_repeats;
  196. m_sequence.end_delay = seqValues_end_delay;
  197. return &m_sequence;
  198. }
  199. /**
  200. @brief 设置PWM的指定单个序列的播放
  201. @param pwm_sequence -[in] 输入序列的值
  202. @param playback_count -[in] 播放次数
  203. @param flags - [in] 播放模式
  204. @return 错误代码
  205. */
  206. uint32_t PWM_SetSimplePwmPlayBack(nrf_pwm_sequence_t *pwm_sequence, uint16_t playback_count, uint32_t flags)
  207. {
  208. return nrf_drv_pwm_simple_playback(&m_pwm, pwm_sequence, playback_count, flags);
  209. }
  210. /**
  211. @brief 设置PWM的指定多个序列的播放
  212. @param pwm_sequence0 -[in] 输入序列的值
  213. @param pwm_sequence1 -[in] 输入序列的值
  214. @param playback_count -[in] 播放次数
  215. @param flags - [in] 播放模式
  216. @return 错误代码
  217. */
  218. uint32_t PWM_SetComplexPwmPlayBack(nrf_pwm_sequence_t *pwm_sequence0, nrf_pwm_sequence_t *pwm_sequence1, uint16_t playback_count, uint32_t flags)
  219. {
  220. return nrf_drv_pwm_complex_playback(&m_pwm, pwm_sequence0, pwm_sequence1, playback_count, flags);
  221. }