Last active
April 24, 2018 20:36
ESP8266 SPI spy sync pin interrupt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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