splink/vhdl/splink.vhdl

217 lines
6.1 KiB
VHDL
Raw Permalink Normal View History

2022-06-21 07:21:16 +02:00
-- splink, ethernet-connected LED controller
-- Copyright (C) 2022 xiretza
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
2022-06-03 19:11:07 +02:00
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
2022-06-18 17:24:47 +02:00
use work.ws2812_pkg.color_t;
use work.ws2812_pkg.colors_vector;
2022-06-03 19:11:07 +02:00
entity splink is
generic (
NUM_STRANDS : positive;
MAX_STRAND_LEN : positive := 256
2022-06-03 19:11:07 +02:00
);
port (
clk : in std_logic;
reset : in std_logic;
2022-06-06 18:42:37 +02:00
udp_length : in std_logic_vector(15 downto 0);
2022-06-05 21:34:31 +02:00
udp_valid : in std_logic;
udp_last : in std_logic;
udp_data : in std_logic_vector(31 downto 0);
2022-06-06 16:45:16 +02:00
frame_number : out unsigned(31 downto 0);
2022-06-05 22:56:29 +02:00
drivers : out std_logic_vector(NUM_STRANDS-1 downto 0)
2022-06-03 19:11:07 +02:00
);
end entity;
architecture a of splink is
2022-06-05 21:34:31 +02:00
constant BITS_PER_LED: natural := 24;
2022-06-18 17:24:47 +02:00
signal led_addr : std_logic_vector(7 downto 0);
signal led_data_a : std_logic_vector(BITS_PER_LED * NUM_STRANDS - 1 downto 0);
signal led_data_b : std_logic_vector(BITS_PER_LED * NUM_STRANDS - 1 downto 0);
2022-06-20 11:42:17 +02:00
signal led_data : std_logic_vector(BITS_PER_LED * NUM_STRANDS - 1 downto 0);
2022-06-18 17:24:47 +02:00
signal led_colors : colors_vector(NUM_STRANDS-1 downto 0);
signal active_strand: natural range 0 to NUM_STRANDS-1;
signal num_pixels: natural range 1 to MAX_STRAND_LEN;
2022-06-06 16:45:16 +02:00
signal current_frame: unsigned(31 downto 0);
signal pixels_received: natural range 0 to MAX_STRAND_LEN-1;
signal run : std_logic;
2022-06-20 11:42:17 +02:00
signal sender_done : std_logic;
2022-06-06 16:45:16 +02:00
-- "PIXL"
constant MAGIC_NUMBER : std_logic_vector(31 downto 0) := x"5049584c";
2022-06-06 18:42:37 +02:00
-- magic + frame num + strand num (4 bytes each)
constant HEADER_LEN : natural := 12;
type receive_state_t is (MAGIC, FRAME_NUM, STRAND_NUM, DATA, DROP);
constant RESET_STATE : receive_state_t := MAGIC;
signal receive_state : receive_state_t;
2022-06-20 11:42:17 +02:00
type bank_t is (BANK_A, BANK_B);
signal output_bank : bank_t;
2022-06-06 12:58:41 +02:00
begin
2022-06-18 17:24:47 +02:00
ws2812_inst: entity work.ws2812_parallel
generic map (
NUM_DRIVERS => NUM_STRANDS,
NUM_LEDS => MAX_STRAND_LEN,
COLOR_ORDER => "GRB",
T_CLK => 12.5 ns,
2022-06-06 12:58:41 +02:00
2022-06-18 17:24:47 +02:00
T0H => 0.35 us,
T0L => 0.9 us,
T1H => 0.7 us,
T1L => 0.55 us,
2022-06-06 12:58:41 +02:00
2022-06-20 11:42:17 +02:00
T_RES => 100 us
2022-06-18 17:24:47 +02:00
)
port map (
n_reset => not reset,
clk => clk,
run => run,
2022-06-20 11:42:17 +02:00
done => sender_done,
2022-06-06 12:58:41 +02:00
2022-06-18 17:24:47 +02:00
led_addr => led_addr,
2022-06-06 12:58:41 +02:00
2022-06-18 17:24:47 +02:00
led_colors => led_colors,
2022-06-06 12:58:41 +02:00
2022-06-18 17:24:47 +02:00
dout => drivers
);
2022-06-20 11:42:17 +02:00
process(led_data)
function make_color(data_in: std_logic_vector(BITS_PER_LED-1 downto 0)) return color_t is
begin
2022-06-20 11:42:17 +02:00
return (
red => data_in(23 downto 16),
green => data_in(15 downto 8),
blue => data_in(7 downto 0)
);
end function;
begin
for i in 0 to NUM_STRANDS-1 loop
2022-06-20 11:42:17 +02:00
led_colors(i) <= make_color(led_data((i+1) * BITS_PER_LED - 1 downto i * BITS_PER_LED));
end loop;
end process;
2022-06-06 12:58:41 +02:00
-- memory inference help
with output_bank select led_data <=
led_data_a when BANK_A,
led_data_b when BANK_B;
fsm: process(clk)
2022-06-20 11:42:17 +02:00
type strand_buffer_t is array(0 to MAX_STRAND_LEN-1) of std_logic_vector(BITS_PER_LED * NUM_STRANDS - 1 downto 0);
variable strand_buffer_a : strand_buffer_t;
variable strand_buffer_b : strand_buffer_t;
2022-06-20 11:42:17 +02:00
variable received_strands : std_logic_vector(NUM_STRANDS-1 downto 0);
variable input_bank : bank_t;
2022-06-05 21:34:31 +02:00
begin
if rising_edge(clk) then
led_data_a <= strand_buffer_a(to_integer(unsigned(led_addr)));
led_data_b <= strand_buffer_b(to_integer(unsigned(led_addr)));
2022-06-06 16:45:16 +02:00
2022-06-20 11:42:17 +02:00
if (and received_strands) and sender_done then
output_bank <= input_bank;
run <= '1';
2022-06-20 11:42:17 +02:00
case input_bank is
when BANK_A => input_bank := BANK_B;
when BANK_B => input_bank := BANK_A;
end case;
frame_number <= current_frame;
received_strands := (others => '0');
else
run <= '0';
2022-06-06 16:45:16 +02:00
end if;
if reset then
2022-06-20 11:42:17 +02:00
received_strands := (others => '0');
receive_state <= RESET_STATE;
elsif udp_valid then
if udp_last then
-- always resynchronize to start of packet
receive_state <= RESET_STATE;
end if;
2022-06-05 21:34:31 +02:00
2022-06-06 12:58:41 +02:00
case receive_state is
when MAGIC =>
2022-06-06 18:42:37 +02:00
if udp_data /= MAGIC_NUMBER then
receive_state <= DROP;
2022-06-20 11:42:17 +02:00
elsif (unsigned(udp_length) - HEADER_LEN) / 4 > MAX_STRAND_LEN then
receive_state <= DROP;
2022-06-06 18:42:37 +02:00
else
2022-06-20 11:42:17 +02:00
num_pixels <= (to_integer(unsigned(udp_length)) - HEADER_LEN) / 4;
receive_state <= STRAND_NUM;
end if;
2022-06-06 12:58:41 +02:00
when STRAND_NUM =>
if unsigned(udp_data) >= NUM_STRANDS then
receive_state <= DROP;
else
active_strand <= to_integer(unsigned(udp_data));
receive_state <= FRAME_NUM;
end if;
2022-06-06 12:58:41 +02:00
when FRAME_NUM =>
2022-06-20 11:42:17 +02:00
if not (or received_strands) then
2022-06-06 16:45:16 +02:00
current_frame <= unsigned(udp_data);
elsif current_frame /= unsigned(udp_data) then
current_frame <= unsigned(udp_data);
2022-06-20 11:42:17 +02:00
received_strands := (others => '0');
2022-06-06 16:45:16 +02:00
end if;
2022-06-06 12:58:41 +02:00
pixels_received <= 0;
receive_state <= DATA;
2022-06-06 12:58:41 +02:00
when DATA =>
if pixels_received /= num_pixels - 1 then
pixels_received <= pixels_received + 1;
2022-06-20 11:42:17 +02:00
elsif udp_last then
received_strands(active_strand) := '1';
else
-- packet too long
receive_state <= DROP;
2022-06-06 12:58:41 +02:00
end if;
when DROP =>
-- wait until udp_last
end case;
2022-06-05 21:34:31 +02:00
end if;
-- workaround for ghdl#2078
if reset = '0' and udp_valid = '1' and receive_state = DATA then
-- workaround for ghdl#2102
for i in 0 to NUM_STRANDS-1 loop
if i = active_strand then
if input_bank = BANK_A then
strand_buffer_a(pixels_received)((i+1) * BITS_PER_LED - 1 downto i * BITS_PER_LED) := udp_data(23 downto 0);
else
strand_buffer_b(pixels_received)((i+1) * BITS_PER_LED - 1 downto i * BITS_PER_LED) := udp_data(23 downto 0);
end if;
end if;
end loop;
end if;
2022-06-05 21:34:31 +02:00
end if;
end process;
2022-06-03 19:11:07 +02:00
end architecture;