xclk.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "nrf_drv_pwm.h"
  4. #include "app_util_platform.h"
  5. #include "app_timer.h"
  6. #include "nrf_drv_clock.h"
  7. #include "app_error.h"
  8. #include "nrf_log.h"
  9. #include "nrf_log_ctrl.h"
  10. #include "nrf_log_default_backends.h"
  11. #include "xclk.h"
  12. static nrf_drv_pwm_t pwm_instance = NRF_DRV_PWM_INSTANCE(0);
  13. static void pwm_handler(nrf_drv_pwm_evt_type_t event_type)
  14. {
  15. if(event_type == NRF_DRV_PWM_EVT_FINISHED)
  16. {
  17. }
  18. }
  19. static ret_code_t xclk_init(void)
  20. {
  21. nrfx_pwm_config_t const pwm_cfg = {
  22. .output_pins = {
  23. CAM_XCLK_PIN | NRF_DRV_PWM_PIN_INVERTED, //将pwm0的通道0映射到引脚17
  24. NRF_DRV_PWM_PIN_NOT_USED,
  25. NRF_DRV_PWM_PIN_NOT_USED,
  26. NRF_DRV_PWM_PIN_NOT_USED, //其他通道不使用
  27. },
  28. .irq_priority = APP_IRQ_PRIORITY_LOWEST, //设置优先级
  29. .base_clock = NRF_PWM_CLK_16MHz, //设置时钟频率1M
  30. .count_mode = NRF_PWM_MODE_UP, //设置向上计数模式
  31. .top_value = 16, //设置top值
  32. .load_mode = NRF_PWM_LOAD_COMMON, //设置装载模式common
  33. .step_mode = NRF_PWM_STEP_AUTO //序列周期自动推进
  34. };
  35. return nrfx_pwm_init(&pwm_instance, &pwm_cfg, pwm_handler);
  36. }
  37. /**
  38. * 开启pwm函数
  39. */
  40. static void xclk_play(void)
  41. {
  42. static const nrf_pwm_values_common_t seq0_values = 8;
  43. //设置pwm播放设置
  44. nrf_pwm_sequence_t const seq0 = {
  45. .values.p_common = &seq0_values, //指向pwm占空比序列
  46. .length = NRF_PWM_VALUES_LENGTH(seq0_values), //计算pwm包含周期数200
  47. .repeats = 0, //序列中周期重复数为0
  48. .end_delay = 0 //序列后不插入延时
  49. };
  50. //自动触发pwm重新播放
  51. (void)nrfx_pwm_simple_playback(&pwm_instance,&seq0,1,NRFX_PWM_FLAG_LOOP);
  52. }
  53. static void xclk_stop(void)
  54. {
  55. nrfx_pwm_stop(&pwm_instance, true);
  56. }
  57. static void xclk_uninit(void)
  58. {
  59. nrfx_pwm_uninit(&pwm_instance);
  60. }
  61. ret_code_t xclk_timer_conf(void)
  62. {
  63. ret_code_t err = xclk_init();
  64. if (err != NRF_SUCCESS) {
  65. NRF_LOG_INFO("xclk pwm failed for rc=%x", err);
  66. }
  67. return err;
  68. }
  69. void camera_enable_out_clock(void)
  70. {
  71. xclk_play();
  72. }
  73. void camera_disable_out_clock()
  74. {
  75. xclk_stop();
  76. }