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.

205 lines
6.0 KiB

  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. entity SpiSlaveE is
  5. generic (
  6. G_DATA_WIDTH : positive := 8; --* data bus width
  7. G_DATA_DIR : natural range 0 to 1 := 0; --* start from lsb/msb 0/1
  8. G_SPI_CPOL : natural range 0 to 1 := 0; --* SPI clock polarity
  9. G_SPI_CPHA : natural range 0 to 1 := 0 --* SPI clock phase
  10. );
  11. port (
  12. --+ system if
  13. Reset_n_i : in std_logic;
  14. Clk_i : in std_logic;
  15. --+ SPI slave if
  16. SpiSclk_i : in std_logic;
  17. SpiSte_i : in std_logic;
  18. SpiMosi_i : in std_logic;
  19. SpiMiso_o : out std_logic;
  20. --+ local VAI if
  21. Data_i : in std_logic_vector(G_DATA_WIDTH-1 downto 0);
  22. DataValid_i : in std_logic;
  23. DataAccept_o : out std_logic;
  24. Data_o : out std_logic_vector(G_DATA_WIDTH-1 downto 0);
  25. DataValid_o : out std_logic;
  26. DataAccept_i : in std_logic
  27. );
  28. end entity SpiSlaveE;
  29. architecture rtl of SpiSlaveE is
  30. type t_spi_state is (IDLE, TRANSFER, STORE);
  31. signal s_spi_state : t_spi_state;
  32. signal s_send_register : std_logic_vector(G_DATA_WIDTH-1 downto 0);
  33. signal s_recv_register : std_logic_vector(G_DATA_WIDTH-1 downto 0);
  34. signal s_sclk_d : std_logic_vector(2 downto 0);
  35. signal s_ste_d : std_logic_vector(2 downto 0);
  36. signal s_mosi_d : std_logic_vector(2 downto 0);
  37. signal s_miso : std_logic;
  38. signal s_data_valid : std_logic;
  39. signal s_transfer_valid : boolean;
  40. signal s_sclk_rising : boolean;
  41. signal s_sclk_falling : boolean;
  42. signal s_read_edge : boolean;
  43. signal s_write_edge : boolean;
  44. alias a_ste : std_logic is s_ste_d(s_ste_d'left);
  45. alias a_mosi : std_logic is s_mosi_d(s_mosi_d'left);
  46. constant C_BIT_COUNTER_START : natural := (G_DATA_WIDTH-1) * G_DATA_DIR;
  47. constant C_BIT_COUNTER_END : natural := (G_DATA_WIDTH-1) * to_integer(not(to_unsigned(G_DATA_DIR, 1)));
  48. begin
  49. --* help signals for edge detection on sclk
  50. s_sclk_rising <= true when s_sclk_d(2 downto 1) = "01" else false;
  51. s_sclk_falling <= true when s_sclk_d(2 downto 1) = "10" else false;
  52. s_read_edge <= s_sclk_rising when G_SPI_CPOL = G_SPI_CPHA else s_sclk_falling;
  53. s_write_edge <= s_sclk_falling when G_SPI_CPOL = G_SPI_CPHA else s_sclk_rising;
  54. --* Sync asynchronous SPI inputs with 3 stage FF line
  55. --* We use 3 FF because of edge detection on sclk line
  56. --* Mosi & ste are also registered with 3 FF to stay in
  57. --* sync with registered sclk
  58. SpiSyncP : process (Reset_n_i, Clk_i) is
  59. begin
  60. if (Reset_n_i = '0') then
  61. if (G_SPI_CPOL = 0) then
  62. s_sclk_d <= (others => '0');
  63. else
  64. s_sclk_d <= (others => '1');
  65. end if;
  66. s_ste_d <= (others => '1');
  67. s_mosi_d <= (others => '0');
  68. elsif rising_edge(Clk_i) then
  69. s_sclk_d <= s_sclk_d(1 downto 0) & SpiSclk_i;
  70. s_ste_d <= s_ste_d(1 downto 0) & SpiSte_i;
  71. s_mosi_d <= s_mosi_d(1 downto 0) & SpiMosi_i;
  72. end if;
  73. end process SpiSyncP;
  74. --* Save local data input when new data is provided and
  75. --* we're not inside a running SPI transmission
  76. SendRegisterP : process (Reset_n_i, Clk_i) is
  77. begin
  78. if (Reset_n_i = '0') then
  79. s_send_register <= (others => '0');
  80. DataAccept_o <= '0';
  81. elsif rising_edge(Clk_i) then
  82. DataAccept_o <= '0';
  83. if (DataValid_i = '1' and s_spi_state = IDLE) then
  84. s_send_register <= Data_i;
  85. DataAccept_o <= '1';
  86. end if;
  87. end if;
  88. end process SendRegisterP;
  89. --* Spi slave control FSM
  90. SpiControlP : process (Reset_n_i, Clk_i) is
  91. variable v_bit_counter : natural range 0 to G_DATA_WIDTH-1;
  92. begin
  93. if (Reset_n_i = '0') then
  94. s_miso <= '0';
  95. s_recv_register <= (others => '0');
  96. v_bit_counter := C_BIT_COUNTER_START;
  97. s_transfer_valid <= false;
  98. s_spi_state <= IDLE;
  99. elsif rising_edge(Clk_i) then
  100. case s_spi_state is
  101. when IDLE =>
  102. s_miso <= '0';
  103. s_recv_register <= (others => '0');
  104. v_bit_counter := C_BIT_COUNTER_START;
  105. s_transfer_valid <= false;
  106. if (a_ste = '0') then
  107. if (G_SPI_CPHA = 0) then
  108. s_miso <= s_send_register(v_bit_counter);
  109. end if;
  110. s_spi_state <= TRANSFER;
  111. end if;
  112. when TRANSFER =>
  113. if s_read_edge then
  114. s_recv_register(v_bit_counter) <= a_mosi;
  115. if (v_bit_counter = C_BIT_COUNTER_END) then
  116. s_spi_state <= STORE;
  117. else
  118. if (G_DATA_DIR = 0) then
  119. v_bit_counter := v_bit_counter + 1;
  120. else
  121. v_bit_counter := v_bit_counter - 1;
  122. end if;
  123. end if;
  124. elsif s_write_edge then
  125. s_miso <= s_send_register(v_bit_counter);
  126. else
  127. if (a_ste = '1') then
  128. s_spi_state <= IDLE;
  129. end if;
  130. end if;
  131. when STORE =>
  132. if (a_ste = '1') then
  133. s_transfer_valid <= true;
  134. s_spi_state <= IDLE;
  135. end if;
  136. when others =>
  137. s_spi_state <= IDLE;
  138. end case;
  139. end if;
  140. end process SpiControlP;
  141. --* Provide received SPI data to local interface
  142. --* Output data is overwritten if it isn't fetched
  143. --* until next finished SPI transmission
  144. RecvRegisterP : process (Reset_n_i, Clk_i) is
  145. begin
  146. if (Reset_n_i = '0') then
  147. Data_o <= (others => '0');
  148. s_data_valid <= '0';
  149. elsif rising_edge(Clk_i) then
  150. if (s_transfer_valid) then
  151. Data_o <= s_recv_register;
  152. s_data_valid <= '1';
  153. end if;
  154. if (DataAccept_i = '1' and s_data_valid = '1') then
  155. s_data_valid <= '0';
  156. end if;
  157. end if;
  158. end process RecvRegisterP;
  159. --+ Output port connections
  160. DataValid_o <= s_data_valid;
  161. SpiMiso_o <= 'Z' when SpiSte_i = '1' else s_miso;
  162. -- psl default clock is rising_edge(Clk_i);
  163. --
  164. -- psl assert always (s_spi_state = IDLE or s_spi_state = TRANSFER or s_spi_state = STORE);
  165. -- psl assert always (s_data_valid and DataAccept_i) -> next not(s_data_valid);
  166. end architecture rtl;