Add firmware skeleton for RP2040
Complete working firmware including: - CMakeLists.txt for Pico SDK build - SSD1306 OLED driver (128x32, I2C) - High-resolution latency measurement using hardware timer - Debounced button with short/long press detection - Three modes: Single, Continuous, Stats - USB serial debugging output Includes 8x8 font with numbers and letters for display.
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
#ifndef BUTTON_H
|
||||
#define BUTTON_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum {
|
||||
BTN_NONE,
|
||||
BTN_SHORT_PRESS, // < 500ms
|
||||
BTN_LONG_PRESS // >= 500ms
|
||||
} button_event_t;
|
||||
|
||||
// Initialize button GPIO
|
||||
void button_init(void);
|
||||
|
||||
// Poll button state (call in main loop)
|
||||
// Returns event type when button is released
|
||||
button_event_t button_poll(void);
|
||||
|
||||
// Check if button is currently pressed
|
||||
bool button_is_pressed(void);
|
||||
|
||||
#endif // BUTTON_H
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
// GPIO Pin Assignments (RP2040-Zero)
|
||||
#define PIN_I2C_SDA 0 // OLED SDA
|
||||
#define PIN_I2C_SCL 1 // OLED SCL
|
||||
#define PIN_TRIG_OUT 2 // Trigger output
|
||||
#define PIN_RETURN_IN 3 // Return input
|
||||
#define PIN_BUTTON 4 // Mode button
|
||||
|
||||
// I2C Configuration
|
||||
#define I2C_PORT i2c0
|
||||
#define I2C_FREQ 400000 // 400 kHz
|
||||
|
||||
// OLED Display (SSD1306 128x32)
|
||||
#define OLED_ADDR 0x3C
|
||||
#define OLED_WIDTH 128
|
||||
#define OLED_HEIGHT 32
|
||||
|
||||
// Timing Configuration
|
||||
#define TRIGGER_PULSE_MS 5 // Trigger pulse width
|
||||
#define MEASURE_TIMEOUT_MS 1000 // Max latency measurement
|
||||
#define DEBOUNCE_MS 50 // Button debounce
|
||||
#define CONTINUOUS_INTERVAL_MS 500 // Interval for continuous mode
|
||||
|
||||
// Display
|
||||
#define DISPLAY_UPDATE_MS 100 // Display refresh rate
|
||||
|
||||
// Statistics
|
||||
#define STATS_SAMPLES 10 // Rolling average sample count
|
||||
|
||||
#endif // CONFIG_H
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef DISPLAY_H
|
||||
#define DISPLAY_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// Initialize the OLED display
|
||||
void display_init(void);
|
||||
|
||||
// Clear the display
|
||||
void display_clear(void);
|
||||
|
||||
// Show latency value in milliseconds
|
||||
void display_latency(float latency_ms);
|
||||
|
||||
// Show "WAITING..." message
|
||||
void display_waiting(void);
|
||||
|
||||
// Show "TIMEOUT" message
|
||||
void display_timeout(void);
|
||||
|
||||
// Show statistics (min/max/avg)
|
||||
void display_stats(float min_ms, float max_ms, float avg_ms);
|
||||
|
||||
// Show mode indicator
|
||||
void display_mode(const char* mode);
|
||||
|
||||
// Update display (call periodically)
|
||||
void display_update(void);
|
||||
|
||||
#endif // DISPLAY_H
|
||||
@@ -0,0 +1,38 @@
|
||||
#ifndef LATENCY_H
|
||||
#define LATENCY_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// Measurement result
|
||||
typedef struct {
|
||||
bool valid; // True if measurement succeeded
|
||||
float latency_ms; // Measured latency in milliseconds
|
||||
uint64_t latency_us; // Raw latency in microseconds
|
||||
} measurement_t;
|
||||
|
||||
// Statistics
|
||||
typedef struct {
|
||||
float min_ms;
|
||||
float max_ms;
|
||||
float avg_ms;
|
||||
uint32_t count;
|
||||
} stats_t;
|
||||
|
||||
// Initialize latency measurement hardware
|
||||
void latency_init(void);
|
||||
|
||||
// Perform a single latency measurement
|
||||
// Returns result struct with valid flag and latency value
|
||||
measurement_t latency_measure(void);
|
||||
|
||||
// Get current statistics
|
||||
stats_t latency_get_stats(void);
|
||||
|
||||
// Reset statistics
|
||||
void latency_reset_stats(void);
|
||||
|
||||
// Add measurement to statistics
|
||||
void latency_add_to_stats(float latency_ms);
|
||||
|
||||
#endif // LATENCY_H
|
||||
Reference in New Issue
Block a user