1 |
145 |
lanttu |
-------------------------------------------------------------------------------
|
2 |
|
|
-- Title : Converts event in button input to a message
|
3 |
|
|
-- Project : Nocbench, Funbase
|
4 |
|
|
-------------------------------------------------------------------------------
|
5 |
|
|
-- File : button_messenger.vhd
|
6 |
|
|
-- Author : Erno Salminen
|
7 |
|
|
-- Company : TUT
|
8 |
|
|
-- Last update: 2012-02-13
|
9 |
|
|
-- Platform :
|
10 |
|
|
-------------------------------------------------------------------------------
|
11 |
|
|
-- Description: On DE2 boards, the push buttons give active-low pulse when
|
12 |
|
|
-- pressed. Therefore, this unit detects falling edge. The sent
|
13 |
|
|
-- message is of form address+data. No decoding in data, just the
|
14 |
|
|
-- raw key input. Currently the destination address is set with
|
15 |
|
|
-- generic parameter.
|
16 |
|
|
-------------------------------------------------------------------------------
|
17 |
|
|
-- Revisions :
|
18 |
|
|
-- Date Version Author Description
|
19 |
|
|
-- 2012-02-10 1.0 ES First version
|
20 |
|
|
-------------------------------------------------------------------------------
|
21 |
|
|
|
22 |
|
|
-------------------------------------------------------------------------------
|
23 |
|
|
-- Funbase IP library Copyright (C) 2011 TUT Department of Computer Systems
|
24 |
|
|
--
|
25 |
|
|
-- This source file may be used and distributed without
|
26 |
|
|
-- restriction provided that this copyright statement is not
|
27 |
|
|
-- removed from the file and that any derivative work contains
|
28 |
|
|
-- the original copyright notice and the associated disclaimer.
|
29 |
|
|
--
|
30 |
|
|
-- This source file is free software; you can redistribute it
|
31 |
|
|
-- and/or modify it under the terms of the GNU Lesser General
|
32 |
|
|
-- Public License as published by the Free Software Foundation;
|
33 |
|
|
-- either version 2.1 of the License, or (at your option) any
|
34 |
|
|
-- later version.
|
35 |
|
|
--
|
36 |
|
|
-- This source is distributed in the hope that it will be
|
37 |
|
|
-- useful, but WITHOUT ANY WARRANTY; without even the implied
|
38 |
|
|
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
39 |
|
|
-- PURPOSE. See the GNU Lesser General Public License for more
|
40 |
|
|
-- details.
|
41 |
|
|
--
|
42 |
|
|
-- You should have received a copy of the GNU Lesser General
|
43 |
|
|
-- Public License along with this source; if not, download it
|
44 |
|
|
-- from http://www.opencores.org/lgpl.shtml
|
45 |
|
|
-------------------------------------------------------------------------------
|
46 |
|
|
library ieee;
|
47 |
|
|
use ieee.std_logic_1164.all;
|
48 |
|
|
use ieee.numeric_std.all;
|
49 |
|
|
|
50 |
|
|
entity button_messenger is
|
51 |
|
|
generic (
|
52 |
|
|
n_buttons_g : integer := 4; -- less or eq to data_width_g
|
53 |
|
|
data_width_g : integer := 32; -- in bits
|
54 |
|
|
comm_width_g : integer := 5; -- in bits
|
55 |
|
|
write_command_g : integer := 2;
|
56 |
|
|
dst_addr_g : integer -- where to send the msg
|
57 |
|
|
);
|
58 |
|
|
port (
|
59 |
|
|
clk : in std_logic;
|
60 |
|
|
rst_n : in std_logic;
|
61 |
|
|
|
62 |
|
|
tx_av_out : out std_logic;
|
63 |
|
|
tx_data_out : out std_logic_vector (data_width_g -1 downto 0);
|
64 |
|
|
tx_comm_out : out std_logic_vector (comm_width_g -1 downto 0);
|
65 |
|
|
tx_we_out : out std_logic;
|
66 |
|
|
tx_full_in : in std_logic;
|
67 |
|
|
|
68 |
|
|
buttons_in : in std_logic_vector (n_buttons_g-1 downto 0)
|
69 |
|
|
);
|
70 |
|
|
|
71 |
|
|
end button_messenger;
|
72 |
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
|
|
architecture rtl of button_messenger is
|
76 |
|
|
|
77 |
|
|
-- Register for edge detection
|
78 |
|
|
signal buttons_r : std_logic_vector (n_buttons_g-1 downto 0);
|
79 |
|
|
signal buttons2_r : std_logic_vector (n_buttons_g-1 downto 0);
|
80 |
|
|
signal buttons3_r : std_logic_vector (n_buttons_g-1 downto 0);
|
81 |
|
|
|
82 |
|
|
-- Simple state machine
|
83 |
|
|
type state_type is (idle, addr, data);
|
84 |
|
|
signal state_r : state_type;
|
85 |
|
|
|
86 |
|
|
begin -- rtl
|
87 |
|
|
|
88 |
|
|
tx_comm_out <= std_logic_vector (to_unsigned(write_command_g, comm_width_g));
|
89 |
|
|
|
90 |
|
|
|
91 |
|
|
-- Loop through 3 states, no branching
|
92 |
|
|
main_p : process (clk, rst_n)
|
93 |
|
|
variable falling_edge_found_v : integer := 0;
|
94 |
|
|
|
95 |
|
|
begin -- process main_p
|
96 |
|
|
if rst_n = '0' then -- asynchronous reset (active low)
|
97 |
|
|
|
98 |
|
|
tx_av_out <= '0';
|
99 |
|
|
tx_data_out <= (others => '0');
|
100 |
|
|
tx_we_out <= '0';
|
101 |
|
|
buttons_r <= (others => '0');
|
102 |
|
|
state_r <= idle;
|
103 |
|
|
|
104 |
|
|
elsif clk'event and clk = '1' then -- rising clock edge
|
105 |
|
|
|
106 |
|
|
buttons_r <= buttons_in;
|
107 |
|
|
buttons2_r <= buttons_r;
|
108 |
|
|
buttons3_r <= buttons2_r;
|
109 |
|
|
|
110 |
|
|
case state_r is
|
111 |
|
|
-----------------------------------------------------------------------
|
112 |
|
|
-- IDLE
|
113 |
|
|
-----------------------------------------------------------------------
|
114 |
|
|
when idle =>
|
115 |
|
|
tx_av_out <= '0';
|
116 |
|
|
tx_data_out <= (others => '0');
|
117 |
|
|
tx_we_out <= '0';
|
118 |
|
|
|
119 |
|
|
-- Edge detection
|
120 |
|
|
falling_edge_found_v := 0;
|
121 |
|
|
for i in 0 to n_buttons_g-1 loop
|
122 |
|
|
if buttons2_r(i) = '0' and buttons3_r (i) = '1' then
|
123 |
|
|
falling_edge_found_v := 1;
|
124 |
|
|
end if;
|
125 |
|
|
end loop; -- i
|
126 |
|
|
|
127 |
|
|
if falling_edge_found_v = 1 then -- Falling edge
|
128 |
|
|
--if buttons_in /= buttons_r then -- Any edge
|
129 |
|
|
state_r <= addr;
|
130 |
|
|
end if;
|
131 |
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
---------------------------------------------------------------------
|
135 |
|
|
-- ADDR
|
136 |
|
|
---------------------------------------------------------------------
|
137 |
|
|
when addr =>
|
138 |
|
|
tx_av_out <= '1';
|
139 |
|
|
tx_data_out <= std_logic_vector (to_unsigned (dst_addr_g, data_width_g));
|
140 |
|
|
tx_we_out <= '1';
|
141 |
|
|
if tx_full_in /= '1' then
|
142 |
|
|
state_r <= data;
|
143 |
|
|
end if;
|
144 |
|
|
|
145 |
|
|
|
146 |
|
|
---------------------------------------------------------------------
|
147 |
|
|
-- DATA
|
148 |
|
|
---------------------------------------------------------------------
|
149 |
|
|
when data =>
|
150 |
|
|
tx_av_out <= '0';
|
151 |
|
|
tx_data_out <= (others => '0');
|
152 |
|
|
tx_data_out (n_buttons_g-1 downto 0) <= buttons_in;
|
153 |
|
|
tx_we_out <= '1';
|
154 |
|
|
if tx_full_in /= '1' then
|
155 |
|
|
state_r <= idle;
|
156 |
|
|
end if;
|
157 |
|
|
|
158 |
|
|
|
159 |
|
|
when others =>
|
160 |
|
|
state_r <= idle;
|
161 |
|
|
|
162 |
|
|
end case;
|
163 |
|
|
|
164 |
|
|
|
165 |
|
|
end if;
|
166 |
|
|
end process main_p;
|
167 |
|
|
|
168 |
|
|
end rtl;
|