usb_rx_pkt.v 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * usb_rx_pkt.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2019 Sylvain Munaut
  7. * All rights reserved.
  8. *
  9. * LGPL v3+, see LICENSE.lgpl3
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 3 of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with this program; if not, write to the Free Software Foundation,
  23. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. */
  25. `default_nettype none
  26. module usb_rx_pkt (
  27. // Low-Level
  28. input wire [1:0] ll_sym,
  29. input wire ll_bit,
  30. input wire ll_valid,
  31. input wire ll_eop,
  32. input wire ll_sync,
  33. input wire ll_bs_skip,
  34. input wire ll_bs_err,
  35. // Packet interface
  36. output reg pkt_start,
  37. output reg pkt_done_ok,
  38. output reg pkt_done_err,
  39. output wire [ 3:0] pkt_pid,
  40. output wire pkt_is_sof,
  41. output wire pkt_is_token,
  42. output wire pkt_is_data,
  43. output wire pkt_is_handshake,
  44. output wire [10:0] pkt_frameno,
  45. output wire [ 6:0] pkt_addr,
  46. output wire [ 3:0] pkt_endp,
  47. output wire [ 7:0] pkt_data,
  48. output reg pkt_data_stb,
  49. // Control
  50. input wire inhibit,
  51. // Common
  52. input wire clk,
  53. input wire rst
  54. );
  55. `include "usb_defs.vh"
  56. // FSM
  57. // ---
  58. localparam
  59. ST_IDLE = 0,
  60. ST_PID = 1,
  61. ST_PID_CHECK = 2,
  62. ST_ERROR = 3,
  63. ST_TOKEN_1 = 4,
  64. ST_TOKEN_2 = 5,
  65. ST_WAIT_EOP = 6,
  66. ST_DATA = 7;
  67. // Signals
  68. // -------
  69. // FSM
  70. reg [3:0] state_nxt;
  71. reg [3:0] state;
  72. reg state_prev_idle;
  73. reg state_prev_error;
  74. // Utils
  75. wire llu_bit_stb;
  76. wire llu_byte_stb;
  77. // Data shift reg & bit counting
  78. wire [7:0] data_nxt;
  79. reg [7:0] data;
  80. reg [3:0] bit_cnt;
  81. reg bit_eop_ok;
  82. wire bit_last;
  83. // CRC checking
  84. wire crc_in_bit;
  85. wire crc_in_valid;
  86. reg crc_in_first;
  87. reg crc_cap;
  88. wire crc5_match;
  89. reg crc5_ok;
  90. wire crc16_match;
  91. reg crc16_ok;
  92. // PID capture and decoding
  93. wire pid_cap;
  94. reg pid_cap_r;
  95. reg pid_valid;
  96. reg [3:0] pid;
  97. reg pid_is_sof;
  98. reg pid_is_token;
  99. reg pid_is_data;
  100. reg pid_is_handshake;
  101. // TOKEN data capture
  102. reg [10:0] token_data;
  103. // Main FSM
  104. // --------
  105. // Next state logic
  106. always @(*)
  107. begin
  108. // Default is to stay put
  109. state_nxt = state;
  110. // Main case
  111. case (state)
  112. ST_IDLE:
  113. // Wait for SYNC to be detected
  114. if (ll_valid && ll_sync && ~inhibit)
  115. state_nxt = ST_PID;
  116. ST_PID:
  117. // Wait for PID capture
  118. if (llu_byte_stb)
  119. state_nxt = ST_PID_CHECK;
  120. ST_PID_CHECK: begin
  121. // Default is to error if no match
  122. state_nxt = ST_ERROR;
  123. // Select state depending on packet type
  124. if (pid_valid) begin
  125. if (pid_is_sof)
  126. state_nxt = ST_TOKEN_1;
  127. else if (pid_is_token)
  128. state_nxt = ST_TOKEN_1;
  129. else if (pid_is_data)
  130. state_nxt = ST_DATA;
  131. else if (pid_is_handshake)
  132. state_nxt = ST_WAIT_EOP;
  133. end
  134. end
  135. ST_ERROR:
  136. // Error, wait for a possible IDLE state to resume
  137. if (ll_valid && (ll_eop || (ll_bs_err && (ll_sym == SYM_J))))
  138. state_nxt = ST_IDLE;
  139. ST_TOKEN_1:
  140. // First data byte
  141. if (ll_valid && ll_eop)
  142. state_nxt = ST_ERROR;
  143. else if (llu_byte_stb)
  144. state_nxt = ST_TOKEN_2;
  145. ST_TOKEN_2:
  146. // Second data byte
  147. if (ll_valid && ll_eop)
  148. state_nxt = ST_ERROR;
  149. else if (llu_byte_stb)
  150. state_nxt = ST_WAIT_EOP;
  151. ST_WAIT_EOP:
  152. // Need EOP at the right place
  153. if (ll_valid && ll_eop)
  154. state_nxt = (bit_eop_ok & (crc5_ok | pid_is_handshake)) ? ST_IDLE : ST_ERROR;
  155. else if (llu_byte_stb)
  156. state_nxt = ST_ERROR;
  157. ST_DATA:
  158. if (ll_valid) begin
  159. if (ll_eop)
  160. state_nxt = (bit_eop_ok & crc16_ok) ? ST_IDLE : ST_ERROR;
  161. else if (ll_bs_err)
  162. state_nxt = ST_ERROR;
  163. end
  164. endcase
  165. end
  166. // State register
  167. always @(posedge clk or posedge rst)
  168. if (rst)
  169. state <= ST_IDLE;
  170. else
  171. state <= state_nxt;
  172. // Utility signals
  173. // ---------------
  174. always @(posedge clk)
  175. begin
  176. state_prev_idle <= (state == ST_IDLE);
  177. state_prev_error <= (state == ST_ERROR);
  178. end
  179. assign llu_bit_stb = ll_valid & ~ll_bs_skip;
  180. assign llu_byte_stb = ll_valid & ~ll_bs_skip & bit_last;
  181. // Data shift register and bit counter
  182. // -----------------------------------
  183. // Next word
  184. assign data_nxt = { ll_bit, data[7:1] };
  185. // Shift reg
  186. always @(posedge clk)
  187. if (llu_bit_stb)
  188. data <= data_nxt;
  189. // Bit counter
  190. always @(posedge clk)
  191. if (state == ST_IDLE)
  192. bit_cnt <= 4'b0110;
  193. else if (llu_bit_stb)
  194. bit_cnt <= { 1'b0, bit_cnt[2:0] } - 1;
  195. // Last bit ?
  196. assign bit_last = bit_cnt[3];
  197. // EOP OK at this position ?
  198. always @(posedge clk)
  199. if (state == ST_IDLE)
  200. bit_eop_ok <= 1'b0;
  201. else if (llu_bit_stb)
  202. bit_eop_ok <= (bit_cnt[2:1] == 2'b10);
  203. // CRC checks
  204. // ----------
  205. // CRC input data
  206. assign crc_in_bit = ll_bit;
  207. assign crc_in_valid = llu_bit_stb;
  208. always @(posedge clk)
  209. if (state == ST_PID)
  210. crc_in_first <= 1'b1;
  211. else if (crc_in_valid)
  212. crc_in_first <= 1'b0;
  213. // CRC5 core
  214. usb_crc #(
  215. .WIDTH(5),
  216. .POLY(5'b00101),
  217. .MATCH(5'b01100)
  218. ) crc_5_I (
  219. .in_bit(crc_in_bit),
  220. .in_first(crc_in_first),
  221. .in_valid(crc_in_valid),
  222. .crc(),
  223. .crc_match(crc5_match),
  224. .clk(clk),
  225. .rst(rst)
  226. );
  227. // CRC16 core
  228. usb_crc #(
  229. .WIDTH(16),
  230. .POLY(16'h8005),
  231. .MATCH(16'h800D)
  232. ) crc_16_I (
  233. .in_bit(crc_in_bit),
  234. .in_first(crc_in_first),
  235. .in_valid(crc_in_valid),
  236. .crc(),
  237. .crc_match(crc16_match),
  238. .clk(clk),
  239. .rst(rst)
  240. );
  241. // Capture CRC status at end of each byte
  242. // This will be a bit 'late' (i.e. a couple cycles after the last
  243. // bit was input), but it's only used when EOP happens, which is
  244. // many cycles after that, so this delay is fine
  245. always @(posedge clk)
  246. crc_cap <= llu_byte_stb;
  247. always @(posedge clk)
  248. if (state == ST_IDLE) begin
  249. crc5_ok <= 1'b0;
  250. crc16_ok <= 1'b0;
  251. end else if (crc_cap) begin
  252. crc5_ok <= crc5_match;
  253. crc16_ok <= crc16_match;
  254. end
  255. // PID capture and decoding
  256. // ------------------------
  257. // When to capture
  258. assign pid_cap = (state == ST_PID) & llu_byte_stb;
  259. // Check PID before capture
  260. always @(posedge clk)
  261. if (pid_cap)
  262. pid_valid <= (data_nxt[3:0] == ~data_nxt[7:4]) && (
  263. (data_nxt[3:0] == PID_SOF) ||
  264. (data_nxt[3:0] == PID_OUT) ||
  265. (data_nxt[3:0] == PID_IN) ||
  266. (data_nxt[3:0] == PID_SETUP) ||
  267. (data_nxt[3:0] == PID_DATA0) ||
  268. (data_nxt[3:0] == PID_DATA1) ||
  269. (data_nxt[3:0] == PID_ACK) ||
  270. (data_nxt[3:0] == PID_NAK) ||
  271. (data_nxt[3:0] == PID_STALL)
  272. );
  273. always @(posedge clk)
  274. pid_cap_r <= pid_cap;
  275. // Capture and decode
  276. always @(posedge clk)
  277. if ((state == ST_PID) && llu_byte_stb)
  278. begin
  279. pid <= data_nxt;
  280. pid_is_sof <= (data_nxt[3:0] == PID_SOF);
  281. pid_is_token <= (data_nxt[3:0] == PID_OUT) || (data_nxt[3:0] == PID_IN) || (data_nxt[3:0] == PID_SETUP);
  282. pid_is_data <= (data_nxt[3:0] == PID_DATA0) || (data_nxt[3:0] == PID_DATA1);
  283. pid_is_handshake <= (data_nxt[3:0] == PID_ACK) || (data_nxt[3:0] == PID_NAK) || (data_nxt[3:0] == PID_STALL);
  284. end
  285. // TOKEN data capture
  286. // ------------------
  287. always @(posedge clk)
  288. if ((state == ST_TOKEN_1) && llu_byte_stb)
  289. token_data[7:0] <= data_nxt[7:0];
  290. always @(posedge clk)
  291. if ((state == ST_TOKEN_2) && llu_byte_stb)
  292. token_data[10:8] <= data_nxt[2:0];
  293. // Output
  294. // ------
  295. // Generate pkt_start on PID capture
  296. always @(posedge clk)
  297. pkt_start <= pid_cap_r & pid_valid;
  298. // Generate packet done signals
  299. always @(posedge clk)
  300. begin
  301. pkt_done_ok <= (state == ST_IDLE) && !state_prev_idle && !state_prev_error;
  302. pkt_done_err <= (state == ST_ERROR) && !state_prev_error;
  303. end
  304. // Output PID and decoded
  305. assign pkt_pid = pid;
  306. assign pkt_is_sof = pid_is_sof;
  307. assign pkt_is_token = pid_is_token;
  308. assign pkt_is_data = pid_is_data;
  309. assign pkt_is_handshake = pid_is_handshake;
  310. // Output token data
  311. assign pkt_frameno = token_data;
  312. assign pkt_addr = token_data[ 6:0];
  313. assign pkt_endp = token_data[10:7];
  314. // Data byte and associated strobe
  315. assign pkt_data = data;
  316. always @(posedge clk)
  317. pkt_data_stb <= (state == ST_DATA) && llu_byte_stb;
  318. endmodule // usb_rx_pkt