From 0d796d6eec09796f0ffaba9915506656e917e3e7 Mon Sep 17 00:00:00 2001
From: Schorcht <gunar@schorcht.net>
Date: Fri, 19 Oct 2018 13:08:32 +0200
Subject: [PATCH] cpu/esp8266: cpu frequency settings

---
 cpu/esp8266/include/user_config.h | 16 +++++++++++++++-
 cpu/esp8266/sdk/main.c            | 13 +++++++++++++
 cpu/esp8266/sdk/main.h            |  2 +-
 cpu/esp8266/startup.c             |  9 +++++----
 4 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/cpu/esp8266/include/user_config.h b/cpu/esp8266/include/user_config.h
index 4a53d4e0f0..94825891a0 100644
--- a/cpu/esp8266/include/user_config.h
+++ b/cpu/esp8266/include/user_config.h
@@ -5,6 +5,13 @@
  * General Public License v2.1. See the file LICENSE in the top level
  * directory for more details.
  */
+/**
+ * @ingroup     cpu_esp8266
+ * @brief       Default configurations required by the SDK
+ * @author      Gunar Schorcht <gunar@schorcht.net>
+ * @file
+ * @{
+ */
 
 #ifndef USER_CONFIG_H
 #define USER_CONFIG_H
@@ -13,10 +20,17 @@
 extern "C" {
 #endif
 
-/* This file is just to satisfy SDK */
+/**
+ * @brief   Default CPU frequency in MHz.
+ * Possible values are 80 and 160.
+ */
+#ifndef ESP8266_CPU_FREQUENCY
+#define ESP8266_CPU_FREQUENCY   80
+#endif
 
 #ifdef __cplusplus
 }
 #endif
 
 #endif /* USER_CONFIG_H */
+/** @} */
diff --git a/cpu/esp8266/sdk/main.c b/cpu/esp8266/sdk/main.c
index 8db72d07bb..f88a815009 100644
--- a/cpu/esp8266/sdk/main.c
+++ b/cpu/esp8266/sdk/main.c
@@ -129,6 +129,19 @@ void system_restart(void)
     __asm__ volatile (" call0 0x40000080 ");
 }
 
+extern bool system_update_cpu_freq(uint8 freq)
+{
+    if (freq == 160) {
+        DPORT.CPU_CLOCK |= DPORT_CPU_CLOCK_X2;
+        ets_update_cpu_frequency(160);
+    }
+    else {
+        DPORT.CPU_CLOCK &= ~DPORT_CPU_CLOCK_X2;
+        ets_update_cpu_frequency(80);
+    }
+    return true;
+}
+
 /**
  * Following code is completly or at least partially from
  * https://github.com/pvvx/esp8266web
diff --git a/cpu/esp8266/sdk/main.h b/cpu/esp8266/sdk/main.h
index e87927e527..c946101c31 100644
--- a/cpu/esp8266/sdk/main.h
+++ b/cpu/esp8266/sdk/main.h
@@ -42,13 +42,13 @@ extern "C" {
 #define system_get_time       phy_get_mactime
 #define system_get_chip_id()  (((DPORT.OTP_MAC1 & 0xffff) << 8) + ((DPORT.OTP_MAC0 >> 24) & 0xff))
 #define system_get_cpu_freq   ets_get_cpu_frequency
-/* TODO #define system_update_cpu_freq  ets_update_cpu_frequency */
 
 extern int      os_printf_plus (const char* format, ...);
 
 extern void     system_deep_sleep (uint32_t time_in_us);
 extern uint8_t  system_get_checksum(uint8_t *ptr, uint32_t len);
 extern void     system_restart (void);
+extern bool     system_update_cpu_freq(uint8 freq);
 
 extern bool     system_rtc_mem_read(uint32_t src_addr, void *des_addr, uint32_t save_size);
 extern bool     system_rtc_mem_write(uint32_t src_addr, void *des_addr, uint32_t save_size);
diff --git a/cpu/esp8266/startup.c b/cpu/esp8266/startup.c
index f37ad180d5..d132d0bb80 100644
--- a/cpu/esp8266/startup.c
+++ b/cpu/esp8266/startup.c
@@ -170,8 +170,8 @@ void IRAM user_init (void)
     syscalls_init ();
     thread_isr_stack_init ();
 
-    /* run system in high performance mode */
-    system_update_cpu_freq(160);
+    /* set system frequency */
+    system_update_cpu_freq(ESP8266_CPU_FREQUENCY);
 
     /* reinit system timer as microsecond timer */
     system_timer_reinit ();
@@ -191,6 +191,7 @@ void IRAM user_init (void)
 #include "esp/dport_regs.h"
 #include "esp/phy_info.h"
 #include "esp/spiflash.h"
+#include "user_config.h"
 
 /**
  * @brief   Defines the structure of the file header in SPI flash
@@ -661,8 +662,8 @@ void __attribute__((noreturn)) IRAM cpu_user_start (void)
 
     /** }@ */
 
-    /* run system in high performance mode */
-    /* TODO system_update_cpu_freq(160); */
+    /* set system frequency */
+    system_update_cpu_freq(ESP8266_CPU_FREQUENCY);
 
     /* PHASE 3: start RIOT-OS kernel */
 
-- 
GitLab