Skip to content

Commit e42720b

Browse files
committed
[FEATURE]: Add UART2Wishbone bridge example
1 parent 6f5e0af commit e42720b

File tree

6 files changed

+516
-0
lines changed

6 files changed

+516
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#-------------------------------------------------------------------------------
2+
# PROJECT: SIMPLE UART FOR FPGA
3+
#-------------------------------------------------------------------------------
4+
# AUTHORS: Jakub Cabal <jakubcabal@gmail.com>
5+
# LICENSE: The MIT License (MIT), please read LICENSE file
6+
# WEBSITE: https://github.com/jakubcabal/uart-for-fpga
7+
#-------------------------------------------------------------------------------
8+
9+
PROJECT_REVISION = "UART2WB_CYC1000"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#-------------------------------------------------------------------------------
2+
# PROJECT: SIMPLE UART FOR FPGA
3+
#-------------------------------------------------------------------------------
4+
# AUTHORS: Jakub Cabal <jakubcabal@gmail.com>
5+
# LICENSE: The MIT License, please read LICENSE file
6+
# WEBSITE: https://github.com/jakubcabal/uart-for-fpga
7+
#-------------------------------------------------------------------------------
8+
9+
# QUARTUS SETTINGS FILE FOR CYC1000 BOARD
10+
set_global_assignment -name FAMILY "Cyclone 10 LP"
11+
set_global_assignment -name DEVICE 10CL025YU256C8G
12+
set_global_assignment -name TOP_LEVEL_ENTITY UART2WB_FPGA_CYC1000
13+
14+
# PROJECT VHDL FILES
15+
set_global_assignment -name VHDL_FILE ../../../rtl/comp/uart_clk_div.vhd
16+
set_global_assignment -name VHDL_FILE ../../../rtl/comp/uart_parity.vhd
17+
set_global_assignment -name VHDL_FILE ../../../rtl/comp/uart_debouncer.vhd
18+
set_global_assignment -name VHDL_FILE ../../../rtl/comp/uart_tx.vhd
19+
set_global_assignment -name VHDL_FILE ../../../rtl/comp/uart_rx.vhd
20+
set_global_assignment -name VHDL_FILE ../../../rtl/uart.vhd
21+
set_global_assignment -name VHDL_FILE ../../common/rst_sync.vhd
22+
set_global_assignment -name VHDL_FILE ../uart2wbm.vhd
23+
set_global_assignment -name VHDL_FILE ../uart2wb_fpga_cyc1000.vhd
24+
25+
# TIMING CONSTRAINTS
26+
set_global_assignment -name SDC_FILE ./uart2wb_cyc1000.sdc
27+
28+
# FPGA PINS ASSIGNMENT
29+
set_location_assignment PIN_M2 -to CLK_12M
30+
set_location_assignment PIN_N6 -to RST_BTN_N
31+
set_location_assignment PIN_T7 -to UART_TXD
32+
set_location_assignment PIN_R7 -to UART_RXD
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#-------------------------------------------------------------------------------
2+
# PROJECT: SIMPLE UART FOR FPGA
3+
#-------------------------------------------------------------------------------
4+
# AUTHORS: Jakub Cabal <jakubcabal@gmail.com>
5+
# LICENSE: The MIT License (MIT), please read LICENSE file
6+
# WEBSITE: https://github.com/jakubcabal/uart-for-fpga
7+
#-------------------------------------------------------------------------------
8+
9+
create_clock -name CLK12M -period 12MHz [get_ports {CLK_12M}]

examples/uart2wb/sw/wishbone.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/python
2+
#-------------------------------------------------------------------------------
3+
# PROJECT: SIMPLE UART FOR FPGA
4+
#-------------------------------------------------------------------------------
5+
# AUTHORS: Jakub Cabal <jakubcabal@gmail.com>
6+
# LICENSE: The MIT License, please read LICENSE file
7+
# WEBSITE: https://github.com/jakubcabal/uart-for-fpga
8+
#-------------------------------------------------------------------------------
9+
10+
import serial
11+
12+
byteorder="little"
13+
14+
class wishbone:
15+
def __init__(self, port="COM1", baudrate=9600):
16+
self.uart = serial.Serial(port, baudrate, timeout=2)
17+
print("The UART on " + self.uart.name + " is open.")
18+
print("The wishbone bus is ready.\n")
19+
20+
def read(self,addr):
21+
cmd = 0x0
22+
cmd = cmd.to_bytes(1,byteorder)
23+
self.uart.write(cmd)
24+
addr = addr.to_bytes(2,byteorder)
25+
self.uart.write(addr)
26+
rbytes=self.uart.read(1)
27+
rbytes=self.uart.read(4)
28+
drd=int.from_bytes(rbytes,byteorder)
29+
return drd
30+
31+
def write(self, addr, data):
32+
cmd = 0x1
33+
cmd = cmd.to_bytes(1,byteorder)
34+
self.uart.write(cmd)
35+
addr = addr.to_bytes(2,byteorder)
36+
self.uart.write(addr)
37+
data = data.to_bytes(4,byteorder)
38+
self.uart.write(data)
39+
rbytes=self.uart.read(1)
40+
41+
def close(self):
42+
self.uart.close()
43+
44+
if __name__ == '__main__':
45+
print("Test of access to CSR (control status registers) via UART2WBM module...")
46+
print("=======================================================================")
47+
wb = wishbone("COM4")
48+
49+
print("\nREAD from 0x0:")
50+
rd = wb.read(0x0)
51+
print("0x%02X" % rd)
52+
53+
print("\nREAD from 0x4:")
54+
rd = wb.read(0x4)
55+
print("0x%02X" % rd)
56+
57+
print("\nWRITE 0x12345678 to 0x4.")
58+
wb.write(0x4,0x12345678)
59+
60+
print("\nREAD from 0x4:")
61+
rd = wb.read(0x4)
62+
print("0x%02X" % rd)
63+
64+
print("\nWRITE 0xABCDEF12 to 0x4.")
65+
wb.write(0x4,0xABCDEF12)
66+
67+
print("\nREAD from 0x4:")
68+
rd = wb.read(0x4)
69+
print("0x%02X" % rd)
70+
71+
print("\nREAD from 0x8844:")
72+
rd = wb.read(0x8844)
73+
print("0x%02X" % rd)
74+
75+
wb.close()
76+
print("\nThe UART is closed.")
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
--------------------------------------------------------------------------------
2+
-- PROJECT: SIMPLE UART FOR FPGA
3+
--------------------------------------------------------------------------------
4+
-- AUTHORS: Jakub Cabal <jakubcabal@gmail.com>
5+
-- LICENSE: The MIT License, please read LICENSE file
6+
-- WEBSITE: https://github.com/jakubcabal/uart-for-fpga
7+
--------------------------------------------------------------------------------
8+
9+
library IEEE;
10+
use IEEE.STD_LOGIC_1164.ALL;
11+
use IEEE.NUMERIC_STD.ALL;
12+
13+
-- TOP MODULE OF UART 2 WISHBONE EXAMPLE FOR CYC1000 BOARD
14+
-- =======================================================
15+
16+
entity UART2WB_FPGA_CYC1000 is
17+
Port (
18+
CLK_12M : in std_logic; -- system clock 12 MHz
19+
RST_BTN_N : in std_logic; -- low active reset button
20+
-- UART INTERFACE
21+
UART_RXD : in std_logic;
22+
UART_TXD : out std_logic
23+
);
24+
end entity;
25+
26+
architecture RTL of UART2WB_FPGA_CYC1000 is
27+
28+
signal rst_btn : std_logic;
29+
signal reset : std_logic;
30+
31+
signal wb_cyc : std_logic;
32+
signal wb_stb : std_logic;
33+
signal wb_we : std_logic;
34+
signal wb_addr : std_logic_vector(15 downto 0);
35+
signal wb_dout : std_logic_vector(31 downto 0);
36+
signal wb_stall : std_logic;
37+
signal wb_ack : std_logic;
38+
signal wb_din : std_logic_vector(31 downto 0);
39+
40+
signal debug_reg_sel : std_logic;
41+
signal debug_reg_we : std_logic;
42+
signal debug_reg : std_logic_vector(31 downto 0);
43+
44+
begin
45+
46+
rst_btn <= not RST_BTN_N;
47+
48+
rst_sync_i : entity work.RST_SYNC
49+
port map (
50+
CLK => CLK_12M,
51+
ASYNC_RST => rst_btn,
52+
SYNCED_RST => reset
53+
);
54+
55+
uart2wbm_i : entity work.UART2WBM
56+
generic map (
57+
CLK_FREQ => 12e6,
58+
BAUD_RATE => 9600
59+
)
60+
port map (
61+
CLK => CLK_12M,
62+
RST => reset,
63+
-- UART INTERFACE
64+
UART_TXD => UART_TXD,
65+
UART_RXD => UART_RXD,
66+
-- WISHBONE MASTER INTERFACE
67+
WB_CYC => wb_cyc,
68+
WB_STB => wb_stb,
69+
WB_WE => wb_we,
70+
WB_ADDR => wb_addr,
71+
WB_DOUT => wb_dout,
72+
WB_STALL => wb_stall,
73+
WB_ACK => wb_ack,
74+
WB_DIN => wb_din
75+
);
76+
77+
debug_reg_sel <= '1' when (wb_addr = X"0004") else '0';
78+
debug_reg_we <= wb_stb and wb_we and debug_reg_sel;
79+
80+
debug_reg_p : process (CLK_12M)
81+
begin
82+
if (rising_edge(CLK_12M)) then
83+
if (debug_reg_we = '1') then
84+
debug_reg <= wb_din;
85+
end if;
86+
end if;
87+
end process;
88+
89+
wb_stall <= '0';
90+
91+
wb_ack_reg_p : process (CLK_12M)
92+
begin
93+
if (rising_edge(CLK_12M)) then
94+
wb_ack <= wb_cyc and wb_stb;
95+
end if;
96+
end process;
97+
98+
wb_dout_reg_p : process (CLK_12M)
99+
begin
100+
if (rising_edge(CLK_12M)) then
101+
case wb_addr is
102+
when X"0000" =>
103+
wb_dout <= X"20210406";
104+
when X"0004" =>
105+
wb_dout <= debug_reg;
106+
when others =>
107+
wb_dout <= X"DEADCAFE";
108+
end case;
109+
end if;
110+
end process;
111+
112+
end architecture;

0 commit comments

Comments
 (0)