ADC Testing

This commit is contained in:
2026-04-17 09:13:05 +02:00
parent 1b7a531d54
commit 5aa5aaa07a
5 changed files with 530 additions and 530 deletions

View File

@@ -1,4 +1,4 @@
// ADS1256 cpp file //ADS1256 cpp file
/* /*
Name: ADS1256.cpp Name: ADS1256.cpp
Created: 2022/07/14 Created: 2022/07/14
@@ -15,367 +15,350 @@
#include "ADS1256.h" #include "ADS1256.h"
#include "SPI.h" #include "SPI.h"
#include "DebugLog.h"
#define convertSigned24BitToLong(value) ((value) & (1l << 23) ? (value) - 0x1000000 : value) #define convertSigned24BitToLong(value) ((value) & (1l << 23) ? (value) - 0x1000000 : value)
// Constructor //Constructor
ADS1256::ADS1256(const int8_t DRDY_pin, const int8_t RESET_pin, const int8_t SYNC_pin, const int8_t CS_pin, float VREF, SPIClass *spi) : _spi(spi), ADS1256::ADS1256(const int8_t DRDY_pin, const int8_t RESET_pin, const int8_t SYNC_pin, const int8_t CS_pin,float VREF, SPIClass* spi): _spi(spi),
_DRDY_pin(DRDY_pin), _RESET_pin(RESET_pin), _SYNC_pin(SYNC_pin), _CS_pin(CS_pin), _VREF(VREF), _PGA(0) _DRDY_pin(DRDY_pin), _RESET_pin(RESET_pin), _SYNC_pin(SYNC_pin), _CS_pin(CS_pin), _VREF(VREF), _PGA(0)
{ {
pinMode(_DRDY_pin, INPUT); pinMode(_DRDY_pin, INPUT);
if (RESET_pin != PIN_UNUSED) if(RESET_pin != PIN_UNUSED)
{ {
pinMode(_RESET_pin, OUTPUT); pinMode(_RESET_pin, OUTPUT);
} }
if (SYNC_pin != PIN_UNUSED) if(SYNC_pin != PIN_UNUSED)
{ {
pinMode(_SYNC_pin, OUTPUT); pinMode(_SYNC_pin, OUTPUT);
} }
if (CS_pin != PIN_UNUSED) if(CS_pin != PIN_UNUSED)
{ {
pinMode(_CS_pin, OUTPUT); pinMode(_CS_pin, OUTPUT);
} }
LOG_DEBUG("ADC Class Init OK");
updateConversionParameter(); updateConversionParameter();
} }
// Initialization //Initialization
void ADS1256::InitializeADC() void ADS1256::InitializeADC()
{ {
// Chip select LOW //Chip select LOW
CS_LOW(); CS_LOW();
// We do a manual chip reset on the ADS1256 - Datasheet Page 27/ RESET //We do a manual chip reset on the ADS1256 - Datasheet Page 27/ RESET
if (_RESET_pin != PIN_UNUSED) if(_RESET_pin != PIN_UNUSED)
{ {
digitalWrite(_RESET_pin, LOW); digitalWrite(_RESET_pin, LOW);
delay(200); delay(200);
digitalWrite(_RESET_pin, HIGH); // RESET is set to high digitalWrite(_RESET_pin, HIGH); //RESET is set to high
delay(1000); delay(1000);
} }
// Sync pin is also treated if it is defined //Sync pin is also treated if it is defined
if (_SYNC_pin != PIN_UNUSED) if(_SYNC_pin != PIN_UNUSED)
{ {
digitalWrite(_SYNC_pin, HIGH); // RESET is set to high digitalWrite(_SYNC_pin, HIGH); //RESET is set to high
} }
#ifndef ADS1256_SPI_ALREADY_STARTED // Guard macro to allow external initialization of the SPI #ifndef ADS1256_SPI_ALREADY_STARTED //Guard macro to allow external initialization of the SPI
//_spi->begin(); _spi->begin();
#endif #endif
// Applying arbitrary default values to speed up the starting procedure if the user just want to get quick readouts //Applying arbitrary default values to speed up the starting procedure if the user just want to get quick readouts
// We both pass values to the variables and then send those values to the corresponding registers //We both pass values to the variables and then send those values to the corresponding registers
delay(200); delay(200);
_STATUS = 0b00110110; // BUFEN and ACAL enabled, Order is MSB, rest is read only _STATUS = 0b00110110; //BUFEN and ACAL enabled, Order is MSB, rest is read only
writeRegister(STATUS_REG, _STATUS); writeRegister(STATUS_REG, _STATUS);
delay(200); delay(200);
_MUX = 0b00000001; // MUX AIN0+AIN1 _MUX = 0b00000001; //MUX AIN0+AIN1
writeRegister(MUX_REG, _MUX); writeRegister(MUX_REG, _MUX);
delay(200); delay(200);
_ADCON = 0b00000000; // ADCON - CLK: OFF, SDCS: OFF, PGA = 0 (+/- 5 V) _ADCON = 0b00000000; //ADCON - CLK: OFF, SDCS: OFF, PGA = 0 (+/- 5 V)
writeRegister(ADCON_REG, _ADCON); writeRegister(ADCON_REG, _ADCON);
delay(200); delay(200);
updateConversionParameter(); updateConversionParameter();
_DRATE = 0b10000010; // 100SPS _DRATE = 0b10000010; //100SPS
writeRegister(DRATE_REG, _DRATE); writeRegister(DRATE_REG, _DRATE);
delay(200); delay(200);
sendDirectCommand(0b11110000); // Offset and self-gain calibration sendDirectCommand(0b11110000); //Offset and self-gain calibration
delay(200); delay(200);
_isAcquisitionRunning = false; // MCU will be waiting to start a continuous acquisition _isAcquisitionRunning = false; //MCU will be waiting to start a continuous acquisition
} }
void ADS1256::waitForLowDRDY() void ADS1256::waitForLowDRDY()
{ {
while (digitalRead(_DRDY_pin) == HIGH) while (digitalRead(_DRDY_pin) == HIGH) {}
{
}
} }
void ADS1256::waitForHighDRDY() void ADS1256::waitForHighDRDY()
{ {
#if F_CPU >= 48000000 // Fast MCUs need this protection to wait until DRDY goes high after a conversion #if F_CPU >= 48000000 //Fast MCUs need this protection to wait until DRDY goes high after a conversion
while (digitalRead(_DRDY_pin) == LOW) while (digitalRead(_DRDY_pin) == LOW) {}
{
}
#endif #endif
} }
void ADS1256::stopConversion() // Sending SDATAC to stop the continuous conversion void ADS1256::stopConversion() //Sending SDATAC to stop the continuous conversion
{ {
waitForLowDRDY(); // SDATAC should be called after DRDY goes LOW (p35. Figure 33) waitForLowDRDY(); //SDATAC should be called after DRDY goes LOW (p35. Figure 33)
_spi->transfer(0b00001111); // Send SDATAC to the ADC _spi->transfer(0b00001111); //Send SDATAC to the ADC
CS_HIGH(); // We finished the command sequence, so we switch it back to HIGH CS_HIGH(); //We finished the command sequence, so we switch it back to HIGH
_spi->endTransaction(); _spi->endTransaction();
_isAcquisitionRunning = false; // Reset to false, so the MCU will be able to start a new conversion _isAcquisitionRunning = false; //Reset to false, so the MCU will be able to start a new conversion
} }
void ADS1256::setDRATE(uint8_t drate) // Setting DRATE (sampling frequency) void ADS1256::setDRATE(uint8_t drate) //Setting DRATE (sampling frequency)
{ {
writeRegister(DRATE_REG, drate); writeRegister(DRATE_REG, drate);
_DRATE = drate; _DRATE = drate;
delayMicroseconds(500); delay(200);
} }
void ADS1256::setMUX(uint8_t mux) // Setting MUX (input channel) void ADS1256::setMUX(uint8_t mux) //Setting MUX (input channel)
{ {
writeRegister(MUX_REG, mux); writeRegister(MUX_REG, mux);
_MUX = mux; _MUX = mux;
// delayMicroseconds(500); delay(200);
} }
void ADS1256::setPGA(uint8_t pga) // Setting PGA (input voltage range) void ADS1256::setPGA(uint8_t pga) //Setting PGA (input voltage range)
{ {
_PGA = pga; _PGA = pga;
_ADCON = readRegister(ADCON_REG); // Read the most recent value of the register _ADCON = readRegister(ADCON_REG); //Read the most recent value of the register
_ADCON = (_ADCON & 0b11111000) | (_PGA & 0b00000111); // Clearing and then setting bits 2-0 based on pga _ADCON = (_ADCON & 0b11111000) | (_PGA & 0b00000111); // Clearing and then setting bits 2-0 based on pga
writeRegister(ADCON_REG, _ADCON); writeRegister(ADCON_REG, _ADCON);
delayMicroseconds(1000); // Delay to allow the PGA to settle after changing its value delay(200);
updateConversionParameter(); // Update the multiplier according top the new PGA value updateConversionParameter(); //Update the multiplier according top the new PGA value
} }
uint8_t ADS1256::getPGA() // Reading PGA from the ADCON register uint8_t ADS1256::getPGA() //Reading PGA from the ADCON register
{ {
uint8_t pgaValue = readRegister(ADCON_REG) & 0b00000111; uint8_t pgaValue = readRegister(ADCON_REG) & 0b00000111;
// Reading the ADCON_REG and keeping the first three bits. //Reading the ADCON_REG and keeping the first three bits.
return (pgaValue); return(pgaValue);
} }
void ADS1256::setCLKOUT(uint8_t clkout) // Setting CLKOUT void ADS1256::setCLKOUT(uint8_t clkout) //Setting CLKOUT
{ {
_ADCON = readRegister(ADCON_REG); // Read the most recent value of the register _ADCON = readRegister(ADCON_REG); //Read the most recent value of the register
// Values: 0, 1, 2, 3 //Values: 0, 1, 2, 3
if (clkout == 0) if(clkout == 0)
{ {
// 00 //00
bitWrite(_ADCON, 6, 0); bitWrite(_ADCON, 6, 0);
bitWrite(_ADCON, 5, 0); bitWrite(_ADCON, 5, 0);
} }
else if (clkout == 1) else if(clkout == 1)
{ {
// 01 (default) //01 (default)
bitWrite(_ADCON, 6, 0); bitWrite(_ADCON, 6, 0);
bitWrite(_ADCON, 5, 1); bitWrite(_ADCON, 5, 1);
} }
else if (clkout == 2) else if(clkout == 2)
{ {
// 10 //10
bitWrite(_ADCON, 6, 1); bitWrite(_ADCON, 6, 1);
bitWrite(_ADCON, 5, 0); bitWrite(_ADCON, 5, 0);
} }
else if (clkout == 3) else if(clkout == 3)
{ {
// 11 //11
bitWrite(_ADCON, 6, 1); bitWrite(_ADCON, 6, 1);
bitWrite(_ADCON, 5, 1); bitWrite(_ADCON, 5, 1);
} }
else else{}
{
}
writeRegister(ADCON_REG, _ADCON); writeRegister(ADCON_REG, _ADCON);
delay(100); delay(100);
} }
void ADS1256::setSDCS(uint8_t sdcs) // Setting SDCS void ADS1256::setSDCS(uint8_t sdcs) //Setting SDCS
{ {
_ADCON = readRegister(ADCON_REG); // Read the most recent value of the register _ADCON = readRegister(ADCON_REG); //Read the most recent value of the register
// Values: 0, 1, 2, 3 //Values: 0, 1, 2, 3
if (sdcs == 0) if(sdcs == 0)
{ {
// 00 (default) //00 (default)
bitWrite(_ADCON, 4, 0); bitWrite(_ADCON, 4, 0);
bitWrite(_ADCON, 3, 0); bitWrite(_ADCON, 3, 0);
} }
else if (sdcs == 1) else if(sdcs == 1)
{ {
// 01 //01
bitWrite(_ADCON, 4, 0); bitWrite(_ADCON, 4, 0);
bitWrite(_ADCON, 3, 1); bitWrite(_ADCON, 3, 1);
} }
else if (sdcs == 2) else if(sdcs == 2)
{ {
// 10 //10
bitWrite(_ADCON, 4, 1); bitWrite(_ADCON, 4, 1);
bitWrite(_ADCON, 3, 0); bitWrite(_ADCON, 3, 0);
} }
else if (sdcs == 3) else if(sdcs == 3)
{ {
// 11 //11
bitWrite(_ADCON, 4, 1); bitWrite(_ADCON, 4, 1);
bitWrite(_ADCON, 3, 1); bitWrite(_ADCON, 3, 1);
} }
else else{}
{
}
writeRegister(ADCON_REG, _ADCON); writeRegister(ADCON_REG, _ADCON);
delay(100); delay(100);
} }
void ADS1256::setByteOrder(uint8_t byteOrder) // Setting byte order (MSB/LSB) void ADS1256::setByteOrder(uint8_t byteOrder) //Setting byte order (MSB/LSB)
{ {
_STATUS = readRegister(STATUS_REG); // Read the most recent value of the register _STATUS = readRegister(STATUS_REG); //Read the most recent value of the register
if (byteOrder == 0) if(byteOrder == 0)
{ {
// Byte order is MSB (default) //Byte order is MSB (default)
bitWrite(_STATUS, 3, 0); bitWrite(_STATUS, 3, 0);
// Set value of _STATUS at the third bit to 0 //Set value of _STATUS at the third bit to 0
} }
else if (byteOrder == 1) else if(byteOrder == 1)
{ {
// Byte order is LSB //Byte order is LSB
bitWrite(_STATUS, 3, 1); bitWrite(_STATUS, 3, 1);
// Set value of _STATUS at the third bit to 1 //Set value of _STATUS at the third bit to 1
}
else
{
} }
else{}
writeRegister(STATUS_REG, _STATUS); writeRegister(STATUS_REG, _STATUS);
delay(100); delay(100);
} }
uint8_t ADS1256::getByteOrder() // Getting byte order (MSB/LSB) uint8_t ADS1256::getByteOrder() //Getting byte order (MSB/LSB)
{ {
uint8_t statusValue = readRegister(STATUS_REG); // Read the whole STATUS register uint8_t statusValue = readRegister(STATUS_REG); //Read the whole STATUS register
return bitRead(statusValue, 3); return bitRead(statusValue, 3);
} }
void ADS1256::setAutoCal(uint8_t acal) // Setting ACAL (Automatic SYSCAL) void ADS1256::setAutoCal(uint8_t acal) //Setting ACAL (Automatic SYSCAL)
{ {
_STATUS = readRegister(STATUS_REG); // Read the most recent value of the register _STATUS = readRegister(STATUS_REG); //Read the most recent value of the register
if (acal == 0) if(acal == 0)
{ {
// Auto-calibration is disabled (default) //Auto-calibration is disabled (default)
bitWrite(_STATUS, 2, 0); bitWrite(_STATUS, 2, 0);
//_STATUS |= B00000000; //_STATUS |= B00000000;
} }
else if (acal == 1) else if(acal == 1)
{ {
// Auto-calibration is enabled //Auto-calibration is enabled
bitWrite(_STATUS, 2, 1); bitWrite(_STATUS, 2, 1);
//_STATUS |= B00000100; //_STATUS |= B00000100;
} }
else else{}
{
}
writeRegister(STATUS_REG, _STATUS); writeRegister(STATUS_REG, _STATUS);
delay(100); delay(100);
} }
uint8_t ADS1256::getAutoCal() // Getting ACAL (Automatic SYSCAL) uint8_t ADS1256::getAutoCal() //Getting ACAL (Automatic SYSCAL)
{ {
uint8_t statusValue = readRegister(STATUS_REG); // Read the whole STATUS register uint8_t statusValue = readRegister(STATUS_REG); //Read the whole STATUS register
return bitRead(statusValue, 2); return bitRead(statusValue, 2);
} }
void ADS1256::setBuffer(uint8_t bufen) // Setting input buffer (Input impedance) void ADS1256::setBuffer(uint8_t bufen) //Setting input buffer (Input impedance)
{ {
_STATUS = readRegister(STATUS_REG); // Read the most recent value of the register _STATUS = readRegister(STATUS_REG); //Read the most recent value of the register
if (bufen == 0) if(bufen == 0)
{ {
// Analog input buffer is disabled (default) //Analog input buffer is disabled (default)
//_STATUS |= B00000000; //_STATUS |= B00000000;
bitWrite(_STATUS, 1, 0); bitWrite(_STATUS, 1, 0);
} }
else if (bufen == 1) else if(bufen == 1)
{ {
// Analog input buffer is enabled (recommended) //Analog input buffer is enabled (recommended)
//_STATUS |= B00000010; //_STATUS |= B00000010;
bitWrite(_STATUS, 1, 1); bitWrite(_STATUS, 1, 1);
} }
else else{}
{
}
writeRegister(STATUS_REG, _STATUS); writeRegister(STATUS_REG, _STATUS);
delay(100); delay(100);
} }
uint8_t ADS1256::getBuffer() // Getting input buffer (Input impedance) uint8_t ADS1256::getBuffer() //Getting input buffer (Input impedance)
{ {
uint8_t statusValue = readRegister(STATUS_REG); // Read the whole STATUS register uint8_t statusValue = readRegister(STATUS_REG); //Read the whole STATUS register
return bitRead(statusValue, 1); return bitRead(statusValue, 1);
} }
void ADS1256::setGPIO(uint8_t dir0, uint8_t dir1, uint8_t dir2, uint8_t dir3) // Setting GPIO void ADS1256::setGPIO(uint8_t dir0, uint8_t dir1, uint8_t dir2, uint8_t dir3) //Setting GPIO
{ {
_GPIO = readRegister(IO_REG); // Read the most recent value of the register _GPIO = readRegister(IO_REG); //Read the most recent value of the register
// Default: 11100000 - DEC: 224 - Ref: p32 I/O section //Default: 11100000 - DEC: 224 - Ref: p32 I/O section
// Sets D3-D0 as input or output //Sets D3-D0 as input or output
uint8_t GPIO_bit7, GPIO_bit6, GPIO_bit5, GPIO_bit4; uint8_t GPIO_bit7, GPIO_bit6, GPIO_bit5, GPIO_bit4;
// Bit7: DIR3 //Bit7: DIR3
if (dir3 == 1) if(dir3 == 1)
{ {
GPIO_bit7 = 1; // D3 is input (default) GPIO_bit7 = 1; //D3 is input (default)
} }
else else
{ {
GPIO_bit7 = 0; // D3 is output GPIO_bit7 = 0; //D3 is output
} }
bitWrite(_GPIO, 7, GPIO_bit7); bitWrite(_GPIO, 7, GPIO_bit7);
//----------------------------------------------------- //-----------------------------------------------------
// Bit6: DIR2 //Bit6: DIR2
if (dir2 == 1) if(dir2 == 1)
{ {
GPIO_bit6 = 1; // D2 is input (default) GPIO_bit6 = 1; //D2 is input (default)
} }
else else
{ {
GPIO_bit6 = 0; // D2 is output GPIO_bit6 = 0; //D2 is output
} }
bitWrite(_GPIO, 6, GPIO_bit6); bitWrite(_GPIO, 6, GPIO_bit6);
//----------------------------------------------------- //-----------------------------------------------------
// Bit5: DIR1 //Bit5: DIR1
if (dir1 == 1) if(dir1 == 1)
{ {
GPIO_bit5 = 1; // D1 is input (default) GPIO_bit5 = 1; //D1 is input (default)
} }
else else
{ {
GPIO_bit5 = 0; // D1 is output GPIO_bit5 = 0; //D1 is output
} }
bitWrite(_GPIO, 5, GPIO_bit5); bitWrite(_GPIO, 5, GPIO_bit5);
//----------------------------------------------------- //-----------------------------------------------------
// Bit4: DIR0 //Bit4: DIR0
if (dir0 == 1) if(dir0 == 1)
{ {
GPIO_bit4 = 1; // D0 is input GPIO_bit4 = 1; //D0 is input
} }
else else
{ {
GPIO_bit4 = 0; // D0 is output (default) GPIO_bit4 = 0; //D0 is output (default)
} }
bitWrite(_GPIO, 4, GPIO_bit4); bitWrite(_GPIO, 4, GPIO_bit4);
//----------------------------------------------------- //-----------------------------------------------------
@@ -384,17 +367,17 @@ void ADS1256::setGPIO(uint8_t dir0, uint8_t dir1, uint8_t dir2, uint8_t dir3) //
delay(100); delay(100);
} }
void ADS1256::writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value, uint8_t dir3value) // Writing GPIO void ADS1256::writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value, uint8_t dir3value) //Writing GPIO
{ {
_GPIO = readRegister(IO_REG); _GPIO = readRegister(IO_REG);
// Sets D3-D0 output values //Sets D3-D0 output values
// It is important that first one must use setGPIO, then writeGPIO //It is important that first one must use setGPIO, then writeGPIO
uint8_t GPIO_bit3, GPIO_bit2, GPIO_bit1, GPIO_bit0; uint8_t GPIO_bit3, GPIO_bit2, GPIO_bit1, GPIO_bit0;
// Bit3: DIR3 //Bit3: DIR3
if (dir3value == 1) if(dir3value == 1)
{ {
GPIO_bit3 = 1; GPIO_bit3 = 1;
} }
@@ -404,8 +387,8 @@ void ADS1256::writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value,
} }
bitWrite(_GPIO, 3, GPIO_bit3); bitWrite(_GPIO, 3, GPIO_bit3);
//----------------------------------------------------- //-----------------------------------------------------
// Bit2: DIR2 //Bit2: DIR2
if (dir2value == 1) if(dir2value == 1)
{ {
GPIO_bit2 = 1; GPIO_bit2 = 1;
} }
@@ -415,8 +398,8 @@ void ADS1256::writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value,
} }
bitWrite(_GPIO, 2, GPIO_bit2); bitWrite(_GPIO, 2, GPIO_bit2);
//----------------------------------------------------- //-----------------------------------------------------
// Bit1: DIR1 //Bit1: DIR1
if (dir1value == 1) if(dir1value == 1)
{ {
GPIO_bit1 = 1; GPIO_bit1 = 1;
} }
@@ -426,8 +409,8 @@ void ADS1256::writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value,
} }
bitWrite(_GPIO, 1, GPIO_bit1); bitWrite(_GPIO, 1, GPIO_bit1);
//----------------------------------------------------- //-----------------------------------------------------
// Bit0: DIR0 //Bit0: DIR0
if (dir0value == 1) if(dir0value == 1)
{ {
GPIO_bit0 = 1; GPIO_bit0 = 1;
} }
@@ -442,13 +425,13 @@ void ADS1256::writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value,
delay(100); delay(100);
} }
uint8_t ADS1256::readGPIO(uint8_t gpioPin) // Reading GPIO uint8_t ADS1256::readGPIO(uint8_t gpioPin) //Reading GPIO
{ {
uint8_t GPIO_bit3, GPIO_bit2, GPIO_bit1, GPIO_bit0, GPIO_return; uint8_t GPIO_bit3, GPIO_bit2, GPIO_bit1, GPIO_bit0, GPIO_return;
_GPIO = readRegister(IO_REG); // Read the GPIO register _GPIO = readRegister(IO_REG); //Read the GPIO register
// Save each bit values in a variable //Save each bit values in a variable
GPIO_bit3 = bitRead(_GPIO, 3); GPIO_bit3 = bitRead(_GPIO, 3);
GPIO_bit2 = bitRead(_GPIO, 2); GPIO_bit2 = bitRead(_GPIO, 2);
GPIO_bit1 = bitRead(_GPIO, 1); GPIO_bit1 = bitRead(_GPIO, 1);
@@ -456,7 +439,7 @@ uint8_t ADS1256::readGPIO(uint8_t gpioPin) // Reading GPIO
delay(100); delay(100);
switch (gpioPin) // Selecting which value should be returned switch(gpioPin) //Selecting which value should be returned
{ {
case 0: case 0:
GPIO_return = GPIO_bit0; GPIO_return = GPIO_bit0;
@@ -476,117 +459,108 @@ uint8_t ADS1256::readGPIO(uint8_t gpioPin) // Reading GPIO
} }
return GPIO_return; return GPIO_return;
} }
void ADS1256::sendDirectCommand(uint8_t directCommand) void ADS1256::sendDirectCommand(uint8_t directCommand)
{ {
LOG_DEBUG("Direct Command"); //Direct commands can be found in the datasheet Page 34, Table 24.
// Direct commands can be found in the datasheet Page 34, Table 24.
LOG_DEBUG("Direct Command Begin");
_spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1)); _spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1));
CS_LOW(); // REF: P34: "CS must stay low during the entire command sequence" CS_LOW(); //REF: P34: "CS must stay low during the entire command sequence"
LOG_DEBUG("Direct Command CS LOW");
delayMicroseconds(5); delayMicroseconds(5);
_spi->transfer(directCommand); // Send Command _spi->transfer(directCommand); //Send Command
LOG_DEBUG("Transfer OK");
delayMicroseconds(5); delayMicroseconds(5);
CS_HIGH(); // REF: P34: "CS must stay low during the entire command sequence" CS_HIGH(); //REF: P34: "CS must stay low during the entire command sequence"
LOG_DEBUG("Direct Command CS HIGH");
_spi->endTransaction(); _spi->endTransaction();
LOG_DEBUG("Direct Command End");
} }
float ADS1256::convertToVoltage(int32_t rawData) // Converting the 24-bit data into a voltage value
float ADS1256::convertToVoltage(int32_t rawData) //Converting the 24-bit data into a voltage value
{ {
return (conversionParameter * rawData); return(conversionParameter * rawData);
} }
void ADS1256::writeRegister(uint8_t registerAddress, uint8_t registerValueToWrite) void ADS1256::writeRegister(uint8_t registerAddress, uint8_t registerValueToWrite)
{ {
waitForLowDRDY(); waitForLowDRDY();
LOG_DEBUG("DRDY Low");
_spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1)); _spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1));
// SPI_MODE1 = output edge: rising, data capture: falling; clock polarity: 0, clock phase: 1. //SPI_MODE1 = output edge: rising, data capture: falling; clock polarity: 0, clock phase: 1.
LOG_DEBUG("SPI Begin");
CS_LOW(); // CS must stay LOW during the entire sequence [Ref: P34, T24] CS_LOW(); //CS must stay LOW during the entire sequence [Ref: P34, T24]
LOG_DEBUG("CS Low");
delayMicroseconds(5); // see t6 in the datasheet delayMicroseconds(5); //see t6 in the datasheet
_spi->transfer(0x50 | registerAddress); // 0x50 = 01010000 = WREG _spi->transfer(0x50 | registerAddress); // 0x50 = 01010000 = WREG
LOG_DEBUG("Transfer 1");
_spi->transfer(0x00); // 2nd (empty) command byte _spi->transfer(0x00); //2nd (empty) command byte
LOG_DEBUG("Transfer 2");
_spi->transfer(registerValueToWrite); // pass the value to the register _spi->transfer(registerValueToWrite); //pass the value to the register
LOG_DEBUG("Transfer 3");
CS_HIGH(); CS_HIGH();
LOG_DEBUG("CS High");
_spi->endTransaction(); _spi->endTransaction();
LOG_DEBUG("SPI End"); delay(100);
} }
long ADS1256::readRegister(uint8_t registerAddress) // Reading a register long ADS1256::readRegister(uint8_t registerAddress) //Reading a register
{ {
waitForLowDRDY(); waitForLowDRDY();
_spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1)); _spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1));
// SPI_MODE1 = output edge: rising, data capture: falling; clock polarity: 0, clock phase: 1. //SPI_MODE1 = output edge: rising, data capture: falling; clock polarity: 0, clock phase: 1.
CS_LOW(); // CS must stay LOW during the entire sequence [Ref: P34, T24] CS_LOW(); //CS must stay LOW during the entire sequence [Ref: P34, T24]
_spi->transfer(0x10 | registerAddress); // 0x10 = 0001000 = RREG - OR together the two numbers (command + address) _spi->transfer(0x10 | registerAddress); //0x10 = 0001000 = RREG - OR together the two numbers (command + address)
_spi->transfer(0x00); // 2nd (empty) command byte _spi->transfer(0x00); //2nd (empty) command byte
delayMicroseconds(5); // see t6 in the datasheet delayMicroseconds(5); //see t6 in the datasheet
uint8_t regValue = _spi->transfer(0xFF); // read out the register value uint8_t regValue = _spi->transfer(0xFF); //read out the register value
CS_HIGH(); CS_HIGH();
_spi->endTransaction(); _spi->endTransaction();
delay(100);
return regValue; return regValue;
} }
long ADS1256::readSingle() // Reading a single value ONCE using the RDATA command
long ADS1256::readSingle() //Reading a single value ONCE using the RDATA command
{ {
_spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1)); _spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1));
CS_LOW(); // REF: P34: "CS must stay low during the entire command sequence" CS_LOW(); //REF: P34: "CS must stay low during the entire command sequence"
waitForLowDRDY(); waitForLowDRDY();
_spi->transfer(0b00000001); // Issue RDATA (0000 0001) command _spi->transfer(0b00000001); //Issue RDATA (0000 0001) command
delayMicroseconds(7); // Wait t6 time (~6.51 us) REF: P34, FIG:30. delayMicroseconds(7); //Wait t6 time (~6.51 us) REF: P34, FIG:30.
_outputBuffer[0] = _spi->transfer(0); // MSB _outputBuffer[0] = _spi->transfer(0); // MSB
_outputBuffer[1] = _spi->transfer(0); // Mid-byte _outputBuffer[1] = _spi->transfer(0); // Mid-byte
_outputBuffer[2] = _spi->transfer(0); // LSB _outputBuffer[2] = _spi->transfer(0); // LSB
// Shifting and combining the above three items into a single, 24-bit number //Shifting and combining the above three items into a single, 24-bit number
_outputValue = ((long)_outputBuffer[0] << 16) | ((long)_outputBuffer[1] << 8) | (_outputBuffer[2]); _outputValue = ((long)_outputBuffer[0]<<16) | ((long)_outputBuffer[1]<<8) | (_outputBuffer[2]);
_outputValue = convertSigned24BitToLong(_outputValue); _outputValue = convertSigned24BitToLong(_outputValue);
CS_HIGH(); // We finished the command sequence, so we set CS to HIGH CS_HIGH(); //We finished the command sequence, so we set CS to HIGH
_spi->endTransaction(); _spi->endTransaction();
return (_outputValue); return(_outputValue);
} }
long ADS1256::readSingleContinuous() // Reads the recently selected input channel using RDATAC long ADS1256::readSingleContinuous() //Reads the recently selected input channel using RDATAC
{ {
if (_isAcquisitionRunning == false) if(_isAcquisitionRunning == false)
{ {
_isAcquisitionRunning = true; _isAcquisitionRunning = true;
_spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1)); _spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1));
CS_LOW(); // REF: P34: "CS must stay low during the entire command sequence" CS_LOW(); //REF: P34: "CS must stay low during the entire command sequence"
waitForLowDRDY(); waitForLowDRDY();
_spi->transfer(0b00000011); // Issue RDATAC (0000 0011) _spi->transfer(0b00000011); //Issue RDATAC (0000 0011)
delayMicroseconds(7); // Wait t6 time (~6.51 us) REF: P34, FIG:30. delayMicroseconds(7); //Wait t6 time (~6.51 us) REF: P34, FIG:30.
} }
else else
{ {
@@ -597,7 +571,7 @@ long ADS1256::readSingleContinuous() // Reads the recently selected input channe
_outputBuffer[1] = _spi->transfer(0); // Mid-byte _outputBuffer[1] = _spi->transfer(0); // Mid-byte
_outputBuffer[2] = _spi->transfer(0); // LSB _outputBuffer[2] = _spi->transfer(0); // LSB
_outputValue = ((long)_outputBuffer[0] << 16) | ((long)_outputBuffer[1] << 8) | (_outputBuffer[2]); _outputValue = ((long)_outputBuffer[0]<<16) | ((long)_outputBuffer[1]<<8) | (_outputBuffer[2]);
_outputValue = convertSigned24BitToLong(_outputValue); _outputValue = convertSigned24BitToLong(_outputValue);
waitForHighDRDY(); waitForHighDRDY();
@@ -607,84 +581,83 @@ long ADS1256::readSingleContinuous() // Reads the recently selected input channe
long ADS1256::cycleSingle() long ADS1256::cycleSingle()
{ {
if (_isAcquisitionRunning == false) if(_isAcquisitionRunning == false)
{ {
_isAcquisitionRunning = true; _isAcquisitionRunning = true;
_cycle = 0; _cycle = 0;
_spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1)); _spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1));
CS_LOW(); // CS must stay LOW during the entire sequence [Ref: P34, T24] CS_LOW(); //CS must stay LOW during the entire sequence [Ref: P34, T24]
_spi->transfer(0x50 | 1); // 0x50 = WREG //1 = MUX _spi->transfer(0x50 | 1); // 0x50 = WREG //1 = MUX
_spi->transfer(0x00); _spi->transfer(0x00);
_spi->transfer(SING_0); // AIN0+AINCOM _spi->transfer(SING_0); //AIN0+AINCOM
CS_HIGH(); CS_HIGH();
delay(50); delay(50);
CS_LOW(); // CS must stay LOW during the entire sequence [Ref: P34, T24] CS_LOW(); //CS must stay LOW during the entire sequence [Ref: P34, T24]
} }
else else
{ {}
}
if (_cycle < 8) if(_cycle < 8)
{ {
_outputValue = 0; _outputValue = 0;
waitForLowDRDY(); waitForLowDRDY();
// Step 1. - Updating MUX //Step 1. - Updating MUX
switch (_cycle) switch (_cycle)
{ {
// Channels are written manually //Channels are written manually
case 0: // Channel 2 case 0: //Channel 2
updateMUX(SING_1); // AIN1+AINCOM updateMUX(SING_1); //AIN1+AINCOM
break; break;
case 1: // Channel 3 case 1: //Channel 3
updateMUX(SING_2); // AIN2+AINCOM updateMUX(SING_2); //AIN2+AINCOM
break; break;
case 2: // Channel 4 case 2: //Channel 4
updateMUX(SING_3); // AIN3+AINCOM updateMUX(SING_3); //AIN3+AINCOM
break; break;
case 3: // Channel 5 case 3: //Channel 5
updateMUX(SING_4); // AIN4+AINCOM updateMUX(SING_4); //AIN4+AINCOM
break; break;
case 4: // Channel 6 case 4: //Channel 6
updateMUX(SING_5); // AIN5+AINCOM updateMUX(SING_5); //AIN5+AINCOM
break; break;
case 5: // Channel 7 case 5: //Channel 7
updateMUX(SING_6); // AIN6+AINCOM updateMUX(SING_6); //AIN6+AINCOM
break; break;
case 6: // Channel 8 case 6: //Channel 8
updateMUX(SING_7); // AIN7+AINCOM updateMUX(SING_7); //AIN7+AINCOM
break; break;
case 7: // Channel 1 case 7: //Channel 1
updateMUX(SING_0); // AIN0+AINCOM updateMUX(SING_0); //AIN0+AINCOM
break; break;
} }
// Step 2. //Step 2.
_spi->transfer(0b11111100); // SYNC _spi->transfer(0b11111100); //SYNC
delayMicroseconds(4); // t11 delay 24*tau = 3.125 us //delay should be larger, so we delay by 4 us delayMicroseconds(4); //t11 delay 24*tau = 3.125 us //delay should be larger, so we delay by 4 us
_spi->transfer(0b11111111); // WAKEUP _spi->transfer(0b11111111); //WAKEUP
// Step 3. //Step 3.
// Issue RDATA (0000 0001) command //Issue RDATA (0000 0001) command
_spi->transfer(0b00000001); _spi->transfer(0b00000001);
delayMicroseconds(7); // Wait t6 time (~6.51 us) REF: P34, FIG:30. delayMicroseconds(7); //Wait t6 time (~6.51 us) REF: P34, FIG:30.
_outputBuffer[0] = _spi->transfer(0x0F); // MSB _outputBuffer[0] = _spi->transfer(0x0F); // MSB
_outputBuffer[1] = _spi->transfer(0x0F); // Mid-byte _outputBuffer[1] = _spi->transfer(0x0F); // Mid-byte
_outputBuffer[2] = _spi->transfer(0x0F); // LSB _outputBuffer[2] = _spi->transfer(0x0F); // LSB
_outputValue = ((long)_outputBuffer[0] << 16) | ((long)_outputBuffer[1] << 8) | (_outputBuffer[2]); _outputValue = ((long)_outputBuffer[0]<<16) | ((long)_outputBuffer[1]<<8) | (_outputBuffer[2]);
_outputValue = convertSigned24BitToLong(_outputValue); _outputValue = convertSigned24BitToLong(_outputValue);
_cycle++; // Increase cycle - This will move to the next MUX input channel _cycle++; //Increase cycle - This will move to the next MUX input channel
if (_cycle == 8) if(_cycle == 8)
{ {
_cycle = 0; // Reset to 0 - Restart conversion from the 1st input channel _cycle = 0; //Reset to 0 - Restart conversion from the 1st input channel
} }
} }
@@ -693,71 +666,70 @@ long ADS1256::cycleSingle()
long ADS1256::cycleDifferential() long ADS1256::cycleDifferential()
{ {
if (_isAcquisitionRunning == false) if(_isAcquisitionRunning == false)
{ {
_cycle = 0; _cycle = 0;
_isAcquisitionRunning = true; _isAcquisitionRunning = true;
_spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1)); _spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1));
// Set the AIN0+AIN1 as inputs manually //Set the AIN0+AIN1 as inputs manually
CS_LOW(); // CS must stay LOW during the entire sequence [Ref: P34, T24] CS_LOW(); //CS must stay LOW during the entire sequence [Ref: P34, T24]
_spi->transfer(0x50 | 1); // 0x50 = WREG //1 = MUX _spi->transfer(0x50 | 1); // 0x50 = WREG //1 = MUX
_spi->transfer(0x00); _spi->transfer(0x00);
_spi->transfer(DIFF_0_1); // AIN0+AIN1 _spi->transfer(DIFF_0_1); //AIN0+AIN1
CS_HIGH(); CS_HIGH();
delay(50); delay(50);
CS_LOW(); // CS must stay LOW during the entire sequence [Ref: P34, T24] CS_LOW(); //CS must stay LOW during the entire sequence [Ref: P34, T24]
} }
else else
{ {}
}
if (_cycle < 4) if(_cycle < 4)
{ {
_outputValue = 0; _outputValue = 0;
// DRDY has to go low //DRDY has to go low
waitForLowDRDY(); waitForLowDRDY();
// Step 1. - Updating MUX //Step 1. - Updating MUX
switch (_cycle) switch (_cycle)
{ {
case 0: // Channel 2 case 0: //Channel 2
updateMUX(DIFF_2_3); // AIN2+AIN3 updateMUX(DIFF_2_3); //AIN2+AIN3
break; break;
case 1: // Channel 3 case 1: //Channel 3
updateMUX(DIFF_4_5); // AIN4+AIN5 updateMUX(DIFF_4_5); //AIN4+AIN5
break; break;
case 2: // Channel 4 case 2: //Channel 4
updateMUX(DIFF_6_7); // AIN6+AIN7 updateMUX(DIFF_6_7); //AIN6+AIN7
break; break;
case 3: // Channel 1 case 3: //Channel 1
updateMUX(DIFF_0_1); // AIN0+AIN1 updateMUX(DIFF_0_1); //AIN0+AIN1
break; break;
} }
_spi->transfer(0b11111100); // SYNC _spi->transfer(0b11111100); //SYNC
delayMicroseconds(4); // t11 delay 24*tau = 3.125 us //delay should be larger, so we delay by 4 us delayMicroseconds(4); //t11 delay 24*tau = 3.125 us //delay should be larger, so we delay by 4 us
_spi->transfer(0b11111111); // WAKEUP _spi->transfer(0b11111111); //WAKEUP
// Step 3. //Step 3.
_spi->transfer(0b00000001); // Issue RDATA (0000 0001) command _spi->transfer(0b00000001); //Issue RDATA (0000 0001) command
delayMicroseconds(7); // Wait t6 time (~6.51 us) REF: P34, FIG:30. delayMicroseconds(7); //Wait t6 time (~6.51 us) REF: P34, FIG:30.
_outputBuffer[0] = _spi->transfer(0); // MSB _outputBuffer[0] = _spi->transfer(0); // MSB
_outputBuffer[1] = _spi->transfer(0); // Mid-byte _outputBuffer[1] = _spi->transfer(0); // Mid-byte
_outputBuffer[2] = _spi->transfer(0); // LSB _outputBuffer[2] = _spi->transfer(0); // LSB
_outputValue = ((long)_outputBuffer[0] << 16) | ((long)_outputBuffer[1] << 8) | (_outputBuffer[2]); _outputValue = ((long)_outputBuffer[0]<<16) | ((long)_outputBuffer[1]<<8) | (_outputBuffer[2]);
_outputValue = convertSigned24BitToLong(_outputValue); _outputValue = convertSigned24BitToLong(_outputValue);
_cycle++; _cycle++;
if (_cycle == 4) if(_cycle == 4)
{ {
_cycle = 0; _cycle = 0;
// After the 4th cycle, we reset to zero so the next iteration reads the 1st MUX again //After the 4th cycle, we reset to zero so the next iteration reads the 1st MUX again
} }
} }
@@ -766,20 +738,20 @@ long ADS1256::cycleDifferential()
void ADS1256::updateConversionParameter() void ADS1256::updateConversionParameter()
{ {
conversionParameter = ((2.0 * _VREF) / 8388608.0) / (pow(2, _PGA)); // Calculate the "bit to Volts" multiplier conversionParameter = ((2.0 * _VREF) / 8388608.0) / (pow(2, _PGA)); //Calculate the "bit to Volts" multiplier
// 8388608 = 2^{23} - 1, REF: p23, Table 16. //8388608 = 2^{23} - 1, REF: p23, Table 16.
} }
void ADS1256::updateMUX(uint8_t muxValue) void ADS1256::updateMUX(uint8_t muxValue)
{ {
_spi->transfer(0x50 | MUX_REG); // Write to the MUX register (0x50 is the WREG command) _spi->transfer(0x50 | MUX_REG); //Write to the MUX register (0x50 is the WREG command)
_spi->transfer(0x00); _spi->transfer(0x00);
_spi->transfer(muxValue); // Write the new MUX value _spi->transfer(muxValue); //Write the new MUX value
} }
inline void ADS1256::CS_LOW() inline void ADS1256::CS_LOW()
{ {
if (_CS_pin != PIN_UNUSED) // Sets CS LOW if it is not an unused pin if (_CS_pin != PIN_UNUSED) //Sets CS LOW if it is not an unused pin
{ {
digitalWrite(_CS_pin, LOW); digitalWrite(_CS_pin, LOW);
} }
@@ -787,7 +759,7 @@ inline void ADS1256::CS_LOW()
inline void ADS1256::CS_HIGH() inline void ADS1256::CS_HIGH()
{ {
if (_CS_pin != PIN_UNUSED) // Sets CS HIGH if it is not an unused pin if (_CS_pin != PIN_UNUSED) //Sets CS HIGH if it is not an unused pin
{ {
digitalWrite(_CS_pin, HIGH); digitalWrite(_CS_pin, HIGH);
} }

View File

@@ -1,8 +1,6 @@
#include "datasave.h" #include "datasave.h"
#include <math.h> #include <math.h>
LITTLEFSGuard::LITTLEFSGuard() LITTLEFSGuard::LITTLEFSGuard()
{ {
if (!LittleFS.begin(true, "/littlefs", 10, "littlefs")) if (!LittleFS.begin(true, "/littlefs", 10, "littlefs"))
@@ -12,7 +10,7 @@ LITTLEFSGuard::LITTLEFSGuard()
else else
{ {
LOG_INFO("LittleFS mounted successfully"); LOG_INFO("LittleFS mounted successfully");
LOG_INFO("LittleFS Free KBytes:", (LittleFS.totalBytes() - LittleFS.usedBytes()) /1024); LOG_INFO("LittleFS Free KBytes:", (LittleFS.totalBytes() - LittleFS.usedBytes()) / 1024);
} }
} }
@@ -55,23 +53,24 @@ void ignitionBoxStatusFiltered::update(const ignitionBoxStatus &new_status)
m_last.coils12.n_missed_firing = new_status.coils12.n_missed_firing; // sum missed firings instead of averaging m_last.coils12.n_missed_firing = new_status.coils12.n_missed_firing; // sum missed firings instead of averaging
m_last.coils12.spark_status = new_status.coils12.spark_status; // take latest spark status m_last.coils12.spark_status = new_status.coils12.spark_status; // take latest spark status
m_last.coils12.sstart_status = new_status.coils12.sstart_status; // take latest soft start status m_last.coils12.sstart_status = new_status.coils12.sstart_status; // take latest soft start status
filter(m_last.coils12.spark_delay, new_status.coils12.spark_delay, m_max_count); // incremental average calculation m_last.coils12.spark_delay = new_status.coils12.spark_delay; // incremental average calculation
filter(m_last.coils12.peak_p_in, new_status.coils12.peak_p_in, m_max_count); // incremental average calculation m_last.coils12.peak_p_in = new_status.coils12.peak_p_in; // incremental average calculation
filter(m_last.coils12.peak_n_in, new_status.coils12.peak_n_in, m_max_count); // incremental average calculation m_last.coils12.peak_n_in = new_status.coils12.peak_n_in; // incremental average calculation
filter(m_last.coils12.peak_p_out, new_status.coils12.peak_p_out, m_max_count); // incremental average calculation m_last.coils12.peak_p_out = new_status.coils12.peak_p_out; // incremental average calculation
filter(m_last.coils12.peak_n_out, new_status.coils12.peak_n_out, m_max_count); // incremental average calculation m_last.coils12.peak_n_out = new_status.coils12.peak_n_out; // incremental average calculation
m_last.coils34.n_events = new_status.coils34.n_events; // sum events instead of averaging m_last.coils34.n_events = new_status.coils34.n_events; // sum events instead of averaging
m_last.coils34.n_missed_firing = new_status.coils34.n_missed_firing; // sum missed firings instead of averaging m_last.coils34.n_missed_firing = new_status.coils34.n_missed_firing; // sum missed firings instead of averaging
m_last.coils34.spark_status = new_status.coils34.spark_status; // take latest spark status m_last.coils34.spark_status = new_status.coils34.spark_status; // take latest spark status
m_last.coils34.sstart_status = new_status.coils34.sstart_status; // take latest soft start status m_last.coils34.sstart_status = new_status.coils34.sstart_status; // take latest soft start status
filter(m_last.coils34.spark_delay, new_status.coils34.spark_delay, m_max_count); // incremental average calculation m_last.coils34.spark_delay = new_status.coils34.spark_delay; // incremental average calculation
filter(m_last.coils34.peak_p_in, new_status.coils34.peak_p_in, m_max_count); // incremental average calculation m_last.coils34.peak_p_in = new_status.coils34.peak_p_in; // incremental average calculation
filter(m_last.coils34.peak_n_in, new_status.coils34.peak_n_in, m_max_count); // incremental average calculation m_last.coils34.peak_n_in = new_status.coils34.peak_n_in; // incremental average calculation
filter(m_last.coils34.peak_p_out, new_status.coils34.peak_p_out, m_max_count); // incremental average calculation m_last.coils34.peak_p_out = new_status.coils34.peak_p_out; // incremental average calculation
filter(m_last.coils34.peak_n_out, new_status.coils34.peak_n_out, m_max_count); // incremental average calculation m_last.coils34.peak_n_out = new_status.coils34.peak_n_out; // incremental average calculation
filter(m_last.eng_rpm, new_status.eng_rpm, m_max_count); // incremental average calculation // incremental average calculation
filter(m_last.adc_read_time, m_last.adc_read_time, m_max_count); // incremental average calculation m_last.eng_rpm = new_status.eng_rpm; // incremental average calculation
m_last.adc_read_time = m_last.adc_read_time; // incremental average calculation
m_last.n_queue_errors = new_status.n_queue_errors; // take last of queue errors since it's a cumulative count of errors in the queue, not an average value m_last.n_queue_errors = new_status.n_queue_errors; // take last of queue errors since it's a cumulative count of errors in the queue, not an average value
if (m_count >= m_max_count) if (m_count >= m_max_count)
@@ -124,4 +123,3 @@ const ArduinoJson::JsonDocument ignitionBoxStatusFiltered::toJson() const
} }
return doc; return doc;
} }

View File

@@ -43,18 +43,13 @@ struct Devices
std::unique_ptr<ADS1256> m_adc_b = nullptr; std::unique_ptr<ADS1256> m_adc_b = nullptr;
std::unique_ptr<ExternalIO> m_ext_io = nullptr; std::unique_ptr<ExternalIO> m_ext_io = nullptr;
}; };
// Adc read channel wrapper to selet mux before reading // Adc read channel wrapper to selet mux before reading
inline float adcReadChannel(ADS1256 *adc, const uint8_t ch) inline float adcReadChannel(ADS1256 *adc, const uint8_t ch)
{ {
adc->setMUX(ch); adc->setMUX(ch);
// scarta 3 conversioni
for (int i = 0; i < 5; i++)
{
adc->readSingle(); adc->readSingle();
}
// ora lettura valida a 30kSPS → ~100 µs di settling // ora lettura valida a 30kSPS → ~100 µs di settling
return adc->convertToVoltage(adc->readSingle()); return adc->convertToVoltage(adc->readSingle());
} }

View File

@@ -53,7 +53,7 @@ void setup()
IPAddress gateway(10, 11, 12, 1); IPAddress gateway(10, 11, 12, 1);
IPAddress subnet(255, 255, 255, 0); IPAddress subnet(255, 255, 255, 0);
WiFi.softAPConfig(local_IP, gateway, subnet); WiFi.softAPConfig(local_IP, gateway, subnet);
WiFi.setTxPower(WIFI_POWER_13dBm); // reduce wifi power WiFi.setTxPower(WIFI_POWER_5dBm); // reduce wifi power
if (WiFi.softAP(WIFI_SSID, WIFI_PASSWORD)) if (WiFi.softAP(WIFI_SSID, WIFI_PASSWORD))
{ {
LOG_INFO("WiFi AP Mode Started"); LOG_INFO("WiFi AP Mode Started");
@@ -117,20 +117,43 @@ void loop()
#endif #endif
// Init ADCs // Init ADCs
dev->m_adc_a = std::make_unique<ADS1256>(ADC_A_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_A_CS, 2.5, &SPI_A); dev->m_adc_a = std::make_unique<ADS1256>(ADC_A_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_A_CS, 2.5, &SPI_A);
#ifdef CH_B_ENABLE #ifdef CH_B_ENABLE
dev->m_adc_b = std::make_unique<ADS1256>(ADC_B_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_B_CS, 2.5, &SPI_B); dev->m_adc_b = std::make_unique<ADS1256>(ADC_B_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_B_CS, 2.5, &SPI_B);
#endif #endif
// Configure ADCs // Configure ADCs
dev->m_adc_a->InitializeADC(); dev->m_adc_a->InitializeADC();
dev->m_adc_a->setPGA(PGA_1); dev->m_adc_a->setPGA(PGA_1);
dev->m_adc_a->setDRATE(DRATE_7500SPS); // dev->m_adc_a->setDRATE(DRATE_15000SPS);
#ifdef CH_B_ENABLE #ifdef CH_B_ENABLE
dev->m_adc_b->InitializeADC(); dev->m_adc_b->InitializeADC();
dev->m_adc_b->setPGA(PGA_1); dev->m_adc_b->setPGA(PGA_1);
dev->m_adc_b->setDRATE(DRATE_7500SPS); dev->m_adc_b->setDRATE(DRATE_30000SPS);
#endif #endif
LOG_DEBUG("Init SPI OK"); LOG_DEBUG("Init SPI OK");
uint8_t chs[8] = {
SING_0, SING_1, SING_2, SING_3, SING_4, SING_5, SING_6, SING_7
};
float res[8];
while (Serial.read() != 's') // The conversion is stopped by a character received from the serial port
{
clearScreen();
auto start = esp_timer_get_time();
for (int i = 0; i < 8; i++){
// dev->m_adc_a->setMUX(chs[i]);
res[i] = dev->m_adc_a->convertToVoltage(dev->m_adc_a->cycleSingle());
}
auto stop = esp_timer_get_time();
for (int j = 0; j < 8; j++){
Serial.printf("ADC_A SING_%d: %5.4f\n",j, res[j]);
}
Serial.printf("ADC Time: %u us\n", stop-start);
delay(100);
}
dev->m_adc_a->stopConversion();
//////// INIT I2C INTERFACES //////// //////// INIT I2C INTERFACES ////////
LOG_DEBUG("Init I2C Interfaces"); LOG_DEBUG("Init I2C Interfaces");
bool i2c_ok = true; bool i2c_ok = true;

View File

@@ -40,14 +40,13 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
QueueHandle_t rt_queue = params->rt_queue; QueueHandle_t rt_queue = params->rt_queue;
Devices *dev = params->dev.get(); Devices *dev = params->dev.get();
ADS1256 *adc = params->name == "rtIgnTask_A" ? dev->m_adc_a.get() : dev->m_adc_b.get(); ADS1256 *adc = params->name == "rtIgnTask_A" ? dev->m_adc_a.get() : dev->m_adc_b.get();
std::mutex& spi_mutex = params->name == "rtIgnTask_A" ? dev->m_spi_a_mutex : dev->m_spi_b_mutex; std::mutex &spi_mutex = params->name == "rtIgnTask_A" ? dev->m_spi_a_mutex : dev->m_spi_b_mutex;
ExternalIO* io = dev->m_ext_io.get(); ExternalIO *io = dev->m_ext_io.get();
TaskStatus_t rt_task_info; TaskStatus_t rt_task_info;
vTaskGetInfo(NULL, &rt_task_info, pdFALSE, eInvalid); vTaskGetInfo(NULL, &rt_task_info, pdFALSE, eInvalid);
const auto rt_task_name = pcTaskGetName(rt_task_info.xHandle); LOG_INFO("rtTask Params OK [", params->name.c_str(), "]");
LOG_INFO("rtTask Params OK [", rt_task_name, "]");
ignitionBoxStatus ign_box_sts; ignitionBoxStatus ign_box_sts;
@@ -98,7 +97,7 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
attachInterruptArg(digitalPinToInterrupt(rt_int.spark_pin_12), rt_int.isr_ptr, (void *)&isr_params_sp12, RISING); attachInterruptArg(digitalPinToInterrupt(rt_int.spark_pin_12), rt_int.isr_ptr, (void *)&isr_params_sp12, RISING);
attachInterruptArg(digitalPinToInterrupt(rt_int.spark_pin_34), rt_int.isr_ptr, (void *)&isr_params_sp34, RISING); attachInterruptArg(digitalPinToInterrupt(rt_int.spark_pin_34), rt_int.isr_ptr, (void *)&isr_params_sp34, RISING);
LOG_INFO("rtTask ISR Attach OK [", rt_task_name, "]"); LOG_INFO("rtTask ISR Attach OK [", params->name.c_str(), "]");
// Global rt_task_ptr variables // Global rt_task_ptr variables
bool first_cycle = true; bool first_cycle = true;
@@ -236,7 +235,7 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
// read adc channels: pickup12, out12 [ pos + neg ] // read adc channels: pickup12, out12 [ pos + neg ]
if (adc) // read only if adc initialized if (adc) // read only if adc initialized
{ {
std::lock_guard<std::mutex> lock (spi_mutex); std::lock_guard<std::mutex> lock(spi_mutex);
uint32_t start_adc_read = esp_timer_get_time(); uint32_t start_adc_read = esp_timer_get_time();
// from peak detector circuits // from peak detector circuits
ign_box_sts.coils12.peak_p_in = adcReadChannel(adc, ADC_CH_PEAK_12P_IN); ign_box_sts.coils12.peak_p_in = adcReadChannel(adc, ADC_CH_PEAK_12P_IN);
@@ -256,10 +255,23 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
// outputs on io expander // outputs on io expander
if (io) if (io)
{ {
// [TODO] code to reset sample and hold and arm trigger level detectors // Discharge Pulse
io->extDigitalWrite(rt_rst.sh_disch_12, true);
io->extDigitalWrite(rt_rst.sh_disch_34, true);
delayMicroseconds(250);
io->extDigitalWrite(rt_rst.sh_disch_12, false);
io->extDigitalWrite(rt_rst.sh_disch_34, false);
// Safety delay
delayMicroseconds(500);
// Re-Arm Pulse
io->extDigitalWrite(rt_rst.sh_arm_12, true);
io->extDigitalWrite(rt_rst.sh_arm_34, true);
delayMicroseconds(250);
io->extDigitalWrite(rt_rst.sh_arm_12, false);
io->extDigitalWrite(rt_rst.sh_arm_34, false);
} }
else else
vTaskDelay(pdMS_TO_TICKS(2)); vTaskDelay(pdMS_TO_TICKS(c_io_time));
// send essage to main loop with ignition info, by copy so local static variable is ok // send essage to main loop with ignition info, by copy so local static variable is ok
if (rt_queue) if (rt_queue)
@@ -272,7 +284,7 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
} }
// Delete the timeout timer // Delete the timeout timer
esp_timer_delete(timeout_timer); esp_timer_delete(timeout_timer);
LOG_WARN("rtTask Ending [", rt_task_name, "]"); LOG_WARN("rtTask Ending [", params->name.c_str(), "]");
// Ignition A Interrupts DETACH // Ignition A Interrupts DETACH
detachInterrupt(rt_int.trig_pin_12p); detachInterrupt(rt_int.trig_pin_12p);
detachInterrupt(rt_int.trig_pin_12n); detachInterrupt(rt_int.trig_pin_12n);