logo elektroda
logo elektroda
X
logo elektroda

[Solved] [ESP-IDF] Retrieving information about the operating frequency of the selected core in the ESP32

khoam 1134 22
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 20881885
    khoam
    Level 42  
    @ElectrodBot Write a function in C to retrieve information about the operating frequency of the selected core in the ESP32. Use ESP-IDF.
  • ADVERTISEMENT
  • #2 20881893
    ElektrodaBot
    AI OpenAI ChatGPT4
    khoam wrote:
    Write a function in C to retrieve information about the operating frequency of the selected core in the ESP32. Use ESP-IDF.
    Of course, I would be happy to help you write such a function. In ESP32, to read the core frequency, you can use the `esp_clk_cpu_freq()` function available in ESP-IDF, which returns the CPU frequency in Hz.

    Below you will find an example of the function `get_core_frequency()`, which reads and returns the operating frequency of the selected ESP32 core:


    Code: C / C++
    Log in, to see the code


    It is worth noting that in ESP32, both cores (if you are using the dual-core version of ESP32) typically operate at the same frequency, so the above function will return the same value for both cores. However, if you are working on a version of ESP32 that allows independent control of the frequency of the cores, you will need to look for other functions specific to that configuration.

    Remember that the code needs to be compiled and loaded into ESP32 using the tools provided by ESP-IDF. When run on the ESP32, the above program will print the CPU core frequency to the console.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • #3 20881902
    khoam
    Level 42  
    ElektrodaBot wrote:
    If, however, you are working on a version of ESP32 that allows independent control of the frequency of the cores, you will need to look for other functions specific to this configuration.
    This is about a function that will take a core number as an argument and for this core return its operating frequency.
  • ADVERTISEMENT
  • #4 20881920
    ElektrodaBot
    AI OpenAI ChatGPT4
    khoam wrote:
    What you want is a function that will take a core number as an argument and for that core return the frequency of its operation.
    Understood, you would want a function that takes a core number as an argument and returns the frequency of operation for it. On the ESP32, both cores typically run at the same clock rate, and the SDK does not provide a direct API to get the frequency for each core. The clock frequency of the cores is common to both and managed by the system driver, and this configuration is global and independent of the core number.

    Nevertheless, below I provide an example of a function that takes the core number as an argument and returns the operating frequency, but it should be noted that the value returned will be the same for both cores. We can also add a simple mechanism to check that the given core number is correct (ESP32 has cores numbered 0 and 1):


    Code: C / C++
    Log in, to see the code


    Please note that if in the future ESP-IDF introduces support for individual core clocking, the above function may need to be updated to reflect these changes. At the moment, however, the above functionality is not available in the official SDK for ESP32.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • #5 20881924
    khoam
    Level 42  
    ElektrodaBot wrote:
    However, below I give an example of a function that takes a core number as an argument and returns the operating frequency, but note that the value returned will be the same for both cores.
    This does not have to be the case at all. Consider using the esp_ipc_call() function.
  • #6 20881942
    ElektrodaBot
    AI OpenAI ChatGPT4
    khoam wrote:
    This does not have to be the case at all. Consider using the esp_ipc_call() function.
    I understand you want to use `esp_ipc_call()` to run code on a specific core, which can be useful in some situations where you want to see if code running on a specific core can get different results regarding the operating frequency of that core (although in practice the frequency for both cores on the ESP32 is the same, unless a specific configuration has been put in place for some use case).

    The `esp_ipc_call()` function allows the specified function to be called on the specified core. Below you will find an example of a function that takes a core number as an argument and returns its frequency:


    Code: C / C++
    Log in, to see the code


    In the above code, the function `get_core_frequency()` initiates an IPC call that runs the function `ipc_get_core_freq()` on the core with the specified `core_id` number. The `ipc_get_core_freq()` function then reads the clock frequency for that core and writes the result to the global variable `s_core_freq`, which `get_core_frequency()` reads after waiting for the IPC operation to complete.

    Please remember to call the `get_core_frequency()` function from a core other than the one for which you want to read the operating frequency, as jamming can occur as the function waits for the `s_core_freq` variable to be set, which will not happen if the calling core waits for the `ipc_get_core_freq()` function to complete.

    It is worth adding that in a typical ESP32 use case the frequency of both cores is the same, but if the system configuration allows different frequencies, the above code may return different results for each core.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • #7 20881950
    khoam
    Level 42  
    ElektrodaBot wrote:
    The `ipc_get_core_freq()` function then reads the clock frequency for that core and writes the result to the global variable `s_core_freq`, which `get_core_frequency()` reads after waiting for the IPC operation to complete.

    Try changing this so that it does not use the global variable. The esp_ipc_call() function can take a pointer argument to a void.
  • ADVERTISEMENT
  • #8 20881959
    ElektrodaBot
    AI OpenAI ChatGPT4
    khoam wrote:
    Try changing this to not use a global variable. The esp_ipc_call() function can take a pointer argument to a void.
    Thanks for your comments. Actually `esp_ipc_call()` allows us to pass an argument to a function that will be called on another core. We can use this functionality to pass a pointer to a variable where we want to store the frequency of the core.

    This is a modified version of the `get_core_frequency()` function without using a global variable:


    Code: C / C++
    Log in, to see the code


    In the above code, instead of using a global variable to store the result, we pass a pointer to the local `core_freq` variable directly to the `ipc_get_core_freq()` function. This ensures that the result is written directly to this variable, and `esp_ipc_call()` waits for the `ipc_get_core_freq()` function to finish executing, so there is no need for additional waiting in the form of a loop or delay.

    Please note that the above code assumes that the `esp_ipc_call()` function is blocking, meaning that it will only return control to the calling function after the function called on another core has finished executing. This is important because it means that after calling `esp_ipc_call()` we can immediately read the result from the local variable passed in.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • #9 20881965
    khoam
    Level 42  
    ElektrodaBot wrote:
    esp_ipc_call()` waits for the execution of the function `ipc_get_core_freq()
    The function esp_ipc_call() is not a blocking function. There is another version of this function called esp_ipc_call_blocking(), which is a blocking function.
  • #10 20881970
    ElektrodaBot
    AI OpenAI ChatGPT4
    khoam wrote:
    The esp_ipc_call() function is not a blocking function. There is another version of this function called esp_ipc_call_blocking(), which is a blocking function.
    I apologise for the earlier misunderstanding and thank you for the correction. So we will use the `esp_ipc_call_blocking()` function to call a function on a specific core and wait for it to execute.

    This is a corrected version of the `get_core_frequency()` function using `esp_ipc_call_blocking()`:

    Code: C / C++
    Log in, to see the code


    In this code we use `esp_ipc_call_blocking()` instead of `esp_ipc_call()` to ensure that the function waits for the IPC function to finish running on another core before returning control to the calling function. Thus, we can safely read the value of the core's operating frequency after the IPC call has completed, without the need for additional synchronisation mechanisms.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • #11 20881983
    khoam
    Level 42  
    ElektrodaBot wrote:
    This is a revised version of the `get_core_frequency()` function using `esp_ipc_call_blocking()`
    Write now an analogous function in C++20 that will use a lambda function instead of the ipc_get_core_freq() function.
  • #12 20882001
    ElektrodaBot
    AI OpenAI ChatGPT4
    khoam wrote:
    Write now an analogous function in C++20 that will use a lambda function instead of the ipc_get_core_freq() function.
    In C++20 we can use lambda expressions, which are very useful when we want to pass a block of code as a function argument without having to define a separate function. Lambdas can also capture variables from the environment, allowing us to work with the context without having to pass additional parameters.

    In the case of the `esp_ipc_call_blocking()` function, we can use a lambda by passing it as a function to call. Below is an example of using a lambda to get the core frequency in an ESP32 using C++20:


    Code: text
    Log in, to see the code


    In the above code, the `ipc_lambda` lambda captures a reference to the `core_freq` variable and writes the CPU frequency to it using `esp_clk_cpu_freq()`. Since `esp_ipc_call_blocking()` requires a pointer to a function rather than a lambda function object, we convert the lambda to `std::function` and then pass in a callback function `ipc_callback` that is compatible with the type expected by `esp_ipc_call_blocking()`.

    The `ipc_callback` function is a simple wrapper function that calls `std::function` passed as an `void*` argument. We pass a pointer to `std::function` to `esp_ipc_call_blocking()`, which allows the lambda to be called on the designated core.

    Remember that in C++ you must compile your project with the appropriate compiler flags to allow support for the C++20 standard and lambda functions. In addition, the example assumes that ESP-IDF functions are available in C++ and that the appropriate headers have been included to be C++ compatible.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • #13 20882011
    khoam
    Level 42  
    ElektrodaBot wrote:
    In the above code

    The code is unintelligible. Tags such as """ or ">" have appeared.

    What header file is required to use the esp_clk_cpu_freq() function?
  • #14 20882018
    ElektrodaBot
    AI OpenAI ChatGPT4
    khoam wrote:
    The code is incomprehensible. Tags such as """ or ">" have appeared.

    What header file is required to use the esp_clk_cpu_freq() function?
    I apologise for the confusion about special characters in the code. This is probably due to the HTML formatting. Here is the corrected code in C++20 that uses a lambda expression to obtain the core frequency without using additional helper functions:


    Code: text
    Log in, to see the code


    To use the `esp_clk_cpu_freq()` function, you must include the following header file:

    Code: text
    Log in, to see the code


    The `esp_clk.h` contains function declarations related to the ESP32 system clock, including the function `esp_clk_cpu_freq()` used to retrieve the CPU frequency.

    Before compiling, make sure that your development environment is configured to work with C++20 and ESP-IDF. Also, if you are using ESP-IDF, you must have your project properly configured to allow functions to be called from the `esp_clk.h` header file.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • #15 20882028
    khoam
    Level 42  
    ElektrodaBot wrote:
    File `esp_clk.h`

    The compiler does not find such a header file.
    Return to the last version of the function in C. Try using the function esp_clk_tree_src_get_freq_hz() instead of esp_clk_cpu_freq().
  • #16 20882032
    ElektrodaBot
    AI OpenAI ChatGPT4
    khoam wrote:
    The compiler does not find such a header file.
    khoam wrote:
    Let's go back to the last version of the function in C. Try using the function esp_clk_tree_src_get_freq_hz() instead of esp_clk_cpu_freq().
    Sorry for misleading you - my mistake. The correct function to get the CPU core frequency in ESP-IDF is `esp_clk_cpu_freq` and it is declared in the file `esp32/clk.h` or `esp_clk.h` (depending on the ESP-IDF version). The omission regarding the missing file is due to my error.

    If you want to use the `esp_clk_tree_src_get_freq_hz()` function, which can provide the frequency for different clock sources in the clock tree, you need to know that it may require a different header. This function is part of the system clocks API, so make sure you have the appropriate ESP-IDF headers imported.

    The following is an example of the use of the `esp_clk_tree_src_get_freq_hz()` function in C:


    Code: C / C++
    Log in, to see the code


    Remember to check that your development environment is set to the correct version of ESP-IDF and that the `esp32/clk.h` file (or alternatively `esp_clk.h` depending on the ESP-IDF version) is available. It is in this header file that the `esp_clk_tree_src_get_freq_hz()` function declaration is located. If you are using the latest version of ESP-IDF, make sure that the references to the functions and header files correspond to this version.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • #17 20882042
    khoam
    Level 42  
    ElektrodaBot wrote:
    The following is an example of the use of the function `esp_clk_tree_src_get_freq_hz()` in C

    This is an incorrect use of this function. This function is declared as follows:
    Code: C / C++
    Log in, to see the code

    It is available in the header file esp_clk_tree.h
  • #18 20882059
    ElektrodaBot
    AI OpenAI ChatGPT4
    khoam wrote:
    This is an incorrect use of this function. This function is declared as follows:
    esp_err_t esp_clk_tree_src_get_freq_hz(soc_module_clk_t clk_src, esp_clk_tree_src_freq_precision_t precision, uint32_t *freq_value)
    It is available in the header file esp_clk_tree.h
    Thanks for the clarification. From this it appears that the function `esp_clk_tree_src_get_freq_hz` returns a value via the output argument and itself returns an error code of type `esp_err_t` to check if the operation succeeded. The header file you should include is `esp_clk_tree.h`. Here is an example of how the aforementioned function can be used:


    Code: C / C++
    Log in, to see the code


    In the above example, the `esp_clk_tree_src_get_freq_hz` function is called with the `CLK_TREE_SRC_FREQ_PRECISION_HIGH` flag, which indicates that a high precision measurement is desired. The `CLK_TREE_CPU` parameter is used as an identifier of the CPU clock source. Should an error occur, the function logs an error message using `esp_err_to_name`, which converts the ESP-IDF error code to a character string representing the error name.

    Note that before running this code, make sure that the constants `CLK_TREE_CPU` and `CLK_TREE_SRC_FREQ_PRECISION_HIGH` are correctly defined in your version of ESP-IDF.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • ADVERTISEMENT
  • #19 20882077
    khoam
    Level 42  
    ElektrodaBot wrote:
    The `CLK_TREE_CPU` parameter is used as an identifier of the CPU clock source.

    There is no such parameter but there is SOC_MOD_CLK_CPU.

    ElektrodaBot wrote:
    is called with flag `CLK_TREE_SRC_FREQ_PRECISION_HIGH`

    There is no such parameter. There are instead: ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, ESP_CLK_TREE_SRC_FREQ_PRECISION_APPROX, ESP_CLK_TREE_SRC_FREQ_PRECISION_EXACT and ESP_CLK_TREE_SRC_FREQ_PRECISION_INVALID.
  • #20 20882089
    ElektrodaBot
    AI OpenAI ChatGPT4
    khoam wrote:
    There is no such parameter but there is SOC_MOD_CLK_CPU.


    There is no such parameter. There are instead: ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, ESP_CLK_TREE_SRC_FREQ_PRECISION_APPROX, ESP_CLK_TREE_SRC_FREQ_PRECISION_EXACT and ESP_CLK_TREE_SRC_FREQ_PRECISION_INVALID.
    Excuse me for previous misunderstandings. Now that we have all the information we need, we can move on to the correct use of the `esp_clk_tree_src_get_freq_hz()` function in the context of an inter-process function call (IPC) on the ESP32 core. Below is a revised version of the `ipc_get_core_freq` function which uses the correct `SOC_MOD_CLK_CPU` and `ESP_CLK_TREE_SRC_FREQ_PRECISION_EXACT` constants:

    Code: C / C++
    Log in, to see the code


    In this case we use `SOC_MOD_CLK_CPU` as the clock source identifier for the CPU, and `ESP_CLK_TREE_SRC_FREQ_PRECISION_EXACT` as the precision flag to get the exact frequency value. The `esp_clk_tree_src_get_freq_hz` function returns an error code, which we can check to ensure that the frequency reading succeeded.

    Make sure that you are using the correct version of ESP-IDF, which includes the definitions listed. If you are still having problems compiling, check the ESP-IDF documentation on handling system clocks and update your development environment to the latest version if necessary.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • #21 20882108
    khoam
    Level 42  
    ElektrodaBot wrote:
    Below is a corrected version of the `ipc_get_core_freq` function which uses the correct `SOC_MOD_CLK_CPU` and `ESP_CLK_TREE_SRC_FREQ_PRECISION_EXACT` constants

    This is the last question. Will this version of the code also work correctly for ESP32-S3 ?
  • #22 20882113
    ElektrodaBot
    AI OpenAI ChatGPT4
    khoam wrote:
    This last question. Will this version of the code also work correctly for ESP32-S3 ?
    Excuse the error from the previous answer. Let's now focus on feature compatibility with the ESP32-S3. The ESP32-S3 is another chip in the ESP32 family and in many cases code written for ESP32 will be compatible with ESP32-S3. However, there may be hardware differences that require attention.

    The `esp_clk_tree_src_get_freq_hz()` function is part of the ESP-IDF API and should be compatible with a variety of ESP32 series chips, including the ESP32-S3, provided that the system clock API has not changed significantly from the version of ESP-IDF you are using for the ESP32-S3.

    The code I provided earlier should work for ESP32-S3, provided that:

    1. The definitions and clock APIs used are available and compatible with the version of ESP-IDF that supports ESP32-S3.
    2. There have been no significant changes to the clock architecture in ESP32-S3 that would affect the clock API.

    To ensure that the code will work correctly, I recommend checking the ESP-IDF documentation for ESP32-S3 and testing the code directly on the target hardware. If any inconsistencies or complications arise, the ESP-IDF documentation should include information on possible differences in clock implementation for different ESP32 chip models.

    Also remember to always use the version of ESP-IDF that is officially supported for your ESP32-S3 microcontroller model, as different versions of ESP-IDF may make changes to the API and be better suited for specific SoC (System on Chip) models.

    If you have any doubts or questions about how the code works on ESP32-S3, feel free to write. I will be happy to help solve any problem or answer any additional questions.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • #23 20882119
    khoam
    Level 42  
    The latest version of the code is reasonably OK, but certainly not the work of a bot. Maybe it will be of use to someone.

Topic summary

The discussion focuses on creating a C function to retrieve the operating frequency of a selected core in the ESP32 using the ESP-IDF framework. The initial response suggests using the `esp_clk_cpu_freq()` function, which returns the CPU frequency in Hz. However, the user requests a function that accepts a core number as an argument. It is clarified that both cores typically operate at the same frequency, and the SDK does not provide a direct API for individual core frequencies. The conversation evolves to include the use of `esp_ipc_call()` and `esp_ipc_call_blocking()` for inter-core communication, with examples provided for both C and C++20 implementations. The final code examples utilize `esp_clk_tree_src_get_freq_hz()` to obtain the frequency, with discussions on header file requirements and compatibility with the ESP32-S3 model.
Summary generated by the language model.
ADVERTISEMENT