1
0
forked from tanchou/Verilog

Code FPGA fonctionnel

This commit is contained in:
Gamenight77
2025-05-27 15:36:40 +02:00
parent 4e16bb3cbe
commit 168431849b
24 changed files with 2038 additions and 4 deletions

View File

@@ -0,0 +1,106 @@
#include <WiFi.h>
#include <WiFiManager.h> // Include WiFiManager library
// TCP server port
const uint16_t port = 1234;
// TCP server object
WiFiServer server(port);
// TCP client object
WiFiClient client;
// Touch sensor settings
const int touchPin = T0; // Use touch pin T0 (GPIO 4)
const int touchThreshold = 30; // Adjust based on testing (lower value = touch detected)
const unsigned long resetHoldTime = 5000; // 5 seconds to trigger reset
unsigned long touchStartTime = 0;
bool touchDetected = false;
void setup() {
// Initialize Serial for UART communication (115200 baud)
Serial.begin(115200);
// Initialize WiFiManager
WiFiManager wifiManager;
// Check for touch sensor reset
if (checkTouchReset()) {
wifiManager.resetSettings();
Serial.println("WiFi settings reset due to touch sensor");
ESP.restart(); // Restart to apply reset
}
// Connect to WiFi using WiFiManager (creates AP if no saved credentials)
if (!wifiManager.autoConnect("ESP32_AP")) {
Serial.println("Failed to connect to WiFi and hit timeout");
ESP.restart();
}
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Start TCP server
server.begin();
Serial.println("TCP server started on port " + String(port));
}
void loop() {
// Check for touch sensor reset
handleTouchReset();
// Check for incoming client connections
if (!client.connected()) {
client = server.available();
if (client) {
Serial.println("New client connected");
}
}
// Relay data from TCP client to UART
while (client.connected() && client.available()) {
char c = client.read();
Serial.write(c); // Send to UART
}
// Relay data from UART to TCP client
while (Serial.available() && client.connected()) {
char c = Serial.read();
client.write(c); // Send to TCP client
}
// Handle client disconnection
if (client && !client.connected()) {
Serial.println("Client disconnected");
client.stop();
}
}
// Function to check touch sensor at boot for reset
bool checkTouchReset() {
int touchValue = touchRead(touchPin);
if (touchValue < touchThreshold) {
unsigned long startTime = millis();
while (touchRead(touchPin) < touchThreshold) {
if (millis() - startTime >= resetHoldTime) {
return true; // Touch held long enough, trigger reset
}
}
}
return false;
}
// Function to handle touch sensor reset during loop
void handleTouchReset() {
int touchValue = touchRead(touchPin);
if (touchValue < touchThreshold && !touchDetected) {
touchDetected = true;
touchStartTime = millis();
} else if (touchValue >= touchThreshold && touchDetected) {
touchDetected = false; // Reset when touch is released
}
if (touchDetected && (millis() - touchStartTime >= resetHoldTime)) {
WiFiManager wifiManager;
wifiManager.resetSettings();
Serial.println("WiFi settings reset due to touch sensor");
ESP.restart(); // Restart to apply reset
}
}

View File

@@ -0,0 +1,134 @@
`default_nettype none
module fpga_wifi_led (
input i_clk,
input i_rx,
output o_tx,
output [5:0] o_leds
);
// === PARAMÈTRES ===
localparam CLK_FREQ = 27_000_000;
localparam BAUD_RATE = 115200;
localparam FIFO_SIZE = 8;
// === SIGNAUX UART RX ===
wire [7:0] rx_data;
wire rx_data_available;
reg rx_rd_en;
// === SIGNAUX UART TX ===
reg [7:0] tx_data;
reg tx_wr_en;
wire tx_fifo_full;
// === SIGNAUX INTERNES ===
reg [5:0] leds_reg;
reg [1:0] state;
reg [7:0] received_byte;
// === ÉTATS DE LA FSM ===
localparam IDLE = 2'd0,
WAIT_BYTE = 2'd1,
PROCESS_CMD = 2'd2,
SEND_RESPONSE = 2'd3;
// === INSTANCIATION UART RX FIFO ===
uart_rx_fifo #(
.CLK_FREQ(CLK_FREQ),
.BAUD_RATE(BAUD_RATE),
.FIFO_SIZE(FIFO_SIZE)
) uart_rx_inst (
.clk(i_clk),
.rd_en(rx_rd_en),
.rd_data(rx_data),
.rx_pin(i_rx),
.data_available(rx_data_available)
);
// === INSTANCIATION UART TX FIFO ===
uart_tx_fifo #(
.CLK_FREQ(CLK_FREQ),
.BAUD_RATE(BAUD_RATE),
.FIFO_SIZE(FIFO_SIZE)
) uart_tx_inst (
.clk(i_clk),
.wr_en(tx_wr_en),
.wr_data(tx_data),
.tx_pin(o_tx),
.fifo_full(tx_fifo_full)
);
// === ASSIGNATION DES LEDS ===
assign o_leds = ~leds_reg;
// === INITIALISATION ===
initial begin
leds_reg = 6'b000000;
state = IDLE;
rx_rd_en = 0;
tx_wr_en = 0;
tx_data = 8'h00;
received_byte = 8'h00;
end
// === MACHINE D'ÉTAT PRINCIPALE ===
always @(posedge i_clk) begin
// Désactiver les signaux de contrôle par défaut
rx_rd_en <= 0;
tx_wr_en <= 0;
case (state)
IDLE: begin
received_byte <= 8'h00;
if (rx_data_available) begin
state <= WAIT_BYTE;
end
end
WAIT_BYTE: begin
if (rx_data_available) begin
rx_rd_en <= 1;
received_byte <= rx_data;
state <= PROCESS_CMD;
end
end
PROCESS_CMD: begin
// Vérifier la commande reçue
if (received_byte == 8'h01) begin
// Allumer la LED 0
leds_reg[0] <= 1;
// Préparer la réponse
state <= SEND_RESPONSE;
end else if (received_byte == 8'h02) begin
// Éteindre la LED 0
leds_reg[0] <= 0;
// Préparer la réponse
state <= SEND_RESPONSE;
end else begin
// Commande non reconnue, éteindre toutes les LEDs
leds_reg <= 6'b000000;
state <= IDLE;
end
end
SEND_RESPONSE: begin
if (!tx_fifo_full) begin
// Renvoyer le même byte reçu (0x01 ou 0x02)
tx_data <= received_byte;
tx_wr_en <= 1;
state <= IDLE;
end
end
default: begin
state <= IDLE;
end
endcase
end
endmodule