prod2u_p4_i2c_stop_start_read.v 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. `timescale 1ns/1ps
  2. module prod2u_p4_i2c_stop_start_read #(
  3. parameter integer HALF_PERIOD_CLKS = 125,
  4. parameter integer TIMEOUT_CLKS = 25000
  5. ) (
  6. input wire clk,
  7. input wire rst_n,
  8. input wire abort,
  9. output reg abort_done,
  10. input wire request_valid,
  11. output wire request_ready,
  12. input wire [6:0] request_device_addr,
  13. input wire [7:0] request_register,
  14. output reg response_valid,
  15. input wire response_ready,
  16. output reg [15:0] response_data,
  17. output reg [3:0] response_error,
  18. output wire busy,
  19. input wire scl_i,
  20. input wire sda_i,
  21. output reg scl_drive_low,
  22. output reg sda_drive_low
  23. );
  24. localparam [3:0] ERROR_NONE = 4'd0;
  25. localparam [3:0] ERROR_ADDRESS_NACK = 4'd1;
  26. localparam [3:0] ERROR_REGISTER_NACK = 4'd2;
  27. localparam [3:0] ERROR_READ_ADDRESS_NACK = 4'd3;
  28. localparam [3:0] ERROR_TIMEOUT = 4'd4;
  29. localparam [3:0] ERROR_STUCK_LOW = 4'd5;
  30. localparam [3:0] ERROR_ARBITRATION_LOST = 4'd6;
  31. localparam [1:0] TX_WRITE_ADDRESS = 2'd0;
  32. localparam [1:0] TX_REGISTER = 2'd1;
  33. localparam [1:0] TX_READ_ADDRESS = 2'd2;
  34. localparam [4:0] ST_IDLE = 5'd0;
  35. localparam [4:0] ST_BUS_WAIT_WRITE = 5'd1;
  36. localparam [4:0] ST_START_HOLD = 5'd2;
  37. localparam [4:0] ST_TX_BIT_LOW = 5'd3;
  38. localparam [4:0] ST_TX_BIT_HIGH = 5'd4;
  39. localparam [4:0] ST_TX_ACK_LOW = 5'd5;
  40. localparam [4:0] ST_TX_ACK_HIGH = 5'd6;
  41. localparam [4:0] ST_STOP_LOW = 5'd7;
  42. localparam [4:0] ST_STOP_SCL_HIGH = 5'd8;
  43. localparam [4:0] ST_STOP_RELEASE = 5'd9;
  44. localparam [4:0] ST_BUS_WAIT_READ = 5'd10;
  45. localparam [4:0] ST_RX_BIT_LOW = 5'd11;
  46. localparam [4:0] ST_RX_BIT_HIGH = 5'd12;
  47. localparam [4:0] ST_RX_ACK_LOW = 5'd13;
  48. localparam [4:0] ST_RX_ACK_HIGH = 5'd14;
  49. localparam [4:0] ST_RESPONSE = 5'd15;
  50. localparam [4:0] ST_ABORT_SCL_HIGH = 5'd16;
  51. localparam [4:0] ST_ABORT_RELEASE = 5'd17;
  52. (* async_reg = "true" *) reg [1:0] scl_sync;
  53. (* async_reg = "true" *) reg [1:0] sda_sync;
  54. (* keep = "true", syn_encoding = "sequential", fsm_encoding = "none" *)
  55. reg [4:0] state;
  56. reg [31:0] phase_count;
  57. reg [31:0] timeout_count;
  58. reg [6:0] device_addr;
  59. reg [7:0] register_addr;
  60. reg [7:0] tx_byte;
  61. reg [1:0] tx_stage;
  62. reg [2:0] bit_index;
  63. reg [15:0] rx_shift;
  64. reg rx_byte_index;
  65. reg [3:0] pending_error;
  66. reg after_stop_read;
  67. wire scl_sample;
  68. wire sda_sample;
  69. assign scl_sample = scl_sync[1];
  70. assign sda_sample = sda_sync[1];
  71. assign request_ready = (state == ST_IDLE) && !response_valid;
  72. assign busy = (state != ST_IDLE) || response_valid;
  73. generate
  74. if (HALF_PERIOD_CLKS < 1) begin : g_half_period_must_be_positive
  75. prod2u_p4_i2c_half_period_must_be_positive u_half_period_error();
  76. end
  77. if (TIMEOUT_CLKS < 1) begin : g_timeout_must_be_positive
  78. prod2u_p4_i2c_timeout_must_be_positive u_timeout_error();
  79. end
  80. endgenerate
  81. always @(posedge clk or negedge rst_n) begin
  82. if (!rst_n) begin
  83. scl_sync <= 2'b11;
  84. sda_sync <= 2'b11;
  85. end else begin
  86. scl_sync <= {scl_sync[0], scl_i};
  87. sda_sync <= {sda_sync[0], sda_i};
  88. end
  89. end
  90. always @(posedge clk or negedge rst_n) begin
  91. if (!rst_n) begin
  92. state <= ST_IDLE;
  93. phase_count <= 32'd0;
  94. timeout_count <= 32'd0;
  95. device_addr <= 7'd0;
  96. register_addr <= 8'd0;
  97. tx_byte <= 8'd0;
  98. tx_stage <= TX_WRITE_ADDRESS;
  99. bit_index <= 3'd0;
  100. rx_shift <= 16'd0;
  101. rx_byte_index <= 1'b0;
  102. pending_error <= ERROR_NONE;
  103. after_stop_read <= 1'b0;
  104. response_valid <= 1'b0;
  105. abort_done <= 1'b0;
  106. response_data <= 16'd0;
  107. response_error <= ERROR_NONE;
  108. scl_drive_low <= 1'b0;
  109. sda_drive_low <= 1'b0;
  110. end else begin
  111. abort_done <= 1'b0;
  112. if (abort && state != ST_ABORT_SCL_HIGH && state != ST_ABORT_RELEASE) begin
  113. response_valid <= 1'b0;
  114. response_data <= 16'd0;
  115. response_error <= ERROR_NONE;
  116. phase_count <= 32'd0;
  117. timeout_count <= 32'd0;
  118. if (state == ST_IDLE) begin
  119. state <= ST_IDLE;
  120. scl_drive_low <= 1'b0;
  121. sda_drive_low <= 1'b0;
  122. abort_done <= 1'b1;
  123. end else begin
  124. state <= ST_ABORT_SCL_HIGH;
  125. scl_drive_low <= 1'b0;
  126. sda_drive_low <= 1'b1;
  127. end
  128. end else if (state != ST_IDLE && state != ST_RESPONSE &&
  129. state != ST_ABORT_SCL_HIGH && state != ST_ABORT_RELEASE &&
  130. timeout_count >= TIMEOUT_CLKS - 1) begin
  131. state <= ST_RESPONSE;
  132. phase_count <= 32'd0;
  133. timeout_count <= 32'd0;
  134. response_valid <= 1'b1;
  135. response_data <= 16'd0;
  136. response_error <= ((state == ST_BUS_WAIT_WRITE) || (state == ST_BUS_WAIT_READ)) ?
  137. ERROR_STUCK_LOW : ERROR_TIMEOUT;
  138. scl_drive_low <= 1'b0;
  139. sda_drive_low <= 1'b0;
  140. end else begin
  141. if (state != ST_IDLE && state != ST_RESPONSE) begin
  142. timeout_count <= timeout_count + 32'd1;
  143. end
  144. case (state)
  145. ST_IDLE: begin
  146. scl_drive_low <= 1'b0;
  147. sda_drive_low <= 1'b0;
  148. phase_count <= 32'd0;
  149. timeout_count <= 32'd0;
  150. if (request_valid) begin
  151. device_addr <= request_device_addr;
  152. register_addr <= request_register;
  153. response_data <= 16'd0;
  154. response_error <= ERROR_NONE;
  155. pending_error <= ERROR_NONE;
  156. after_stop_read <= 1'b0;
  157. rx_shift <= 16'd0;
  158. state <= ST_BUS_WAIT_WRITE;
  159. end
  160. end
  161. ST_BUS_WAIT_WRITE,
  162. ST_BUS_WAIT_READ: begin
  163. scl_drive_low <= 1'b0;
  164. sda_drive_low <= 1'b0;
  165. if (scl_sample && sda_sample) begin
  166. if (phase_count >= HALF_PERIOD_CLKS - 1) begin
  167. phase_count <= 32'd0;
  168. sda_drive_low <= 1'b1;
  169. tx_stage <= (state == ST_BUS_WAIT_WRITE) ?
  170. TX_WRITE_ADDRESS : TX_READ_ADDRESS;
  171. state <= ST_START_HOLD;
  172. end else begin
  173. phase_count <= phase_count + 32'd1;
  174. end
  175. end else begin
  176. phase_count <= 32'd0;
  177. end
  178. end
  179. ST_START_HOLD: begin
  180. if (scl_sample) begin
  181. if (phase_count >= HALF_PERIOD_CLKS - 1) begin
  182. phase_count <= 32'd0;
  183. tx_byte <= (tx_stage == TX_READ_ADDRESS) ?
  184. {device_addr, 1'b1} : {device_addr, 1'b0};
  185. bit_index <= 3'd7;
  186. scl_drive_low <= 1'b1;
  187. state <= ST_TX_BIT_LOW;
  188. end else begin
  189. phase_count <= phase_count + 32'd1;
  190. end
  191. end
  192. end
  193. ST_TX_BIT_LOW: begin
  194. if (phase_count == 32'd0) begin
  195. sda_drive_low <= ~tx_byte[bit_index];
  196. end
  197. if (phase_count >= HALF_PERIOD_CLKS - 1) begin
  198. phase_count <= 32'd0;
  199. scl_drive_low <= 1'b0;
  200. state <= ST_TX_BIT_HIGH;
  201. end else begin
  202. phase_count <= phase_count + 32'd1;
  203. end
  204. end
  205. ST_TX_BIT_HIGH: begin
  206. if (scl_sample) begin
  207. if (tx_byte[bit_index] && !sda_sample) begin
  208. state <= ST_RESPONSE;
  209. timeout_count <= 32'd0;
  210. response_valid <= 1'b1;
  211. response_data <= 16'd0;
  212. response_error <= ERROR_ARBITRATION_LOST;
  213. scl_drive_low <= 1'b0;
  214. sda_drive_low <= 1'b0;
  215. end else if (phase_count >= HALF_PERIOD_CLKS - 1) begin
  216. phase_count <= 32'd0;
  217. scl_drive_low <= 1'b1;
  218. if (bit_index == 3'd0) begin
  219. state <= ST_TX_ACK_LOW;
  220. end else begin
  221. bit_index <= bit_index - 3'd1;
  222. state <= ST_TX_BIT_LOW;
  223. end
  224. end else begin
  225. phase_count <= phase_count + 32'd1;
  226. end
  227. end
  228. end
  229. ST_TX_ACK_LOW: begin
  230. if (phase_count == 32'd0) begin
  231. sda_drive_low <= 1'b0;
  232. end
  233. if (phase_count >= HALF_PERIOD_CLKS - 1) begin
  234. phase_count <= 32'd0;
  235. scl_drive_low <= 1'b0;
  236. state <= ST_TX_ACK_HIGH;
  237. end else begin
  238. phase_count <= phase_count + 32'd1;
  239. end
  240. end
  241. ST_TX_ACK_HIGH: begin
  242. if (scl_sample) begin
  243. if (phase_count >= HALF_PERIOD_CLKS - 1) begin
  244. phase_count <= 32'd0;
  245. scl_drive_low <= 1'b1;
  246. if (sda_sample) begin
  247. case (tx_stage)
  248. TX_WRITE_ADDRESS: pending_error <= ERROR_ADDRESS_NACK;
  249. TX_REGISTER: pending_error <= ERROR_REGISTER_NACK;
  250. default: pending_error <= ERROR_READ_ADDRESS_NACK;
  251. endcase
  252. after_stop_read <= 1'b0;
  253. state <= ST_STOP_LOW;
  254. end else if (tx_stage == TX_WRITE_ADDRESS) begin
  255. tx_byte <= register_addr;
  256. tx_stage <= TX_REGISTER;
  257. bit_index <= 3'd7;
  258. state <= ST_TX_BIT_LOW;
  259. end else if (tx_stage == TX_REGISTER) begin
  260. pending_error <= ERROR_NONE;
  261. after_stop_read <= 1'b1;
  262. state <= ST_STOP_LOW;
  263. end else begin
  264. rx_shift <= 16'd0;
  265. rx_byte_index <= 1'b0;
  266. bit_index <= 3'd7;
  267. state <= ST_RX_BIT_LOW;
  268. end
  269. end else begin
  270. phase_count <= phase_count + 32'd1;
  271. end
  272. end
  273. end
  274. ST_RX_BIT_LOW: begin
  275. if (phase_count == 32'd0) begin
  276. sda_drive_low <= 1'b0;
  277. end
  278. if (phase_count >= HALF_PERIOD_CLKS - 1) begin
  279. phase_count <= 32'd0;
  280. scl_drive_low <= 1'b0;
  281. state <= ST_RX_BIT_HIGH;
  282. end else begin
  283. phase_count <= phase_count + 32'd1;
  284. end
  285. end
  286. ST_RX_BIT_HIGH: begin
  287. if (scl_sample) begin
  288. if (phase_count >= HALF_PERIOD_CLKS - 1) begin
  289. phase_count <= 32'd0;
  290. rx_shift <= {rx_shift[14:0], sda_sample};
  291. scl_drive_low <= 1'b1;
  292. if (bit_index == 3'd0) begin
  293. state <= ST_RX_ACK_LOW;
  294. end else begin
  295. bit_index <= bit_index - 3'd1;
  296. state <= ST_RX_BIT_LOW;
  297. end
  298. end else begin
  299. phase_count <= phase_count + 32'd1;
  300. end
  301. end
  302. end
  303. ST_RX_ACK_LOW: begin
  304. if (phase_count == 32'd0) begin
  305. sda_drive_low <= (rx_byte_index == 1'b0);
  306. end
  307. if (phase_count >= HALF_PERIOD_CLKS - 1) begin
  308. phase_count <= 32'd0;
  309. scl_drive_low <= 1'b0;
  310. state <= ST_RX_ACK_HIGH;
  311. end else begin
  312. phase_count <= phase_count + 32'd1;
  313. end
  314. end
  315. ST_RX_ACK_HIGH: begin
  316. if (scl_sample) begin
  317. if (phase_count >= HALF_PERIOD_CLKS - 1) begin
  318. phase_count <= 32'd0;
  319. scl_drive_low <= 1'b1;
  320. if (rx_byte_index == 1'b0) begin
  321. rx_byte_index <= 1'b1;
  322. bit_index <= 3'd7;
  323. state <= ST_RX_BIT_LOW;
  324. end else begin
  325. pending_error <= ERROR_NONE;
  326. after_stop_read <= 1'b0;
  327. state <= ST_STOP_LOW;
  328. end
  329. end else begin
  330. phase_count <= phase_count + 32'd1;
  331. end
  332. end
  333. end
  334. ST_STOP_LOW: begin
  335. scl_drive_low <= 1'b1;
  336. sda_drive_low <= 1'b1;
  337. if (phase_count >= HALF_PERIOD_CLKS - 1) begin
  338. phase_count <= 32'd0;
  339. scl_drive_low <= 1'b0;
  340. state <= ST_STOP_SCL_HIGH;
  341. end else begin
  342. phase_count <= phase_count + 32'd1;
  343. end
  344. end
  345. ST_STOP_SCL_HIGH: begin
  346. if (scl_sample) begin
  347. if (phase_count >= HALF_PERIOD_CLKS - 1) begin
  348. phase_count <= 32'd0;
  349. sda_drive_low <= 1'b0;
  350. state <= ST_STOP_RELEASE;
  351. end else begin
  352. phase_count <= phase_count + 32'd1;
  353. end
  354. end
  355. end
  356. ST_STOP_RELEASE: begin
  357. scl_drive_low <= 1'b0;
  358. sda_drive_low <= 1'b0;
  359. if (scl_sample && sda_sample) begin
  360. if (phase_count >= HALF_PERIOD_CLKS - 1) begin
  361. phase_count <= 32'd0;
  362. if (after_stop_read && pending_error == ERROR_NONE) begin
  363. after_stop_read <= 1'b0;
  364. state <= ST_BUS_WAIT_READ;
  365. end else begin
  366. state <= ST_RESPONSE;
  367. response_valid <= 1'b1;
  368. response_data <= (pending_error == ERROR_NONE) ? rx_shift : 16'd0;
  369. response_error <= pending_error;
  370. timeout_count <= 32'd0;
  371. end
  372. end else begin
  373. phase_count <= phase_count + 32'd1;
  374. end
  375. end else begin
  376. phase_count <= 32'd0;
  377. end
  378. end
  379. ST_RESPONSE: begin
  380. scl_drive_low <= 1'b0;
  381. sda_drive_low <= 1'b0;
  382. if (response_valid && response_ready) begin
  383. response_valid <= 1'b0;
  384. state <= ST_IDLE;
  385. end
  386. end
  387. ST_ABORT_SCL_HIGH: begin
  388. scl_drive_low <= 1'b0;
  389. sda_drive_low <= 1'b1;
  390. if (scl_sample || timeout_count >= TIMEOUT_CLKS - 1) begin
  391. timeout_count <= 32'd0;
  392. state <= ST_ABORT_RELEASE;
  393. end
  394. end
  395. ST_ABORT_RELEASE: begin
  396. scl_drive_low <= 1'b0;
  397. sda_drive_low <= 1'b0;
  398. timeout_count <= 32'd0;
  399. state <= ST_IDLE;
  400. abort_done <= 1'b1;
  401. end
  402. default: begin
  403. state <= ST_IDLE;
  404. scl_drive_low <= 1'b0;
  405. sda_drive_low <= 1'b0;
  406. end
  407. endcase
  408. end
  409. end
  410. end
  411. endmodule