xclk.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #define NO_CAMERA_LEDC_CHANNEL 0xFF
  15. static ledc_channel_t g_ledc_channel = NO_CAMERA_LEDC_CHANNEL;
  16. esp_err_t xclk_timer_conf(int ledc_timer, int xclk_freq_hz)
  17. {
  18. ledc_timer_config_t timer_conf;
  19. timer_conf.duty_resolution = LEDC_TIMER_1_BIT;
  20. timer_conf.freq_hz = xclk_freq_hz;
  21. timer_conf.speed_mode = LEDC_LOW_SPEED_MODE;
  22. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0)
  23. timer_conf.deconfigure = false;
  24. #endif
  25. #if ESP_IDF_VERSION_MAJOR >= 4
  26. timer_conf.clk_cfg = LEDC_AUTO_CLK;
  27. #endif
  28. timer_conf.timer_num = (ledc_timer_t)ledc_timer;
  29. esp_err_t err = ledc_timer_config(&timer_conf);
  30. if (err != ESP_OK) {
  31. ESP_LOGE(TAG, "ledc_timer_config failed for freq %d, rc=%x", xclk_freq_hz, err);
  32. }
  33. return err;
  34. }
  35. esp_err_t camera_enable_out_clock(const camera_config_t* config)
  36. {
  37. esp_err_t err = xclk_timer_conf(config->ledc_timer, config->xclk_freq_hz);
  38. if (err != ESP_OK) {
  39. ESP_LOGE(TAG, "ledc_timer_config failed, rc=%x", err);
  40. return err;
  41. }
  42. g_ledc_channel = config->ledc_channel;
  43. ledc_channel_config_t ch_conf = {0};
  44. ch_conf.gpio_num = config->pin_xclk;
  45. ch_conf.speed_mode = LEDC_LOW_SPEED_MODE;
  46. ch_conf.channel = config->ledc_channel;
  47. ch_conf.intr_type = LEDC_INTR_DISABLE;
  48. ch_conf.timer_sel = config->ledc_timer;
  49. ch_conf.duty = 1;
  50. ch_conf.hpoint = 0;
  51. err = ledc_channel_config(&ch_conf);
  52. if (err != ESP_OK) {
  53. ESP_LOGE(TAG, "ledc_channel_config failed, rc=%x", err);
  54. return err;
  55. }
  56. return ESP_OK;
  57. }
  58. void camera_disable_out_clock()
  59. {
  60. if (g_ledc_channel != NO_CAMERA_LEDC_CHANNEL) {
  61. ledc_stop(LEDC_LOW_SPEED_MODE, g_ledc_channel, 0);
  62. g_ledc_channel = NO_CAMERA_LEDC_CHANNEL;
  63. }
  64. }