bsp_pwm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. #define PWM1_INSTANCE 1
  16. /*********************************************************************
  17. * LOCAL VARIABLES
  18. */
  19. static nrf_drv_pwm_t m_pwm = NRF_DRV_PWM_INSTANCE(PWM_INSTANCE);
  20. static nrf_drv_pwm_t m1_pwm = NRF_DRV_PWM_INSTANCE(PWM1_INSTANCE);
  21. static nrf_drv_pwm_config_t m_config =
  22. {
  23. .output_pins =
  24. {
  25. NRF_DRV_PWM_PIN_NOT_USED, // channel 0
  26. NRF_DRV_PWM_PIN_NOT_USED, // channel 1
  27. NRF_DRV_PWM_PIN_NOT_USED, // channel 2
  28. NRF_DRV_PWM_PIN_NOT_USED // channel 3
  29. },
  30. .irq_priority = PWM0_IRQ_PRIORITY,
  31. .base_clock = NRF_PWM_CLK_125kHz,
  32. .count_mode = NRF_PWM_MODE_UP,
  33. .top_value = 15612,
  34. .load_mode = NRF_PWM_LOAD_INDIVIDUAL,
  35. .step_mode = NRF_PWM_STEP_AUTO
  36. };
  37. static nrf_drv_pwm_config_t m1_config =
  38. {
  39. .output_pins =
  40. {
  41. NRF_DRV_PWM_PIN_NOT_USED, // channel 0
  42. NRF_DRV_PWM_PIN_NOT_USED, // channel 1
  43. NRF_DRV_PWM_PIN_NOT_USED, // channel 2
  44. NRF_DRV_PWM_PIN_NOT_USED // channel 3
  45. },
  46. .irq_priority = PWM0_IRQ_PRIORITY,
  47. .base_clock = NRF_PWM_CLK_125kHz,
  48. .count_mode = NRF_PWM_MODE_UP,
  49. .top_value = 15612,
  50. .load_mode = NRF_PWM_LOAD_INDIVIDUAL,
  51. .step_mode = NRF_PWM_STEP_AUTO
  52. };
  53. static nrfx_pwm_handler_t m_pwm_callback = NULL;
  54. static nrfx_pwm_handler_t m1_pwm_callback = NULL;
  55. static void bsp_pwm_init_process(void)
  56. {
  57. if(Except_TxError(EXCEPT_PWM_INIT,"bsp_pwm_init_error\r\n") == 0)
  58. {
  59. Process_Stop(bsp_pwm_init_process);
  60. }
  61. }
  62. static void bsp_pwm1_init_process(void)
  63. {
  64. if(Except_TxError(EXCEPT_PWM_INIT,"bsp_pwm1_init_error\r\n") == 0)
  65. {
  66. Process_Stop(bsp_pwm1_init_process);
  67. }
  68. }
  69. /*********************************************************************
  70. * LOCAL FUNCTIONS
  71. */
  72. /**
  73. @brief PWM 初始化
  74. @prama 无
  75. @return 无
  76. */
  77. static void Pwm_init(void)
  78. {
  79. int ret = 0;
  80. ret_code_t errCode;
  81. errCode = nrf_drv_pwm_init(&m_pwm, &m_config, m_pwm_callback);
  82. if(errCode != NRF_SUCCESS)ret = -1;
  83. if(ret == -1)
  84. {
  85. Process_Start(0,"bsp_pwm_init_process",bsp_pwm_init_process);
  86. }
  87. }
  88. /**
  89. @brief PWM 初始化
  90. @prama 无
  91. @return 无
  92. */
  93. static void Pwm1_init(void)
  94. {
  95. int ret = 0;
  96. ret_code_t errCode;
  97. errCode = nrf_drv_pwm_init(&m1_pwm, &m1_config, m1_pwm_callback);
  98. if(errCode != NRF_SUCCESS)ret = -1;
  99. if(ret == -1)
  100. {
  101. Process_Start(0,"bsp_pwm1_init_process",bsp_pwm1_init_process);
  102. }
  103. }
  104. ///**
  105. // @brief PWM 唤醒操作
  106. // @prama t - [in] 唤醒时间
  107. // @return 无
  108. //*/
  109. //static void cb_pwmWakeup(uint32_t t)
  110. //{
  111. // Pwm_init();
  112. //}
  113. ///**
  114. // @brief PWM 睡眠操作
  115. // @prama t - [in] 睡眠时间
  116. // @return 无
  117. //*/
  118. //static void cb_pwmSleep(uint32_t t)
  119. //{
  120. // nrfx_pwm_uninit(&m_pwm);
  121. //}
  122. /**
  123. @brief 设置PWM的通道的加载模式
  124. @param load_mode -[in] 加载模式
  125. @return 无
  126. */
  127. static void SetPwm_ChannelsLoadMode(nrf_pwm_dec_load_t load_mode)
  128. {
  129. m_config.load_mode = load_mode;
  130. }
  131. /**
  132. @brief 设置PWM1的通道的加载模式
  133. @param load_mode -[in] 加载模式
  134. @return 无
  135. */
  136. static void SetPwm1_ChannelsLoadMode(nrf_pwm_dec_load_t load_mode)
  137. {
  138. m1_config.load_mode = load_mode;
  139. }
  140. /*********************************************************************
  141. * PUBLIC FUNCTIONS
  142. */
  143. /**
  144. @brief 设置PWM的引脚通道
  145. @param p_pins -[in] 输入的引脚通道
  146. @return 无
  147. */
  148. void SetPwm_Channels(uint32_t channel_1, uint32_t channel_2, uint32_t channel_3, uint32_t channel_4)
  149. {
  150. m_config.output_pins[0] = channel_1;
  151. m_config.output_pins[1] = channel_2;
  152. m_config.output_pins[2] = channel_3;
  153. m_config.output_pins[3] = channel_4;
  154. }
  155. /**
  156. @brief 设置PWM的中断回调函数
  157. @param pwm_callback -[in] 中断函数的地址
  158. @param irq_priority -[in] 中断函数的优先级
  159. @return 无
  160. */
  161. void SetPwm_IRQ(nrfx_pwm_handler_t pwm_callback, uint8_t irq_priority)
  162. {
  163. m_pwm_callback = pwm_callback;
  164. m_config.irq_priority = irq_priority;
  165. }
  166. /**
  167. @brief 设置PWM的基础时钟
  168. @param clock -[in] 时钟频率
  169. @return 无
  170. */
  171. void SetPwm_BaseClock(nrf_pwm_clk_t clock)
  172. {
  173. m_config.base_clock = clock;
  174. }
  175. /**
  176. @brief 设置PWM的计数模式
  177. @param count_mode -[in] 计数模式
  178. @return 无
  179. */
  180. void SetPwm_CountMode(nrf_pwm_mode_t count_mode)
  181. {
  182. m_config.count_mode = count_mode;
  183. }
  184. /**
  185. @brief 设置PWM的最大的占空比阈值
  186. @param Max_duty_cycle_value -[in] 最大的占空比阈值,根据频率决定。
  187. @return 无
  188. */
  189. void SetPwm_DutyCycleThreshold(uint16_t Max_duty_cycle_value)
  190. {
  191. m_config.top_value = Max_duty_cycle_value;
  192. }
  193. /**
  194. @brief 设置PWM的序列的推进模式,定义下一个cycle的进行方式。
  195. @param step_mode -[in] 推进模式
  196. @return 无
  197. */
  198. void SetPwm_SequenceStepMode(nrf_pwm_dec_step_t step_mode)
  199. {
  200. m_config.step_mode = step_mode;
  201. }
  202. /**
  203. @brief PWM 的初始化
  204. @param 无
  205. @return 无
  206. */
  207. void Pwm_Initialize(void){
  208. Pwm_init();
  209. }
  210. /**
  211. @brief PWM 的未初始化
  212. @param 无
  213. @return 无
  214. */
  215. void Pwm_UnInitialize(void)
  216. {
  217. nrfx_pwm_uninit(&m_pwm);
  218. }
  219. /**
  220. @brief 设置一个独立通道序列
  221. @param p_seqValues -[in] 指向一个独立通道序列
  222. @param seqValues_length - [in] 序列的大小
  223. @param seqValues_repeats -[in] 序列的每个占空比重复的次数
  224. @param seqValues_end_delay - [in] 每个序列中,最后一个占空比重复的次数
  225. @return 无
  226. */
  227. 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)
  228. {
  229. static nrf_pwm_sequence_t m_sequence;
  230. if(m_config.load_mode != NRF_PWM_LOAD_INDIVIDUAL)
  231. {
  232. SetPwm_ChannelsLoadMode(NRF_PWM_LOAD_INDIVIDUAL);
  233. Pwm_UnInitialize();
  234. Pwm_Initialize();
  235. }
  236. m_sequence.values.p_individual = (nrf_pwm_values_individual_t*)p_seqValues;
  237. m_sequence.length = seqValues_length;
  238. m_sequence.repeats = seqValues_repeats;
  239. m_sequence.end_delay = seqValues_end_delay;
  240. return &m_sequence;
  241. }
  242. /**
  243. @brief 设置一个共用通道序列
  244. @param p_seqValues -[in] 指向一个共用通道序列
  245. @param seqValues_length - [in] 序列的大小
  246. @param seqValues_repeats -[in] 序列的每个占空比重复的次数
  247. @param seqValues_end_delay - [in] 每个序列中,最后一个占空比重复的次数
  248. @return 无
  249. */
  250. 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)
  251. {
  252. static nrf_pwm_sequence_t m_sequence;
  253. if(m_config.load_mode != NRF_PWM_LOAD_COMMON)
  254. {
  255. SetPwm_ChannelsLoadMode(NRF_PWM_LOAD_COMMON);
  256. Pwm_UnInitialize();
  257. Pwm_Initialize();
  258. }
  259. m_sequence.values.p_common = (nrf_pwm_values_common_t*)p_seqValues;
  260. m_sequence.length = seqValues_length;
  261. m_sequence.repeats = seqValues_repeats;
  262. m_sequence.end_delay = seqValues_end_delay;
  263. return &m_sequence;
  264. }
  265. /**
  266. @brief 设置PWM的指定单个序列的播放
  267. @param pwm_sequence -[in] 输入序列的值
  268. @param playback_count -[in] 播放次数
  269. @param flags - [in] 播放模式
  270. @return 错误代码
  271. */
  272. uint32_t SetSimplePwmPlayBack(nrf_pwm_sequence_t *pwm_sequence, uint16_t playback_count, uint32_t flags)
  273. {
  274. return nrf_drv_pwm_simple_playback(&m_pwm, pwm_sequence, playback_count, flags);
  275. }
  276. /**
  277. @brief 设置PWM的指定多个序列的播放
  278. @param pwm_sequence0 -[in] 输入序列的值
  279. @param pwm_sequence1 -[in] 输入序列的值
  280. @param playback_count -[in] 播放次数
  281. @param flags - [in] 播放模式
  282. @return 错误代码
  283. */
  284. uint32_t SetComplexPwmPlayBack(nrf_pwm_sequence_t *pwm_sequence0, nrf_pwm_sequence_t *pwm_sequence1, uint16_t playback_count, uint32_t flags)
  285. {
  286. return nrf_drv_pwm_complex_playback(&m_pwm, pwm_sequence0, pwm_sequence1, playback_count, flags);
  287. }
  288. /****************************************************************************************************************************************************************************/
  289. /**
  290. @brief 设置PWM1的引脚通道
  291. @param p_pins -[in] 输入的引脚通道
  292. @return 无
  293. */
  294. void SetPwm1_Channels(uint32_t channel_1, uint32_t channel_2, uint32_t channel_3, uint32_t channel_4)
  295. {
  296. m1_config.output_pins[0] = channel_1;
  297. m1_config.output_pins[1] = channel_2;
  298. m1_config.output_pins[2] = channel_3;
  299. m1_config.output_pins[3] = channel_4;
  300. }
  301. /**
  302. @brief 设置PWM1的中断回调函数
  303. @param pwm_callback -[in] 中断函数的地址
  304. @param irq_priority -[in] 中断函数的优先级
  305. @return 无
  306. */
  307. void SetPwm1_IRQ(nrfx_pwm_handler_t pwm_callback, uint8_t irq_priority)
  308. {
  309. m1_pwm_callback = pwm_callback;
  310. m1_config.irq_priority = irq_priority;
  311. }
  312. /**
  313. @brief 设置PWM1的基础时钟
  314. @param clock -[in] 时钟频率
  315. @return 无
  316. */
  317. void SetPwm1_BaseClock(nrf_pwm_clk_t clock)
  318. {
  319. m1_config.base_clock = clock;
  320. }
  321. /**
  322. @brief 设置PWM1的计数模式
  323. @param count_mode -[in] 计数模式
  324. @return 无
  325. */
  326. void SetPwm1_CountMode(nrf_pwm_mode_t count_mode)
  327. {
  328. m1_config.count_mode = count_mode;
  329. }
  330. /**
  331. @brief 设置PWM1的最大的占空比阈值
  332. @param Max_duty_cycle_value -[in] 最大的占空比阈值,根据频率决定。
  333. @return 无
  334. */
  335. void SetPwm1_DutyCycleThreshold(uint16_t Max_duty_cycle_value)
  336. {
  337. m1_config.top_value = Max_duty_cycle_value;
  338. }
  339. /**
  340. @brief 设置PWM1的序列的推进模式,定义下一个cycle的进行方式。
  341. @param step_mode -[in] 推进模式
  342. @return 无
  343. */
  344. void SetPwm1_SequenceStepMode(nrf_pwm_dec_step_t step_mode)
  345. {
  346. m1_config.step_mode = step_mode;
  347. }
  348. /**
  349. @brief PWM1 的初始化
  350. @param 无
  351. @return 无
  352. */
  353. void Pwm1_Initialize(void){
  354. Pwm1_init();
  355. }
  356. /**
  357. @brief PWM 的未初始化
  358. @param 无
  359. @return 无
  360. */
  361. void Pwm1_UnInitialize(void)
  362. {
  363. nrfx_pwm_uninit(&m1_pwm);
  364. }
  365. /**
  366. @brief 设置一个独立通道序列
  367. @param p_seqValues -[in] 指向一个独立通道序列
  368. @param seqValues_length - [in] 序列的大小
  369. @param seqValues_repeats -[in] 序列的每个占空比重复的次数
  370. @param seqValues_end_delay - [in] 每个序列中,最后一个占空比重复的次数
  371. @return 无
  372. */
  373. nrf_pwm_sequence_t* Pwm1_SetIndSequence(pwm_values_individual_t *p_seqValues, uint16_t seqValues_length, uint32_t seqValues_repeats, uint32_t seqValues_end_delay)
  374. {
  375. static nrf_pwm_sequence_t m1_sequence;
  376. if(m1_config.load_mode != NRF_PWM_LOAD_INDIVIDUAL)
  377. {
  378. SetPwm1_ChannelsLoadMode(NRF_PWM_LOAD_INDIVIDUAL);
  379. Pwm1_UnInitialize();
  380. Pwm1_Initialize();
  381. }
  382. m1_sequence.values.p_individual = (nrf_pwm_values_individual_t*)p_seqValues;
  383. m1_sequence.length = seqValues_length;
  384. m1_sequence.repeats = seqValues_repeats;
  385. m1_sequence.end_delay = seqValues_end_delay;
  386. return &m1_sequence;
  387. }
  388. /**
  389. @brief 设置一个共用通道序列
  390. @param p_seqValues -[in] 指向一个共用通道序列
  391. @param seqValues_length - [in] 序列的大小
  392. @param seqValues_repeats -[in] 序列的每个占空比重复的次数
  393. @param seqValues_end_delay - [in] 每个序列中,最后一个占空比重复的次数
  394. @return 无
  395. */
  396. nrf_pwm_sequence_t* Pwm1_SetComSequence(pwm_values_common_t *p_seqValues, uint16_t seqValues_length, uint32_t seqValues_repeats, uint32_t seqValues_end_delay)
  397. {
  398. static nrf_pwm_sequence_t m1_sequence;
  399. if(m1_config.load_mode != NRF_PWM_LOAD_COMMON)
  400. {
  401. SetPwm1_ChannelsLoadMode(NRF_PWM_LOAD_COMMON);
  402. Pwm1_UnInitialize();
  403. Pwm1_Initialize();
  404. }
  405. m1_sequence.values.p_common = (nrf_pwm_values_common_t*)p_seqValues;
  406. m1_sequence.length = seqValues_length;
  407. m1_sequence.repeats = seqValues_repeats;
  408. m1_sequence.end_delay = seqValues_end_delay;
  409. return &m1_sequence;
  410. }
  411. /**
  412. @brief 设置PWM的指定单个序列的播放
  413. @param pwm_sequence -[in] 输入序列的值
  414. @param playback_count -[in] 播放次数
  415. @param flags - [in] 播放模式
  416. @return 错误代码
  417. */
  418. uint32_t SetSimplePwm1PlayBack(nrf_pwm_sequence_t *pwm_sequence, uint16_t playback_count, uint32_t flags)
  419. {
  420. return nrf_drv_pwm_simple_playback(&m1_pwm, pwm_sequence, playback_count, flags);
  421. }
  422. /**
  423. @brief 设置PWM的指定多个序列的播放
  424. @param pwm_sequence0 -[in] 输入序列的值
  425. @param pwm_sequence1 -[in] 输入序列的值
  426. @param playback_count -[in] 播放次数
  427. @param flags - [in] 播放模式
  428. @return 错误代码
  429. */
  430. uint32_t SetComplexPwm1PlayBack(nrf_pwm_sequence_t *pwm_sequence0, nrf_pwm_sequence_t *pwm_sequence1, uint16_t playback_count, uint32_t flags)
  431. {
  432. return nrf_drv_pwm_complex_playback(&m1_pwm, pwm_sequence0, pwm_sequence1, playback_count, flags);
  433. }