1 |
2 |
sonicwave |
----------------------------------------------------------------------------------
|
2 |
|
|
-- Company: University of Southern Denmark
|
3 |
|
|
-- Engineer: Simon Falsig
|
4 |
|
|
--
|
5 |
|
|
-- Create Date: 11/5/2010
|
6 |
|
|
-- Design Name TosNet
|
7 |
|
|
-- Module Name: app_master - Behavioral
|
8 |
|
|
-- File Name: app_master.vhd
|
9 |
|
|
-- Project Name: TosNet
|
10 |
|
|
-- Target Devices: Spartan3/6
|
11 |
|
|
-- Tool versions: Xilinx ISE 12.2
|
12 |
|
|
-- Description: The master discovery module performs master discovery and
|
13 |
|
|
-- setup during startup. The node with the lowest node_id is
|
14 |
|
|
-- designated as the master, and transmits this information to
|
15 |
|
|
-- all other nodes in the network.
|
16 |
|
|
--
|
17 |
|
|
-- Revision:
|
18 |
|
|
-- Revision 3.2 - Initial release
|
19 |
|
|
--
|
20 |
|
|
-- Copyright 2010
|
21 |
|
|
--
|
22 |
|
|
-- This module is free software: you can redistribute it and/or modify
|
23 |
|
|
-- it under the terms of the GNU Lesser General Public License as published by
|
24 |
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
25 |
|
|
-- (at your option) any later version.
|
26 |
|
|
--
|
27 |
|
|
-- This module is distributed in the hope that it will be useful,
|
28 |
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
29 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
30 |
|
|
-- GNU Lesser General Public License for more details.
|
31 |
|
|
--
|
32 |
|
|
-- You should have received a copy of the GNU Lesser General Public License
|
33 |
|
|
-- along with this module. If not, see <http://www.gnu.org/licenses/>.
|
34 |
|
|
----------------------------------------------------------------------------------
|
35 |
|
|
library IEEE;
|
36 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
37 |
|
|
use IEEE.STD_LOGIC_ARITH.ALL;
|
38 |
|
|
use IEEE.STD_LOGIC_UNSIGNED.ALL;
|
39 |
|
|
use work.commandpack.all;
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
entity tdl_app_master is
|
43 |
|
|
Port ( app_enable : in STD_LOGIC;
|
44 |
|
|
app_data_in : in STD_LOGIC_VECTOR(7 downto 0);
|
45 |
|
|
app_data_in_strobe : in STD_LOGIC;
|
46 |
|
|
app_data_out : out STD_LOGIC_VECTOR(7 downto 0);
|
47 |
|
|
app_data_out_strobe : out STD_LOGIC;
|
48 |
|
|
app_data_out_enable : out STD_LOGIC;
|
49 |
|
|
app_buffer_full : in STD_LOGIC;
|
50 |
|
|
app_packet_error : in STD_LOGIC;
|
51 |
|
|
app_force_packet_error : out STD_LOGIC;
|
52 |
|
|
app_cmd_valid : in STD_LOGIC;
|
53 |
|
|
app_sync_strobe : in STD_LOGIC;
|
54 |
|
|
app_is_master : out STD_LOGIC;
|
55 |
|
|
app_dsc_done : out STD_LOGIC;
|
56 |
|
|
app_node_id : in STD_LOGIC_VECTOR(3 downto 0);
|
57 |
|
|
app_clk : in STD_LOGIC;
|
58 |
|
|
app_reset : in STD_LOGIC);
|
59 |
|
|
end tdl_app_master;
|
60 |
|
|
|
61 |
|
|
architecture Behavioral of tdl_app_master is
|
62 |
|
|
|
63 |
|
|
type STATES is (OFF, IDLE, DSC, CMP, SET, SET_RESPOND, WAIT_FOR_SET, DONE);
|
64 |
|
|
|
65 |
|
|
signal state : STATES := OFF;
|
66 |
|
|
signal next_state : STATES := OFF;
|
67 |
|
|
|
68 |
|
|
signal last_data_in_strobe : STD_LOGIC;
|
69 |
|
|
signal lowest_current_id : STD_LOGIC_VECTOR(3 downto 0);
|
70 |
|
|
|
71 |
|
|
signal counter : STD_LOGIC_VECTOR(2 downto 0) := "000";
|
72 |
|
|
|
73 |
|
|
begin
|
74 |
|
|
|
75 |
|
|
process(app_clk)
|
76 |
|
|
begin
|
77 |
|
|
if(app_clk = '1' and app_clk'EVENT) then
|
78 |
|
|
if(app_reset = '1') then
|
79 |
|
|
state <= OFF;
|
80 |
|
|
else
|
81 |
|
|
state <= next_state;
|
82 |
|
|
end if;
|
83 |
|
|
|
84 |
|
|
if(app_buffer_full = '1') then
|
85 |
|
|
counter <= counter;
|
86 |
|
|
elsif(state = next_state) then
|
87 |
|
|
counter <= counter + 1;
|
88 |
|
|
else
|
89 |
|
|
counter <= "000";
|
90 |
|
|
end if;
|
91 |
|
|
|
92 |
|
|
case state is
|
93 |
|
|
|
94 |
|
|
when OFF =>
|
95 |
|
|
app_data_out_enable <= '0';
|
96 |
|
|
app_data_out_strobe <= '0';
|
97 |
|
|
app_data_out <= "00000000";
|
98 |
|
|
app_force_packet_error <= 'Z';
|
99 |
|
|
app_is_master <= '0';
|
100 |
|
|
app_dsc_done <= '0';
|
101 |
|
|
last_data_in_strobe <= '0';
|
102 |
|
|
lowest_current_id <= app_node_id;
|
103 |
|
|
|
104 |
|
|
when IDLE =>
|
105 |
|
|
app_data_out_enable <= '0';
|
106 |
|
|
app_data_out_strobe <= '0';
|
107 |
|
|
app_dsc_done <= '0';
|
108 |
|
|
app_is_master <= '0';
|
109 |
|
|
|
110 |
|
|
when DSC => --Transmit discovery packet
|
111 |
|
|
app_data_out_enable <= '1';
|
112 |
|
|
app_data_out <= CMD_MASTER_DSC & lowest_current_id;
|
113 |
|
|
app_data_out_strobe <= '1';
|
114 |
|
|
|
115 |
|
|
when CMP => --Compare received packets to lowest known id
|
116 |
|
|
app_data_out_enable <= '0';
|
117 |
|
|
app_data_out_strobe <= '0';
|
118 |
|
|
|
119 |
|
|
if(app_data_in_strobe = '1' and last_data_in_strobe = '0' and app_cmd_valid = '1') then
|
120 |
|
|
if(app_data_in(7 downto 4) = CMD_MASTER_DSC) then
|
121 |
|
|
if(app_data_in(3 downto 0) = app_node_id) then
|
122 |
|
|
app_is_master <= '1';
|
123 |
|
|
elsif(app_data_in(3 downto 0) < app_node_id) then
|
124 |
|
|
lowest_current_id <= app_data_in(3 downto 0);
|
125 |
|
|
end if;
|
126 |
|
|
end if;
|
127 |
|
|
end if;
|
128 |
|
|
|
129 |
|
|
when SET => --Transmit set packet (master only)
|
130 |
|
|
app_data_out_enable <= '1';
|
131 |
|
|
app_data_out <= CMD_MASTER_SET & lowest_current_id;
|
132 |
|
|
app_data_out_strobe <= '1';
|
133 |
|
|
|
134 |
|
|
when SET_RESPOND => --Forward set packet (slave only)
|
135 |
|
|
app_data_out_enable <= '1';
|
136 |
|
|
app_data_out <= app_data_in(7 downto 0);
|
137 |
|
|
app_data_out_strobe <= '1';
|
138 |
|
|
|
139 |
|
|
when WAIT_FOR_SET => --Wait until set is received again (master only)
|
140 |
|
|
app_data_out_enable <= '0';
|
141 |
|
|
app_data_out_strobe <= '0';
|
142 |
|
|
|
143 |
|
|
when DONE => --Done!
|
144 |
|
|
app_dsc_done <= '1';
|
145 |
|
|
app_data_out_enable <= '0';
|
146 |
|
|
app_data_out_strobe <= '0';
|
147 |
|
|
app_data_out <= "00000000";
|
148 |
|
|
app_force_packet_error <= 'Z';
|
149 |
|
|
|
150 |
|
|
end case;
|
151 |
|
|
|
152 |
|
|
last_data_in_strobe <= app_data_in_strobe;
|
153 |
|
|
|
154 |
|
|
end if;
|
155 |
|
|
end process;
|
156 |
|
|
|
157 |
|
|
|
158 |
|
|
process(state, app_enable, app_data_in, app_data_in_strobe, last_data_in_strobe, app_node_id, app_cmd_valid)
|
159 |
|
|
begin
|
160 |
|
|
case state is
|
161 |
|
|
when OFF =>
|
162 |
|
|
if(app_enable = '1') then
|
163 |
|
|
next_state <= IDLE;
|
164 |
|
|
else
|
165 |
|
|
next_state <= OFF;
|
166 |
|
|
end if;
|
167 |
|
|
when IDLE =>
|
168 |
|
|
next_state <= DSC;
|
169 |
|
|
when DSC =>
|
170 |
|
|
next_state <= CMP;
|
171 |
|
|
when CMP =>
|
172 |
|
|
next_state <= CMP;
|
173 |
|
|
|
174 |
|
|
if(app_data_in_strobe = '1' and last_data_in_strobe = '0' and app_cmd_valid = '1') then
|
175 |
|
|
if(app_data_in(7 downto 4) = CMD_MASTER_DSC) then
|
176 |
|
|
if(app_data_in(3 downto 0) = app_node_id) then
|
177 |
|
|
next_state <= SET;
|
178 |
|
|
else
|
179 |
|
|
next_state <= DSC;
|
180 |
|
|
end if;
|
181 |
|
|
elsif(app_data_in(7 downto 4) = CMD_MASTER_SET) then
|
182 |
|
|
if(app_data_in(3 downto 0) > app_node_id) then --Make a quick sanity check on the received master node id, if it is larger than the id of this node, then something is wrong...
|
183 |
|
|
next_state <= IDLE;
|
184 |
|
|
else
|
185 |
|
|
next_state <= SET_RESPOND;
|
186 |
|
|
end if;
|
187 |
|
|
end if;
|
188 |
|
|
end if;
|
189 |
|
|
when SET =>
|
190 |
|
|
next_state <= WAIT_FOR_SET;
|
191 |
|
|
|
192 |
|
|
when SET_RESPOND =>
|
193 |
|
|
next_state <= DONE;
|
194 |
|
|
|
195 |
|
|
when WAIT_FOR_SET =>
|
196 |
|
|
next_state <= WAIT_FOR_SET;
|
197 |
|
|
|
198 |
|
|
if(app_data_in_strobe = '1' and last_data_in_strobe = '0' and app_cmd_valid = '1') then
|
199 |
|
|
if(app_data_in(7 downto 4) = CMD_MASTER_SET) then
|
200 |
|
|
next_state <= DONE;
|
201 |
|
|
end if;
|
202 |
|
|
end if;
|
203 |
|
|
|
204 |
|
|
when DONE =>
|
205 |
|
|
next_state <= DONE;
|
206 |
|
|
end case;
|
207 |
|
|
end process;
|
208 |
|
|
|
209 |
|
|
|
210 |
|
|
end Behavioral;
|
211 |
|
|
|