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 (
|
2022-06-05 16:51:03 +02:00
|
|
|
NUM_STRANDS : positive;
|
2022-06-05 16:42:26 +02:00
|
|
|
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
|
|
|
|
2022-06-05 16:51:03 +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-05 16:42:26 +02:00
|
|
|
|
2022-06-18 17:24:47 +02:00
|
|
|
signal led_addr : std_logic_vector(7 downto 0);
|
2022-06-20 13:15:06 +02:00
|
|
|
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);
|
2022-06-05 16:42:26 +02:00
|
|
|
|
2022-06-06 15:24:49 +02:00
|
|
|
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);
|
2022-06-06 15:24:49 +02:00
|
|
|
signal pixels_received: natural range 0 to MAX_STRAND_LEN-1;
|
|
|
|
|
2022-06-06 18:38:56 +02:00
|
|
|
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
|
|
|
|
2022-06-06 17:11:39 +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;
|
2022-06-06 17:11:39 +02:00
|
|
|
|
|
|
|
type receive_state_t is (MAGIC, FRAME_NUM, STRAND_NUM, DATA, DROP);
|
|
|
|
constant RESET_STATE : receive_state_t := MAGIC;
|
2022-06-06 15:24:49 +02:00
|
|
|
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-06 15:24:49 +02:00
|
|
|
|
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
|
2022-06-06 15:24:49 +02:00
|
|
|
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;
|
2022-06-06 15:24:49 +02:00
|
|
|
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));
|
2022-06-06 15:24:49 +02:00
|
|
|
end loop;
|
|
|
|
end process;
|
2022-06-06 12:58:41 +02:00
|
|
|
|
2022-06-20 13:15:06 +02:00
|
|
|
-- memory inference help
|
|
|
|
with output_bank select led_data <=
|
|
|
|
led_data_a when BANK_A,
|
|
|
|
led_data_b when BANK_B;
|
|
|
|
|
2022-06-06 15:24:49 +02:00
|
|
|
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);
|
|
|
|
|
2022-06-20 13:15:06 +02:00
|
|
|
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
|
2022-06-20 13:15:06 +02:00
|
|
|
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;
|
2022-06-06 18:38:56 +02:00
|
|
|
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;
|
|
|
|
|
2022-06-06 15:24:49 +02:00
|
|
|
if reset then
|
2022-06-20 11:42:17 +02:00
|
|
|
received_strands := (others => '0');
|
2022-06-06 17:11:39 +02:00
|
|
|
receive_state <= RESET_STATE;
|
2022-06-06 15:24:49 +02:00
|
|
|
elsif udp_valid then
|
|
|
|
if udp_last then
|
|
|
|
-- always resynchronize to start of packet
|
2022-06-06 17:11:39 +02:00
|
|
|
receive_state <= RESET_STATE;
|
2022-06-06 15:24:49 +02:00
|
|
|
end if;
|
2022-06-05 21:34:31 +02:00
|
|
|
|
2022-06-06 12:58:41 +02:00
|
|
|
case receive_state is
|
2022-06-06 17:11:39 +02:00
|
|
|
when MAGIC =>
|
2022-06-06 18:42:37 +02:00
|
|
|
if udp_data /= MAGIC_NUMBER then
|
2022-06-06 17:11:39 +02:00
|
|
|
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;
|
2022-06-06 17:11:39 +02:00
|
|
|
end if;
|
|
|
|
|
2022-06-06 12:58:41 +02:00
|
|
|
when STRAND_NUM =>
|
2022-06-06 18:25:27 +02:00
|
|
|
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
|
|
|
|
2022-06-06 15:24:49 +02:00
|
|
|
pixels_received <= 0;
|
|
|
|
receive_state <= DATA;
|
2022-06-06 12:58:41 +02:00
|
|
|
|
|
|
|
when DATA =>
|
2022-06-06 15:24:49 +02:00
|
|
|
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
|
2022-06-06 15:24:49 +02:00
|
|
|
-- 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;
|
2022-06-20 11:43:01 +02:00
|
|
|
|
|
|
|
-- workaround for ghdl#2078
|
|
|
|
if reset = '0' and udp_valid = '1' and receive_state = DATA then
|
|
|
|
if input_bank = BANK_A then
|
|
|
|
strand_buffer_a(pixels_received)((active_strand+1) * BITS_PER_LED - 1 downto active_strand * BITS_PER_LED) := udp_data(23 downto 0);
|
|
|
|
else
|
|
|
|
strand_buffer_b(pixels_received)((active_strand+1) * BITS_PER_LED - 1 downto active_strand * BITS_PER_LED) := udp_data(23 downto 0);
|
|
|
|
end if;
|
|
|
|
end if;
|
2022-06-05 21:34:31 +02:00
|
|
|
end if;
|
|
|
|
end process;
|
2022-06-03 19:11:07 +02:00
|
|
|
end architecture;
|