bsp_pwm.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 "bsp_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. static void Pwm_init(void)
  52. {
  53. int ret = 0;
  54. ret_code_t errCode;
  55. errCode = nrf_drv_pwm_init(&m_pwm, &m_config, m_pwm_callback);
  56. if(errCode != NRF_SUCCESS)ret = -1;
  57. if(ret == -1)
  58. {
  59. Process_Start(0,"pwm_init",bsp_pwm_init_process);
  60. }
  61. }
  62. ///**
  63. // @brief PWM 唤醒操作
  64. // @prama t - [in] 唤醒时间
  65. // @return 无
  66. //*/
  67. //static void cb_pwmWakeup(uint32_t t)
  68. //{
  69. // Pwm_init();
  70. //}
  71. ///**
  72. // @brief PWM 睡眠操作
  73. // @prama t - [in] 睡眠时间
  74. // @return 无
  75. //*/
  76. //static void cb_pwmSleep(uint32_t t)
  77. //{
  78. // nrfx_pwm_uninit(&m_pwm);
  79. //}
  80. /**
  81. @brief 设置PWM的通道的加载模式
  82. @param load_mode -[in] 加载模式
  83. @return 无
  84. */
  85. static void SetPwm_ChannelsLoadMode(nrf_pwm_dec_load_t load_mode)
  86. {
  87. m_config.load_mode = load_mode;
  88. }
  89. /*********************************************************************
  90. * PUBLIC FUNCTIONS
  91. */
  92. /**
  93. @brief 设置PWM的引脚通道
  94. @param p_pins -[in] 输入的引脚通道
  95. @return 无
  96. */
  97. void SetPwm_Channels(uint32_t channel_1, uint32_t channel_2, uint32_t channel_3, uint32_t channel_4)
  98. {
  99. m_config.output_pins[0] = channel_1;
  100. m_config.output_pins[1] = channel_2;
  101. m_config.output_pins[2] = channel_3;
  102. m_config.output_pins[3] = channel_4;
  103. }
  104. /**
  105. @brief 设置PWM的中断回调函数
  106. @param pwm_callback -[in] 中断函数的地址
  107. @param irq_priority -[in] 中断函数的优先级
  108. @return 无
  109. */
  110. void SetPwm_IRQ(nrfx_pwm_handler_t pwm_callback, uint8_t irq_priority)
  111. {
  112. m_pwm_callback = pwm_callback;
  113. m_config.irq_priority = irq_priority;
  114. }
  115. /**
  116. @brief 设置PWM的基础时钟
  117. @param clock -[in] 时钟频率
  118. @return 无
  119. */
  120. void SetPwm_BaseClock(nrf_pwm_clk_t clock)
  121. {
  122. m_config.base_clock = clock;
  123. }
  124. /**
  125. @brief 设置PWM的计数模式
  126. @param count_mode -[in] 计数模式
  127. @return 无
  128. */
  129. void SetPwm_CountMode(nrf_pwm_mode_t count_mode)
  130. {
  131. m_config.count_mode = count_mode;
  132. }
  133. /**
  134. @brief 设置PWM的最大的占空比阈值
  135. @param Max_duty_cycle_value -[in] 最大的占空比阈值,根据频率决定。
  136. @return 无
  137. */
  138. void SetPwm_DutyCycleThreshold(uint16_t Max_duty_cycle_value)
  139. {
  140. m_config.top_value = Max_duty_cycle_value;
  141. }
  142. /**
  143. @brief 设置PWM的序列的推进模式,定义下一个cycle的进行方式。
  144. @param step_mode -[in] 推进模式
  145. @return 无
  146. */
  147. void SetPwm_SequenceStepMode(nrf_pwm_dec_step_t step_mode)
  148. {
  149. m_config.step_mode = step_mode;
  150. }
  151. /**
  152. @brief PWM 的初始化
  153. @param 无
  154. @return 无
  155. */
  156. void Pwm_Initialize(void){
  157. Pwm_init();
  158. }
  159. /**
  160. @brief PWM 的未初始化
  161. @param 无
  162. @return 无
  163. */
  164. void Pwm_UnInitialize(void)
  165. {
  166. nrfx_pwm_uninit(&m_pwm);
  167. }
  168. /**
  169. @brief 设置一个独立通道序列
  170. @param p_seqValues -[in] 指向一个独立通道序列
  171. @param seqValues_length - [in] 序列的大小
  172. @param seqValues_repeats -[in] 序列的每个占空比重复的次数
  173. @param seqValues_end_delay - [in] 每个序列中,最后一个占空比重复的次数
  174. @return 无
  175. */
  176. 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)
  177. {
  178. static nrf_pwm_sequence_t m_sequence;
  179. if(m_config.load_mode != NRF_PWM_LOAD_INDIVIDUAL)
  180. {
  181. SetPwm_ChannelsLoadMode(NRF_PWM_LOAD_INDIVIDUAL);
  182. Pwm_UnInitialize();
  183. Pwm_Initialize();
  184. }
  185. m_sequence.values.p_individual = (nrf_pwm_values_individual_t*)p_seqValues;
  186. m_sequence.length = seqValues_length;
  187. m_sequence.repeats = seqValues_repeats;
  188. m_sequence.end_delay = seqValues_end_delay;
  189. return &m_sequence;
  190. }
  191. /**
  192. @brief 设置一个共用通道序列
  193. @param p_seqValues -[in] 指向一个共用通道序列
  194. @param seqValues_length - [in] 序列的大小
  195. @param seqValues_repeats -[in] 序列的每个占空比重复的次数
  196. @param seqValues_end_delay - [in] 每个序列中,最后一个占空比重复的次数
  197. @return 无
  198. */
  199. 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)
  200. {
  201. static nrf_pwm_sequence_t m_sequence;
  202. if(m_config.load_mode != NRF_PWM_LOAD_COMMON)
  203. {
  204. SetPwm_ChannelsLoadMode(NRF_PWM_LOAD_COMMON);
  205. Pwm_UnInitialize();
  206. Pwm_Initialize();
  207. }
  208. m_sequence.values.p_common = (nrf_pwm_values_common_t*)p_seqValues;
  209. m_sequence.length = seqValues_length;
  210. m_sequence.repeats = seqValues_repeats;
  211. m_sequence.end_delay = seqValues_end_delay;
  212. return &m_sequence;
  213. }
  214. /**
  215. @brief 设置PWM的指定单个序列的播放
  216. @param pwm_sequence -[in] 输入序列的值
  217. @param playback_count -[in] 播放次数
  218. @param flags - [in] 播放模式
  219. @return 错误代码
  220. */
  221. uint32_t SetSimplePwmPlayBack(nrf_pwm_sequence_t *pwm_sequence, uint16_t playback_count, uint32_t flags)
  222. {
  223. return nrf_drv_pwm_simple_playback(&m_pwm, pwm_sequence, playback_count, flags);
  224. }
  225. /**
  226. @brief 设置PWM的指定多个序列的播放
  227. @param pwm_sequence0 -[in] 输入序列的值
  228. @param pwm_sequence1 -[in] 输入序列的值
  229. @param playback_count -[in] 播放次数
  230. @param flags - [in] 播放模式
  231. @return 错误代码
  232. */
  233. uint32_t SetComplexPwmPlayBack(nrf_pwm_sequence_t *pwm_sequence0, nrf_pwm_sequence_t *pwm_sequence1, uint16_t playback_count, uint32_t flags)
  234. {
  235. return nrf_drv_pwm_complex_playback(&m_pwm, pwm_sequence0, pwm_sequence1, playback_count, flags);
  236. }