cpu_freq.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdint.h>
  14. #include "esp_attr.h"
  15. #include "rom/ets_sys.h"
  16. #include "rom/uart.h"
  17. #include "sdkconfig.h"
  18. #include "soc/soc.h"
  19. #include "soc/rtc.h"
  20. #include "soc/rtc_cntl_reg.h"
  21. /*
  22. * This function is not exposed as an API at this point,
  23. * because FreeRTOS doesn't yet support dynamic changing of
  24. * CPU frequency. Also we need to implement hooks for
  25. * components which want to be notified of CPU frequency
  26. * changes.
  27. */
  28. void esp_set_cpu_freq(void)
  29. {
  30. uint32_t freq_mhz = CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ;
  31. rtc_cpu_freq_t freq = RTC_CPU_FREQ_80M;
  32. switch(freq_mhz) {
  33. case 240:
  34. freq = RTC_CPU_FREQ_240M;
  35. break;
  36. case 160:
  37. freq = RTC_CPU_FREQ_160M;
  38. break;
  39. default:
  40. freq_mhz = 80;
  41. /* no break */
  42. case 80:
  43. freq = RTC_CPU_FREQ_80M;
  44. break;
  45. }
  46. // Wait for UART TX to finish, otherwise some UART output will be lost
  47. // when switching APB frequency
  48. uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM);
  49. rtc_config_t cfg = RTC_CONFIG_DEFAULT();
  50. rtc_init(cfg);
  51. rtc_clk_cpu_freq_set(freq);
  52. #if ESP32_RTC_CLOCK_SOURCE_EXTERNAL_CRYSTAL
  53. rtc_clk_slow_freq_set(RTC_SLOW_FREQ_32K_XTAL);
  54. #endif
  55. }
  56. void IRAM_ATTR ets_update_cpu_frequency(uint32_t ticks_per_us)
  57. {
  58. extern uint32_t g_ticks_per_us_pro; // g_ticks_us defined in ROM for PRO CPU
  59. extern uint32_t g_ticks_per_us_app; // same defined for APP CPU
  60. g_ticks_per_us_pro = ticks_per_us;
  61. g_ticks_per_us_app = ticks_per_us;
  62. }