|
| 1 | +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | + |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "sdkconfig.h" |
| 16 | +#include "freertos/FreeRTOS.h" |
| 17 | +#include "freertos/semphr.h" |
| 18 | +#include "freertos/task.h" |
| 19 | +#include "freertos/xtensa_timer.h" |
| 20 | +#include "esp_attr.h" |
| 21 | +#include "esp_log.h" |
| 22 | +#include "soc/rtc.h" |
| 23 | +#include "soc/rtc_cntl_reg.h" |
| 24 | +#include "rom/rtc.h" |
| 25 | +#include "soc/apb_ctrl_reg.h" |
| 26 | +#include "esp32-hal.h" |
| 27 | +#include "esp32-hal-cpu.h" |
| 28 | + |
| 29 | +typedef struct apb_change_cb_s { |
| 30 | + struct apb_change_cb_s * next; |
| 31 | + void * arg; |
| 32 | + apb_change_cb_t cb; |
| 33 | +} apb_change_t; |
| 34 | + |
| 35 | +const uint32_t MHZ = 1000000; |
| 36 | + |
| 37 | +static apb_change_t * apb_change_callbacks = NULL; |
| 38 | +static xSemaphoreHandle apb_change_lock = NULL; |
| 39 | + |
| 40 | +static void initApbChangeCallback(){ |
| 41 | + static volatile bool initialized = false; |
| 42 | + if(!initialized){ |
| 43 | + initialized = true; |
| 44 | + apb_change_lock = xSemaphoreCreateMutex(); |
| 45 | + if(!apb_change_lock){ |
| 46 | + initialized = false; |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +static void triggerApbChangeCallback(apb_change_ev_t ev_type, uint32_t old_apb, uint32_t new_apb){ |
| 52 | + initApbChangeCallback(); |
| 53 | + xSemaphoreTake(apb_change_lock, portMAX_DELAY); |
| 54 | + apb_change_t * r = apb_change_callbacks; |
| 55 | + while(r != NULL){ |
| 56 | + r->cb(r->arg, ev_type, old_apb, new_apb); |
| 57 | + r=r->next; |
| 58 | + } |
| 59 | + xSemaphoreGive(apb_change_lock); |
| 60 | +} |
| 61 | + |
| 62 | +bool addApbChangeCallback(void * arg, apb_change_cb_t cb){ |
| 63 | + initApbChangeCallback(); |
| 64 | + apb_change_t * c = (apb_change_t*)malloc(sizeof(apb_change_t)); |
| 65 | + if(!c){ |
| 66 | + log_e("Callback Object Malloc Failed"); |
| 67 | + return false; |
| 68 | + } |
| 69 | + c->next = NULL; |
| 70 | + c->arg = arg; |
| 71 | + c->cb = cb; |
| 72 | + xSemaphoreTake(apb_change_lock, portMAX_DELAY); |
| 73 | + if(apb_change_callbacks == NULL){ |
| 74 | + apb_change_callbacks = c; |
| 75 | + } else { |
| 76 | + apb_change_t * r = apb_change_callbacks; |
| 77 | + if(r->cb != cb || r->arg != arg){ |
| 78 | + while(r->next){ |
| 79 | + r = r->next; |
| 80 | + if(r->cb == cb && r->arg == arg){ |
| 81 | + free(c); |
| 82 | + goto unlock_and_exit; |
| 83 | + } |
| 84 | + } |
| 85 | + r->next = c; |
| 86 | + } |
| 87 | + } |
| 88 | +unlock_and_exit: |
| 89 | + xSemaphoreGive(apb_change_lock); |
| 90 | + return true; |
| 91 | +} |
| 92 | + |
| 93 | +bool removeApbChangeCallback(void * arg, apb_change_cb_t cb){ |
| 94 | + initApbChangeCallback(); |
| 95 | + xSemaphoreTake(apb_change_lock, portMAX_DELAY); |
| 96 | + apb_change_t * r = apb_change_callbacks; |
| 97 | + if(r == NULL){ |
| 98 | + xSemaphoreGive(apb_change_lock); |
| 99 | + return false; |
| 100 | + } |
| 101 | + if(r->cb == cb && r->arg == arg){ |
| 102 | + apb_change_callbacks = r->next; |
| 103 | + free(r); |
| 104 | + } else { |
| 105 | + while(r->next && (r->next->cb != cb || r->next->arg != arg)){ |
| 106 | + r = r->next; |
| 107 | + } |
| 108 | + if(r->next == NULL || r->next->cb != cb || r->next->arg != arg){ |
| 109 | + xSemaphoreGive(apb_change_lock); |
| 110 | + return false; |
| 111 | + } |
| 112 | + apb_change_t * c = r->next; |
| 113 | + r->next = c->next; |
| 114 | + free(c); |
| 115 | + } |
| 116 | + xSemaphoreGive(apb_change_lock); |
| 117 | + return true; |
| 118 | +} |
| 119 | + |
| 120 | +static uint32_t calculateApb(rtc_cpu_freq_config_t * conf){ |
| 121 | + if(conf->freq_mhz >= 80){ |
| 122 | + return 80 * MHZ; |
| 123 | + } |
| 124 | + return (conf->source_freq_mhz * MHZ) / conf->div; |
| 125 | +} |
| 126 | + |
| 127 | +void esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us); //private in IDF |
| 128 | + |
| 129 | +bool setCpuFrequencyMhz(uint32_t cpu_freq_mhz){ |
| 130 | + rtc_cpu_freq_config_t conf, cconf; |
| 131 | + uint32_t capb, apb; |
| 132 | + //Get XTAL Frequency and calculate min CPU MHz |
| 133 | + rtc_xtal_freq_t xtal = rtc_clk_xtal_freq_get(); |
| 134 | + uint32_t min_cpu_mhz = 10; |
| 135 | + if(xtal > RTC_XTAL_FREQ_AUTO){ |
| 136 | + if(xtal < RTC_XTAL_FREQ_40M) { |
| 137 | + min_cpu_mhz = xtal / 2; //13Mhz for 26Mhz XTAL |
| 138 | + if(cpu_freq_mhz <= xtal && cpu_freq_mhz != xtal && cpu_freq_mhz != (xtal/2)){ |
| 139 | + log_e("Bad frequency: %u MHz! Options are: 240, 160, 80, %u and %u MHz", cpu_freq_mhz, xtal, xtal/2); |
| 140 | + return false; |
| 141 | + } |
| 142 | + } else if(cpu_freq_mhz <= xtal && cpu_freq_mhz != xtal && cpu_freq_mhz != (xtal/2) && cpu_freq_mhz != (xtal/4)){ |
| 143 | + log_e("Bad frequency: %u MHz! Options are: 240, 160, 80, %u, %u and %u MHz", cpu_freq_mhz, xtal, xtal/2, xtal/4); |
| 144 | + return false; |
| 145 | + } |
| 146 | + } |
| 147 | + if(cpu_freq_mhz > xtal && cpu_freq_mhz != 240 && cpu_freq_mhz != 160 && cpu_freq_mhz != 80){ |
| 148 | + if(xtal >= RTC_XTAL_FREQ_40M){ |
| 149 | + log_e("Bad frequency: %u MHz! Options are: 240, 160, 80, %u, %u and %u MHz", cpu_freq_mhz, xtal, xtal/2, xtal/4); |
| 150 | + } else { |
| 151 | + log_e("Bad frequency: %u MHz! Options are: 240, 160, 80, %u and %u MHz", cpu_freq_mhz, xtal, xtal/2); |
| 152 | + } |
| 153 | + return false; |
| 154 | + } |
| 155 | + //Get current CPU clock configuration |
| 156 | + rtc_clk_cpu_freq_get_config(&cconf); |
| 157 | + //return if frequency has not changed |
| 158 | + if(cconf.freq_mhz == cpu_freq_mhz){ |
| 159 | + return true; |
| 160 | + } |
| 161 | + //Get configuration for the new CPU frequency |
| 162 | + if(!rtc_clk_cpu_freq_mhz_to_config(cpu_freq_mhz, &conf)){ |
| 163 | + log_e("CPU clock could not be set to %u MHz", cpu_freq_mhz); |
| 164 | + return false; |
| 165 | + } |
| 166 | + //Current APB |
| 167 | + capb = calculateApb(&cconf); |
| 168 | + //New APB |
| 169 | + apb = calculateApb(&conf); |
| 170 | + log_d("%s: %u / %u = %u Mhz, APB: %u Hz", (conf.source == RTC_CPU_FREQ_SRC_PLL)?"PLL":((conf.source == RTC_CPU_FREQ_SRC_APLL)?"APLL":((conf.source == RTC_CPU_FREQ_SRC_XTAL)?"XTAL":"8M")), conf.source_freq_mhz, conf.div, conf.freq_mhz, apb); |
| 171 | + //Call peripheral functions before the APB change |
| 172 | + if(apb_change_callbacks){ |
| 173 | + triggerApbChangeCallback(APB_BEFORE_CHANGE, capb, apb); |
| 174 | + } |
| 175 | + //Make the frequency change |
| 176 | + rtc_clk_cpu_freq_set_config_fast(&conf); |
| 177 | + if(capb != apb){ |
| 178 | + //Update REF_TICK (uncomment if REF_TICK is different than 1MHz) |
| 179 | + //if(conf.freq_mhz < 80){ |
| 180 | + // ESP_REG(APB_CTRL_XTAL_TICK_CONF_REG) = conf.freq_mhz / (REF_CLK_FREQ / MHZ) - 1; |
| 181 | + //} |
| 182 | + //Update APB Freq REG |
| 183 | + rtc_clk_apb_freq_update(apb); |
| 184 | + //Update esp_timer divisor |
| 185 | + esp_timer_impl_update_apb_freq(apb / MHZ); |
| 186 | + } |
| 187 | + //Update FreeRTOS Tick Divisor |
| 188 | + uint32_t fcpu = (conf.freq_mhz >= 80)?(conf.freq_mhz * MHZ):(apb); |
| 189 | + _xt_tick_divisor = fcpu / XT_TICK_PER_SEC; |
| 190 | + //Call peripheral functions after the APB change |
| 191 | + if(apb_change_callbacks){ |
| 192 | + triggerApbChangeCallback(APB_AFTER_CHANGE, capb, apb); |
| 193 | + } |
| 194 | + return true; |
| 195 | +} |
| 196 | + |
| 197 | +uint32_t getCpuFrequencyMhz(){ |
| 198 | + rtc_cpu_freq_config_t conf; |
| 199 | + rtc_clk_cpu_freq_get_config(&conf); |
| 200 | + return conf.freq_mhz; |
| 201 | +} |
| 202 | + |
| 203 | +uint32_t getXtalFrequencyMhz(){ |
| 204 | + return rtc_clk_xtal_freq_get(); |
| 205 | +} |
| 206 | + |
| 207 | +uint32_t getApbFrequency(){ |
| 208 | + rtc_cpu_freq_config_t conf; |
| 209 | + rtc_clk_cpu_freq_get_config(&conf); |
| 210 | + return calculateApb(&conf); |
| 211 | +} |
0 commit comments