OpenCores
URL https://opencores.org/ocsvn/ft245r_interface/ft245r_interface/trunk

Subversion Repositories ft245r_interface

[/] [ft245r_interface/] [trunk/] [ft245rl_interface.vhd] - Blame information for rev 5

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
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 5 pradd
        busy            <= '0' when c_state = IDLE else
96
                                        '1';
97 4 pradd
 
98
        -- Reset the FT245R chip on powerup.
99
        nrst            <= '0' when c_state = INIT else
100
                                        '1';
101 2 pradd
 
102 4 pradd
        nrd             <= '0' when c_state = READ_BYTE or c_state = READ_BYTE1 or
103
                                                                (c_state = DO_DELAY and (n_state = READ_BYTE1 or n_state = READ_BYTE2)) else
104
                                        '1';
105
 
106
        nwr             <= '1' when (c_state = WRITE_BYTE and ntxe = '0') or (c_state = DO_DELAY and n_state = WRITE_BYTE1) else
107
                                        '0';
108
 
109
        we                      <= '1' when (c_state = WRITE_BYTE and ntxe = '0') or c_state = WRITE_BYTE1 or (c_state = DO_DELAY and (n_state = WRITE_BYTE1 or n_state = WRITE_BYTE2)) else
110
                                        '0';
111
 
112
        process(clk, reset, nrxf, ntxe, nce, data_available, fetch_next_byte, do_write)
113 2 pradd
        begin
114
                if(reset = '0')then
115
                        c_state         <= INIT;
116
                elsif(rising_edge(clk) and nce = '0')then
117
 
118
                        case c_state is
119
                                -- The module enters this state on powerup or when 'reset' is low.
120
                                when INIT =>
121
                                                                        delay_cnt               <= 0;
122
                                                                        current_delay   <= 0;
123
                                                                        c_state                 <= IDLE;
124
 
125
                                -- This is the "main loop"
126
                                when IDLE =>
127
                                                                        -- If this condition is true, we may safely read another byte from FT245RL's FIFO
128
                                                                        if(nrxf = '0' and data_available = '0')then
129
                                                                                c_state         <= READ_BYTE;
130
 
131
                                                                        -- We have to clear 'data_available' when the client module is requesting a new byte
132
                                                                        elsif(fetch_next_byte = '1')then
133
                                                                                data_available  <= '0';
134 5 pradd
                                                                                if(nrxf = '0')then
135
                                                                                        c_state         <= READ_BYTE;
136
                                                                                else
137
                                                                                        c_state         <= IDLE;
138
                                                                                end if;
139 2 pradd
 
140
                                                                        -- Well, here we simply write a byte to FT245RL's data bus
141
                                                                        elsif(do_write = '1')then
142
                                                                                c_state         <= WRITE_BYTE;
143
                                                                        end if;
144
 
145
 
146
                                -- Read one byte from the device
147
                                when READ_BYTE =>
148
                                                                        current_delay   <= t3_delay;
149
                                                                        c_state                 <= DO_DELAY;
150
                                                                        n_state                 <= READ_BYTE1;
151
 
152
                                when READ_BYTE1 =>
153
                                                                        current_delay   <= t1_delay - t3_delay;
154
                                                                        c_state                 <= DO_DELAY;
155
                                                                        n_state                 <= READ_BYTE2;
156
 
157
                                when READ_BYTE2 =>
158
                                                                        data_out                        <= in_buff;
159
                                                                        current_delay   <= t5_delay;
160
                                                                        c_state                 <= DO_DELAY;
161
                                                                        n_state                 <= READ_BYTE3;
162
 
163
                                when READ_BYTE3 =>
164
                                                                        current_delay   <= t2_delay;
165
                                                                        c_state                 <= DO_DELAY;
166
                                                                        n_state                 <= IDLE;
167
                                                                        data_available  <= '1';
168 4 pradd
 
169 2 pradd
                                -- Write one byte to the device
170
                                when WRITE_BYTE =>
171
                                                                        if(ntxe = '0')then
172
                                                                                current_delay<= t7_delay;
173
                                                                                c_state         <= DO_DELAY;
174
                                                                                n_state         <= WRITE_BYTE1;
175
                                                                                out_buff                <= data_in;
176
                                                                        else
177
                                                                                c_state         <= WRITE_BYTE;
178
                                                                        end if;
179
 
180
                                when WRITE_BYTE1 =>
181
                                                                        current_delay   <= t11_delay;
182
                                                                        c_state                 <= DO_DELAY;
183
                                                                        n_state                 <= WRITE_BYTE2;
184
 
185
                                when WRITE_BYTE2 =>
186
                                                                        current_delay   <= t12_delay;
187
                                                                        c_state                 <=DO_DELAY;
188
                                                                        n_state                 <= WRITE_BYTE3;
189
 
190
                                when WRITE_BYTE3 =>
191
                                                                        c_state                 <= IDLE;
192
 
193
                                when DO_DELAY =>
194
                                                                        if(delay_cnt < current_delay)then
195
                                                                                delay_cnt       <= delay_cnt + 1;
196
                                                                        else
197
                                                                                c_state         <= n_state;
198
                                                                                delay_cnt       <= 0;
199
                                                                        end if;
200
 
201
                                when others =>
202
                                                                        c_state         <= INIT;
203
                        end case;       -- c_state
204
                end if;
205
        end process;
206
 
207
end action;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.