xclk.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "driver/gpio.h"
  2. #include "driver/ledc.h"
  3. #include "esp_err.h"
  4. #include "esp_log.h"
  5. #include "esp_system.h"
  6. #include "xclk.h"
  7. #include "esp_camera.h"
  8. #if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
  9. #include "esp32-hal-log.h"
  10. #else
  11. #include "esp_log.h"
  12. static const char* TAG = "camera_xclk";
  13. #endif
  14. static ledc_channel_t g_ledc_channel = 0;
  15. esp_err_t xclk_timer_conf(int ledc_timer, int xclk_freq_hz)
  16. {
  17. ledc_timer_config_t timer_conf;
  18. timer_conf.duty_resolution = LEDC_TIMER_1_BIT;
  19. timer_conf.freq_hz = xclk_freq_hz;
  20. timer_conf.speed_mode = LEDC_LOW_SPEED_MODE;
  21. #if ESP_IDF_VERSION_MAJOR >= 4
  22. timer_conf.clk_cfg = LEDC_AUTO_CLK;
  23. #endif
  24. timer_conf.timer_num = (ledc_timer_t)ledc_timer;
  25. esp_err_t err = ledc_timer_config(&timer_conf);
  26. if (err != ESP_OK) {
  27. ESP_LOGE(TAG, "ledc_timer_config failed for freq %d, rc=%x", xclk_freq_hz, err);
  28. }
  29. return err;
  30. }
  31. esp_err_t camera_enable_out_clock(camera_config_t* config)
  32. {
  33. esp_err_t err = xclk_timer_conf(config->ledc_timer, config->xclk_freq_hz);
  34. if (err != ESP_OK) {
  35. ESP_LOGE(TAG, "ledc_timer_config failed, rc=%x", err);
  36. return err;
  37. }
  38. g_ledc_channel = config->ledc_channel;
  39. ledc_channel_config_t ch_conf;
  40. ch_conf.gpio_num = config->pin_xclk;
  41. ch_conf.speed_mode = LEDC_LOW_SPEED_MODE;
  42. ch_conf.channel = config->ledc_channel;
  43. ch_conf.intr_type = LEDC_INTR_DISABLE;
  44. ch_conf.timer_sel = config->ledc_timer;
  45. ch_conf.duty = 1;
  46. ch_conf.hpoint = 0;
  47. err = ledc_channel_config(&ch_conf);
  48. if (err != ESP_OK) {
  49. ESP_LOGE(TAG, "ledc_channel_config failed, rc=%x", err);
  50. return err;
  51. }
  52. return ESP_OK;
  53. }
  54. void camera_disable_out_clock()
  55. {
  56. ledc_stop(LEDC_LOW_SPEED_MODE, g_ledc_channel, 0);
  57. }