Compare commits
3 Commits
e5baa7cec5
...
e8f7e2f75c
| Author | SHA1 | Date |
|---|---|---|
|
|
e8f7e2f75c | |
|
|
1b78440309 | |
|
|
88a585408a |
|
|
@ -1,3 +1,3 @@
|
||||||
idf_component_register(SRCS "app_console.c"
|
idf_component_register(SRCS "app_console.c"
|
||||||
INCLUDE_DIRS "."
|
INCLUDE_DIRS "."
|
||||||
PRIV_REQUIRES console wifi_cfg wifi_controller iperf nvs_flash)
|
PRIV_REQUIRES console wifi_cfg wifi_controller iperf nvs_flash gps_sync)
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
#include "argtable3/argtable3.h"
|
#include "argtable3/argtable3.h"
|
||||||
#include "wifi_cfg.h"
|
#include "wifi_cfg.h"
|
||||||
#include "iperf.h"
|
#include "iperf.h"
|
||||||
|
#include "gps_sync.h"
|
||||||
#include "wifi_controller.h"
|
#include "wifi_controller.h"
|
||||||
#include "esp_wifi.h"
|
#include "esp_wifi.h"
|
||||||
#include "nvs.h"
|
#include "nvs.h"
|
||||||
|
|
@ -11,6 +12,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
// --- Helper: Prompt Update ---
|
// --- Helper: Prompt Update ---
|
||||||
// Updates the "esp32>" vs "esp32*>" prompt based on dirty state
|
// Updates the "esp32>" vs "esp32*>" prompt based on dirty state
|
||||||
|
|
@ -126,6 +128,116 @@ static void register_nvs_cmd(void) {
|
||||||
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
|
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// COMMAND: gps (Configure & Status)
|
||||||
|
// ============================================================================
|
||||||
|
static struct {
|
||||||
|
struct arg_lit *enable;
|
||||||
|
struct arg_lit *disable;
|
||||||
|
struct arg_lit *status;
|
||||||
|
struct arg_lit *help;
|
||||||
|
struct arg_end *end;
|
||||||
|
} gps_args;
|
||||||
|
|
||||||
|
static int cmd_gps(int argc, char **argv) {
|
||||||
|
int nerrors = arg_parse(argc, argv, (void **)&gps_args);
|
||||||
|
if (nerrors > 0) {
|
||||||
|
arg_print_errors(stderr, gps_args.end, argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gps_args.help->count > 0) {
|
||||||
|
printf("Usage: gps [--enable|--disable|--status]\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
nvs_handle_t h;
|
||||||
|
esp_err_t err = nvs_open("storage", NVS_READWRITE, &h);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
printf("Error opening NVS: %s\n", esp_err_to_name(err));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- HANDLE SETTERS ---
|
||||||
|
bool changed = false;
|
||||||
|
if (gps_args.enable->count > 0) {
|
||||||
|
nvs_set_u8(h, "gps_enabled", 1);
|
||||||
|
printf("GPS set to ENABLED. (Reboot required)\n");
|
||||||
|
changed = true;
|
||||||
|
} else if (gps_args.disable->count > 0) {
|
||||||
|
nvs_set_u8(h, "gps_enabled", 0);
|
||||||
|
printf("GPS set to DISABLED. (Reboot required)\n");
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changed) nvs_commit(h);
|
||||||
|
|
||||||
|
// --- DISPLAY STATUS ---
|
||||||
|
// 1. NVS State
|
||||||
|
uint8_t val = 1;
|
||||||
|
if (nvs_get_u8(h, "gps_enabled", &val) != ESP_OK) val = 1;
|
||||||
|
printf("GPS NVS State: %s\n", val ? "ENABLED" : "DISABLED");
|
||||||
|
nvs_close(h);
|
||||||
|
|
||||||
|
// 2. Runtime Status
|
||||||
|
if (val) {
|
||||||
|
gps_timestamp_t ts = gps_get_timestamp();
|
||||||
|
int64_t pps_age = gps_get_pps_age_ms();
|
||||||
|
|
||||||
|
printf("Status: %s\n", ts.synced ? "SYNCED" : "SEARCHING");
|
||||||
|
|
||||||
|
// Print Raw NMEA ---
|
||||||
|
char nmea_buf[128];
|
||||||
|
gps_get_last_nmea(nmea_buf, sizeof(nmea_buf));
|
||||||
|
|
||||||
|
// Remove trailing \r\n for cleaner printing
|
||||||
|
size_t len = strlen(nmea_buf);
|
||||||
|
if (len > 0 && (nmea_buf[len-1] == '\r' || nmea_buf[len-1] == '\n')) nmea_buf[len-1] = 0;
|
||||||
|
if (len > 1 && (nmea_buf[len-2] == '\r' || nmea_buf[len-2] == '\n')) nmea_buf[len-2] = 0;
|
||||||
|
|
||||||
|
printf("Last NMEA: [%s]\n", nmea_buf);
|
||||||
|
// ---------------------------
|
||||||
|
|
||||||
|
if (pps_age < 0) {
|
||||||
|
printf("PPS Signal: NEVER DETECTED\n");
|
||||||
|
} else if (pps_age > 1100) {
|
||||||
|
printf("PPS Signal: LOST (Last seen %" PRId64 " ms ago)\n", pps_age);
|
||||||
|
} else {
|
||||||
|
printf("PPS Signal: ACTIVE (Age: %" PRId64 " ms)\n", pps_age);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ts.gps_us > 0) {
|
||||||
|
time_t now_sec = ts.gps_us / 1000000;
|
||||||
|
struct tm tm_info;
|
||||||
|
gmtime_r(&now_sec, &tm_info);
|
||||||
|
char time_buf[64];
|
||||||
|
strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S UTC", &tm_info);
|
||||||
|
printf("Current Time: %s\n", time_buf);
|
||||||
|
} else {
|
||||||
|
printf("Current Time: <Unknown> (System Epoch)\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
end_cmd();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void register_gps_cmd(void) {
|
||||||
|
gps_args.enable = arg_lit0(NULL, "enable", "Enable GPS");
|
||||||
|
gps_args.disable = arg_lit0(NULL, "disable", "Disable GPS");
|
||||||
|
gps_args.status = arg_lit0(NULL, "status", "Show Status");
|
||||||
|
gps_args.help = arg_lit0("h", "help", "Help");
|
||||||
|
gps_args.end = arg_end(2);
|
||||||
|
|
||||||
|
const esp_console_cmd_t cmd = {
|
||||||
|
.command = "gps",
|
||||||
|
.help = "Configure GPS",
|
||||||
|
.func = &cmd_gps,
|
||||||
|
.argtable = &gps_args
|
||||||
|
};
|
||||||
|
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
|
||||||
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// COMMAND: iperf
|
// COMMAND: iperf
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
@ -447,4 +559,5 @@ void app_console_register_commands(void) {
|
||||||
register_scan_cmd();
|
register_scan_cmd();
|
||||||
register_wifi_cmd();
|
register_wifi_cmd();
|
||||||
register_nvs_cmd();
|
register_nvs_cmd();
|
||||||
|
register_gps_cmd();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,54 +25,48 @@ static bool use_gps_for_logs = false;
|
||||||
static SemaphoreHandle_t sync_mutex;
|
static SemaphoreHandle_t sync_mutex;
|
||||||
static volatile bool force_sync_update = true;
|
static volatile bool force_sync_update = true;
|
||||||
|
|
||||||
// PPS interrupt
|
// Debug Buffer
|
||||||
// Stores the monotonic time of the rising edge of the PPS signal
|
static char s_last_nmea_line[128] = "<WAITING FOR DATA>";
|
||||||
|
|
||||||
|
// PPS interrupt: Captures the exact monotonic time of the rising edge
|
||||||
static void IRAM_ATTR pps_isr_handler(void* arg) {
|
static void IRAM_ATTR pps_isr_handler(void* arg) {
|
||||||
// Capture time immediately
|
|
||||||
int64_t now = esp_timer_get_time();
|
int64_t now = esp_timer_get_time();
|
||||||
last_pps_monotonic = now;
|
last_pps_monotonic = now;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse GPS time from NMEA (GPRMC or GNRMC)
|
// Parse GPS time from NMEA (GPRMC or GNRMC)
|
||||||
static bool parse_gprmc(const char* nmea, struct tm* tm_out, bool* valid) {
|
static bool parse_gprmc(const char* nmea, struct tm* tm_out, bool* valid) {
|
||||||
// Check Header
|
|
||||||
if (strncmp(nmea, "$GPRMC", 6) != 0 && strncmp(nmea, "$GNRMC", 6) != 0) return false;
|
if (strncmp(nmea, "$GPRMC", 6) != 0 && strncmp(nmea, "$GNRMC", 6) != 0) return false;
|
||||||
|
|
||||||
// Find Time Field
|
|
||||||
char *p = strchr(nmea, ',');
|
char *p = strchr(nmea, ',');
|
||||||
if (!p) return false;
|
if (!p) return false;
|
||||||
p++; // Move past comma
|
p++; // Move past comma
|
||||||
|
|
||||||
int hour, min, sec;
|
int hour, min, sec;
|
||||||
// Scan %2d%2d%2d effectively, using floats can be safer for sub-seconds but int is fine for PPS
|
|
||||||
if (sscanf(p, "%2d%2d%2d", &hour, &min, &sec) != 3) return false;
|
if (sscanf(p, "%2d%2d%2d", &hour, &min, &sec) != 3) return false;
|
||||||
|
|
||||||
// Find Status Field (A=Active/Valid, V=Void)
|
|
||||||
p = strchr(p, ',');
|
p = strchr(p, ',');
|
||||||
if (!p) return false;
|
if (!p) return false;
|
||||||
p++;
|
p++;
|
||||||
*valid = (*p == 'A');
|
*valid = (*p == 'A');
|
||||||
|
|
||||||
// Skip Latitude, N/S, Longitude, E/W, Speed, Course (6 fields)
|
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
p = strchr(p, ',');
|
p = strchr(p, ',');
|
||||||
if (!p) return false;
|
if (!p) return false;
|
||||||
p++;
|
p++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Date Field
|
|
||||||
int day, month, year;
|
int day, month, year;
|
||||||
if (sscanf(p, "%2d%2d%2d", &day, &month, &year) != 3) return false;
|
if (sscanf(p, "%2d%2d%2d", &day, &month, &year) != 3) return false;
|
||||||
|
|
||||||
// Adjust Year (NMEA provides 2 digits)
|
|
||||||
year += (year < 80) ? 2000 : 1900;
|
year += (year < 80) ? 2000 : 1900;
|
||||||
|
|
||||||
tm_out->tm_sec = sec;
|
tm_out->tm_sec = sec;
|
||||||
tm_out->tm_min = min;
|
tm_out->tm_min = min;
|
||||||
tm_out->tm_hour = hour;
|
tm_out->tm_hour = hour;
|
||||||
tm_out->tm_mday = day;
|
tm_out->tm_mday = day;
|
||||||
tm_out->tm_mon = month - 1; // tm_mon is 0-11
|
tm_out->tm_mon = month - 1;
|
||||||
tm_out->tm_year = year - 1900; // tm_year is years since 1900
|
tm_out->tm_year = year - 1900;
|
||||||
tm_out->tm_isdst = 0;
|
tm_out->tm_isdst = 0;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -83,8 +77,6 @@ void gps_force_next_update(void) {
|
||||||
ESP_LOGW(TAG, "Requesting forced GPS sync update");
|
ESP_LOGW(TAG, "Requesting forced GPS sync update");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper to convert struct tm to time_t (UTC assumption)
|
|
||||||
// Uses standard mktime but we assume TZ is handled or default is UTC
|
|
||||||
static time_t timegm_impl(struct tm *tm) {
|
static time_t timegm_impl(struct tm *tm) {
|
||||||
time_t t = mktime(tm);
|
time_t t = mktime(tm);
|
||||||
return t;
|
return t;
|
||||||
|
|
@ -96,65 +88,50 @@ static void gps_task(void* arg) {
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
static int log_counter = 0;
|
static int log_counter = 0;
|
||||||
|
|
||||||
// Ensure timezone is UTC for correct time math
|
|
||||||
setenv("TZ", "UTC", 1);
|
setenv("TZ", "UTC", 1);
|
||||||
tzset();
|
tzset();
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
// Read from UART with a reasonable timeout
|
|
||||||
int len = uart_read_bytes(gps_uart_num, d_buf, sizeof(d_buf), pdMS_TO_TICKS(100));
|
int len = uart_read_bytes(gps_uart_num, d_buf, sizeof(d_buf), pdMS_TO_TICKS(100));
|
||||||
|
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
uint8_t data = d_buf[i];
|
uint8_t data = d_buf[i];
|
||||||
|
|
||||||
// Buffer the line
|
|
||||||
if (data == '\n' || data == '\r') {
|
if (data == '\n' || data == '\r') {
|
||||||
if (pos > 0) {
|
if (pos > 0) {
|
||||||
line[pos] = '\0';
|
line[pos] = '\0';
|
||||||
|
|
||||||
|
// Copy to debug buffer
|
||||||
|
xSemaphoreTake(sync_mutex, portMAX_DELAY);
|
||||||
|
strncpy(s_last_nmea_line, line, sizeof(s_last_nmea_line) - 1);
|
||||||
|
s_last_nmea_line[sizeof(s_last_nmea_line) - 1] = '\0';
|
||||||
|
xSemaphoreGive(sync_mutex);
|
||||||
|
|
||||||
struct tm gps_tm;
|
struct tm gps_tm;
|
||||||
bool valid_fix;
|
bool valid_fix;
|
||||||
|
|
||||||
// Try to parse GPRMC
|
|
||||||
if (parse_gprmc(line, &gps_tm, &valid_fix)) {
|
if (parse_gprmc(line, &gps_tm, &valid_fix)) {
|
||||||
if (valid_fix) {
|
if (valid_fix) {
|
||||||
// 1. Convert Parsed Time to Seconds
|
|
||||||
time_t gps_time_sec = timegm_impl(&gps_tm);
|
time_t gps_time_sec = timegm_impl(&gps_tm);
|
||||||
|
int64_t last_pps_snap = last_pps_monotonic;
|
||||||
// 2. Critical Section: Read PPS Timestamp
|
|
||||||
xSemaphoreTake(sync_mutex, portMAX_DELAY);
|
|
||||||
int64_t last_pps = last_pps_monotonic;
|
|
||||||
xSemaphoreGive(sync_mutex);
|
|
||||||
|
|
||||||
// 3. Analyze Timing
|
|
||||||
int64_t now = esp_timer_get_time();
|
int64_t now = esp_timer_get_time();
|
||||||
int64_t age_us = now - last_pps;
|
int64_t age_us = now - last_pps_snap;
|
||||||
|
|
||||||
// The PPS pulse described by this message should have happened
|
if (last_pps_snap > 0 && age_us < 900000) {
|
||||||
// fairly recently (e.g., within the last 800ms).
|
|
||||||
// If age > 900ms, we likely missed the pulse or UART is lagging badly.
|
|
||||||
if (last_pps > 0 && age_us < 900000) {
|
|
||||||
|
|
||||||
// Calculate Offset
|
|
||||||
// GPS Time (in microseconds) = Seconds * 1M
|
|
||||||
int64_t gps_time_us = (int64_t)gps_time_sec * 1000000LL;
|
int64_t gps_time_us = (int64_t)gps_time_sec * 1000000LL;
|
||||||
|
int64_t new_offset = gps_time_us - last_pps_snap;
|
||||||
// Offset = GPS_Timestamp - Monotonic_Timestamp
|
|
||||||
// This means: GPS = Monotonic + Offset
|
|
||||||
int64_t new_offset = gps_time_us - last_pps;
|
|
||||||
|
|
||||||
xSemaphoreTake(sync_mutex, portMAX_DELAY);
|
xSemaphoreTake(sync_mutex, portMAX_DELAY);
|
||||||
if (monotonic_offset_us == 0 || force_sync_update) {
|
if (monotonic_offset_us == 0 || force_sync_update) {
|
||||||
// Hard Snap (First fix or Forced)
|
|
||||||
monotonic_offset_us = new_offset;
|
monotonic_offset_us = new_offset;
|
||||||
if (force_sync_update) {
|
if (force_sync_update) {
|
||||||
ESP_LOGW(TAG, "GPS SNAP: Offset forced to %" PRIi64 " us", monotonic_offset_us);
|
ESP_LOGW(TAG, "GPS SNAP: Offset forced to %" PRIi64 " us", monotonic_offset_us);
|
||||||
force_sync_update = false;
|
force_sync_update = false;
|
||||||
log_counter = 0; // Force immediate log
|
log_counter = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Exponential Smoothing (Filter Jitter)
|
|
||||||
// 90% old, 10% new
|
|
||||||
monotonic_offset_us = (monotonic_offset_us * 9 + new_offset) / 10;
|
monotonic_offset_us = (monotonic_offset_us * 9 + new_offset) / 10;
|
||||||
}
|
}
|
||||||
gps_has_fix = true;
|
gps_has_fix = true;
|
||||||
|
|
@ -162,15 +139,16 @@ static void gps_task(void* arg) {
|
||||||
|
|
||||||
// Periodic Logging
|
// Periodic Logging
|
||||||
if (log_counter <= 0) {
|
if (log_counter <= 0) {
|
||||||
ESP_LOGI(TAG, "GPS Sync: %02d:%02d:%02d | Offset: %" PRIi64 " us | PPS Age: %" PRIi64 " ms",
|
// CHANGED FROM ESP_LOGI TO ESP_LOGD (Hidden by default)
|
||||||
|
ESP_LOGD(TAG, "GPS Sync: %02d:%02d:%02d | Offset: %" PRIi64 " us | PPS Age: %" PRIi64 " ms",
|
||||||
gps_tm.tm_hour, gps_tm.tm_min, gps_tm.tm_sec,
|
gps_tm.tm_hour, gps_tm.tm_min, gps_tm.tm_sec,
|
||||||
monotonic_offset_us, age_us / 1000);
|
monotonic_offset_us, age_us / 1000);
|
||||||
log_counter = 10; // Log every 10 valid fixes
|
log_counter = 10;
|
||||||
}
|
}
|
||||||
log_counter--;
|
log_counter--;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// PPS signal lost or not correlated
|
// Keep Warnings visible
|
||||||
if (log_counter <= 0) {
|
if (log_counter <= 0) {
|
||||||
ESP_LOGW(TAG, "GPS valid but PPS missing/old (Age: %" PRIi64 " ms)", age_us / 1000);
|
ESP_LOGW(TAG, "GPS valid but PPS missing/old (Age: %" PRIi64 " ms)", age_us / 1000);
|
||||||
log_counter = 10;
|
log_counter = 10;
|
||||||
|
|
@ -193,11 +171,17 @@ static void gps_task(void* arg) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void gps_get_last_nmea(char *buf, size_t max_len) {
|
||||||
|
if (!buf || max_len == 0) return;
|
||||||
|
xSemaphoreTake(sync_mutex, portMAX_DELAY);
|
||||||
|
strncpy(buf, s_last_nmea_line, max_len);
|
||||||
|
buf[max_len - 1] = '\0';
|
||||||
|
xSemaphoreGive(sync_mutex);
|
||||||
|
}
|
||||||
|
|
||||||
void gps_sync_init(const gps_sync_config_t *config, bool use_gps_log_timestamps) {
|
void gps_sync_init(const gps_sync_config_t *config, bool use_gps_log_timestamps) {
|
||||||
ESP_LOGI(TAG, "Initializing GPS Sync (UART %d, PPS GPIO %d)", config->uart_port, config->pps_pin);
|
ESP_LOGI(TAG, "Initializing GPS Sync (UART %d, PPS GPIO %d)", config->uart_port, config->pps_pin);
|
||||||
|
|
||||||
// 1. Initial PPS Pin Check (Input Mode)
|
|
||||||
// We poll briefly just to see if the pin is physically toggling before committing resources.
|
|
||||||
gpio_config_t pps_poll_conf = {
|
gpio_config_t pps_poll_conf = {
|
||||||
.pin_bit_mask = (1ULL << config->pps_pin),
|
.pin_bit_mask = (1ULL << config->pps_pin),
|
||||||
.mode = GPIO_MODE_INPUT,
|
.mode = GPIO_MODE_INPUT,
|
||||||
|
|
@ -209,7 +193,6 @@ void gps_sync_init(const gps_sync_config_t *config, bool use_gps_log_timestamps)
|
||||||
|
|
||||||
bool pps_detected = false;
|
bool pps_detected = false;
|
||||||
int start_level = gpio_get_level(config->pps_pin);
|
int start_level = gpio_get_level(config->pps_pin);
|
||||||
// Poll for up to 2 seconds
|
|
||||||
for (int i = 0; i < 2000; i++) {
|
for (int i = 0; i < 2000; i++) {
|
||||||
if (gpio_get_level(config->pps_pin) != start_level) {
|
if (gpio_get_level(config->pps_pin) != start_level) {
|
||||||
pps_detected = true;
|
pps_detected = true;
|
||||||
|
|
@ -220,12 +203,10 @@ void gps_sync_init(const gps_sync_config_t *config, bool use_gps_log_timestamps)
|
||||||
|
|
||||||
if (!pps_detected) {
|
if (!pps_detected) {
|
||||||
ESP_LOGW(TAG, "No PPS signal detected on GPIO %d during boot check.", config->pps_pin);
|
ESP_LOGW(TAG, "No PPS signal detected on GPIO %d during boot check.", config->pps_pin);
|
||||||
// We continue anyway, as GPS might gain lock later.
|
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGI(TAG, "PPS signal activity detected.");
|
ESP_LOGI(TAG, "PPS signal activity detected.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Setup Globals
|
|
||||||
gps_uart_num = config->uart_port;
|
gps_uart_num = config->uart_port;
|
||||||
use_gps_for_logs = use_gps_log_timestamps;
|
use_gps_for_logs = use_gps_log_timestamps;
|
||||||
gps_force_next_update();
|
gps_force_next_update();
|
||||||
|
|
@ -235,7 +216,6 @@ void gps_sync_init(const gps_sync_config_t *config, bool use_gps_log_timestamps)
|
||||||
esp_log_set_vprintf(gps_log_vprintf);
|
esp_log_set_vprintf(gps_log_vprintf);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Setup UART
|
|
||||||
uart_config_t uart_config = {
|
uart_config_t uart_config = {
|
||||||
.baud_rate = GPS_BAUD_RATE,
|
.baud_rate = GPS_BAUD_RATE,
|
||||||
.data_bits = UART_DATA_8_BITS,
|
.data_bits = UART_DATA_8_BITS,
|
||||||
|
|
@ -249,35 +229,28 @@ void gps_sync_init(const gps_sync_config_t *config, bool use_gps_log_timestamps)
|
||||||
ESP_ERROR_CHECK(uart_param_config(config->uart_port, &uart_config));
|
ESP_ERROR_CHECK(uart_param_config(config->uart_port, &uart_config));
|
||||||
ESP_ERROR_CHECK(uart_set_pin(config->uart_port, config->tx_pin, config->rx_pin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
|
ESP_ERROR_CHECK(uart_set_pin(config->uart_port, config->tx_pin, config->rx_pin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
|
||||||
|
|
||||||
// 4. Setup PPS Interrupt (Rising Edge)
|
|
||||||
gpio_config_t pps_intr_conf = {
|
gpio_config_t pps_intr_conf = {
|
||||||
.intr_type = GPIO_INTR_POSEDGE,
|
.intr_type = GPIO_INTR_POSEDGE,
|
||||||
.mode = GPIO_MODE_INPUT,
|
.mode = GPIO_MODE_INPUT,
|
||||||
.pin_bit_mask = (1ULL << config->pps_pin),
|
.pin_bit_mask = (1ULL << config->pps_pin),
|
||||||
.pull_up_en = GPIO_PULLUP_DISABLE, // High-Z usually best for driven PPS
|
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||||
};
|
};
|
||||||
ESP_ERROR_CHECK(gpio_config(&pps_intr_conf));
|
ESP_ERROR_CHECK(gpio_config(&pps_intr_conf));
|
||||||
|
|
||||||
// Install ISR service if not already done by other components
|
|
||||||
// Note: If you have other components using GPIO ISRs, ensure flags match or install is called only once.
|
|
||||||
gpio_install_isr_service(0);
|
gpio_install_isr_service(0);
|
||||||
ESP_ERROR_CHECK(gpio_isr_handler_add(config->pps_pin, pps_isr_handler, NULL));
|
ESP_ERROR_CHECK(gpio_isr_handler_add(config->pps_pin, pps_isr_handler, NULL));
|
||||||
|
|
||||||
// 5. Start Processor Task
|
|
||||||
xTaskCreate(gps_task, "gps_task", 4096, NULL, 5, NULL);
|
xTaskCreate(gps_task, "gps_task", 4096, NULL, 5, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
gps_timestamp_t gps_get_timestamp(void) {
|
gps_timestamp_t gps_get_timestamp(void) {
|
||||||
gps_timestamp_t ts;
|
gps_timestamp_t ts;
|
||||||
// Capture raw monotonic time
|
|
||||||
clock_gettime(CLOCK_MONOTONIC, &ts.mono_ts);
|
clock_gettime(CLOCK_MONOTONIC, &ts.mono_ts);
|
||||||
|
|
||||||
// Calculate microseconds
|
|
||||||
ts.monotonic_us = (int64_t)ts.mono_ts.tv_sec * 1000000LL + ts.mono_ts.tv_nsec / 1000;
|
ts.monotonic_us = (int64_t)ts.mono_ts.tv_sec * 1000000LL + ts.mono_ts.tv_nsec / 1000;
|
||||||
ts.monotonic_ms = ts.monotonic_us / 1000;
|
ts.monotonic_ms = ts.monotonic_us / 1000;
|
||||||
|
|
||||||
// Apply Offset safely
|
|
||||||
xSemaphoreTake(sync_mutex, portMAX_DELAY);
|
xSemaphoreTake(sync_mutex, portMAX_DELAY);
|
||||||
ts.gps_us = ts.monotonic_us + monotonic_offset_us;
|
ts.gps_us = ts.monotonic_us + monotonic_offset_us;
|
||||||
ts.synced = gps_has_fix;
|
ts.synced = gps_has_fix;
|
||||||
|
|
@ -295,6 +268,13 @@ bool gps_is_synced(void) {
|
||||||
return gps_has_fix;
|
return gps_has_fix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- NEW: PPS Age Getter ---
|
||||||
|
int64_t gps_get_pps_age_ms(void) {
|
||||||
|
if (last_pps_monotonic == 0) return -1;
|
||||||
|
int64_t now = esp_timer_get_time();
|
||||||
|
return (now - last_pps_monotonic) / 1000;
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------- LOGGING SYSTEM INTERCEPTION ----------------
|
// ---------------- LOGGING SYSTEM INTERCEPTION ----------------
|
||||||
|
|
||||||
uint32_t gps_log_timestamp(void) {
|
uint32_t gps_log_timestamp(void) {
|
||||||
|
|
@ -308,7 +288,6 @@ int gps_log_vprintf(const char *fmt, va_list args) {
|
||||||
|
|
||||||
if (use_gps_for_logs) {
|
if (use_gps_for_logs) {
|
||||||
char *timestamp_start = NULL;
|
char *timestamp_start = NULL;
|
||||||
// Find ESP log timestamp format "(1234)"
|
|
||||||
for (int i = 0; buffer[i] != '\0' && i < sizeof(buffer) - 20; i++) {
|
for (int i = 0; buffer[i] != '\0' && i < sizeof(buffer) - 20; i++) {
|
||||||
if ((buffer[i] == 'I' || buffer[i] == 'W' || buffer[i] == 'E' ||
|
if ((buffer[i] == 'I' || buffer[i] == 'W' || buffer[i] == 'E' ||
|
||||||
buffer[i] == 'D' || buffer[i] == 'V') &&
|
buffer[i] == 'D' || buffer[i] == 'V') &&
|
||||||
|
|
@ -326,7 +305,6 @@ int gps_log_vprintf(const char *fmt, va_list args) {
|
||||||
char reformatted[512];
|
char reformatted[512];
|
||||||
size_t prefix_len = timestamp_start - buffer;
|
size_t prefix_len = timestamp_start - buffer;
|
||||||
|
|
||||||
// Copy prefix "I "
|
|
||||||
if (prefix_len > sizeof(reformatted)) prefix_len = sizeof(reformatted);
|
if (prefix_len > sizeof(reformatted)) prefix_len = sizeof(reformatted);
|
||||||
memcpy(reformatted, buffer, prefix_len);
|
memcpy(reformatted, buffer, prefix_len);
|
||||||
|
|
||||||
|
|
@ -336,24 +314,19 @@ int gps_log_vprintf(const char *fmt, va_list args) {
|
||||||
int64_t log_mono_us = (int64_t)monotonic_log_ms * 1000;
|
int64_t log_mono_us = (int64_t)monotonic_log_ms * 1000;
|
||||||
int64_t log_gps_us = log_mono_us + monotonic_offset_us;
|
int64_t log_gps_us = log_mono_us + monotonic_offset_us;
|
||||||
|
|
||||||
// Split into seconds and fractional ms
|
|
||||||
uint64_t gps_sec = log_gps_us / 1000000;
|
uint64_t gps_sec = log_gps_us / 1000000;
|
||||||
uint32_t gps_ms = (log_gps_us % 1000000) / 1000;
|
uint32_t gps_ms = (log_gps_us % 1000000) / 1000;
|
||||||
|
|
||||||
// Overwrite timestamp with GPS time e.g., "+1703000000.123"
|
|
||||||
decimal_len = snprintf(reformatted + prefix_len,
|
decimal_len = snprintf(reformatted + prefix_len,
|
||||||
sizeof(reformatted) - prefix_len,
|
sizeof(reformatted) - prefix_len,
|
||||||
"+%" PRIu64 ".%03lu", gps_sec, gps_ms);
|
"+%" PRIu64 ".%03lu", gps_sec, gps_ms);
|
||||||
} else {
|
} else {
|
||||||
// Fallback: Keep monotonic but mark as unsynced
|
|
||||||
uint32_t sec = monotonic_log_ms / 1000;
|
uint32_t sec = monotonic_log_ms / 1000;
|
||||||
uint32_t ms = monotonic_log_ms % 1000;
|
uint32_t ms = monotonic_log_ms % 1000;
|
||||||
decimal_len = snprintf(reformatted + prefix_len,
|
decimal_len = snprintf(reformatted + prefix_len,
|
||||||
sizeof(reformatted) - prefix_len,
|
sizeof(reformatted) - prefix_len,
|
||||||
"*%lu.%03lu", (unsigned long)sec, (unsigned long)ms);
|
"*%lu.%03lu", (unsigned long)sec, (unsigned long)ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy remainder of message
|
|
||||||
strcpy(reformatted + prefix_len + decimal_len, timestamp_end);
|
strcpy(reformatted + prefix_len + decimal_len, timestamp_end);
|
||||||
return printf("%s", reformatted);
|
return printf("%s", reformatted);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,8 +23,16 @@ typedef struct {
|
||||||
|
|
||||||
void gps_sync_init(const gps_sync_config_t *config, bool use_gps_log_timestamps);
|
void gps_sync_init(const gps_sync_config_t *config, bool use_gps_log_timestamps);
|
||||||
void gps_force_next_update(void);
|
void gps_force_next_update(void);
|
||||||
|
|
||||||
|
// --- Getters ---
|
||||||
gps_timestamp_t gps_get_timestamp(void);
|
gps_timestamp_t gps_get_timestamp(void);
|
||||||
int64_t gps_get_monotonic_ms(void);
|
int64_t gps_get_monotonic_ms(void);
|
||||||
bool gps_is_synced(void);
|
bool gps_is_synced(void);
|
||||||
|
|
||||||
|
// Check health of the physical PPS signal
|
||||||
|
int64_t gps_get_pps_age_ms(void);
|
||||||
|
void gps_get_last_nmea(char *buf, size_t max_len);
|
||||||
|
|
||||||
|
// --- Logging Hooks ---
|
||||||
uint32_t gps_log_timestamp(void);
|
uint32_t gps_log_timestamp(void);
|
||||||
int gps_log_vprintf(const char *fmt, va_list args);
|
int gps_log_vprintf(const char *fmt, va_list args);
|
||||||
|
|
|
||||||
45
main/main.c
45
main/main.c
|
|
@ -9,6 +9,7 @@
|
||||||
#include "esp_vfs_dev.h"
|
#include "esp_vfs_dev.h"
|
||||||
#include "driver/uart.h"
|
#include "driver/uart.h"
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
|
#include "nvs.h" // Added for NVS read
|
||||||
#include "esp_netif.h"
|
#include "esp_netif.h"
|
||||||
#include "esp_event.h"
|
#include "esp_event.h"
|
||||||
|
|
||||||
|
|
@ -34,11 +35,8 @@ static const char *TAG = "MAIN";
|
||||||
static char s_cli_prompt[32] = "esp32> ";
|
static char s_cli_prompt[32] = "esp32> ";
|
||||||
|
|
||||||
// --- Prompt Updater ---
|
// --- Prompt Updater ---
|
||||||
// This is called by app_console.c commands whenever a setting is changed.
|
|
||||||
void app_console_update_prompt(void) {
|
void app_console_update_prompt(void) {
|
||||||
bool dirty = false;
|
bool dirty = false;
|
||||||
|
|
||||||
// Check if any component has unsaved changes (RAM != NVS)
|
|
||||||
if (wifi_ctl_param_is_unsaved()) dirty = true;
|
if (wifi_ctl_param_is_unsaved()) dirty = true;
|
||||||
if (iperf_param_is_unsaved()) dirty = true;
|
if (iperf_param_is_unsaved()) dirty = true;
|
||||||
|
|
||||||
|
|
@ -49,6 +47,21 @@ void app_console_update_prompt(void) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Helper: Check NVS for GPS Enable ---
|
||||||
|
static bool is_gps_enabled(void) {
|
||||||
|
nvs_handle_t h;
|
||||||
|
uint8_t val = 1; // Default to Enabled (1)
|
||||||
|
|
||||||
|
// Check 'storage' namespace first (where iperf/system settings live)
|
||||||
|
if (nvs_open("storage", NVS_READONLY, &h) == ESP_OK) {
|
||||||
|
if (nvs_get_u8(h, "gps_enabled", &val) != ESP_OK) {
|
||||||
|
val = 1; // Key missing = Enabled
|
||||||
|
}
|
||||||
|
nvs_close(h);
|
||||||
|
}
|
||||||
|
return (val != 0);
|
||||||
|
}
|
||||||
|
|
||||||
// --- System Commands ---
|
// --- System Commands ---
|
||||||
|
|
||||||
static int cmd_restart(int argc, char **argv) {
|
static int cmd_restart(int argc, char **argv) {
|
||||||
|
|
@ -93,13 +106,21 @@ void app_main(void) {
|
||||||
// 2. Initialize Netif & Event Loop
|
// 2. Initialize Netif & Event Loop
|
||||||
ESP_ERROR_CHECK(esp_netif_init());
|
ESP_ERROR_CHECK(esp_netif_init());
|
||||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||||
const gps_sync_config_t gps_cfg = {
|
|
||||||
.uart_port = UART_NUM_1,
|
// -------------------------------------------------------------
|
||||||
.tx_pin = GPS_TX_PIN,
|
// GPS Initialization (Conditional)
|
||||||
.rx_pin = GPS_RX_PIN,
|
// -------------------------------------------------------------
|
||||||
.pps_pin = GPS_PPS_PIN,
|
if (is_gps_enabled()) {
|
||||||
};
|
const gps_sync_config_t gps_cfg = {
|
||||||
gps_sync_init(&gps_cfg, true);
|
.uart_port = UART_NUM_1,
|
||||||
|
.tx_pin = GPS_TX_PIN,
|
||||||
|
.rx_pin = GPS_RX_PIN,
|
||||||
|
.pps_pin = GPS_PPS_PIN,
|
||||||
|
};
|
||||||
|
gps_sync_init(&gps_cfg, true);
|
||||||
|
} else {
|
||||||
|
ESP_LOGW(TAG, "GPS initialization skipped (Disabled in NVS)");
|
||||||
|
}
|
||||||
|
|
||||||
// 3. Hardware Init
|
// 3. Hardware Init
|
||||||
status_led_init(RGB_LED_GPIO, HAS_RGB_LED);
|
status_led_init(RGB_LED_GPIO, HAS_RGB_LED);
|
||||||
|
|
@ -116,13 +137,9 @@ void app_main(void) {
|
||||||
esp_console_repl_t *repl = NULL;
|
esp_console_repl_t *repl = NULL;
|
||||||
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
|
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
// CRITICAL FIX: Use the mutable buffer, NOT a string literal
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
repl_config.prompt = s_cli_prompt;
|
repl_config.prompt = s_cli_prompt;
|
||||||
repl_config.max_cmdline_length = 1024;
|
repl_config.max_cmdline_length = 1024;
|
||||||
|
|
||||||
// Install UART driver for Console (Standard IO)
|
|
||||||
esp_console_dev_uart_config_t hw_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
|
esp_console_dev_uart_config_t hw_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
|
||||||
ESP_ERROR_CHECK(esp_console_new_repl_uart(&hw_config, &repl_config, &repl));
|
ESP_ERROR_CHECK(esp_console_new_repl_uart(&hw_config, &repl_config, &repl));
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue