1
0
forked from tanchou/Verilog

Semaine 6 init

This commit is contained in:
Gamenight77
2025-05-19 09:14:04 +02:00
parent 6ad0716f8f
commit 75d1ff029b
24 changed files with 2490 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
import serial
import time
import struct
# === Paramètres de communication ===
SERIAL_PORT = "COM6" # Modifie selon ton système, ex. "/dev/ttyUSB0" sur Linux
BAUDRATE = 115200 # Change si différent
TIMEOUT = 2 # secondes
# === Commandes (doivent correspondre aux valeurs Verilog) ===
CMD_STOP = 3
CMD_ONE = 1
CMD_CONTINUOUS = 2
def send_command(ser, command):
"""Envoie une commande au FPGA"""
ser.write(bytes([command]))
def read_distance(ser):
"""Lit 2 octets et les convertit en distance (int16)"""
data = ser.read(2)
if len(data) != 2:
return None
# Interprétation little-endian (LSB, MSB)
lsb, msb = data[0], data[1]
distance = msb << 8 | lsb
return distance
def main():
with serial.Serial(SERIAL_PORT, BAUDRATE, timeout=TIMEOUT) as ser:
print("Connexion ouverte sur", SERIAL_PORT)
mode = input("Mode (one / continuous / stop) ? ").strip().lower()
if mode == "one":
send_command(ser, CMD_ONE)
print("Mesure unique demandée. Attente résultat...")
time.sleep(0.05)
distance = read_distance(ser)
if distance is not None:
print(f"Distance mesurée : {distance} cm")
else:
print("Erreur : distance non reçue.")
elif mode == "continuous":
send_command(ser, CMD_CONTINUOUS)
print("Mesures continues (CTRL+C pour stopper) :")
try:
while True:
distance = read_distance(ser)
if distance is not None:
print(f"Distance : {distance} cm")
else:
print("... (aucune donnée reçue)")
time.sleep(0.1)
except KeyboardInterrupt:
send_command(ser, CMD_STOP)
print("\nMesure continue arrêtée.")
elif mode == "stop":
send_command(ser, CMD_STOP)
print("Commande STOP envoyée.")
else:
print("Commande invalide.")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,187 @@
`timescale 1ns/1ps
module tb_ultrason_commands;
// === Signaux ===
reg clk = 0;
always #18.5 clk = ~clk; // Horloge 27 MHz (période ~37ns)
wire tx, rx;
wire [5:0] leds;
wire ultrason_sig;
reg tx_enable = 0;
wire tx_ready;
reg [7:0] data_in = 8'h00;
wire [7:0] data_out;
wire data_available;
reg rd_en = 0;
// === Paramètres ===
localparam CLK_FREQ = 27_000_000;
localparam BAUD_RATE = 115_200;
localparam CLK_PERIOD = 37; // ns
// === Module testé ===
top_uart_ultrason_command dut (
.clk(clk),
.rx(rx),
.tx(tx),
.ultrason_sig(ultrason_sig),
.leds(leds)
);
// === Simulation capteur ultrason ===
// Supposons que fake_sensor fournit un signal 'distance' pour vérification
wire [15:0] sensor_distance;
ultrasonic_sensor fake_sensor (
.clk(clk),
.signal(ultrason_sig),
);
// === RX FIFO pour observer la sortie UART ===
uart_rx_fifo #(
.CLK_FREQ(CLK_FREQ),
.BAUD_RATE(BAUD_RATE)
) uart_rx_fifo_inst (
.clk(clk),
.rx_pin(tx),
.rd_en(rd_en),
.rd_data(data_out),
.data_available(data_available)
);
// === TX pour injecter des commandes UART ===
uart_tx #(
.CLK_FREQ(CLK_FREQ),
.BAUD_RATE(BAUD_RATE)
) uart_tx_inst (
.clk(clk),
.tx_enable(tx_enable),
.tx_ready(tx_ready),
.data(data_in),
.tx(rx),
.rst_p(1'b0)
);
// === Tâches pour simplifier les tests ===
task send_command(input [7:0] cmd);
begin
wait(tx_ready);
$display("[%0t ns] Envoi commande: %0d", $time, cmd);
data_in = cmd;
tx_enable = 1;
#(CLK_PERIOD * 2);
tx_enable = 0;
end
endtask
task read_distance(output [15:0] distance);
reg [7:0] lsb, msb;
begin
// Attendre premier octet (LSB)
wait(data_available);
#(CLK_PERIOD);
rd_en = 1;
#(CLK_PERIOD);
lsb = data_out;
rd_en = 0;
$display("[%0t ns] Reçu octet LSB: %0d", $time, lsb);
// Attendre second octet (MSB)
wait(data_available);
#(CLK_PERIOD);
rd_en = 1;
#(CLK_PERIOD);
msb = data_out;
rd_en = 0;
$display("[%0t ns] Reçu octet MSB: %0d", $time, msb);
distance = {msb, lsb};
end
endtask
task check_leds(input [7:0] cmd);
begin
#(CLK_PERIOD * 10); // Attendre mise à jour des LEDs
if (leds !== cmd[7:2]) begin
$display("[%0t ns] ERREUR: LEDs=%b, attendu=%b", $time, leds, cmd[7:2]);
end else begin
$display("[%0t ns] LEDs correctes: %b", $time, leds);
end
end
endtask
// === Séquence de test ===
initial begin
$dumpfile("runs/ultrason_commands.vcd");
$dumpvars(0, tb_ultrason_commands);
$display("==== Début Test UART Ultrason ====");
// Initialisation
#(CLK_PERIOD * 10);
// Test 1: Commande ONE (8'd1)
$display("=== Test 1: Commande ONE ===");
send_command(8'd1); // Commande: ONE
check_leds(8'd1);
begin
reg [15:0] received_distance;
read_distance(received_distance);
if (received_distance == sensor_distance) begin
$display("[%0t ns] Distance correcte: %0d", $time, received_distance);
end else begin
$display("[%0t ns] ERREUR: Distance reçue=%0d, attendu=%0d", $time, received_distance, sensor_distance);
end
end
// Test 2: Commande CONTINUOUS (8'd2)
$display("=== Test 2: Commande CONTINUOUS ===");
send_command(8'd2); // Commande: CONTINUOUS
check_leds(8'd2);
repeat (3) begin // Lire 3 mesures consécutives
reg [15:0] received_distance;
read_distance(received_distance);
if (received_distance == sensor_distance) begin
$display("[%0t ns] Distance continue correcte: %0d", $time, received_distance);
end else begin
$display("[%0t ns] ERREUR: Distance reçue=%0d, attendu=%0d", $time, received_distance, sensor_distance);
end
#(CLK_PERIOD * 13500000); // Attendre ~0.5s (délai dans WAIT)
end
// Test 3: Commande STOP (8'd3)
$display("=== Test 3: Commande STOP ===");
send_command(8'd3); // Commande: STOP
check_leds(8'd3);
#(CLK_PERIOD * 13500000); // Attendre pour vérifier l'arrêt
if (data_available) begin
$display("[%0t ns] Vérification STOP: aucune donnée ne doit être reçue", $time);
#(CLK_PERIOD * 1000);
if (data_available) begin
$display("[%0t ns] ERREUR: Données reçues après STOP", $time);
end else begin
$display("[%0t ns] STOP correct: aucune donnée reçue", $time);
end
end
// Test 4: Commande invalide (8'd4)
$display("=== Test 4: Commande invalide ===");
send_command(8'd4); // Commande invalide
#(CLK_PERIOD * 1000);
if (data_available) begin
$display("[%0t ns] ERREUR: Données reçues pour commande invalide", $time);
end else begin
$display("[%0t ns] Commande invalide ignorée correctement", $time);
end
// Fin de la simulation
$display("==== Fin Test UART Ultrason ====");
#(CLK_PERIOD * 1000);
$stop;
end
endmodule