Skip to content

Instantly share code, notes, and snippets.

@nophead
Last active April 24, 2018 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nophead/d2bee0b721845a5ed905669f598b6007 to your computer and use it in GitHub Desktop.
Save nophead/d2bee0b721845a5ed905669f598b6007 to your computer and use it in GitHub Desktop.
ESP8266 SPI spy sync pin interrupt
int voltint = 0, currentint = 0, wattint = 0; // raw values
void ICACHE_RAM_ATTR sync_isr() { // Sync pin interrupt on falling edge
SPI1S |= SPISSRES; // Reset HSPI slave
SPI1S &= ~SPISSRES;
SPI1CMD = SPICMDUSR; // Start HSPI slave
static bool glitch = false;
if(data_ready) { // If a data has been received by the HSPI
data_ready = false;
int volts = (data[0] << 16) + (data[1] << 8) + data[2]; // assemble 24 bits
if(volts > 5 * voltint / 8 || glitch) { // reject samples that have been shifted right by noise on the clock line?
glitch = false; // sample accepted
voltint = volts;
currentint = (data[4] << 16) + (data[5] << 8) + data[6];
wattint = (data[8] << 16) + (data[9] << 8) + data[10];
if(wattint > 0xFF0000) // sometimes goes slightly negative with no load
wattint = 0;
++samples;
new_readings = true; // new raw readings ready
}
else
glitch = true; // only reject one sample
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment