1 |
52 |
zero_gravi |
-- #################################################################################################
|
2 |
|
|
-- # << NEORV32 - Smart LED (WS2811/WS2812) Interface (NEOLED) >> #
|
3 |
|
|
-- # ********************************************************************************************* #
|
4 |
62 |
zero_gravi |
-- # Hardware interface for direct control of "smart LEDs" using an asynchronous serial data #
|
5 |
52 |
zero_gravi |
-- # line. Compatible with the WS2811 and WS2812 LEDs. #
|
6 |
|
|
-- # #
|
7 |
62 |
zero_gravi |
-- # NeoPixel-compatible, RGB (24-bit) and RGBW (32-bit) modes supported (in "parallel") #
|
8 |
|
|
-- # (TM) "NeoPixel" is a trademark of Adafruit Industries. #
|
9 |
52 |
zero_gravi |
-- # #
|
10 |
62 |
zero_gravi |
-- # The interface uses a programmable carrier frequency (800 KHz for the WS2812 LEDs) #
|
11 |
52 |
zero_gravi |
-- # configurable via the control register's clock prescaler bits (ctrl_clksel*_c) and the period #
|
12 |
|
|
-- # length configuration bits (ctrl_t_tot_*_c). "high-times" for sending a ZERO or a ONE bit are #
|
13 |
|
|
-- # configured using the ctrl_t_0h_*_c and ctrl_t_1h_*_c bits, respectively. 32-bit transfers #
|
14 |
|
|
-- # (for RGBW modules) and 24-bit transfers (for RGB modules) are supported via ctrl_mode__c. #
|
15 |
|
|
-- # #
|
16 |
65 |
zero_gravi |
-- # The device features a TX buffer (FIFO) with <FIFO_DEPTH> entries with configurable interrupt. #
|
17 |
52 |
zero_gravi |
-- # ********************************************************************************************* #
|
18 |
|
|
-- # BSD 3-Clause License #
|
19 |
|
|
-- # #
|
20 |
|
|
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
|
21 |
|
|
-- # #
|
22 |
|
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
23 |
|
|
-- # permitted provided that the following conditions are met: #
|
24 |
|
|
-- # #
|
25 |
|
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
26 |
|
|
-- # conditions and the following disclaimer. #
|
27 |
|
|
-- # #
|
28 |
|
|
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
29 |
|
|
-- # conditions and the following disclaimer in the documentation and/or other materials #
|
30 |
|
|
-- # provided with the distribution. #
|
31 |
|
|
-- # #
|
32 |
|
|
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
33 |
|
|
-- # endorse or promote products derived from this software without specific prior written #
|
34 |
|
|
-- # permission. #
|
35 |
|
|
-- # #
|
36 |
|
|
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
37 |
|
|
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
38 |
|
|
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
39 |
|
|
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
40 |
|
|
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
41 |
|
|
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
42 |
|
|
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
43 |
|
|
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
44 |
|
|
-- # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
45 |
|
|
-- # ********************************************************************************************* #
|
46 |
|
|
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
47 |
|
|
-- #################################################################################################
|
48 |
|
|
|
49 |
|
|
library ieee;
|
50 |
|
|
use ieee.std_logic_1164.all;
|
51 |
|
|
use ieee.numeric_std.all;
|
52 |
|
|
|
53 |
|
|
library neorv32;
|
54 |
|
|
use neorv32.neorv32_package.all;
|
55 |
|
|
|
56 |
|
|
entity neorv32_neoled is
|
57 |
62 |
zero_gravi |
generic (
|
58 |
|
|
FIFO_DEPTH : natural -- TX FIFO depth (1..32k, power of two)
|
59 |
|
|
);
|
60 |
52 |
zero_gravi |
port (
|
61 |
|
|
-- host access --
|
62 |
|
|
clk_i : in std_ulogic; -- global clock line
|
63 |
|
|
addr_i : in std_ulogic_vector(31 downto 0); -- address
|
64 |
|
|
rden_i : in std_ulogic; -- read enable
|
65 |
|
|
wren_i : in std_ulogic; -- write enable
|
66 |
|
|
data_i : in std_ulogic_vector(31 downto 0); -- data in
|
67 |
|
|
data_o : out std_ulogic_vector(31 downto 0); -- data out
|
68 |
|
|
ack_o : out std_ulogic; -- transfer acknowledge
|
69 |
|
|
-- clock generator --
|
70 |
|
|
clkgen_en_o : out std_ulogic; -- enable clock generator
|
71 |
|
|
clkgen_i : in std_ulogic_vector(07 downto 0);
|
72 |
|
|
-- interrupt --
|
73 |
|
|
irq_o : out std_ulogic; -- interrupt request
|
74 |
|
|
-- NEOLED output --
|
75 |
|
|
neoled_o : out std_ulogic -- serial async data line
|
76 |
|
|
);
|
77 |
|
|
end neorv32_neoled;
|
78 |
|
|
|
79 |
|
|
architecture neorv32_neoled_rtl of neorv32_neoled is
|
80 |
|
|
|
81 |
|
|
-- IO space: module base address --
|
82 |
|
|
constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
|
83 |
|
|
constant lo_abb_c : natural := index_size_f(neoled_size_c); -- low address boundary bit
|
84 |
|
|
|
85 |
|
|
-- access control --
|
86 |
|
|
signal acc_en : std_ulogic; -- module access enable
|
87 |
|
|
signal addr : std_ulogic_vector(31 downto 0); -- access address
|
88 |
|
|
signal wren : std_ulogic; -- word write enable
|
89 |
|
|
signal rden : std_ulogic; -- read enable
|
90 |
|
|
|
91 |
|
|
-- Control register bits --
|
92 |
68 |
zero_gravi |
constant ctrl_en_c : natural := 0; -- r/w: module enable
|
93 |
65 |
zero_gravi |
constant ctrl_mode_c : natural := 1; -- r/w: 0 = 24-bit RGB mode, 1 = 32-bit RGBW mode
|
94 |
|
|
constant ctrl_strobe_c : natural := 2; -- r/w: 0 = send normal data, 1 = send LED strobe command (RESET) on data write
|
95 |
62 |
zero_gravi |
--
|
96 |
65 |
zero_gravi |
constant ctrl_clksel0_c : natural := 3; -- r/w: prescaler select bit 0
|
97 |
|
|
constant ctrl_clksel1_c : natural := 4; -- r/w: prescaler select bit 1
|
98 |
|
|
constant ctrl_clksel2_c : natural := 5; -- r/w: prescaler select bit 2
|
99 |
52 |
zero_gravi |
--
|
100 |
65 |
zero_gravi |
constant ctrl_bufs_0_c : natural := 6; -- r/-: log2(FIFO_DEPTH) bit 0
|
101 |
|
|
constant ctrl_bufs_1_c : natural := 7; -- r/-: log2(FIFO_DEPTH) bit 1
|
102 |
|
|
constant ctrl_bufs_2_c : natural := 8; -- r/-: log2(FIFO_DEPTH) bit 2
|
103 |
|
|
constant ctrl_bufs_3_c : natural := 9; -- r/-: log2(FIFO_DEPTH) bit 3
|
104 |
52 |
zero_gravi |
--
|
105 |
65 |
zero_gravi |
constant ctrl_t_tot_0_c : natural := 10; -- r/w: pulse-clock ticks per total period bit 0
|
106 |
|
|
constant ctrl_t_tot_1_c : natural := 11; -- r/w: pulse-clock ticks per total period bit 1
|
107 |
|
|
constant ctrl_t_tot_2_c : natural := 12; -- r/w: pulse-clock ticks per total period bit 2
|
108 |
|
|
constant ctrl_t_tot_3_c : natural := 13; -- r/w: pulse-clock ticks per total period bit 3
|
109 |
|
|
constant ctrl_t_tot_4_c : natural := 14; -- r/w: pulse-clock ticks per total period bit 4
|
110 |
52 |
zero_gravi |
--
|
111 |
65 |
zero_gravi |
constant ctrl_t_0h_0_c : natural := 15; -- r/w: pulse-clock ticks per ZERO high-time bit 0
|
112 |
|
|
constant ctrl_t_0h_1_c : natural := 16; -- r/w: pulse-clock ticks per ZERO high-time bit 1
|
113 |
|
|
constant ctrl_t_0h_2_c : natural := 17; -- r/w: pulse-clock ticks per ZERO high-time bit 2
|
114 |
|
|
constant ctrl_t_0h_3_c : natural := 18; -- r/w: pulse-clock ticks per ZERO high-time bit 3
|
115 |
|
|
constant ctrl_t_0h_4_c : natural := 19; -- r/w: pulse-clock ticks per ZERO high-time bit 4
|
116 |
52 |
zero_gravi |
--
|
117 |
65 |
zero_gravi |
constant ctrl_t_1h_0_c : natural := 20; -- r/w: pulse-clock ticks per ONE high-time bit 0
|
118 |
|
|
constant ctrl_t_1h_1_c : natural := 21; -- r/w: pulse-clock ticks per ONE high-time bit 1
|
119 |
|
|
constant ctrl_t_1h_2_c : natural := 22; -- r/w: pulse-clock ticks per ONE high-time bit 2
|
120 |
|
|
constant ctrl_t_1h_3_c : natural := 23; -- r/w: pulse-clock ticks per ONE high-time bit 3
|
121 |
|
|
constant ctrl_t_1h_4_c : natural := 24; -- r/w: pulse-clock ticks per ONE high-time bit 4
|
122 |
52 |
zero_gravi |
--
|
123 |
65 |
zero_gravi |
constant ctrl_irq_conf_c : natural := 27; -- r/w: interrupt config: 1=IRQ when buffer is empty, 0=IRQ when buffer is half-empty
|
124 |
|
|
constant ctrl_tx_empty_c : natural := 28; -- r/-: TX FIFO is empty
|
125 |
|
|
constant ctrl_tx_half_c : natural := 29; -- r/-: TX FIFO is at least half-full
|
126 |
|
|
constant ctrl_tx_full_c : natural := 30; -- r/-: TX FIFO is full
|
127 |
|
|
constant ctrl_tx_busy_c : natural := 31; -- r/-: serial TX engine busy when set
|
128 |
52 |
zero_gravi |
|
129 |
|
|
-- control register --
|
130 |
|
|
type ctrl_t is record
|
131 |
|
|
enable : std_ulogic;
|
132 |
|
|
mode : std_ulogic;
|
133 |
62 |
zero_gravi |
strobe : std_ulogic;
|
134 |
52 |
zero_gravi |
clk_prsc : std_ulogic_vector(2 downto 0);
|
135 |
65 |
zero_gravi |
irq_conf : std_ulogic;
|
136 |
52 |
zero_gravi |
-- pulse config --
|
137 |
|
|
t_total : std_ulogic_vector(4 downto 0);
|
138 |
|
|
t0_high : std_ulogic_vector(4 downto 0);
|
139 |
|
|
t1_high : std_ulogic_vector(4 downto 0);
|
140 |
|
|
end record;
|
141 |
|
|
signal ctrl : ctrl_t;
|
142 |
|
|
|
143 |
|
|
-- transmission buffer --
|
144 |
|
|
type tx_buffer_t is record
|
145 |
65 |
zero_gravi |
we : std_ulogic; -- write enable
|
146 |
|
|
re : std_ulogic; -- read enable
|
147 |
|
|
clear : std_ulogic; -- sync reset, high-active
|
148 |
|
|
wdata : std_ulogic_vector(31+2 downto 0); -- write data (excluding mode)
|
149 |
|
|
rdata : std_ulogic_vector(31+2 downto 0); -- read data (including mode)
|
150 |
|
|
avail : std_ulogic; -- data available?
|
151 |
|
|
free : std_ulogic; -- free entry available?
|
152 |
|
|
half : std_ulogic; -- half full
|
153 |
52 |
zero_gravi |
end record;
|
154 |
|
|
signal tx_buffer : tx_buffer_t;
|
155 |
|
|
|
156 |
68 |
zero_gravi |
-- interrupt generator --
|
157 |
|
|
type irq_t is record
|
158 |
69 |
zero_gravi |
set : std_ulogic;
|
159 |
|
|
buf : std_ulogic_vector(1 downto 0);
|
160 |
68 |
zero_gravi |
end record;
|
161 |
|
|
signal irq : irq_t;
|
162 |
|
|
|
163 |
52 |
zero_gravi |
-- serial transmission engine --
|
164 |
62 |
zero_gravi |
type serial_state_t is (S_IDLE, S_INIT, S_GETBIT, S_PULSE, S_STROBE);
|
165 |
52 |
zero_gravi |
type serial_t is record
|
166 |
|
|
-- state control --
|
167 |
62 |
zero_gravi |
state : serial_state_t;
|
168 |
|
|
mode : std_ulogic;
|
169 |
68 |
zero_gravi |
done : std_ulogic;
|
170 |
62 |
zero_gravi |
busy : std_ulogic;
|
171 |
|
|
bit_cnt : std_ulogic_vector(5 downto 0);
|
172 |
52 |
zero_gravi |
-- shift register --
|
173 |
62 |
zero_gravi |
sreg : std_ulogic_vector(31 downto 0);
|
174 |
|
|
next_bit : std_ulogic; -- next bit to send
|
175 |
52 |
zero_gravi |
-- pulse generator --
|
176 |
62 |
zero_gravi |
pulse_clk : std_ulogic; -- pulse cycle "clock"
|
177 |
|
|
pulse_cnt : std_ulogic_vector(4 downto 0);
|
178 |
|
|
t_high : std_ulogic_vector(4 downto 0);
|
179 |
|
|
strobe_cnt : std_ulogic_vector(6 downto 0);
|
180 |
|
|
tx_out : std_ulogic;
|
181 |
52 |
zero_gravi |
end record;
|
182 |
|
|
signal serial : serial_t;
|
183 |
|
|
|
184 |
|
|
begin
|
185 |
|
|
|
186 |
|
|
-- Sanity Checks --------------------------------------------------------------------------
|
187 |
|
|
-- -------------------------------------------------------------------------------------------
|
188 |
62 |
zero_gravi |
assert not ((is_power_of_two_f(FIFO_DEPTH) = false) or (FIFO_DEPTH < 1) or (FIFO_DEPTH > 32768)) report
|
189 |
|
|
"NEORV32 PROCESSOR CONFIG ERROR! Invalid <NEOLED.FIFO_DEPTH> buffer size configuration (1..32k)!" severity error;
|
190 |
52 |
zero_gravi |
|
191 |
|
|
|
192 |
|
|
-- Access Control -------------------------------------------------------------------------
|
193 |
|
|
-- -------------------------------------------------------------------------------------------
|
194 |
|
|
acc_en <= '1' when (addr_i(hi_abb_c downto lo_abb_c) = neoled_base_c(hi_abb_c downto lo_abb_c)) else '0';
|
195 |
|
|
addr <= neoled_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
|
196 |
|
|
wren <= acc_en and wren_i;
|
197 |
|
|
rden <= acc_en and rden_i;
|
198 |
|
|
|
199 |
|
|
|
200 |
|
|
-- Read/Write Access ----------------------------------------------------------------------
|
201 |
|
|
-- -------------------------------------------------------------------------------------------
|
202 |
|
|
rw_access: process(clk_i)
|
203 |
|
|
begin
|
204 |
|
|
if rising_edge(clk_i) then
|
205 |
|
|
-- access acknowledge --
|
206 |
|
|
ack_o <= wren or rden;
|
207 |
|
|
|
208 |
62 |
zero_gravi |
-- write access: control register --
|
209 |
|
|
if (wren = '1') and (addr = neoled_ctrl_addr_c) then
|
210 |
68 |
zero_gravi |
ctrl.enable <= data_i(ctrl_en_c);
|
211 |
62 |
zero_gravi |
ctrl.mode <= data_i(ctrl_mode_c);
|
212 |
|
|
ctrl.strobe <= data_i(ctrl_strobe_c);
|
213 |
|
|
ctrl.clk_prsc <= data_i(ctrl_clksel2_c downto ctrl_clksel0_c);
|
214 |
65 |
zero_gravi |
ctrl.irq_conf <= data_i(ctrl_irq_conf_c);
|
215 |
62 |
zero_gravi |
ctrl.t_total <= data_i(ctrl_t_tot_4_c downto ctrl_t_tot_0_c);
|
216 |
|
|
ctrl.t0_high <= data_i(ctrl_t_0h_4_c downto ctrl_t_0h_0_c);
|
217 |
|
|
ctrl.t1_high <= data_i(ctrl_t_1h_4_c downto ctrl_t_1h_0_c);
|
218 |
52 |
zero_gravi |
end if;
|
219 |
|
|
|
220 |
|
|
-- read access: control register --
|
221 |
|
|
data_o <= (others => '0');
|
222 |
62 |
zero_gravi |
if (rden = '1') then -- and (addr = neoled_ctrl_addr_c) then
|
223 |
68 |
zero_gravi |
data_o(ctrl_en_c) <= ctrl.enable;
|
224 |
52 |
zero_gravi |
data_o(ctrl_mode_c) <= ctrl.mode;
|
225 |
62 |
zero_gravi |
data_o(ctrl_strobe_c) <= ctrl.strobe;
|
226 |
52 |
zero_gravi |
data_o(ctrl_clksel2_c downto ctrl_clksel0_c) <= ctrl.clk_prsc;
|
227 |
65 |
zero_gravi |
data_o(ctrl_irq_conf_c) <= ctrl.irq_conf or bool_to_ulogic_f(boolean(FIFO_DEPTH = 1)); -- tie to one if FIFO_DEPTH is 1
|
228 |
62 |
zero_gravi |
data_o(ctrl_bufs_3_c downto ctrl_bufs_0_c) <= std_ulogic_vector(to_unsigned(index_size_f(FIFO_DEPTH), 4));
|
229 |
52 |
zero_gravi |
data_o(ctrl_t_tot_4_c downto ctrl_t_tot_0_c) <= ctrl.t_total;
|
230 |
|
|
data_o(ctrl_t_0h_4_c downto ctrl_t_0h_0_c) <= ctrl.t0_high;
|
231 |
|
|
data_o(ctrl_t_1h_4_c downto ctrl_t_1h_0_c) <= ctrl.t1_high;
|
232 |
62 |
zero_gravi |
--
|
233 |
|
|
data_o(ctrl_tx_empty_c) <= not tx_buffer.avail;
|
234 |
|
|
data_o(ctrl_tx_half_c) <= tx_buffer.half;
|
235 |
|
|
data_o(ctrl_tx_full_c) <= not tx_buffer.free;
|
236 |
|
|
data_o(ctrl_tx_busy_c) <= serial.busy;
|
237 |
52 |
zero_gravi |
end if;
|
238 |
|
|
end if;
|
239 |
|
|
end process rw_access;
|
240 |
|
|
|
241 |
|
|
-- enable external clock generator --
|
242 |
|
|
clkgen_en_o <= ctrl.enable;
|
243 |
|
|
|
244 |
62 |
zero_gravi |
-- FIFO write access --
|
245 |
|
|
tx_buffer.we <= '1' when (wren = '1') and (addr = neoled_data_addr_c) else '0';
|
246 |
|
|
tx_buffer.wdata <= ctrl.strobe & ctrl.mode & data_i;
|
247 |
|
|
tx_buffer.clear <= not ctrl.enable;
|
248 |
52 |
zero_gravi |
|
249 |
62 |
zero_gravi |
|
250 |
|
|
-- IRQ Generator --------------------------------------------------------------------------
|
251 |
|
|
-- -------------------------------------------------------------------------------------------
|
252 |
69 |
zero_gravi |
irq_select: process(ctrl, tx_buffer, serial.done)
|
253 |
68 |
zero_gravi |
begin
|
254 |
69 |
zero_gravi |
if (FIFO_DEPTH = 1) or (ctrl.irq_conf = '1') then
|
255 |
|
|
irq.set <= tx_buffer.free and serial.done; -- fire IRQ if FIFO is empty
|
256 |
68 |
zero_gravi |
else
|
257 |
69 |
zero_gravi |
irq.set <= not tx_buffer.half; -- fire IRQ if FIFO is less than half-full
|
258 |
68 |
zero_gravi |
end if;
|
259 |
|
|
end process irq_select;
|
260 |
|
|
|
261 |
69 |
zero_gravi |
-- Interrupt Edge Detector --
|
262 |
|
|
irq_detect: process(clk_i)
|
263 |
62 |
zero_gravi |
begin
|
264 |
|
|
if rising_edge(clk_i) then
|
265 |
65 |
zero_gravi |
if (ctrl.enable = '0') then
|
266 |
69 |
zero_gravi |
irq.buf <= "00";
|
267 |
65 |
zero_gravi |
else
|
268 |
69 |
zero_gravi |
irq.buf <= irq.buf(0) & irq.set;
|
269 |
65 |
zero_gravi |
end if;
|
270 |
62 |
zero_gravi |
end if;
|
271 |
69 |
zero_gravi |
end process irq_detect;
|
272 |
62 |
zero_gravi |
|
273 |
68 |
zero_gravi |
-- IRQ request to CPU --
|
274 |
69 |
zero_gravi |
irq_o <= '1' when (irq.buf = "01") else '0';
|
275 |
62 |
zero_gravi |
|
276 |
68 |
zero_gravi |
|
277 |
52 |
zero_gravi |
-- TX Buffer (FIFO) -----------------------------------------------------------------------
|
278 |
|
|
-- -------------------------------------------------------------------------------------------
|
279 |
61 |
zero_gravi |
tx_data_fifo: neorv32_fifo
|
280 |
|
|
generic map (
|
281 |
62 |
zero_gravi |
FIFO_DEPTH => FIFO_DEPTH, -- number of fifo entries; has to be a power of two; min 1
|
282 |
|
|
FIFO_WIDTH => 32+2, -- size of data elements in fifo
|
283 |
|
|
FIFO_RSYNC => true, -- sync read
|
284 |
|
|
FIFO_SAFE => true -- safe access
|
285 |
61 |
zero_gravi |
)
|
286 |
|
|
port map (
|
287 |
|
|
-- control --
|
288 |
|
|
clk_i => clk_i, -- clock, rising edge
|
289 |
|
|
rstn_i => '1', -- async reset, low-active
|
290 |
62 |
zero_gravi |
clear_i => tx_buffer.clear, -- sync reset, high-active
|
291 |
65 |
zero_gravi |
level_o => open, -- fill level
|
292 |
|
|
half_o => tx_buffer.half, -- FIFO is at least half full
|
293 |
61 |
zero_gravi |
-- write port --
|
294 |
62 |
zero_gravi |
wdata_i => tx_buffer.wdata, -- write data
|
295 |
61 |
zero_gravi |
we_i => tx_buffer.we, -- write enable
|
296 |
|
|
free_o => tx_buffer.free, -- at least one entry is free when set
|
297 |
|
|
-- read port --
|
298 |
|
|
re_i => tx_buffer.re, -- read enable
|
299 |
|
|
rdata_o => tx_buffer.rdata, -- read data
|
300 |
|
|
avail_o => tx_buffer.avail -- data available when set
|
301 |
|
|
);
|
302 |
52 |
zero_gravi |
|
303 |
65 |
zero_gravi |
-- try to get new TX data --
|
304 |
|
|
tx_buffer.re <= '1' when (serial.state = S_IDLE) else '0';
|
305 |
52 |
zero_gravi |
|
306 |
|
|
|
307 |
|
|
-- Serial TX Engine -----------------------------------------------------------------------
|
308 |
|
|
-- -------------------------------------------------------------------------------------------
|
309 |
|
|
serial_engine: process(clk_i)
|
310 |
|
|
begin
|
311 |
|
|
if rising_edge(clk_i) then
|
312 |
62 |
zero_gravi |
-- clock generator --
|
313 |
52 |
zero_gravi |
serial.pulse_clk <= clkgen_i(to_integer(unsigned(ctrl.clk_prsc)));
|
314 |
|
|
|
315 |
68 |
zero_gravi |
-- defaults --
|
316 |
|
|
serial.done <= '0';
|
317 |
|
|
|
318 |
62 |
zero_gravi |
-- FSM --
|
319 |
52 |
zero_gravi |
if (ctrl.enable = '0') then -- disabled
|
320 |
62 |
zero_gravi |
serial.state <= S_IDLE;
|
321 |
52 |
zero_gravi |
else
|
322 |
|
|
case serial.state is
|
323 |
|
|
|
324 |
|
|
when S_IDLE => -- waiting for new TX data
|
325 |
|
|
-- ------------------------------------------------------------
|
326 |
62 |
zero_gravi |
serial.tx_out <= '0';
|
327 |
|
|
serial.pulse_cnt <= (others => '0');
|
328 |
|
|
serial.strobe_cnt <= (others => '0');
|
329 |
52 |
zero_gravi |
if (tx_buffer.avail = '1') then
|
330 |
|
|
serial.state <= S_INIT;
|
331 |
|
|
end if;
|
332 |
|
|
|
333 |
|
|
when S_INIT => -- initialize TX shift engine
|
334 |
|
|
-- ------------------------------------------------------------
|
335 |
62 |
zero_gravi |
if (tx_buffer.rdata(33) = '0') then -- send data
|
336 |
|
|
if (tx_buffer.rdata(32) = '0') then -- mode = "RGB"
|
337 |
|
|
serial.mode <= '0';
|
338 |
|
|
serial.bit_cnt <= "011000"; -- total number of bits to send: 3x8=24
|
339 |
|
|
else -- mode = "RGBW"
|
340 |
|
|
serial.mode <= '1';
|
341 |
|
|
serial.bit_cnt <= "100000"; -- total number of bits to send: 4x8=32
|
342 |
|
|
end if;
|
343 |
|
|
serial.sreg <= tx_buffer.rdata(31 downto 00);
|
344 |
|
|
serial.state <= S_GETBIT;
|
345 |
|
|
else -- send RESET command
|
346 |
|
|
serial.state <= S_STROBE;
|
347 |
52 |
zero_gravi |
end if;
|
348 |
|
|
|
349 |
|
|
when S_GETBIT => -- get next TX bit
|
350 |
|
|
-- ------------------------------------------------------------
|
351 |
|
|
serial.sreg <= serial.sreg(serial.sreg'left-1 downto 0) & '0'; -- shift left by one position (MSB-first)
|
352 |
|
|
serial.bit_cnt <= std_ulogic_vector(unsigned(serial.bit_cnt) - 1);
|
353 |
|
|
serial.pulse_cnt <= (others => '0');
|
354 |
65 |
zero_gravi |
if (serial.next_bit = '0') then -- send zero-bit
|
355 |
|
|
serial.t_high <= ctrl.t0_high;
|
356 |
|
|
else -- send one-bit
|
357 |
|
|
serial.t_high <= ctrl.t1_high;
|
358 |
|
|
end if;
|
359 |
52 |
zero_gravi |
if (serial.bit_cnt = "000000") then -- all done?
|
360 |
65 |
zero_gravi |
serial.tx_out <= '0';
|
361 |
68 |
zero_gravi |
serial.done <= '1'; -- done sending data
|
362 |
65 |
zero_gravi |
serial.state <= S_IDLE;
|
363 |
|
|
else -- send current data MSB
|
364 |
|
|
serial.tx_out <= '1';
|
365 |
52 |
zero_gravi |
serial.state <= S_PULSE; -- transmit single pulse
|
366 |
|
|
end if;
|
367 |
|
|
|
368 |
|
|
when S_PULSE => -- send pulse with specific duty cycle
|
369 |
|
|
-- ------------------------------------------------------------
|
370 |
|
|
-- total pulse length = ctrl.t_total
|
371 |
|
|
-- pulse high time = serial.t_high
|
372 |
|
|
if (serial.pulse_clk = '1') then
|
373 |
|
|
serial.pulse_cnt <= std_ulogic_vector(unsigned(serial.pulse_cnt) + 1);
|
374 |
|
|
-- T_high reached? --
|
375 |
|
|
if (serial.pulse_cnt = serial.t_high) then
|
376 |
62 |
zero_gravi |
serial.tx_out <= '0';
|
377 |
52 |
zero_gravi |
end if;
|
378 |
|
|
-- T_total reached? --
|
379 |
|
|
if (serial.pulse_cnt = ctrl.t_total) then
|
380 |
|
|
serial.state <= S_GETBIT; -- get next bit to send
|
381 |
|
|
end if;
|
382 |
|
|
end if;
|
383 |
|
|
|
384 |
62 |
zero_gravi |
when S_STROBE => -- strobe LED data ("RESET" command)
|
385 |
|
|
-- ------------------------------------------------------------
|
386 |
|
|
-- wait for 127 * ctrl.t_total to _ensure_ RESET
|
387 |
|
|
if (serial.pulse_clk = '1') then
|
388 |
|
|
-- T_total reached? --
|
389 |
|
|
if (serial.pulse_cnt = ctrl.t_total) then
|
390 |
|
|
serial.pulse_cnt <= (others => '0');
|
391 |
|
|
serial.strobe_cnt <= std_ulogic_vector(unsigned(serial.strobe_cnt) + 1);
|
392 |
|
|
else
|
393 |
|
|
serial.pulse_cnt <= std_ulogic_vector(unsigned(serial.pulse_cnt) + 1);
|
394 |
|
|
end if;
|
395 |
|
|
end if;
|
396 |
|
|
-- number of LOW periods reached for RESET? --
|
397 |
|
|
if (and_reduce_f(serial.strobe_cnt) = '1') then
|
398 |
68 |
zero_gravi |
serial.done <= '1'; -- done sending RESET
|
399 |
62 |
zero_gravi |
serial.state <= S_IDLE;
|
400 |
|
|
end if;
|
401 |
|
|
|
402 |
52 |
zero_gravi |
when others => -- undefined
|
403 |
|
|
-- ------------------------------------------------------------
|
404 |
|
|
serial.state <= S_IDLE;
|
405 |
|
|
|
406 |
|
|
end case;
|
407 |
|
|
end if;
|
408 |
62 |
zero_gravi |
-- serial data tx_out --
|
409 |
|
|
neoled_o <= serial.tx_out and ctrl.enable;
|
410 |
52 |
zero_gravi |
end if;
|
411 |
|
|
end process serial_engine;
|
412 |
|
|
|
413 |
|
|
-- SREG's TX data: bit 23 for RGB mode (24-bit), bit 31 for RGBW mode (32-bit) --
|
414 |
|
|
serial.next_bit <= serial.sreg(23) when (serial.mode = '0') else serial.sreg(31);
|
415 |
|
|
|
416 |
|
|
-- TX engine status --
|
417 |
65 |
zero_gravi |
serial.busy <= '0' when (serial.state = S_IDLE) else '1';
|
418 |
52 |
zero_gravi |
|
419 |
|
|
|
420 |
|
|
end neorv32_neoled_rtl;
|