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.

118 lines
3.9 KiB

  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. --+ including vhdl 2008 libraries
  4. --+ These lines can be commented out when using
  5. --+ a simulator with built-in VHDL 2008 support
  6. library ieee_proposed;
  7. use ieee_proposed.standard_additions.all;
  8. use ieee_proposed.std_logic_1164_additions.all;
  9. library libvhdl;
  10. use libvhdl.AssertP.all;
  11. package SimP is
  12. procedure wait_cycles (signal clk : in std_logic; n : in natural);
  13. procedure spi_master ( data_in : in std_logic_vector; data_out : out std_logic_vector;
  14. signal sclk : inout std_logic; signal ste : out std_logic;
  15. signal mosi : out std_logic; signal miso : in std_logic;
  16. cpol : in natural range 0 to 1; cpha : in natural range 0 to 1;
  17. period : in time);
  18. procedure spi_slave ( data_in : in std_logic_vector; data_out : out std_logic_vector;
  19. signal sclk : in std_logic; signal ste : in std_logic;
  20. signal mosi : in std_logic; signal miso : out std_logic;
  21. cpol : in natural range 0 to 1; cpha : in natural range 0 to 1);
  22. end package SimP;
  23. package body SimP is
  24. -- wait for n rising edges on clk
  25. procedure wait_cycles (signal clk : in std_logic; n : in natural) is
  26. begin
  27. for i in 1 to n loop
  28. wait until rising_edge(clk);
  29. end loop;
  30. end procedure wait_cycles;
  31. -- configurable spi master which supports all combinations of cpol & cpha
  32. procedure spi_master ( data_in : in std_logic_vector; data_out : out std_logic_vector;
  33. signal sclk : inout std_logic; signal ste : out std_logic;
  34. signal mosi : out std_logic; signal miso : in std_logic;
  35. cpol : in natural range 0 to 1; cpha : in natural range 0 to 1;
  36. period : in time) is
  37. begin
  38. assert_equal(data_in'length, data_out'length, "data_in & data_out must have same length!");
  39. sclk <= std_logic'val(cpol+2);
  40. ste <= '0';
  41. if (cpha = 0) then
  42. for i in data_in'range loop
  43. mosi <= data_in(i);
  44. wait for period/2;
  45. sclk <= not(sclk);
  46. data_out(i) := miso;
  47. wait for period/2;
  48. sclk <= not(sclk);
  49. end loop;
  50. wait for period/2;
  51. else
  52. mosi <= '1';
  53. wait for period/2;
  54. for i in data_in'range loop
  55. sclk <= not(sclk);
  56. mosi <= data_in(i);
  57. wait for period/2;
  58. sclk <= not(sclk);
  59. data_out(i) := miso;
  60. wait for period/2;
  61. end loop;
  62. end if;
  63. ste <= '1';
  64. mosi <= '1';
  65. wait for period/2;
  66. end procedure spi_master;
  67. -- configurable spi slave which supports all combinations of cpol & cpha
  68. procedure spi_slave ( data_in : in std_logic_vector; data_out : out std_logic_vector;
  69. signal sclk : in std_logic; signal ste : in std_logic;
  70. signal mosi : in std_logic; signal miso : out std_logic;
  71. cpol : in natural range 0 to 1; cpha : in natural range 0 to 1) is
  72. variable v_cpol : std_logic := std_logic'val(cpol+2);
  73. begin
  74. assert_equal(data_in'length, data_out'length, "data_in & data_out must have same length!");
  75. miso <= 'Z';
  76. wait until ste = '0';
  77. if (cpha = 0) then
  78. for i in data_in'range loop
  79. miso <= data_in(i);
  80. wait until sclk'event and sclk = not(v_cpol);
  81. data_out(i) := mosi;
  82. wait until sclk'event and sclk = v_cpol;
  83. end loop;
  84. else
  85. for i in data_in'range loop
  86. wait until sclk'event and sclk = not(v_cpol);
  87. miso <= data_in(i);
  88. wait until sclk'event and sclk = v_cpol;
  89. data_out(i) := mosi;
  90. end loop;
  91. end if;
  92. wait until ste = '1';
  93. miso <= 'Z';
  94. end procedure spi_slave;
  95. end package body SimP;