1 |
2 |
pradd |
--------------------------------------------------------------------------
|
2 |
|
|
--------------------------------------------------------------------------
|
3 |
|
|
-- Title : FT245R interface
|
4 |
|
|
-- File : ft245rl_interface.vhd
|
5 |
|
|
-- Author : Alexey Lyashko <pradd@opencores.org>
|
6 |
|
|
-- License : LGPL
|
7 |
|
|
--------------------------------------------------------------------------
|
8 |
|
|
-- Description :
|
9 |
|
|
-- The controller simplifies the communication with FT245R chip. While
|
10 |
|
|
-- provided interface is very similar to that of the chip itself,
|
11 |
|
|
-- this controller takes care of all the delays and other aspects of
|
12 |
|
|
-- FT245R's protocol as well as provifes separate ports for input and
|
13 |
|
|
-- output.
|
14 |
|
|
--------------------------------------------------------------------------
|
15 |
|
|
--------------------------------------------------------------------------
|
16 |
|
|
|
17 |
|
|
library ieee;
|
18 |
|
|
use ieee.std_logic_1164.all;
|
19 |
|
|
use ieee.numeric_std.all;
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
entity ft245rl_interface is
|
23 |
|
|
-- This value is for 400MHz clock. Change it to suit your configuration.
|
24 |
|
|
generic (min_delay : real := 2.5);
|
25 |
|
|
port
|
26 |
|
|
(
|
27 |
|
|
-- physical FT245RL interface
|
28 |
|
|
data_io : inout std_logic_vector(7 downto 0);
|
29 |
|
|
nrst : out std_logic := '1';
|
30 |
|
|
ntxe : in std_logic;
|
31 |
|
|
nrxf : in std_logic;
|
32 |
|
|
nwr : out std_logic := '0';
|
33 |
|
|
nrd : out std_logic := '1';
|
34 |
|
|
|
35 |
|
|
-- logical interface
|
36 |
|
|
clk : in std_logic; -- System clock
|
37 |
|
|
data_in : in std_logic_vector(7 downto 0); -- Input from client entity
|
38 |
|
|
data_out : out std_logic_vector(7 downto 0); -- Output to client entity
|
39 |
|
|
nce : in std_logic := '1'; -- "Chip" enable
|
40 |
|
|
fetch_next_byte : in std_logic := '0'; -- Strobing this to '1' instructs the module to poll FT245RL for next available byte
|
41 |
|
|
do_write : in std_logic := '0'; -- Strobing this to '1' instructs the module to write byte to FT245RL
|
42 |
|
|
busy : out std_logic := '0'; -- Is '1' when the module is processing data
|
43 |
|
|
data_available : buffer std_logic := '0'; -- Notifies the client of data availability
|
44 |
|
|
reset : in std_logic := '1' -- Resets the module
|
45 |
|
|
);
|
46 |
|
|
end ft245rl_interface;
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
architecture action of ft245rl_interface is
|
50 |
|
|
type state_t is (
|
51 |
|
|
INIT,
|
52 |
|
|
IDLE,
|
53 |
|
|
READ_BYTE,
|
54 |
|
|
READ_BYTE1,
|
55 |
|
|
READ_BYTE2,
|
56 |
|
|
READ_BYTE3,
|
57 |
|
|
WRITE_BYTE,
|
58 |
|
|
WRITE_BYTE1,
|
59 |
|
|
WRITE_BYTE2,
|
60 |
|
|
WRITE_BYTE3,
|
61 |
|
|
DO_DELAY
|
62 |
|
|
);
|
63 |
|
|
signal c_state : state_t := INIT; -- current state
|
64 |
|
|
signal n_state : state_t := INIT; -- next state
|
65 |
|
|
|
66 |
|
|
signal delay_cnt:integer := 0; -- Delay counter register
|
67 |
|
|
signal current_delay : integer := 0; -- This register holds number of clock cycles
|
68 |
|
|
-- needed by the specified delay
|
69 |
|
|
signal in_buff : std_logic_vector(7 downto 0); -- holds data received from FT245RL
|
70 |
|
|
signal out_buff: std_logic_vector(7 downto 0); -- holds data to be sent to FT245RL
|
71 |
|
|
signal we : std_logic := '0'; -- enables data output to FT245RL
|
72 |
|
|
|
73 |
|
|
-- All delay specs may be found in FT245RL datasheet at
|
74 |
|
|
-- http://www.ftdichip.com/Support/Documents/DataSheets/ICs/DS_FT245R.pdf
|
75 |
|
|
constant t1_delay : integer := integer(50.0 / min_delay) - 1;
|
76 |
|
|
constant t2_delay : integer := integer((50.0 + 80.0) / min_delay) - 1;
|
77 |
|
|
constant t3_delay : integer := integer(35.0 / min_delay) - 1;
|
78 |
|
|
constant t4_delay : integer := 0;
|
79 |
|
|
constant t5_delay : integer := integer(25.0 / min_delay) - 1;
|
80 |
|
|
constant t6_delay : integer := integer(80.0 / min_delay) - 1;
|
81 |
|
|
constant t7_delay : integer := integer(50.0 / min_delay) - 1;
|
82 |
|
|
constant t8_delay : integer := integer(50.0 / min_delay) - 1;
|
83 |
|
|
constant t9_delay : integer := integer(20.0 / min_delay) - 1;
|
84 |
|
|
constant t10_delay : integer := 0;
|
85 |
|
|
constant t11_delay : integer := integer(25.0 / min_delay) - 1;
|
86 |
|
|
constant t12_delay : integer := integer(80.0 / min_delay) - 1;
|
87 |
|
|
begin
|
88 |
|
|
|
89 |
|
|
-- Bidirectional bus implementation.
|
90 |
|
|
data_io <= out_buff when we = '1' else
|
91 |
|
|
"ZZZZZZZZ" when we = '0' else
|
92 |
|
|
"XXXXXXXX";
|
93 |
|
|
in_buff <= data_io;
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
process(clk, reset, nrxf, ntxe, nce)
|
97 |
|
|
begin
|
98 |
|
|
if(reset = '0')then
|
99 |
|
|
c_state <= INIT;
|
100 |
|
|
elsif(rising_edge(clk) and nce = '0')then
|
101 |
|
|
|
102 |
|
|
case c_state is
|
103 |
|
|
-- The module enters this state on powerup or when 'reset' is low.
|
104 |
|
|
when INIT =>
|
105 |
|
|
delay_cnt <= 0;
|
106 |
|
|
current_delay <= 0;
|
107 |
|
|
c_state <= IDLE;
|
108 |
|
|
nrst <= '0'; -- Reset FT245RL on init
|
109 |
|
|
|
110 |
|
|
-- This is the "main loop"
|
111 |
|
|
when IDLE =>
|
112 |
|
|
nrst <= '1';
|
113 |
|
|
|
114 |
|
|
-- If this condition is true, we may safely read another byte from FT245RL's FIFO
|
115 |
|
|
if(nrxf = '0' and data_available = '0')then
|
116 |
|
|
c_state <= READ_BYTE;
|
117 |
|
|
|
118 |
|
|
-- We have to clear 'data_available' when the client module is requesting a new byte
|
119 |
|
|
elsif(fetch_next_byte = '1')then
|
120 |
|
|
data_available <= '0';
|
121 |
|
|
c_state <= IDLE;
|
122 |
|
|
|
123 |
|
|
-- Well, here we simply write a byte to FT245RL's data bus
|
124 |
|
|
elsif(do_write = '1')then
|
125 |
|
|
c_state <= WRITE_BYTE;
|
126 |
|
|
end if;
|
127 |
|
|
|
128 |
|
|
|
129 |
|
|
-- Read one byte from the device
|
130 |
|
|
when READ_BYTE =>
|
131 |
|
|
busy <= '1';
|
132 |
|
|
nrd <= '0';
|
133 |
|
|
current_delay <= t3_delay;
|
134 |
|
|
c_state <= DO_DELAY;
|
135 |
|
|
n_state <= READ_BYTE1;
|
136 |
|
|
|
137 |
|
|
when READ_BYTE1 =>
|
138 |
|
|
current_delay <= t1_delay - t3_delay;
|
139 |
|
|
c_state <= DO_DELAY;
|
140 |
|
|
n_state <= READ_BYTE2;
|
141 |
|
|
|
142 |
|
|
when READ_BYTE2 =>
|
143 |
|
|
data_out <= in_buff;
|
144 |
|
|
nrd <= '1';
|
145 |
|
|
current_delay <= t5_delay;
|
146 |
|
|
c_state <= DO_DELAY;
|
147 |
|
|
n_state <= READ_BYTE3;
|
148 |
|
|
|
149 |
|
|
when READ_BYTE3 =>
|
150 |
|
|
current_delay <= t2_delay;
|
151 |
|
|
c_state <= DO_DELAY;
|
152 |
|
|
n_state <= IDLE;
|
153 |
|
|
data_available <= '1';
|
154 |
|
|
busy <= '0';
|
155 |
|
|
|
156 |
|
|
-- Write one byte to the device
|
157 |
|
|
when WRITE_BYTE =>
|
158 |
|
|
busy <= '1';
|
159 |
|
|
if(ntxe = '0')then
|
160 |
|
|
nwr <= '1';
|
161 |
|
|
we <= '1';
|
162 |
|
|
current_delay<= t7_delay;
|
163 |
|
|
c_state <= DO_DELAY;
|
164 |
|
|
n_state <= WRITE_BYTE1;
|
165 |
|
|
out_buff <= data_in;
|
166 |
|
|
else
|
167 |
|
|
c_state <= WRITE_BYTE;
|
168 |
|
|
end if;
|
169 |
|
|
|
170 |
|
|
when WRITE_BYTE1 =>
|
171 |
|
|
nwr <= '0';
|
172 |
|
|
current_delay <= t11_delay;
|
173 |
|
|
c_state <= DO_DELAY;
|
174 |
|
|
n_state <= WRITE_BYTE2;
|
175 |
|
|
|
176 |
|
|
when WRITE_BYTE2 =>
|
177 |
|
|
we <= '0';
|
178 |
|
|
current_delay <= t12_delay;
|
179 |
|
|
c_state <=DO_DELAY;
|
180 |
|
|
n_state <= WRITE_BYTE3;
|
181 |
|
|
|
182 |
|
|
when WRITE_BYTE3 =>
|
183 |
|
|
busy <= '0';
|
184 |
|
|
c_state <= IDLE;
|
185 |
|
|
|
186 |
|
|
when DO_DELAY =>
|
187 |
|
|
if(delay_cnt < current_delay)then
|
188 |
|
|
delay_cnt <= delay_cnt + 1;
|
189 |
|
|
else
|
190 |
|
|
c_state <= n_state;
|
191 |
|
|
delay_cnt <= 0;
|
192 |
|
|
end if;
|
193 |
|
|
|
194 |
|
|
when others =>
|
195 |
|
|
c_state <= INIT;
|
196 |
|
|
end case; -- c_state
|
197 |
|
|
end if;
|
198 |
|
|
end process;
|
199 |
|
|
|
200 |
|
|
end action;
|