usbf_sie_rx.v 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. //-----------------------------------------------------------------
  2. // USB Serial Port
  3. // V0.1
  4. // Ultra-Embedded.com
  5. // Copyright 2020
  6. //
  7. // Email: admin@ultra-embedded.com
  8. //
  9. // License: LGPL
  10. //-----------------------------------------------------------------
  11. //
  12. // This source file may be used and distributed without
  13. // restriction provided that this copyright statement is not
  14. // removed from the file and that any derivative work contains
  15. // the original copyright notice and the associated disclaimer.
  16. //
  17. // This source file is free software; you can redistribute it
  18. // and/or modify it under the terms of the GNU Lesser General
  19. // Public License as published by the Free Software Foundation;
  20. // either version 2.1 of the License, or (at your option) any
  21. // later version.
  22. //
  23. // This source is distributed in the hope that it will be
  24. // useful, but WITHOUT ANY WARRANTY; without even the implied
  25. // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  26. // PURPOSE. See the GNU Lesser General Public License for more
  27. // details.
  28. //
  29. // You should have received a copy of the GNU Lesser General
  30. // Public License along with this source; if not, write to the
  31. // Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  32. // Boston, MA 02111-1307 USA
  33. //-----------------------------------------------------------------
  34. //`define USB_RANDOM_CORRUPT 6
  35. //-----------------------------------------------------------------
  36. // Generated File
  37. //-----------------------------------------------------------------
  38. module usbf_sie_rx
  39. (
  40. // Inputs
  41. input clk_i
  42. ,input rst_i
  43. ,input enable_i
  44. ,input [ 7:0] utmi_data_i
  45. ,input utmi_rxvalid_i
  46. ,input utmi_rxactive_i
  47. ,input [ 6:0] current_addr_i
  48. // Outputs
  49. ,output [ 7:0] pid_o
  50. ,output frame_valid_o
  51. ,output [ 10:0] frame_number_o
  52. ,output token_valid_o
  53. ,output [ 6:0] token_addr_o
  54. ,output [ 3:0] token_ep_o
  55. ,output token_crc_err_o
  56. ,output handshake_valid_o
  57. ,output data_valid_o
  58. ,output data_strb_o
  59. ,output [ 7:0] data_o
  60. ,output data_last_o
  61. ,output data_crc_err_o
  62. ,output data_complete_o
  63. );
  64. //-----------------------------------------------------------------
  65. // Defines:
  66. //-----------------------------------------------------------------
  67. `include "usbf_defs.v"
  68. localparam STATE_W = 4;
  69. localparam STATE_RX_IDLE = 4'd0;
  70. localparam STATE_RX_TOKEN2 = 4'd1;
  71. localparam STATE_RX_TOKEN3 = 4'd2;
  72. localparam STATE_RX_TOKEN_COMPLETE = 4'd3;
  73. localparam STATE_RX_SOF2 = 4'd4;
  74. localparam STATE_RX_SOF3 = 4'd5;
  75. localparam STATE_RX_DATA = 4'd6;
  76. localparam STATE_RX_DATA_COMPLETE = 4'd7;
  77. localparam STATE_RX_IGNORED = 4'd8;
  78. reg [STATE_W-1:0] state_q;
  79. //-----------------------------------------------------------------
  80. // Wire / Regs
  81. //-----------------------------------------------------------------
  82. `define USB_FRAME_W 11
  83. reg [`USB_FRAME_W-1:0] frame_num_q;
  84. `define USB_DEV_W 7
  85. reg [`USB_DEV_W-1:0] token_dev_q;
  86. `define USB_EP_W 4
  87. reg [`USB_EP_W-1:0] token_ep_q;
  88. `define USB_PID_W 8
  89. reg [`USB_PID_W-1:0] token_pid_q;
  90. //-----------------------------------------------------------------
  91. // Data delay (to strip the CRC16 trailing bytes)
  92. //-----------------------------------------------------------------
  93. reg [31:0] data_buffer_q;
  94. reg [3:0] data_valid_q;
  95. reg [3:0] rx_active_q;
  96. wire shift_en_w = (utmi_rxvalid_i & utmi_rxactive_i) || !utmi_rxactive_i;
  97. always @ (posedge clk_i or posedge rst_i)
  98. if (rst_i)
  99. data_buffer_q <= 32'b0;
  100. else if (shift_en_w)
  101. data_buffer_q <= {utmi_data_i, data_buffer_q[31:8]};
  102. always @ (posedge clk_i or posedge rst_i)
  103. if (rst_i)
  104. data_valid_q <= 4'b0;
  105. else if (shift_en_w)
  106. data_valid_q <= {(utmi_rxvalid_i & utmi_rxactive_i), data_valid_q[3:1]};
  107. else
  108. data_valid_q <= {data_valid_q[3:1], 1'b0};
  109. reg [1:0] data_crc_q;
  110. always @ (posedge clk_i or posedge rst_i)
  111. if (rst_i)
  112. data_crc_q <= 2'b0;
  113. else if (shift_en_w)
  114. data_crc_q <= {!utmi_rxactive_i, data_crc_q[1]};
  115. always @ (posedge clk_i or posedge rst_i)
  116. if (rst_i)
  117. rx_active_q <= 4'b0;
  118. else
  119. rx_active_q <= {utmi_rxactive_i, rx_active_q[3:1]};
  120. wire [7:0] data_w = data_buffer_q[7:0];
  121. wire data_ready_w = data_valid_q[0];
  122. wire crc_byte_w = data_crc_q[0];
  123. wire rx_active_w = rx_active_q[0];
  124. wire address_match_w = (token_dev_q == current_addr_i);
  125. //-----------------------------------------------------------------
  126. // Next state
  127. //-----------------------------------------------------------------
  128. reg [STATE_W-1:0] next_state_r;
  129. always @ *
  130. begin
  131. next_state_r = state_q;
  132. case (state_q)
  133. //-----------------------------------------
  134. // IDLE
  135. //-----------------------------------------
  136. STATE_RX_IDLE :
  137. begin
  138. if (data_ready_w)
  139. begin
  140. // Decode PID
  141. case (data_w)
  142. `PID_OUT, `PID_IN, `PID_SETUP, `PID_PING:
  143. next_state_r = STATE_RX_TOKEN2;
  144. `PID_SOF:
  145. next_state_r = STATE_RX_SOF2;
  146. `PID_DATA0, `PID_DATA1, `PID_DATA2, `PID_MDATA:
  147. begin
  148. next_state_r = STATE_RX_DATA;
  149. end
  150. `PID_ACK, `PID_NAK, `PID_STALL, `PID_NYET:
  151. next_state_r = STATE_RX_IDLE;
  152. default : // SPLIT / ERR
  153. next_state_r = STATE_RX_IGNORED;
  154. endcase
  155. end
  156. end
  157. //-----------------------------------------
  158. // RX_IGNORED: Unknown / unsupported
  159. //-----------------------------------------
  160. STATE_RX_IGNORED :
  161. begin
  162. // Wait until the end of the packet
  163. if (!rx_active_w)
  164. next_state_r = STATE_RX_IDLE;
  165. end
  166. //-----------------------------------------
  167. // SOF (BYTE 2)
  168. //-----------------------------------------
  169. STATE_RX_SOF2 :
  170. begin
  171. if (data_ready_w)
  172. next_state_r = STATE_RX_SOF3;
  173. else if (!rx_active_w)
  174. next_state_r = STATE_RX_IDLE;
  175. end
  176. //-----------------------------------------
  177. // SOF (BYTE 3)
  178. //-----------------------------------------
  179. STATE_RX_SOF3 :
  180. begin
  181. if (data_ready_w || !rx_active_w)
  182. next_state_r = STATE_RX_IDLE;
  183. end
  184. //-----------------------------------------
  185. // TOKEN (IN/OUT/SETUP) (Address/Endpoint)
  186. //-----------------------------------------
  187. STATE_RX_TOKEN2 :
  188. begin
  189. if (data_ready_w)
  190. next_state_r = STATE_RX_TOKEN3;
  191. else if (!rx_active_w)
  192. next_state_r = STATE_RX_IDLE;
  193. end
  194. //-----------------------------------------
  195. // TOKEN (IN/OUT/SETUP) (Endpoint/CRC)
  196. //-----------------------------------------
  197. STATE_RX_TOKEN3 :
  198. begin
  199. if (data_ready_w)
  200. next_state_r = STATE_RX_TOKEN_COMPLETE;
  201. else if (!rx_active_w)
  202. next_state_r = STATE_RX_IDLE;
  203. end
  204. //-----------------------------------------
  205. // RX_TOKEN_COMPLETE
  206. //-----------------------------------------
  207. STATE_RX_TOKEN_COMPLETE :
  208. begin
  209. next_state_r = STATE_RX_IDLE;
  210. end
  211. //-----------------------------------------
  212. // RX_DATA
  213. //-----------------------------------------
  214. STATE_RX_DATA :
  215. begin
  216. // Receive complete
  217. if (crc_byte_w)
  218. next_state_r = STATE_RX_DATA_COMPLETE;
  219. end
  220. //-----------------------------------------
  221. // RX_DATA_COMPLETE
  222. //-----------------------------------------
  223. STATE_RX_DATA_COMPLETE :
  224. begin
  225. if (!rx_active_w)
  226. next_state_r = STATE_RX_IDLE;
  227. end
  228. default :
  229. ;
  230. endcase
  231. end
  232. // Update state
  233. always @ (posedge clk_i or posedge rst_i)
  234. if (rst_i)
  235. state_q <= STATE_RX_IDLE;
  236. else if (!enable_i)
  237. state_q <= STATE_RX_IDLE;
  238. else
  239. state_q <= next_state_r;
  240. //-----------------------------------------------------------------
  241. // Handshake:
  242. //-----------------------------------------------------------------
  243. reg handshake_valid_q;
  244. always @ (posedge clk_i or posedge rst_i)
  245. if (rst_i)
  246. handshake_valid_q <= 1'b0;
  247. else if (state_q == STATE_RX_IDLE && data_ready_w)
  248. begin
  249. case (data_w)
  250. `PID_ACK, `PID_NAK, `PID_STALL, `PID_NYET:
  251. handshake_valid_q <= address_match_w;
  252. default :
  253. handshake_valid_q <= 1'b0;
  254. endcase
  255. end
  256. else
  257. handshake_valid_q <= 1'b0;
  258. assign handshake_valid_o = handshake_valid_q;
  259. //-----------------------------------------------------------------
  260. // SOF: Frame number
  261. //-----------------------------------------------------------------
  262. always @ (posedge clk_i or posedge rst_i)
  263. if (rst_i)
  264. frame_num_q <= `USB_FRAME_W'b0;
  265. else if (state_q == STATE_RX_SOF2 && data_ready_w)
  266. frame_num_q <= {3'b0, data_w};
  267. else if (state_q == STATE_RX_SOF3 && data_ready_w)
  268. frame_num_q <= {data_w[2:0], frame_num_q[7:0]};
  269. else if (!enable_i)
  270. frame_num_q <= `USB_FRAME_W'b0;
  271. assign frame_number_o = frame_num_q;
  272. reg frame_valid_q;
  273. always @ (posedge clk_i or posedge rst_i)
  274. if (rst_i)
  275. frame_valid_q <= 1'b0;
  276. else
  277. frame_valid_q <= (state_q == STATE_RX_SOF3 && data_ready_w);
  278. assign frame_valid_o = frame_valid_q;
  279. //-----------------------------------------------------------------
  280. // Token: PID
  281. //-----------------------------------------------------------------
  282. always @ (posedge clk_i or posedge rst_i)
  283. if (rst_i)
  284. token_pid_q <= `USB_PID_W'b0;
  285. else if (state_q == STATE_RX_IDLE && data_ready_w)
  286. token_pid_q <= data_w;
  287. else if (!enable_i)
  288. token_pid_q <= `USB_PID_W'b0;
  289. assign pid_o = token_pid_q;
  290. reg token_valid_q;
  291. always @ (posedge clk_i or posedge rst_i)
  292. if (rst_i)
  293. token_valid_q <= 1'b0;
  294. else
  295. token_valid_q <= (state_q == STATE_RX_TOKEN_COMPLETE) && address_match_w;
  296. assign token_valid_o = token_valid_q;
  297. //-----------------------------------------------------------------
  298. // Token: Device Address
  299. //-----------------------------------------------------------------
  300. always @ (posedge clk_i or posedge rst_i)
  301. if (rst_i)
  302. token_dev_q <= `USB_DEV_W'b0;
  303. else if (state_q == STATE_RX_TOKEN2 && data_ready_w)
  304. token_dev_q <= data_w[6:0];
  305. else if (!enable_i)
  306. token_dev_q <= `USB_DEV_W'b0;
  307. assign token_addr_o = token_dev_q;
  308. //-----------------------------------------------------------------
  309. // Token: Endpoint
  310. //-----------------------------------------------------------------
  311. always @ (posedge clk_i or posedge rst_i)
  312. if (rst_i)
  313. token_ep_q <= `USB_EP_W'b0;
  314. else if (state_q == STATE_RX_TOKEN2 && data_ready_w)
  315. token_ep_q[0] <= data_w[7];
  316. else if (state_q == STATE_RX_TOKEN3 && data_ready_w)
  317. token_ep_q[3:1] <= data_w[2:0];
  318. else if (!enable_i)
  319. token_ep_q <= `USB_EP_W'b0;
  320. assign token_ep_o = token_ep_q;
  321. assign token_crc_err_o = 1'b0;
  322. wire [7:0] input_data_w = data_w;
  323. wire input_ready_w = state_q == STATE_RX_DATA && data_ready_w && !crc_byte_w;
  324. //-----------------------------------------------------------------
  325. // CRC16: Generate CRC16 on incoming data bytes
  326. //-----------------------------------------------------------------
  327. reg [15:0] crc_sum_q;
  328. wire [15:0] crc_out_w;
  329. reg crc_err_q;
  330. `ifdef USB_RANDOM_CORRUPT
  331. reg [`USB_RANDOM_CORRUPT-1:0] corrupt_ctr_q;
  332. always @(posedge clk_i)
  333. corrupt_ctr_q <= corrupt_ctr_q + 1'b1;
  334. wire corrupt_rx = !corrupt_ctr_q;
  335. `else
  336. wire corrupt_rx = 1'b0;
  337. `endif
  338. usbf_crc16
  339. u_crc16
  340. (
  341. .crc_in_i(crc_sum_q),
  342. .din_i(data_w),
  343. .crc_out_o(crc_out_w)
  344. );
  345. always @ (posedge clk_i or posedge rst_i)
  346. if (rst_i)
  347. crc_sum_q <= 16'hFFFF;
  348. else if (state_q == STATE_RX_IDLE)
  349. crc_sum_q <= 16'hFFFF ^ corrupt_rx;
  350. else if (data_ready_w)
  351. crc_sum_q <= crc_out_w;
  352. always @ (posedge clk_i or posedge rst_i)
  353. if (rst_i)
  354. crc_err_q <= 1'b0;
  355. else if (state_q == STATE_RX_IDLE)
  356. crc_err_q <= 1'b0;
  357. else if (state_q == STATE_RX_DATA_COMPLETE && next_state_r == STATE_RX_IDLE)
  358. crc_err_q <= (crc_sum_q != 16'hB001);
  359. assign data_crc_err_o = crc_err_q;
  360. reg data_complete_q;
  361. always @ (posedge clk_i or posedge rst_i)
  362. if (rst_i)
  363. data_complete_q <= 1'b0;
  364. else if (state_q == STATE_RX_DATA_COMPLETE && next_state_r == STATE_RX_IDLE)
  365. data_complete_q <= 1'b1;
  366. else
  367. data_complete_q <= 1'b0;
  368. assign data_complete_o = data_complete_q;
  369. reg data_zlp_q;
  370. always @ (posedge clk_i or posedge rst_i)
  371. if (rst_i)
  372. data_zlp_q <= 1'b0;
  373. else if (state_q == STATE_RX_IDLE && next_state_r == STATE_RX_DATA)
  374. data_zlp_q <= 1'b1;
  375. else if (input_ready_w)
  376. data_zlp_q <= 1'b0;
  377. //-----------------------------------------------------------------
  378. // Data Output
  379. //-----------------------------------------------------------------
  380. reg valid_q;
  381. reg last_q;
  382. reg [7:0] data_q;
  383. reg mask_q;
  384. always @ (posedge clk_i or posedge rst_i)
  385. if (rst_i)
  386. begin
  387. valid_q <= 1'b0;
  388. data_q <= 8'b0;
  389. mask_q <= 1'b0;
  390. last_q <= 1'b0;
  391. end
  392. else
  393. begin
  394. valid_q <= input_ready_w || ((state_q == STATE_RX_DATA) && crc_byte_w && data_zlp_q);
  395. data_q <= input_data_w;
  396. mask_q <= input_ready_w;
  397. last_q <= (state_q == STATE_RX_DATA) && crc_byte_w;
  398. end
  399. // Data
  400. assign data_valid_o = valid_q;
  401. assign data_strb_o = mask_q;
  402. assign data_o = data_q;
  403. assign data_last_o = last_q | crc_byte_w;
  404. endmodule