bsp_pwm.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*********************************************************************
  2. * INCLUDES
  3. */
  4. #include "sdk_common.h"
  5. #include "SEGGER_RTT.h"
  6. #include "usr_config.h"
  7. #include "system.h"
  8. #include "nrf_gpio.h"
  9. #include "bsp_pwm.h"
  10. /*********************************************************************
  11. * DEFINITIONS
  12. */
  13. #define PWM_INSTANCE 0
  14. /*********************************************************************
  15. * LOCAL VARIABLES
  16. */
  17. static nrf_drv_pwm_t m_pwm = NRF_DRV_PWM_INSTANCE(PWM_INSTANCE);
  18. static nrf_drv_pwm_config_t m_config =
  19. {
  20. .output_pins =
  21. {
  22. NRF_DRV_PWM_PIN_NOT_USED, // channel 0
  23. NRF_DRV_PWM_PIN_NOT_USED, // channel 1
  24. NRF_DRV_PWM_PIN_NOT_USED, // channel 2
  25. NRF_DRV_PWM_PIN_NOT_USED // channel 3
  26. },
  27. .irq_priority = PWM0_IRQ_PRIORITY,
  28. .base_clock = NRF_PWM_CLK_125kHz,
  29. .count_mode = NRF_PWM_MODE_UP,
  30. .top_value = 15612,
  31. .load_mode = NRF_PWM_LOAD_INDIVIDUAL,
  32. .step_mode = NRF_PWM_STEP_AUTO
  33. };
  34. static nrfx_pwm_handler_t m_pwm_callback = NULL;
  35. /*********************************************************************
  36. * LOCAL FUNCTIONS
  37. */
  38. /**
  39. @brief PWM 初始化
  40. @prama 无
  41. @return 无
  42. */
  43. static void Pwm_init(void)
  44. {
  45. APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm, &m_config, m_pwm_callback));
  46. }
  47. ///**
  48. // @brief PWM 唤醒操作
  49. // @prama t - [in] 唤醒时间
  50. // @return 无
  51. //*/
  52. //static void cb_pwmWakeup(uint32_t t)
  53. //{
  54. // Pwm_init();
  55. //}
  56. ///**
  57. // @brief PWM 睡眠操作
  58. // @prama t - [in] 睡眠时间
  59. // @return 无
  60. //*/
  61. //static void cb_pwmSleep(uint32_t t)
  62. //{
  63. // nrfx_pwm_uninit(&m_pwm);
  64. //}
  65. /**
  66. @brief 设置PWM的通道的加载模式
  67. @param load_mode -[in] 加载模式
  68. @return 无
  69. */
  70. static void SetPwm_ChannelsLoadMode(nrf_pwm_dec_load_t load_mode)
  71. {
  72. m_config.load_mode = load_mode;
  73. }
  74. /*********************************************************************
  75. * PUBLIC FUNCTIONS
  76. */
  77. /**
  78. @brief 设置PWM的引脚通道
  79. @param p_pins -[in] 输入的引脚通道
  80. @return 无
  81. */
  82. void SetPwm_Channels(uint32_t channel_1, uint32_t channel_2, uint32_t channel_3, uint32_t channel_4)
  83. {
  84. m_config.output_pins[0] = channel_1;
  85. m_config.output_pins[1] = channel_2;
  86. m_config.output_pins[2] = channel_3;
  87. m_config.output_pins[3] = channel_4;
  88. }
  89. /**
  90. @brief 设置PWM的中断回调函数
  91. @param pwm_callback -[in] 中断函数的地址
  92. @param irq_priority -[in] 中断函数的优先级
  93. @return 无
  94. */
  95. void SetPwm_IRQ(nrfx_pwm_handler_t pwm_callback, uint8_t irq_priority)
  96. {
  97. m_pwm_callback = pwm_callback;
  98. m_config.irq_priority = irq_priority;
  99. }
  100. /**
  101. @brief 设置PWM的基础时钟
  102. @param clock -[in] 时钟频率
  103. @return 无
  104. */
  105. void SetPwm_BaseClock(nrf_pwm_clk_t clock)
  106. {
  107. m_config.base_clock = clock;
  108. }
  109. /**
  110. @brief 设置PWM的计数模式
  111. @param count_mode -[in] 计数模式
  112. @return 无
  113. */
  114. void SetPwm_CountMode(nrf_pwm_mode_t count_mode)
  115. {
  116. m_config.count_mode = count_mode;
  117. }
  118. /**
  119. @brief 设置PWM的最大的占空比阈值
  120. @param Max_duty_cycle_value -[in] 最大的占空比阈值,根据频率决定。
  121. @return 无
  122. */
  123. void SetPwm_DutyCycleThreshold(uint16_t Max_duty_cycle_value)
  124. {
  125. m_config.top_value = Max_duty_cycle_value;
  126. }
  127. /**
  128. @brief 设置PWM的序列的推进模式,定义下一个cycle的进行方式。
  129. @param step_mode -[in] 推进模式
  130. @return 无
  131. */
  132. void SetPwm_SequenceStepMode(nrf_pwm_dec_step_t step_mode)
  133. {
  134. m_config.step_mode = step_mode;
  135. }
  136. /**
  137. @brief PWM 的初始化
  138. @param 无
  139. @return 无
  140. */
  141. void Pwm_Initialize(void){
  142. Pwm_init();
  143. }
  144. /**
  145. @brief PWM 的未初始化
  146. @param 无
  147. @return 无
  148. */
  149. void Pwm_UnInitialize(void)
  150. {
  151. nrfx_pwm_uninit(&m_pwm);
  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. SetPwm_ChannelsLoadMode(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. SetPwm_ChannelsLoadMode(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 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 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. }