Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 59e4e955ff |
Binary file not shown.
@@ -3,4 +3,3 @@
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
||||
unpacked_fs
|
||||
|
||||
Vendored
+2
-1
@@ -1,7 +1,8 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"Jason2866.esp-decoder",
|
||||
"pioarduino.pioarduino-ide"
|
||||
"pioarduino.pioarduino-ide",
|
||||
"platformio.platformio-ide"
|
||||
],
|
||||
"unwantedRecommendations": [
|
||||
"ms-vscode.cpptools-extension-pack"
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1,212 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Astro Rotax Monitor</title>
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header class="page-header">
|
||||
<div class="header-content">
|
||||
<img src="logo_astro_dev.svg" alt="Astro Tecnologie" class="logo">
|
||||
</div>
|
||||
<div>
|
||||
<h1>Rotax Ignition Box Monitor</h1>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<!-- TAB BUTTONS -->
|
||||
<div class="tabs">
|
||||
<button class="tab-button active" onclick="openTab('tab1')">Monitor</button>
|
||||
<button class="tab-button" onclick="openTab('tab2')">Grafico</button>
|
||||
</div>
|
||||
|
||||
<!-- TAB 1 (contenuto attuale) -->
|
||||
<div id="tab1" class="tab-content active">
|
||||
|
||||
<div id="loadingIndicator" class="loading-indicator">
|
||||
<span class="spinner"></span> Waiting for data...
|
||||
</div>
|
||||
|
||||
<div class="tables-container">
|
||||
<div class="box">
|
||||
<h2>Box_A</h2>
|
||||
<div class="box-data">
|
||||
<p><strong>Timestamp:</strong> <span id="a_timestamp">-</span></p>
|
||||
<p><strong>Data Valid:</strong> <span id="a_datavalid">-</span></p>
|
||||
<p><strong>Generator voltage:</strong> <span id="a_volts_gen">-</span></p>
|
||||
<p><strong>ADC read time:</strong> <span id="a_adc_read_time">-</span></p>
|
||||
<p><strong>Queue errors:</strong> <span id="a_n_queue_errors">-</span></p>
|
||||
</div>
|
||||
<div class="rpm-highlight">
|
||||
<strong>Engine RPM:</strong> <span id="a_eng_rpm">-</span>
|
||||
</div>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Property</th>
|
||||
<th>Pickup 12</th>
|
||||
<th>Pickup 34</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>Spark delay</td>
|
||||
<td id="a_coils12_spark_delay">-</td>
|
||||
<td id="a_coils34_spark_delay">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Spark status</td>
|
||||
<td id="a_coils12_spark_status">-</td>
|
||||
<td id="a_coils34_spark_status">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Soft start status</td>
|
||||
<td id="a_coils12_sstart_status">-</td>
|
||||
<td id="a_coils34_sstart_status">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak P in</td>
|
||||
<td id="a_coils12_peak_p_in">-</td>
|
||||
<td id="a_coils34_peak_p_in">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak N in</td>
|
||||
<td id="a_coils12_peak_n_in">-</td>
|
||||
<td id="a_coils34_peak_n_in">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak P out</td>
|
||||
<td id="a_coils12_peak_p_out">-</td>
|
||||
<td id="a_coils34_peak_p_out">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak N out</td>
|
||||
<td id="a_coils12_peak_n_out">-</td>
|
||||
<td id="a_coils34_peak_n_out">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Level spark</td>
|
||||
<td id="a_coils12_level_spark">-</td>
|
||||
<td id="a_coils34_level_spark">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Spark Events</td>
|
||||
<td id="a_coils12_n_events">-</td>
|
||||
<td id="a_coils34_n_events">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Missed Events</td>
|
||||
<td id="a_coils12_n_missed_firing">-</td>
|
||||
<td id="a_coils34_n_missed_firing">-</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div class="box">
|
||||
<h2>Box_B</h2>
|
||||
<div class="box-data">
|
||||
<p><strong>Timestamp:</strong> <span id="b_timestamp">-</span></p>
|
||||
<p><strong>Data Valid:</strong> <span id="b_datavalid">-</span></p>
|
||||
<p><strong>Generator voltage:</strong> <span id="b_volts_gen">-</span></p>
|
||||
<p><strong>ADC read time:</strong> <span id="b_adc_read_time">-</span></p>
|
||||
<p><strong>Queue errors:</strong> <span id="b_n_queue_errors">-</span></p>
|
||||
</div>
|
||||
<div class="rpm-highlight">
|
||||
<strong>Engine RPM:</strong> <span id="b_eng_rpm">-</span>
|
||||
</div>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Property</th>
|
||||
<th>Pickup 12</th>
|
||||
<th>Pickup 34</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>Spark delay</td>
|
||||
<td id="b_coils12_spark_delay">-</td>
|
||||
<td id="b_coils34_spark_delay">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Spark status</td>
|
||||
<td id="b_coils12_spark_status">-</td>
|
||||
<td id="b_coils34_spark_status">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Soft start status</td>
|
||||
<td id="b_coils12_sstart_status">-</td>
|
||||
<td id="b_coils34_sstart_status">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak P in</td>
|
||||
<td id="b_coils12_peak_p_in">-</td>
|
||||
<td id="b_coils34_peak_p_in">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak N in</td>
|
||||
<td id="b_coils12_peak_n_in">-</td>
|
||||
<td id="b_coils34_peak_n_in">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak P out</td>
|
||||
<td id="b_coils12_peak_p_out">-</td>
|
||||
<td id="b_coils34_peak_p_out">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak N out</td>
|
||||
<td id="b_coils12_peak_n_out">-</td>
|
||||
<td id="b_coils34_peak_n_out">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Level spark</td>
|
||||
<td id="b_coils12_level_spark">-</td>
|
||||
<td id="b_coils34_level_spark">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Spark Events</td>
|
||||
<td id="b_coils12_n_events">-</td>
|
||||
<td id="b_coils34_n_events">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Missed Events</td>
|
||||
<td id="b_coils12_n_missed_firing">-</td>
|
||||
<td id="b_coils34_n_missed_firing">-</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div> <!-- END TAB1 -->
|
||||
|
||||
<!-- TAB 2 (grafico) -->
|
||||
<div id="tab2" class="tab-content">
|
||||
<div class="chart-container">
|
||||
<h3>Box A</h3>
|
||||
<canvas id="chartA" height="100"></canvas>
|
||||
</div>
|
||||
|
||||
<div class="chart-container">
|
||||
<h3>Box B</h3>
|
||||
<canvas id="chartB" height="100"></canvas>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
<div class="upload-section">
|
||||
<h3>Upload file to Flash</h3>
|
||||
<p>Select a file and upload it to Flash.</p>
|
||||
<input type="file" id="littlefsFile">
|
||||
<button onclick="uploadLittleFS()">Upload</button>
|
||||
<div id="uploadStatus" class="upload-status">No file uploaded yet.</div>
|
||||
</div>
|
||||
|
||||
<script src="chart.js"></script>
|
||||
<script src="script.js"></script>
|
||||
|
||||
</html>
|
||||
@@ -1,215 +0,0 @@
|
||||
// ADS1256 header file
|
||||
/*
|
||||
Name: ADS1256.h
|
||||
Created: 2022/07/14
|
||||
Author: Curious Scientist
|
||||
Editor: Notepad++
|
||||
Comment: Visit https://curiousscientist.tech/blog/ADS1256-custom-library
|
||||
Special thanks to
|
||||
Abraão Queiroz for spending time on the code and suggesting corrections for ESP32 microcontrollers
|
||||
Benjamin Pelletier for pointing out and fixing an issue around the handling of the DRDY signal
|
||||
*/
|
||||
|
||||
#ifndef _ADS1256_h
|
||||
#define _ADS1256_h
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
// SPI Frequency
|
||||
#define SPI_FREQ 1920000
|
||||
|
||||
// Differential inputs
|
||||
#define DIFF_0_1 0b00000001 // A0 + A1 as differential input
|
||||
#define DIFF_2_3 0b00100011 // A2 + A3 as differential input
|
||||
#define DIFF_4_5 0b01000101 // A4 + A5 as differential input
|
||||
#define DIFF_6_7 0b01100111 // A6 + A7 as differential input
|
||||
|
||||
// Single-ended inputs
|
||||
#define SING_0 0b00001111 // A0 + GND (common) as single-ended input
|
||||
#define SING_1 0b00011111 // A1 + GND (common) as single-ended input
|
||||
#define SING_2 0b00101111 // A2 + GND (common) as single-ended input
|
||||
#define SING_3 0b00111111 // A3 + GND (common) as single-ended input
|
||||
#define SING_4 0b01001111 // A4 + GND (common) as single-ended input
|
||||
#define SING_5 0b01011111 // A5 + GND (common) as single-ended input
|
||||
#define SING_6 0b01101111 // A6 + GND (common) as single-ended input
|
||||
#define SING_7 0b01111111 // A7 + GND (common) as single-ended input
|
||||
|
||||
// PGA settings //Input voltage range
|
||||
#define PGA_1 0b00000000 // ± 5 V
|
||||
#define PGA_2 0b00000001 // ± 2.5 V
|
||||
#define PGA_4 0b00000010 // ± 1.25 V
|
||||
#define PGA_8 0b00000011 // ± 625 mV
|
||||
#define PGA_16 0b00000100 // ± 312.5 mV
|
||||
#define PGA_32 0b00000101 //+ 156.25 mV
|
||||
#define PGA_64 0b00000110 // ± 78.125 mV
|
||||
|
||||
// Datarate //DEC
|
||||
#define DRATE_30000SPS 0b11110000 // 240
|
||||
#define DRATE_15000SPS 0b11100000 // 224
|
||||
#define DRATE_7500SPS 0b11010000 // 208
|
||||
#define DRATE_3750SPS 0b11000000 // 192
|
||||
#define DRATE_2000SPS 0b10110000 // 176
|
||||
#define DRATE_1000SPS 0b10100001 // 161
|
||||
#define DRATE_500SPS 0b10010010 // 146
|
||||
#define DRATE_100SPS 0b10000010 // 130
|
||||
#define DRATE_60SPS 0b01110010 // 114
|
||||
#define DRATE_50SPS 0b01100011 // 99
|
||||
#define DRATE_30SPS 0b01010011 // 83
|
||||
#define DRATE_25SPS 0b01000011 // 67
|
||||
#define DRATE_15SPS 0b00110011 // 51
|
||||
#define DRATE_10SPS 0b00100011 // 35
|
||||
#define DRATE_5SPS 0b00010011 // 19
|
||||
#define DRATE_2SPS 0b00000011 // 3
|
||||
|
||||
// Status register
|
||||
#define BITORDER_MSB 0
|
||||
#define BITORDER_LSB 1
|
||||
#define ACAL_DISABLED 0
|
||||
#define ACAL_ENABLED 1
|
||||
#define BUFFER_DISABLED 0
|
||||
#define BUFFER_ENABLED 1
|
||||
|
||||
// Register addresses
|
||||
#define STATUS_REG 0x00
|
||||
#define MUX_REG 0x01
|
||||
#define ADCON_REG 0x02
|
||||
#define DRATE_REG 0x03
|
||||
#define IO_REG 0x04
|
||||
#define OFC0_REG 0x05
|
||||
#define OFC1_REG 0x06
|
||||
#define OFC2_REG 0x07
|
||||
#define FSC0_REG 0x08
|
||||
#define FSC1_REG 0x09
|
||||
#define FSC2_REG 0x0A
|
||||
|
||||
// Command definitions
|
||||
#define WAKEUP 0b00000000
|
||||
#define RDATA 0b00000001
|
||||
#define RDATAC 0b00000011
|
||||
#define SDATAC 0b00001111
|
||||
#define RREG 0b00010000
|
||||
#define WREG 0b01010000
|
||||
#define SELFCAL 0b11110000
|
||||
#define SELFOCAL 0b11110001
|
||||
#define SELFGCAL 0b11110010
|
||||
#define SYSOCAL 0b11110011
|
||||
#define SYSGCAL 0b11110100
|
||||
#define SYNC 0b11111100
|
||||
#define STANDBY 0b11111101
|
||||
#define RESET 0b11111110
|
||||
//----------------------------------------------------------------
|
||||
|
||||
class ADS1256
|
||||
{
|
||||
public:
|
||||
static constexpr int8_t PIN_UNUSED = -1;
|
||||
|
||||
// Constructor
|
||||
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);
|
||||
~ADS1256()
|
||||
{
|
||||
vSemaphoreDelete(m_drdyHigh);
|
||||
vSemaphoreDelete(m_drdyLow);
|
||||
}
|
||||
|
||||
// Initializing function
|
||||
void InitializeADC();
|
||||
// ADS1256(int drate, int pga, int byteOrder, bool bufen);
|
||||
|
||||
// Read a register
|
||||
long readRegister(uint8_t registerAddress);
|
||||
|
||||
// Write a register
|
||||
void writeRegister(uint8_t registerAddress, uint8_t registerValueToWrite);
|
||||
|
||||
// Individual methods
|
||||
void setDRATE(uint8_t drate);
|
||||
void setPGA(uint8_t pga);
|
||||
uint8_t getPGA();
|
||||
void setMUX(uint8_t mux);
|
||||
void setByteOrder(uint8_t byteOrder);
|
||||
uint8_t getByteOrder();
|
||||
void setBuffer(uint8_t bufen);
|
||||
uint8_t getBuffer();
|
||||
void setAutoCal(uint8_t acal);
|
||||
uint8_t getAutoCal();
|
||||
void setGPIO(uint8_t dir0, uint8_t dir1, uint8_t dir2, uint8_t dir3);
|
||||
void writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value, uint8_t dir3value);
|
||||
uint8_t readGPIO(uint8_t gpioPin);
|
||||
void setCLKOUT(uint8_t clkout);
|
||||
void setSDCS(uint8_t sdcs);
|
||||
void sendDirectCommand(uint8_t directCommand);
|
||||
|
||||
// Get a single conversion
|
||||
long readSingle();
|
||||
|
||||
// Single input continuous reading
|
||||
long readSingleContinuous();
|
||||
|
||||
// Cycling through the single-ended inputs
|
||||
long cycleSingle(); // Ax + COM
|
||||
|
||||
// Cycling through the differential inputs
|
||||
long cycleDifferential(); // Ax + Ay
|
||||
|
||||
// Converts the reading into a voltage value
|
||||
float convertToVoltage(int32_t rawData);
|
||||
|
||||
// Stop AD
|
||||
void stopConversion();
|
||||
|
||||
// functions for callback
|
||||
inline uint8_t getDRDYpin()
|
||||
{
|
||||
return m_DRDY_pin;
|
||||
}
|
||||
|
||||
SemaphoreHandle_t getDRDYsemaphoreHigh()
|
||||
{
|
||||
return m_drdyHigh;
|
||||
}
|
||||
|
||||
SemaphoreHandle_t getDRDYsemaphoreLow()
|
||||
{
|
||||
return m_drdyLow;
|
||||
}
|
||||
|
||||
private:
|
||||
SPIClass *_spi; // Pointer to an SPIClass object
|
||||
|
||||
void waitForLowDRDY(); // Block until DRDY is low
|
||||
void waitForHighDRDY(); // Block until DRDY is high
|
||||
void updateMUX(uint8_t muxValue);
|
||||
inline void CS_LOW();
|
||||
inline void CS_HIGH();
|
||||
|
||||
void updateConversionParameter(); // Refresh the conversion parameter based on the PGA
|
||||
|
||||
float m_VREF = 0; // Value of the reference voltage
|
||||
float m_conversionParameter = 0; // PGA-dependent multiplier
|
||||
// Pins
|
||||
int8_t m_DRDY_pin; // Pin assigned for DRDY
|
||||
int8_t m_RESET_pin; // Pin assigned for RESET
|
||||
int8_t m_SYNC_pin; // Pin assigned for SYNC
|
||||
int8_t m_CS_pin; // Pin assigned for CS
|
||||
|
||||
// Register values
|
||||
uint8_t m_DRATE; // Value of the DRATE register
|
||||
uint8_t m_ADCON; // Value of the ADCON register
|
||||
uint8_t m_MUX; // Value of the MUX register
|
||||
uint8_t m_PGA; // Value of the PGA (within ADCON)
|
||||
uint8_t m_GPIO; // Value of the GPIO register
|
||||
uint8_t m_STATUS; // Value of the status register
|
||||
uint8_t m_GPIOvalue; // GPIO value
|
||||
uint8_t m_ByteOrder; // Byte order
|
||||
|
||||
uint8_t m_outputBuffer[3]; // 3-byte (24-bit) buffer for the fast acquisition - Single-channel, continuous
|
||||
int32_t m_outputValue; // Combined value of the m_outputBuffer[3]
|
||||
bool m_isAcquisitionRunning; // bool that keeps track of the acquisition (running or not)
|
||||
uint8_t m_cycle; // Tracks the cycles as the MUX is cycling through the input channels
|
||||
|
||||
SemaphoreHandle_t m_drdyHigh;
|
||||
SemaphoreHandle_t m_drdyLow;
|
||||
};
|
||||
#endif
|
||||
@@ -1,38 +0,0 @@
|
||||
#include <led.h>
|
||||
|
||||
RGBled::RGBled(const uint8_t pin) : m_led(pin)
|
||||
{
|
||||
pinMode(m_led, OUTPUT);
|
||||
writeStatus(RGBled::ERROR);
|
||||
m_brightness = 1.0f;
|
||||
}
|
||||
|
||||
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)
|
||||
return;
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_status = s;
|
||||
writeStatus(m_status);
|
||||
}
|
||||
|
||||
const RGBled::LedStatus RGBled::getSatus(void)
|
||||
{
|
||||
return m_status;
|
||||
}
|
||||
|
||||
void RGBled::writeStatus(const RGBled::LedStatus s)
|
||||
{
|
||||
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));
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
// System Inlcudes
|
||||
#include <Arduino.h>
|
||||
#include <mutex>
|
||||
|
||||
#define RED 0x00FF00
|
||||
#define GREEN 0xFF0000
|
||||
#define BLUE 0x0000FF
|
||||
#define WHITE 0xFFFFFF
|
||||
#define YELLOW 0xFFFF00
|
||||
#define CYAN 0xFF00FF
|
||||
#define MAGENTA 0x00FFFF
|
||||
#define ORANGE 0xA5FF00
|
||||
#define PURPLE 0x008080
|
||||
#define PINK 0x69FFB4
|
||||
#define LIME 0xCD3232
|
||||
#define SKY_BLUE 0xCE87EB
|
||||
#define GOLD 0xD7FF00
|
||||
#define TURQUOISE 0xE040D0
|
||||
#define INDIGO 0x004B82
|
||||
#define GRAY 0x808080
|
||||
|
||||
class RGBled
|
||||
{
|
||||
public:
|
||||
enum LedStatus
|
||||
{
|
||||
OK = GREEN,
|
||||
ERROR = RED,
|
||||
INIT = YELLOW,
|
||||
DATA_A = CYAN,
|
||||
DATA_B = MAGENTA,
|
||||
DATA_ALL = ORANGE,
|
||||
IDLE = GRAY
|
||||
};
|
||||
|
||||
struct color_t
|
||||
{
|
||||
uint8_t a, r, g, b;
|
||||
};
|
||||
|
||||
union color_u
|
||||
{
|
||||
uint32_t status;
|
||||
color_t color;
|
||||
};
|
||||
|
||||
public:
|
||||
RGBled(const uint8_t pin = 48);
|
||||
~RGBled();
|
||||
|
||||
void setBrightness(const float b);
|
||||
void setStatus(const LedStatus s);
|
||||
const LedStatus getSatus(void);
|
||||
|
||||
private:
|
||||
void writeStatus(const LedStatus s);
|
||||
|
||||
private:
|
||||
LedStatus m_status = LedStatus::IDLE;
|
||||
std::mutex m_mutex;
|
||||
float m_brightness;
|
||||
const uint8_t m_led;
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
# Name, Type, SubType, Offset, Size, Flags
|
||||
nvs, data, nvs, 0x9000, 0x5000,
|
||||
otadata, data, ota, 0xe000, 0x2000,
|
||||
app0, app, ota_0, 0x10000, 0x700000,
|
||||
app1, app, ota_1, 0x710000,0x700000,
|
||||
spiffs, data, spiffs, 0xE10000,0x1F0000,
|
||||
|
@@ -1,6 +0,0 @@
|
||||
# ESP32 Partition Table
|
||||
# Name, Type, SubType, Offset, Size
|
||||
nvs, data, nvs, 0x9000, 0x4000
|
||||
phy_init, data, phy, 0xd000, 0x1000
|
||||
factory, app, factory, 0x10000, 0x300000
|
||||
littlefs, data, littlefs, 0x310000, 0xCF0000
|
||||
|
@@ -1,6 +0,0 @@
|
||||
# ESP32 Partition Table
|
||||
# Name, Type, SubType, Offset, Size
|
||||
nvs, data, nvs, 0x9000, 0x4000
|
||||
phy_init, data, phy, 0xd000, 0x1000
|
||||
factory, app, factory, 0x10000, 0x300000
|
||||
spiffs, data, spiffs, 0x310000, 0xCF0000
|
||||
|
+16
-10
@@ -10,8 +10,6 @@
|
||||
|
||||
[env:esp32-s3-devkitc1-n16r8]
|
||||
board = esp32-s3-devkitc1-n16r8
|
||||
board_build.partitions = partitions/no_ota_10mb_littlefs.csv
|
||||
board_build.filesystem = littlefs
|
||||
platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
@@ -21,10 +19,14 @@ lib_deps =
|
||||
me-no-dev/AsyncTCP@^3.3.2
|
||||
me-no-dev/ESPAsyncWebServer@^3.6.0
|
||||
upload_protocol = esptool
|
||||
upload_port = /dev/ttyACM1
|
||||
upload_port = /dev/ttyACM2
|
||||
upload_speed = 921600
|
||||
monitor_port = /dev/ttyACM0
|
||||
monitor_speed = 921600
|
||||
|
||||
;Monitor configuration
|
||||
monitor_speed = 115200
|
||||
monitor_port = /dev/ttyACM2
|
||||
|
||||
; Build configuration
|
||||
build_type = release
|
||||
build_flags =
|
||||
-DCORE_DEBUG_LEVEL=3
|
||||
@@ -38,19 +40,23 @@ build_flags =
|
||||
|
||||
[env:esp32-s3-devkitc1-n16r8-debug]
|
||||
board = ${env:esp32-s3-devkitc1-n16r8.board}
|
||||
board_build.partitions = ${env:esp32-s3-devkitc1-n16r8.board_build.partitions}
|
||||
board_build.filesystem = ${env:esp32-s3-devkitc1-n16r8.board_build.filesystem}
|
||||
platform = ${env:esp32-s3-devkitc1-n16r8.platform}
|
||||
framework = ${env:esp32-s3-devkitc1-n16r8.framework}
|
||||
lib_deps =
|
||||
${env:esp32-s3-devkitc1-n16r8.lib_deps}
|
||||
upload_protocol = esptool
|
||||
upload_port = /dev/ttyACM1
|
||||
upload_port = /dev/ttyACM2
|
||||
upload_speed = 921600
|
||||
monitor_port = /dev/ttyACM0
|
||||
monitor_speed = 921600
|
||||
|
||||
;Monitor configuration
|
||||
monitor_speed = 115200
|
||||
monitor_port = /dev/ttyACM2
|
||||
|
||||
; Debug configuration
|
||||
debug_tool = esp-builtin
|
||||
debug_speed = 15000
|
||||
|
||||
; Build configuration
|
||||
build_type = debug
|
||||
build_flags =
|
||||
-O0
|
||||
|
||||
@@ -0,0 +1,766 @@
|
||||
//ADS1256 cpp file
|
||||
/*
|
||||
Name: ADS1256.cpp
|
||||
Created: 2022/07/14
|
||||
Author: Curious Scientist
|
||||
Editor: Notepad++
|
||||
Comment: Visit https://curiousscientist.tech/blog/ADS1256-custom-library
|
||||
Special thanks to:
|
||||
Abraão Queiroz for spending time on the code and suggesting corrections for ESP32 microcontrollers
|
||||
Benjamin Pelletier for pointing out and fixing an issue around the handling of the DRDY signal
|
||||
RadoMmm for suggesting an improvement on the ADC-to-Volts conversion
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "ADS1256.h"
|
||||
#include "SPI.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),
|
||||
_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)
|
||||
{
|
||||
pinMode(_RESET_pin, OUTPUT);
|
||||
}
|
||||
|
||||
if(SYNC_pin != PIN_UNUSED)
|
||||
{
|
||||
pinMode(_SYNC_pin, OUTPUT);
|
||||
}
|
||||
|
||||
if(CS_pin != PIN_UNUSED)
|
||||
{
|
||||
pinMode(_CS_pin, OUTPUT);
|
||||
}
|
||||
|
||||
updateConversionParameter();
|
||||
}
|
||||
|
||||
//Initialization
|
||||
void ADS1256::InitializeADC()
|
||||
{
|
||||
//Chip select LOW
|
||||
CS_LOW();
|
||||
|
||||
//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
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
//Sync pin is also treated if it is defined
|
||||
if(_SYNC_pin != PIN_UNUSED)
|
||||
{
|
||||
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();
|
||||
#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
|
||||
delay(200);
|
||||
|
||||
_STATUS = 0b00110110; //BUFEN and ACAL enabled, Order is MSB, rest is read only
|
||||
writeRegister(STATUS_REG, _STATUS);
|
||||
delay(200);
|
||||
|
||||
_MUX = 0b00000001; //MUX AIN0+AIN1
|
||||
writeRegister(MUX_REG, _MUX);
|
||||
delay(200);
|
||||
|
||||
_ADCON = 0b00000000; //ADCON - CLK: OFF, SDCS: OFF, PGA = 0 (+/- 5 V)
|
||||
writeRegister(ADCON_REG, _ADCON);
|
||||
delay(200);
|
||||
|
||||
updateConversionParameter();
|
||||
|
||||
_DRATE = 0b10000010; //100SPS
|
||||
writeRegister(DRATE_REG, _DRATE);
|
||||
delay(200);
|
||||
|
||||
sendDirectCommand(0b11110000); //Offset and self-gain calibration
|
||||
delay(200);
|
||||
|
||||
_isAcquisitionRunning = false; //MCU will be waiting to start a continuous acquisition
|
||||
}
|
||||
|
||||
void ADS1256::waitForLowDRDY()
|
||||
{
|
||||
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) {}
|
||||
#endif
|
||||
}
|
||||
|
||||
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
|
||||
_spi->endTransaction();
|
||||
|
||||
_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)
|
||||
{
|
||||
writeRegister(DRATE_REG, drate);
|
||||
_DRATE = drate;
|
||||
delay(200);
|
||||
}
|
||||
|
||||
void ADS1256::setMUX(uint8_t mux) //Setting MUX (input channel)
|
||||
{
|
||||
writeRegister(MUX_REG, mux);
|
||||
_MUX = mux;
|
||||
delay(200);
|
||||
}
|
||||
|
||||
void ADS1256::setPGA(uint8_t pga) //Setting PGA (input voltage range)
|
||||
{
|
||||
_PGA = pga;
|
||||
_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);
|
||||
delay(200);
|
||||
|
||||
updateConversionParameter(); //Update the multiplier according top the new PGA value
|
||||
}
|
||||
|
||||
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.
|
||||
|
||||
return(pgaValue);
|
||||
}
|
||||
|
||||
void ADS1256::setCLKOUT(uint8_t clkout) //Setting CLKOUT
|
||||
{
|
||||
_ADCON = readRegister(ADCON_REG); //Read the most recent value of the register
|
||||
|
||||
//Values: 0, 1, 2, 3
|
||||
|
||||
if(clkout == 0)
|
||||
{
|
||||
//00
|
||||
bitWrite(_ADCON, 6, 0);
|
||||
bitWrite(_ADCON, 5, 0);
|
||||
}
|
||||
else if(clkout == 1)
|
||||
{
|
||||
//01 (default)
|
||||
bitWrite(_ADCON, 6, 0);
|
||||
bitWrite(_ADCON, 5, 1);
|
||||
}
|
||||
else if(clkout == 2)
|
||||
{
|
||||
//10
|
||||
bitWrite(_ADCON, 6, 1);
|
||||
bitWrite(_ADCON, 5, 0);
|
||||
}
|
||||
else if(clkout == 3)
|
||||
{
|
||||
//11
|
||||
bitWrite(_ADCON, 6, 1);
|
||||
bitWrite(_ADCON, 5, 1);
|
||||
}
|
||||
else{}
|
||||
|
||||
writeRegister(ADCON_REG, _ADCON);
|
||||
delay(100);
|
||||
}
|
||||
|
||||
void ADS1256::setSDCS(uint8_t sdcs) //Setting SDCS
|
||||
{
|
||||
_ADCON = readRegister(ADCON_REG); //Read the most recent value of the register
|
||||
|
||||
//Values: 0, 1, 2, 3
|
||||
|
||||
if(sdcs == 0)
|
||||
{
|
||||
//00 (default)
|
||||
bitWrite(_ADCON, 4, 0);
|
||||
bitWrite(_ADCON, 3, 0);
|
||||
}
|
||||
else if(sdcs == 1)
|
||||
{
|
||||
//01
|
||||
bitWrite(_ADCON, 4, 0);
|
||||
bitWrite(_ADCON, 3, 1);
|
||||
}
|
||||
else if(sdcs == 2)
|
||||
{
|
||||
//10
|
||||
bitWrite(_ADCON, 4, 1);
|
||||
bitWrite(_ADCON, 3, 0);
|
||||
}
|
||||
else if(sdcs == 3)
|
||||
{
|
||||
//11
|
||||
bitWrite(_ADCON, 4, 1);
|
||||
bitWrite(_ADCON, 3, 1);
|
||||
}
|
||||
else{}
|
||||
|
||||
writeRegister(ADCON_REG, _ADCON);
|
||||
delay(100);
|
||||
}
|
||||
|
||||
void ADS1256::setByteOrder(uint8_t byteOrder) //Setting byte order (MSB/LSB)
|
||||
{
|
||||
_STATUS = readRegister(STATUS_REG); //Read the most recent value of the register
|
||||
|
||||
if(byteOrder == 0)
|
||||
{
|
||||
//Byte order is MSB (default)
|
||||
bitWrite(_STATUS, 3, 0);
|
||||
//Set value of _STATUS at the third bit to 0
|
||||
}
|
||||
else if(byteOrder == 1)
|
||||
{
|
||||
//Byte order is LSB
|
||||
bitWrite(_STATUS, 3, 1);
|
||||
//Set value of _STATUS at the third bit to 1
|
||||
}
|
||||
else{}
|
||||
|
||||
writeRegister(STATUS_REG, _STATUS);
|
||||
delay(100);
|
||||
}
|
||||
|
||||
uint8_t ADS1256::getByteOrder() //Getting byte order (MSB/LSB)
|
||||
{
|
||||
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)
|
||||
{
|
||||
_STATUS = readRegister(STATUS_REG); //Read the most recent value of the register
|
||||
|
||||
if(acal == 0)
|
||||
{
|
||||
//Auto-calibration is disabled (default)
|
||||
bitWrite(_STATUS, 2, 0);
|
||||
//_STATUS |= B00000000;
|
||||
}
|
||||
else if(acal == 1)
|
||||
{
|
||||
//Auto-calibration is enabled
|
||||
bitWrite(_STATUS, 2, 1);
|
||||
//_STATUS |= B00000100;
|
||||
}
|
||||
else{}
|
||||
|
||||
writeRegister(STATUS_REG, _STATUS);
|
||||
delay(100);
|
||||
}
|
||||
|
||||
uint8_t ADS1256::getAutoCal() //Getting ACAL (Automatic SYSCAL)
|
||||
{
|
||||
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)
|
||||
{
|
||||
_STATUS = readRegister(STATUS_REG); //Read the most recent value of the register
|
||||
|
||||
if(bufen == 0)
|
||||
{
|
||||
//Analog input buffer is disabled (default)
|
||||
//_STATUS |= B00000000;
|
||||
bitWrite(_STATUS, 1, 0);
|
||||
}
|
||||
else if(bufen == 1)
|
||||
{
|
||||
//Analog input buffer is enabled (recommended)
|
||||
//_STATUS |= B00000010;
|
||||
bitWrite(_STATUS, 1, 1);
|
||||
}
|
||||
else{}
|
||||
|
||||
writeRegister(STATUS_REG, _STATUS);
|
||||
delay(100);
|
||||
}
|
||||
|
||||
uint8_t ADS1256::getBuffer() //Getting input buffer (Input impedance)
|
||||
{
|
||||
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
|
||||
{
|
||||
_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
|
||||
uint8_t GPIO_bit7, GPIO_bit6, GPIO_bit5, GPIO_bit4;
|
||||
|
||||
//Bit7: DIR3
|
||||
if(dir3 == 1)
|
||||
{
|
||||
GPIO_bit7 = 1; //D3 is input (default)
|
||||
}
|
||||
else
|
||||
{
|
||||
GPIO_bit7 = 0; //D3 is output
|
||||
}
|
||||
bitWrite(_GPIO, 7, GPIO_bit7);
|
||||
//-----------------------------------------------------
|
||||
//Bit6: DIR2
|
||||
if(dir2 == 1)
|
||||
{
|
||||
GPIO_bit6 = 1; //D2 is input (default)
|
||||
}
|
||||
else
|
||||
{
|
||||
GPIO_bit6 = 0; //D2 is output
|
||||
}
|
||||
bitWrite(_GPIO, 6, GPIO_bit6);
|
||||
//-----------------------------------------------------
|
||||
//Bit5: DIR1
|
||||
if(dir1 == 1)
|
||||
{
|
||||
GPIO_bit5 = 1; //D1 is input (default)
|
||||
}
|
||||
else
|
||||
{
|
||||
GPIO_bit5 = 0; //D1 is output
|
||||
}
|
||||
bitWrite(_GPIO, 5, GPIO_bit5);
|
||||
//-----------------------------------------------------
|
||||
//Bit4: DIR0
|
||||
if(dir0 == 1)
|
||||
{
|
||||
GPIO_bit4 = 1; //D0 is input
|
||||
}
|
||||
else
|
||||
{
|
||||
GPIO_bit4 = 0; //D0 is output (default)
|
||||
}
|
||||
bitWrite(_GPIO, 4, GPIO_bit4);
|
||||
//-----------------------------------------------------
|
||||
|
||||
writeRegister(IO_REG, _GPIO);
|
||||
delay(100);
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
uint8_t GPIO_bit3, GPIO_bit2, GPIO_bit1, GPIO_bit0;
|
||||
|
||||
//Bit3: DIR3
|
||||
if(dir3value == 1)
|
||||
{
|
||||
GPIO_bit3 = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
GPIO_bit3 = 0;
|
||||
}
|
||||
bitWrite(_GPIO, 3, GPIO_bit3);
|
||||
//-----------------------------------------------------
|
||||
//Bit2: DIR2
|
||||
if(dir2value == 1)
|
||||
{
|
||||
GPIO_bit2 = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
GPIO_bit2 = 0;
|
||||
}
|
||||
bitWrite(_GPIO, 2, GPIO_bit2);
|
||||
//-----------------------------------------------------
|
||||
//Bit1: DIR1
|
||||
if(dir1value == 1)
|
||||
{
|
||||
GPIO_bit1 = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
GPIO_bit1 = 0;
|
||||
}
|
||||
bitWrite(_GPIO, 1, GPIO_bit1);
|
||||
//-----------------------------------------------------
|
||||
//Bit0: DIR0
|
||||
if(dir0value == 1)
|
||||
{
|
||||
GPIO_bit0 = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
GPIO_bit0 = 0;
|
||||
}
|
||||
bitWrite(_GPIO, 0, GPIO_bit0);
|
||||
//-----------------------------------------------------
|
||||
|
||||
writeRegister(IO_REG, _GPIO);
|
||||
delay(100);
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
//Save each bit values in a variable
|
||||
GPIO_bit3 = bitRead(_GPIO, 3);
|
||||
GPIO_bit2 = bitRead(_GPIO, 2);
|
||||
GPIO_bit1 = bitRead(_GPIO, 1);
|
||||
GPIO_bit0 = bitRead(_GPIO, 0);
|
||||
|
||||
delay(100);
|
||||
|
||||
switch(gpioPin) //Selecting which value should be returned
|
||||
{
|
||||
case 0:
|
||||
GPIO_return = GPIO_bit0;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
GPIO_return = GPIO_bit1;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
GPIO_return = GPIO_bit2;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
GPIO_return = GPIO_bit3;
|
||||
break;
|
||||
}
|
||||
|
||||
return GPIO_return;
|
||||
|
||||
}
|
||||
|
||||
void ADS1256::sendDirectCommand(uint8_t directCommand)
|
||||
{
|
||||
//Direct commands can be found in the datasheet Page 34, Table 24.
|
||||
_spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1));
|
||||
|
||||
CS_LOW(); //REF: P34: "CS must stay low during the entire command sequence"
|
||||
delayMicroseconds(5);
|
||||
_spi->transfer(directCommand); //Send Command
|
||||
delayMicroseconds(5);
|
||||
CS_HIGH(); //REF: P34: "CS must stay low during the entire command sequence"
|
||||
|
||||
_spi->endTransaction();
|
||||
}
|
||||
|
||||
|
||||
float ADS1256::convertToVoltage(int32_t rawData) //Converting the 24-bit data into a voltage value
|
||||
{
|
||||
return(conversionParameter * rawData);
|
||||
}
|
||||
|
||||
void ADS1256::writeRegister(uint8_t registerAddress, uint8_t registerValueToWrite)
|
||||
{
|
||||
waitForLowDRDY();
|
||||
|
||||
_spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1));
|
||||
//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]
|
||||
|
||||
delayMicroseconds(5); //see t6 in the datasheet
|
||||
|
||||
_spi->transfer(0x50 | registerAddress); // 0x50 = 01010000 = WREG
|
||||
|
||||
_spi->transfer(0x00); //2nd (empty) command byte
|
||||
|
||||
_spi->transfer(registerValueToWrite); //pass the value to the register
|
||||
|
||||
CS_HIGH();
|
||||
_spi->endTransaction();
|
||||
delay(100);
|
||||
|
||||
}
|
||||
|
||||
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.
|
||||
|
||||
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(0x00); //2nd (empty) command byte
|
||||
|
||||
delayMicroseconds(5); //see t6 in the datasheet
|
||||
|
||||
uint8_t regValue = _spi->transfer(0xFF); //read out the register value
|
||||
|
||||
CS_HIGH();
|
||||
_spi->endTransaction();
|
||||
delay(100);
|
||||
return regValue;
|
||||
}
|
||||
|
||||
|
||||
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"
|
||||
waitForLowDRDY();
|
||||
_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]);
|
||||
_outputValue = convertSigned24BitToLong(_outputValue);
|
||||
|
||||
CS_HIGH(); //We finished the command sequence, so we set CS to HIGH
|
||||
_spi->endTransaction();
|
||||
|
||||
return(_outputValue);
|
||||
}
|
||||
|
||||
long ADS1256::readSingleContinuous() //Reads the recently selected input channel using RDATAC
|
||||
{
|
||||
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"
|
||||
waitForLowDRDY();
|
||||
_spi->transfer(0b00000011); //Issue RDATAC (0000 0011)
|
||||
delayMicroseconds(7); //Wait t6 time (~6.51 us) REF: P34, FIG:30.
|
||||
}
|
||||
else
|
||||
{
|
||||
waitForLowDRDY();
|
||||
}
|
||||
|
||||
_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 = convertSigned24BitToLong(_outputValue);
|
||||
|
||||
waitForHighDRDY();
|
||||
|
||||
return _outputValue;
|
||||
}
|
||||
|
||||
long ADS1256::cycleSingle()
|
||||
{
|
||||
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]
|
||||
_spi->transfer(0x50 | 1); // 0x50 = WREG //1 = MUX
|
||||
_spi->transfer(0x00);
|
||||
_spi->transfer(SING_0); //AIN0+AINCOM
|
||||
CS_HIGH();
|
||||
delay(50);
|
||||
CS_LOW(); //CS must stay LOW during the entire sequence [Ref: P34, T24]
|
||||
}
|
||||
else
|
||||
{}
|
||||
|
||||
if(_cycle < 8)
|
||||
{
|
||||
_outputValue = 0;
|
||||
waitForLowDRDY();
|
||||
//Step 1. - Updating MUX
|
||||
switch (_cycle)
|
||||
{
|
||||
//Channels are written manually
|
||||
case 0: //Channel 2
|
||||
updateMUX(SING_1); //AIN1+AINCOM
|
||||
break;
|
||||
|
||||
case 1: //Channel 3
|
||||
updateMUX(SING_2); //AIN2+AINCOM
|
||||
break;
|
||||
|
||||
case 2: //Channel 4
|
||||
updateMUX(SING_3); //AIN3+AINCOM
|
||||
break;
|
||||
|
||||
case 3: //Channel 5
|
||||
updateMUX(SING_4); //AIN4+AINCOM
|
||||
break;
|
||||
|
||||
case 4: //Channel 6
|
||||
updateMUX(SING_5); //AIN5+AINCOM
|
||||
break;
|
||||
|
||||
case 5: //Channel 7
|
||||
updateMUX(SING_6); //AIN6+AINCOM
|
||||
break;
|
||||
|
||||
case 6: //Channel 8
|
||||
updateMUX(SING_7); //AIN7+AINCOM
|
||||
break;
|
||||
|
||||
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 3.
|
||||
//Issue RDATA (0000 0001) command
|
||||
_spi->transfer(0b00000001);
|
||||
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 = convertSigned24BitToLong(_outputValue);
|
||||
|
||||
_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
|
||||
}
|
||||
}
|
||||
|
||||
return _outputValue;
|
||||
}
|
||||
|
||||
long ADS1256::cycleDifferential()
|
||||
{
|
||||
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]
|
||||
_spi->transfer(0x50 | 1); // 0x50 = WREG //1 = MUX
|
||||
_spi->transfer(0x00);
|
||||
_spi->transfer(DIFF_0_1); //AIN0+AIN1
|
||||
CS_HIGH();
|
||||
delay(50);
|
||||
CS_LOW(); //CS must stay LOW during the entire sequence [Ref: P34, T24]
|
||||
}
|
||||
else
|
||||
{}
|
||||
|
||||
if(_cycle < 4)
|
||||
{
|
||||
_outputValue = 0;
|
||||
//DRDY has to go low
|
||||
waitForLowDRDY();
|
||||
|
||||
//Step 1. - Updating MUX
|
||||
switch (_cycle)
|
||||
{
|
||||
case 0: //Channel 2
|
||||
updateMUX(DIFF_2_3); //AIN2+AIN3
|
||||
break;
|
||||
|
||||
case 1: //Channel 3
|
||||
updateMUX(DIFF_4_5); //AIN4+AIN5
|
||||
break;
|
||||
|
||||
case 2: //Channel 4
|
||||
updateMUX(DIFF_6_7); //AIN6+AIN7
|
||||
break;
|
||||
|
||||
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
|
||||
|
||||
//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 = convertSigned24BitToLong(_outputValue);
|
||||
|
||||
_cycle++;
|
||||
if(_cycle == 4)
|
||||
{
|
||||
_cycle = 0;
|
||||
//After the 4th cycle, we reset to zero so the next iteration reads the 1st MUX again
|
||||
}
|
||||
}
|
||||
|
||||
return _outputValue;
|
||||
}
|
||||
|
||||
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.
|
||||
}
|
||||
|
||||
void ADS1256::updateMUX(uint8_t muxValue)
|
||||
{
|
||||
_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
|
||||
}
|
||||
|
||||
inline void ADS1256::CS_LOW()
|
||||
{
|
||||
if (_CS_pin != PIN_UNUSED) //Sets CS LOW if it is not an unused pin
|
||||
{
|
||||
digitalWrite(_CS_pin, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
inline void ADS1256::CS_HIGH()
|
||||
{
|
||||
if (_CS_pin != PIN_UNUSED) //Sets CS HIGH if it is not an unused pin
|
||||
{
|
||||
digitalWrite(_CS_pin, HIGH);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
//ADS1256 header file
|
||||
/*
|
||||
Name: ADS1256.h
|
||||
Created: 2022/07/14
|
||||
Author: Curious Scientist
|
||||
Editor: Notepad++
|
||||
Comment: Visit https://curiousscientist.tech/blog/ADS1256-custom-library
|
||||
Special thanks to
|
||||
Abraão Queiroz for spending time on the code and suggesting corrections for ESP32 microcontrollers
|
||||
Benjamin Pelletier for pointing out and fixing an issue around the handling of the DRDY signal
|
||||
*/
|
||||
|
||||
#ifndef _ADS1256_h
|
||||
#define _ADS1256_h
|
||||
|
||||
#include <SPI.h>
|
||||
|
||||
//Differential inputs
|
||||
#define DIFF_0_1 0b00000001 //A0 + A1 as differential input
|
||||
#define DIFF_2_3 0b00100011 //A2 + A3 as differential input
|
||||
#define DIFF_4_5 0b01000101 //A4 + A5 as differential input
|
||||
#define DIFF_6_7 0b01100111 //A6 + A7 as differential input
|
||||
|
||||
//Single-ended inputs
|
||||
#define SING_0 0b00001111 //A0 + GND (common) as single-ended input
|
||||
#define SING_1 0b00011111 //A1 + GND (common) as single-ended input
|
||||
#define SING_2 0b00101111 //A2 + GND (common) as single-ended input
|
||||
#define SING_3 0b00111111 //A3 + GND (common) as single-ended input
|
||||
#define SING_4 0b01001111 //A4 + GND (common) as single-ended input
|
||||
#define SING_5 0b01011111 //A5 + GND (common) as single-ended input
|
||||
#define SING_6 0b01101111 //A6 + GND (common) as single-ended input
|
||||
#define SING_7 0b01111111 //A7 + GND (common) as single-ended input
|
||||
|
||||
//PGA settings //Input voltage range
|
||||
#define PGA_1 0b00000000 //± 5 V
|
||||
#define PGA_2 0b00000001 //± 2.5 V
|
||||
#define PGA_4 0b00000010 //± 1.25 V
|
||||
#define PGA_8 0b00000011 //± 625 mV
|
||||
#define PGA_16 0b00000100 //± 312.5 mV
|
||||
#define PGA_32 0b00000101 //+ 156.25 mV
|
||||
#define PGA_64 0b00000110 //± 78.125 mV
|
||||
|
||||
//Datarate //DEC
|
||||
#define DRATE_30000SPS 0b11110000 //240
|
||||
#define DRATE_15000SPS 0b11100000 //224
|
||||
#define DRATE_7500SPS 0b11010000 //208
|
||||
#define DRATE_3750SPS 0b11000000 //192
|
||||
#define DRATE_2000SPS 0b10110000 //176
|
||||
#define DRATE_1000SPS 0b10100001 //161
|
||||
#define DRATE_500SPS 0b10010010 //146
|
||||
#define DRATE_100SPS 0b10000010 //130
|
||||
#define DRATE_60SPS 0b01110010 //114
|
||||
#define DRATE_50SPS 0b01100011 //99
|
||||
#define DRATE_30SPS 0b01010011 //83
|
||||
#define DRATE_25SPS 0b01000011 //67
|
||||
#define DRATE_15SPS 0b00110011 //51
|
||||
#define DRATE_10SPS 0b00100011 //35
|
||||
#define DRATE_5SPS 0b00010011 //19
|
||||
#define DRATE_2SPS 0b00000011 //3
|
||||
|
||||
//Status register
|
||||
#define BITORDER_MSB 0
|
||||
#define BITORDER_LSB 1
|
||||
#define ACAL_DISABLED 0
|
||||
#define ACAL_ENABLED 1
|
||||
#define BUFFER_DISABLED 0
|
||||
#define BUFFER_ENABLED 1
|
||||
|
||||
//Register addresses
|
||||
#define STATUS_REG 0x00
|
||||
#define MUX_REG 0x01
|
||||
#define ADCON_REG 0x02
|
||||
#define DRATE_REG 0x03
|
||||
#define IO_REG 0x04
|
||||
#define OFC0_REG 0x05
|
||||
#define OFC1_REG 0x06
|
||||
#define OFC2_REG 0x07
|
||||
#define FSC0_REG 0x08
|
||||
#define FSC1_REG 0x09
|
||||
#define FSC2_REG 0x0A
|
||||
|
||||
//Command definitions
|
||||
#define WAKEUP 0b00000000
|
||||
#define RDATA 0b00000001
|
||||
#define RDATAC 0b00000011
|
||||
#define SDATAC 0b00001111
|
||||
#define RREG 0b00010000
|
||||
#define WREG 0b01010000
|
||||
#define SELFCAL 0b11110000
|
||||
#define SELFOCAL 0b11110001
|
||||
#define SELFGCAL 0b11110010
|
||||
#define SYSOCAL 0b11110011
|
||||
#define SYSGCAL 0b11110100
|
||||
#define SYNC 0b11111100
|
||||
#define STANDBY 0b11111101
|
||||
#define RESET 0b11111110
|
||||
//----------------------------------------------------------------
|
||||
|
||||
|
||||
class ADS1256
|
||||
{
|
||||
public:
|
||||
static constexpr int8_t PIN_UNUSED = -1;
|
||||
|
||||
//Constructor
|
||||
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);
|
||||
|
||||
//Initializing function
|
||||
void InitializeADC();
|
||||
//ADS1256(int drate, int pga, int byteOrder, bool bufen);
|
||||
|
||||
//Read a register
|
||||
long readRegister(uint8_t registerAddress);
|
||||
|
||||
//Write a register
|
||||
void writeRegister(uint8_t registerAddress, uint8_t registerValueToWrite);
|
||||
|
||||
//Individual methods
|
||||
void setDRATE(uint8_t drate);
|
||||
void setPGA(uint8_t pga);
|
||||
uint8_t getPGA();
|
||||
void setMUX(uint8_t mux);
|
||||
void setByteOrder(uint8_t byteOrder);
|
||||
uint8_t getByteOrder();
|
||||
void setBuffer(uint8_t bufen);
|
||||
uint8_t getBuffer();
|
||||
void setAutoCal(uint8_t acal);
|
||||
uint8_t getAutoCal();
|
||||
void setGPIO(uint8_t dir0, uint8_t dir1, uint8_t dir2, uint8_t dir3);
|
||||
void writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value, uint8_t dir3value);
|
||||
uint8_t readGPIO(uint8_t gpioPin);
|
||||
void setCLKOUT(uint8_t clkout);
|
||||
void setSDCS(uint8_t sdcs);
|
||||
void sendDirectCommand(uint8_t directCommand);
|
||||
|
||||
//Get a single conversion
|
||||
long readSingle();
|
||||
|
||||
//Single input continuous reading
|
||||
long readSingleContinuous();
|
||||
|
||||
//Cycling through the single-ended inputs
|
||||
long cycleSingle(); //Ax + COM
|
||||
|
||||
//Cycling through the differential inputs
|
||||
long cycleDifferential(); //Ax + Ay
|
||||
|
||||
//Converts the reading into a voltage value
|
||||
float convertToVoltage(int32_t rawData);
|
||||
|
||||
//Stop AD
|
||||
void stopConversion();
|
||||
|
||||
private:
|
||||
|
||||
SPIClass* _spi; //Pointer to an SPIClass object
|
||||
|
||||
void waitForLowDRDY(); // Block until DRDY is low
|
||||
void waitForHighDRDY(); // Block until DRDY is high
|
||||
void updateMUX(uint8_t muxValue);
|
||||
inline void CS_LOW();
|
||||
inline void CS_HIGH();
|
||||
|
||||
void updateConversionParameter(); //Refresh the conversion parameter based on the PGA
|
||||
|
||||
float _VREF = 0; //Value of the reference voltage
|
||||
float conversionParameter = 0; //PGA-dependent multiplier
|
||||
//Pins
|
||||
int8_t _DRDY_pin; //Pin assigned for DRDY
|
||||
int8_t _RESET_pin; //Pin assigned for RESET
|
||||
int8_t _SYNC_pin; //Pin assigned for SYNC
|
||||
int8_t _CS_pin; //Pin assigned for CS
|
||||
|
||||
//Register values
|
||||
byte _DRATE; //Value of the DRATE register
|
||||
byte _ADCON; //Value of the ADCON register
|
||||
byte _MUX; //Value of the MUX register
|
||||
byte _PGA; //Value of the PGA (within ADCON)
|
||||
byte _GPIO; //Value of the GPIO register
|
||||
byte _STATUS; //Value of the status register
|
||||
byte _GPIOvalue; //GPIO value
|
||||
byte _ByteOrder; //Byte order
|
||||
|
||||
byte _outputBuffer[3]; //3-byte (24-bit) buffer for the fast acquisition - Single-channel, continuous
|
||||
long _outputValue; //Combined value of the _outputBuffer[3]
|
||||
bool _isAcquisitionRunning; //bool that keeps track of the acquisition (running or not)
|
||||
uint8_t _cycle; //Tracks the cycles as the MUX is cycling through the input channels
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
// ADC Channels
|
||||
|
||||
#define A1_RAW 0
|
||||
#define A2_RAW 1
|
||||
#define B1_RAW 2
|
||||
#define B2_RAW 3
|
||||
|
||||
#define A1_COND 4
|
||||
#define A2_COND 5
|
||||
#define B1_COND 6
|
||||
#define B2_COND 7
|
||||
@@ -1,123 +0,0 @@
|
||||
#include "datasave.h"
|
||||
#include <math.h>
|
||||
|
||||
LITTLEFSGuard::LITTLEFSGuard()
|
||||
{
|
||||
if (!LittleFS.begin(true, "/littlefs", 10, "littlefs"))
|
||||
{
|
||||
LOG_ERROR("Failed to mount LittleFS");
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_INFO("LittleFS mounted successfully");
|
||||
LOG_INFO("LittleFS Free KBytes:", (LittleFS.totalBytes() - LittleFS.usedBytes()) / 1024);
|
||||
}
|
||||
}
|
||||
|
||||
LITTLEFSGuard::~LITTLEFSGuard()
|
||||
{
|
||||
LittleFS.end();
|
||||
LOG_INFO("LittleFS unmounted successfully");
|
||||
}
|
||||
|
||||
void ignitionBoxStatusFiltered::filter(int32_t &old, const int32_t value, const uint32_t k)
|
||||
{
|
||||
float alpha = 1.0f / (float)k;
|
||||
old = old + (int32_t)(alpha * (float)(value - old));
|
||||
}
|
||||
|
||||
void ignitionBoxStatusFiltered::filter(float &old, const float value, const uint32_t k)
|
||||
{
|
||||
float alpha = 1.0f / (float)k;
|
||||
old = old + (float)(alpha * (float)(value - old));
|
||||
}
|
||||
|
||||
void ignitionBoxStatusFiltered::reset()
|
||||
{
|
||||
m_last = ignitionBoxStatus();
|
||||
m_count = 0;
|
||||
m_data_valid = false;
|
||||
}
|
||||
|
||||
void ignitionBoxStatusFiltered::update(const ignitionBoxStatus &new_status)
|
||||
{
|
||||
if (m_count == 0 && !m_data_valid)
|
||||
{
|
||||
m_last = new_status;
|
||||
}
|
||||
m_count++;
|
||||
// simple moving average calculation
|
||||
m_last.timestamp = new_status.timestamp; // keep timestamp of latest status
|
||||
m_last.coils12.n_events = new_status.coils12.n_events; // sum events 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.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
|
||||
filter(m_last.coils12.peak_p_in, new_status.coils12.peak_p_in, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils12.peak_n_in, new_status.coils12.peak_n_in, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils12.peak_p_out, new_status.coils12.peak_p_out, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils12.peak_n_out, new_status.coils12.peak_n_out, m_max_count); // incremental average calculation
|
||||
|
||||
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.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
|
||||
filter(m_last.coils34.spark_delay, new_status.coils34.spark_delay, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils34.peak_p_in, new_status.coils34.peak_p_in, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils34.peak_n_in, new_status.coils34.peak_n_in, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils34.peak_p_out, new_status.coils34.peak_p_out, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils34.peak_n_out, new_status.coils34.peak_n_out, m_max_count); // 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.n_queue_errors = new_status.n_queue_errors;
|
||||
|
||||
if (m_count >= m_max_count)
|
||||
{
|
||||
m_count = 0; // reset count after reaching max samples to average
|
||||
m_data_valid = true; // set data valid flag after first average is calculated
|
||||
}
|
||||
}
|
||||
|
||||
const bool ignitionBoxStatusFiltered::get(ignitionBoxStatus &status) const
|
||||
{
|
||||
if (m_data_valid)
|
||||
{
|
||||
status = m_last;
|
||||
}
|
||||
return m_data_valid;
|
||||
}
|
||||
|
||||
const ArduinoJson::JsonDocument ignitionBoxStatusFiltered::toJson() const
|
||||
{
|
||||
ArduinoJson::JsonDocument doc;
|
||||
if (m_data_valid)
|
||||
{
|
||||
doc["timestamp"] = m_last.timestamp;
|
||||
doc["datavalid"] = m_data_valid ? "TRUE" : "FALSE";
|
||||
|
||||
doc["coils12"]["n_events"] = m_last.coils12.n_events;
|
||||
doc["coils12"]["n_missed_firing"] = m_last.coils12.n_missed_firing;
|
||||
doc["coils12"]["spark_delay"] = m_last.coils12.spark_delay;
|
||||
doc["coils12"]["spark_status"] = sparkStatusNames.at(m_last.coils12.spark_status);
|
||||
doc["coils12"]["peak_p_in"] = m_last.coils12.peak_p_in;
|
||||
doc["coils12"]["peak_n_in"] = m_last.coils12.peak_n_in;
|
||||
doc["coils12"]["peak_p_out"] = m_last.coils12.peak_p_out;
|
||||
doc["coils12"]["peak_n_out"] = m_last.coils12.peak_n_out;
|
||||
doc["coils12"]["sstart_status"] = softStartStatusNames.at(m_last.coils12.sstart_status);
|
||||
|
||||
doc["coils34"]["n_events"] = m_last.coils34.n_events;
|
||||
doc["coils34"]["n_missed_firing"] = m_last.coils34.n_missed_firing;
|
||||
doc["coils34"]["spark_delay"] = m_last.coils34.spark_delay;
|
||||
doc["coils34"]["spark_status"] = sparkStatusNames.at(m_last.coils34.spark_status);
|
||||
doc["coils34"]["peak_p_in"] = m_last.coils34.peak_p_in;
|
||||
doc["coils34"]["peak_n_in"] = m_last.coils34.peak_n_in;
|
||||
doc["coils34"]["peak_p_out"] = m_last.coils34.peak_p_out;
|
||||
doc["coils34"]["peak_n_out"] = m_last.coils34.peak_n_out;
|
||||
doc["coils34"]["sstart_status"] = softStartStatusNames.at(m_last.coils34.sstart_status);
|
||||
|
||||
doc["eng_rpm"] = m_last.eng_rpm;
|
||||
doc["adc_read_time"] = m_last.adc_read_time;
|
||||
doc["n_queue_errors"] = m_last.n_queue_errors;
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
#pragma once
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_INFO
|
||||
|
||||
// System Includes
|
||||
#include <Arduino.h>
|
||||
#include <DebugLog.h>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <ArduinoJson.h>
|
||||
#include <filesystem>
|
||||
#include <LittleFS.h>
|
||||
|
||||
// Project Includes
|
||||
#include "isr.h"
|
||||
#include "psvector.h"
|
||||
|
||||
const uint32_t max_history = 256;
|
||||
|
||||
class LITTLEFSGuard
|
||||
{
|
||||
public:
|
||||
LITTLEFSGuard();
|
||||
~LITTLEFSGuard();
|
||||
};
|
||||
|
||||
class ignitionBoxStatusFiltered
|
||||
{
|
||||
private:
|
||||
ignitionBoxStatus m_last;
|
||||
uint32_t m_count = 0;
|
||||
uint32_t m_max_count = 100; // number of samples to average before resetting
|
||||
bool m_data_valid = false; // flag to indicate if the average data is valid (i.e. at least one sample has been added)
|
||||
|
||||
public:
|
||||
ignitionBoxStatusFiltered() = default;
|
||||
ignitionBoxStatusFiltered(const uint32_t max_count) : m_max_count(max_count)
|
||||
{
|
||||
m_data_valid = false;
|
||||
m_count = 0;
|
||||
}
|
||||
|
||||
void reset();
|
||||
void update(const ignitionBoxStatus &new_status);
|
||||
const bool get(ignitionBoxStatus &status) const;
|
||||
const ArduinoJson::JsonDocument toJson() const;
|
||||
|
||||
private:
|
||||
void filter(int32_t &old, const int32_t value, const uint32_t k);
|
||||
void filter(float &old, const float value, const uint32_t k);
|
||||
};
|
||||
@@ -1,95 +0,0 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#include <map>
|
||||
#include <psvector.h>
|
||||
|
||||
// =====================
|
||||
// Event Flags (bitmask)
|
||||
// =====================
|
||||
static const uint32_t TRIG_FLAG_12P = (1 << 0);
|
||||
static const uint32_t TRIG_FLAG_12N = (1 << 1);
|
||||
static const uint32_t TRIG_FLAG_34P = (1 << 2);
|
||||
static const uint32_t TRIG_FLAG_34N = (1 << 3);
|
||||
|
||||
static const uint32_t SPARK_FLAG_TIMEOUT = (1 << 8);
|
||||
static const uint32_t SPARK_FLAG_12 = (1 << 9);
|
||||
static const uint32_t SPARK_FLAG_34 = (1 << 10);
|
||||
|
||||
// Spark Status
|
||||
enum sparkStatus
|
||||
{
|
||||
SPARK_POS_OK,
|
||||
SPARK_NEG_OK,
|
||||
SPARK_POS_SKIP,
|
||||
SPARK_NEG_SKIP,
|
||||
SPARK_POS_WAIT,
|
||||
SPARK_NEG_WAIT,
|
||||
SPARK_POS_FAIL,
|
||||
SPARK_NEG_FAIL,
|
||||
SPARK_POS_UNEXPECTED,
|
||||
SPARK_NEG_UNEXPECTED,
|
||||
SPARK_SYNC_FAIL,
|
||||
};
|
||||
|
||||
static const std::map<const sparkStatus, const char *> sparkStatusNames = {
|
||||
{SPARK_POS_OK, "SPARK_POS_OK"},
|
||||
{SPARK_NEG_OK, "SPARK_NEG_OK"},
|
||||
{SPARK_POS_SKIP, "SPARK_POS_SKIP"},
|
||||
{SPARK_NEG_SKIP, "SPARK_NEG_SKIP"},
|
||||
{SPARK_POS_WAIT, "SPARK_POS_WAIT"},
|
||||
{SPARK_NEG_WAIT, "SPARK_NEG_WAIT"},
|
||||
{SPARK_POS_FAIL, "SPARK_POS_FAIL"},
|
||||
{SPARK_NEG_FAIL, "SPARK_NEG_FAIL"},
|
||||
{SPARK_POS_UNEXPECTED, "SPARK_POS_UNEXPECTED"},
|
||||
{SPARK_NEG_UNEXPECTED, "SPARK_NEG_UNEXPECTED"},
|
||||
{SPARK_SYNC_FAIL, "SPARK_SYNC_FAIL"},
|
||||
};
|
||||
|
||||
enum softStartStatus
|
||||
{
|
||||
NORMAL,
|
||||
SOFT_START,
|
||||
ERROR,
|
||||
};
|
||||
|
||||
const std::map<const softStartStatus, const char *> softStartStatusNames = {
|
||||
{NORMAL, "NORMAL"},
|
||||
{SOFT_START, "SOFT_START"},
|
||||
{ERROR, "ERROR"},
|
||||
};
|
||||
|
||||
struct coilsStatus
|
||||
{
|
||||
int64_t trig_time = 0;
|
||||
int64_t spark_time = 0;
|
||||
int32_t spark_delay = 0; // in microseconds
|
||||
sparkStatus spark_status = sparkStatus::SPARK_POS_OK;
|
||||
softStartStatus sstart_status = softStartStatus::NORMAL;
|
||||
float peak_p_in = 0.0;
|
||||
float peak_n_in = 0.0;
|
||||
float peak_p_out = 0.0;
|
||||
float peak_n_out = 0.0;
|
||||
float level_spark = 0.0;
|
||||
uint32_t n_events = 0;
|
||||
uint32_t n_missed_firing = 0;
|
||||
};
|
||||
|
||||
// Task internal Status
|
||||
struct ignitionBoxStatus
|
||||
{
|
||||
int64_t timestamp = 0;
|
||||
// coils pairs for each ignition
|
||||
coilsStatus coils12;
|
||||
coilsStatus coils34;
|
||||
// voltage from generator
|
||||
float volts_gen = 0.0;
|
||||
// enine rpm
|
||||
int32_t eng_rpm = 0;
|
||||
// debug values
|
||||
uint32_t n_queue_errors = 0;
|
||||
int32_t adc_read_time = 0;
|
||||
};
|
||||
|
||||
|
||||
template <typename T>
|
||||
using PSRAMVector = std::vector<T, PSRAMAllocator<T>>;
|
||||
+14
-19
@@ -1,26 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
// Library defines
|
||||
#define ADS1256_SPI_ALREADY_STARTED
|
||||
|
||||
// System Includes
|
||||
#include <memory>
|
||||
|
||||
// Device Libraries
|
||||
#include <ADS1256.h>
|
||||
#include <AD5292.h>
|
||||
#include <extio.h>
|
||||
#include <Wire.h>
|
||||
#include <PCA95x5.h>
|
||||
|
||||
// ADC Channel mapping
|
||||
#define ADC_CH_PEAK_12P_IN SING_0
|
||||
#define ADC_CH_PEAK_12N_IN SING_1
|
||||
#define ADC_CH_PEAK_34P_IN SING_2
|
||||
#define ADC_CH_PEAK_34N_IN SING_3
|
||||
#define ADC_CH_PEAK_12P_OUT SING_4
|
||||
#define ADC_CH_PEAK_12N_OUT SING_5
|
||||
#define ADC_CH_PEAK_34P_OUT SING_6
|
||||
#define ADC_CH_PEAK_34N_OUT SING_7
|
||||
#define ADC_CH_PEAK_12P_IN SING_0
|
||||
#define ADC_CH_PEAK_12N_IN SING_1
|
||||
#define ADC_CH_PEAK_34P_IN SING_2
|
||||
#define ADC_CH_PEAK_34N_IN SING_3
|
||||
#define ADC_CH_PEAK_12P_OUT SING_4
|
||||
#define ADC_CH_PEAK_12N_OUT SING_5
|
||||
#define ADC_CH_PEAK_34P_OUT SING_6
|
||||
#define ADC_CH_PEAK_34N_OUT SING_7
|
||||
|
||||
// Device Pointer structs for tasks
|
||||
struct Devices
|
||||
@@ -46,10 +39,12 @@ struct Devices
|
||||
};
|
||||
|
||||
// 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->readSingle();
|
||||
// scarta 3 conversioni
|
||||
for (int i = 0; i < 3; i++) {
|
||||
adc->readSingle();
|
||||
}
|
||||
// ora lettura valida a 30kSPS → ~100 µs di settling
|
||||
return adc->convertToVoltage(adc->readSingle());
|
||||
}
|
||||
|
||||
@@ -1,129 +0,0 @@
|
||||
#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));
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
#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;
|
||||
};
|
||||
@@ -4,7 +4,7 @@
|
||||
// ISR (Pass return bitmask to ISR management function)
|
||||
// one function for each wake up pin conncted to a trigger
|
||||
// =====================
|
||||
void trig_isr_A(void *arg)
|
||||
void trig_isr(void *arg)
|
||||
{
|
||||
const int64_t time_us = esp_timer_get_time();
|
||||
|
||||
@@ -36,13 +36,18 @@ void trig_isr_A(void *arg)
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
case SPARK_FLAG_12:
|
||||
|
||||
box->coils34.spark_ok = false;
|
||||
box->coils12.spark_ok = true;
|
||||
box->coils12.spark_time = time_us;
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
// vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
case SPARK_FLAG_34:
|
||||
box->coils12.spark_ok = false;
|
||||
box->coils34.spark_ok = true;
|
||||
box->coils34.spark_time = time_us;
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
// vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -50,53 +55,4 @@ void trig_isr_A(void *arg)
|
||||
|
||||
if (xHigherPriorityTaskWoken)
|
||||
portYIELD_FROM_ISR();
|
||||
}
|
||||
|
||||
|
||||
void trig_isr_B(void *arg)
|
||||
{
|
||||
const int64_t time_us = esp_timer_get_time();
|
||||
|
||||
// exit if invalid args
|
||||
if (!arg)
|
||||
return;
|
||||
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
isrParams *params = (isrParams *)arg;
|
||||
ignitionBoxStatus *box = params->ign_stat;
|
||||
TaskHandle_t task_handle = params->rt_handle_ptr;
|
||||
|
||||
// exit if task not running
|
||||
if (!task_handle)
|
||||
return;
|
||||
|
||||
switch (params->flag)
|
||||
{
|
||||
case TRIG_FLAG_12P:
|
||||
case TRIG_FLAG_12N:
|
||||
// only on first trigger to avoid multiple firing due to noise, to be fixed with hardware debounce
|
||||
box->coils12.trig_time = time_us;
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
case TRIG_FLAG_34P:
|
||||
case TRIG_FLAG_34N:
|
||||
// only on first trigger to avoid multiple firing due to noise, to be fixed with hardware debounce
|
||||
box->coils34.trig_time = time_us;
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
case SPARK_FLAG_12:
|
||||
|
||||
box->coils12.spark_time = time_us;
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
case SPARK_FLAG_34:
|
||||
box->coils34.spark_time = time_us;
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (xHigherPriorityTaskWoken)
|
||||
portYIELD_FROM_ISR();
|
||||
}
|
||||
}
|
||||
+86
-5
@@ -12,12 +12,94 @@
|
||||
#else
|
||||
#include "pins_test.h"
|
||||
#endif
|
||||
#include "datastruct.h"
|
||||
|
||||
#define CORE_0 0
|
||||
#define CORE_1 1
|
||||
#define RT_TASK_STACK 4096 // in words
|
||||
#define RT_TASK_PRIORITY (configMAX_PRIORITIES - 5) // highest priority after wifi tasks
|
||||
#define TASK_STACK 4096 // in words
|
||||
#define TASK_PRIORITY (configMAX_PRIORITIES - 4) // highest priority after wifi tasks
|
||||
|
||||
// =====================
|
||||
// Event Flags (bitmask)
|
||||
// =====================
|
||||
static const uint32_t TRIG_FLAG_12P = (1 << 0);
|
||||
static const uint32_t TRIG_FLAG_12N = (1 << 1);
|
||||
static const uint32_t TRIG_FLAG_34P = (1 << 2);
|
||||
static const uint32_t TRIG_FLAG_34N = (1 << 3);
|
||||
|
||||
static const uint32_t SPARK_FLAG_NIL = (1 << 8);
|
||||
static const uint32_t SPARK_FLAG_12 = (1 << 9);
|
||||
static const uint32_t SPARK_FLAG_34 = (1 << 10);
|
||||
static const uint32_t SPARK_FLAG_TIMEOUT = (1 << 11);
|
||||
|
||||
// Spark Status
|
||||
enum sparkStatus
|
||||
{
|
||||
SPARK_POS_OK,
|
||||
SPARK_NEG_OK,
|
||||
SPARK_POS_SKIP,
|
||||
SPARK_NEG_SKIP,
|
||||
SPARK_POS_WAIT,
|
||||
SPARK_NEG_WAIT,
|
||||
SPARK_POS_FAIL,
|
||||
SPARK_NEG_FAIL,
|
||||
SPARK_POS_UNEXPECTED,
|
||||
SPARK_NEG_UNEXPECTED,
|
||||
SPARK_SYNC_FAIL,
|
||||
};
|
||||
|
||||
static const std::map<const sparkStatus, const char *> sparkStatusNames = {
|
||||
{SPARK_POS_OK, "SPARK_POS_OK"},
|
||||
{SPARK_NEG_OK, "SPARK_NEG_OK"},
|
||||
{SPARK_POS_SKIP, "SPARK_POS_SKIP"},
|
||||
{SPARK_NEG_SKIP, "SPARK_NEG_SKIP"},
|
||||
{SPARK_POS_WAIT, "SPARK_POS_WAIT"},
|
||||
{SPARK_NEG_WAIT, "SPARK_NEG_WAIT"},
|
||||
{SPARK_POS_FAIL, "SPARK_POS_FAIL"},
|
||||
{SPARK_NEG_FAIL, "SPARK_NEG_FAIL"},
|
||||
{SPARK_POS_UNEXPECTED, "SPARK_POS_UNEXPECTED"},
|
||||
{SPARK_NEG_UNEXPECTED, "SPARK_NEG_UNEXPECTED"},
|
||||
{SPARK_SYNC_FAIL, "SPARK_SYNC_FAIL"},
|
||||
};
|
||||
|
||||
enum softStartStatus
|
||||
{
|
||||
NORMAL,
|
||||
SOFT_START,
|
||||
ERROR,
|
||||
};
|
||||
|
||||
const std::map<const softStartStatus, const char *> softStartStatusNames = {
|
||||
{NORMAL, "NORMAL"},
|
||||
{SOFT_START, "SOFT_START"},
|
||||
{ERROR, "ERROR"},
|
||||
};
|
||||
|
||||
struct coilsStatus
|
||||
{
|
||||
int64_t trig_time = 0;
|
||||
int64_t spark_time = 0;
|
||||
int64_t spark_delay = 0; // in microseconds
|
||||
sparkStatus spark_status = sparkStatus::SPARK_POS_OK;
|
||||
softStartStatus sstart_status = softStartStatus::NORMAL;
|
||||
float peak_p_in = 0.0, peak_n_in = 0.0;
|
||||
float peak_p_out = 0.0, peak_n_out = 0.0;
|
||||
float trigger_spark = 0.0;
|
||||
bool spark_ok = false;
|
||||
uint32_t n_events = 0;
|
||||
|
||||
};
|
||||
|
||||
// Task internal Status
|
||||
struct ignitionBoxStatus
|
||||
{
|
||||
int64_t timestamp = 0;
|
||||
// coils pairs for each ignition
|
||||
coilsStatus coils12;
|
||||
coilsStatus coils34;
|
||||
// voltage from generator
|
||||
float volts_gen = 0.0;
|
||||
uint32_t n_queue_errors = 0;
|
||||
};
|
||||
|
||||
struct isrParams
|
||||
{
|
||||
@@ -26,5 +108,4 @@ struct isrParams
|
||||
TaskHandle_t rt_handle_ptr;
|
||||
};
|
||||
|
||||
void IRAM_ATTR trig_isr_A(void *arg);
|
||||
void IRAM_ATTR trig_isr_B(void *arg);
|
||||
void IRAM_ATTR trig_isr(void *arg);
|
||||
|
||||
+85
-17
@@ -1,20 +1,16 @@
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_INFO
|
||||
|
||||
// Arduino Libraries
|
||||
#include <Arduino.h>
|
||||
#include <DebugLog.h>
|
||||
#include <DebugLogEnable.h>
|
||||
#include <SPI.h>
|
||||
#include <WiFi.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
// Definitions
|
||||
#include <tasks.h>
|
||||
#include <channels.h>
|
||||
#include <devices.h>
|
||||
#include <datasave.h>
|
||||
#include <webserver.h>
|
||||
#include <ui.h>
|
||||
#include <led.h>
|
||||
|
||||
#define CH_A_ENABLE
|
||||
#define CH_B_ENABLE
|
||||
@@ -31,20 +27,20 @@
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.begin(921600);
|
||||
delay(250);
|
||||
Serial.setTimeout(5000);
|
||||
|
||||
// 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());
|
||||
LOG_INFO("ESP32 Chip:", ESP.getChipModel());
|
||||
if (psramFound())
|
||||
{
|
||||
LOG_DEBUG("ESP32 PSram Found");
|
||||
LOG_DEBUG("ESP32 PSram:", ESP.getPsramSize());
|
||||
LOG_INFO("ESP32 PSram Found");
|
||||
LOG_INFO("ESP32 PSram:", ESP.getPsramSize());
|
||||
psramInit();
|
||||
}
|
||||
LOG_DEBUG("ESP32 Flash:", ESP.getFlashChipSize());
|
||||
@@ -82,7 +78,6 @@ void setup()
|
||||
initSparkPinInputs();
|
||||
}
|
||||
|
||||
////////////////////// MAIN LOOP //////////////////////
|
||||
void loop()
|
||||
{
|
||||
// global variables
|
||||
@@ -92,10 +87,49 @@ void loop()
|
||||
|
||||
Devices dev;
|
||||
bool running = true;
|
||||
std::mutex fs_mutex;
|
||||
LITTLEFSGuard fsGuard;
|
||||
static Devices dev;
|
||||
|
||||
// Task handle
|
||||
static TaskHandle_t trigA_TaskHandle = NULL;
|
||||
static TaskHandle_t trigB_TaskHandle = NULL;
|
||||
|
||||
static QueueHandle_t rt_taskA_queue = xQueueCreate(10, sizeof(ignitionBoxStatus));
|
||||
static QueueHandle_t rt_taskB_queue = xQueueCreate(10, sizeof(ignitionBoxStatus));
|
||||
static rtTaskParams taskA_params{
|
||||
.rt_running = true,
|
||||
.dev = &dev,
|
||||
.rt_handle_ptr = &trigA_TaskHandle,
|
||||
.rt_queue = rt_taskA_queue,
|
||||
.rt_int = rtTaskInterrupts{
|
||||
.isr_ptr = trig_isr,
|
||||
.trig_pin_12p = TRIG_PIN_A12P,
|
||||
.trig_pin_12n = TRIG_PIN_A12N,
|
||||
.trig_pin_34p = TRIG_PIN_A34P,
|
||||
.trig_pin_34n = TRIG_PIN_A34N,
|
||||
.spark_pin_12 = SPARK_PIN_A12,
|
||||
.spark_pin_34 = SPARK_PIN_A34},
|
||||
.rt_resets = rtTaskResets{.rst_io_12p = RST_EXT_A12P, .rst_io_12n = RST_EXT_A12N, .rst_io_34p = RST_EXT_A34P, .rst_io_34n = RST_EXT_A34N}};
|
||||
|
||||
LOG_INFO("Task Variables OK");
|
||||
|
||||
#ifdef CH_B_ENABLE
|
||||
QueueHandle_t rt_taskB_queue = xQueueCreate(10, sizeof(ignitionBoxStatus));
|
||||
rtTaskParams taskB_params{
|
||||
.rt_running = true,
|
||||
.dev = &dev,
|
||||
.rt_handle_ptr = &trigB_TaskHandle,
|
||||
.rt_queue = rt_taskB_queue,
|
||||
.rt_int = rtTaskInterrupts{
|
||||
.isr_ptr = trig_isr,
|
||||
.trig_pin_12p = TRIG_PIN_B12P,
|
||||
.trig_pin_12n = TRIG_PIN_B12N,
|
||||
.trig_pin_34p = TRIG_PIN_B34P,
|
||||
.trig_pin_34n = TRIG_PIN_B34N,
|
||||
.spark_pin_12 = SPARK_PIN_B12,
|
||||
.spark_pin_34 = SPARK_PIN_B34},
|
||||
.rt_resets = rtTaskResets{.rst_io_12p = RST_EXT_B12P, .rst_io_12n = RST_EXT_B12N, .rst_io_34p = RST_EXT_B34P, .rst_io_34n = RST_EXT_B34N}};
|
||||
#endif
|
||||
|
||||
//////// INIT SPI INTERFACES ////////
|
||||
bool spiA_ok = true;
|
||||
bool spiB_ok = true;
|
||||
//////// INIT SPI INTERFACES ////////
|
||||
@@ -143,6 +177,7 @@ void loop()
|
||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||
esp_restart();
|
||||
}
|
||||
LOG_INFO("Init SPI OK");
|
||||
|
||||
LOG_DEBUG("Init SPI -> OK");
|
||||
|
||||
@@ -254,7 +289,7 @@ void loop()
|
||||
// Ignition A on Core 0
|
||||
if (ignA_task_success != pdPASS || ignB_task_success != pdPASS)
|
||||
{
|
||||
LOG_ERROR("Unable to initialize ISR task");
|
||||
LOG_ERROR("Unble to initialize ISR task");
|
||||
LOG_ERROR("5 seconds to restart...");
|
||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||
esp_restart();
|
||||
@@ -300,6 +335,10 @@ void loop()
|
||||
uint32_t this_loop = millis();
|
||||
if (this_loop - monitor_loop > 5000)
|
||||
{
|
||||
if (ignA.coils12.spark_status == sparkStatus::SPARK_NEG_FAIL || ignA.coils12.spark_status == sparkStatus::SPARK_POS_FAIL)
|
||||
missed_firings12++;
|
||||
if (ignA.coils34.spark_status == sparkStatus::SPARK_POS_FAIL || ignA.coils34.spark_status == sparkStatus::SPARK_NEG_FAIL)
|
||||
missed_firings34++;
|
||||
clearScreen();
|
||||
printRunningTasksMod(Serial);
|
||||
monitor_loop = millis();
|
||||
@@ -316,4 +355,33 @@ void loop()
|
||||
#endif
|
||||
} //////////////// INNER LOOP /////////////////////
|
||||
|
||||
} ////////////////////// MAIN LOOP //////////////////////
|
||||
Serial.println("========== Coils 34 =============");
|
||||
printField("Events", (uint32_t)ignA.coils34.n_events, 0, 11);
|
||||
printField("Missed Firing", missed_firings34, 0, 12);
|
||||
printField("Spark Dly", (uint32_t)ignA.coils34.spark_delay, 0, 13);
|
||||
printField("Spark Sts", sparkStatusNames.at(ignA.coils34.spark_status), 0, 14);
|
||||
// printField("Peak P_IN", ignA.coils34.peak_p_in, 0, 15);
|
||||
// printField("Peak P_OUT", ignA.coils34.peak_p_out, 0, 16);
|
||||
// printField("Peak N_IN", ignA.coils34.peak_n_in, 0, 17);
|
||||
// printField("Peak N_OUT", ignA.coils34.peak_n_out, 0, 18);
|
||||
printField("Soft Start ", softStartStatusNames.at(ignA.coils34.sstart_status), 0, 19);
|
||||
|
||||
Serial.println("========== END =============");
|
||||
Serial.println();
|
||||
auto delta = (esp_timer_get_time() - last) / 1000000.0f; //in seconds
|
||||
delta = delta > 0 ? 1.0f / delta : 0; // Calculate frequency (Hz)
|
||||
printField("Frequency (Hz)", delta, 0, 21);
|
||||
printField("Queue Errors", (uint32_t)ignA.n_queue_errors, 0, 22);
|
||||
last = esp_timer_get_time();
|
||||
} else
|
||||
{
|
||||
Serial.println("Waiting for data... ");;
|
||||
}
|
||||
}
|
||||
|
||||
if (trigA_TaskHandle)
|
||||
vTaskDelete(trigA_TaskHandle);
|
||||
if (trigB_TaskHandle)
|
||||
vTaskDelete(trigB_TaskHandle);
|
||||
////////////////////// MAIN LOOP //////////////////////
|
||||
}
|
||||
|
||||
+58
-105
@@ -4,19 +4,19 @@
|
||||
// =====================
|
||||
// USB (RISERVATA)
|
||||
// =====================
|
||||
#define USB_DM 19
|
||||
#define USB_DP 20
|
||||
#define USB_DM 19
|
||||
#define USB_DP 20
|
||||
|
||||
// =====================
|
||||
// UART DEBUG (RISERVATA)
|
||||
// =====================
|
||||
#define UART_TX 43
|
||||
#define UART_RX 44
|
||||
#define UART_TX 43
|
||||
#define UART_RX 44
|
||||
|
||||
// =====================
|
||||
// RGB Led
|
||||
// =====================
|
||||
#define LED 48
|
||||
#define LED 48
|
||||
|
||||
// =====================
|
||||
// STRAPPING CRITICI (NON USARE)
|
||||
@@ -26,134 +26,87 @@
|
||||
// =====================
|
||||
// SPI BUS ADC1 (VSPI)
|
||||
// =====================
|
||||
#define SPI_A_MOSI 10
|
||||
#define SPI_A_SCK 11
|
||||
#define SPI_A_MISO 12
|
||||
#define SPI_A_MOSI 11
|
||||
#define SPI_A_MISO 13
|
||||
#define SPI_A_SCK 12
|
||||
|
||||
// =====================
|
||||
// SPI BUS ADC2 (HSPI)
|
||||
// =====================
|
||||
#define SPI_B_MOSI 36
|
||||
#define SPI_B_SCK 37
|
||||
#define SPI_B_MISO 38
|
||||
#define SPI_B_MOSI 35
|
||||
#define SPI_B_MISO 37
|
||||
#define SPI_B_SCK 36
|
||||
|
||||
// =====================
|
||||
// I2C BUS (PCA9555)
|
||||
// =====================
|
||||
#define SDA 8
|
||||
#define SCL 9
|
||||
#define I2C_INT 17
|
||||
#define SDA 8
|
||||
#define SCL 9
|
||||
|
||||
// =====================
|
||||
// ADC CONTROL
|
||||
// =====================
|
||||
#define ADC_A_CS 14
|
||||
#define ADC_A_DRDY 13
|
||||
#define ADC_A_CS 4
|
||||
#define ADC_A_DRDY 5
|
||||
#define ADC_A_RST 6
|
||||
#define ADC_A_SYNC 7
|
||||
|
||||
#define ADC_B_CS 21
|
||||
#define ADC_B_DRDY 47
|
||||
#define ADC_B_CS 14
|
||||
#define ADC_B_DRDY 15
|
||||
#define ADC_B_RST 16
|
||||
#define ADC_B_SYNC 17
|
||||
|
||||
// =====================
|
||||
// DIGITAL POT
|
||||
// =====================
|
||||
//#define POT_A_CS 1
|
||||
//#define POT_B_CS 2
|
||||
|
||||
// =====================
|
||||
// TRIGGER INPUT INTERRUPTS
|
||||
// =====================
|
||||
#define TRIG_PIN_A12P 6
|
||||
#define TRIG_PIN_A12N 7
|
||||
#define TRIG_PIN_A34P 15
|
||||
#define TRIG_PIN_A34N 16
|
||||
#define TRIG_PIN_B12P 42
|
||||
#define TRIG_PIN_B12N 41
|
||||
#define TRIG_PIN_B34P 40
|
||||
#define TRIG_PIN_B34N 39
|
||||
#define TRIG_PIN_A12P 18
|
||||
#define TRIG_PIN_A12N 21
|
||||
#define TRIG_PIN_A34P 1
|
||||
#define TRIG_PIN_A34N 2
|
||||
#define TRIG_PIN_B12P 38
|
||||
#define TRIG_PIN_B12N 39
|
||||
#define TRIG_PIN_B34P 40
|
||||
#define TRIG_PIN_B34N 41
|
||||
|
||||
// =====================
|
||||
// SPARK DETECT INPUTS
|
||||
// =====================
|
||||
#define SPARK_PIN_A12 4
|
||||
#define SPARK_PIN_A34 5
|
||||
#define SPARK_PIN_B12 1
|
||||
#define SPARK_PIN_B34 2
|
||||
|
||||
// +++++++++++++++++++++
|
||||
// MACRO TO COMBINE PIN NUMBER AND ADDRESS
|
||||
#define PIN2ADDR(p, a) ((1UL << p) | ((uint32_t)(a) << 16))
|
||||
// +++++++++++++++++++++
|
||||
#define SPARK_PIN_A12 42
|
||||
#define SPARK_PIN_A34 45 // OK (strapping ma consentito) 45
|
||||
#define SPARK_PIN_B12 46 // OK (strapping ma consentito) 46
|
||||
#define SPARK_PIN_B34 47
|
||||
|
||||
// =====================
|
||||
// PCA9555 I/O EXPANDER INTERRUPT (Common)
|
||||
// PCA9555 (I2C EXPANDER)
|
||||
// =====================
|
||||
#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 PIN2ADDR(0, EXPANDER_A_OUT_ADDR)
|
||||
#define POT_CS_A34 PIN2ADDR(1, EXPANDER_A_OUT_ADDR)
|
||||
|
||||
// --- SOFT START FORCE LINES ---
|
||||
#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 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)
|
||||
// --- RESET LINES ---
|
||||
#define RST_EXT_A12P 0
|
||||
#define RST_EXT_A12N 1
|
||||
#define RST_EXT_A34P 2
|
||||
#define RST_EXT_A34N 3
|
||||
#define RST_EXT_B12P 4
|
||||
#define RST_EXT_B12N 5
|
||||
#define RST_EXT_B34P 6
|
||||
#define RST_EXT_B34N 7
|
||||
|
||||
// --- RELAY ---
|
||||
#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)
|
||||
#define A_EXT_RELAY 8
|
||||
#define B_EXT_RELAY 9
|
||||
|
||||
// =====================
|
||||
// PCA9555 I/O EXPANDER BOX_A (IN)
|
||||
// =====================
|
||||
#define EXPANDER_A_IN_ADDR 0xFF
|
||||
|
||||
#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 PIN2ADDR(0, EXPANDER_B_OUT_ADDR)
|
||||
#define POT_CS_B34 PIN2ADDR(1, EXPANDER_B_OUT_ADDR)
|
||||
|
||||
// --- SOFT START FORCE LINES ---
|
||||
#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 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 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 BOX_B (IN)
|
||||
// =====================
|
||||
#define EXPANDER_B_IN_ADDR 0xFF
|
||||
|
||||
#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)
|
||||
// --- STATUS / BUTTON ---
|
||||
#define BTN_3 10
|
||||
#define BTN_4 11
|
||||
#define STA_1 12
|
||||
#define STA_2 13
|
||||
#define STA_3 14
|
||||
#define STA_4 15
|
||||
|
||||
// Init Pin Functions
|
||||
inline void initTriggerPinsInputs()
|
||||
|
||||
@@ -65,7 +65,7 @@
|
||||
#define RST_EXT_A34N 3
|
||||
|
||||
// --- RELAY ---
|
||||
#define SH_ARM_A34 8
|
||||
#define A_EXT_RELAY 8
|
||||
|
||||
|
||||
// Init Pin Functions
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include "esp_heap_caps.h"
|
||||
|
||||
// Allocator custom per PSRAM
|
||||
template <typename T>
|
||||
struct PSRAMAllocator {
|
||||
using value_type = T;
|
||||
|
||||
PSRAMAllocator() noexcept {}
|
||||
|
||||
template <typename U>
|
||||
PSRAMAllocator(const PSRAMAllocator<U>&) noexcept {}
|
||||
|
||||
T* allocate(std::size_t n) {
|
||||
void* ptr = heap_caps_malloc(n * sizeof(T), MALLOC_CAP_SPIRAM);
|
||||
if (!ptr) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
return static_cast<T*>(ptr);
|
||||
}
|
||||
|
||||
void deallocate(T* p, std::size_t) noexcept {
|
||||
heap_caps_free(p);
|
||||
}
|
||||
};
|
||||
+98
-74
@@ -1,13 +1,8 @@
|
||||
#include "tasks.h"
|
||||
#include <esp_timer.h>
|
||||
#include <datasave.h>
|
||||
#include <mutex>
|
||||
|
||||
//// GLOBAL STATIC FUNCTIONS
|
||||
|
||||
// Timeout callback for microsecond precision
|
||||
void spark_timeout_callback(void *arg)
|
||||
{
|
||||
void spark_timeout_callback(void* arg) {
|
||||
TaskHandle_t handle = (TaskHandle_t)arg;
|
||||
xTaskNotify(handle, SPARK_FLAG_TIMEOUT, eSetValueWithOverwrite);
|
||||
}
|
||||
@@ -39,11 +34,12 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
|
||||
LOG_ERROR("Null rt_task_ptr parameters");
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
LOG_INFO("rtTask Params OK");
|
||||
|
||||
// Task Parameters and Devices
|
||||
rtTaskParams *params = (rtTaskParams *)pvParameters;
|
||||
const rtTaskInterruptParams rt_int = params->rt_int; // copy to avoid external override
|
||||
const rtTaskIOParams rt_rst = params->rt_io; // copy to avoid external override
|
||||
const rtTaskInterrupts rt_int = params->rt_int; // copy to avoid external override
|
||||
const rtTaskResets rt_rst = params->rt_resets; // copy to avoid external override
|
||||
QueueHandle_t rt_queue = params->rt_queue;
|
||||
Devices *dev = params->dev;
|
||||
ExternalIO *io = dev->m_ext_io;
|
||||
@@ -60,27 +56,27 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
|
||||
ignitionBoxStatus ign_box_sts;
|
||||
|
||||
// Variables for ISR, static to be fixed in memory locations
|
||||
isrParams isr_params_t12p{
|
||||
static isrParams isr_params_t12p{
|
||||
.flag = TRIG_FLAG_12P,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_task_info.xHandle};
|
||||
isrParams isr_params_t12n{
|
||||
.rt_handle_ptr = rt_handle_ptr};
|
||||
static isrParams isr_params_t12n{
|
||||
.flag = TRIG_FLAG_12N,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_task_info.xHandle};
|
||||
isrParams isr_params_t34p{
|
||||
.rt_handle_ptr = rt_handle_ptr};
|
||||
static isrParams isr_params_t34p{
|
||||
.flag = TRIG_FLAG_34P,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_task_info.xHandle};
|
||||
isrParams isr_params_t34n{
|
||||
.rt_handle_ptr = rt_handle_ptr};
|
||||
static isrParams isr_params_t34n{
|
||||
.flag = TRIG_FLAG_34N,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_task_info.xHandle};
|
||||
isrParams isr_params_sp12{
|
||||
.rt_handle_ptr = rt_handle_ptr};
|
||||
static isrParams isr_params_sp12{
|
||||
.flag = SPARK_FLAG_12,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_task_info.xHandle};
|
||||
isrParams isr_params_sp34{
|
||||
.rt_handle_ptr = rt_handle_ptr};
|
||||
static isrParams isr_params_sp34{
|
||||
.flag = SPARK_FLAG_34,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_task_info.xHandle};
|
||||
@@ -89,7 +85,7 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
|
||||
esp_timer_handle_t timeout_timer;
|
||||
esp_timer_create_args_t timer_args = {
|
||||
.callback = spark_timeout_callback,
|
||||
.arg = (void *)rt_task_info.xHandle,
|
||||
.arg = (void*)rt_handle_ptr,
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
.name = "spark_timeout"};
|
||||
if (esp_timer_create(&timer_args, &timeout_timer) != ESP_OK)
|
||||
@@ -106,14 +102,19 @@ 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_34), rt_int.isr_ptr, (void *)&isr_params_sp34, RISING);
|
||||
|
||||
LOG_INFO("rtTask ISR Attach OK [", params->name.c_str(), "]");
|
||||
LOG_INFO("rtTask ISR Attach OK");
|
||||
|
||||
// Compute Reset Pin Bitmask
|
||||
const uint16_t rst_bitmask = (1 << rt_rst.rst_io_12p) |
|
||||
(1 << rt_rst.rst_io_12n) |
|
||||
(1 << rt_rst.rst_io_34p) |
|
||||
(1 << rt_rst.rst_io_34n);
|
||||
|
||||
LOG_WARN("rtTask Init Correct");
|
||||
// Global rt_task_ptr variables
|
||||
bool first_cycle = true;
|
||||
bool cycle12 = false;
|
||||
bool cycle34 = false;
|
||||
int64_t last_cycle_time = 0;
|
||||
uint32_t n_errors = 0;
|
||||
|
||||
while (params->rt_running)
|
||||
{
|
||||
@@ -126,45 +127,72 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
|
||||
ULONG_MAX, // pulisci i primi 8 bit
|
||||
&pickup_flag, // valore ricevuto
|
||||
portMAX_DELAY);
|
||||
|
||||
|
||||
if (first_cycle && pickup_flag != TRIG_FLAG_12P) // skip first cycle because of possible initial noise on pickup signals at startu
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.print("\033[2J"); // clear screen
|
||||
Serial.print("\033[H"); // cursor home
|
||||
LOG_INFO("Iteration [", it++, "]");
|
||||
|
||||
if (!names.contains(pickup_flag))
|
||||
{
|
||||
LOG_ERROR("Wrong Pickup Flag");
|
||||
LOG_ERROR("Pickup Flags: ", printBits(pickup_flag).c_str());
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_INFO("Pickup Trigger: ", names.at(pickup_flag));
|
||||
}
|
||||
#endif
|
||||
|
||||
// Start microsecond precision timeout timer
|
||||
esp_timer_stop(timeout_timer); // stop timer in case it was running from previous cycle
|
||||
esp_timer_start_once(timeout_timer, spark_timeout_max);
|
||||
|
||||
spark_flag = SPARK_FLAG_NIL; // default value in case of timeout, to be set by ISR if spark event occours
|
||||
// WAIT FOR SPARK TO HAPPEN OR TIMEOUT
|
||||
xTaskNotifyWait(
|
||||
0x00, // non pulire all'ingresso
|
||||
ULONG_MAX, // pulisci i primi 8 bit
|
||||
&spark_flag, // valore ricevuto
|
||||
BaseType_t sp = pdFALSE;
|
||||
sp = xTaskNotifyWait(
|
||||
0x00, // non pulire all'ingresso
|
||||
ULONG_MAX, // pulisci i primi 8 bit
|
||||
&spark_flag, // valore ricevuto
|
||||
portMAX_DELAY); // wait indefinitely, timeout handled by esp_timer
|
||||
|
||||
// Handle timeout or spark event
|
||||
if (spark_flag != SPARK_FLAG_TIMEOUT)
|
||||
if (spark_flag == SPARK_FLAG_TIMEOUT) {
|
||||
spark_flag = SPARK_FLAG_NIL;
|
||||
} else {
|
||||
// Spark occurred, stop the timer
|
||||
esp_timer_stop(timeout_timer);
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
// LOG_INFO("Spark Flags: ", printBits(spark_flag).c_str());
|
||||
LOG_INFO("Spark12:", ign_box_sts.coils12.spark_ok ? "TRUE" : "FALSE");
|
||||
LOG_INFO("Spark34:", ign_box_sts.coils34.spark_ok ? "TRUE" : "FALSE");
|
||||
if (names.contains(spark_flag))
|
||||
LOG_INFO("Spark Trigger:", names.at(spark_flag));
|
||||
#endif
|
||||
xTaskNotifyStateClear(NULL);
|
||||
ulTaskNotifyValueClear(NULL, 0xFFFFFFFF);
|
||||
|
||||
// A trigger from pickup 12 is followed by a spark event on 34 or vice versa pickup 34 triggers spark on 12
|
||||
if ((pickup_flag == TRIG_FLAG_12P || pickup_flag == TRIG_FLAG_12N) && (spark_flag != SPARK_FLAG_12 && spark_flag != SPARK_FLAG_TIMEOUT))
|
||||
if ((pickup_flag == TRIG_FLAG_12P || pickup_flag == TRIG_FLAG_12N) && (spark_flag != SPARK_FLAG_12 && spark_flag != SPARK_FLAG_NIL))
|
||||
{
|
||||
ign_box_sts.coils12.spark_status = ign_box_sts.coils34.spark_status = sparkStatus::SPARK_SYNC_FAIL;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Select coil status reference based on pickup_flag
|
||||
coilsStatus *coils;
|
||||
switch (pickup_flag)
|
||||
{
|
||||
case TRIG_FLAG_12P:
|
||||
{
|
||||
first_cycle = false;
|
||||
// compute engine rpm from cycle time
|
||||
auto current_time = esp_timer_get_time();
|
||||
auto cycle_time = current_time - last_cycle_time;
|
||||
last_cycle_time = current_time;
|
||||
ign_box_sts.eng_rpm = (int32_t)(60.0f / (cycle_time / 1000000.0f));
|
||||
}
|
||||
case TRIG_FLAG_12N:
|
||||
coils = &ign_box_sts.coils12;
|
||||
break;
|
||||
@@ -174,21 +202,24 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
|
||||
break;
|
||||
}
|
||||
|
||||
// Select logic based on pickup and spark flags
|
||||
switch (pickup_flag)
|
||||
{
|
||||
case TRIG_FLAG_12P:
|
||||
case TRIG_FLAG_34P:
|
||||
{
|
||||
// Timeout not occourred, expected POSITIVE edge spark OCCOURRED
|
||||
if (spark_flag != SPARK_FLAG_TIMEOUT)
|
||||
if (spark_flag != SPARK_FLAG_NIL)
|
||||
{
|
||||
coils->spark_delay = (int32_t)(coils->spark_time - coils->trig_time);
|
||||
coils->spark_delay = coils->spark_time - coils->trig_time;
|
||||
coils->sstart_status = softStartStatus::NORMAL; // because spark on positive edge
|
||||
coils->spark_status = sparkStatus::SPARK_POS_OK; // do not wait for spark on negative edge
|
||||
#ifdef DEBUG
|
||||
LOG_INFO("Spark on POSITIVE pulse");
|
||||
LOG_INFO("Spark Delay Time: ", (int32_t)coils->spark_delay);
|
||||
#endif
|
||||
}
|
||||
// Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED
|
||||
else if (spark_flag == SPARK_FLAG_TIMEOUT)
|
||||
else if (spark_flag == SPARK_FLAG_NIL)
|
||||
{
|
||||
coils->spark_status = sparkStatus::SPARK_NEG_WAIT;
|
||||
coils->sstart_status = softStartStatus::NORMAL;
|
||||
@@ -201,20 +232,24 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
|
||||
{
|
||||
const bool expected_negative = coils->spark_status == sparkStatus::SPARK_NEG_WAIT;
|
||||
// Timeout not occourred, expected NEGATIVE edge spark OCCOURRED
|
||||
if (spark_flag != SPARK_FLAG_TIMEOUT && expected_negative)
|
||||
if (spark_flag != SPARK_FLAG_NIL && expected_negative)
|
||||
{
|
||||
coils->spark_delay = (int32_t)(coils->spark_time - coils->trig_time);
|
||||
coils->spark_delay = coils->spark_time - coils->trig_time;
|
||||
coils->sstart_status = softStartStatus::SOFT_START;
|
||||
coils->spark_status = sparkStatus::SPARK_NEG_OK;
|
||||
#ifdef DEBUG
|
||||
LOG_INFO("Spark on NEGATIVE pulse");
|
||||
LOG_INFO("Spark Delay Time: ", (int32_t)coils->spark_delay);
|
||||
#endif
|
||||
}
|
||||
// Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED
|
||||
else if (spark_flag == SPARK_FLAG_TIMEOUT && expected_negative)
|
||||
else if (spark_flag == SPARK_FLAG_NIL && expected_negative)
|
||||
{
|
||||
coils->sstart_status = softStartStatus::ERROR;
|
||||
coils->spark_status = sparkStatus::SPARK_NEG_FAIL;
|
||||
}
|
||||
// Timeout not occouured, unexpected negative edge spark
|
||||
else if (spark_flag != SPARK_FLAG_TIMEOUT && !expected_negative)
|
||||
else if (spark_flag != SPARK_FLAG_NIL && !expected_negative)
|
||||
{
|
||||
coils->sstart_status = softStartStatus::SOFT_START;
|
||||
coils->spark_status = sparkStatus::SPARK_NEG_UNEXPECTED;
|
||||
@@ -228,6 +263,11 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
|
||||
break;
|
||||
}
|
||||
default:
|
||||
#ifdef DEUG
|
||||
LOG_ERROR("Invalid Interrupt");
|
||||
LOG_ERROR("Pickup Flags: ", printBits(pickup_flag).c_str());
|
||||
LOG_ERROR("Spark Flags: ", printBits(spark_flag).c_str());
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -235,17 +275,10 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
|
||||
{
|
||||
cycle12 = false;
|
||||
cycle34 = false;
|
||||
|
||||
if (ign_box_sts.coils12.spark_status == sparkStatus::SPARK_POS_FAIL || ign_box_sts.coils12.spark_status == sparkStatus::SPARK_NEG_FAIL)
|
||||
ign_box_sts.coils12.n_missed_firing++;
|
||||
if (ign_box_sts.coils34.spark_status == sparkStatus::SPARK_POS_FAIL || ign_box_sts.coils34.spark_status == sparkStatus::SPARK_NEG_FAIL)
|
||||
ign_box_sts.coils34.n_missed_firing++;
|
||||
|
||||
// vTaskDelay(pdMS_TO_TICKS(1)); // delay 1ms to allow peak detectors to charge for negative cycle
|
||||
// read adc channels: pickup12, out12 [ pos + neg ]
|
||||
if (adc) // read only if adc initialized
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(spi_mutex);
|
||||
uint32_t start_adc_read = esp_timer_get_time();
|
||||
// from peak detector circuits
|
||||
ign_box_sts.coils12.peak_p_in = adc->convertToVoltage(adc->cycleSingle());
|
||||
ign_box_sts.coils12.peak_n_in = adc->convertToVoltage(adc->cycleSingle());
|
||||
@@ -259,42 +292,33 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
|
||||
adc->stopConversion();
|
||||
}
|
||||
else // simulate adc read timig
|
||||
vTaskDelay(pdMS_TO_TICKS(c_adc_time));
|
||||
vTaskDelay(pdMS_TO_TICKS(1));
|
||||
|
||||
// reset peak detectors + sample and hold
|
||||
// outputs on io expander
|
||||
if (io)
|
||||
{
|
||||
// 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);
|
||||
const uint16_t iostat = io->read();
|
||||
io->write(iostat | rst_bitmask);
|
||||
vTaskDelay(pdMS_TO_TICKS(1));
|
||||
io->write(iostat & ~rst_bitmask);
|
||||
}
|
||||
else
|
||||
vTaskDelay(pdMS_TO_TICKS(c_io_time));
|
||||
vTaskDelay(pdMS_TO_TICKS(1));
|
||||
|
||||
// send essage to main loop with ignition info, by copy so local static variable is ok
|
||||
if (rt_queue)
|
||||
{
|
||||
ign_box_sts.timestamp = esp_timer_get_time(); // update data timestamp
|
||||
if (xQueueSendToBack(rt_queue, (void *)&ign_box_sts, 0) != pdPASS)
|
||||
ign_box_sts.n_queue_errors = ++n_errors;
|
||||
if (xQueueSendToBack(rt_queue, (void *)&ign_box_sts, 0) != pdPASS)
|
||||
{
|
||||
ign_box_sts.n_queue_errors++;
|
||||
LOG_ERROR("Failed to send to rt_queue");
|
||||
}
|
||||
}
|
||||
}
|
||||
// Delete the timeout timer
|
||||
esp_timer_delete(timeout_timer);
|
||||
LOG_WARN("rtTask Ending [", params->name.c_str(), "]");
|
||||
LOG_WARN("Ending realTime Task");
|
||||
// Ignition A Interrupts DETACH
|
||||
detachInterrupt(rt_int.trig_pin_12p);
|
||||
detachInterrupt(rt_int.trig_pin_12n);
|
||||
|
||||
@@ -2,19 +2,12 @@
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
||||
|
||||
// Serial debug flag
|
||||
// #define DEBUG
|
||||
//#define DEBUG
|
||||
|
||||
// Arduino Libraries
|
||||
#include <Arduino.h>
|
||||
#include <DebugLog.h>
|
||||
#include "utils.h"
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <filesystem>
|
||||
#include <FS.h>
|
||||
#include <LittleFS.h>
|
||||
#include <datasave.h>
|
||||
#include <functional>
|
||||
|
||||
// ISR
|
||||
#include "isr.h"
|
||||
@@ -38,7 +31,8 @@ static const std::map<const uint32_t, const char *> names = {
|
||||
};
|
||||
#endif
|
||||
|
||||
class rtIgnitionTask
|
||||
// RT task Interrupt parameters
|
||||
struct rtTaskInterrupts
|
||||
{
|
||||
using PSHistory = PSRAMVector<ignitionBoxStatus>;
|
||||
// using PSHistory = std::vector<ignitionBoxStatus>;
|
||||
@@ -159,3 +153,25 @@ private:
|
||||
static const uint8_t c_adc_time = 4; // in mS
|
||||
static const uint8_t c_io_time = 2; // in mS
|
||||
};
|
||||
|
||||
// RT Task Peak Detector Reset pins
|
||||
struct rtTaskResets
|
||||
{
|
||||
const uint8_t rst_io_12p;
|
||||
const uint8_t rst_io_12n;
|
||||
const uint8_t rst_io_34p;
|
||||
const uint8_t rst_io_34n;
|
||||
};
|
||||
|
||||
// RT task parameters
|
||||
struct rtTaskParams
|
||||
{
|
||||
bool rt_running; // run flag, false to terminate
|
||||
Devices *dev;
|
||||
TaskHandle_t* rt_handle_ptr;
|
||||
const QueueHandle_t rt_queue;
|
||||
const rtTaskInterrupts rt_int; // interrupt pins to attach
|
||||
const rtTaskResets rt_resets; // reset ping for peak detectors
|
||||
};
|
||||
|
||||
void rtIgnitionTask(void *pvParameters);
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
#include <ui.h>
|
||||
|
||||
void clearScreen()
|
||||
{
|
||||
Serial.print("\033[2J"); // clear screen
|
||||
Serial.print("\033[H"); // cursor home
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
void setCursor(const uint8_t x, const uint8_t y)
|
||||
{
|
||||
Serial.printf("\033[%d;%d", y, x + 1);
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
void printField(const char name[], const uint32_t val)
|
||||
{
|
||||
Serial.printf("%15s: %06u\n", name, val);
|
||||
}
|
||||
|
||||
void printField(const char name[], const int32_t val)
|
||||
{
|
||||
Serial.printf("%15s: %06d\n", name, val);
|
||||
}
|
||||
|
||||
void printField(const char name[], const int64_t val)
|
||||
{
|
||||
Serial.printf("%15s: %06u\n", name, (uint64_t)val);
|
||||
}
|
||||
|
||||
void printField(const char name[], const float val)
|
||||
{
|
||||
Serial.printf("%15s: %4.2f\n", name, val);
|
||||
}
|
||||
|
||||
void printField(const char name[], const char *val)
|
||||
{
|
||||
Serial.printf("%15s: %s\n", name, val);
|
||||
}
|
||||
|
||||
void printInfo(const ignitionBoxStatus &info)
|
||||
{
|
||||
clearScreen();
|
||||
setCursor(0, 0);
|
||||
printField("++ Timestamp ++", (uint32_t)info.timestamp);
|
||||
Serial.println("========== Coils 12 =============");
|
||||
printField("Events", info.coils12.n_events);
|
||||
printField("Events Missed", info.coils12.n_missed_firing);
|
||||
printField("Spark Dly", (uint32_t)info.coils12.spark_delay);
|
||||
printField("Spark Sts", sparkStatusNames.at(info.coils12.spark_status));
|
||||
printField("Peak P_IN", info.coils12.peak_p_in);
|
||||
printField("Peak N_IN", info.coils12.peak_n_in);
|
||||
printField("Peak P_OUT", info.coils12.peak_p_out);
|
||||
printField("Peak N_OUT", info.coils12.peak_n_out);
|
||||
printField("Soft Start ", softStartStatusNames.at(info.coils12.sstart_status));
|
||||
|
||||
Serial.println("========== Coils 34 =============");
|
||||
printField("Events", info.coils34.n_events);
|
||||
printField("Events Missed", info.coils34.n_missed_firing);
|
||||
printField("Spark Dly", (uint32_t)info.coils34.spark_delay);
|
||||
printField("Spark Sts", sparkStatusNames.at(info.coils34.spark_status));
|
||||
printField("Peak P_IN", info.coils34.peak_p_in);
|
||||
printField("Peak N_IN", info.coils34.peak_n_in);
|
||||
printField("Peak P_OUT", info.coils34.peak_p_out);
|
||||
printField("Peak N_OUT", info.coils34.peak_n_out);
|
||||
printField("Soft Start ", softStartStatusNames.at(info.coils34.sstart_status));
|
||||
|
||||
Serial.println("============ END ===============");
|
||||
Serial.println();
|
||||
printField("Engine RPM", info.eng_rpm);
|
||||
printField("ADC Read Time", info.adc_read_time);
|
||||
printField("Queue Errors", info.n_queue_errors);
|
||||
}
|
||||
+53
-10
@@ -1,16 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <datastruct.h>
|
||||
#include <string>
|
||||
|
||||
void clearScreen();
|
||||
void setCursor(const uint8_t x, const uint8_t y);
|
||||
void printField(const char name[], const uint32_t val);
|
||||
void printField(const char name[], const int32_t val);
|
||||
void printField(const char name[], const int64_t val);
|
||||
void printField(const char name[], const float val);
|
||||
void printField(const char name[], const char *val);
|
||||
static bool firstRun = true;
|
||||
|
||||
void printInfo(const ignitionBoxStatus &info);
|
||||
void clearScreen(){
|
||||
Serial.print("\033[2J"); // clear screen
|
||||
Serial.print("\033[H"); // cursor home
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
void setCursor(const uint8_t x, const uint8_t y) {
|
||||
Serial.printf("\033[%d;%d", y, x+1);
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
void printField(const char name[], const uint32_t val, const uint8_t x, const uint8_t y) {
|
||||
if (firstRun) {
|
||||
setCursor(x,y);
|
||||
Serial.printf("%15s: %06d\n", name, val);
|
||||
return;
|
||||
}
|
||||
setCursor(x+16, y);
|
||||
Serial.print(val);
|
||||
}
|
||||
|
||||
void printField(const char name[], const int64_t val, const uint8_t x, const uint8_t y) {
|
||||
if (firstRun) {
|
||||
setCursor(x,y);
|
||||
Serial.printf("%15s: %06u\n", name, (uint64_t)val);
|
||||
return;
|
||||
}
|
||||
setCursor(x+16, y);
|
||||
Serial.print((uint64_t)val);
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
void printField(const char name[], const float val, const uint8_t x, const uint8_t y) {
|
||||
if (firstRun) {
|
||||
setCursor(x,y);
|
||||
Serial.printf("%15s: %4.2f\n", name, val);
|
||||
return;
|
||||
}
|
||||
setCursor(x+16, y);
|
||||
Serial.print(val);
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
void printField(const char name[], const char *val, const uint8_t x, const uint8_t y) {
|
||||
if (firstRun) {
|
||||
setCursor(x,y);
|
||||
Serial.printf("%15s: %s\n", name, val);
|
||||
return;
|
||||
}
|
||||
setCursor(x+16, y);
|
||||
Serial.print(val);
|
||||
Serial.flush();
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
#include "utils.h"
|
||||
#include "freertos_stats.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
<<<<<<< Updated upstream
|
||||
std::string printBits(uint32_t value) {
|
||||
=======
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/portable.h"
|
||||
|
||||
@@ -19,14 +20,13 @@
|
||||
|
||||
std::string printBits(uint32_t value)
|
||||
{
|
||||
>>>>>>> Stashed changes
|
||||
std::string result;
|
||||
for (int i = 31; i >= 0; i--)
|
||||
{
|
||||
for (int i = 31; i >= 0; i--) {
|
||||
// ottieni il singolo bit
|
||||
result += ((value >> i) & 1) ? '1' : '0';
|
||||
// aggiungi uno spazio ogni 8 bit, tranne dopo l'ultimo
|
||||
if (i % 8 == 0 && i != 0)
|
||||
{
|
||||
if (i % 8 == 0 && i != 0) {
|
||||
result += ' ';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,5 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <string>
|
||||
#include <datastruct.h>
|
||||
|
||||
std::string printBits(uint32_t value);
|
||||
|
||||
void printRunningTasksMod(Print &printer, std::function<bool(const TaskStatus_t &a, const TaskStatus_t &b)> orderBy = nullptr);
|
||||
|
||||
inline void swapHistory(PSRAMVector<ignitionBoxStatus>* active, PSRAMVector<ignitionBoxStatus>* writable) {
|
||||
auto *temp = active;
|
||||
active = writable; // switch active and writable buffers
|
||||
writable = temp; // ensure writable_history points to the buffer we just filled
|
||||
}
|
||||
|
||||
@@ -1,166 +0,0 @@
|
||||
#include <webserver.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
static std::map<const std::string, AstroWebServer::c_commandEnum> s_webserverCommands = {
|
||||
{"setTime", AstroWebServer::SET_TIME},
|
||||
};
|
||||
|
||||
void on_ping(TimerHandle_t xTimer)
|
||||
{
|
||||
if (!xTimer)
|
||||
return;
|
||||
auto ws = (AsyncWebSocket *)pvTimerGetTimerID(xTimer);
|
||||
ws->pingAll();
|
||||
ws->cleanupClients();
|
||||
}
|
||||
|
||||
AstroWebServer::AstroWebServer(const uint8_t port, fs::FS &filesystem) : m_port(port), m_webserver(AsyncWebServer(port)), m_websocket(AsyncWebSocket("/ws")), m_filesystem(filesystem)
|
||||
{
|
||||
LOG_DEBUG("Initializing Web Server");
|
||||
m_websocket.onEvent([this](AsyncWebSocket *server, AsyncWebSocketClient *client,
|
||||
AwsEventType type, void *arg, uint8_t *data, size_t len)
|
||||
{ onWsEvent(server, client, type, arg, data, len); });
|
||||
|
||||
m_webserver.addHandler(&m_websocket);
|
||||
m_webserver.serveStatic("/", m_filesystem, "/").setDefaultFile("index.html");
|
||||
|
||||
m_webserver.on("/upload", HTTP_POST, [this](AsyncWebServerRequest *request)
|
||||
{ onUploadRequest(request); }, [this](AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final)
|
||||
{ onUploadHandler(request, filename, index, data, len, final); });
|
||||
|
||||
m_webserver.begin();
|
||||
m_websocket.enable(true);
|
||||
|
||||
m_pingTimer = xTimerCreate("wsPingTimer", pdMS_TO_TICKS(2000), pdTRUE, (void *)&m_websocket, on_ping);
|
||||
LOG_DEBUG("Webserver Init OK");
|
||||
}
|
||||
|
||||
AstroWebServer::~AstroWebServer()
|
||||
{
|
||||
xTimerDelete(m_pingTimer, pdMS_TO_TICKS(10));
|
||||
m_webserver.removeHandler(&m_websocket);
|
||||
m_webserver.end();
|
||||
}
|
||||
|
||||
void AstroWebServer::sendWsData(const String &data)
|
||||
{
|
||||
if (m_websocket.count())
|
||||
{
|
||||
m_websocket.textAll(data);
|
||||
}
|
||||
}
|
||||
|
||||
void AstroWebServer::onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case WS_EVT_CONNECT:
|
||||
LOG_DEBUG("WS client IP[", client->remoteIP().toString().c_str(), "]-ID[", client->id(), "] CONNECTED");
|
||||
break;
|
||||
case WS_EVT_DISCONNECT:
|
||||
LOG_DEBUG("WS client IP[", client->remoteIP().toString().c_str(), "]-ID[", client->id(), "] DISCONNECTED");
|
||||
break;
|
||||
case WS_EVT_PONG:
|
||||
LOG_DEBUG("WS client IP[", client->remoteIP().toString().c_str(), "]-ID[", client->id(), "] PONG");
|
||||
break;
|
||||
case WS_EVT_DATA:
|
||||
{
|
||||
AwsFrameInfo *info = (AwsFrameInfo *)arg;
|
||||
if (info->final && info->index == 0 && info->len == len)
|
||||
{
|
||||
std::string data_str((char *)data, len);
|
||||
ArduinoJson::JsonDocument doc;
|
||||
if (auto rv = ArduinoJson::deserializeJson(doc, data_str) != ArduinoJson::DeserializationError::Ok)
|
||||
{
|
||||
LOG_ERROR("WS Client unable to deserialize Json");
|
||||
return;
|
||||
}
|
||||
if (!doc["cmd"].is<std::string>() || !s_webserverCommands.contains(doc["cmd"]))
|
||||
{
|
||||
LOG_WARN("WS Client Invalid Json command [", doc["cmd"].as<std::string>().c_str(), "]");
|
||||
return;
|
||||
}
|
||||
std::string buffer;
|
||||
switch (s_webserverCommands.at(doc["cmd"]))
|
||||
{
|
||||
case SET_TIME:
|
||||
{
|
||||
auto epoch = doc["time"].as<time_t>();
|
||||
timeval te{
|
||||
.tv_sec = epoch,
|
||||
.tv_usec = 0,
|
||||
};
|
||||
timezone tz{
|
||||
.tz_minuteswest = 0,
|
||||
.tz_dsttime = DST_MET,
|
||||
};
|
||||
settimeofday(&te, &tz);
|
||||
time_t now = time(nullptr);
|
||||
struct tm *t = localtime(&now);
|
||||
buffer.resize(64);
|
||||
strftime(buffer.data(), sizeof(buffer), "%Y-%m-%d %H:%M:%S", t);
|
||||
LOG_DEBUG("WS Client set Datetime to: ", buffer.c_str());
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
// call external command callback
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AstroWebServer::onUploadRequest(AsyncWebServerRequest *request)
|
||||
{
|
||||
if (m_uploadFailed)
|
||||
request->send(500, "text/plain", "Upload failed");
|
||||
else
|
||||
request->send(200, "text/plain", "Upload successful");
|
||||
}
|
||||
|
||||
void AstroWebServer::onUploadHandler(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final)
|
||||
{
|
||||
if (index == 0) // only on first iteration to open file
|
||||
{
|
||||
m_uploadFailed = false;
|
||||
String safeName = filename;
|
||||
int slashIndex = safeName.lastIndexOf('/');
|
||||
if (slashIndex >= 0)
|
||||
safeName = safeName.substring(slashIndex + 1);
|
||||
if (safeName.length() == 0)
|
||||
{
|
||||
m_uploadFailed = true;
|
||||
LOG_ERROR("Invalid file name");
|
||||
return;
|
||||
}
|
||||
|
||||
const std::filesystem::path filePath = std::filesystem::path(m_filesystem.mountpoint()) / safeName.c_str();
|
||||
if (m_filesystem.exists(filePath.c_str()))
|
||||
m_filesystem.remove(filePath.c_str());
|
||||
|
||||
m_uploadFile = m_filesystem.open(filePath.c_str(), FILE_WRITE);
|
||||
if (!m_uploadFile)
|
||||
{
|
||||
m_uploadFailed = true;
|
||||
LOG_ERROR("Failed to open upload file:", filePath.c_str());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Actual write of file data
|
||||
if (!m_uploadFailed && m_uploadFile)
|
||||
{
|
||||
if (m_uploadFile.write(data, len) != len)
|
||||
m_uploadFailed = true;
|
||||
}
|
||||
|
||||
// close the file and save on final call
|
||||
if (final && m_uploadFile)
|
||||
{
|
||||
m_uploadFile.close();
|
||||
if (!m_uploadFailed)
|
||||
LOG_INFO("Uploaded file to LittleFS:", filename.c_str());
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
#pragma once
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
||||
|
||||
// System includes
|
||||
#include <Arduino.h>
|
||||
#include <DebugLog.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <AsyncTCP.h>
|
||||
#include <filesystem>
|
||||
#include <map>
|
||||
|
||||
#include <FS.h>
|
||||
|
||||
class AstroWebServer
|
||||
{
|
||||
|
||||
public:
|
||||
AstroWebServer(const uint8_t port, fs::FS &filesystem);
|
||||
~AstroWebServer();
|
||||
|
||||
void sendWsData(const String &data);
|
||||
|
||||
private:
|
||||
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
|
||||
AwsEventType type, void *arg, uint8_t *data, size_t len);
|
||||
|
||||
void onUploadRequest(AsyncWebServerRequest *request);
|
||||
void onUploadHandler(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final);
|
||||
|
||||
void onStart(AsyncWebServerRequest *request);
|
||||
void onStop(AsyncWebServerRequest *request);
|
||||
void onDownload(AsyncWebServerRequest *request);
|
||||
|
||||
private:
|
||||
const uint8_t m_port = 80;
|
||||
fs::FS &m_filesystem;
|
||||
AsyncWebServer m_webserver;
|
||||
AsyncWebSocket m_websocket;
|
||||
bool m_uploadFailed = false;
|
||||
fs::File m_uploadFile;
|
||||
TimerHandle_t m_pingTimer = NULL;
|
||||
|
||||
public:
|
||||
enum c_commandEnum
|
||||
{
|
||||
SET_TIME
|
||||
};
|
||||
};
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,188 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Astro Rotax Monitor</title>
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header class="page-header">
|
||||
<div class="header-content">
|
||||
<img src="logo_astro_dev.svg" alt="Astro Tecnologie" class="logo">
|
||||
</div>
|
||||
<div>
|
||||
|
||||
<h1>Rotax Ignition Box Monitor</h1>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div id="loadingIndicator" class="loading-indicator">
|
||||
<span class="spinner"></span> Waiting for data...
|
||||
</div>
|
||||
|
||||
<div class="tables-container">
|
||||
<div class="box">
|
||||
<h2>Box_A</h2>
|
||||
<div class="box-data">
|
||||
<p><strong>Timestamp:</strong> <span id="a_timestamp">-</span></p>
|
||||
<p><strong>Data Valid:</strong> <span id="a_datavalid">-</span></p>
|
||||
<p><strong>Generator voltage:</strong> <span id="a_volts_gen">-</span></p>
|
||||
<p><strong>ADC read time:</strong> <span id="a_adc_read_time">-</span></p>
|
||||
<p><strong>Queue errors:</strong> <span id="a_n_queue_errors">-</span></p>
|
||||
</div>
|
||||
<div class="rpm-highlight">
|
||||
<strong>Engine RPM:</strong> <span id="a_eng_rpm">-</span>
|
||||
</div>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Property</th>
|
||||
<th>Pickup 12</th>
|
||||
<th>Pickup 34</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>Spark delay</td>
|
||||
<td id="a_coils12_spark_delay">-</td>
|
||||
<td id="a_coils34_spark_delay">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Spark status</td>
|
||||
<td id="a_coils12_spark_status">-</td>
|
||||
<td id="a_coils34_spark_status">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Soft start status</td>
|
||||
<td id="a_coils12_sstart_status">-</td>
|
||||
<td id="a_coils34_sstart_status">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak P in</td>
|
||||
<td id="a_coils12_peak_p_in">-</td>
|
||||
<td id="a_coils34_peak_p_in">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak N in</td>
|
||||
<td id="a_coils12_peak_n_in">-</td>
|
||||
<td id="a_coils34_peak_n_in">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak P out</td>
|
||||
<td id="a_coils12_peak_p_out">-</td>
|
||||
<td id="a_coils34_peak_p_out">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak N out</td>
|
||||
<td id="a_coils12_peak_n_out">-</td>
|
||||
<td id="a_coils34_peak_n_out">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Level spark</td>
|
||||
<td id="a_coils12_level_spark">-</td>
|
||||
<td id="a_coils34_level_spark">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Spark Events</td>
|
||||
<td id="a_coils12_n_events">-</td>
|
||||
<td id="a_coils34_n_events">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Missed Events</td>
|
||||
<td id="a_coils12_n_missed_firing">-</td>
|
||||
<td id="a_coils34_n_missed_firing">-</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div class="box">
|
||||
<h2>Box_B</h2>
|
||||
<div class="box-data">
|
||||
<p><strong>Timestamp:</strong> <span id="b_timestamp">-</span></p>
|
||||
<p><strong>Data Valid:</strong> <span id="b_datavalid">-</span></p>
|
||||
<p><strong>Generator voltage:</strong> <span id="b_volts_gen">-</span></p>
|
||||
<p><strong>ADC read time:</strong> <span id="b_adc_read_time">-</span></p>
|
||||
<p><strong>Queue errors:</strong> <span id="b_n_queue_errors">-</span></p>
|
||||
</div>
|
||||
<div class="rpm-highlight">
|
||||
<strong>Engine RPM:</strong> <span id="b_eng_rpm">-</span>
|
||||
</div>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Property</th>
|
||||
<th>Pickup 12</th>
|
||||
<th>Pickup 34</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>Spark delay</td>
|
||||
<td id="b_coils12_spark_delay">-</td>
|
||||
<td id="b_coils34_spark_delay">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Spark status</td>
|
||||
<td id="b_coils12_spark_status">-</td>
|
||||
<td id="b_coils34_spark_status">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Soft start status</td>
|
||||
<td id="b_coils12_sstart_status">-</td>
|
||||
<td id="b_coils34_sstart_status">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak P in</td>
|
||||
<td id="b_coils12_peak_p_in">-</td>
|
||||
<td id="b_coils34_peak_p_in">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak N in</td>
|
||||
<td id="b_coils12_peak_n_in">-</td>
|
||||
<td id="b_coils34_peak_n_in">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak P out</td>
|
||||
<td id="b_coils12_peak_p_out">-</td>
|
||||
<td id="b_coils34_peak_p_out">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak N out</td>
|
||||
<td id="b_coils12_peak_n_out">-</td>
|
||||
<td id="b_coils34_peak_n_out">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Level spark</td>
|
||||
<td id="b_coils12_level_spark">-</td>
|
||||
<td id="b_coils34_level_spark">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Spark Events</td>
|
||||
<td id="b_coils12_n_events">-</td>
|
||||
<td id="b_coils34_n_events">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Missed Events</td>
|
||||
<td id="b_coils12_n_missed_firing">-</td>
|
||||
<td id="b_coils34_n_missed_firing">-</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="upload-section">
|
||||
<h3>Upload file to Flash</h3>
|
||||
<p>Select a file and upload it to Flash.</p>
|
||||
<input type="file" id="littlefsFile">
|
||||
<button onclick="uploadLittleFS()">Upload</button>
|
||||
<div id="uploadStatus" class="upload-status">No file uploaded yet.</div>
|
||||
</div>
|
||||
|
||||
<script src="script.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 31 KiB |
@@ -3,26 +3,6 @@ let lastMessageTimestamp = 0;
|
||||
const IDLE_THRESHOLD_MS = 1000;
|
||||
const loadingIndicator = document.getElementById("loadingIndicator");
|
||||
|
||||
let chartA, chartB;
|
||||
|
||||
let dataA = {
|
||||
labels: [],
|
||||
datasets: [
|
||||
{ label: "RPM", data: [] },
|
||||
{ label: "Coils12 Delay", data: [] },
|
||||
{ label: "Coils34 Delay", data: [] }
|
||||
]
|
||||
};
|
||||
|
||||
let dataB = {
|
||||
labels: [],
|
||||
datasets: [
|
||||
{ label: "RPM", data: [] },
|
||||
{ label: "Coils12 Delay", data: [] },
|
||||
{ label: "Coils34 Delay", data: [] }
|
||||
]
|
||||
};
|
||||
|
||||
function setLoadingIndicator(visible) {
|
||||
if (!loadingIndicator) {
|
||||
return;
|
||||
@@ -45,11 +25,6 @@ function connectWS() {
|
||||
console.log("WebSocket connesso");
|
||||
lastMessageTimestamp = Date.now();
|
||||
setLoadingIndicator(false);
|
||||
|
||||
ws.send(JSON.stringify({
|
||||
cmd: "setTime",
|
||||
time: Math.floor(Date.now() / 1000)
|
||||
}));
|
||||
};
|
||||
|
||||
ws.onclose = () => {
|
||||
@@ -71,8 +46,6 @@ function connectWS() {
|
||||
lastMessageTimestamp = Date.now();
|
||||
setLoadingIndicator(false);
|
||||
|
||||
updateCharts(data)
|
||||
|
||||
// Update Box_A
|
||||
if (data.box_a) {
|
||||
const boxA = data.box_a;
|
||||
@@ -145,51 +118,6 @@ function connectWS() {
|
||||
};
|
||||
}
|
||||
|
||||
function updateCharts(data) {
|
||||
|
||||
const t = new Date().toLocaleTimeString();
|
||||
|
||||
// ===== BOX A =====
|
||||
dataA.labels.push(t);
|
||||
if (data.box_a) {
|
||||
dataA.datasets[0].data.push(data.box_a.eng_rpm / 10);
|
||||
dataA.datasets[1].data.push(data.box_a.coils12.spark_delay);
|
||||
dataA.datasets[2].data.push(data.box_a.coils34.spark_delay);
|
||||
} else {
|
||||
dataA.datasets[0].data.push(undefined);
|
||||
dataA.datasets[1].data.push(undefined);
|
||||
dataA.datasets[2].data.push(undefined);
|
||||
}
|
||||
|
||||
// ===== BOX B =====
|
||||
dataB.labels.push(t);
|
||||
if (data.box_b) {
|
||||
dataB.datasets[0].data.push(data.box_b.eng_rpm / 10);
|
||||
dataB.datasets[1].data.push(data.box_b.coils12.spark_delay);
|
||||
dataB.datasets[2].data.push(data.box_b.coils34.spark_delay);
|
||||
} else {
|
||||
dataB.datasets[0].data.push(undefined);
|
||||
dataB.datasets[1].data.push(undefined);
|
||||
dataB.datasets[2].data.push(undefined);
|
||||
}
|
||||
|
||||
// limite buffer
|
||||
const maxPoints = 100;
|
||||
|
||||
if (dataA.labels.length > maxPoints) {
|
||||
dataA.labels.shift();
|
||||
dataA.datasets.forEach(d => d.data.shift());
|
||||
}
|
||||
|
||||
if (dataB.labels.length > maxPoints) {
|
||||
dataB.labels.shift();
|
||||
dataB.datasets.forEach(d => d.data.shift());
|
||||
}
|
||||
|
||||
chartA.update();
|
||||
chartB.update();
|
||||
}
|
||||
|
||||
function start() {
|
||||
fetch("/start");
|
||||
}
|
||||
@@ -232,61 +160,5 @@ function uploadLittleFS() {
|
||||
});
|
||||
}
|
||||
|
||||
function openTab(tabId) {
|
||||
document.querySelectorAll('.tab-content').forEach(tab => {
|
||||
tab.classList.remove('active');
|
||||
});
|
||||
|
||||
document.querySelectorAll('.tab-button').forEach(btn => {
|
||||
btn.classList.remove('active');
|
||||
});
|
||||
|
||||
document.getElementById(tabId).classList.add('active');
|
||||
|
||||
event.target.classList.add('active');
|
||||
}
|
||||
|
||||
function initCharts() {
|
||||
const ctxA = document.getElementById('chartA').getContext('2d');
|
||||
const ctxB = document.getElementById('chartB').getContext('2d');
|
||||
|
||||
chartA = new Chart(ctxA, {
|
||||
type: 'line',
|
||||
data: dataA,
|
||||
options: {
|
||||
animation: false,
|
||||
responsive: true,
|
||||
scales: {
|
||||
x: {
|
||||
display: true
|
||||
},
|
||||
y: {
|
||||
beginAtZero: true
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
chartB = new Chart(ctxB, {
|
||||
type: 'line',
|
||||
data: dataB,
|
||||
options: {
|
||||
animation: false,
|
||||
responsive: true,
|
||||
scales: {
|
||||
x: {
|
||||
display: true
|
||||
},
|
||||
y: {
|
||||
beginAtZero: true
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
window.onload = () => {
|
||||
initCharts();
|
||||
};
|
||||
|
||||
setInterval(updateLoadingState, 200);
|
||||
connectWS();
|
||||
@@ -219,41 +219,3 @@ button:hover {
|
||||
span {
|
||||
color: var(--text-dark);
|
||||
}
|
||||
|
||||
/* TABS */
|
||||
.tabs {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
margin: 20px;
|
||||
}
|
||||
|
||||
.tab-button {
|
||||
padding: 10px 20px;
|
||||
margin: 0 5px;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
background: var(--border-color);
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.tab-button.active {
|
||||
background: var(--primary-blue);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.tab-content {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.tab-content.active {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.chart-container {
|
||||
max-width: 1000px;
|
||||
margin: 20px auto;
|
||||
background: white;
|
||||
padding: 20px;
|
||||
border-radius: 6px;
|
||||
box-shadow: 0 1px 3px rgba(0,0,0,0.08);
|
||||
}
|
||||
+2
-1
@@ -1,7 +1,8 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"Jason2866.esp-decoder",
|
||||
"pioarduino.pioarduino-ide"
|
||||
"pioarduino.pioarduino-ide",
|
||||
"platformio.platformio-ide"
|
||||
],
|
||||
"unwantedRecommendations": [
|
||||
"ms-vscode.cpptools-extension-pack"
|
||||
|
||||
@@ -16,18 +16,18 @@ lib_deps =
|
||||
hideakitai/DebugLog@^0.8.4
|
||||
board_build.flash_size = 4MB
|
||||
board_build.partitions = default.csv
|
||||
monitor_speed = 921600
|
||||
monitor_speed = 115200
|
||||
build_type = release
|
||||
|
||||
[env:esp32-devtest-debug]
|
||||
board = esp32dev
|
||||
platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip
|
||||
framework = arduino
|
||||
|
||||
lib_deps =
|
||||
hideakitai/DebugLog@^0.8.4
|
||||
board_build.flash_size = 4MB
|
||||
board_build.partitions = default.csv
|
||||
monitor_speed = 921600
|
||||
monitor_speed = 115200
|
||||
build_type = debug
|
||||
build_flags =
|
||||
-O0
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
// ANSI colors
|
||||
#define COLOR_RESET "\033[0m"
|
||||
#define COLOR_RED "\033[31m"
|
||||
#define COLOR_GREEN "\033[32m"
|
||||
#define COLOR_BLUE "\033[34m"
|
||||
#define COLOR_MAGENTA "\033[35m"
|
||||
#define COLOR_CYAN "\033[36m"
|
||||
#define COLOR_YELLOW "\033[33m"
|
||||
#define COLOR_WHITE "\033[37m"
|
||||
#define COLOR_LBLUE "\033[94m"
|
||||
+33
-193
@@ -1,11 +1,7 @@
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <DebugLog.h>
|
||||
|
||||
#include "timer.h"
|
||||
#include "colors.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
static hw_timer_t *timerA = NULL;
|
||||
@@ -19,27 +15,16 @@ static uint32_t count = 0;
|
||||
#define SPARK_DLY_MIN 10
|
||||
#define SPARK_DLY_MAX 490
|
||||
|
||||
#define COIL_PULSE_MIN 100
|
||||
#define COIL_PULSE_MAX 1000
|
||||
|
||||
#define SPARK_PULSE_MIN 10
|
||||
#define SPARK_PULSE_MAX 500
|
||||
|
||||
#define PAUSE_LONG_MIN 5000
|
||||
#define PAUSE_LONG_MAX PAUSE_LONG_MIN * 100
|
||||
#define PAUSE_LONG_MAX PAUSE_LONG_MIN*100
|
||||
|
||||
#define RPM_MIN 250
|
||||
#define RPM_MAX 5500
|
||||
|
||||
void clearScreen()
|
||||
{
|
||||
Serial.print("\033[2J"); // clear screen
|
||||
Serial.print("\033[H"); // cursor home
|
||||
Serial.flush();
|
||||
void clearScreen(){
|
||||
Serial.print("\033[2J"); // clear screen
|
||||
Serial.print("\033[H"); // cursor home
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
static uint32_t set_rpm = 500;
|
||||
static uint32_t set_delay = 100;
|
||||
static double filtered = 0;
|
||||
|
||||
static const std::map<const uint32_t, const char *> pin2Name = {
|
||||
{PIN_TRIG_A12P, "HIGH_PIN_TRIG_A12P"},
|
||||
@@ -58,48 +43,19 @@ static const std::map<const uint32_t, const char *> pin2Name = {
|
||||
{State::S_WAIT_10MS, "S_WAIT_10MS"}};
|
||||
|
||||
static timerStatus stsA = {
|
||||
.clock_period_us = (uint32_t)PERIOD_US,
|
||||
.pause_long_us = 10000,
|
||||
.pause_short_us = 1000,
|
||||
.coil_pulse_us = 1000,
|
||||
.spark_pulse_us = 100,
|
||||
.spark_delay_us = 50,
|
||||
.pins = {
|
||||
.pin_trig_12p = PIN_TRIG_A12P,
|
||||
.pin_trig_12n = PIN_TRIG_A12N,
|
||||
.pin_trig_34p = PIN_TRIG_A34P,
|
||||
.pin_trig_34n = PIN_TRIG_A34N,
|
||||
.pin_spark_12 = SPARK_A12,
|
||||
.pin_spark_34 = SPARK_A34},
|
||||
.main_task = NULL};
|
||||
|
||||
static timerStatus stsB = {
|
||||
.clock_period_us = (uint32_t)PERIOD_US,
|
||||
.pause_long_us = 10000,
|
||||
.pause_short_us = 1000,
|
||||
.coil_pulse_us = 500,
|
||||
.spark_pulse_us = 100,
|
||||
.spark_delay_us = 50,
|
||||
.pins = {
|
||||
.pin_trig_12p = PIN_TRIG_B12P,
|
||||
.pin_trig_12n = PIN_TRIG_B12N,
|
||||
.pin_trig_34p = PIN_TRIG_B34P,
|
||||
.pin_trig_34n = PIN_TRIG_B34N,
|
||||
.pin_spark_12 = SPARK_B12,
|
||||
.pin_spark_34 = SPARK_B34},
|
||||
.main_task = NULL};
|
||||
|
||||
static bool isEnabled_A = false;
|
||||
static bool isEnabled_B = false;
|
||||
|
||||
static String last_command;
|
||||
.clock_period_us = (uint32_t)PERIOD_US,
|
||||
.pause_long_us = 10000,
|
||||
.pause_short_us = 1000,
|
||||
.coil_pulse_us = 1000,
|
||||
.spark_pulse_us = 100,
|
||||
.spark_delay_us = 50,
|
||||
.main_task = NULL};
|
||||
|
||||
void setup()
|
||||
{
|
||||
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
Serial.setTimeout(100);
|
||||
LOG_ATTACH_SERIAL(Serial);
|
||||
|
||||
pinMode(PIN_TRIG_A12P, OUTPUT);
|
||||
@@ -117,152 +73,36 @@ void setup()
|
||||
pinMode(SPARK_B34, OUTPUT);
|
||||
|
||||
pinMode(SPARK_DELAY_POT, ANALOG);
|
||||
pinMode(FREQ_POT, ANALOG);
|
||||
|
||||
pinMode(ENABLE_PIN_A, INPUT_PULLUP);
|
||||
pinMode(ENABLE_PIN_B, INPUT_PULLUP);
|
||||
|
||||
// get the task handle for the main loop
|
||||
stsA.main_task = xTaskGetCurrentTaskHandleForCore(1);
|
||||
stsB.main_task = xTaskGetCurrentTaskHandleForCore(1);
|
||||
|
||||
// Begin timer with preset fixed frequency
|
||||
timerA = timerBegin(FREQUENCY);
|
||||
timerB = timerBegin(FREQUENCY);
|
||||
|
||||
// Stop timers because of autostart
|
||||
timerStop(timerA);
|
||||
timerStop(timerB);
|
||||
|
||||
// Attach interrupts and call callback every timer expiry
|
||||
timerAttachInterruptArg(timerA, &onTimer, (void *)&stsA);
|
||||
timerAttachInterruptArg(timerB, &onTimer, (void *)&stsB);
|
||||
timerAlarm(timerA, 1, true, 0); // infinite number of reloads
|
||||
timerAlarm(timerB, 1, true, 0);
|
||||
timerAlarm(timerA, 1, true, 0);
|
||||
|
||||
LOG_INFO("Setup Complete");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
clearScreen();
|
||||
|
||||
Serial.printf("\t++++ Loop: %u ++++\n", count++);
|
||||
|
||||
if (isEnabled_A)
|
||||
Serial.println("==== System A is" COLOR_GREEN " ENABLED" COLOR_RESET " ====");
|
||||
else
|
||||
Serial.println("==== System A is" COLOR_RED " DISABLED" COLOR_RESET " ====");
|
||||
|
||||
if (isEnabled_B)
|
||||
Serial.println("==== System B is" COLOR_GREEN " ENABLED" COLOR_RESET " ====");
|
||||
else
|
||||
Serial.println("==== System B is" COLOR_RED " DISABLED" COLOR_RESET " ====");
|
||||
|
||||
Serial.printf("Spark Delay uS: %u\n", stsA.spark_delay_us);
|
||||
Serial.printf("Soft Start: %s\n", stsA.soft_start ? "ENABLED" : "DISABLED");
|
||||
Serial.printf("Engine Rpm: %u\n", (uint32_t)(set_rpm));
|
||||
Serial.printf("Coil Pulse: %u uS\n", stsA.coil_pulse_us);
|
||||
Serial.printf("Spark Pulse: %u uS\n", stsA.spark_pulse_us);
|
||||
Serial.println(COLOR_CYAN "-------------------------------------");
|
||||
Serial.println("E[a/b] > Enable Box a/b | D[a/b] > Disable a/b");
|
||||
Serial.println("S[ddd] > Spark Delay | R[dddd] > Engine RPM");
|
||||
Serial.println("C[ddd] > Spark Pulse | P[ddd] > Coil Pulse");
|
||||
Serial.println("-------------------------------------" COLOR_RESET);
|
||||
Serial.printf("Last Command: %s\n", last_command.c_str());
|
||||
|
||||
auto str = Serial.readStringUntil('\n');
|
||||
if (!str.isEmpty())
|
||||
{
|
||||
last_command = str;
|
||||
const auto cmd = str.charAt(0);
|
||||
char c;
|
||||
switch (cmd)
|
||||
{
|
||||
case 'E':
|
||||
{
|
||||
char box;
|
||||
sscanf(str.c_str(), "%c%c\n", &c, &box);
|
||||
if (box == 'a' && !isEnabled_A)
|
||||
{
|
||||
timerStart(timerA);
|
||||
isEnabled_A = true;
|
||||
}
|
||||
else if (box == 'b' && !isEnabled_B)
|
||||
{
|
||||
timerStart(timerB);
|
||||
isEnabled_B = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'D':
|
||||
{
|
||||
char c;
|
||||
char box;
|
||||
sscanf(str.c_str(), "%c%c\n", &c, &box);
|
||||
if (box == 'a' && isEnabled_A)
|
||||
{
|
||||
timerStop(timerA);
|
||||
isEnabled_A = false;
|
||||
}
|
||||
else if (box == 'b' && isEnabled_B)
|
||||
{
|
||||
timerStop(timerB);
|
||||
isEnabled_B = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'R':
|
||||
{
|
||||
int new_rpm;
|
||||
sscanf(str.c_str(), "%c%d\n", &c, &new_rpm);
|
||||
new_rpm = min(RPM_MAX, max(RPM_MIN, new_rpm));
|
||||
stsA.pause_long_us = (uint32_t)(60000000.0f / (float)new_rpm / 2.0f);
|
||||
stsB.pause_long_us = stsA.pause_long_us;
|
||||
set_rpm = (uint32_t)new_rpm;
|
||||
break;
|
||||
}
|
||||
case 'S':
|
||||
{
|
||||
int new_delay;
|
||||
sscanf(str.c_str(), "%c%d\n", &c, &new_delay);
|
||||
new_delay = min(SPARK_DLY_MAX, max(SPARK_DLY_MIN, new_delay));
|
||||
stsA.spark_delay_us = (uint32_t)(new_delay);
|
||||
if (stsA.spark_delay_us > (SPARK_DLY_MIN + SPARK_DLY_MAX) / 2)
|
||||
{
|
||||
stsA.soft_start = true;
|
||||
stsA.spark_delay_us -= (SPARK_DLY_MIN + SPARK_DLY_MAX) / 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
stsA.soft_start = false;
|
||||
}
|
||||
stsB.soft_start = stsA.soft_start;
|
||||
stsB.spark_delay_us = stsA.spark_delay_us;
|
||||
break;
|
||||
}
|
||||
case 'P':
|
||||
{
|
||||
int new_pulse;
|
||||
sscanf(str.c_str(), "%c%d\n", &c, &new_pulse);
|
||||
new_pulse = min(COIL_PULSE_MAX, max(COIL_PULSE_MIN, new_pulse));
|
||||
stsA.coil_pulse_us = stsB.coil_pulse_us = (uint32_t)new_pulse;
|
||||
break;
|
||||
}
|
||||
case 'C':
|
||||
{
|
||||
int new_pulse;
|
||||
sscanf(str.c_str(), "%c%d\n", &c, &new_pulse);
|
||||
new_pulse = min(SPARK_PULSE_MAX, max(SPARK_PULSE_MIN, new_pulse));
|
||||
stsA.spark_pulse_us = stsB.spark_pulse_us = (uint32_t)new_pulse;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
Serial.read();
|
||||
LOG_INFO("Loop: ", count++);
|
||||
uint32_t spark_delay = (uint32_t)(map(analogRead(SPARK_DELAY_POT), 0, 4096, SPARK_DLY_MIN, SPARK_DLY_MAX) / PERIOD_US);
|
||||
stsA.spark_delay_us = spark_delay * PERIOD_US;
|
||||
if (stsA.spark_delay_us > (SPARK_DLY_MIN + SPARK_DLY_MAX) / 2) {
|
||||
stsA.soft_start = true;
|
||||
stsA.spark_delay_us -= (SPARK_DLY_MIN + SPARK_DLY_MAX) / 2;
|
||||
} else {
|
||||
stsA.soft_start = false;
|
||||
}
|
||||
|
||||
str.clear();
|
||||
delay(1000);
|
||||
double new_val = (float)(map(analogRead(FREQ_POT), 0, 4096, PAUSE_LONG_MIN, PAUSE_LONG_MAX));
|
||||
filtered = filtered + 0.1 * (new_val - filtered);
|
||||
stsA.pause_long_us = (uint32_t)filtered;
|
||||
LOG_INFO("Spark Delay uS: ", stsA.spark_delay_us, "\tSoft Start: ", stsA.soft_start ? "TRUE" : "FALSE");
|
||||
LOG_INFO("Pause: ", (uint32_t)(stsA.pause_long_us / 1000), "ms");
|
||||
LOG_INFO("Coil Pulse: ", stsA.coil_pulse_us, "us");
|
||||
LOG_INFO("Spark Pulse: ", stsA.spark_pulse_us, "us");
|
||||
|
||||
|
||||
delay(100);
|
||||
clearScreen();
|
||||
}
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
// Enable Pin
|
||||
#define ENABLE_PIN_A 16
|
||||
#define ENABLE_PIN_B 15
|
||||
|
||||
///// Ignition Box A /////
|
||||
#define PIN_TRIG_A12P 18
|
||||
#define PIN_TRIG_A12N 19
|
||||
|
||||
@@ -7,18 +7,20 @@ void onTimer(void *arg)
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
timerStatus *params = (timerStatus *)(arg);
|
||||
TaskHandle_t task = params->main_task;
|
||||
const timerPins pins = params->pins;
|
||||
|
||||
// increment state time
|
||||
params->state_time += params->clock_period_us;
|
||||
|
||||
digitalWrite(PIN_TRIG_B12P, HIGH);
|
||||
|
||||
switch (params->state)
|
||||
{
|
||||
|
||||
case S_12P:
|
||||
if (params->state_time == params->clock_period_us && !params->coil12p_high)
|
||||
{
|
||||
digitalWrite(pins.pin_trig_12p, HIGH);
|
||||
// xTaskNotifyFromISR(task, PIN_TRIG_A12P, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A12P, HIGH);
|
||||
params->coil12p_high = true;
|
||||
wait_sent = false;
|
||||
}
|
||||
@@ -27,18 +29,21 @@ void onTimer(void *arg)
|
||||
{
|
||||
if (params->state_time == params->spark_delay_us)
|
||||
{
|
||||
digitalWrite(pins.pin_spark_12, HIGH);
|
||||
// xTaskNotifyFromISR(task, SPARK_A12, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A12, HIGH);
|
||||
}
|
||||
|
||||
if (params->state_time == (params->spark_delay_us + params->spark_pulse_us))
|
||||
{
|
||||
digitalWrite(pins.pin_spark_12, LOW);
|
||||
// xTaskNotifyFromISR(task, ~SPARK_A12, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A12, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
if (params->state_time >= params->coil_pulse_us && params->coil12p_high)
|
||||
{
|
||||
digitalWrite(pins.pin_trig_12p, LOW);
|
||||
// xTaskNotifyFromISR(task, ~PIN_TRIG_A12P, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A12P, LOW);
|
||||
params->coil12p_high = false;
|
||||
}
|
||||
|
||||
@@ -52,7 +57,8 @@ void onTimer(void *arg)
|
||||
case S_12N:
|
||||
if (params->state_time == params->clock_period_us && !params->coil12n_high)
|
||||
{
|
||||
digitalWrite(pins.pin_trig_12n, HIGH);
|
||||
// xTaskNotifyFromISR(task, PIN_TRIG_A12N, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A12N, HIGH);
|
||||
params->coil12n_high = true;
|
||||
}
|
||||
|
||||
@@ -60,18 +66,21 @@ void onTimer(void *arg)
|
||||
{
|
||||
if (params->state_time == params->spark_delay_us)
|
||||
{
|
||||
digitalWrite(pins.pin_spark_12, HIGH);
|
||||
// xTaskNotifyFromISR(task, SPARK_A12, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A12, HIGH);
|
||||
}
|
||||
|
||||
if (params->state_time == (params->spark_delay_us + params->spark_pulse_us))
|
||||
{
|
||||
digitalWrite(pins.pin_spark_12, LOW);
|
||||
// xTaskNotifyFromISR(task, ~SPARK_A12, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A12, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
if (params->state_time >= params->coil_pulse_us && params->coil12n_high)
|
||||
{
|
||||
digitalWrite(pins.pin_trig_12n, LOW);
|
||||
// xTaskNotifyFromISR(task, ~PIN_TRIG_A12N, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A12N, LOW);
|
||||
params->coil12n_high = false;
|
||||
params->state = S_WAIT_10MS;
|
||||
params->state_time = 0;
|
||||
@@ -81,6 +90,7 @@ void onTimer(void *arg)
|
||||
case S_WAIT_10MS:
|
||||
if (!wait_sent)
|
||||
{
|
||||
// xTaskNotifyFromISR(task, S_WAIT_10MS, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
wait_sent = true;
|
||||
}
|
||||
if (params->state_time >= params->pause_long_us)
|
||||
@@ -93,7 +103,8 @@ void onTimer(void *arg)
|
||||
case S_34P:
|
||||
if (params->state_time == params->clock_period_us && !params->coil34p_high)
|
||||
{
|
||||
digitalWrite(pins.pin_trig_34p, HIGH);
|
||||
// xTaskNotifyFromISR(task, PIN_TRIG_A34P, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A34P, HIGH);
|
||||
params->coil34p_high = true;;
|
||||
wait_sent = false;
|
||||
}
|
||||
@@ -102,18 +113,21 @@ void onTimer(void *arg)
|
||||
{
|
||||
if (params->state_time == params->spark_delay_us)
|
||||
{
|
||||
digitalWrite(pins.pin_spark_34, HIGH);
|
||||
// xTaskNotifyFromISR(task, SPARK_A34, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A34, HIGH);
|
||||
}
|
||||
|
||||
if (params->state_time == params->spark_delay_us + params->spark_pulse_us)
|
||||
{
|
||||
digitalWrite(pins.pin_spark_34, LOW);
|
||||
// xTaskNotifyFromISR(task, ~SPARK_A34, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A34, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
if (params->state_time >= params->coil_pulse_us && params->coil34p_high)
|
||||
{
|
||||
digitalWrite(pins.pin_trig_34p, LOW);
|
||||
// xTaskNotifyFromISR(task, ~PIN_TRIG_A34P, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A34P, LOW);
|
||||
params->coil34p_high = false;
|
||||
}
|
||||
|
||||
@@ -127,7 +141,8 @@ void onTimer(void *arg)
|
||||
case S_34N:
|
||||
if (params->state_time == params->clock_period_us && !params->coil34n_high)
|
||||
{
|
||||
digitalWrite(pins.pin_trig_34n, HIGH);
|
||||
// xTaskNotifyFromISR(task, PIN_TRIG_A34N, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A34N, HIGH);
|
||||
params->coil34n_high = true;
|
||||
}
|
||||
|
||||
@@ -135,18 +150,21 @@ void onTimer(void *arg)
|
||||
{
|
||||
if (params->state_time == params->spark_delay_us)
|
||||
{
|
||||
digitalWrite(pins.pin_spark_34, HIGH);
|
||||
// xTaskNotifyFromISR(task, SPARK_A34, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A34, HIGH);
|
||||
}
|
||||
|
||||
if (params->state_time == params->spark_delay_us + params->spark_pulse_us)
|
||||
{
|
||||
digitalWrite(pins.pin_spark_34, LOW);
|
||||
// xTaskNotifyFromISR(task, ~SPARK_A34, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(SPARK_A34, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
if (params->state_time >= params->coil_pulse_us && params->coil34n_high)
|
||||
{
|
||||
digitalWrite(pins.pin_trig_34n, LOW);
|
||||
// xTaskNotifyFromISR(task, ~PIN_TRIG_A34N, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
digitalWrite(PIN_TRIG_A34N, LOW);
|
||||
params->coil34n_high = false;
|
||||
params->state = S_WAIT_10MS_END;
|
||||
params->state_time = 0;
|
||||
@@ -156,6 +174,7 @@ void onTimer(void *arg)
|
||||
case S_WAIT_10MS_END:
|
||||
if (!wait_sent)
|
||||
{
|
||||
// xTaskNotifyFromISR(task, S_WAIT_10MS_END, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
wait_sent = true;
|
||||
}
|
||||
if (params->state_time >= params->pause_long_us)
|
||||
@@ -165,7 +184,9 @@ void onTimer(void *arg)
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
digitalWrite(PIN_TRIG_B12P, LOW);
|
||||
|
||||
if (xHigherPriorityTaskWoken)
|
||||
portYIELD_FROM_ISR();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <DebugLog.h>
|
||||
#include "pins.h"
|
||||
@@ -21,15 +19,6 @@ enum State
|
||||
S_WAIT_10MS_END
|
||||
};
|
||||
|
||||
struct timerPins {
|
||||
const uint8_t pin_trig_12p;
|
||||
const uint8_t pin_trig_12n;
|
||||
const uint8_t pin_trig_34p;
|
||||
const uint8_t pin_trig_34n;
|
||||
const uint8_t pin_spark_12;
|
||||
const uint8_t pin_spark_34;
|
||||
};
|
||||
|
||||
struct timerStatus
|
||||
{
|
||||
State state = State::S_12P;
|
||||
@@ -45,7 +34,6 @@ struct timerStatus
|
||||
bool coil34p_high = false;
|
||||
bool coil12n_high = false;
|
||||
bool coil34n_high = false;
|
||||
timerPins pins;
|
||||
TaskHandle_t main_task;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user