From 12aef01d37064af5c586de4e42548ca7502d2e23 Mon Sep 17 00:00:00 2001 From: luckydizzier <134082331+luckydizzier@users.noreply.github.com> Date: Thu, 19 Jun 2025 05:41:51 +0200 Subject: [PATCH] feat(firmware): centralize pin config and ota constraints --- docs/configuration/pins.md | 16 ++++---- docs/hardware/esp8266.md | 2 +- docs/hardware/pinmap.md | 20 +++++----- firmware/config/pins_config.h | 27 ++++++++++++++ firmware/due/main.ino | 12 ++++++ firmware/shared/config/config.h | 3 -- firmware/shared/config/pins.h | 65 ++++++++++++++++++++++++++------- 7 files changed, 110 insertions(+), 35 deletions(-) create mode 100644 firmware/config/pins_config.h diff --git a/docs/configuration/pins.md b/docs/configuration/pins.md index ad6f0d0..fe6cfbb 100644 --- a/docs/configuration/pins.md +++ b/docs/configuration/pins.md @@ -1,20 +1,20 @@ # Pin Assignments -This table lists all recommended connections for the DDS-Controller hardware. The ESP8266-01 module works at **3.3V** logic levels only. All peripherals except the LCD shield must be connected on Arduino pins **20** and above. +This table lists all recommended connections for the DDS-Controller hardware. The ESP8266-01 module works at **3.3V** logic levels only. All peripherals except the LCD shield are placed on pins **20** and above. Change the values in `firmware/config/pins_config.h` if you use a different layout. | Arduino Pin | Peripheral | Function | |-------------|-------------|-------------------| | 8,9,4,5,6,7 | LCD Shield | RS,E,D4,D5,D6,D7 | | A0 | LCD Shield | Buttons ADC | -| 10 | AD9850 | WCLK | -| 11 | AD9850 | FQUD | -| 12 | AD9850 | DATA | -| 13 | AD9850 | RESET | | 20 | ESP8266-01 | RX (from Due TX) | | 21 | ESP8266-01 | TX (to Due RX) | -| 24 | ESP8266-01 | GPIO2 status LED | -| 25 | DDS Output | Control line | -| 26 | ESP8266-01 | GPIO0 (OTA) | +| 22 | AD9850 | WCLK | +| 23 | AD9850 | FQUD | +| 24 | AD9850 | DATA | +| 25 | AD9850 | RESET | +| 26 | ESP8266-01 | GPIO2 status LED | +| 27 | DDS Output | Control line | +| 28 | ESP8266-01 | GPIO0 (OTA) | ``` D20..D53 -> custom peripherals diff --git a/docs/hardware/esp8266.md b/docs/hardware/esp8266.md index 3a6b9cf..0b22bdb 100644 --- a/docs/hardware/esp8266.md +++ b/docs/hardware/esp8266.md @@ -15,4 +15,4 @@ The ESP8266-01 module is integrated as a WiFi bridge for the DDS-Controller. It - **GPIO0_LED** (GPIO0) controls a WiFi status LED whenever OTA updates are enabled. Otherwise this pin remains un-driven so the Arduino Due can repurpose it. - UART pins start from Due pin **20** upward to avoid conflicts with the LCD shield. -Use level shifting if any connection might expose the ESP8266-01 to 5V. See the in-code comments in `firmware/esp/main.cpp` for further notes. +Use level shifting if any connection might expose the ESP8266-01 to 5V. Pin numbers can be remapped in `firmware/config/pins_config.h`. See the in-code comments in `firmware/esp/main.cpp` for further notes. diff --git a/docs/hardware/pinmap.md b/docs/hardware/pinmap.md index f15e937..5b8e3d1 100644 --- a/docs/hardware/pinmap.md +++ b/docs/hardware/pinmap.md @@ -1,20 +1,20 @@ # Hardware Pin Mapping – DDS-Controller -> _This document acts as the single source of truth for wiring. See `firmware/shared/config/pins.h` for the authoritative definitions._ +> _This document acts as the single source of truth for wiring. Pin numbers are configured in `firmware/config/pins_config.h` and re-exported via `firmware/shared/config/pins.h`._ | Arduino Due Pin | Peripheral | Peripheral Pin | Notes | |-----------------|-------------------|----------------|------------------------------------| | 8, 9, 4, 5, 6, 7| LCD Keypad Shield | RS,E,D4,D5,D6,D7 | Uses standard Arduino shield layout | | A0 | LCD Keypad Shield | Buttons ADC | Reads multiple buttons via resistor ladder | -| 10 | AD9850 DDS | WCLK | Serial clock to DDS module | -| 11 | AD9850 DDS | FQUD | Frequency update | -| 12 | AD9850 DDS | DATA | Serial data | -| 13 | AD9850 DDS | RESET | Module reset | -| 24 | ESP8266-01 | GPIO2 | WiFi status LED | -| 26 | ESP8266-01 | GPIO0 | OTA LED or Arduino control | -| 20 | ESP8266-01 | RX | Connected to Due TX (SoftwareSerial) | -| 21 | ESP8266-01 | TX | Connected to Due RX (SoftwareSerial) | -| 25 | DDS Output Enable | Control line | Toggles DDS output | +| 20 | ESP8266-01 | RX | SoftwareSerial RX from Due | +| 21 | ESP8266-01 | TX | SoftwareSerial TX to Due | +| 22 | AD9850 DDS | WCLK | Serial clock to DDS module | +| 23 | AD9850 DDS | FQUD | Frequency update | +| 24 | AD9850 DDS | DATA | Serial data | +| 25 | AD9850 DDS | RESET | Module reset | +| 26 | ESP8266-01 | GPIO2 | WiFi status LED | +| 27 | DDS Output Enable | Control line | Toggles DDS output | +| 28 | ESP8266-01 | GPIO0 | OTA trigger (keep high unless flashing) | **Safety Notice:** The ESP8266-01 module operates at **3.3V** logic levels only. Connecting it directly to 5V will permanently damage the module. diff --git a/firmware/config/pins_config.h b/firmware/config/pins_config.h new file mode 100644 index 0000000..cbd40ed --- /dev/null +++ b/firmware/config/pins_config.h @@ -0,0 +1,27 @@ +#ifndef FIRMWARE_PINS_CONFIG_H +#define FIRMWARE_PINS_CONFIG_H + +// Default pin mapping for DDS-Controller. +// Modify these values to match your wiring. +// All custom peripherals start from pin 20 and above. + +// ESP8266-01 SoftwareSerial +#define PIN_ESP_RX 20 +#define PIN_ESP_TX 21 + +// DDS module (AD9850) +#define PIN_DDS_WCLK 22 +#define PIN_DDS_FQUD 23 +#define PIN_DDS_DATA 24 +#define PIN_DDS_RESET 25 + +// Status LED on ESP GPIO2 +#define PIN_ESP_LED 26 + +// Output enable control for DDS +#define PIN_OUTPUT_CONTROL 27 + +// ESP8266 GPIO0 (reserved for OTA) +#define PIN_ESP_GPIO0 28 + +#endif // FIRMWARE_PINS_CONFIG_H diff --git a/firmware/due/main.ino b/firmware/due/main.ino index 81ca915..eb7fd8e 100644 --- a/firmware/due/main.ino +++ b/firmware/due/main.ino @@ -33,6 +33,7 @@ void setup() { esp_begin(espSerial, parser); pinMode(PIN_ESP_LED, OUTPUT); #if !USE_ESP_OTA + // Keep ESP8266 in normal boot mode when OTA is disabled pinMode(PIN_ESP_GPIO0, OUTPUT); digitalWrite(PIN_ESP_GPIO0, HIGH); #endif @@ -49,6 +50,17 @@ void setup() { void loop() { menu.update(); +#ifdef USE_ESP + static unsigned long lastLedToggle = 0; + if (millis() - lastLedToggle > 1000) { + digitalWrite(PIN_ESP_LED, !digitalRead(PIN_ESP_LED)); + lastLedToggle = millis(); + } +#if !USE_ESP_OTA + // Ensure the ESP remains in run mode when OTA is disabled + digitalWrite(PIN_ESP_GPIO0, HIGH); +#endif +#endif if (Serial.available()) { String cmd = Serial.readStringUntil('\n'); diff --git a/firmware/shared/config/config.h b/firmware/shared/config/config.h index 5e4df6d..1896fcd 100644 --- a/firmware/shared/config/config.h +++ b/firmware/shared/config/config.h @@ -15,7 +15,4 @@ #define ESP_BAUD_RATE 9600 -// Optional pin for controlling ESP8266 GPIO0 when OTA is disabled -#define PIN_ESP_GPIO0 26 - #endif // CONFIG_H diff --git a/firmware/shared/config/pins.h b/firmware/shared/config/pins.h index 9318fc5..de5fc8a 100644 --- a/firmware/shared/config/pins.h +++ b/firmware/shared/config/pins.h @@ -2,21 +2,60 @@ #define CONFIG_PINS_H // Centralized pin definitions for DDS-Controller hardware -// Adjust these values to match the wiring as documented in docs/hardware/pinmap.md +// All numeric values are provided by firmware/config/pins_config.h -// DDS (AD9850) -#define PIN_DDS_WCLK 10 -#define PIN_DDS_FQUD 11 -#define PIN_DDS_DATA 12 -#define PIN_DDS_RESET 13 +#include "../../config/pins_config.h" -// ESP8266-01 communication via SoftwareSerial -#define PIN_ESP_RX 20 // Due TX to ESP8266 RX -#define PIN_ESP_TX 21 // Due RX to ESP8266 TX -#define PIN_ESP_LED 24 // GPIO2 on ESP8266 - -// Control output -#define PIN_OUTPUT_CONTROL 25 +// Re-export for firmware modules #define OUTPUT_CONTROL_PIN PIN_OUTPUT_CONTROL +// Validate that no two pins share the same number +#define VALIDATE_PIN_UNIQUE(a,b) static_assert((a) != (b), "Pin conflict: " #a " and " #b) + +VALIDATE_PIN_UNIQUE(PIN_ESP_RX, PIN_ESP_TX); +VALIDATE_PIN_UNIQUE(PIN_ESP_RX, PIN_DDS_WCLK); +VALIDATE_PIN_UNIQUE(PIN_ESP_RX, PIN_DDS_FQUD); +VALIDATE_PIN_UNIQUE(PIN_ESP_RX, PIN_DDS_DATA); +VALIDATE_PIN_UNIQUE(PIN_ESP_RX, PIN_DDS_RESET); +VALIDATE_PIN_UNIQUE(PIN_ESP_RX, PIN_ESP_LED); +VALIDATE_PIN_UNIQUE(PIN_ESP_RX, PIN_OUTPUT_CONTROL); +VALIDATE_PIN_UNIQUE(PIN_ESP_RX, PIN_ESP_GPIO0); + +VALIDATE_PIN_UNIQUE(PIN_ESP_TX, PIN_DDS_WCLK); +VALIDATE_PIN_UNIQUE(PIN_ESP_TX, PIN_DDS_FQUD); +VALIDATE_PIN_UNIQUE(PIN_ESP_TX, PIN_DDS_DATA); +VALIDATE_PIN_UNIQUE(PIN_ESP_TX, PIN_DDS_RESET); +VALIDATE_PIN_UNIQUE(PIN_ESP_TX, PIN_ESP_LED); +VALIDATE_PIN_UNIQUE(PIN_ESP_TX, PIN_OUTPUT_CONTROL); +VALIDATE_PIN_UNIQUE(PIN_ESP_TX, PIN_ESP_GPIO0); + +VALIDATE_PIN_UNIQUE(PIN_DDS_WCLK, PIN_DDS_FQUD); +VALIDATE_PIN_UNIQUE(PIN_DDS_WCLK, PIN_DDS_DATA); +VALIDATE_PIN_UNIQUE(PIN_DDS_WCLK, PIN_DDS_RESET); +VALIDATE_PIN_UNIQUE(PIN_DDS_WCLK, PIN_ESP_LED); +VALIDATE_PIN_UNIQUE(PIN_DDS_WCLK, PIN_OUTPUT_CONTROL); +VALIDATE_PIN_UNIQUE(PIN_DDS_WCLK, PIN_ESP_GPIO0); + +VALIDATE_PIN_UNIQUE(PIN_DDS_FQUD, PIN_DDS_DATA); +VALIDATE_PIN_UNIQUE(PIN_DDS_FQUD, PIN_DDS_RESET); +VALIDATE_PIN_UNIQUE(PIN_DDS_FQUD, PIN_ESP_LED); +VALIDATE_PIN_UNIQUE(PIN_DDS_FQUD, PIN_OUTPUT_CONTROL); +VALIDATE_PIN_UNIQUE(PIN_DDS_FQUD, PIN_ESP_GPIO0); + +VALIDATE_PIN_UNIQUE(PIN_DDS_DATA, PIN_DDS_RESET); +VALIDATE_PIN_UNIQUE(PIN_DDS_DATA, PIN_ESP_LED); +VALIDATE_PIN_UNIQUE(PIN_DDS_DATA, PIN_OUTPUT_CONTROL); +VALIDATE_PIN_UNIQUE(PIN_DDS_DATA, PIN_ESP_GPIO0); + +VALIDATE_PIN_UNIQUE(PIN_DDS_RESET, PIN_ESP_LED); +VALIDATE_PIN_UNIQUE(PIN_DDS_RESET, PIN_OUTPUT_CONTROL); +VALIDATE_PIN_UNIQUE(PIN_DDS_RESET, PIN_ESP_GPIO0); + +VALIDATE_PIN_UNIQUE(PIN_ESP_LED, PIN_OUTPUT_CONTROL); +VALIDATE_PIN_UNIQUE(PIN_ESP_LED, PIN_ESP_GPIO0); + +VALIDATE_PIN_UNIQUE(PIN_OUTPUT_CONTROL, PIN_ESP_GPIO0); + +#undef VALIDATE_PIN_UNIQUE + #endif // CONFIG_PINS_H