io expander class ok , adc not working

This commit is contained in:
Emanuele Trabattoni
2026-04-14 11:02:33 +02:00
parent 782aa95ee6
commit 899c8cffbc
11 changed files with 838 additions and 574 deletions

View File

@@ -1,4 +1,4 @@
//ADS1256 cpp file
// ADS1256 cpp file
/*
Name: ADS1256.cpp
Created: 2022/07/14
@@ -15,350 +15,376 @@
#include "ADS1256.h"
#include "SPI.h"
#include "DebugLog.h"
#define convertSigned24BitToLong(value) ((value) & (1l << 23) ? (value) - 0x1000000 : value)
//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),
// 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),
_DRDY_pin(DRDY_pin), _RESET_pin(RESET_pin), _SYNC_pin(SYNC_pin), _CS_pin(CS_pin), _VREF(VREF), _PGA(0)
{
pinMode(_DRDY_pin, INPUT);
if(RESET_pin != PIN_UNUSED)
if (RESET_pin != PIN_UNUSED)
{
pinMode(_RESET_pin, OUTPUT);
}
if(SYNC_pin != PIN_UNUSED)
if (SYNC_pin != PIN_UNUSED)
{
pinMode(_SYNC_pin, OUTPUT);
}
if(CS_pin != PIN_UNUSED)
if (CS_pin != PIN_UNUSED)
{
pinMode(_CS_pin, OUTPUT);
}
LOG_DEBUG("ADC Class Init OK");
updateConversionParameter();
}
//Initialization
// Initialization
void ADS1256::InitializeADC()
{
//Chip select LOW
LOG_DEBUG("Initialize ADC");
// Chip select LOW
CS_LOW();
//We do a manual chip reset on the ADS1256 - Datasheet Page 27/ RESET
if(_RESET_pin != PIN_UNUSED)
// We do a manual chip reset on the ADS1256 - Datasheet Page 27/ RESET
if (_RESET_pin != PIN_UNUSED)
{
digitalWrite(_RESET_pin, LOW);
delay(200);
digitalWrite(_RESET_pin, HIGH); //RESET is set to high
digitalWrite(_RESET_pin, HIGH); // RESET is set to high
delay(1000);
}
//Sync pin is also treated if it is defined
if(_SYNC_pin != PIN_UNUSED)
// Sync pin is also treated if it is defined
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
_spi->begin();
#ifndef ADS1256_SPI_ALREADY_STARTED // Guard macro to allow external initialization of the SPI
//_spi->begin();
#endif
//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
// 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
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);
delay(200);
LOG_DEBUG("Status REG OK");
_MUX = 0b00000001; //MUX AIN0+AIN1
_MUX = 0b00000001; // MUX AIN0+AIN1
writeRegister(MUX_REG, _MUX);
delay(200);
LOG_DEBUG("Mux REG OK");
_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);
delay(200);
LOG_DEBUG("Adcon REG OK");
updateConversionParameter();
_DRATE = 0b10000010; //100SPS
_DRATE = 0b10000010; // 100SPS
writeRegister(DRATE_REG, _DRATE);
delay(200);
LOG_DEBUG("Drate REG OK");
sendDirectCommand(0b11110000); //Offset and self-gain calibration
sendDirectCommand(0b11110000); // Offset and self-gain calibration
LOG_DEBUG("Direct Command OK");
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()
{
while (digitalRead(_DRDY_pin) == HIGH) {}
while (digitalRead(_DRDY_pin) == HIGH)
{
}
}
void ADS1256::waitForHighDRDY()
{
#if F_CPU >= 48000000 //Fast MCUs need this protection to wait until DRDY goes high after a conversion
while (digitalRead(_DRDY_pin) == LOW) {}
#if F_CPU >= 48000000 // Fast MCUs need this protection to wait until DRDY goes high after a conversion
while (digitalRead(_DRDY_pin) == LOW)
{
}
#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)
_spi->transfer(0b00001111); //Send SDATAC to the ADC
CS_HIGH(); //We finished the command sequence, so we switch it back to HIGH
waitForLowDRDY(); // SDATAC should be called after DRDY goes LOW (p35. Figure 33)
_spi->transfer(0b00001111); // Send SDATAC to the ADC
CS_HIGH(); // We finished the command sequence, so we switch it back to HIGH
_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)
{
LOG_DEBUG("SetDrate ADC");
writeRegister(DRATE_REG, drate);
_DRATE = drate;
delayMicroseconds(500);
}
void ADS1256::setMUX(uint8_t mux) //Setting MUX (input channel)
void ADS1256::setMUX(uint8_t mux) // Setting MUX (input channel)
{
LOG_DEBUG("SetMux ADC");
writeRegister(MUX_REG, mux);
_MUX = mux;
//delayMicroseconds(500);
// delayMicroseconds(500);
}
void ADS1256::setPGA(uint8_t pga) //Setting PGA (input voltage range)
void ADS1256::setPGA(uint8_t pga) // Setting PGA (input voltage range)
{
LOG_DEBUG("SetPga ADC");
_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
writeRegister(ADCON_REG, _ADCON);
delayMicroseconds(1000); //Delay to allow the PGA to settle after changing its value
delayMicroseconds(1000); // Delay to allow the PGA to settle after changing its value
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;
//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, 5, 0);
}
else if(clkout == 1)
else if (clkout == 1)
{
//01 (default)
// 01 (default)
bitWrite(_ADCON, 6, 0);
bitWrite(_ADCON, 5, 1);
}
else if(clkout == 2)
else if (clkout == 2)
{
//10
// 10
bitWrite(_ADCON, 6, 1);
bitWrite(_ADCON, 5, 0);
}
else if(clkout == 3)
else if (clkout == 3)
{
//11
// 11
bitWrite(_ADCON, 6, 1);
bitWrite(_ADCON, 5, 1);
}
else{}
else
{
}
writeRegister(ADCON_REG, _ADCON);
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, 3, 0);
}
else if(sdcs == 1)
else if (sdcs == 1)
{
//01
// 01
bitWrite(_ADCON, 4, 0);
bitWrite(_ADCON, 3, 1);
}
else if(sdcs == 2)
else if (sdcs == 2)
{
//10
// 10
bitWrite(_ADCON, 4, 1);
bitWrite(_ADCON, 3, 0);
}
else if(sdcs == 3)
else if (sdcs == 3)
{
//11
// 11
bitWrite(_ADCON, 4, 1);
bitWrite(_ADCON, 3, 1);
}
else{}
else
{
}
writeRegister(ADCON_REG, _ADCON);
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);
//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);
//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);
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);
}
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);
//_STATUS |= B00000000;
}
else if(acal == 1)
else if (acal == 1)
{
//Auto-calibration is enabled
// Auto-calibration is enabled
bitWrite(_STATUS, 2, 1);
//_STATUS |= B00000100;
}
else{}
else
{
}
writeRegister(STATUS_REG, _STATUS);
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);
}
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;
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;
bitWrite(_STATUS, 1, 1);
}
else{}
else
{
}
writeRegister(STATUS_REG, _STATUS);
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);
}
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
//Sets D3-D0 as input or output
// Default: 11100000 - DEC: 224 - Ref: p32 I/O section
// Sets D3-D0 as input or output
uint8_t GPIO_bit7, GPIO_bit6, GPIO_bit5, GPIO_bit4;
//Bit7: DIR3
if(dir3 == 1)
// Bit7: DIR3
if (dir3 == 1)
{
GPIO_bit7 = 1; //D3 is input (default)
GPIO_bit7 = 1; // D3 is input (default)
}
else
{
GPIO_bit7 = 0; //D3 is output
GPIO_bit7 = 0; // D3 is output
}
bitWrite(_GPIO, 7, GPIO_bit7);
//-----------------------------------------------------
//Bit6: DIR2
if(dir2 == 1)
// Bit6: DIR2
if (dir2 == 1)
{
GPIO_bit6 = 1; //D2 is input (default)
GPIO_bit6 = 1; // D2 is input (default)
}
else
{
GPIO_bit6 = 0; //D2 is output
GPIO_bit6 = 0; // D2 is output
}
bitWrite(_GPIO, 6, GPIO_bit6);
//-----------------------------------------------------
//Bit5: DIR1
if(dir1 == 1)
// Bit5: DIR1
if (dir1 == 1)
{
GPIO_bit5 = 1; //D1 is input (default)
GPIO_bit5 = 1; // D1 is input (default)
}
else
{
GPIO_bit5 = 0; //D1 is output
GPIO_bit5 = 0; // D1 is output
}
bitWrite(_GPIO, 5, GPIO_bit5);
//-----------------------------------------------------
//Bit4: DIR0
if(dir0 == 1)
// Bit4: DIR0
if (dir0 == 1)
{
GPIO_bit4 = 1; //D0 is input
GPIO_bit4 = 1; // D0 is input
}
else
{
GPIO_bit4 = 0; //D0 is output (default)
GPIO_bit4 = 0; // D0 is output (default)
}
bitWrite(_GPIO, 4, GPIO_bit4);
//-----------------------------------------------------
@@ -367,17 +393,17 @@ void ADS1256::setGPIO(uint8_t dir0, uint8_t dir1, uint8_t dir2, uint8_t dir3) //
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);
//Sets D3-D0 output values
//It is important that first one must use setGPIO, then writeGPIO
// Sets D3-D0 output values
// It is important that first one must use setGPIO, then writeGPIO
uint8_t GPIO_bit3, GPIO_bit2, GPIO_bit1, GPIO_bit0;
//Bit3: DIR3
if(dir3value == 1)
// Bit3: DIR3
if (dir3value == 1)
{
GPIO_bit3 = 1;
}
@@ -387,8 +413,8 @@ void ADS1256::writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value,
}
bitWrite(_GPIO, 3, GPIO_bit3);
//-----------------------------------------------------
//Bit2: DIR2
if(dir2value == 1)
// Bit2: DIR2
if (dir2value == 1)
{
GPIO_bit2 = 1;
}
@@ -398,8 +424,8 @@ void ADS1256::writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value,
}
bitWrite(_GPIO, 2, GPIO_bit2);
//-----------------------------------------------------
//Bit1: DIR1
if(dir1value == 1)
// Bit1: DIR1
if (dir1value == 1)
{
GPIO_bit1 = 1;
}
@@ -409,8 +435,8 @@ void ADS1256::writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value,
}
bitWrite(_GPIO, 1, GPIO_bit1);
//-----------------------------------------------------
//Bit0: DIR0
if(dir0value == 1)
// Bit0: DIR0
if (dir0value == 1)
{
GPIO_bit0 = 1;
}
@@ -425,13 +451,13 @@ void ADS1256::writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value,
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;
_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_bit2 = bitRead(_GPIO, 2);
GPIO_bit1 = bitRead(_GPIO, 1);
@@ -439,7 +465,7 @@ uint8_t ADS1256::readGPIO(uint8_t gpioPin) //Reading GPIO
delay(100);
switch(gpioPin) //Selecting which value should be returned
switch (gpioPin) // Selecting which value should be returned
{
case 0:
GPIO_return = GPIO_bit0;
@@ -459,66 +485,78 @@ uint8_t ADS1256::readGPIO(uint8_t gpioPin) //Reading GPIO
}
return GPIO_return;
}
void ADS1256::sendDirectCommand(uint8_t directCommand)
{
//Direct commands can be found in the datasheet Page 34, Table 24.
LOG_DEBUG("Direct Command");
// Direct commands can be found in the datasheet Page 34, Table 24.
LOG_DEBUG("Direct Command Begin");
_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);
_spi->transfer(directCommand); //Send Command
_spi->transfer(directCommand); // Send Command
LOG_DEBUG("Transfer OK");
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();
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)
{
waitForLowDRDY();
LOG_DEBUG("DRDY Low");
_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
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();
LOG_DEBUG("CS High");
_spi->endTransaction();
LOG_DEBUG("SPI End");
}
long ADS1256::readRegister(uint8_t registerAddress) //Reading a register
long ADS1256::readRegister(uint8_t registerAddress) // Reading a register
{
waitForLowDRDY();
_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();
_spi->endTransaction();
@@ -526,39 +564,38 @@ long ADS1256::readRegister(uint8_t registerAddress) //Reading a register
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));
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();
_spi->transfer(0b00000001); //Issue RDATA (0000 0001) command
delayMicroseconds(7); //Wait t6 time (~6.51 us) REF: P34, FIG:30.
_spi->transfer(0b00000001); // Issue RDATA (0000 0001) command
delayMicroseconds(7); // Wait t6 time (~6.51 us) REF: P34, FIG:30.
_outputBuffer[0] = _spi->transfer(0); // MSB
_outputBuffer[1] = _spi->transfer(0); // Mid-byte
_outputBuffer[2] = _spi->transfer(0); // LSB
//Shifting and combining the above three items into a single, 24-bit number
_outputValue = ((long)_outputBuffer[0]<<16) | ((long)_outputBuffer[1]<<8) | (_outputBuffer[2]);
// 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 = 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();
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;
_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();
_spi->transfer(0b00000011); //Issue RDATAC (0000 0011)
delayMicroseconds(7); //Wait t6 time (~6.51 us) REF: P34, FIG:30.
_spi->transfer(0b00000011); // Issue RDATAC (0000 0011)
delayMicroseconds(7); // Wait t6 time (~6.51 us) REF: P34, FIG:30.
}
else
{
@@ -569,7 +606,7 @@ long ADS1256::readSingleContinuous() //Reads the recently selected input channel
_outputBuffer[1] = _spi->transfer(0); // Mid-byte
_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);
waitForHighDRDY();
@@ -579,83 +616,84 @@ long ADS1256::readSingleContinuous() //Reads the recently selected input channel
long ADS1256::cycleSingle()
{
if(_isAcquisitionRunning == false)
if (_isAcquisitionRunning == false)
{
_isAcquisitionRunning = true;
_cycle = 0;
_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(0x00);
_spi->transfer(SING_0); //AIN0+AINCOM
_spi->transfer(SING_0); // AIN0+AINCOM
CS_HIGH();
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
{}
{
}
if(_cycle < 8)
if (_cycle < 8)
{
_outputValue = 0;
waitForLowDRDY();
//Step 1. - Updating MUX
// Step 1. - Updating MUX
switch (_cycle)
{
//Channels are written manually
case 0: //Channel 2
updateMUX(SING_1); //AIN1+AINCOM
// Channels are written manually
case 0: // Channel 2
updateMUX(SING_1); // AIN1+AINCOM
break;
case 1: //Channel 3
updateMUX(SING_2); //AIN2+AINCOM
case 1: // Channel 3
updateMUX(SING_2); // AIN2+AINCOM
break;
case 2: //Channel 4
updateMUX(SING_3); //AIN3+AINCOM
case 2: // Channel 4
updateMUX(SING_3); // AIN3+AINCOM
break;
case 3: //Channel 5
updateMUX(SING_4); //AIN4+AINCOM
case 3: // Channel 5
updateMUX(SING_4); // AIN4+AINCOM
break;
case 4: //Channel 6
updateMUX(SING_5); //AIN5+AINCOM
case 4: // Channel 6
updateMUX(SING_5); // AIN5+AINCOM
break;
case 5: //Channel 7
updateMUX(SING_6); //AIN6+AINCOM
case 5: // Channel 7
updateMUX(SING_6); // AIN6+AINCOM
break;
case 6: //Channel 8
updateMUX(SING_7); //AIN7+AINCOM
case 6: // Channel 8
updateMUX(SING_7); // AIN7+AINCOM
break;
case 7: //Channel 1
updateMUX(SING_0); //AIN0+AINCOM
case 7: // Channel 1
updateMUX(SING_0); // AIN0+AINCOM
break;
}
//Step 2.
_spi->transfer(0b11111100); //SYNC
delayMicroseconds(4); //t11 delay 24*tau = 3.125 us //delay should be larger, so we delay by 4 us
_spi->transfer(0b11111111); //WAKEUP
// Step 2.
_spi->transfer(0b11111100); // SYNC
delayMicroseconds(4); // t11 delay 24*tau = 3.125 us //delay should be larger, so we delay by 4 us
_spi->transfer(0b11111111); // WAKEUP
//Step 3.
//Issue RDATA (0000 0001) command
// Step 3.
// Issue RDATA (0000 0001) command
_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[1] = _spi->transfer(0x0F); // Mid-byte
_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);
_cycle++; //Increase cycle - This will move to the next MUX input channel
if(_cycle == 8)
_cycle++; // Increase cycle - This will move to the next MUX input channel
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
}
}
@@ -664,70 +702,71 @@ long ADS1256::cycleSingle()
long ADS1256::cycleDifferential()
{
if(_isAcquisitionRunning == false)
if (_isAcquisitionRunning == false)
{
_cycle = 0;
_isAcquisitionRunning = true;
_spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1));
//Set the AIN0+AIN1 as inputs manually
CS_LOW(); //CS must stay LOW during the entire sequence [Ref: P34, T24]
// Set the AIN0+AIN1 as inputs manually
CS_LOW(); // CS must stay LOW during the entire sequence [Ref: P34, T24]
_spi->transfer(0x50 | 1); // 0x50 = WREG //1 = MUX
_spi->transfer(0x00);
_spi->transfer(DIFF_0_1); //AIN0+AIN1
_spi->transfer(DIFF_0_1); // AIN0+AIN1
CS_HIGH();
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
{}
{
}
if(_cycle < 4)
if (_cycle < 4)
{
_outputValue = 0;
//DRDY has to go low
// DRDY has to go low
waitForLowDRDY();
//Step 1. - Updating MUX
// Step 1. - Updating MUX
switch (_cycle)
{
case 0: //Channel 2
updateMUX(DIFF_2_3); //AIN2+AIN3
case 0: // Channel 2
updateMUX(DIFF_2_3); // AIN2+AIN3
break;
case 1: //Channel 3
updateMUX(DIFF_4_5); //AIN4+AIN5
case 1: // Channel 3
updateMUX(DIFF_4_5); // AIN4+AIN5
break;
case 2: //Channel 4
updateMUX(DIFF_6_7); //AIN6+AIN7
case 2: // Channel 4
updateMUX(DIFF_6_7); // AIN6+AIN7
break;
case 3: //Channel 1
updateMUX(DIFF_0_1); //AIN0+AIN1
case 3: // Channel 1
updateMUX(DIFF_0_1); // AIN0+AIN1
break;
}
_spi->transfer(0b11111100); //SYNC
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(0b11111100); // SYNC
delayMicroseconds(4); // t11 delay 24*tau = 3.125 us //delay should be larger, so we delay by 4 us
_spi->transfer(0b11111111); // WAKEUP
//Step 3.
_spi->transfer(0b00000001); //Issue RDATA (0000 0001) command
delayMicroseconds(7); //Wait t6 time (~6.51 us) REF: P34, FIG:30.
// Step 3.
_spi->transfer(0b00000001); // Issue RDATA (0000 0001) command
delayMicroseconds(7); // Wait t6 time (~6.51 us) REF: P34, FIG:30.
_outputBuffer[0] = _spi->transfer(0); // MSB
_outputBuffer[1] = _spi->transfer(0); // Mid-byte
_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);
_cycle++;
if(_cycle == 4)
if (_cycle == 4)
{
_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
}
}
@@ -736,20 +775,20 @@ long ADS1256::cycleDifferential()
void ADS1256::updateConversionParameter()
{
conversionParameter = ((2.0 * _VREF) / 8388608.0) / (pow(2, _PGA)); //Calculate the "bit to Volts" multiplier
//8388608 = 2^{23} - 1, REF: p23, Table 16.
conversionParameter = ((2.0 * _VREF) / 8388608.0) / (pow(2, _PGA)); // Calculate the "bit to Volts" multiplier
// 8388608 = 2^{23} - 1, REF: p23, Table 16.
}
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(muxValue); //Write the new MUX value
_spi->transfer(muxValue); // Write the new MUX value
}
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);
}
@@ -757,7 +796,7 @@ inline void ADS1256::CS_LOW()
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);
}

View File

@@ -4,6 +4,7 @@ RGBled::RGBled(const uint8_t pin) : m_led(pin)
{
pinMode(m_led, OUTPUT);
writeStatus(RGBled::ERROR);
m_brightness = 1.0f;
}
RGBled::~RGBled()
@@ -11,6 +12,11 @@ RGBled::~RGBled()
pinMode(m_led, INPUT);
}
void RGBled::setBrightness(const float b)
{
m_brightness = b;
}
void RGBled::setStatus(const LedStatus s)
{
if (m_status == s)
@@ -27,6 +33,6 @@ const RGBled::LedStatus RGBled::getSatus(void)
void RGBled::writeStatus(const RGBled::LedStatus s)
{
RGBled::color_u u{.status = s};
rgbLedWrite(m_led, u.color.r, u.color.g, u.color.b);
const RGBled::color_u u{.status = s};
rgbLedWrite(m_led, (uint8_t)(m_brightness*u.color.r), (uint8_t)(m_brightness*u.color.g), (uint8_t)(m_brightness*u.color.b));
}

View File

@@ -50,6 +50,7 @@ public:
RGBled(const uint8_t pin = 48);
~RGBled();
void setBrightness(const float b);
void setStatus(const LedStatus s);
const LedStatus getSatus(void);
@@ -59,5 +60,6 @@ private:
private:
LedStatus m_status = LedStatus::IDLE;
std::mutex m_mutex;
float m_brightness;
const uint8_t m_led;
};

View File

@@ -28,7 +28,7 @@ monitor_port = /dev/ttyACM0
monitor_speed = 921600
build_type = release
build_flags =
-DCORE_DEBUG_LEVEL=1
-DCORE_DEBUG_LEVEL=5
-DARDUINO_USB_CDC_ON_BOOT=0
-DARDUINO_USB_MODE=0
-DCONFIG_ASYNC_TCP_MAX_ACK_TIME=5000
@@ -59,7 +59,7 @@ build_flags =
-O0
-g3
-ggdb3
-DCORE_DEBUG_LEVEL=3
-DCORE_DEBUG_LEVEL=5
-DARDUINO_USB_CDC_ON_BOOT=0
-DARDUINO_USB_MODE=0
-DCONFIG_ASYNC_TCP_MAX_ACK_TIME=5000

View File

@@ -9,7 +9,8 @@
// Device Libraries
#include <ADS1256.h>
#include <AD5292.h>
#include <PCA95x5.h>
#include <extio.h>
#include <Wire.h>
// ADC Channel mapping
#define ADC_CH_PEAK_12P_IN SING_0
@@ -24,23 +25,25 @@
// Device Pointer structs for tasks
struct Devices
{
// Busses
std::unique_ptr<TwoWire> m_i2c = nullptr;
std::unique_ptr<SPIClass> m_spi_a = nullptr;
std::unique_ptr<SPIClass> m_spi_b = nullptr;
// Bus Mutextes
std::mutex m_spi_a_mutex;
std::mutex m_spi_b_mutex;
std::mutex m_i2c_mutex;
// Device Pointers
std::unique_ptr<AD5292> m_pot_a = nullptr;
std::unique_ptr<AD5292> m_pot_b = nullptr;
std::unique_ptr<ADS1256> m_adc_a = nullptr;
std::unique_ptr<ADS1256> m_adc_b = nullptr;
std::unique_ptr<PCA9555> m_expander_a = nullptr;
std::unique_ptr<PCA9555> m_expander_b = nullptr;
std::unique_ptr<PCA9555> m_expander_inputs_ab = nullptr;
std::unique_ptr<ExternalIO> m_ext_io = nullptr;
std::mutex m_spi_a_mutex;
std::mutex m_spi_b_mutex;
std::mutex m_i2c_mutex;
};
// Adc read channel wrapper to selet mux before reading

129
RotaxMonitor/src/extio.cpp Normal file
View File

@@ -0,0 +1,129 @@
#include <extio.h>
// Static interrupt callback
static void onExpanderInterrupt(void *arg)
{
auto cls = (ExternalIO *)(arg);
if (!cls) // invalid args
return;
cls->extReadInterrupt();
}
ExternalIO::ExternalIO(TwoWire &i2c, std::mutex &i2c_mutex, const uint8_t int_pin) : m_i2cMutex(i2c_mutex), m_i2c(i2c), m_intPin(int_pin)
{
std::lock_guard<std::mutex> lock(m_i2cMutex);
// Attach OUT expanders on BUS
m_outMap[EXPANDER_A_OUT_ADDR] = std::make_unique<PCA9555>();
m_outMap[EXPANDER_A_OUT_ADDR]->attach(m_i2c, EXPANDER_A_OUT_ADDR);
m_outMap[EXPANDER_B_OUT_ADDR] = std::make_unique<PCA9555>();
m_outMap[EXPANDER_B_OUT_ADDR]->attach(m_i2c, EXPANDER_B_OUT_ADDR);
for (auto &[a, e] : m_outMap)
{
e->direction(PCA95x5::Direction::OUT_ALL);
e->polarity(PCA95x5::Polarity::ORIGINAL_ALL);
};
// Attach IN Expanders on Bus
m_inMap[EXPANDER_A_IN_ADDR] = std::make_unique<PCA9555>();
m_inMap[EXPANDER_A_IN_ADDR]->attach(m_i2c, EXPANDER_A_IN_ADDR);
m_inMap[EXPANDER_B_IN_ADDR] = std::make_unique<PCA9555>();
m_inMap[EXPANDER_B_IN_ADDR]->attach(m_i2c, EXPANDER_B_IN_ADDR);
for (auto &[a, e] : m_inMap)
{
e->direction(PCA95x5::Direction::IN_ALL);
e->polarity(PCA95x5::Polarity::ORIGINAL_ALL);
m_lastInputState[a] = e->read(); /// initialize input state to collect interrupts
};
}
ExternalIO::~ExternalIO() {
}
void ExternalIO::extDigitalWrite(const uint32_t mappedPin, const bool val)
{
std::lock_guard<std::mutex> lock(m_i2cMutex);
const io_t pa = map2pin(mappedPin);
if (!m_outMap.contains(pa.addr))
{
LOG_ERROR("Undefined IO Expander addr: [", pa.addr, "]");
return;
}
auto &io = m_outMap.at(pa.addr);
if (!io->write(static_cast<PCA95x5::Port::Port>(pa.pin), val ? PCA95x5::Level::H : PCA95x5::Level::L))
{
LOG_ERROR("IO Expander [", pa.addr, "] Unable to WRITE Port [", pa.pin, "] to [", val ? "HIGH" : "LOW");
LOG_ERROR("IO Expander Error [", io->i2c_error(), "]");
}
}
const bool ExternalIO::extDigitalRead(const uint32_t mappedPin)
{
std::lock_guard<std::mutex> lock(m_i2cMutex);
const io_t pa = map2pin(mappedPin);
if (!m_inMap.contains(pa.addr))
{
LOG_ERROR("Undefined IO Expander addr: [", pa.addr, "]");
return false;
}
auto &io = m_inMap.at(pa.addr);
const bool rv = io->read(static_cast<PCA95x5::Port::Port>(pa.pin)) == PCA95x5::Level::H ? true : false; // read value
const uint8_t err = io->i2c_error();
if (err)
{
LOG_ERROR("IO Expander [", pa.addr, "] Unable to READ Port [", pa.pin, "]");
LOG_ERROR("IO Expander Error [", err, "]");
}
return rv;
}
void ExternalIO::extAttachInterrupt(ExtInterruptCb cb)
{
attachInterruptArg(EXPANDER_ALL_INTERRUPT, onExpanderInterrupt, (void *)(this), FALLING);
m_extInterruptCb = cb;
}
void ExternalIO::extDetachInterrupt()
{
detachInterrupt(EXPANDER_ALL_INTERRUPT);
}
void ExternalIO::extReadInterrupt()
{
std::lock_guard<std::mutex> lock(m_i2cMutex);
disableInterrupt(EXPANDER_ALL_INTERRUPT);
// read all registers and collect
IOstate interruptState;
for (auto &[a, e] : m_inMap)
{
interruptState[a] = e->read();
}
m_lastInputState = interruptState; // restore to current values
// compare to last state to see the difference
if (m_extInterruptCb)
{
for (auto &[a, v] : interruptState)
{
if (v)
m_extInterruptCb(stat2map(a, v));
}
}
enableInterrupt(EXPANDER_ALL_INTERRUPT);
}
const ExternalIO::io_t ExternalIO::map2pin(const uint32_t mappedIO)
{
return io_t{
.addr = (uint8_t)((mappedIO >> 16) & (uint8_t)0xFF),
.pin = (uint8_t)(mappedIO && (uint32_t)0xFF),
};
}
const uint32_t ExternalIO::stat2map(const uint8_t addr, const uint16_t stat)
{
if (!stat)
return 0;
return (uint32_t)(addr << 16) | (1UL << __builtin_ctz(stat));
}

49
RotaxMonitor/src/extio.h Normal file
View File

@@ -0,0 +1,49 @@
#pragma once
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
#include <Arduino.h>
#include <DebugLog.h>
#include <PCA95x5.h>
#include <pins.h>
#include <memory>
#include <map>
class ExternalIO
{
using IOptr = std::unique_ptr<PCA9555>;
using IOmap = std::map<const uint8_t, IOptr>;
using IOstate = std::map<const uint8_t, uint16_t>;
using ExtInterruptCb = std::function<void(const uint32_t)>;
struct io_t
{
uint8_t addr;
uint8_t pin;
};
public:
ExternalIO(TwoWire &i2c, std::mutex &i2c_mutex, const uint8_t int_pin);
~ExternalIO();
void extDigitalWrite(const uint32_t mappedPin, const bool val);
const bool extDigitalRead(const uint32_t mappedPin);
void extAttachInterrupt(ExtInterruptCb cb = nullptr);
void extDetachInterrupt();
void extReadInterrupt();
private:
const io_t map2pin(const uint32_t mappedIO);
const uint32_t stat2map(const uint8_t addr, const uint16_t stat);
private:
const uint8_t m_intPin;
IOmap m_inMap;
IOmap m_outMap;
uint8_t m_intPinChanged;
IOstate m_lastInputState;
ExtInterruptCb m_extInterruptCb = nullptr;
std::mutex &m_i2cMutex;
TwoWire &m_i2c;
};

View File

@@ -18,11 +18,12 @@
// Defines to enable channel B
#define CH_B_ENABLE
// #define TEST
// Debug Defines
#define WIFI_SSID "AstroRotaxMonitor"
#define WIFI_PASSWORD "maledettirotax"
#define PSRAM_MAX 4096
#define QUEUE_MAX 256
void setup()
{
@@ -79,49 +80,77 @@ void loop()
{
// global variables
RGBled led;
led.setBrightness(0.025f);
led.setStatus(RGBled::LedStatus::INIT);
std::shared_ptr<Devices> dev = std::make_shared<Devices>();
bool running = true;
std::mutex fs_mutex;
LITTLEFSGuard fsGuard;
//////// INIT SPI PORTS ////////
//////// INIT SPI INTERFACES ////////
bool spiA_ok = true;
bool spiB_ok = true;
// Init 2 SPI interfaces
// SPIClass SPI_A(FSPI);
// spiA_ok = SPI_A.begin(SPI_A_SCK, SPI_A_MISO, SPI_A_MOSI);
// SPI_A.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1
// #ifdef CH_B_ENABLE
// SPIClass SPI_B(HSPI);
// spiB_ok = SPI_B.begin(SPI_B_SCK, SPI_B_MISO, SPI_B_MOSI);
// SPI_B.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1
// #endif
LOG_DEBUG("Init SPI Interfaces");
SPIClass SPI_A(FSPI);
spiA_ok = SPI_A.begin(SPI_A_SCK, SPI_A_MISO, SPI_A_MOSI);
SPI_A.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1
LOG_DEBUG("Init SPI A ok");
#ifdef CH_B_ENABLE
delay(50);
SPIClass SPI_B(HSPI);
spiB_ok = SPI_B.begin(SPI_B_SCK, SPI_B_MISO, SPI_B_MOSI);
SPI_B.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1
LOG_DEBUG("Init SPI B ok");
#endif
if (!spiA_ok || !spiB_ok)
{
LOG_ERROR("Unable to Initialize SPI Busses");
LOG_ERROR("5 seconds to restart...");
//vTaskDelay(pdMS_TO_TICKS(5000));
//esp_restart();
}
//dev->m_spi_a = std::make_unique<SPIClass*>(&SPI_A);
#ifdef CH_B_ENABLE
//dev->m_spi_b = std::make_unique<SPIClass*>(&SPI_B);
#endif
// 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);
LOG_DEBUG("Init ADC A pointer ok");
#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);
LOG_DEBUG("Init ADC B pointer ok");
#endif
// Configure ADCs
dev->m_adc_a->InitializeADC();
dev->m_adc_a->setPGA(PGA_1);
dev->m_adc_a->setDRATE(DRATE_7500SPS);
LOG_DEBUG("Init ADC A params ok");
#ifdef CH_B_ENABLE
dev->m_adc_b->InitializeADC();
dev->m_adc_b->setPGA(PGA_1);
dev->m_adc_b->setDRATE(DRATE_7500SPS);
LOG_DEBUG("Init ADC B params ok");
#endif
LOG_DEBUG("Init SPI OK");
//////// INIT I2C INTERFACES ////////
LOG_DEBUG("Init I2C Interfaces");
bool i2c_ok = true;
i2c_ok = Wire.begin();
if (!i2c_ok)
{
LOG_ERROR("Unable to Initialize I2C Bus");
LOG_ERROR("5 seconds to restart...");
vTaskDelay(pdMS_TO_TICKS(5000));
esp_restart();
}
LOG_DEBUG("Init SPI OK");
// Resources Initialization
std::shared_ptr<Devices> dev = std::make_shared<Devices>();
// dev->m_spi_a = std::make_unique<SPIClass>(SPI_A);
// dev->m_spi_b = std::make_unique<SPIClass>(SPI_B);
// // Init ADC_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);
// 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_a->InitializeADC();
// dev->m_adc_a->setPGA(PGA_1);
// dev->m_adc_a->setDRATE(DRATE_7500SPS);
// dev->m_adc_b->InitializeADC();
// dev->m_adc_b->setPGA(PGA_1);
// dev->m_adc_b->setDRATE(DRATE_7500SPS);
// Init IO Expanders
dev->m_ext_io = std::make_unique<ExternalIO>(Wire, dev->m_i2c_mutex, EXPANDER_ALL_INTERRUPT);
//////// INIT REALTIME TASKS PARAMETERS ////////
const rtIgnitionTask::rtTaskParams taskA_params{
.rt_running = true,
.name = "rtIgnTask_A",
@@ -184,9 +213,10 @@ void loop()
.rt_queue = nullptr,
.dev = dev};
auto task_A = rtIgnitionTask(taskA_params, 4096, 256, CORE_0, fs_mutex);
//////// SPAWN REALTIME TASKS ////////
auto task_A = rtIgnitionTask(taskA_params, PSRAM_MAX, QUEUE_MAX, CORE_0, fs_mutex);
delay(50);
auto task_B = rtIgnitionTask(taskB_params, 4096, 256, CORE_1, fs_mutex);
auto task_B = rtIgnitionTask(taskB_params, PSRAM_MAX, QUEUE_MAX, CORE_1, fs_mutex);
// Ignition A on Core 0
auto ignA_task_success = task_A.getStatus() == rtIgnitionTask::OK ? pdPASS : pdFAIL;
@@ -206,22 +236,26 @@ void loop()
{
led.setStatus(RGBled::LedStatus::ERROR);
LOG_ERROR("Unable to start realtime tasks");
} else
}
else
{
LOG_DEBUG("Real Time Tasks A & B initialized");
led.setStatus(RGBled::LedStatus::OK);
}
AstroWebServer webPage(80, LittleFS); // Initialize webserver and Websocket
//////// SPAWN WEBSERVER and WEBSOCKET ////////
AstroWebServer webPage(80, LittleFS);
ArduinoJson::JsonDocument json_data;
bool data_a, data_b;
task_A.onMessage([&webPage, &json_data, &data_a](ignitionBoxStatusFiltered sts){
task_A.onMessage([&webPage, &json_data, &data_a](ignitionBoxStatusFiltered sts)
{
json_data["box_a"] = sts.toJson();
data_a = true;
});
data_a = true; });
task_B.onMessage([&webPage, &json_data, &data_b](ignitionBoxStatusFiltered sts){
task_B.onMessage([&webPage, &json_data, &data_b](ignitionBoxStatusFiltered sts)
{
json_data["box_b"] = sts.toJson();
data_b = true;
});
data_b = true; });
// task_A.enableSave(true, "ignitionA_test.csv");
// task_B.enableSave(true, "ignitionB_test.csv");
@@ -238,7 +272,8 @@ void loop()
printRunningTasksMod(Serial);
monitor_loop = millis();
}
if ((data_a && data_b) || (this_loop - data_loop > 500)) {
if ((data_a && data_b) || (this_loop - data_loop > 500))
{
webPage.sendWsData(json_data.as<String>());
json_data.clear();
data_a = data_b = false;

View File

@@ -79,86 +79,87 @@
#define SPARK_PIN_B12 1
#define SPARK_PIN_B34 2
// =====================
// PCA9555 I/O EXPANDER BOX_A
// =====================
// +++++++++++++++++++++
// MACRO TO COMBINE PIN NUMBER AND ADDRESS
#define PIN2ADDR(p, a) ((1UL << p) | ((uint32_t)(a) << 16))
// +++++++++++++++++++++
#define EXPANDER_A_ADDR 0x010101
// =====================
// PCA9555 I/O EXPANDER INTERRUPT (Common)
// =====================
#define EXPANDER_ALL_INTERRUPT 17
// =====================
// PCA9555 I/O EXPANDER BOX_A (OUT)
// =====================
#define EXPANDER_A_OUT_ADDR 0xFF
// --- DIGITAL POT CHIP SELECT LINES ---
#define POT_CS_A12 0
#define POT_CS_A34 1
#define POT_CS_A12 PIN2ADDR(0, EXPANDER_A_OUT_ADDR)
#define POT_CS_A34 PIN2ADDR(1, EXPANDER_A_OUT_ADDR)
// --- SOFT START FORCE LINES ---
#define SS_FORCE_A 2
#define SS_INIBHIT_A12 3
#define SS_INHIBIT_A34 4
#define SS_FORCE_A PIN2ADDR(2, EXPANDER_A_OUT_ADDR)
#define SS_INIBHIT_A12 PIN2ADDR(3, EXPANDER_A_OUT_ADDR)
#define SS_INHIBIT_A34 PIN2ADDR(4, EXPANDER_A_OUT_ADDR)
// --- SAMPLE AND HOLD ARM AND DISCHARGE ---
#define SH_DISCH_A12 5
#define SH_DISCH_A34 6
#define SH_ARM_A12 7
#define SH_ARM_A34 8
#define SH_DISCH_A12 PIN2ADDR(5, EXPANDER_A_OUT_ADDR)
#define SH_DISCH_A34 PIN2ADDR(6, EXPANDER_A_OUT_ADDR)
#define SH_ARM_A12 PIN2ADDR(7, EXPANDER_A_OUT_ADDR)
#define SH_ARM_A34 PIN2ADDR(8, EXPANDER_A_OUT_ADDR)
// --- RELAY ---
#define RELAY_IN_A12 9
#define RELAY_OUT_A12 10
#define RELAY_IN_A34 11
#define RELAY_OUT_A34 12
// --- STATUS / BUTTON ---
#define STA_2 13
#define STA_3 14
#define STA_4 15
#define RELAY_IN_A12 PIN2ADDR(9, EXPANDER_A_OUT_ADDR)
#define RELAY_OUT_A12 PIN2ADDR(10, EXPANDER_A_OUT_ADDR)
#define RELAY_IN_A34 PIN2ADDR(11, EXPANDER_A_OUT_ADDR)
#define RELAY_OUT_A34 PIN2ADDR(12, EXPANDER_A_OUT_ADDR)
// =====================
// PCA9555 I/O EXPANDER BOX_B
// PCA9555 I/O EXPANDER BOX_A (IN)
// =====================
#define EXPANDER_A_IN_ADDR 0xFF
#define EXPANDER_B_ADDR 0x101010
#define SS_A12_ON PIN2ADDR(0, EXPANDER_A_IN_ADDR)
#define SS_A12_OFF PIN2ADDR(1, EXPANDER_A_IN_ADDR)
#define SS_A34_ON PIN2ADDR(2, EXPANDER_A_IN_ADDR)
#define SS_A34_OFF PIN2ADDR(3, EXPANDER_A_IN_ADDR)
// =====================
// PCA9555 I/O EXPANDER BOX_B (OUT)
// =====================
#define EXPANDER_B_OUT_ADDR 0xFF
// --- DIGITAL POT CHIP SELECT LINES ---
#define POT_CS_B12 0
#define POT_CS_B34 1
#define POT_CS_B12 PIN2ADDR(0, EXPANDER_B_OUT_ADDR)
#define POT_CS_B34 PIN2ADDR(1, EXPANDER_B_OUT_ADDR)
// --- SOFT START FORCE LINES ---
#define SS_FORCE_B 2
#define SS_INIBHIT_B12 3
#define SS_INHIBIT_B34 4
#define SS_FORCE_B PIN2ADDR(2, EXPANDER_B_OUT_ADDR)
#define SS_INIBHIT_B12 PIN2ADDR(3, EXPANDER_B_OUT_ADDR)
#define SS_INHIBIT_B34 PIN2ADDR(4, EXPANDER_B_OUT_ADDR)
// --- SAMPLE AND HOLD ARM AND DISCHARGE ---
#define SH_DISCH_B12 5
#define SH_DISCH_B34 6
#define SH_ARM_B12 7
#define SH_ARM_B34 8
#define SH_DISCH_B12 PIN2ADDR(5, EXPANDER_B_OUT_ADDR)
#define SH_DISCH_B34 PIN2ADDR(6, EXPANDER_B_OUT_ADDR)
#define SH_ARM_B12 PIN2ADDR(7, EXPANDER_B_OUT_ADDR)
#define SH_ARM_B34 PIN2ADDR(8, EXPANDER_B_OUT_ADDR)
// --- RELAY ---
#define RELAY_IN_B12 9
#define RELAY_OUT_B12 10
#define RELAY_IN_B34 11
#define RELAY_OUT_B34 12
// --- STATUS / BUTTON ---
#define STA_2 13
#define STA_3 14
#define STA_4 15
#define RELAY_IN_B12 PIN2ADDR(9, EXPANDER_B_OUT_ADDR)
#define RELAY_OUT_B12 PIN2ADDR(10, EXPANDER_B_OUT_ADDR)
#define RELAY_IN_B34 PIN2ADDR(11, EXPANDER_B_OUT_ADDR)
#define RELAY_OUT_B34 PIN2ADDR(12, EXPANDER_B_OUT_ADDR)
// =====================
// PCA9555 I/O EXPANDER INPUTS A+B
// PCA9555 I/O EXPANDER BOX_B (IN)
// =====================
#define EXPANDER_B_IN_ADDR 0xFF
#define EXPANDER_IN_ADDR 0x0a0a0a
#define SS_A12_ON
#define SS_A12_OFF
#define SS_A34_ON
#define SS_A34_OFF
#define SS_B12_ON
#define SS_B12_OFF
#define SS_B34_ON
#define SS_B34_OFF
#define SS_B12_ON PIN2ADDR(0, EXPANDER_B_IN_ADDR)
#define SS_B12_OFF PIN2ADDR(1, EXPANDER_B_IN_ADDR)
#define SS_B34_ON PIN2ADDR(2, EXPANDER_B_IN_ADDR)
#define SS_B34_OFF PIN2ADDR(3, EXPANDER_B_IN_ADDR)
// Init Pin Functions
inline void initTriggerPinsInputs()

View File

@@ -39,7 +39,7 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
QueueHandle_t rt_queue = params->rt_queue;
Devices *dev = params->dev.get();
ADS1256 *adc = dev->m_adc_a.get();
PCA9555 *io = dev->m_expander_a.get();
ExternalIO* io = dev->m_ext_io.get();
TaskStatus_t rt_task_info;
vTaskGetInfo(NULL, &rt_task_info, pdFALSE, eInvalid);

View File

@@ -59,19 +59,19 @@ public:
struct rtTaskIOParams
{
const uint32_t expander_addr;
const uint8_t pot_cs_12;
const uint8_t pot_cs_34;
const uint8_t ss_force;
const uint8_t ss_inhibit_12;
const uint8_t ss_inhibit_34;
const uint8_t sh_disch_12;
const uint8_t sh_disch_34;
const uint8_t sh_arm_12;
const uint8_t sh_arm_34;
const uint8_t relay_in_12;
const uint8_t relay_in_34;
const uint8_t relay_out_12;
const uint8_t relay_out_34;
const uint32_t pot_cs_12;
const uint32_t pot_cs_34;
const uint32_t ss_force;
const uint32_t ss_inhibit_12;
const uint32_t ss_inhibit_34;
const uint32_t sh_disch_12;
const uint32_t sh_disch_34;
const uint32_t sh_arm_12;
const uint32_t sh_arm_34;
const uint32_t relay_in_12;
const uint32_t relay_in_34;
const uint32_t relay_out_12;
const uint32_t relay_out_34;
};
// RT task parameters