xclk.c 2.1 KB

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