Change CPU Frequency



  • Is it possible to change CPU frequency on Lopy4 from 160Mhz to 80MHz? I have tried to change
    #define CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ 160
    to
    #define CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ 80
    and
    #define CONFIG_ESP32_DEFAULT_CPU_FREQ_160 1
    to
    #define CONFIG_ESP32_DEFAULT_CPU_FREQ_80 1
    in sdkconfig.h file, but I still get 160Mhz when I call machine.freq().
    Am I leaving something behind?

    Thanks,

    JuanMa



  • @combaindeft Yes, but only for telling the frequency, and not setting it. If you look at the esp32 port of micropython.org, you can use that method for setting the frequecy, or at least it is intended to do so. It accepts an argument, but does not call the idf function. Code below:

    STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) {
        if (n_args == 0) {
            // get
            return mp_obj_new_int(ets_get_cpu_frequency() * 1000000);
        } else {
            // set
            mp_int_t freq = mp_obj_get_int(args[0]) / 1000000;
            if (freq != 80 && freq != 160 && freq != 240) {
                mp_raise_ValueError("frequency can only be either 80Mhz, 160MHz or 240MHz");
            }
            /*
            system_update_cpu_freq(freq);
            */
            return mp_const_none;
        }
    }
    STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 1, machine_freq);
    

    Edit: This code does not work.



  • @robert-hh IIRC, the LoPy's we have, do have the machine.freq() defined.



  • @combaindeft Other ports of micropython use machine.freq() for that purpose.



  • So, you have to compile a new firmware to lower the cpu-freq?

    I thought you could do it "on the fly" by calling a function.

    Could that be achived? Adding a fuction the the Machine Library?



  • @robert-hh @this-wiederkehr Thanks!! I will try right now :)





  • @juanma Sorry, I had to dig for an old post first. So that's what I would do:
    That is a little bit tricky, since you have to rebuild lib/libesp32.a too. For that, I did the following:
    a) go into any of the pycom-esp-idf/examples. I choose. examples/wifi/scan
    b) run make. It will lead you through configure. Just exit there, and compiles a build. The configuration does not matter, since you will take that from the pycom directory in step c
    c) take skdconfig.h from pycom's esp32 directory and put that into pycom-esp-idf/examples/wifi/scan/build/include
    d) modify it to your needs and save it. It has a new modification date then-
    e) run make again on that example
    f) now there is a libesp32.a in pycom-esp-idf/examples/wifi/scan/build/esp32 with the intended modification
    g) copy that lib file back to pycom's esp32/lib. Don't forget to save the old version.- You will need that for pycom's devices.
    Then make your firmware again.



  • @robert-hh Sorry for my ignorance, but how can I rebuildthe libs in the esp-idf in order to change the cpu frequency?



  • @robert-hh Maybe I am doing something wrong. How can I rebuild the libs in the esp-idf? Thanks :)



  • Thanks!
    I have not realized that I have to rebuild the libs. I was aware of the ESP32 bug, but I don´t want to change the frequency dynamically, only to work on a lower frequency to save power.



  • @juanma changes to sdkconfig.h have no effect until you rebuild the libs in the esp-idf. There was a bug in the ESP32 (or the esp-idf), that you cannot drop directly with machine.frequency to 80 MHz. As far as I recall, you have to go down to 40 MHz first and then go up again.


Log in to reply
 

Pycom on Twitter

Looks like your connection to Pycom Forum was lost, please wait while we try to reconnect.