usbf_sie_rx.v 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. //-----------------------------------------------------------------
  35. // Generated File
  36. //-----------------------------------------------------------------
  37. module usbf_sie_rx
  38. (
  39. // Inputs
  40. input clk_i
  41. ,input rst_i
  42. ,input enable_i
  43. ,input [ 7:0] utmi_data_i
  44. ,input utmi_rxvalid_i
  45. ,input utmi_rxactive_i
  46. ,input [ 6:0] current_addr_i
  47. // Outputs
  48. ,output [ 7:0] pid_o
  49. ,output frame_valid_o
  50. ,output [ 10:0] frame_number_o
  51. ,output token_valid_o
  52. ,output [ 6:0] token_addr_o
  53. ,output [ 3:0] token_ep_o
  54. ,output token_crc_err_o
  55. ,output handshake_valid_o
  56. ,output data_valid_o
  57. ,output data_strb_o
  58. ,output [ 7:0] data_o
  59. ,output data_last_o
  60. ,output data_crc_err_o
  61. ,output data_complete_o
  62. );
  63. //-----------------------------------------------------------------
  64. // Defines:
  65. //-----------------------------------------------------------------
  66. `include "usbf_defs.v"
  67. localparam STATE_W = 4;
  68. localparam STATE_RX_IDLE = 4'd0;
  69. localparam STATE_RX_TOKEN2 = 4'd1;
  70. localparam STATE_RX_TOKEN3 = 4'd2;
  71. localparam STATE_RX_TOKEN_COMPLETE = 4'd3;
  72. localparam STATE_RX_SOF2 = 4'd4;
  73. localparam STATE_RX_SOF3 = 4'd5;
  74. localparam STATE_RX_DATA = 4'd6;
  75. localparam STATE_RX_DATA_COMPLETE = 4'd7;
  76. localparam STATE_RX_IGNORED = 4'd8;
  77. reg [STATE_W-1:0] state_q;
  78. //-----------------------------------------------------------------
  79. // Wire / Regs
  80. //-----------------------------------------------------------------
  81. `define USB_FRAME_W 11
  82. reg [`USB_FRAME_W-1:0] frame_num_q;
  83. `define USB_DEV_W 7
  84. reg [`USB_DEV_W-1:0] token_dev_q;
  85. `define USB_EP_W 4
  86. reg [`USB_EP_W-1:0] token_ep_q;
  87. `define USB_PID_W 8
  88. reg [`USB_PID_W-1:0] token_pid_q;
  89. //-----------------------------------------------------------------
  90. // Data delay (to strip the CRC16 trailing bytes)
  91. //-----------------------------------------------------------------
  92. reg [31:0] data_buffer_q;
  93. reg [3:0] data_valid_q;
  94. reg [3:0] rx_active_q;
  95. wire shift_en_w = (utmi_rxvalid_i & utmi_rxactive_i) || !utmi_rxactive_i;
  96. always @ (posedge clk_i or posedge rst_i)
  97. if (rst_i)
  98. data_buffer_q <= 32'b0;
  99. else if (shift_en_w)
  100. data_buffer_q <= {utmi_data_i, data_buffer_q[31:8]};
  101. always @ (posedge clk_i or posedge rst_i)
  102. if (rst_i)
  103. data_valid_q <= 4'b0;
  104. else if (shift_en_w)
  105. data_valid_q <= {(utmi_rxvalid_i & utmi_rxactive_i), data_valid_q[3:1]};
  106. else
  107. data_valid_q <= {data_valid_q[3:1], 1'b0};
  108. reg [1:0] data_crc_q;
  109. always @ (posedge clk_i or posedge rst_i)
  110. if (rst_i)
  111. data_crc_q <= 2'b0;
  112. else if (shift_en_w)
  113. data_crc_q <= {!utmi_rxactive_i, data_crc_q[1]};
  114. always @ (posedge clk_i or posedge rst_i)
  115. if (rst_i)
  116. rx_active_q <= 4'b0;
  117. else
  118. rx_active_q <= {utmi_rxactive_i, rx_active_q[3:1]};
  119. wire [7:0] data_w = data_buffer_q[7:0];
  120. wire data_ready_w = data_valid_q[0];
  121. wire crc_byte_w = data_crc_q[0];
  122. wire rx_active_w = rx_active_q[0];
  123. wire address_match_w = (token_dev_q == current_addr_i);
  124. //-----------------------------------------------------------------
  125. // Next state
  126. //-----------------------------------------------------------------
  127. reg [STATE_W-1:0] next_state_r;
  128. always @ *
  129. begin
  130. next_state_r = state_q;
  131. case (state_q)
  132. //-----------------------------------------
  133. // IDLE
  134. //-----------------------------------------
  135. STATE_RX_IDLE :
  136. begin
  137. if (data_ready_w)
  138. begin
  139. // Decode PID
  140. case (data_w)
  141. `PID_OUT, `PID_IN, `PID_SETUP, `PID_PING:
  142. next_state_r = STATE_RX_TOKEN2;
  143. `PID_SOF:
  144. next_state_r = STATE_RX_SOF2;
  145. `PID_DATA0, `PID_DATA1, `PID_DATA2, `PID_MDATA:
  146. begin
  147. next_state_r = STATE_RX_DATA;
  148. end
  149. `PID_ACK, `PID_NAK, `PID_STALL, `PID_NYET:
  150. next_state_r = STATE_RX_IDLE;
  151. default : // SPLIT / ERR
  152. next_state_r = STATE_RX_IGNORED;
  153. endcase
  154. end
  155. end
  156. //-----------------------------------------
  157. // RX_IGNORED: Unknown / unsupported
  158. //-----------------------------------------
  159. STATE_RX_IGNORED :
  160. begin
  161. // Wait until the end of the packet
  162. if (!rx_active_w)
  163. next_state_r = STATE_RX_IDLE;
  164. end
  165. //-----------------------------------------
  166. // SOF (BYTE 2)
  167. //-----------------------------------------
  168. STATE_RX_SOF2 :
  169. begin
  170. if (data_ready_w)
  171. next_state_r = STATE_RX_SOF3;
  172. else if (!rx_active_w)
  173. next_state_r = STATE_RX_IDLE;
  174. end
  175. //-----------------------------------------
  176. // SOF (BYTE 3)
  177. //-----------------------------------------
  178. STATE_RX_SOF3 :
  179. begin
  180. if (data_ready_w || !rx_active_w)
  181. next_state_r = STATE_RX_IDLE;
  182. end
  183. //-----------------------------------------
  184. // TOKEN (IN/OUT/SETUP) (Address/Endpoint)
  185. //-----------------------------------------
  186. STATE_RX_TOKEN2 :
  187. begin
  188. if (data_ready_w)
  189. next_state_r = STATE_RX_TOKEN3;
  190. else if (!rx_active_w)
  191. next_state_r = STATE_RX_IDLE;
  192. end
  193. //-----------------------------------------
  194. // TOKEN (IN/OUT/SETUP) (Endpoint/CRC)
  195. //-----------------------------------------
  196. STATE_RX_TOKEN3 :
  197. begin
  198. if (data_ready_w)
  199. next_state_r = STATE_RX_TOKEN_COMPLETE;
  200. else if (!rx_active_w)
  201. next_state_r = STATE_RX_IDLE;
  202. end
  203. //-----------------------------------------
  204. // RX_TOKEN_COMPLETE
  205. //-----------------------------------------
  206. STATE_RX_TOKEN_COMPLETE :
  207. begin
  208. next_state_r = STATE_RX_IDLE;
  209. end
  210. //-----------------------------------------
  211. // RX_DATA
  212. //-----------------------------------------
  213. STATE_RX_DATA :
  214. begin
  215. // Receive complete
  216. if (crc_byte_w)
  217. next_state_r = STATE_RX_DATA_COMPLETE;
  218. end
  219. //-----------------------------------------
  220. // RX_DATA_COMPLETE
  221. //-----------------------------------------
  222. STATE_RX_DATA_COMPLETE :
  223. begin
  224. if (!rx_active_w)
  225. next_state_r = STATE_RX_IDLE;
  226. end
  227. default :
  228. ;
  229. endcase
  230. end
  231. // Update state
  232. always @ (posedge clk_i or posedge rst_i)
  233. if (rst_i)
  234. state_q <= STATE_RX_IDLE;
  235. else if (!enable_i)
  236. state_q <= STATE_RX_IDLE;
  237. else
  238. state_q <= next_state_r;
  239. //-----------------------------------------------------------------
  240. // Handshake:
  241. //-----------------------------------------------------------------
  242. reg handshake_valid_q;
  243. always @ (posedge clk_i or posedge rst_i)
  244. if (rst_i)
  245. handshake_valid_q <= 1'b0;
  246. else if (state_q == STATE_RX_IDLE && data_ready_w)
  247. begin
  248. case (data_w)
  249. `PID_ACK, `PID_NAK, `PID_STALL, `PID_NYET:
  250. handshake_valid_q <= address_match_w;
  251. default :
  252. handshake_valid_q <= 1'b0;
  253. endcase
  254. end
  255. else
  256. handshake_valid_q <= 1'b0;
  257. assign handshake_valid_o = handshake_valid_q;
  258. //-----------------------------------------------------------------
  259. // SOF: Frame number
  260. //-----------------------------------------------------------------
  261. always @ (posedge clk_i or posedge rst_i)
  262. if (rst_i)
  263. frame_num_q <= `USB_FRAME_W'b0;
  264. else if (state_q == STATE_RX_SOF2 && data_ready_w)
  265. frame_num_q <= {3'b0, data_w};
  266. else if (state_q == STATE_RX_SOF3 && data_ready_w)
  267. frame_num_q <= {data_w[2:0], frame_num_q[7:0]};
  268. else if (!enable_i)
  269. frame_num_q <= `USB_FRAME_W'b0;
  270. assign frame_number_o = frame_num_q;
  271. reg frame_valid_q;
  272. always @ (posedge clk_i or posedge rst_i)
  273. if (rst_i)
  274. frame_valid_q <= 1'b0;
  275. else
  276. frame_valid_q <= (state_q == STATE_RX_SOF3 && data_ready_w);
  277. assign frame_valid_o = frame_valid_q;
  278. //-----------------------------------------------------------------
  279. // Token: PID
  280. //-----------------------------------------------------------------
  281. always @ (posedge clk_i or posedge rst_i)
  282. if (rst_i)
  283. token_pid_q <= `USB_PID_W'b0;
  284. else if (state_q == STATE_RX_IDLE && data_ready_w)
  285. token_pid_q <= data_w;
  286. else if (!enable_i)
  287. token_pid_q <= `USB_PID_W'b0;
  288. assign pid_o = token_pid_q;
  289. reg token_valid_q;
  290. always @ (posedge clk_i or posedge rst_i)
  291. if (rst_i)
  292. token_valid_q <= 1'b0;
  293. else
  294. token_valid_q <= (state_q == STATE_RX_TOKEN_COMPLETE) && address_match_w;
  295. assign token_valid_o = token_valid_q;
  296. //-----------------------------------------------------------------
  297. // Token: Device Address
  298. //-----------------------------------------------------------------
  299. always @ (posedge clk_i or posedge rst_i)
  300. if (rst_i)
  301. token_dev_q <= `USB_DEV_W'b0;
  302. else if (state_q == STATE_RX_TOKEN2 && data_ready_w)
  303. token_dev_q <= data_w[6:0];
  304. else if (!enable_i)
  305. token_dev_q <= `USB_DEV_W'b0;
  306. assign token_addr_o = token_dev_q;
  307. //-----------------------------------------------------------------
  308. // Token: Endpoint
  309. //-----------------------------------------------------------------
  310. always @ (posedge clk_i or posedge rst_i)
  311. if (rst_i)
  312. token_ep_q <= `USB_EP_W'b0;
  313. else if (state_q == STATE_RX_TOKEN2 && data_ready_w)
  314. token_ep_q[0] <= data_w[7];
  315. else if (state_q == STATE_RX_TOKEN3 && data_ready_w)
  316. token_ep_q[3:1] <= data_w[2:0];
  317. else if (!enable_i)
  318. token_ep_q <= `USB_EP_W'b0;
  319. assign token_ep_o = token_ep_q;
  320. assign token_crc_err_o = 1'b0;
  321. wire [7:0] input_data_w = data_w;
  322. wire input_ready_w = state_q == STATE_RX_DATA && data_ready_w && !crc_byte_w;
  323. //-----------------------------------------------------------------
  324. // CRC16: Generate CRC16 on incoming data bytes
  325. //-----------------------------------------------------------------
  326. reg [15:0] crc_sum_q;
  327. wire [15:0] crc_out_w;
  328. reg crc_err_q;
  329. usbf_crc16
  330. u_crc16
  331. (
  332. .crc_in_i(crc_sum_q),
  333. .din_i(data_w),
  334. .crc_out_o(crc_out_w)
  335. );
  336. always @ (posedge clk_i or posedge rst_i)
  337. if (rst_i)
  338. crc_sum_q <= 16'hFFFF;
  339. else if (state_q == STATE_RX_IDLE)
  340. crc_sum_q <= 16'hFFFF;
  341. else if (data_ready_w)
  342. crc_sum_q <= crc_out_w;
  343. always @ (posedge clk_i or posedge rst_i)
  344. if (rst_i)
  345. crc_err_q <= 1'b0;
  346. else if (state_q == STATE_RX_IDLE)
  347. crc_err_q <= 1'b0;
  348. else if (state_q == STATE_RX_DATA_COMPLETE && next_state_r == STATE_RX_IDLE)
  349. crc_err_q <= (crc_sum_q != 16'hB001);
  350. assign data_crc_err_o = crc_err_q;
  351. reg data_complete_q;
  352. always @ (posedge clk_i or posedge rst_i)
  353. if (rst_i)
  354. data_complete_q <= 1'b0;
  355. else if (state_q == STATE_RX_DATA_COMPLETE && next_state_r == STATE_RX_IDLE)
  356. data_complete_q <= 1'b1;
  357. else
  358. data_complete_q <= 1'b0;
  359. assign data_complete_o = data_complete_q;
  360. reg data_zlp_q;
  361. always @ (posedge clk_i or posedge rst_i)
  362. if (rst_i)
  363. data_zlp_q <= 1'b0;
  364. else if (state_q == STATE_RX_IDLE && next_state_r == STATE_RX_DATA)
  365. data_zlp_q <= 1'b1;
  366. else if (input_ready_w)
  367. data_zlp_q <= 1'b0;
  368. //-----------------------------------------------------------------
  369. // Data Output
  370. //-----------------------------------------------------------------
  371. reg valid_q;
  372. reg last_q;
  373. reg [7:0] data_q;
  374. reg mask_q;
  375. always @ (posedge clk_i or posedge rst_i)
  376. if (rst_i)
  377. begin
  378. valid_q <= 1'b0;
  379. data_q <= 8'b0;
  380. mask_q <= 1'b0;
  381. last_q <= 1'b0;
  382. end
  383. else
  384. begin
  385. valid_q <= input_ready_w || ((state_q == STATE_RX_DATA) && crc_byte_w && data_zlp_q);
  386. data_q <= input_data_w;
  387. mask_q <= input_ready_w;
  388. last_q <= (state_q == STATE_RX_DATA) && crc_byte_w;
  389. end
  390. // Data
  391. assign data_valid_o = valid_q;
  392. assign data_strb_o = mask_q;
  393. assign data_o = data_q;
  394. assign data_last_o = last_q | crc_byte_w;
  395. endmodule