main
John Lauer
Publish assumption-based R11 assembly study, locked firmware and new review footage
d61d6cb
#include "fan_tach.h"
#include <string.h>
void fan_tach_init(fan_tach *f){memset(f,0,sizeof(*f));}
void fan_tach_edge(fan_tach *f,uint32_t now){
if(!f->seen){f->seen=true;f->last_edge_us=now;return;}
uint32_t period=now-f->last_edge_us;f->last_edge_us=now;
/* 2 pulses/rev. Invalid timing restarts qualification, never becomes a healthy edge. */
if(period<2500 || period>60000){f->count=0;f->next=0;return;}
f->periods_us[f->next]=period;f->next=(f->next+1)%8;if(f->count<8)f->count++;
}
/* Telemetry accepts 500..12000 RPM at 2 pulses/revolution. This does not
* authorize loading at those speeds: load_core retains 5000..10000 RPM.
* Preserve the original 15 ms stale bound at qualified load speeds; allow
* slower idle telemetry up to two periods (120 ms maximum). */
static uint32_t stale_after_us(const fan_tach *f){
uint32_t longest=0,sum=0;
for(unsigned i=0;i<f->count;i++)sum+=f->periods_us[i];
/* Mixed-speed windows must not extend the load-qualified stale bound. */
if(f->count && sum<=6000u*f->count)return 15000;
for(unsigned i=0;i<f->count;i++)if(f->periods_us[i]>longest)longest=f->periods_us[i];
uint32_t timeout=longest*2;
return timeout>15000 ? timeout : 15000;
}
void fan_tach_expire(fan_tach *f,uint32_t now){
if(f->seen && now-f->last_edge_us>stale_after_us(f))fan_tach_init(f);
}
bool fan_tach_read(const fan_tach *f,uint32_t now,float *rpm){
*rpm=0;
if(!f->seen || f->count<8 || now-f->last_edge_us>stale_after_us(f))return false;
uint32_t sum=0;for(unsigned i=0;i<8;i++)sum+=f->periods_us[i];
*rpm=30000000.0f*8.0f/(float)sum;return true;
}