esp32-camera/target/xclk.c
Juan Schiavoni c0c17bd3de
Fails with ESP32 (Standard) and not run with ESP-IDF v5.2 (#542)
* Fails with ESP32 (standard) and ESP-IDF v5.2

With IDF 5.2 master it works with esp32-s3 but fails with esp32 standard, and has two problems:

First is that when compiling it does not find the function: ets_delay_us'.
implicit declaration of function 'ets_delay_us';

Second that it fails to initialize xlock.
camera_xclk: ledc_timer_config failed, rc=103

The solution is to add #define ets_delay_us esp_rom_delay_us when the idf version is greater than 5 and initialize the variable timer_conf.deconfigure = false;

* Build fails with ESP32 (Standard) and does not run with ESP-IDF v5.2

With IDF 5.2 master it works with esp32-s3 but fails with esp32 standard, and has two problems:

First is that when compiling it does not find the function: ets_delay_us'.
implicit declaration of function 'ets_delay_us';

Second that it fails to initialize xlock.
camera_xclk: ledc_timer_config failed, rc=103

The solution is to add #define ets_delay_us esp_rom_delay_us when the idf version is greater than 5 and initialize the variable timer_conf.deconfigure = false;

* Fails with ESP32 (Standard) and not run with ESP-IDF v5.2

With IDF 5.2 master it works with esp32-s3 but fails with esp32 standard, and has two problems:

First is that when compiling it does not find the function: ets_delay_us'. implicit declaration of function 'ets_delay_us';

Second that it fails to initialize xlock.
camera_xclk: ledc_timer_config failed, rc=103

The solution is to add #define ets_delay_us esp_rom_delay_us when the idf version is greater than 5 and initialize the variable timer_conf.deconfigure = false;

---------

Co-authored-by: Me No Dev <me-no-dev@users.noreply.github.com>
2023-07-18 12:49:07 +03:00

72 lines
2 KiB
C
Executable file

#include "driver/gpio.h"
#include "driver/ledc.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_system.h"
#include "xclk.h"
#include "esp_camera.h"
#if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
#include "esp32-hal-log.h"
#else
#include "esp_log.h"
static const char* TAG = "camera_xclk";
#endif
#define NO_CAMERA_LEDC_CHANNEL 0xFF
static ledc_channel_t g_ledc_channel = NO_CAMERA_LEDC_CHANNEL;
esp_err_t xclk_timer_conf(int ledc_timer, int xclk_freq_hz)
{
ledc_timer_config_t timer_conf;
timer_conf.duty_resolution = LEDC_TIMER_1_BIT;
timer_conf.freq_hz = xclk_freq_hz;
timer_conf.speed_mode = LEDC_LOW_SPEED_MODE;
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0)
timer_conf.deconfigure = false;
#endif
#if ESP_IDF_VERSION_MAJOR >= 4
timer_conf.clk_cfg = LEDC_AUTO_CLK;
#endif
timer_conf.timer_num = (ledc_timer_t)ledc_timer;
esp_err_t err = ledc_timer_config(&timer_conf);
if (err != ESP_OK) {
ESP_LOGE(TAG, "ledc_timer_config failed for freq %d, rc=%x", xclk_freq_hz, err);
}
return err;
}
esp_err_t camera_enable_out_clock(const camera_config_t* config)
{
esp_err_t err = xclk_timer_conf(config->ledc_timer, config->xclk_freq_hz);
if (err != ESP_OK) {
ESP_LOGE(TAG, "ledc_timer_config failed, rc=%x", err);
return err;
}
g_ledc_channel = config->ledc_channel;
ledc_channel_config_t ch_conf;
ch_conf.gpio_num = config->pin_xclk;
ch_conf.speed_mode = LEDC_LOW_SPEED_MODE;
ch_conf.channel = config->ledc_channel;
ch_conf.intr_type = LEDC_INTR_DISABLE;
ch_conf.timer_sel = config->ledc_timer;
ch_conf.duty = 1;
ch_conf.hpoint = 0;
err = ledc_channel_config(&ch_conf);
if (err != ESP_OK) {
ESP_LOGE(TAG, "ledc_channel_config failed, rc=%x", err);
return err;
}
return ESP_OK;
}
void camera_disable_out_clock()
{
if (g_ledc_channel != NO_CAMERA_LEDC_CHANNEL) {
ledc_stop(LEDC_LOW_SPEED_MODE, g_ledc_channel, 0);
g_ledc_channel = NO_CAMERA_LEDC_CHANNEL;
}
}