STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include "main.h"
2
3// ── Globals ───────────────────────────────────────────────────
4byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05};
5
7
8/**
9 * The message this build prints. Prefer STAR_OTA_TEST_MESSAGE — the
10 * library-wide flag that `firmware/tools/ota_upload.py --message` and the
11 * Test-GUI's OTA tab both set — and fall back to this project's older
12 * -DOTA_MESSAGE so the local ota_upload.py keeps working.
13 */
14static const char* currentMessage() {
15 return StarOTA::testMessage()[0] != '\0' ? StarOTA::testMessage()
17}
18unsigned long lastPrintMillis = 0;
19unsigned long bootTime = 0;
20
21// ══════════════════════════════════════════════════════════════
22// SETUP
23// ══════════════════════════════════════════════════════════════
24void setup() {
25 Serial.begin(115200);
26 delay(1000); // Give USB-CDC time to enumerate
27
28 Serial.println("============================================");
29 Serial.println(" ESP32-S3 Ethernet OTA Test Firmware");
30 Serial.println("============================================");
31 Serial.println();
32
33 // ── LED ──────────────────────────────────────────────────
34 pinMode(Pins.LED, OUTPUT);
35 digitalWrite(Pins.LED, LOW);
36
37 // ── Ethernet init (mirrors hotfire SensorHotfireCore pattern) ─
38 Serial.println("[ETH] Initializing SPI for W5500...");
39 Serial.print(" Pins -> SCLK=");
40 Serial.print(Pins.ETH_SCLK);
41 Serial.print(" MISO=");
42 Serial.print(Pins.ETH_MISO);
43 Serial.print(" MOSI=");
44 Serial.print(Pins.ETH_MOSI);
45 Serial.print(" CS=");
46 Serial.println(Pins.ETH_CS);
47
48 SPI.begin(Pins.ETH_SCLK, Pins.ETH_MISO, Pins.ETH_MOSI, Pins.ETH_CS);
50 Serial.println("[ETH] SPI.begin() done.");
51
52 Ethernet.init(Pins.ETH_CS);
54 Serial.println("[ETH] Ethernet.init() done.");
55
60
61 Ethernet.begin(mac, ip, dns, gateway, subnet);
63
64 Serial.print("[ETH] Ethernet.begin() done. IP = ");
65 Serial.println(Ethernet.localIP());
66
67 if (Ethernet.localIP() == IPAddress(0, 0, 0, 0)) {
68 Serial.println(
69 "[ETH] WARNING: IP is 0.0.0.0 — check cable / W5500 wiring!");
70 }
71
72 // ── OTA TCP server ──────────────────────────────────────
73 otaServer.begin();
74 Serial.println();
75
76 // ── Ready ────────────────────────────────────────────────
77 Serial.println("[MAIN] Setup complete. Entering main loop.");
78 Serial.print("[MAIN] Current firmware message: \"");
79 Serial.print(currentMessage());
80 Serial.println("\"");
81 Serial.println();
82
83 bootTime = millis();
84 lastPrintMillis = 0; // Force immediate first print
85}
86
87// ══════════════════════════════════════════════════════════════
88// LOOP
89// ══════════════════════════════════════════════════════════════
90void loop() {
91 // ── 1. Non-blocking OTA check ────────────────────────────
92 // Blocks only while a transfer is actually running, and never returns on
93 // success — the board reboots into the new image.
94 otaServer.poll();
95
96 // ── 2. Periodic serial message ───────────────────────────
97 unsigned long now = millis();
99 lastPrintMillis = now;
100
101 unsigned long uptime = (now - bootTime) / 1000;
102 unsigned long mins = uptime / 60;
103 unsigned long secs = uptime % 60;
104
105 Serial.print("[MSG] ");
106 Serial.print(currentMessage());
107 Serial.print(" | uptime ");
108 Serial.print(mins);
109 Serial.print("m ");
110 Serial.print(secs);
111 Serial.print("s | IP ");
112 Serial.println(Ethernet.localIP());
113
114 // Blink LED briefly to show life
115 digitalWrite(Pins.LED, HIGH);
116 delay(50);
117 digitalWrite(Pins.LED, LOW);
118 }
119
120 delay(10); // Small yield
121}
static SerialStub Serial
Definition Arduino.h:128
#define HIGH
Definition Arduino.h:19
void pinMode(int, int)
Definition Arduino.h:46
#define OUTPUT
Definition Arduino.h:22
unsigned long millis()
Definition Arduino.h:29
#define LOW
Definition Arduino.h:20
void digitalWrite(int, int)
Definition Arduino.h:48
#define ETHERNET_SPI_DELAY
Definition main.h:33
#define OTA_GATEWAY
Definition main.h:18
#define ETHERNET_BEGIN_DELAY
Definition main.h:35
#define ETHERNET_INIT_DELAY
Definition main.h:34
#define PRINT_INTERVAL_MS
Definition main.h:32
#define OTA_TCP_PORT
Definition main.h:21
#define OTA_STATIC_IP
Definition main.h:17
#define OTA_SUBNET
Definition main.h:19
#define OTA_MESSAGE
Definition main.h:28
#define OTA_DNS
Definition main.h:20
static EthernetClass Ethernet
Definition Ethernet.h:116
void delay(int ms)
The OTA listener.
void setup()
Definition main.cpp:142
constexpr const Layout & Pins
void loop()
Definition main.cpp:201
IPAddress dns(192, 168, 2, 1)
byte mac[6]
Definition main.cpp:31
IPAddress subnet(255, 255, 255, 0)
IPAddress gateway(0, 0, 0, 0)
static const char * currentMessage()
The message this build prints.
Definition main.cpp:14
StarOTA::Server otaServer(OTA_TCP_PORT)
unsigned long bootTime
Definition main.cpp:19
unsigned long lastPrintMillis
Definition main.cpp:18
const char * testMessage()
The compiled-in test message ("" when none).