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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_neoled.vhd] - Blame information for rev 62

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

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

powered by: WebSVN 2.1.0

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