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.

110 lines
3.6 KiB

  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. library libvhdl;
  4. use libvhdl.AssertP.all;
  5. package SimP is
  6. procedure wait_cycles (signal clk : in std_logic; n : in natural);
  7. procedure spi_master ( data_in : in std_logic_vector; data_out : out std_logic_vector;
  8. signal sclk : inout std_logic; signal ste : out std_logic;
  9. signal mosi : out std_logic; signal miso : in std_logic;
  10. cpol : in natural range 0 to 1; cpha : in natural range 0 to 1;
  11. 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; cpha : in natural range 0 to 1);
  16. end package SimP;
  17. package body SimP is
  18. -- wait for n rising edges 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. -- configurable spi master which supports all combinations of cpol & cpha
  26. procedure spi_master ( data_in : in std_logic_vector; data_out : out std_logic_vector;
  27. signal sclk : inout std_logic; signal ste : out std_logic;
  28. signal mosi : out std_logic; signal miso : in std_logic;
  29. cpol : in natural range 0 to 1; cpha : in natural range 0 to 1;
  30. period : in time) is
  31. begin
  32. assert_equal(data_in'length, data_out'length, "data_in & data_out must have same length!");
  33. sclk <= std_logic'val(cpol+2);
  34. ste <= '0';
  35. if (cpha = 0) then
  36. for i in data_in'range loop
  37. mosi <= data_in(i);
  38. wait for period;
  39. sclk <= not(sclk);
  40. data_out(i) := miso;
  41. wait for period;
  42. sclk <= not(sclk);
  43. end loop;
  44. wait for period;
  45. else
  46. mosi <= '1';
  47. wait for period;
  48. for i in data_in'range loop
  49. sclk <= not(sclk);
  50. mosi <= data_in(i);
  51. wait for period;
  52. sclk <= not(sclk);
  53. data_out(i) := miso;
  54. wait for period;
  55. end loop;
  56. end if;
  57. ste <= '1';
  58. mosi <= '1';
  59. wait for period;
  60. end procedure spi_master;
  61. -- configurable spi slave which supports all combinations of cpol & cpha
  62. procedure spi_slave ( data_in : in std_logic_vector; data_out : out std_logic_vector;
  63. signal sclk : in std_logic; signal ste : in std_logic;
  64. signal mosi : in std_logic; signal miso : out std_logic;
  65. cpol : in natural range 0 to 1; cpha : in natural range 0 to 1) is
  66. variable v_cpol : std_logic := std_logic'val(cpol+2);
  67. begin
  68. assert_equal(data_in'length, data_out'length, "data_in & data_out must have same length!");
  69. miso <= 'Z';
  70. wait until ste = '0';
  71. if (cpha = 0) then
  72. for i in data_in'range loop
  73. miso <= data_in(i);
  74. wait until sclk'event and sclk = not(v_cpol);
  75. data_out(i) := mosi;
  76. wait until sclk'event and sclk = v_cpol;
  77. end loop;
  78. else
  79. for i in data_in'range loop
  80. wait until sclk'event and sclk = not(v_cpol);
  81. miso <= data_in(i);
  82. wait until sclk'event and sclk = v_cpol;
  83. data_out(i) := mosi;
  84. end loop;
  85. end if;
  86. wait until ste = '1';
  87. miso <= 'Z';
  88. end procedure spi_slave;
  89. end package body SimP;