Left for archival purposes.

User avatar
By Fr4gg0r
#8112
Code: Select allstatic int node_set_cpu_freq( lua_State* L )
{
  uint32_t new_freq = luaL_checkinteger( L, 1 );
  if(new_freq == 160){
    REG_SET_BIT(0x3ff00014, BIT(0));
    os_update_cpu_frequency(160);
  }else{
    REG_CLR_BIT(0x3ff00014, BIT(0));
    os_update_cpu_frequency(80);
  }
  new_freq = ets_get_cpu_frequency();
  lua_pushinteger(L, new_freq);
  return 1;
}


This is supposed to increase cpu freq from 80 to 160. However, I do not see any differences in execution speed with the following benchmark:
Code: Select allfunction runOnce(freq)
sum = 0
node.set_cpu_freq(freq)
for runs=0,4 do

  now=tmr.now()
  a = 0
  b = 2
  c = 1
for ii=0,10 do
  for i=0,1000 do
    a = i+b
    c = bit.bxor(c, 1)
    tmr.wdclr()
    node.set_cpu_freq(freq)
  end
end

  nnow = tmr.now()
  timeDiff = (nnow-now)/1000
  sum = sum + timeDiff
 
  print("time needed: " .. timeDiff )
end
print("total time needed: " .. sum)
end


print("======================")
freq=node.setcpufreq(80)
print("Setting CPU FREQUENCY TO " .. freq .. "MHz")
runOnce(80)
print("======================")

print("======================")
freq=node.setcpufreq(160)
print("Setting CPU FREQUENCY TO " .. freq .. "MHz")
runOnce(160)
print("======================")


Maybe someone else has an idea? tmr.now() is supposed to return the system time in micro seconds. Maybe the system time runs faster at 160MHz?
Last edited by Fr4gg0r on Tue Feb 03, 2015 1:03 pm, edited 1 time in total.
User avatar
By raz123
#8825 1. Have you seen this post? viewtopic.php?p=8107#p8107 -- he claims that he has seen an improvement in speed. If you build it with 160 Mhz, especially if you implement it as part of a function, I'd love to test it :)

2. "SDK periodically disables 160MHz."

3. Unrelated, but are you sure that you shouldn't be clearing the bit, here?
Code: Select all}else{
    REG_SET_BIT(0x3ff00014, BIT(0));
User avatar
By Fr4gg0r
#8869 pvvx claims a lot ;)

However, thanks for the finding. Now it works. It is indeed a lot faster. Someone with a good multimeter should compare the power consumption. For most users 160Mhz as default seems to be adequate.
Code: Select all> dofile("benchmark.lua")
======================
Setting CPU FREQUENCY TO 80MHz
time needed: 2118
time needed: 2118
time needed: 2118
time needed: 2118
time needed: 2118
total time needed: 10590
======================
======================
Setting CPU FREQUENCY TO 160MHz
time needed: 1059
time needed: 1059
time needed: 1059
time needed: 1059
time needed: 1059
total time needed: 5295
======================


You can test it here: https://github.com/jrahlf/nodemcu-firmw ... master/bin
Someone else can make a pull request, I can't. xD