@@ -1092,35 +1092,24 @@ int rg_system_get_log_level(void)
10921092
10931093void rg_system_set_overclock (int level )
10941094{
1095- #if defined(ESP_PLATFORM ) && CONFIG_IDF_TARGET_ESP32
1096- // None of this is documented by espressif but can be found in the file rtc_clk.c
1095+ #if CONFIG_IDF_TARGET_ESP32
1096+ // #include "driver/uart.h"
1097+ // None of this is documented by espressif but there are comments to be found in the file `rtc_clk.c`
10971098 #define I2C_BBPLL 0x66
1098- #define I2C_BBPLL_ENDIV5 11
1099- #define I2C_BBPLL_BBADC_DSMP 9
11001099 #define I2C_BBPLL_HOSTID 4
1101- #define I2C_BBPLL_OC_LREF 2
1102- #define I2C_BBPLL_OC_DIV_7_0 3
1103- #define I2C_BBPLL_OC_DCUR 5
1104- #define BBPLL_ENDIV5_VAL_320M 0x43
1105- #define BBPLL_BBADC_DSMP_VAL_320M 0x84
1100+ #define I2C_BBPLL_ENDIV5 11 // This controls the BBPLL frequency. It should already be at 480Mhz
1101+ #define I2C_BBPLL_BBADC_DSMP 9 // This controls the BBPLL frequency. It should already be at 480Mhz
1102+ #define I2C_BBPLL_OC_LREF 2 // This is specific to the installed crystal (24/26/40), we don't care
1103+ #define I2C_BBPLL_OC_DIV_7_0 3 // This is the PLL divider to get the CPU clock (our main concern)
1104+ #define I2C_BBPLL_OC_DCUR 5 // This is specific to the installed crystal (24/26/40), we don't care
11061105 #define BBPLL_ENDIV5_VAL_480M 0xc3
11071106 #define BBPLL_BBADC_DSMP_VAL_480M 0x74
11081107 extern void rom_i2c_writeReg (uint8_t block , uint8_t host_id , uint8_t reg_add , uint8_t data );
11091108 extern uint8_t rom_i2c_readReg (uint8_t block , uint8_t host_id , uint8_t reg_add );
1109+ extern int uart_set_baudrate (int uart_num , uint32_t baud_rate );
11101110
1111- uint8_t div_ref = 0 ;
1112- uint8_t div7_0 = (level + 4 ) * 8 ;
1113- uint8_t div10_8 = 0 ;
1114- uint8_t lref = 0 ;
1115- uint8_t dcur = 6 ;
1116- uint8_t bw = 3 ;
1117- uint8_t ENDIV5 = BBPLL_ENDIV5_VAL_480M ;
1118- uint8_t BBADC_DSMP = BBPLL_BBADC_DSMP_VAL_480M ;
1119- uint8_t BBADC_OC_LREF = (lref << 7 ) | (div10_8 << 4 ) | (div_ref );
1120- uint8_t BBADC_OC_DIV_7_0 = div7_0 ;
1121- uint8_t BBADC_OC_DCUR = (bw << 6 ) | dcur ;
1122-
1123- static uint8_t BASE_ENDIV5 , BASE_BBADC_DSMP , BASE_BBADC_OC_LREF , BASE_BBADC_OC_DIV_7_0 , BASE_BBADC_OC_DCUR , BASE_SAVED ;
1111+ static uint8_t BASE_ENDIV5 , BASE_BBADC_DSMP , BASE_BBADC_OC_LREF , BASE_BBADC_OC_DIV_7_0 , BASE_BBADC_OC_DCUR ;
1112+ static bool BASE_SAVED = false;
11241113 if (!BASE_SAVED )
11251114 {
11261115 BASE_ENDIV5 = rom_i2c_readReg (I2C_BBPLL , I2C_BBPLL_HOSTID , I2C_BBPLL_ENDIV5 );
@@ -1131,48 +1120,56 @@ void rg_system_set_overclock(int level)
11311120 BASE_SAVED = true;
11321121 }
11331122
1134- if (level == 0 )
1135- {
1136- ENDIV5 = BASE_ENDIV5 ;
1137- BBADC_DSMP = BASE_BBADC_DSMP ;
1138- BBADC_OC_LREF = BASE_BBADC_OC_LREF ;
1139- BBADC_OC_DIV_7_0 = BASE_BBADC_OC_DIV_7_0 ;
1140- BBADC_OC_DCUR = BASE_BBADC_OC_DCUR ;
1141- }
1142- else if (level < -4 || level > 3 )
1123+ uint8_t ENDIV5 = BASE_ENDIV5 ;
1124+ uint8_t BBADC_DSMP = BASE_BBADC_DSMP ;
1125+ uint8_t BBADC_OC_LREF = BASE_BBADC_OC_LREF ;
1126+ uint8_t BBADC_OC_DIV_7_0 = BASE_BBADC_OC_DIV_7_0 ;
1127+ uint8_t BBADC_OC_DCUR = BASE_BBADC_OC_DCUR ;
1128+
1129+ if (level < -4 || level > 3 )
11431130 {
11441131 RG_LOGW ("Invalid level %d, min:-4 max:3" , level );
11451132 return ;
11461133 }
1134+ else if (level != 0 )
1135+ {
1136+ uint8_t div_ref = 0 ;
1137+ uint8_t div7_0 = (level + 4 ) * 8 ;
1138+ uint8_t div10_8 = 0 ;
1139+ uint8_t lref = 0 ;
1140+ uint8_t dcur = 6 ;
1141+ uint8_t bw = 3 ;
1142+ ENDIV5 = BBPLL_ENDIV5_VAL_480M ;
1143+ BBADC_DSMP = BBPLL_BBADC_DSMP_VAL_480M ;
1144+ BBADC_OC_LREF = (lref << 7 ) | (div10_8 << 4 ) | (div_ref );
1145+ BBADC_OC_DIV_7_0 = div7_0 ;
1146+ BBADC_OC_DCUR = (bw << 6 ) | dcur ;
1147+ }
11471148
11481149 RG_LOGW (" " );
11491150 RG_LOGW ("BASE: %d %d %d %d %d" , BASE_ENDIV5 , BASE_BBADC_DSMP , BASE_BBADC_OC_LREF , BASE_BBADC_OC_DIV_7_0 , BASE_BBADC_OC_DCUR );
11501151 RG_LOGW ("NEW : %d %d %d %d %d" , ENDIV5 , BBADC_DSMP , BBADC_OC_LREF , BBADC_OC_DIV_7_0 , BBADC_OC_DCUR );
11511152 RG_LOGW (" " );
11521153
1154+ RG_LOGW ("Preparing peripherals for the speed change..." );
1155+ rg_task_delay (10 ); // Wait for the log to be sent
1156+
1157+ float overclock_ratio = (240 + (level * 40 )) / 240.f ;
1158+ rg_audio_set_sample_rate (app .sampleRate / overclock_ratio );
1159+ uart_set_baudrate (0 , 115200 / overclock_ratio );
1160+
1161+ RG_LOGW ("Updating clock registers!" );
11531162 rom_i2c_writeReg (I2C_BBPLL , I2C_BBPLL_HOSTID , I2C_BBPLL_ENDIV5 , ENDIV5 );
11541163 rom_i2c_writeReg (I2C_BBPLL , I2C_BBPLL_HOSTID , I2C_BBPLL_BBADC_DSMP , BBADC_DSMP );
1155- rom_i2c_writeReg (I2C_BBPLL , I2C_BBPLL_HOSTID , I2C_BBPLL_OC_LREF , BBADC_OC_LREF );
1164+ // rom_i2c_writeReg(I2C_BBPLL, I2C_BBPLL_HOSTID, I2C_BBPLL_OC_LREF, BBADC_OC_LREF);
11561165 rom_i2c_writeReg (I2C_BBPLL , I2C_BBPLL_HOSTID , I2C_BBPLL_OC_DIV_7_0 , BBADC_OC_DIV_7_0 );
1157- rom_i2c_writeReg (I2C_BBPLL , I2C_BBPLL_HOSTID , I2C_BBPLL_OC_DCUR , BBADC_OC_DCUR );
1158-
1166+ // rom_i2c_writeReg(I2C_BBPLL, I2C_BBPLL_HOSTID, I2C_BBPLL_OC_DCUR, BBADC_OC_DCUR);
11591167 RG_LOGW ("Overclock applied!" );
1160- #if 0
1161- extern uint64_t esp_rtc_get_time_us (void );
1162- uint64_t start = esp_rtc_get_time_us ();
1163- int64_t end = rg_system_timer () + 1000000 ;
1164- while (rg_system_timer () < end )
1165- continue ;
1166- overclock_ratio = 1000000.f / (esp_rtc_get_time_us () - start );
1167- #endif
1168- // overclock_ratio = (240 + (app.overclock * 40)) / 240.f;
11691168
1170- // rg_audio_set_sample_rate( app.sampleRate / overclock_ratio) ;
1169+ app .overclock = level ;
11711170#else
11721171 RG_LOGE ("Overclock not supported on this platform!" );
11731172#endif
1174-
1175- app .overclock = level ;
11761173}
11771174
11781175int rg_system_get_overclock (void )
0 commit comments