spi_rxtx.vhdl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. library work;
  5. use work.wishbone_types.all;
  6. entity spi_rxtx is
  7. generic (
  8. DATA_LINES : positive := 1; -- Number of data lines
  9. -- 1=MISO/MOSI, otherwise 2 or 4
  10. INPUT_DELAY : natural range 0 to 1 := 1 -- Delay latching of SPI input:
  11. -- 0=no delay, 1=clk/2
  12. );
  13. port (
  14. clk : in std_ulogic;
  15. rst : in std_ulogic;
  16. --
  17. -- Clock divider
  18. -- SCK = CLK/((CLK_DIV+1)*2) : 0=CLK/2, 1=CLK/4, 2=CLK/6....
  19. --
  20. -- This need to be changed before a command.
  21. -- XX TODO add handshake
  22. clk_div_i : in natural range 0 to 255;
  23. --
  24. -- Command port (includes write data)
  25. --
  26. -- Valid & ready: command sampled when valid=1 and ready=1
  27. cmd_valid_i : in std_ulogic;
  28. cmd_ready_o : out std_ulogic;
  29. -- Command modes:
  30. -- 000 : Single bit read+write
  31. -- 010 : Single bit read
  32. -- 011 : Single bit write
  33. -- 100 : Dual read
  34. -- 101 : Dual write
  35. -- 110 : Quad read
  36. -- 111 : Quad write
  37. cmd_mode_i : in std_ulogic_vector(2 downto 0);
  38. -- # clocks-1 in a command (#bits-1)
  39. cmd_clks_i : in std_ulogic_vector(2 downto 0);
  40. -- Write data (sampled with command)
  41. cmd_txd_i : in std_ulogic_vector(7 downto 0);
  42. --
  43. -- Read data port. Data valid when d_ack=1, no ready
  44. -- signal, receiver must be ready
  45. --
  46. d_rxd_o : out std_ulogic_vector(7 downto 0);
  47. d_ack_o : out std_ulogic := '0';
  48. -- Set when all commands are done. Needed for callers to know when
  49. -- to release CS#
  50. bus_idle_o : out std_ulogic;
  51. --
  52. -- SPI port. These might need to go into special IOBUFs or STARTUPE2 on
  53. -- Xilinx.
  54. --
  55. -- Data lines are organized as follow:
  56. --
  57. -- DATA_LINES = 1
  58. --
  59. -- sdat_o(0) is MOSI (master output slave input)
  60. -- sdat_i(0) is MISO (master input slave output)
  61. --
  62. -- DATA_LINES > 1
  63. --
  64. -- sdat_o(0..n) are DQ(0..n)
  65. -- sdat_i(0..n) are DQ(0..n)
  66. --
  67. -- as such, beware that:
  68. --
  69. -- sdat_o(0) is MOSI (master output slave input)
  70. -- sdat_i(1) is MISO (master input slave output)
  71. --
  72. -- In order to leave dealing with the details of how to wire the tristate
  73. -- and bidirectional pins to the system specific toplevel, we separate
  74. -- the input and output signals, and provide a "sdat_oe" signal which
  75. -- is the "output enable" of each line.
  76. --
  77. sck : out std_ulogic;
  78. sdat_o : out std_ulogic_vector(DATA_LINES-1 downto 0);
  79. sdat_oe : out std_ulogic_vector(DATA_LINES-1 downto 0);
  80. sdat_i : in std_ulogic_vector(DATA_LINES-1 downto 0)
  81. );
  82. end entity spi_rxtx;
  83. architecture rtl of spi_rxtx is
  84. -- Internal clock signal. Output is gated by sck_en_int
  85. signal sck_0 : std_ulogic;
  86. signal sck_1 : std_ulogic;
  87. -- Clock divider latch
  88. signal clk_div : natural range 0 to 255;
  89. -- 1 clk pulses indicating when to send and when to latch
  90. --
  91. -- Typically for CPOL=CPHA
  92. -- sck_send is sck falling edge
  93. -- sck_recv is sck rising edge
  94. --
  95. -- Those pulses are generated "ahead" of the corresponding
  96. -- edge so then are "seen" at the rising sysclk edge matching
  97. -- the corresponding sck edgeg.
  98. signal sck_send : std_ulogic;
  99. signal sck_recv : std_ulogic;
  100. -- Command mode latch
  101. signal cmd_mode : std_ulogic_vector(2 downto 0);
  102. -- Output shift register (use fifo ?)
  103. signal oreg : std_ulogic_vector(7 downto 0);
  104. -- Input latch
  105. signal dat_i_l : std_ulogic_vector(DATA_LINES-1 downto 0);
  106. -- Data ack latch
  107. signal dat_ack_l : std_ulogic;
  108. -- Delayed recv signal for the read machine
  109. signal sck_recv_d : std_ulogic := '0';
  110. -- Input shift register (use fifo ?)
  111. signal ireg : std_ulogic_vector(7 downto 0) := (others => '0');
  112. -- Bit counter
  113. signal bit_count : std_ulogic_vector(2 downto 0);
  114. -- Next/start/stop command signals. Set when counter goes negative
  115. signal next_cmd : std_ulogic;
  116. signal start_cmd : std_ulogic;
  117. signal end_cmd : std_ulogic;
  118. function data_single(mode : std_ulogic_vector(2 downto 0)) return boolean is
  119. begin
  120. return mode(2) = '0';
  121. end;
  122. function data_dual(mode : std_ulogic_vector(2 downto 0)) return boolean is
  123. begin
  124. return mode(2 downto 1) = "10";
  125. end;
  126. function data_quad(mode : std_ulogic_vector(2 downto 0)) return boolean is
  127. begin
  128. return mode(2 downto 1) = "11";
  129. end;
  130. function data_write(mode : std_ulogic_vector(2 downto 0)) return boolean is
  131. begin
  132. return mode(0) = '1';
  133. end;
  134. type state_t is (STANDBY, DATA);
  135. signal state : state_t := STANDBY;
  136. begin
  137. -- We don't support multiple data lines at this point
  138. assert DATA_LINES = 1 or DATA_LINES = 2 or DATA_LINES = 4
  139. report "Unsupported DATA_LINES configuration !" severity failure;
  140. -- Clock generation
  141. --
  142. -- XX HARD WIRE CPOL=1 CPHA=1 for now
  143. sck_gen: process(clk)
  144. variable counter : integer range 0 to 255;
  145. begin
  146. if rising_edge(clk) then
  147. if rst = '1' then
  148. sck_0 <= '1';
  149. sck_1 <= '1';
  150. sck_send <= '0';
  151. sck_recv <= '0';
  152. clk_div <= 0;
  153. counter := 0;
  154. elsif counter = clk_div then
  155. counter := 0;
  156. -- Latch new divider
  157. clk_div <= clk_div_i;
  158. -- Internal version of the clock
  159. sck_0 <= not sck_0;
  160. -- Generate send/receive pulses to run out state machine
  161. sck_recv <= not sck_0;
  162. sck_send <= sck_0;
  163. else
  164. counter := counter + 1;
  165. sck_recv <= '0';
  166. sck_send <= '0';
  167. end if;
  168. -- Delayed version of the clock to line up with
  169. -- the up/down signals
  170. --
  171. -- XXX Figure out a better way
  172. if (state = DATA and end_cmd = '0') or (next_cmd = '1' and cmd_valid_i = '1') then
  173. sck_1 <= sck_0;
  174. else
  175. sck_1 <= '1';
  176. end if;
  177. end if;
  178. end process;
  179. -- SPI clock
  180. sck <= sck_1;
  181. -- Ready to start the next command. This is set on the clock down
  182. -- after the counter goes negative.
  183. -- Note: in addition to latching a new command, this will cause
  184. -- the counter to be reloaded.
  185. next_cmd <= '1' when sck_send = '1' and bit_count = "111" else '0';
  186. -- We start a command when we have a valid request at that time.
  187. start_cmd <= next_cmd and cmd_valid_i;
  188. -- We end commands if we get start_cmd and there's nothing to
  189. -- start. This sends up to standby holding CLK high
  190. end_cmd <= next_cmd and not cmd_valid_i;
  191. -- Generate cmd_ready. It will go up and down with sck, we could
  192. -- gate it with cmd_valid to make it look cleaner but that would
  193. -- add yet another combinational loop on the wishbone that I'm
  194. -- to avoid.
  195. cmd_ready_o <= next_cmd;
  196. -- Generate bus_idle_o
  197. bus_idle_o <= '1' when state = STANDBY else '0';
  198. -- Main state machine. Also generates cmd and data ACKs
  199. machine: process(clk)
  200. begin
  201. if rising_edge(clk) then
  202. if rst = '1' then
  203. state <= STANDBY;
  204. cmd_mode <= "000";
  205. else
  206. -- First clk down of a new cycle. Latch a request if any
  207. -- or get out.
  208. if start_cmd = '1' then
  209. state <= DATA;
  210. cmd_mode <= cmd_mode_i;
  211. elsif end_cmd = '1' then
  212. state <= STANDBY;
  213. end if;
  214. end if;
  215. end if;
  216. end process;
  217. -- Run the bit counter in DATA state. It will update on rising
  218. -- SCK edges. It starts at d_clks on command latch
  219. count_bit: process(clk)
  220. begin
  221. if rising_edge(clk) then
  222. if rst = '1' then
  223. bit_count <= (others => '0');
  224. else
  225. if start_cmd = '1' then
  226. bit_count <= cmd_clks_i;
  227. elsif state /= DATA then
  228. bit_count <= (others => '1');
  229. elsif sck_recv = '1' then
  230. bit_count <= std_ulogic_vector(unsigned(bit_count) - 1);
  231. end if;
  232. end if;
  233. end if;
  234. end process;
  235. -- Shift output data
  236. shift_out: process(clk)
  237. begin
  238. if rising_edge(clk) then
  239. -- Starting a command
  240. if start_cmd = '1' then
  241. oreg <= cmd_txd_i(7 downto 0);
  242. elsif sck_send = '1' then
  243. -- Get shift amount
  244. if data_single(cmd_mode) then
  245. oreg <= oreg(6 downto 0) & '0';
  246. elsif data_dual(cmd_mode) then
  247. oreg <= oreg(5 downto 0) & "00";
  248. else
  249. oreg <= oreg(3 downto 0) & "0000";
  250. end if;
  251. end if;
  252. end if;
  253. end process;
  254. -- Data out
  255. sdat_o(0) <= oreg(7);
  256. dl2: if DATA_LINES > 1 generate
  257. sdat_o(1) <= oreg(6);
  258. end generate;
  259. dl4: if DATA_LINES > 2 generate
  260. sdat_o(2) <= oreg(5);
  261. sdat_o(3) <= oreg(4);
  262. end generate;
  263. -- Data lines direction
  264. dlines: process(all)
  265. begin
  266. for i in DATA_LINES-1 downto 0 loop
  267. sdat_oe(i) <= '0';
  268. if state = DATA then
  269. -- In single mode, we always enable MOSI, otherwise
  270. -- we control the output enable based on the direction
  271. -- of transfer.
  272. --
  273. if i = 0 and (data_single(cmd_mode) or data_write(cmd_mode)) then
  274. sdat_oe(i) <= '1';
  275. end if;
  276. if i = 1 and data_dual(cmd_mode) and data_write(cmd_mode) then
  277. sdat_oe(i) <= '1';
  278. end if;
  279. if i > 0 and data_quad(cmd_mode) and data_write(cmd_mode) then
  280. sdat_oe(i) <= '1';
  281. end if;
  282. end if;
  283. end loop;
  284. end process;
  285. -- Latch input data no delay
  286. input_delay_0: if INPUT_DELAY = 0 generate
  287. process(clk)
  288. begin
  289. if rising_edge(clk) then
  290. dat_i_l <= sdat_i;
  291. end if;
  292. end process;
  293. end generate;
  294. -- Latch input data half clock delay
  295. input_delay_1: if INPUT_DELAY = 1 generate
  296. process(clk)
  297. begin
  298. if falling_edge(clk) then
  299. dat_i_l <= sdat_i;
  300. end if;
  301. end process;
  302. end generate;
  303. -- Shift input data
  304. shift_in: process(clk)
  305. begin
  306. if rising_edge(clk) then
  307. -- Delay the receive signal to match the input latch
  308. if state = DATA then
  309. sck_recv_d <= sck_recv;
  310. else
  311. sck_recv_d <= '0';
  312. end if;
  313. -- Generate read data acks
  314. if bit_count = "000" and sck_recv = '1' then
  315. dat_ack_l <= not cmd_mode(0);
  316. else
  317. dat_ack_l <= '0';
  318. end if;
  319. -- And delay them as well
  320. d_ack_o <= dat_ack_l;
  321. -- Shift register on delayed data & receive signal
  322. if sck_recv_d = '1' then
  323. if DATA_LINES = 1 then
  324. ireg <= ireg(6 downto 0) & dat_i_l(0);
  325. else
  326. if data_dual(cmd_mode) then
  327. ireg <= ireg(5 downto 0) & dat_i_l(1) & dat_i_l(0);
  328. elsif data_quad(cmd_mode) then
  329. ireg <= ireg(3 downto 0) & dat_i_l(3) & dat_i_l(2) & dat_i_l(1) & dat_i_l(0);
  330. else
  331. assert(data_single(cmd_mode));
  332. ireg <= ireg(6 downto 0) & dat_i_l(1);
  333. end if;
  334. end if;
  335. end if;
  336. end if;
  337. end process;
  338. -- Data recieve register
  339. d_rxd_o <= ireg;
  340. end architecture;