forked from tanchou/Verilog
init semaine 7
This commit is contained in:
5
Semaine_7/DHT11/.gitignore
vendored
Normal file
5
Semaine_7/DHT11/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
runs
|
||||
.vscode
|
||||
workspace.code-workspace
|
||||
*.pyc
|
||||
.idea
|
9
Semaine_7/DHT11/README.md
Normal file
9
Semaine_7/DHT11/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# ULTRASON VIA UART
|
||||
|
||||
## Description
|
||||
This project is designed to control an ultrasonic sensor using UART communication. The ultrasonic sensor is used to measure distance, and the data is transmitted via UART to a connected device.
|
||||
|
||||
## Commands
|
||||
0x01: Start one mesurement of the distance.
|
||||
0x02: Start continuous mesurement of the distance.
|
||||
0x03: Stop continuous mesurement of the distance.
|
24
Semaine_7/DHT11/constraints/dht11_interface.cst
Normal file
24
Semaine_7/DHT11/constraints/dht11_interface.cst
Normal file
@@ -0,0 +1,24 @@
|
||||
IO_LOC "tx" 69;
|
||||
IO_PORT "tx" IO_TYPE=LVCMOS33 PULL_MODE=UP BANK_VCCIO=3.3;
|
||||
|
||||
IO_LOC "rx" 70;
|
||||
IO_PORT "rx" IO_TYPE=LVCMOS33 PULL_MODE=UP BANK_VCCIO=3.3;
|
||||
|
||||
IO_LOC "clk" 4;
|
||||
IO_PORT "clk" IO_TYPE=LVCMOS33 PULL_MODE=UP BANK_VCCIO=3.3;
|
||||
|
||||
IO_LOC "ultrason_sig" 73;
|
||||
IO_PORT "ultrason_sig" IO_TYPE=LVCMOS33 PULL_MODE=UP DRIVE=8 BANK_VCCIO=3.3;
|
||||
|
||||
IO_LOC "leds[0]" 15;
|
||||
IO_PORT "leds[0]" PULL_MODE=UP DRIVE=8 BANK_VCCIO=1.8;
|
||||
IO_LOC "leds[1]" 16;
|
||||
IO_PORT "leds[1]" PULL_MODE=UP DRIVE=8 BANK_VCCIO=1.8;
|
||||
IO_LOC "leds[2]" 17;
|
||||
IO_PORT "leds[2]" PULL_MODE=UP DRIVE=8 BANK_VCCIO=1.8;
|
||||
IO_LOC "leds[3]" 18;
|
||||
IO_PORT "leds[3]" PULL_MODE=UP DRIVE=8 BANK_VCCIO=1.8;
|
||||
IO_LOC "leds[4]" 19;
|
||||
IO_PORT "leds[4]" PULL_MODE=UP DRIVE=8 BANK_VCCIO=1.8;
|
||||
IO_LOC "leds[5]" 20;
|
||||
IO_PORT "leds[5]" PULL_MODE=UP DRIVE=8 BANK_VCCIO=1.8;
|
9
Semaine_7/DHT11/project.bat
Normal file
9
Semaine_7/DHT11/project.bat
Normal file
@@ -0,0 +1,9 @@
|
||||
@call c:\oss-cad-suite\environment.bat
|
||||
@echo off
|
||||
|
||||
mkdir runs
|
||||
|
||||
if "%1"=="sim" call scripts\windows\simulate.bat
|
||||
if "%1"=="wave" call scripts\windows\gtkwave.bat
|
||||
if "%1"=="clean" call scripts\windows\clean.bat
|
||||
if "%1"=="build" call scripts\windows\build.bat
|
24
Semaine_7/DHT11/project.sh
Normal file
24
Semaine_7/DHT11/project.sh
Normal file
@@ -0,0 +1,24 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Charger l'environnement OSS CAD Suite
|
||||
source /home/louis/oss-cad-suite/environment
|
||||
|
||||
mkdir -p runs
|
||||
|
||||
case "$1" in
|
||||
sim)
|
||||
bash scripts/linux/simulate.sh
|
||||
;;
|
||||
wave)
|
||||
bash scripts/linux/gtkwave.sh
|
||||
;;
|
||||
clean)
|
||||
bash scripts/linux/clean.sh
|
||||
;;
|
||||
build)
|
||||
bash scripts/linux/build.sh
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 {sim|wave|clean|build}"
|
||||
;;
|
||||
esac
|
46
Semaine_7/DHT11/scripts/linux/build.sh
Normal file
46
Semaine_7/DHT11/scripts/linux/build.sh
Normal file
@@ -0,0 +1,46 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Aller à la racine du projet
|
||||
cd "$(dirname "$0")/../.." || exit 1
|
||||
|
||||
# Config de base
|
||||
DEVICE="GW2AR-LV18QN88C8/I7"
|
||||
BOARD="tangnano20k"
|
||||
TOP="top_uart_ultrason_command"
|
||||
CST_FILE="$TOP.cst"
|
||||
JSON_FILE="runs/$TOP.json"
|
||||
PNR_JSON="runs/pnr_$TOP.json"
|
||||
BITSTREAM="runs/$TOP.fs"
|
||||
|
||||
# Créer le dossier runs si nécessaire
|
||||
mkdir -p runs
|
||||
|
||||
echo "=== Étape 1 : Synthèse avec Yosys ==="
|
||||
yosys -p "read_verilog -sv src/verilog/$TOP.v IP/verilog/ultrasonic_fpga.v IP/verilog/uart_tx_fifo.v IP/verilog/uart_rx_fifo.v IP/verilog/rxuartlite.v IP/verilog/fifo.v IP/verilog/uart_tx.v; synth_gowin -top $TOP -json $JSON_FILE"
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "=== Erreur lors de la synthèse ==="
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "=== Étape 2 : Placement & Routage avec nextpnr-himbaechel ==="
|
||||
nextpnr-himbaechel --json "$JSON_FILE" --write "$PNR_JSON" --device "$DEVICE" --vopt cst=constraints/"$CST_FILE" --vopt family=GW2A-18C
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "=== Erreur lors du placement/routage ==="
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "=== Étape 3 : Packing avec gowin_pack ==="
|
||||
gowin_pack -d "$DEVICE" -o "$BITSTREAM" "$PNR_JSON"
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "=== Erreur lors du packing ==="
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "=== Étape 4 : Flash avec openFPGALoader ==="
|
||||
openFPGALoader -b "$BOARD" "$BITSTREAM"
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "=== Erreur lors du flash ==="
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "=== Compilation et flash réussis ==="
|
4
Semaine_7/DHT11/scripts/linux/clean.sh
Normal file
4
Semaine_7/DHT11/scripts/linux/clean.sh
Normal file
@@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo "=== Nettoyage des fichiers générés ==="
|
||||
rm -rf runs/*
|
5
Semaine_7/DHT11/scripts/linux/gtkwave.sh
Normal file
5
Semaine_7/DHT11/scripts/linux/gtkwave.sh
Normal file
@@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo "=== Lancement de GTKWave ==="
|
||||
gtkwave runs/sim.vcd
|
||||
echo "=== GTKWave terminé ==="
|
17
Semaine_7/DHT11/scripts/linux/simulate.sh
Normal file
17
Semaine_7/DHT11/scripts/linux/simulate.sh
Normal file
@@ -0,0 +1,17 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo "=== Simulation avec Icarus Verilog ==="
|
||||
|
||||
OUT="runs/sim.vvp"
|
||||
TOP="tb_dht11"
|
||||
DIRS=("src/verilog" "tests/verilog")
|
||||
|
||||
FILES=()
|
||||
for dir in "${DIRS[@]}"; do
|
||||
for file in "$dir"/*.v; do
|
||||
FILES+=("$file")
|
||||
done
|
||||
done
|
||||
|
||||
iverilog -g2012 -o "$OUT" -s "$TOP" "${FILES[@]}"
|
||||
vvp "$OUT"
|
45
Semaine_7/DHT11/scripts/windows/build.bat
Normal file
45
Semaine_7/DHT11/scripts/windows/build.bat
Normal file
@@ -0,0 +1,45 @@
|
||||
@echo off
|
||||
setlocal
|
||||
|
||||
rem === Aller à la racine du projet ===
|
||||
cd /d %~dp0\..
|
||||
|
||||
rem === Config de base ===
|
||||
set DEVICE=GW2AR-LV18QN88C8/I7
|
||||
set BOARD=tangnano20k
|
||||
set TOP=top_uart_ultrason_command
|
||||
set CST_FILE=%TOP%.cst
|
||||
set JSON_FILE=runs/%TOP%.json
|
||||
set PNR_JSON=runs/pnr_%TOP%.json
|
||||
set BITSTREAM=runs/%TOP%.fs
|
||||
|
||||
rem === Créer le dossier runs si nécessaire ===
|
||||
if not exist runs (
|
||||
mkdir runs
|
||||
)
|
||||
|
||||
echo === Étape 1 : Synthèse avec Yosys ===
|
||||
yosys -p "read_verilog -sv src/verilog/%TOP%.v IP/verilog/ultrasonic_fpga.v IP/verilog/uart_tx_fifo.v IP/verilog/uart_rx_fifo.v IP/verilog/rxuartlite.v IP/verilog/fifo.v IP/verilog/uart_tx.v; synth_gowin -top %TOP% -json %JSON_FILE%"
|
||||
if errorlevel 1 goto error
|
||||
|
||||
echo === Étape 2 : Placement & Routage avec nextpnr-himbaechel ===
|
||||
nextpnr-himbaechel --json %JSON_FILE% --write %PNR_JSON% --device %DEVICE% --vopt cst=constraints/%CST_FILE% --vopt family=GW2A-18C
|
||||
if errorlevel 1 goto error
|
||||
|
||||
echo === Étape 3 : Packing avec gowin_pack ===
|
||||
gowin_pack -d %DEVICE% -o %BITSTREAM% %PNR_JSON%
|
||||
if errorlevel 1 goto error
|
||||
|
||||
echo === Étape 4 : Flash avec openFPGALoader ===
|
||||
openFPGALoader -b %BOARD% %BITSTREAM%
|
||||
if errorlevel 1 goto error
|
||||
|
||||
echo === Compilation et flash réussis ===
|
||||
goto end
|
||||
|
||||
:error
|
||||
echo === Une erreur est survenue ===
|
||||
|
||||
:end
|
||||
endlocal
|
||||
pause
|
4
Semaine_7/DHT11/scripts/windows/clean.bat
Normal file
4
Semaine_7/DHT11/scripts/windows/clean.bat
Normal file
@@ -0,0 +1,4 @@
|
||||
@echo off
|
||||
echo === Nettoyage du dossier runs ===
|
||||
rd /s /q runs
|
||||
mkdir runs
|
3
Semaine_7/DHT11/scripts/windows/gtkwave.bat
Normal file
3
Semaine_7/DHT11/scripts/windows/gtkwave.bat
Normal file
@@ -0,0 +1,3 @@
|
||||
@echo off
|
||||
echo === Lancement de GTKWave ===
|
||||
gtkwave runs/sim.vcd
|
29
Semaine_7/DHT11/scripts/windows/simulate.bat
Normal file
29
Semaine_7/DHT11/scripts/windows/simulate.bat
Normal file
@@ -0,0 +1,29 @@
|
||||
@echo off
|
||||
echo === Simulation avec Icarus Verilog ===
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
:: Dossier de sortie
|
||||
set OUT=runs/sim.vvp
|
||||
|
||||
:: Top-level testbench module
|
||||
set TOP=tb_dht11
|
||||
|
||||
:: Répertoires contenant des fichiers .v
|
||||
set DIRS=src/verilog tests/verilog IP/verilog
|
||||
|
||||
:: Variable pour stocker les fichiers
|
||||
set FILES=
|
||||
|
||||
:: Boucle sur chaque dossier
|
||||
for %%D in (%DIRS%) do (
|
||||
for %%F in (%%D\*.v) do (
|
||||
set FILES=!FILES! %%F
|
||||
)
|
||||
)
|
||||
|
||||
:: Compilation avec Icarus Verilog
|
||||
iverilog -g2012 -o %OUT% -s %TOP% %FILES%
|
||||
|
||||
endlocal
|
||||
|
||||
vvp runs/sim.vvp
|
213
Semaine_7/DHT11/src/verilog/dht11_interface.v
Normal file
213
Semaine_7/DHT11/src/verilog/dht11_interface.v
Normal file
@@ -0,0 +1,213 @@
|
||||
module dht11_interface #(
|
||||
parameter CLK_FREQ = 27_000_000
|
||||
)(
|
||||
input wire i_clk,
|
||||
inout wire io_dht11_sig,
|
||||
input wire i_start,
|
||||
output reg o_dht11_data_ready,
|
||||
output reg o_busy,
|
||||
output reg [7:0] o_temp_data,
|
||||
output reg [7:0] o_hum_data,
|
||||
output reg o_dht11_error
|
||||
);
|
||||
|
||||
// === DHT11 INTERFACE ===
|
||||
|
||||
// === PARAMÈTRES ===
|
||||
localparam T_18MS = CLK_FREQ * 18 / 1_000; // cycles pour 18ms a partir
|
||||
localparam T_80US = CLK_FREQ * 81 / 1_000_000;
|
||||
localparam T_79US = CLK_FREQ * 79 / 1_000_000;
|
||||
localparam T_71US = CLK_FREQ * 71 / 1_000_000;
|
||||
localparam T_51US = CLK_FREQ * 51 / 1_000_000;
|
||||
localparam T_50US = CLK_FREQ * 50 / 1_000_000;
|
||||
localparam T_49US = CLK_FREQ * 49 / 1_000_000;
|
||||
localparam T_41US = CLK_FREQ * 41 / 1_000_000;
|
||||
localparam T_28US = CLK_FREQ * 28 / 1_000_000;
|
||||
localparam T_26US = CLK_FREQ * 26 / 1_000_000;
|
||||
localparam T_20US = CLK_FREQ * 20 / 1_000_000;
|
||||
|
||||
|
||||
// === Signal bidirectionnel ===
|
||||
reg sig_dir;
|
||||
reg sig_out;
|
||||
wire sig_in;
|
||||
|
||||
assign io_dht11_sig = sig_dir ? sig_out : 1'bz; // Si sig_dir = 1, on force la valeur de sig_out sur la ligne, sinon on laisse la ligne libre (1'bz)
|
||||
assign sig_in = io_dht11_sig;
|
||||
|
||||
// === REGISTRES ===
|
||||
reg [3:0] state;
|
||||
reg [31:0] timer;
|
||||
|
||||
reg [7:0] temp_data, hum_data;
|
||||
reg [7:0] temp_dec, hum_dec, checksum;
|
||||
reg [2:0] bit_count;
|
||||
reg [5:0] bit_index;
|
||||
reg [39:0] raw_data;
|
||||
|
||||
// === FSM ===
|
||||
localparam IDLE = 4'd0, // Pull up la ligne
|
||||
START = 4'd1, // Pull low 18ms
|
||||
WAIT_RESPONSE = 4'd2, // Release la ligne (entre 20 et 40us)
|
||||
RESPONSE_LOW = 4'd3, // DHT11 pull low 80us
|
||||
RESPONSE_HIGH = 4'd4, // DHT11 pull high 80us
|
||||
READ_BITS_LOW = 4'd5,
|
||||
READ_BITS_HIGH = 4'd6,
|
||||
DONE = 4'd7,
|
||||
ERROR = 4'd8;
|
||||
|
||||
// === INITIALISATION ===
|
||||
initial begin
|
||||
sig_dir = 0;
|
||||
sig_out = 1;
|
||||
timer = 0;
|
||||
state = IDLE;
|
||||
bit_index = 0;
|
||||
raw_data = 0;
|
||||
o_dht11_data_ready = 0;
|
||||
o_dht11_error = 0;
|
||||
end
|
||||
|
||||
// === FSM principale ===
|
||||
always @(posedge i_clk) begin
|
||||
case (state)
|
||||
|
||||
IDLE: begin
|
||||
sig_dir <= 0;
|
||||
//sig_out <= 1;
|
||||
timer <= 0;
|
||||
bit_index <= 0;
|
||||
raw_data <= 0;
|
||||
|
||||
o_busy <= 0;
|
||||
|
||||
if (i_start) begin
|
||||
sig_dir <= 1;
|
||||
sig_out <= 0;
|
||||
timer <= 0;
|
||||
o_busy <= 1;
|
||||
state <= START;
|
||||
o_dht11_data_ready <= 0;
|
||||
o_dht11_error <= 0;
|
||||
end
|
||||
end
|
||||
|
||||
START: begin
|
||||
timer <= timer + 1;
|
||||
if (timer >= T_18MS) begin
|
||||
sig_dir <= 0; // libérer la ligne
|
||||
timer <= 0;
|
||||
state <= WAIT_RESPONSE;
|
||||
end
|
||||
end
|
||||
|
||||
WAIT_RESPONSE: begin
|
||||
timer <= timer + 1;
|
||||
|
||||
if (sig_in == 0) begin
|
||||
if (timer > T_20US && timer < T_41US) begin
|
||||
|
||||
state <= RESPONSE_LOW;
|
||||
timer <= 0;
|
||||
end else begin
|
||||
state <= ERROR;
|
||||
end
|
||||
end else if (timer > T_41US) begin
|
||||
state <= ERROR;
|
||||
end
|
||||
end
|
||||
|
||||
RESPONSE_LOW: begin
|
||||
timer <= timer + 1;
|
||||
|
||||
if (sig_in == 1) begin
|
||||
if (timer > T_79US && timer < T_80US) begin
|
||||
timer <= 0;
|
||||
state <= RESPONSE_HIGH;
|
||||
end else begin
|
||||
state <= ERROR;
|
||||
end
|
||||
end else if (timer > T_80US) begin
|
||||
state <= ERROR;
|
||||
end
|
||||
end
|
||||
|
||||
RESPONSE_HIGH: begin
|
||||
timer <= timer + 1;
|
||||
|
||||
if (sig_in == 0) begin
|
||||
if (timer > T_79US && timer < T_80US) begin
|
||||
timer <= 0;
|
||||
state <= READ_BITS_LOW;
|
||||
end else begin
|
||||
state <= ERROR;
|
||||
end
|
||||
end else if (timer > T_80US) begin
|
||||
state <= ERROR;
|
||||
end
|
||||
end
|
||||
|
||||
READ_BITS_LOW: begin
|
||||
timer <= timer + 1;
|
||||
if (sig_in == 1) begin
|
||||
if (timer > T_49US && timer < T_51US) begin
|
||||
timer <= 0;
|
||||
state <= READ_BITS_HIGH;
|
||||
end else begin
|
||||
state <= ERROR;
|
||||
end
|
||||
end else if (timer > T_51US) begin
|
||||
state <= ERROR;
|
||||
end
|
||||
end
|
||||
|
||||
READ_BITS_HIGH: begin // entre 26 et 28us = 0 et ~70us = 1
|
||||
timer <= timer + 1;
|
||||
if (sig_in == 0) begin
|
||||
if (timer <= T_26US) begin
|
||||
state <= ERROR;
|
||||
end
|
||||
|
||||
raw_data <= {raw_data[38:0], (timer > T_28US)}; // 1 si high > ~28us
|
||||
timer <= 0;
|
||||
bit_index <= bit_index + 1;
|
||||
|
||||
if (bit_index == 39) begin // Code a testé ici pour etre sur de capter le dernier bit
|
||||
state <= DONE;
|
||||
end else begin
|
||||
state <= READ_BITS_LOW;
|
||||
end
|
||||
|
||||
end else if (timer > T_71US) begin
|
||||
state <= ERROR;
|
||||
end
|
||||
end
|
||||
|
||||
DONE: begin
|
||||
hum_data <= raw_data[39:32];
|
||||
hum_dec <= raw_data[31:24];
|
||||
temp_data <= raw_data[23:16];
|
||||
temp_dec <= raw_data[15:8];
|
||||
checksum <= raw_data[7:0];
|
||||
|
||||
if (raw_data[7:0] == (raw_data[39:32] + raw_data[31:24] + raw_data[23:16] + raw_data[15:8])) begin
|
||||
o_hum_data <= raw_data[39:32];
|
||||
o_temp_data <= raw_data[23:16];
|
||||
o_dht11_data_ready <= 1;
|
||||
end else begin
|
||||
o_dht11_error <= 1;
|
||||
end
|
||||
|
||||
o_busy <= 0;
|
||||
state <= IDLE;
|
||||
end
|
||||
|
||||
ERROR: begin
|
||||
o_dht11_error <= 1;
|
||||
state <= IDLE;
|
||||
end
|
||||
|
||||
endcase
|
||||
end
|
||||
|
||||
endmodule
|
174
Semaine_7/DHT11/src/verilog/dht11_model.v
Normal file
174
Semaine_7/DHT11/src/verilog/dht11_model.v
Normal file
@@ -0,0 +1,174 @@
|
||||
module dht11_model #(
|
||||
parameter CLK_FREQ = 27_000_000
|
||||
)
|
||||
(
|
||||
inout wire data, // Ligne de données bidirectionnelle
|
||||
input wire clk, // Horloge système (27 MHz)
|
||||
input wire rst_n // Reset actif bas
|
||||
);
|
||||
|
||||
// Paramètres pour les timings (basés sur une horloge de 27 MHz, période ~37 ns)
|
||||
localparam integer CLK_PERIOD_NS = (1_000_000_000 / CLK_FREQ); // 37 ns
|
||||
localparam T_START_LOW = (17_000_000 / CLK_PERIOD_NS); // 17 ms (ajusté selon votre code)
|
||||
localparam T_START_HIGH = (40_000 / CLK_PERIOD_NS); // 40 µs
|
||||
localparam T_RESP_LOW = (80_000 / CLK_PERIOD_NS); // 80 µs
|
||||
localparam T_RESP_HIGH = (80_000 / CLK_PERIOD_NS); // 80 µs
|
||||
localparam T_BIT_LOW = (50_000 / CLK_PERIOD_NS); // 50 µs pour le signal LOW de chaque bit
|
||||
localparam T_BIT0_HIGH = (27_000 / CLK_PERIOD_NS); // 26 µs pour le signal HIGH d'un bit '0'
|
||||
localparam T_BIT1_HIGH = (70_000 / CLK_PERIOD_NS); // 70 µs pour le signal HIGH d'un bit '1'
|
||||
localparam T_BIT_GAP = (50_000 / CLK_PERIOD_NS); // 50 µs entre bits
|
||||
localparam DATA_BITS = 40; // 40 bits de données
|
||||
|
||||
// États de la machine à états de Moore
|
||||
localparam IDLE = 4'd0,
|
||||
WAIT_START_LOW = 4'd1,
|
||||
WAIT_START_HIGH= 4'd2,
|
||||
RESPONSE_LOW = 4'd3,
|
||||
RESPONSE_HIGH = 4'd4,
|
||||
SEND_BIT_LOW = 4'd5,
|
||||
SEND_BIT_HIGH = 4'd6,
|
||||
END_TRANS = 4'd7,
|
||||
LAST_BIT_LOW = 4'd8;
|
||||
|
||||
// Signaux internes
|
||||
reg [3:0] state; // État actuel
|
||||
reg [31:0] counter; // Compteur pour les timings (supporte jusqu'à 20 ms)
|
||||
reg [5:0] bit_index; // Index du bit à envoyer
|
||||
reg data_out; // Valeur de sortie sur la ligne data
|
||||
reg data_oe; // Output enable (1 = sortie, 0 = haute impédance)
|
||||
wire data_in; // Valeur lue sur la ligne data
|
||||
|
||||
// Données simulées (exemple : humidité = 45.0%, température = 23.0°C)
|
||||
reg [7:0] humidity_int = 8'h2D; // 45 en décimal
|
||||
reg [7:0] humidity_dec = 8'h00; // 0
|
||||
reg [7:0] temp_int = 8'h17; // 23 en décimal
|
||||
reg [7:0] temp_dec = 8'h00; // 0
|
||||
reg [7:0] checksum; // Checksum
|
||||
reg [39:0] data_shift; // Registre pour les 40 bits de données
|
||||
|
||||
// Gestion de la ligne bidirectionnelle
|
||||
assign data = data_oe ? data_out : 1'bz;
|
||||
assign data_in = data;
|
||||
|
||||
// Calcul du checksum et préparation des données
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
checksum <= 8'h00;
|
||||
data_shift <= 40'b0;
|
||||
end else begin
|
||||
checksum <= humidity_int + humidity_dec + temp_int + temp_dec;
|
||||
data_shift <= {humidity_int, humidity_dec, temp_int, temp_dec, checksum};
|
||||
end
|
||||
end
|
||||
|
||||
// Logique séquentielle (machine à états de Moore)
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
state <= IDLE;
|
||||
counter <= 32'b0;
|
||||
bit_index <= 6'b0;
|
||||
data_out <= 1'b1;
|
||||
data_oe <= 1'b0;
|
||||
end else begin
|
||||
counter <= counter + 1; // Incrément du compteur par défaut
|
||||
// assert (bit_index < DATA_BITS) else $error("Index out of bounds: %d", bit_index);
|
||||
|
||||
case (state)
|
||||
IDLE: begin
|
||||
counter <= 32'b0;
|
||||
bit_index <= 6'b0;
|
||||
data_out <= 1'b1;
|
||||
data_oe <= 1'b0;
|
||||
if (data_in === 1'b0) begin
|
||||
state <= WAIT_START_LOW;
|
||||
end
|
||||
end
|
||||
WAIT_START_LOW: begin
|
||||
data_out <= 1'b1;
|
||||
data_oe <= 1'b0;
|
||||
if (data_in === 1'b1 && counter >= T_START_LOW) begin
|
||||
state <= WAIT_START_HIGH;
|
||||
counter <= 32'b0;
|
||||
$display("17ms");
|
||||
end else if (data_in === 1'b1) begin
|
||||
state <= IDLE; // Signal de démarrage trop court
|
||||
end
|
||||
end
|
||||
WAIT_START_HIGH: begin
|
||||
data_out <= 1'b1;
|
||||
data_oe <= 1'b0;
|
||||
if (counter >= T_START_HIGH) begin
|
||||
state <= RESPONSE_LOW;
|
||||
counter <= 32'b0;
|
||||
end
|
||||
end
|
||||
RESPONSE_LOW: begin
|
||||
data_out <= 1'b0;
|
||||
data_oe <= 1'b1;
|
||||
if (counter >= T_RESP_LOW) begin
|
||||
state <= RESPONSE_HIGH;
|
||||
counter <= 32'b0;
|
||||
end
|
||||
end
|
||||
RESPONSE_HIGH: begin
|
||||
data_out <= 1'b1;
|
||||
data_oe <= 1'b1;
|
||||
if (counter >= T_RESP_HIGH) begin
|
||||
counter <= 32'b0;
|
||||
state <= SEND_BIT_LOW;
|
||||
end
|
||||
end
|
||||
SEND_BIT_LOW: begin
|
||||
|
||||
data_out <= 1'b0;
|
||||
data_oe <= 1'b1;
|
||||
if (counter >= T_BIT_LOW) begin
|
||||
state <= SEND_BIT_HIGH;
|
||||
counter <= 32'b0;
|
||||
end
|
||||
end
|
||||
SEND_BIT_HIGH: begin
|
||||
data_out <= 1'b1;
|
||||
data_oe <= 1'b1;
|
||||
|
||||
if (counter >= (data_shift[39-bit_index] ? T_BIT1_HIGH : T_BIT0_HIGH)) begin
|
||||
counter <= 32'b0;
|
||||
bit_index <= bit_index + 1;
|
||||
if (bit_index + 1 < DATA_BITS) begin
|
||||
state <= SEND_BIT_LOW;
|
||||
|
||||
end else begin
|
||||
state <= LAST_BIT_LOW;
|
||||
bit_index <= 6'b0;
|
||||
counter <= 32'b0;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
LAST_BIT_LOW: begin
|
||||
data_out <= 1'b0;
|
||||
data_oe <= 1'b1;
|
||||
if (counter >= T_BIT_LOW) begin
|
||||
state <= END_TRANS;
|
||||
counter <= 32'b0;
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
END_TRANS: begin
|
||||
data_out <= 1'b1;
|
||||
data_oe <= 1'b0;
|
||||
counter <= 32'b0;
|
||||
state <= IDLE;
|
||||
end
|
||||
default: begin
|
||||
state <= IDLE;
|
||||
counter <= 32'b0;
|
||||
data_out <= 1'b1;
|
||||
data_oe <= 1'b0;
|
||||
end
|
||||
endcase
|
||||
end
|
||||
end
|
||||
|
||||
endmodule
|
65
Semaine_7/DHT11/tests/verilog/tb_dht11.v
Normal file
65
Semaine_7/DHT11/tests/verilog/tb_dht11.v
Normal file
@@ -0,0 +1,65 @@
|
||||
`timescale 1ns/1ps
|
||||
|
||||
module tb_dht11;
|
||||
|
||||
reg clk = 0;
|
||||
always #18.5 clk = ~clk; // Génère une clock 27 MHz
|
||||
|
||||
// === Registres ===
|
||||
wire io_dht11_sig;
|
||||
reg dht11_start;
|
||||
wire dht11_data_ready;
|
||||
wire dht11_busy;
|
||||
wire [7:0] dht11_temp_data;
|
||||
wire [7:0] dht11_hum_data;
|
||||
wire dht11_error;
|
||||
|
||||
// === Simulation du module DHT11 ===
|
||||
dht11_model dht11_model (
|
||||
.data(io_dht11_sig),
|
||||
.clk(clk),
|
||||
.rst_n(1'b1) // Reset non utilisé dans ce test
|
||||
);
|
||||
|
||||
// === Module DHT11 INTERFACE ===
|
||||
dht11_interface dht11_interface (
|
||||
.i_clk(clk),
|
||||
.io_dht11_sig(io_dht11_sig),
|
||||
.i_start(dht11_start),
|
||||
.o_dht11_data_ready(dht11_data_ready),
|
||||
.o_busy(dht11_busy),
|
||||
.o_temp_data(dht11_temp_data),
|
||||
.o_hum_data(dht11_hum_data),
|
||||
.o_dht11_error(dht11_error)
|
||||
);
|
||||
|
||||
pullup(io_dht11_sig);
|
||||
|
||||
// === TEST SEQUENCE ===
|
||||
initial begin
|
||||
$dumpfile("runs/sim.vcd");
|
||||
$dumpvars(0, tb_dht11);
|
||||
dht11_start = 0;
|
||||
|
||||
$display("==== Start DHT11 Test ====");
|
||||
|
||||
#100;
|
||||
dht11_start = 1; // Démarre la lecture des données
|
||||
$display("DHT11 start...");
|
||||
|
||||
wait(dht11_busy); // Attend que le module soit occupé
|
||||
$display("DHT11 busy...");
|
||||
|
||||
dht11_start = 0;
|
||||
|
||||
wait(dht11_data_ready); // Attend que les données soient prêtes
|
||||
$display("DHT11 data ready...");
|
||||
|
||||
$display("Température : %d.%d °C", dht11_temp_data);
|
||||
$display("Humidité : %d.%d %%", dht11_hum_data);
|
||||
|
||||
$display("==== End DHT11 Test ====");
|
||||
$finish;
|
||||
end
|
||||
|
||||
endmodule
|
Reference in New Issue
Block a user