Library of reusable VHDL components
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.7 KiB

  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. --use ieee.numeric_std.all;
  4. library libvhdl;
  5. use libvhdl.AssertP.all;
  6. package SimP is
  7. procedure wait_cycles (signal clk : in std_logic; n : in natural);
  8. procedure spi_master (data_in : in std_logic_vector; data_out : out std_logic_vector;
  9. signal sclk : inout std_logic; signal ste : out std_logic;
  10. signal mosi : out std_logic; signal miso : in std_logic;
  11. cpol : in natural range 0 to 1; period : in time);
  12. procedure spi_slave (data_in : in std_logic_vector; data_out : out std_logic_vector;
  13. signal sclk : in std_logic; signal ste : in std_logic;
  14. signal mosi : in std_logic; signal miso : out std_logic;
  15. cpol : in natural range 0 to 1);
  16. end package SimP;
  17. package body SimP is
  18. -- wait for n rising egdes on clk
  19. procedure wait_cycles (signal clk : in std_logic; n : in natural) is
  20. begin
  21. for i in 1 to n loop
  22. wait until rising_edge(clk);
  23. end loop;
  24. end procedure wait_cycles;
  25. procedure spi_master ( data_in : in std_logic_vector; data_out : out std_logic_vector;
  26. signal sclk : inout std_logic; signal ste : out std_logic;
  27. signal mosi : out std_logic; signal miso : in std_logic;
  28. cpol : in natural range 0 to 1; period : in time) is
  29. begin
  30. assert_equal(data_in'length, data_out'length, "data_in & data_out must have same length!");
  31. sclk <= std_logic'val(cpol+2);
  32. ste <= '0';
  33. mosi <= '1';
  34. wait for period;
  35. for i in data_in'range loop
  36. sclk <= not(sclk);
  37. mosi <= data_in(i);
  38. wait for period;
  39. sclk <= not(sclk);
  40. data_out(i) := miso;
  41. wait for period;
  42. end loop;
  43. ste <= '1';
  44. mosi <= '1';
  45. wait for period;
  46. end procedure spi_master;
  47. procedure spi_slave ( data_in : in std_logic_vector; data_out : out std_logic_vector;
  48. signal sclk : in std_logic; signal ste : in std_logic;
  49. signal mosi : in std_logic; signal miso : out std_logic;
  50. cpol : in natural range 0 to 1) is
  51. variable v_cpol : std_logic := std_logic'val(cpol+2);
  52. begin
  53. assert_equal(data_in'length, data_out'length, "data_in & data_out must have same length!");
  54. miso <= 'Z';
  55. wait until ste = '0';
  56. for i in data_in'range loop
  57. wait until sclk'event and sclk = not(v_cpol);
  58. miso <= data_in(i);
  59. wait until sclk'event and sclk = v_cpol;
  60. data_out(i) := mosi;
  61. end loop;
  62. wait until ste = '1';
  63. miso <= 'Z';
  64. end procedure spi_slave;
  65. end package body SimP;