Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3.  
  4.  
  5. entity SPI is
  6. Port (
  7. -- Til og fra LC3
  8. clk : in std_logic;
  9. sys_reset : in std_logic;
  10. rd : in std_logic;
  11. SEL : in std_logic_vector(2 downto 0);
  12. status : out std_logic;
  13.  
  14. -- Fra Slow clk
  15. tick_fall : in std_logic;
  16. tick_rise : in std_logic;
  17. tick_reset : out std_logic;
  18.  
  19. -- Til og fra ADC
  20. cs : out std_logic;
  21. MOSI : out std_logic;
  22. MISO : in std_logic;
  23. spi_clk : out std_logic;
  24.  
  25. -- Til Data register
  26. data_out : out std_logic_vector(15 downto 0)
  27. -- data_en : out std_logic
  28.  
  29. );
  30. end SPI;
  31.  
  32. architecture Behavioral of SPI is
  33. TYPE State_type IS (init, start, waiting, rising, falling);
  34. signal state : State_type;
  35. signal next_state : State_type;
  36. signal count_signal : std_logic;
  37. signal count_reset : std_logic;
  38. signal count_max : std_logic;
  39. -- signal shift_en : std_logic;
  40. signal shift_in : std_logic_vector(16 downto 0);
  41. signal shift_out : std_logic_vector(16 downto 0);
  42. signal shift_ctrl : std_logic_vector(1 downto 0);
  43. -- signal data_in : std_logic_vector(15 downto 0);
  44. --signal data_out_signal : std_logic_vector(9 downto 0);
  45. signal select_singal : std_logic_vector(2 downto 0);
  46.  
  47. signal clk_reg_en : std_logic;
  48.  
  49.  
  50. Begin
  51. -- spi_clk <= count_signal;
  52.  
  53. Process(sys_reset, clk) -- Register
  54. Begin
  55. if (sys_reset = '1') then -- Reset signal
  56. state <= init;
  57. elsif( rising_edge(clk) ) then -- Rise og fall clk
  58. state <= next_state;
  59. end if;
  60. end process;
  61.  
  62. --next state
  63. next_state_logic : process (rd, state, tick_fall, tick_rise, count_max)
  64. begin
  65. next_state <= state;
  66. case state is
  67. when init =>
  68. if (rd = '1') then
  69. next_state <= start;
  70. end if;
  71. When start =>
  72. next_state <= waiting;
  73. WHEN waiting =>
  74. if (tick_fall = '1') then
  75. next_state <= falling;
  76. elsif (tick_rise = '1') then
  77. next_state <= rising;
  78. end if;
  79. WHEN falling =>
  80. if( count_max = '1') then
  81. next_state <= init;
  82. else
  83. next_state <= waiting;
  84. end if;
  85. When rising =>
  86. next_state <= waiting;
  87. end case;
  88. end process;
  89.  
  90. --output logic.
  91. output_logic : process ( state )
  92. begin
  93. cs <= '0';
  94. status <= '0';
  95. tick_reset <= '0';
  96. count_reset <= '0';
  97. count_signal <= '0';
  98. shift_ctrl <= "00";
  99. clk_reg_en <= '1';
  100. -- shift_en <= '0';
  101. case state is
  102. when init =>
  103. status <= '1';
  104. tick_reset <= '1';
  105. count_reset <= '1';
  106. cs <= '1';
  107. When start =>
  108. shift_ctrl <= "11"; --
  109. -- shift_en <= '1';
  110. WHEN waiting =>
  111. clk_reg_en <= '0';
  112. WHEN falling =>
  113. shift_ctrl <= "01"; -- Signal til at skrifte en gang til venstre.
  114. When rising =>
  115. count_signal <= '1';
  116. end case;
  117. end process;
  118.  
  119. clock_flop : entity work.d_ff
  120. port map(
  121. clk => clk,
  122. reset => sys_reset,
  123. en => clk_reg_en,
  124. data_in => count_signal,
  125. data_out => spi_clk
  126. );
  127.  
  128. select_singal_reg : entity work.reg_reset
  129. port map(
  130. clk => clk,
  131. reset => sys_reset,
  132. en => rd,
  133. d => SEL,
  134. q => select_singal
  135. );
  136.  
  137. shift_in <= "11" & select_singal & "00000000000" & miso;
  138. mosi <= shift_out(16);
  139. -- mosi <= '1';
  140. data_out <= shift_out(15 downto 0);
  141.  
  142. Shift_reg_u : entity work.shift_universal
  143. generic map (N => 17)
  144. port map(
  145. clk => clk,
  146. reset => sys_reset,
  147. ctrl => shift_ctrl,
  148. d => shift_in,
  149. q => shift_out
  150. );
  151.  
  152.  
  153.  
  154. counter : entity work.vores_counter
  155. generic map(
  156. N => 5, -- Antal bits der skal bruges til at t�lle til tallet.
  157. m => 18 -- Giver m-1 cycler
  158. )
  159. port map(
  160. count => count_signal,
  161. reset => count_reset,
  162. max_tick => count_max,
  163. q => open
  164. );
  165. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement