forked from tanchou/Verilog
uart v3
This commit is contained in:
@@ -0,0 +1,29 @@
|
|||||||
|
module distance_display_led (
|
||||||
|
input wire [8:0] distance,
|
||||||
|
output reg [5:0] leds
|
||||||
|
);
|
||||||
|
|
||||||
|
// Constante
|
||||||
|
parameter MIN_DIST = 2;
|
||||||
|
parameter MAX_DIST = 349;
|
||||||
|
parameter LEVELS = 5;
|
||||||
|
parameter PART_SIZE = (MAX_DIST - MIN_DIST + 1) / LEVELS;
|
||||||
|
|
||||||
|
always @(*) begin
|
||||||
|
if (distance <= MIN_DIST + PART_SIZE*0)
|
||||||
|
leds = 6'b111111;
|
||||||
|
else if (distance <= MIN_DIST + PART_SIZE*1)
|
||||||
|
leds = 6'b111110;
|
||||||
|
else if (distance <= MIN_DIST + PART_SIZE*2)
|
||||||
|
leds = 6'b111100;
|
||||||
|
else if (distance <= MIN_DIST + PART_SIZE*3)
|
||||||
|
leds = 6'b111000;
|
||||||
|
else if (distance <= MIN_DIST + PART_SIZE*4)
|
||||||
|
leds = 6'b110000;
|
||||||
|
else if (distance <= MIN_DIST + PART_SIZE*5)
|
||||||
|
leds = 6'b100000;
|
||||||
|
else
|
||||||
|
leds = 6'b000000;
|
||||||
|
end
|
||||||
|
|
||||||
|
endmodule
|
||||||
20561
Semaine_3/Capteur_recule_bidirectionel_V2/pnr_top_ultrasonic_led.json
Normal file
20561
Semaine_3/Capteur_recule_bidirectionel_V2/pnr_top_ultrasonic_led.json
Normal file
File diff suppressed because one or more lines are too long
@@ -0,0 +1,29 @@
|
|||||||
|
//Copyright (C)2014-2025 Gowin Semiconductor Corporation.
|
||||||
|
//All rights reserved.
|
||||||
|
//File Title: Physical Constraints file
|
||||||
|
//Tool Version: V1.9.11.01 Education (64-bit)
|
||||||
|
//Part Number: GW2AR-LV18QN88C8/I7
|
||||||
|
//Device: GW2AR-18
|
||||||
|
//Device Version: C
|
||||||
|
//Created Time: Mon 04 28 14:13:08 2025
|
||||||
|
|
||||||
|
IO_LOC "ws2812_dout" 79;
|
||||||
|
IO_PORT "ws2812_dout" IO_TYPE=LVCMOS18 PULL_MODE=UP DRIVE=8 BANK_VCCIO=1.8;
|
||||||
|
IO_LOC "leds[5]" 20;
|
||||||
|
IO_PORT "leds[5]" IO_TYPE=LVCMOS33 PULL_MODE=UP DRIVE=8 BANK_VCCIO=3.3;
|
||||||
|
IO_LOC "leds[4]" 19;
|
||||||
|
IO_PORT "leds[4]" IO_TYPE=LVCMOS33 PULL_MODE=UP DRIVE=8 BANK_VCCIO=3.3;
|
||||||
|
IO_LOC "leds[3]" 18;
|
||||||
|
IO_PORT "leds[3]" IO_TYPE=LVCMOS33 PULL_MODE=UP DRIVE=8 BANK_VCCIO=3.3;
|
||||||
|
IO_LOC "leds[2]" 17;
|
||||||
|
IO_PORT "leds[2]" IO_TYPE=LVCMOS33 PULL_MODE=UP DRIVE=8 BANK_VCCIO=3.3;
|
||||||
|
IO_LOC "leds[1]" 16;
|
||||||
|
IO_PORT "leds[1]" IO_TYPE=LVCMOS33 PULL_MODE=UP DRIVE=8 BANK_VCCIO=3.3;
|
||||||
|
IO_LOC "leds[0]" 15;
|
||||||
|
IO_PORT "leds[0]" IO_TYPE=LVCMOS33 PULL_MODE=UP DRIVE=8 BANK_VCCIO=3.3;
|
||||||
|
IO_LOC "sig" 73;
|
||||||
|
IO_PORT "sig" IO_TYPE=LVCMOS33 PULL_MODE=UP DRIVE=8 BANK_VCCIO=3.3;
|
||||||
|
IO_LOC "start" 88;
|
||||||
|
IO_PORT "start" 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;
|
||||||
1358
Semaine_3/Capteur_recule_bidirectionel_V2/top_ultrasonic_led.fs
Normal file
1358
Semaine_3/Capteur_recule_bidirectionel_V2/top_ultrasonic_led.fs
Normal file
File diff suppressed because one or more lines are too long
34178
Semaine_3/Capteur_recule_bidirectionel_V2/top_ultrasonic_led.json
Normal file
34178
Semaine_3/Capteur_recule_bidirectionel_V2/top_ultrasonic_led.json
Normal file
File diff suppressed because it is too large
Load Diff
142
Semaine_3/Capteur_recule_bidirectionel_V2/ultrasonic_fpga.v
Normal file
142
Semaine_3/Capteur_recule_bidirectionel_V2/ultrasonic_fpga.v
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
module ultrasonic_fpga #(
|
||||||
|
parameter integer CLK_FREQ = 27_000_000 // Fréquence d'horloge en Hz
|
||||||
|
)(
|
||||||
|
input wire clk,
|
||||||
|
input wire start,
|
||||||
|
inout wire sig, // Broche bidirectionnelle vers le capteur
|
||||||
|
output reg [15:0] distance, // Distance mesurée en cm
|
||||||
|
output reg [2:0] state
|
||||||
|
);
|
||||||
|
reg [15:0] trig_counter = 0;
|
||||||
|
reg [31:0] echo_counter = 0;
|
||||||
|
reg [31:0] echo_div_counter = 0;
|
||||||
|
reg [15:0] distance_counter = 0;
|
||||||
|
|
||||||
|
reg sig_out;
|
||||||
|
reg sig_dir; // 1: output, 0: input
|
||||||
|
|
||||||
|
assign sig = sig_dir ? sig_out : 1'bz; // bz pour dire que le fpga laisse le fils libre et n'oblige pas de valeur
|
||||||
|
|
||||||
|
reg sig_int, sig_ok;
|
||||||
|
|
||||||
|
localparam IDLE = 3'd0,
|
||||||
|
TRIG_HIGH = 3'd1,
|
||||||
|
TRIG_LOW = 3'd2,
|
||||||
|
WAIT_ECHO = 3'd3,
|
||||||
|
MEASURE_ECHO = 3'd4,
|
||||||
|
COMPUTE = 3'd5,
|
||||||
|
DONE = 3'd6,
|
||||||
|
WAIT_NEXT = 3'd7;
|
||||||
|
|
||||||
|
localparam integer TRIG_PULSE_CYCLES = CLK_FREQ / 100_000; // 10us pulse
|
||||||
|
localparam integer DIST_DIVISOR = (58 * CLK_FREQ) / 1_000_000; // pour conversion us -> cm
|
||||||
|
localparam integer MAX_CM = 350;
|
||||||
|
localparam integer TIMEOUT_CYCLES = (MAX_CM * 58 * CLK_FREQ) / 1000000;
|
||||||
|
|
||||||
|
localparam WAIT_NEXT_CYCLES = (CLK_FREQ / 1000) * 100; // 60 ms
|
||||||
|
|
||||||
|
reg [31:0] wait_counter;
|
||||||
|
|
||||||
|
always @(posedge clk) begin
|
||||||
|
sig_int <= sig;
|
||||||
|
sig_ok <= sig_int;
|
||||||
|
end
|
||||||
|
|
||||||
|
always @(posedge clk) begin // FSM
|
||||||
|
|
||||||
|
case (state)
|
||||||
|
IDLE: begin
|
||||||
|
sig_out <= 0;
|
||||||
|
sig_dir <= 0;
|
||||||
|
distance <= 0;
|
||||||
|
if (start) begin
|
||||||
|
state <= TRIG_HIGH;
|
||||||
|
trig_counter <= 0;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
TRIG_HIGH: begin
|
||||||
|
sig_out <= 1;
|
||||||
|
sig_dir <= 1;
|
||||||
|
if (trig_counter < TRIG_PULSE_CYCLES) begin
|
||||||
|
trig_counter <= trig_counter + 1;
|
||||||
|
end else begin
|
||||||
|
trig_counter <= 0;
|
||||||
|
state <= TRIG_LOW;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
TRIG_LOW: begin
|
||||||
|
sig_out <= 0;
|
||||||
|
sig_dir <= 0; // Mettre en entrée
|
||||||
|
|
||||||
|
if (sig_ok) begin
|
||||||
|
state <= TRIG_LOW;
|
||||||
|
end else
|
||||||
|
state <= WAIT_ECHO;
|
||||||
|
end
|
||||||
|
|
||||||
|
WAIT_ECHO: begin
|
||||||
|
if (sig_ok) begin
|
||||||
|
echo_counter <= 0;
|
||||||
|
state <= MEASURE_ECHO;
|
||||||
|
end else if (echo_counter >= TIMEOUT_CYCLES) begin
|
||||||
|
distance <= 0;
|
||||||
|
state <= DONE;
|
||||||
|
end else begin
|
||||||
|
echo_counter <= echo_counter + 1;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
MEASURE_ECHO: begin
|
||||||
|
if (sig_ok) begin
|
||||||
|
if (echo_counter < TIMEOUT_CYCLES) begin
|
||||||
|
echo_counter <= echo_counter + 1;
|
||||||
|
end else begin
|
||||||
|
state <= DONE;
|
||||||
|
end
|
||||||
|
|
||||||
|
end else begin
|
||||||
|
state <= COMPUTE;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
COMPUTE: begin
|
||||||
|
if (echo_counter >= DIST_DIVISOR) begin
|
||||||
|
echo_counter <= echo_counter - DIST_DIVISOR;
|
||||||
|
distance_counter <= distance_counter + 1;
|
||||||
|
state <= COMPUTE;
|
||||||
|
end else begin
|
||||||
|
distance <= distance_counter;
|
||||||
|
state <= DONE;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
DONE: begin
|
||||||
|
if (start) begin
|
||||||
|
wait_counter <= 0;
|
||||||
|
state <= WAIT_NEXT;
|
||||||
|
end else begin
|
||||||
|
state <= IDLE;
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
WAIT_NEXT: begin
|
||||||
|
wait_counter <= wait_counter + 1;
|
||||||
|
if (wait_counter >= WAIT_NEXT_CYCLES) begin
|
||||||
|
state <= TRIG_HIGH;
|
||||||
|
trig_counter <= 0;
|
||||||
|
distance_counter <= 0;
|
||||||
|
echo_counter <= 0;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
default: begin
|
||||||
|
state <= IDLE; // Reset to IDLE state in case of an error
|
||||||
|
end
|
||||||
|
endcase
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
endmodule
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
# Projet FPGA (Tang Nano 20K) + ESP32
|
|
||||||
|
|
||||||
## Objectif global
|
|
||||||
|
|
||||||
Le but est de pouvoir se connecter à l’ESP32 via Wi-Fi, et de communiquer avec un PC (ou autre appareil USB connecté au FPGA).
|
|
||||||
L’ESP32 agit comme **esclave** pour le FPGA et sert uniquement de **portail Wi-Fi**.
|
|
||||||
Le FPGA fait le lien entre les appareils Wi-Fi et le périphérique USB.
|
|
||||||
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Rôles des composants
|
|
||||||
|
|
||||||
### FPGA (Tang Nano 20K)
|
|
||||||
- Gère l’interface UART avec l’ESP32
|
|
||||||
- Gère la communication USB avec le PC
|
|
||||||
- Fait le routage bidirectionnel des données (mux / buffer intelligent)
|
|
||||||
|
|
||||||
### ESP32
|
|
||||||
- Crée un réseau Wi-Fi local
|
|
||||||
- Écoute via une connexion UART avec le FPGA
|
|
||||||
- Reçoit les commandes du FPGA et envoie les données des clients Wi-Fi
|
|
||||||
|
|
||||||
### PC (ou autre appareil USB)
|
|
||||||
- Envoie et reçoit des données (via terminal série ou logiciel personnalisé)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Architecture
|
|
||||||
|
|
||||||
```
|
|
||||||
[ PC via USB ]
|
|
||||||
│
|
|
||||||
┌───────▼────────┐
|
|
||||||
│ uart_usb │ <— UART avec le PC
|
|
||||||
└──────┬─────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌──────────────┐
|
|
||||||
│ uart_core │ <— Routeur/contrôleur central
|
|
||||||
└────┬────┬────┘
|
|
||||||
│ │
|
|
||||||
┌─────────────┘ └────────────┐
|
|
||||||
▼ ▼
|
|
||||||
[uart_wifi] [user_logic] (LEDs)
|
|
||||||
<— UART avec ESP32 (comporte les modules fonctionnels)
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Détails des modules
|
|
||||||
|
|
||||||
### `uart_usb`
|
|
||||||
- Interface UART vers le PC (via USB-UART)
|
|
||||||
- Peut utiliser un convertisseur USB-UART via `uart_rx_pc` / `uart_tx_pc`
|
|
||||||
- Fournit :
|
|
||||||
- `rx_data`, `rx_valid`, `rx_ready`
|
|
||||||
- `tx_data`, `tx_valid`, `tx_ready`
|
|
||||||
|
|
||||||
### `uart_wifi`
|
|
||||||
- Interface UART avec l’ESP32
|
|
||||||
- Même interface que `uart_usb`, mais avec `uart_rx_esp` / `uart_tx_esp`
|
|
||||||
- Sert à la communication Wi-Fi
|
|
||||||
|
|
||||||
### `uart_core`
|
|
||||||
- Module central de routage UART
|
|
||||||
- Gère la logique de communication :
|
|
||||||
- Lecture des commandes depuis le PC → envoie à l’ESP32
|
|
||||||
- Réception de réponse de l’ESP32 → envoie au PC
|
|
||||||
- Peut être codé comme une FSM maître ou un router simple
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Signaux principaux
|
|
||||||
|
|
||||||
| Signal | Description |
|
|
||||||
|------------------------|------------------------------------------------------|
|
|
||||||
| `uart_rx_pc` | UART RX depuis le PC |
|
|
||||||
| `uart_tx_pc` | UART TX vers le PC |
|
|
||||||
| `uart_rx_esp` | UART RX depuis l’ESP32 |
|
|
||||||
| `uart_tx_esp` | UART TX vers l’ESP32 |
|
|
||||||
| `fifo_rx_pc_to_esp` | Données du PC à transférer à l’ESP32 |
|
|
||||||
| `fifo_rx_esp_to_pc` | Données de l’ESP32 à transférer au PC |
|
|
||||||
| `link_manager` | Logique de contrôle des transferts entre buffers/UART|
|
|
||||||
| `status_led` | Gestion des LEDs de statut |
|
|
||||||
@@ -1,233 +0,0 @@
|
|||||||
#include <WiFi.h>
|
|
||||||
#include <WebServer.h>
|
|
||||||
#include "esp_wifi.h"
|
|
||||||
|
|
||||||
const char* ssid = "ESP32-Louis";
|
|
||||||
const char* password = "motdepasse";
|
|
||||||
|
|
||||||
WebServer server(80);
|
|
||||||
|
|
||||||
void handleRoot() {
|
|
||||||
digitalWrite(2,HIGH);
|
|
||||||
wifi_sta_list_t sta_list;
|
|
||||||
esp_wifi_ap_get_sta_list(&sta_list);
|
|
||||||
|
|
||||||
String page = "";
|
|
||||||
page += "<!DOCTYPE html>";
|
|
||||||
page += "<html>";
|
|
||||||
page += "<head>";
|
|
||||||
page += "<title>ESP32</title>";
|
|
||||||
page += "<meta charset=\"UTF-8\">";
|
|
||||||
page += "</head>";
|
|
||||||
page += "<body>";
|
|
||||||
page += "<h1>Appareils connectés à l'ESP32</h1>";
|
|
||||||
page += "<ul>";
|
|
||||||
|
|
||||||
page += "<ul>";
|
|
||||||
|
|
||||||
for (int i = 0; i < sta_list.num; i++) {
|
|
||||||
const wifi_sta_info_t& client = sta_list.sta[i];
|
|
||||||
char macStr[18];
|
|
||||||
snprintf(macStr, sizeof(macStr),
|
|
||||||
"%02X:%02X:%02X:%02X:%02X:%02X",
|
|
||||||
client.mac[0], client.mac[1], client.mac[2],
|
|
||||||
client.mac[3], client.mac[4], client.mac[5]);
|
|
||||||
page += "<li>MAC : ";
|
|
||||||
page += macStr;
|
|
||||||
page += "</li>";
|
|
||||||
}
|
|
||||||
|
|
||||||
page += "</ul>";
|
|
||||||
page += "<p>Nombre total : " + String(sta_list.num) + "</p>";
|
|
||||||
|
|
||||||
page += "</body></html>";
|
|
||||||
|
|
||||||
server.send(200, "text/html", page);
|
|
||||||
digitalWrite(2,LOW);
|
|
||||||
}
|
|
||||||
|
|
||||||
void onClientConnected(WiFiEvent_t event, WiFiEventInfo_t info) {
|
|
||||||
digitalWrite(2, HIGH);
|
|
||||||
|
|
||||||
const wifi_event_ap_staconnected_t* conn = reinterpret_cast<const wifi_event_ap_staconnected_t*>(&info);
|
|
||||||
|
|
||||||
|
|
||||||
byte packet[11]; // 2 header + 1 code + 6 MAC + 1 fin
|
|
||||||
packet[0] = 0x02;
|
|
||||||
packet[1] = 0x02;
|
|
||||||
packet[2] = 0x01;
|
|
||||||
|
|
||||||
// Copier l'adr MAC dans le tableau
|
|
||||||
memcpy(&packet[3], conn->mac, 6);
|
|
||||||
|
|
||||||
packet[9] = 0x1B; // marqueur avant fin
|
|
||||||
packet[10] = 0x03; // fin de trame
|
|
||||||
|
|
||||||
Serial.write(packet, sizeof(packet));
|
|
||||||
digitalWrite(2, LOW);
|
|
||||||
}
|
|
||||||
|
|
||||||
void onClientDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
|
|
||||||
digitalWrite(2, HIGH);
|
|
||||||
|
|
||||||
const wifi_event_ap_stadisconnected_t* disc = reinterpret_cast<const wifi_event_ap_stadisconnected_t*>(&info);
|
|
||||||
|
|
||||||
byte packet[12];
|
|
||||||
packet[0] = 0x02;
|
|
||||||
packet[1] = 0x02; // OP_CODE: Connection Update
|
|
||||||
packet[2] = 0x00; // Disconnected
|
|
||||||
|
|
||||||
memcpy(&packet[3], disc->mac, 6); // MAC address
|
|
||||||
|
|
||||||
packet[9] = 0x1B; // marqueur avant fin
|
|
||||||
packet[10] = 0x03; // fin de trame
|
|
||||||
packet[11] = '\n'; // (optionnel, pour debug dans terminal série)
|
|
||||||
|
|
||||||
Serial.write(packet, 11); // <= envoie bien 11 octets, pas 12 (on ne compte pas le \n ici si tu veux l’ignorer)
|
|
||||||
digitalWrite(2, LOW);
|
|
||||||
}
|
|
||||||
|
|
||||||
#define BUFFER_SIZE 64
|
|
||||||
uint8_t rxBuffer[BUFFER_SIZE];
|
|
||||||
uint8_t rxIndex = 0;
|
|
||||||
bool inFrame = false;
|
|
||||||
|
|
||||||
void processCommand(uint8_t* data, int length) {
|
|
||||||
// Vérifie la validité de la trame
|
|
||||||
if (length < 4 || data[0] != 0x02 || data[length - 2] != 0x1B || data[length - 1] != 0x03) {
|
|
||||||
byte packet[] = {0x02, 0x00, 0x03, 0x1B, 0x03}; // Erreur : Trame invalide
|
|
||||||
Serial.write(packet, sizeof(packet));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t type = data[1];
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
|
|
||||||
case 0x01: { // Wi-Fi State
|
|
||||||
bool wifiUp = WiFi.status() == WL_CONNECTED;
|
|
||||||
byte packet[] = {0x02, 0x01, wifiUp ? 0x01 : 0x00, 0x1B, 0x03};
|
|
||||||
Serial.write(packet, sizeof(packet));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case 0x03: { // Request Connected Devices
|
|
||||||
wifi_sta_list_t staList;
|
|
||||||
if (esp_wifi_ap_get_sta_list(&staList) == ESP_OK) {
|
|
||||||
byte packet[3 + 1 + 6 * 10 + 2]; // max 10 clients
|
|
||||||
uint8_t index = 0;
|
|
||||||
packet[index++] = 0x02;
|
|
||||||
packet[index++] = 0x04;
|
|
||||||
packet[index++] = staList.num; // LEN
|
|
||||||
|
|
||||||
for (int i = 0; i < staList.num; i++) {
|
|
||||||
memcpy(&packet[index], staList.sta[i].mac, 6);
|
|
||||||
index += 6;
|
|
||||||
}
|
|
||||||
|
|
||||||
packet[index++] = 0x1B;
|
|
||||||
packet[index++] = 0x03;
|
|
||||||
|
|
||||||
Serial.write(packet, index);
|
|
||||||
} else {
|
|
||||||
byte error[] = {0x02, 0x00, 0x02, 0x1B, 0x03}; // Erreur : args
|
|
||||||
Serial.write(error, sizeof(error));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case 0x05: { // Send Message
|
|
||||||
if (length < 12) {
|
|
||||||
byte packet[] = {0x02, 0x00, 0x02, 0x1B, 0x03}; // args error
|
|
||||||
Serial.write(packet, sizeof(packet));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t* mac = &data[2];
|
|
||||||
uint8_t msgLen = data[8];
|
|
||||||
|
|
||||||
if (length != 9 + msgLen + 2) {
|
|
||||||
byte packet[] = {0x02, 0x00, 0x04, 0x1B, 0x03}; // too long trame
|
|
||||||
Serial.write(packet, sizeof(packet));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ici tu pourrais ajouter une logique pour router le message au bon appareil (plus tard)
|
|
||||||
// ACK possible :
|
|
||||||
byte ack[] = {0x02, 0x04, 0x01, 0x1B, 0x03}; // ACK
|
|
||||||
Serial.write(ack, sizeof(ack));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
default: {
|
|
||||||
byte packet[] = {0x02, 0x00, 0x01, 0x1B, 0x03}; // Erreur : commande inconnue
|
|
||||||
Serial.write(packet, sizeof(packet));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(115200);
|
|
||||||
if (!WiFi.softAP(ssid, password)) {
|
|
||||||
byte packet[] = {0x02, 0x01, 0x00, 0x03};
|
|
||||||
Serial.write(packet, sizeof(packet));
|
|
||||||
}
|
|
||||||
|
|
||||||
WiFi.onEvent(onClientConnected, ARDUINO_EVENT_WIFI_AP_STACONNECTED);
|
|
||||||
WiFi.onEvent(onClientDisconnected, ARDUINO_EVENT_WIFI_AP_STADISCONNECTED);
|
|
||||||
|
|
||||||
delay(1000); // Donne un peu de temps pour démarrer le WiFi
|
|
||||||
|
|
||||||
server.on("/", handleRoot);
|
|
||||||
server.begin();
|
|
||||||
|
|
||||||
pinMode(2, OUTPUT);
|
|
||||||
|
|
||||||
byte packet[] = {0x02, 0x01, 0x01, 0x03};
|
|
||||||
Serial.write(packet, sizeof(packet));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
server.handleClient();
|
|
||||||
|
|
||||||
bool escaping = false;
|
|
||||||
|
|
||||||
while (Serial.available()) {
|
|
||||||
uint8_t b = Serial.read();
|
|
||||||
|
|
||||||
if (!inFrame) {
|
|
||||||
if (b == 0x02) {
|
|
||||||
inFrame = true;
|
|
||||||
rxIndex = 0;
|
|
||||||
rxBuffer[rxIndex++] = b;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (escaping) {
|
|
||||||
if (rxIndex < BUFFER_SIZE) {
|
|
||||||
rxBuffer[rxIndex++] = b;
|
|
||||||
}
|
|
||||||
escaping = false;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (b == 0x1B) {
|
|
||||||
escaping = true;
|
|
||||||
} else if (b == 0x03) {
|
|
||||||
rxBuffer[rxIndex++] = b;
|
|
||||||
processCommand(rxBuffer, rxIndex);
|
|
||||||
inFrame = false;
|
|
||||||
rxIndex = 0;
|
|
||||||
} else {
|
|
||||||
if (rxIndex < BUFFER_SIZE) {
|
|
||||||
rxBuffer[rxIndex++] = b;
|
|
||||||
} else {
|
|
||||||
inFrame = false;
|
|
||||||
rxIndex = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
import serial
|
|
||||||
|
|
||||||
def main():
|
|
||||||
try:
|
|
||||||
with serial.Serial('COM5', 115200, timeout=1) as ser:
|
|
||||||
buffer = []
|
|
||||||
while True:
|
|
||||||
byte = ser.read(1)
|
|
||||||
if byte:
|
|
||||||
value = byte[0]
|
|
||||||
buffer.append(value)
|
|
||||||
print(f"Reçu: {value:#04x}")
|
|
||||||
|
|
||||||
# Vérifie début de trame
|
|
||||||
if len(buffer) == 1 and buffer[0] != 0x02:
|
|
||||||
buffer.clear()
|
|
||||||
|
|
||||||
# Vérifie fin de trame
|
|
||||||
if len(buffer) >= 3 and buffer[-2] == 0x1B and buffer[-1] == 0x03:
|
|
||||||
print("\n=== Trame complète reçue ===")
|
|
||||||
print("Trame :", ' '.join(f"{b:#04x}" for b in buffer))
|
|
||||||
print("=============================\n")
|
|
||||||
buffer.clear()
|
|
||||||
except serial.SerialException as e:
|
|
||||||
print("Erreur de port série :", e)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
import serial
|
|
||||||
|
|
||||||
def read_frame(ser):
|
|
||||||
frame = []
|
|
||||||
in_frame = False
|
|
||||||
|
|
||||||
while True:
|
|
||||||
byte = ser.read()
|
|
||||||
if not byte:
|
|
||||||
continue
|
|
||||||
b = byte[0]
|
|
||||||
if not in_frame:
|
|
||||||
if b == 0x02:
|
|
||||||
frame = [b]
|
|
||||||
in_frame = True
|
|
||||||
else:
|
|
||||||
frame.append(b)
|
|
||||||
if len(frame) >= 2 and frame[-2] == 0x1B and frame[-1] == 0x03:
|
|
||||||
return frame
|
|
||||||
|
|
||||||
def interpret_frame(frame):
|
|
||||||
if len(frame) < 4 or frame[0] != 0x02 or frame[-2:] != [0x1B, 0x03]:
|
|
||||||
return "Trame invalide"
|
|
||||||
|
|
||||||
op_code = frame[1]
|
|
||||||
if op_code == 0x00:
|
|
||||||
return f"[Erreur] Code: {hex(frame[2])}"
|
|
||||||
elif op_code == 0x01:
|
|
||||||
state = frame[2]
|
|
||||||
return f"[Wi-Fi] {'UP' if state == 1 else 'DOWN'}"
|
|
||||||
elif op_code == 0x02:
|
|
||||||
status = 'Connecté' if frame[2] == 0x01 else 'Déconnecté'
|
|
||||||
mac = ':'.join(f"{b:02X}" for b in frame[3:9])
|
|
||||||
return f"[Connexion] {status} - {mac}"
|
|
||||||
elif op_code == 0x03:
|
|
||||||
return "[Demande appareils connectés]"
|
|
||||||
elif op_code == 0x04:
|
|
||||||
mac = ':'.join(f"{b:02X}" for b in frame[2:8])
|
|
||||||
length = frame[8]
|
|
||||||
msg = bytes(frame[9:9+length]).decode(errors='ignore')
|
|
||||||
return f"[Message] à {mac} : {msg}"
|
|
||||||
else:
|
|
||||||
return f"[OpCode inconnu] {hex(op_code)}"
|
|
||||||
|
|
||||||
def main():
|
|
||||||
with serial.Serial('COM5', 115200, timeout=1) as ser:
|
|
||||||
print("Lecture des trames...")
|
|
||||||
while True:
|
|
||||||
frame = read_frame(ser)
|
|
||||||
info = interpret_frame(frame)
|
|
||||||
print(info)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
import serial
|
|
||||||
import time
|
|
||||||
|
|
||||||
START_BYTE = 0x02
|
|
||||||
END_BYTES = [0x1B, 0x03]
|
|
||||||
|
|
||||||
def build_command(opcode, payload=b''):
|
|
||||||
frame = bytearray()
|
|
||||||
frame.append(START_BYTE)
|
|
||||||
frame.append(opcode)
|
|
||||||
frame.extend(payload)
|
|
||||||
frame.extend(END_BYTES)
|
|
||||||
return frame
|
|
||||||
|
|
||||||
def send_command(ser, opcode, payload=b''):
|
|
||||||
frame = build_command(opcode, payload)
|
|
||||||
print(f"Envoi : {[hex(b) for b in frame]}")
|
|
||||||
ser.write(frame)
|
|
||||||
|
|
||||||
def main():
|
|
||||||
port = 'COM5'
|
|
||||||
baud = 115200
|
|
||||||
|
|
||||||
try:
|
|
||||||
with serial.Serial(port, baud, timeout=2) as ser:
|
|
||||||
while True:
|
|
||||||
print("\nCommandes disponibles :")
|
|
||||||
print("1. État du Wi-Fi")
|
|
||||||
print("2. Liste des clients connectés")
|
|
||||||
print("3. Envoyer un message")
|
|
||||||
print("4. Quitter")
|
|
||||||
|
|
||||||
choix = input("Choix (1-4) : ")
|
|
||||||
|
|
||||||
if choix == "1":
|
|
||||||
send_command(ser, 0x01)
|
|
||||||
elif choix == "2":
|
|
||||||
send_command(ser, 0x03)
|
|
||||||
elif choix == "3":
|
|
||||||
msg = input("Message à envoyer : ")
|
|
||||||
msg_bytes = msg.encode('utf-8')
|
|
||||||
send_command(ser, 0x05, msg_bytes)
|
|
||||||
elif choix == "4":
|
|
||||||
print("Fermeture.")
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
print("Choix invalide.")
|
|
||||||
|
|
||||||
time.sleep(0.5)
|
|
||||||
|
|
||||||
print("Réponse reçue :")
|
|
||||||
while ser.in_waiting:
|
|
||||||
byte = ser.read(1)
|
|
||||||
print(f"Reçu : 0x{byte[0]:02X}")
|
|
||||||
|
|
||||||
except serial.SerialException as e:
|
|
||||||
print(f"Erreur de port série : {e}")
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
@@ -1,106 +0,0 @@
|
|||||||
#include <WiFi.h>
|
|
||||||
#include <WebServer.h>
|
|
||||||
#include "esp_wifi.h"
|
|
||||||
|
|
||||||
const char* ssid = "ESP32-Louis";
|
|
||||||
const char* password = "motdepasse";
|
|
||||||
|
|
||||||
WebServer server(80);
|
|
||||||
|
|
||||||
void handleRoot() {
|
|
||||||
digitalWrite(2,HIGH);
|
|
||||||
wifi_sta_list_t sta_list;
|
|
||||||
esp_wifi_ap_get_sta_list(&sta_list);
|
|
||||||
|
|
||||||
String page = "";
|
|
||||||
page += "<!DOCTYPE html>";
|
|
||||||
page += "<html>";
|
|
||||||
page += "<head>";
|
|
||||||
page += "<title>ESP32</title>";
|
|
||||||
page += "<meta charset=\"UTF-8\">";
|
|
||||||
page += "</head>";
|
|
||||||
page += "<body>";
|
|
||||||
page += "<h1>Appareils connectés à l'ESP32</h1>";
|
|
||||||
page += "<ul>";
|
|
||||||
|
|
||||||
page += "<ul>";
|
|
||||||
|
|
||||||
for (int i = 0; i < sta_list.num; i++) {
|
|
||||||
const wifi_sta_info_t& client = sta_list.sta[i];
|
|
||||||
char macStr[18];
|
|
||||||
snprintf(macStr, sizeof(macStr),
|
|
||||||
"%02X:%02X:%02X:%02X:%02X:%02X",
|
|
||||||
client.mac[0], client.mac[1], client.mac[2],
|
|
||||||
client.mac[3], client.mac[4], client.mac[5]);
|
|
||||||
page += "<li>MAC : ";
|
|
||||||
page += macStr;
|
|
||||||
page += "</li>";
|
|
||||||
}
|
|
||||||
|
|
||||||
page += "</ul>";
|
|
||||||
page += "<p>Nombre total : " + String(sta_list.num) + "</p>";
|
|
||||||
|
|
||||||
page += "</body></html>";
|
|
||||||
|
|
||||||
server.send(200, "text/html", page);
|
|
||||||
digitalWrite(2,LOW);
|
|
||||||
}
|
|
||||||
|
|
||||||
void onClientConnected(WiFiEvent_t event, WiFiEventInfo_t info) {
|
|
||||||
digitalWrite(2,HIGH);
|
|
||||||
|
|
||||||
wifi_sta_list_t sta_list;
|
|
||||||
esp_wifi_ap_get_sta_list(&sta_list);
|
|
||||||
|
|
||||||
for (int i = 0; i < sta_list.num; i++) {
|
|
||||||
const wifi_sta_info_t& client = sta_list.sta[i];
|
|
||||||
char macStr[18];
|
|
||||||
snprintf(macStr, sizeof(macStr),
|
|
||||||
"%02X:%02X:%02X:%02X:%02X:%02X",
|
|
||||||
client.mac[0], client.mac[1], client.mac[2],
|
|
||||||
client.mac[3], client.mac[4], client.mac[5]);
|
|
||||||
Serial.print("Adresse MAC : ");
|
|
||||||
Serial.println(macStr);
|
|
||||||
}
|
|
||||||
|
|
||||||
digitalWrite(2,LOW);
|
|
||||||
}
|
|
||||||
|
|
||||||
void onClientDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
|
|
||||||
digitalWrite(2,HIGH);
|
|
||||||
|
|
||||||
wifi_sta_list_t sta_list;
|
|
||||||
esp_wifi_ap_get_sta_list(&sta_list);
|
|
||||||
|
|
||||||
for (int i = 0; i < sta_list.num; i++) {
|
|
||||||
const wifi_sta_info_t& client = sta_list.sta[i];
|
|
||||||
char macStr[18];
|
|
||||||
snprintf(macStr, sizeof(macStr),
|
|
||||||
"%02X:%02X:%02X:%02X:%02X:%02X",
|
|
||||||
client.mac[0], client.mac[1], client.mac[2],
|
|
||||||
client.mac[3], client.mac[4], client.mac[5]);
|
|
||||||
Serial.print("",macStr);
|
|
||||||
}
|
|
||||||
|
|
||||||
digitalWrite(2,LOW);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(115200);
|
|
||||||
WiFi.softAP(ssid, password);
|
|
||||||
|
|
||||||
WiFi.onEvent(onClientConnected, ARDUINO_EVENT_WIFI_AP_STACONNECTED);
|
|
||||||
WiFi.onEvent(onClientDisconnected, ARDUINO_EVENT_WIFI_AP_STADISCONNECTED);
|
|
||||||
|
|
||||||
delay(1000); // Donne un peu de temps pour démarrer le WiFi
|
|
||||||
|
|
||||||
|
|
||||||
server.on("/", handleRoot);
|
|
||||||
server.begin();
|
|
||||||
|
|
||||||
pinMode(2, OUTPUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
server.handleClient();
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
void setup() {
|
|
||||||
Serial.begin(115200);
|
|
||||||
pinMode(2, OUTPUT); // LED intégrée sur beaucoup d'ESP32
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
digitalWrite(2, HIGH);
|
|
||||||
delay(500);
|
|
||||||
digitalWrite(2, LOW);
|
|
||||||
delay(500);
|
|
||||||
}
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
# Protocole between FPGA and ESP32
|
|
||||||
|
|
||||||
Ce protocole permet la communication entre le FPGA et l'ESP32 via UART, principalement pour transférer des données ou des commandes de contrôle simples.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Structure générale des trames
|
|
||||||
|
|
||||||
Chaque trame est encadrée par des caractères spéciaux :
|
|
||||||
|
|
||||||
- **ESCAPE** (Before special char) : `0x1B`
|
|
||||||
- **STX** (Start of Text) : `0x02`
|
|
||||||
- **ETX** (End of Text) : `0x03`
|
|
||||||
|
|
||||||
|
|
||||||
**Format de trame :**
|
|
||||||
```
|
|
||||||
0x02 OP_CODE VALUE_1 VALUE_2 ... 0x1B 0x03
|
|
||||||
```
|
|
||||||
---
|
|
||||||
|
|
||||||
## Détail des commandes
|
|
||||||
|
|
||||||
| OP_CODE | Nom | Description | Format |
|
|
||||||
|---------|---------------------------|---------------------------------------------------------|-------------------------------------------------------------------------------|
|
|
||||||
| 0x00 | Error | Indique une erreur | `0x02 0x00 [ERROR_CODE] 0x1B 0x03` |
|
|
||||||
| 0x01 | Wi-Fi State | Indique l'état du Wi-Fi (0 = down, 1 = up) | `0x02 0x01 [0x00/0x01] 0x1B 0x03` |
|
|
||||||
| 0x02 | Connection Update | Notification de connexion ou déconnexion d’un appareil | `0x02 0x02 [0x00/0x01] [MAC_ADDR (6 bytes)] 0x1B 0x03` |
|
|
||||||
| 0x03 | Request Connected Devices | Demande la liste des appareils connectés | `0x02 0x03 0x1B 0x03` |
|
|
||||||
| 0x04 | Send Connected Devices | Envoie la liste des appareil connecter | `0x02 0x04 [LEN (1 byte)] [MAC_LIST (n bytes)[MAC_ADDR (6 bytes)]] 0x1B 0x03` |
|
|
||||||
| 0x05 | Send Message | Envoie un message à un appareil connecté via son MAC | `0x02 0x05 [MAC_ADDR (6 bytes)] [LEN (1 byte)] [MESSAGE] 0x1B 0x03` |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Détail des code erreur
|
|
||||||
|
|
||||||
| ERROR_CODE | Description |
|
|
||||||
|---------------|---------------------------------------------------------------|
|
|
||||||
| 0x00 | |
|
|
||||||
| 0x01 | Unknow command |
|
|
||||||
| 0x02 | Args error |
|
|
||||||
| 0x03 | Invalid Trame |
|
|
||||||
| 0x04 | Too long trame |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Détails des champs
|
|
||||||
|
|
||||||
- **MAC_ADDR** : 6 octets représentant l’adresse MAC du destinataire.
|
|
||||||
- **LEN** : Longueur du message à envoyer (1 octet).
|
|
||||||
- **MESSAGE** : Suite d’octets de taille `LEN` représentant le message (binaire ou texte selon le contexte).
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Exemples
|
|
||||||
|
|
||||||
### Exemple 1 : Wi-Fi actif
|
|
||||||
```
|
|
||||||
0x02 0x01 0x01 0x03
|
|
||||||
```
|
|
||||||
→ Indique que le Wi-Fi est actif.
|
|
||||||
|
|
||||||
### Exemple 2 : Connexion d'un appareil
|
|
||||||
```
|
|
||||||
0x02 0x02 0x01 0x12 0x34 0x56 0x78 0x9A 0xBC 0x03
|
|
||||||
```
|
|
||||||
→ Un appareil avec l’adresse MAC `12:34:56:78:9A:BC` vient de se connecter.
|
|
||||||
|
|
||||||
### Exemple 3 : Envoi de message
|
|
||||||
```
|
|
||||||
0x02 0x04 0x12 0x34 0x56 0x78 0x9A 0xBC 0x05 0x48 0x65 0x6C 0x6C 0x6F 0x03
|
|
||||||
```
|
|
||||||
→ Envoi du message `"Hello"` à l’appareil `12:34:56:78:9A:BC`.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Remarques
|
|
||||||
|
|
||||||
- Aucune vérification CRC/Checksum n’est ajoutée pour le moment (possible amélioration future).
|
|
||||||
- Le protocole est extensible : il suffit d’ajouter de nouveaux OP_CODEs au besoin.
|
|
||||||
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
`timescale 1ns / 1ps
|
|
||||||
|
|
||||||
module tb_top_uart_rx_tx;
|
|
||||||
|
|
||||||
parameter CLK_FREQ = 27_000_000;
|
|
||||||
parameter BAUD_RATE = 115200;
|
|
||||||
|
|
||||||
// Signaux
|
|
||||||
reg clk = 0;
|
|
||||||
reg start = 0;
|
|
||||||
reg [7:0] data_in = 0;
|
|
||||||
wire [7:0] data_out;
|
|
||||||
wire valid;
|
|
||||||
wire tx;
|
|
||||||
wire rx; // On connecte tx directement à rx pour le test
|
|
||||||
|
|
||||||
// Instance du module à tester
|
|
||||||
top_uart_rx_tx #(
|
|
||||||
.CLK_FREQ(CLK_FREQ),
|
|
||||||
.BAUD_RATE(BAUD_RATE)
|
|
||||||
) uut (
|
|
||||||
.clk(clk),
|
|
||||||
.start(start),
|
|
||||||
.data_in(data_in),
|
|
||||||
.rx(rx),
|
|
||||||
.data_out(data_out),
|
|
||||||
.valid(valid),
|
|
||||||
.tx(tx)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Boucle le tx sur rx
|
|
||||||
assign rx = tx;
|
|
||||||
|
|
||||||
// Clock à 50 MHz (20 ns période)
|
|
||||||
always #10 clk = ~clk;
|
|
||||||
|
|
||||||
// Simulation principale
|
|
||||||
initial begin
|
|
||||||
$display("Début de la simulation");
|
|
||||||
$dumpfile("uart_loopback.vcd"); // Pour GTKWave
|
|
||||||
$dumpvars(0, tb_top_uart_rx_tx);
|
|
||||||
|
|
||||||
// Attendre un peu
|
|
||||||
#(20 * 10);
|
|
||||||
|
|
||||||
// Envoi d'une valeur
|
|
||||||
data_in = 8'hA5; // Exemple de data
|
|
||||||
start = 1;
|
|
||||||
#20;
|
|
||||||
start = 0;
|
|
||||||
|
|
||||||
// Attendre la réception (valeur valid = 1)
|
|
||||||
wait(valid == 1);
|
|
||||||
|
|
||||||
// Affichage des résultats
|
|
||||||
$display("Data envoyee : 0x%h", data_in);
|
|
||||||
$display("Data recue : 0x%h", data_out);
|
|
||||||
|
|
||||||
if (data_out == data_in)
|
|
||||||
$display("Test reussi !");
|
|
||||||
else
|
|
||||||
$display("Test echoue...");
|
|
||||||
|
|
||||||
// Fin de simulation
|
|
||||||
#(20 * 10);
|
|
||||||
$finish;
|
|
||||||
end
|
|
||||||
|
|
||||||
endmodule
|
|
||||||
|
Before Width: | Height: | Size: 62 KiB After Width: | Height: | Size: 62 KiB |
@@ -6,6 +6,7 @@ module rx_fifo #(
|
|||||||
input rst_p,
|
input rst_p,
|
||||||
input [WIDTH-1:0] rx_data_in,
|
input [WIDTH-1:0] rx_data_in,
|
||||||
input rx_data_valid, // Indique que les données reçues sont valides
|
input rx_data_valid, // Indique que les données reçues sont valides
|
||||||
|
input read_fifo, // Indique que la FIFO doit être lue
|
||||||
output reg [WIDTH-1:0] rx_data_out,
|
output reg [WIDTH-1:0] rx_data_out,
|
||||||
output reg rx_data_ready, // Indique que les données peuvent être lues
|
output reg rx_data_ready, // Indique que les données peuvent être lues
|
||||||
output reg fifo_empty, // FIFO vide
|
output reg fifo_empty, // FIFO vide
|
||||||
@@ -34,7 +35,7 @@ module rx_fifo #(
|
|||||||
end
|
end
|
||||||
|
|
||||||
// Lecture de la FIFO
|
// Lecture de la FIFO
|
||||||
if (!fifo_empty) begin
|
if (!fifo_empty && read_fifo) begin
|
||||||
rx_data_out <= fifo_mem[rd_ptr];
|
rx_data_out <= fifo_mem[rd_ptr];
|
||||||
rd_ptr <= rd_ptr + 1;
|
rd_ptr <= rd_ptr + 1;
|
||||||
fifo_count <= fifo_count - 1;
|
fifo_count <= fifo_count - 1;
|
||||||
1092
Semaine_3/UARTV3/tb_rx_tx.out
Normal file
1092
Semaine_3/UARTV3/tb_rx_tx.out
Normal file
File diff suppressed because it is too large
Load Diff
79
Semaine_3/UARTV3/tb_top_uart_rx_tx.v
Normal file
79
Semaine_3/UARTV3/tb_top_uart_rx_tx.v
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
`timescale 1ns / 1ps
|
||||||
|
|
||||||
|
module tb_top_uart_rx_tx;
|
||||||
|
|
||||||
|
// Signaux
|
||||||
|
reg clk = 0;
|
||||||
|
reg [7:0] data_in = 0;
|
||||||
|
wire [7:0] rx_data; // rx_data est maintenant la donnée reçue
|
||||||
|
wire rx_data_valid; // rx_data_valid est la validité des données reçues
|
||||||
|
wire tx; // Signal tx à connecter à rx pour le test
|
||||||
|
reg rx_data_ready; // rx_data_ready sert a indiquer de passer à la réception suivante
|
||||||
|
reg tx_data_valid;
|
||||||
|
reg read_fifo = 0;
|
||||||
|
|
||||||
|
// Instance du module à tester
|
||||||
|
uart_top top_inst (
|
||||||
|
.clk(clk),
|
||||||
|
.rst(1'b0), // Assurez-vous de définir un reset dans le testbench
|
||||||
|
.uart_rx(rx), // Connecte tx au rx pour le test
|
||||||
|
.uart_tx(tx), // Signal de transmission UART
|
||||||
|
.rx_data(rx_data), // Données reçues
|
||||||
|
.rx_data_valid(rx_data_valid), // Données reçues valides
|
||||||
|
.rx_data_ready(rx_data_ready), // Prêt à recevoir (observe ce signal, ne le connecte pas en entrée)
|
||||||
|
.tx_data(data_in), // Données à transmettre
|
||||||
|
.tx_data_valid(tx_data_valid), // Transmission valide (toujours activée dans ce test)
|
||||||
|
.tx_data_ready(), // Prêt pour la transmission (non utilisé dans ce test)
|
||||||
|
.read_fifo(read_fifo)
|
||||||
|
|
||||||
|
);
|
||||||
|
|
||||||
|
// Boucle le tx sur rx
|
||||||
|
assign rx = tx;
|
||||||
|
|
||||||
|
|
||||||
|
always #18.5 clk = ~clk;
|
||||||
|
|
||||||
|
// Simulation principale
|
||||||
|
initial begin
|
||||||
|
$display("Debut de la simulation");
|
||||||
|
$dumpfile("uart_loopback.vcd"); // Pour GTKWave
|
||||||
|
$dumpvars(0, tb_top_uart_rx_tx);
|
||||||
|
|
||||||
|
// Attendre un peu
|
||||||
|
#200;
|
||||||
|
|
||||||
|
rx_data_ready = 1; // Indique que le module est prêt à recevoir des données
|
||||||
|
|
||||||
|
// Envoi d'une valeur
|
||||||
|
data_in = 8'hA5; // Exemple de data
|
||||||
|
tx_data_valid = 1;
|
||||||
|
#20;
|
||||||
|
tx_data_valid = 0;
|
||||||
|
rx_data_ready = 0; // Indique que le module n'est pas prêt à recevoir des données
|
||||||
|
|
||||||
|
#200;
|
||||||
|
|
||||||
|
// Attendre la réception (valeur valid = 1)
|
||||||
|
$display("Attente de rx_data_valid");
|
||||||
|
wait(rx_data_valid == 1);
|
||||||
|
|
||||||
|
read_fifo = 1; // Indique que la FIFO doit être lue
|
||||||
|
|
||||||
|
// Affichage des résultats
|
||||||
|
$display("Data envoyee : 0x%h", data_in);
|
||||||
|
$display("Data recue : 0x%h", rx_data);
|
||||||
|
|
||||||
|
read_fifo = 0; // Réinitialiser le signal de lecture de FIFO
|
||||||
|
|
||||||
|
if (rx_data == data_in)
|
||||||
|
$display("Test reussi !");
|
||||||
|
else
|
||||||
|
$display("Test echoue...");
|
||||||
|
|
||||||
|
// Fin de simulation
|
||||||
|
#200;
|
||||||
|
$finish;
|
||||||
|
end
|
||||||
|
|
||||||
|
endmodule
|
||||||
@@ -23,6 +23,7 @@ module tx_fifo #(
|
|||||||
reg [4:0] fifo_count = 0;
|
reg [4:0] fifo_count = 0;
|
||||||
|
|
||||||
always @(posedge clk or posedge rst_p) begin
|
always @(posedge clk or posedge rst_p) begin
|
||||||
|
tx_data_out <= fifo_mem[rd_ptr];
|
||||||
if (rst_p) begin
|
if (rst_p) begin
|
||||||
wr_ptr <= 0;
|
wr_ptr <= 0;
|
||||||
rd_ptr <= 0;
|
rd_ptr <= 0;
|
||||||
@@ -40,7 +41,7 @@ module tx_fifo #(
|
|||||||
|
|
||||||
// Lecture depuis FIFO
|
// Lecture depuis FIFO
|
||||||
if (uart_tx_ready && !fifo_empty) begin
|
if (uart_tx_ready && !fifo_empty) begin
|
||||||
tx_data_out <= fifo_mem[rd_ptr];
|
|
||||||
rd_ptr <= rd_ptr + 1;
|
rd_ptr <= rd_ptr + 1;
|
||||||
fifo_count <= fifo_count - 1;
|
fifo_count <= fifo_count - 1;
|
||||||
end
|
end
|
||||||
2711893
Semaine_3/UARTV3/uart_loopback.vcd
Normal file
2711893
Semaine_3/UARTV3/uart_loopback.vcd
Normal file
File diff suppressed because it is too large
Load Diff
@@ -11,7 +11,7 @@ module uart_rx #(
|
|||||||
output reg rx_data_valid //received serial data is valid
|
output reg rx_data_valid //received serial data is valid
|
||||||
);
|
);
|
||||||
|
|
||||||
localparam CYCLE = CLK_FREQ * / BAUD_RATE;
|
localparam CYCLE = CLK_FREQ / BAUD_RATE;
|
||||||
|
|
||||||
//state machine code
|
//state machine code
|
||||||
localparam S_IDLE = 1;
|
localparam S_IDLE = 1;
|
||||||
@@ -32,7 +32,7 @@ reg[2:0] bit_cnt; //bit counter
|
|||||||
|
|
||||||
assign rx_negedge = rx_d1 && ~rx_d0; // Front déscendant
|
assign rx_negedge = rx_d1 && ~rx_d0; // Front déscendant
|
||||||
|
|
||||||
always@(posedge clk or negedge rst_p) // Filtrage du signial
|
always@(posedge clk or posedge rst_p) // Filtrage du signial
|
||||||
begin
|
begin
|
||||||
if(rst_p == 1'b1)begin
|
if(rst_p == 1'b1)begin
|
||||||
rx_d0 <= 1'b0;
|
rx_d0 <= 1'b0;
|
||||||
@@ -45,7 +45,7 @@ begin
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
always@(posedge clk or negedge rst_p)begin // Compteur d'etat
|
always@(posedge clk or posedge rst_p)begin // Compteur d'etat
|
||||||
if(rst_p == 1'b1)
|
if(rst_p == 1'b1)
|
||||||
state <= S_IDLE;
|
state <= S_IDLE;
|
||||||
else
|
else
|
||||||
@@ -89,7 +89,7 @@ always@(*)begin
|
|||||||
endcase
|
endcase
|
||||||
end
|
end
|
||||||
|
|
||||||
always@(posedge clk or negedge rst_p)
|
always@(posedge clk or posedge rst_p)
|
||||||
begin
|
begin
|
||||||
if(rst_p == 1'b1)
|
if(rst_p == 1'b1)
|
||||||
rx_data_valid <= 1'b0;
|
rx_data_valid <= 1'b0;
|
||||||
@@ -99,7 +99,7 @@ begin
|
|||||||
rx_data_valid <= 1'b0;
|
rx_data_valid <= 1'b0;
|
||||||
end
|
end
|
||||||
|
|
||||||
always@(posedge clk or negedge rst_p)
|
always@(posedge clk or posedge rst_p)
|
||||||
begin
|
begin
|
||||||
if(rst_p == 1'b1)
|
if(rst_p == 1'b1)
|
||||||
rx_data <= 8'd0;
|
rx_data <= 8'd0;
|
||||||
@@ -107,7 +107,7 @@ begin
|
|||||||
rx_data <= rx_bits;//latch received data
|
rx_data <= rx_bits;//latch received data
|
||||||
end
|
end
|
||||||
|
|
||||||
always@(posedge clk or negedge rst_p)
|
always@(posedge clk or posedge rst_p)
|
||||||
begin
|
begin
|
||||||
if(rst_p == 1'b1)
|
if(rst_p == 1'b1)
|
||||||
begin
|
begin
|
||||||
@@ -123,7 +123,7 @@ begin
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
always@(posedge clk or negedge rst_p)
|
always@(posedge clk or posedge rst_p)
|
||||||
begin
|
begin
|
||||||
if(rst_p == 1'b1)
|
if(rst_p == 1'b1)
|
||||||
cycle_cnt <= 16'd0;
|
cycle_cnt <= 16'd0;
|
||||||
@@ -133,7 +133,7 @@ begin
|
|||||||
cycle_cnt <= cycle_cnt + 16'd1;
|
cycle_cnt <= cycle_cnt + 16'd1;
|
||||||
end
|
end
|
||||||
//receive serial data bit data
|
//receive serial data bit data
|
||||||
always@(posedge clk or negedge rst_p)
|
always@(posedge clk or posedge rst_p)
|
||||||
begin
|
begin
|
||||||
if(rst_p == 1'b1)
|
if(rst_p == 1'b1)
|
||||||
rx_bits <= 8'd0;
|
rx_bits <= 8'd0;
|
||||||
@@ -5,19 +5,20 @@ module uart_top(
|
|||||||
input wire uart_rx,
|
input wire uart_rx,
|
||||||
output wire uart_tx,
|
output wire uart_tx,
|
||||||
|
|
||||||
// Interfaces RX vers utilisateur
|
// Interfaces RX
|
||||||
output wire [7:0] rx_data,
|
output wire [7:0] rx_data,
|
||||||
output wire rx_data_valid,
|
output wire rx_data_valid,
|
||||||
input wire rx_data_ready,
|
input wire rx_data_ready,
|
||||||
|
input wire read_fifo,
|
||||||
|
|
||||||
// Interfaces TX depuis utilisateur
|
// Interfaces TX
|
||||||
input wire [7:0] tx_data,
|
input wire [7:0] tx_data,
|
||||||
input wire tx_data_valid,
|
input wire tx_data_valid,
|
||||||
output wire tx_data_ready
|
output wire tx_data_ready
|
||||||
);
|
);
|
||||||
|
|
||||||
parameter CLK_FRE = 27_000_000; // Hz
|
parameter CLK_FRE = 27_000_000; // Hz
|
||||||
parameter UART_FRE = 115200; // Baudrate
|
parameter BAUD_RATE = 115200; // Baudrate
|
||||||
|
|
||||||
// === Signaux internes ===
|
// === Signaux internes ===
|
||||||
wire [7:0] uart_rx_data;
|
wire [7:0] uart_rx_data;
|
||||||
@@ -28,6 +29,11 @@ module uart_top(
|
|||||||
wire uart_tx_data_valid;
|
wire uart_tx_data_valid;
|
||||||
wire uart_tx_data_ready;
|
wire uart_tx_data_ready;
|
||||||
|
|
||||||
|
wire tx_fifo_empty;
|
||||||
|
wire tx_fifo_full;
|
||||||
|
wire rx_fifo_empty;
|
||||||
|
wire rx_fifo_full;
|
||||||
|
|
||||||
// === FIFO RX ===
|
// === FIFO RX ===
|
||||||
rx_fifo #(
|
rx_fifo #(
|
||||||
.WIDTH(8),
|
.WIDTH(8),
|
||||||
@@ -39,8 +45,9 @@ module uart_top(
|
|||||||
.rx_data_valid (uart_rx_data_valid),
|
.rx_data_valid (uart_rx_data_valid),
|
||||||
.rx_data_out (rx_data),
|
.rx_data_out (rx_data),
|
||||||
.rx_data_ready (rx_data_ready),
|
.rx_data_ready (rx_data_ready),
|
||||||
.fifo_empty (), // pas utilisé ici
|
.fifo_empty (rx_fifo_empty),
|
||||||
.fifo_full ()
|
.fifo_full (rx_fifo_full),
|
||||||
|
.read_fifo (read_fifo)
|
||||||
);
|
);
|
||||||
|
|
||||||
// === FIFO TX ===
|
// === FIFO TX ===
|
||||||
@@ -54,14 +61,13 @@ module uart_top(
|
|||||||
.tx_data_valid (tx_data_valid),
|
.tx_data_valid (tx_data_valid),
|
||||||
.tx_data_ready (tx_data_ready),
|
.tx_data_ready (tx_data_ready),
|
||||||
.tx_data_out (uart_tx_data),
|
.tx_data_out (uart_tx_data),
|
||||||
.uart_tx_ready (uart_tx_data_ready)
|
.uart_tx_ready (uart_tx_data_ready),
|
||||||
|
.fifo_empty (tx_fifo_empty),
|
||||||
|
.fifo_full (tx_fifo_full)
|
||||||
);
|
);
|
||||||
|
|
||||||
// === Instanciation RX UART ===
|
// === Instanciation RX UART ===
|
||||||
uart_rx #(
|
uart_rx uart_rx_inst (
|
||||||
.CLK_FRE(CLK_FRE),
|
|
||||||
.BAUD_RATE(UART_FRE)
|
|
||||||
) uart_rx_inst (
|
|
||||||
.clk (clk),
|
.clk (clk),
|
||||||
.rst_p (rst),
|
.rst_p (rst),
|
||||||
.rx_data (uart_rx_data),
|
.rx_data (uart_rx_data),
|
||||||
@@ -71,16 +77,15 @@ module uart_top(
|
|||||||
);
|
);
|
||||||
|
|
||||||
// === Instanciation TX UART ===
|
// === Instanciation TX UART ===
|
||||||
uart_tx #(
|
uart_tx uart_tx_inst (
|
||||||
.CLK_FRE(CLK_FRE),
|
|
||||||
.BAUD_RATE(UART_FRE)
|
|
||||||
) uart_tx_inst (
|
|
||||||
.clk (clk),
|
.clk (clk),
|
||||||
.rst_p (rst),
|
.rst_p (rst),
|
||||||
.tx_data (uart_tx_data),
|
.data (uart_tx_data),
|
||||||
.tx_data_valid (uart_tx_data_valid),
|
.tx_data_valid (uart_tx_data_valid),
|
||||||
.tx_data_ready (uart_tx_data_ready),
|
.tx_data_ready (uart_tx_data_ready),
|
||||||
.tx_pin (uart_tx)
|
.tx (uart_tx)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
assign uart_tx_data_valid = (!tx_fifo_empty && uart_tx_data_ready) ? 1'b1 : 1'b0;
|
||||||
|
|
||||||
endmodule
|
endmodule
|
||||||
@@ -21,10 +21,10 @@ module uart_tx #(
|
|||||||
reg [1:0] state = IDLE;
|
reg [1:0] state = IDLE;
|
||||||
reg [1:0] next_state;
|
reg [1:0] next_state;
|
||||||
reg [15:0] cycle_cnt; //baud counter
|
reg [15:0] cycle_cnt; //baud counter
|
||||||
reg [3:0] bit_index = 0;
|
|
||||||
reg [15:0] clk_count = 0;
|
|
||||||
reg [7:0] tx_data = 0;
|
|
||||||
reg tx_reg;
|
reg tx_reg;
|
||||||
|
reg [2:0] bit_cnt;
|
||||||
|
reg [7:0] tx_data_latch = 0;
|
||||||
|
|
||||||
|
|
||||||
assign tx = tx_reg;
|
assign tx = tx_reg;
|
||||||
|
|
||||||
@@ -67,30 +67,28 @@ module uart_tx #(
|
|||||||
|
|
||||||
always@(posedge clk or posedge rst_p)begin // tx_data_ready block
|
always@(posedge clk or posedge rst_p)begin // tx_data_ready block
|
||||||
if(rst_p == 1'b1)
|
if(rst_p == 1'b1)
|
||||||
begin
|
tx_data_ready <= 1'b0; // Reset
|
||||||
tx_data_ready <= 1'b0;
|
else if(state == IDLE && tx_data_valid == 1'b1)
|
||||||
end
|
tx_data_ready <= 1'b0; // Pas prêt tant que les données sont valides
|
||||||
|
|
||||||
else if(state == IDLE)
|
else if(state == IDLE)
|
||||||
if(tx_data_valid == 1'b1)
|
tx_data_ready <= 1'b1;
|
||||||
tx_data_ready <= 1'b0;
|
|
||||||
else
|
|
||||||
tx_data_ready <= 1'b1;
|
|
||||||
|
|
||||||
else if(state == STOP && cycle_cnt == CYCLE - 1)
|
else if(state == STOP && cycle_cnt == CYCLE - 1)
|
||||||
tx_data_ready <= 1'b1;
|
tx_data_ready <= 1'b1; // Prêt une fois le bit STOP envoyé
|
||||||
|
else
|
||||||
|
tx_data_ready <= tx_data_ready; // Reste inchangé dans d'autres cas
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
always@(posedge clk or posedge rst_p) begin // tx_data_latch block
|
always@(posedge clk or posedge rst_p) begin // tx_data_latch block
|
||||||
if(rst_p == 1'b1)begin
|
if(rst_p == 1'b1) begin
|
||||||
tx_data_latch <= 8'd0;
|
tx_data_latch <= 8'd0;
|
||||||
|
end else if(state == IDLE && tx_data_valid == 1'b1) begin
|
||||||
end else if(state == IDLE && tx_data_valid == 1'b1)
|
tx_data_latch <= data; // Charger les données de `data` dans `tx_data_latch`
|
||||||
tx_data_latch <= tx_data;
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
always@(posedge clk or posedge rst_p)begin // DATA bit_cnt block
|
always@(posedge clk or posedge rst_p)begin // DATA bit_cnt block
|
||||||
if(rst_p == 1'b1)begin
|
if(rst_p == 1'b1)begin
|
||||||
bit_cnt <= 3'd0;
|
bit_cnt <= 3'd0;
|
||||||
26
Semaine_3/UARTV3/uartread.py
Normal file
26
Semaine_3/UARTV3/uartread.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import serial
|
||||||
|
|
||||||
|
# Configuration du port série
|
||||||
|
ser = serial.Serial(
|
||||||
|
port='COM6', # Remplace par ton port (ex : '/dev/ttyUSB0' sur Linux)
|
||||||
|
baudrate=115200,
|
||||||
|
timeout=1 # 1 seconde d'attente max pour la lecture
|
||||||
|
)
|
||||||
|
|
||||||
|
print("Lecture série en cours (Ctrl+C pour arrêter)...")
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
if ser.in_waiting >= 2:
|
||||||
|
# Lecture des 2 octets (MSB en premier)
|
||||||
|
high_byte = ser.read()
|
||||||
|
low_byte = ser.read()
|
||||||
|
|
||||||
|
if high_byte and low_byte:
|
||||||
|
# Conversion en entier 16 bits
|
||||||
|
distance = (high_byte[0] << 8) | low_byte[0]
|
||||||
|
print(f"Distance mesurée : {high_byte}, {low_byte} cm")
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\nArrêt manuel.")
|
||||||
|
finally:
|
||||||
|
ser.close()
|
||||||
Reference in New Issue
Block a user