-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathws2812Controller.vhd
284 lines (216 loc) · 5.7 KB
/
ws2812Controller.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use IEEE.math_real.all;
entity ws2812Controller is
port(
clk : in std_logic;
sclr : in std_logic;
triggerWriteOut : in std_logic;
load_byte : in std_logic;
byteIn : in std_logic_vector(7 downto 0);
serialOut : out std_LOGIC
);
end entity;
architecture arch of ws2812Controller is
signal i_byteIn : std_logic_vector(7 downto 0);
signal fifo_sclr : std_logic;
signal fifo_rdreq : std_logic;
signal fifo_wreq : std_logic;
signal fifo_empty : std_logic;
signal fifo_full : std_logic;
signal fifo_q : std_logic_vector(7 downto 0);
signal fifo_usedw : std_LOGIC_VECTOR(7 downto 0);
component fifo is
PORT
(
clock : IN STD_LOGIC ;
data : IN STD_LOGIC_VECTOR (7 DOWNTO 0);
rdreq : IN STD_LOGIC ;
sclr : IN STD_LOGIC ;
wrreq : IN STD_LOGIC ;
empty : OUT STD_LOGIC ;
full : OUT STD_LOGIC ;
q : OUT STD_LOGIC_VECTOR (7 DOWNTO 0);
usedw : OUT STD_LOGIC_VECTOR (7 DOWNTO 0)
);
end component;
signal shiftReg_load : std_logic;
signal shiftReg_enable : std_logic;
signal shiftReg_bit : std_logic;
component shiftreg_piso is
port(
clk : in std_logic;
sclr : in std_logic;
load : in std_logic;
enable_out : in std_logic;
data : in std_logic_vector(7 downto 0);
shiftpos : out unsigned (2 downto 0);
shiftout : out std_logic
);
end component;
signal serialiser_reqw : std_logic;
signal serialiser_req_out : std_logic;
signal serialiserBusy : std_logic;
component ws2812b_serialiser is
port(
clk : in std_logic;
reqw : in std_logic;
bit_value_in : in std_logic;
req_out : in std_logic;
output : out std_logic;
busy_flag : out std_logic
);
end component;
type top_level_state_type is (idle, outputting);
signal top_level_state : top_level_state_type;
type fifo_to_shift_state_t is (idle, loading, shift_ready);
signal fifo_to_shift_state : fifo_to_shift_state_t;
signal bits_shifted_cnt : integer range 0 to 7;
signal bits_shifted_cnt_us : unsigned (2 downto 0);
signal serial_output : std_logic;
type shift_out_to_ser_t is (idle,loading,outputting,bit_done,byte_done);
signal shift_out_to_ser_state : shift_out_to_ser_t;
begin
fifo_load_proc : process(clk)
begin
if(rising_edge(clk)) then
if(fifo_full = '0' and load_byte = '1') then
fifo_wreq <= '1';
else
fifo_wreq <= '0';
end if;
end if;
end process;
triggerWriteProc : process(clk)
begin
if(rising_edge(clk))then
case top_level_state is
when idle =>
if(triggerWriteOut = '1')then
top_level_state <= outputting;
end if;
when outputting =>
if(fifo_usedw = x"00") then
top_level_state <= idle;
end if;
when others =>
--nothing
end case;
end if;
end process;
fifoIn : fifo
port map(
clock => clk,
data => byteIn,
rdreq => fifo_rdreq,
sclr => fifo_sclr,
wrreq => fifo_wreq,
empty => fifo_empty,
full => fifo_full,
q => fifo_q,
usedw => fifo_usedw
);
fifo_t_shift_set : process(clk)
begin
if(rising_edge(clk)) then
case fifo_to_shift_state is
when idle =>
if(top_level_state = outputting and shift_out_to_ser_state = idle) then
fifo_to_shift_state <= shift_ready;
end if;
when loading =>
fifo_to_shift_state <= shift_ready;
when shift_ready =>
if(bits_shifted_cnt >= 7) then
fifo_to_shift_state <= idle;
end if;
end case;
end if;
end process;
fifo2shift_output : process(fifo_to_shift_state)
begin
case fifo_to_shift_state is
when idle =>
fifo_rdreq <= '0';
shiftReg_load <= '0';
when loading =>
fifo_rdreq <= '1';
shiftReg_load <= '1';
when shift_ready =>
fifo_rdreq <= '0';
shiftReg_load <= '0';
end case;
end process;
shift_to_serial_set : process(clk)
begin
if(rising_edge(clk)) then
case shift_out_to_ser_state is
when idle =>
if(serialiserBusy = '0' and top_level_state = outputting and fifo_to_shift_state = shift_ready)then
shift_out_to_ser_state <= loading;
end if;
when loading =>
shift_out_to_ser_state <= outputting;
when outputting =>
if(serialiserBusy = '0') then
shift_out_to_ser_state <= bit_done;
end if;
when bit_done =>
if(bits_shifted_cnt = 7) then
shift_out_to_ser_state <= byte_done;
else
shift_out_to_ser_state <= loading;
end if;
when byte_done =>
shift_out_to_ser_state <= idle;
end case;
end if;
end process;
process(shift_out_to_ser_state)
begin
case shift_out_to_ser_state is
when idle =>
shiftReg_enable <= '0';
serialiser_reqw <= '0';
serialiser_req_out <= '0';
when loading =>
shiftReg_enable <= '1';
serialiser_reqw <= '1';
serialiser_req_out <= '0';
when outputting =>
shiftReg_enable <= '0';
serialiser_reqw <= '0';
serialiser_req_out <= '1';
when bit_done =>
shiftReg_enable <= '0';
serialiser_reqw <= '0';
serialiser_req_out <= '0';
when byte_done =>
shiftReg_enable <= '0';
serialiser_reqw <= '0';
serialiser_req_out <= '0';
end case;
end process;
shiftOutReg : shiftreg_piso
port map(
clk => clk,
sclr => sclr,
load => shiftReg_load,
data => fifo_q,
shiftout => shiftReg_bit,
enable_out => shiftReg_enable,
shiftpos => bits_shifted_cnt_us
);
bits_shifted_cnt <= 7 - to_integer(bits_shifted_cnt_us);
serial_out : ws2812b_serialiser
port map(
clk => clk,
reqw => serialiser_reqw,
bit_value_in => shiftReg_bit,
req_out => serialiser_req_out,
output => serial_output,
busy_flag => serialiserBusy
);
serialOut <= serial_output;
end architecture;