From 8171cab9cbaa8ede7dc8562eefec278a187c44bd Mon Sep 17 00:00:00 2001 From: Emanuele Trabattoni Date: Tue, 14 Apr 2026 14:16:11 +0200 Subject: [PATCH] adc ok --- RotaxMonitor/lib/ADS1256/ADS1256.cpp | 9 --------- RotaxMonitor/src/devices.h | 2 +- RotaxMonitor/src/main.cpp | 20 ++++++++------------ RotaxMonitor/src/pins.h | 6 ------ RotaxMonitor/src/tasks.cpp | 7 +++++-- 5 files changed, 14 insertions(+), 30 deletions(-) diff --git a/RotaxMonitor/lib/ADS1256/ADS1256.cpp b/RotaxMonitor/lib/ADS1256/ADS1256.cpp index 7a8f51f..2841ae5 100644 --- a/RotaxMonitor/lib/ADS1256/ADS1256.cpp +++ b/RotaxMonitor/lib/ADS1256/ADS1256.cpp @@ -47,7 +47,6 @@ ADS1256::ADS1256(const int8_t DRDY_pin, const int8_t RESET_pin, const int8_t SYN // Initialization void ADS1256::InitializeADC() { - LOG_DEBUG("Initialize ADC"); // Chip select LOW CS_LOW(); @@ -77,27 +76,22 @@ void ADS1256::InitializeADC() _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 writeRegister(MUX_REG, _MUX); delay(200); - LOG_DEBUG("Mux REG OK"); _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 writeRegister(DRATE_REG, _DRATE); delay(200); - LOG_DEBUG("Drate REG OK"); 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 @@ -131,7 +125,6 @@ void ADS1256::stopConversion() // Sending SDATAC to stop the continuous conversi void ADS1256::setDRATE(uint8_t drate) // Setting DRATE (sampling frequency) { - LOG_DEBUG("SetDrate ADC"); writeRegister(DRATE_REG, drate); _DRATE = drate; delayMicroseconds(500); @@ -139,7 +132,6 @@ void ADS1256::setDRATE(uint8_t drate) // Setting DRATE (sampling frequency) void ADS1256::setMUX(uint8_t mux) // Setting MUX (input channel) { - LOG_DEBUG("SetMux ADC"); writeRegister(MUX_REG, mux); _MUX = mux; // delayMicroseconds(500); @@ -147,7 +139,6 @@ void ADS1256::setMUX(uint8_t mux) // Setting MUX (input channel) 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 diff --git a/RotaxMonitor/src/devices.h b/RotaxMonitor/src/devices.h index 70a8b7d..a9d6f25 100644 --- a/RotaxMonitor/src/devices.h +++ b/RotaxMonitor/src/devices.h @@ -51,7 +51,7 @@ inline float adcReadChannel(ADS1256 *adc, const uint8_t ch) { adc->setMUX(ch); // scarta 3 conversioni - for (int i = 0; i < 3; i++) + for (int i = 0; i < 5; i++) { adc->readSingle(); } diff --git a/RotaxMonitor/src/main.cpp b/RotaxMonitor/src/main.cpp index 68afb1f..386a564 100644 --- a/RotaxMonitor/src/main.cpp +++ b/RotaxMonitor/src/main.cpp @@ -17,7 +17,7 @@ #include // Defines to enable channel B -#define CH_B_ENABLE +// #define CH_B_ENABLE // Debug Defines #define WIFI_SSID "AstroRotaxMonitor" @@ -32,7 +32,7 @@ void setup() // Setup Logger LOG_ATTACH_SERIAL(Serial); - LOG_SET_LEVEL(DebugLogLevel::LVL_DEBUG); + LOG_SET_LEVEL(DebugLogLevel::LVL_INFO); // Print Processor Info LOG_DEBUG("ESP32 Chip:", ESP.getChipModel()); @@ -108,37 +108,33 @@ void loop() { LOG_ERROR("Unable to Initialize SPI Busses"); LOG_ERROR("5 seconds to restart..."); - //vTaskDelay(pdMS_TO_TICKS(5000)); - //esp_restart(); + vTaskDelay(pdMS_TO_TICKS(5000)); + esp_restart(); } - //dev->m_spi_a = std::make_unique(&SPI_A); + dev->m_spi_a.reset(&SPI_A); #ifdef CH_B_ENABLE - //dev->m_spi_b = std::make_unique(&SPI_B); + dev->m_spi_b.reset(&SPI_B); #endif // Init ADCs dev->m_adc_a = std::make_unique(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(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(); + i2c_ok = Wire.begin(SDA, SCL, 100000); if (!i2c_ok) { LOG_ERROR("Unable to Initialize I2C Bus"); @@ -148,7 +144,7 @@ void loop() } // Init IO Expanders - dev->m_ext_io = std::make_unique(Wire, dev->m_i2c_mutex, EXPANDER_ALL_INTERRUPT); + // dev->m_ext_io = std::make_unique(Wire, dev->m_i2c_mutex, EXPANDER_ALL_INTERRUPT); //////// INIT REALTIME TASKS PARAMETERS //////// const rtIgnitionTask::rtTaskParams taskA_params{ diff --git a/RotaxMonitor/src/pins.h b/RotaxMonitor/src/pins.h index 562c76f..8ed24f7 100644 --- a/RotaxMonitor/src/pins.h +++ b/RotaxMonitor/src/pins.h @@ -53,12 +53,6 @@ #define ADC_B_CS 21 #define ADC_B_DRDY 47 -// ===================== -// DIGITAL POT -// ===================== -#define POT_A_CS 18 -#define POT_B_CS 35 - // ===================== // TRIGGER INPUT INTERRUPTS // ===================== diff --git a/RotaxMonitor/src/tasks.cpp b/RotaxMonitor/src/tasks.cpp index 9c8416b..dcf3587 100644 --- a/RotaxMonitor/src/tasks.cpp +++ b/RotaxMonitor/src/tasks.cpp @@ -1,6 +1,7 @@ #include "tasks.h" #include #include +#include //// GLOBAL STATIC FUNCTIONS @@ -38,7 +39,8 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters) const rtTaskIOParams rt_rst = params->rt_io; // copy to avoid external override QueueHandle_t rt_queue = params->rt_queue; Devices *dev = params->dev.get(); - ADS1256 *adc = dev->m_adc_a.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; ExternalIO* io = dev->m_ext_io.get(); TaskStatus_t rt_task_info; @@ -234,6 +236,7 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters) // read adc channels: pickup12, out12 [ pos + neg ] if (adc) // read only if adc initialized { + std::lock_guard lock (spi_mutex); uint32_t start_adc_read = esp_timer_get_time(); // from peak detector circuits ign_box_sts.coils12.peak_p_in = adcReadChannel(adc, ADC_CH_PEAK_12P_IN); @@ -256,7 +259,7 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters) // [TODO] code to reset sample and hold and arm trigger level detectors } else - vTaskDelay(pdMS_TO_TICKS(1)); + vTaskDelay(pdMS_TO_TICKS(2)); // send essage to main loop with ignition info, by copy so local static variable is ok if (rt_queue)