trace_packet_tx.v 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. `timescale 1ns/1ps
  2. // -----------------------------------------------------------------------------
  3. // 16-byte UART Trace packet transmitter.
  4. //
  5. // This intentionally follows the packet format used by the user's previous
  6. // EF2L45 UART Trace project:
  7. //
  8. // Byte0 A5
  9. // Byte1 5A
  10. // Byte2 packet type
  11. // Byte3-4 sequence (big-endian)
  12. // Byte5-6 event_id (big-endian)
  13. // Byte7-10 timestamp (big-endian)
  14. // Byte11-14 data (big-endian)
  15. // Byte15 CRC-8
  16. //
  17. // CRC-8:
  18. // polynomial = 0x07
  19. // init = 0x00
  20. // covers bytes 2..14
  21. //
  22. // For this firmware packet_type is normally 0x82 (TRACE).
  23. // -----------------------------------------------------------------------------
  24. module trace_packet_tx #(
  25. parameter integer CLOCK_HZ = 25_000_000,
  26. parameter integer BAUD = 115_200
  27. )(
  28. input wire clk,
  29. input wire reset_n,
  30. input wire start,
  31. input wire [7:0] packet_type,
  32. input wire [15:0] seq,
  33. input wire [15:0] event_id,
  34. input wire [31:0] timestamp,
  35. input wire [31:0] data,
  36. output wire uart_tx,
  37. output reg busy
  38. );
  39. reg uart_start;
  40. reg [7:0] uart_data;
  41. wire uart_busy;
  42. reg [7:0] type_latched;
  43. reg [15:0] seq_latched;
  44. reg [15:0] event_latched;
  45. reg [31:0] time_latched;
  46. reg [31:0] data_latched;
  47. reg [4:0] byte_index;
  48. reg [7:0] crc_reg;
  49. localparam [1:0]
  50. S_IDLE = 2'd0,
  51. S_SEND = 2'd1,
  52. S_WAIT = 2'd2;
  53. reg [1:0] state;
  54. uart_tx #(
  55. .CLOCK_HZ(CLOCK_HZ),
  56. .BAUD(BAUD)
  57. ) u_uart_tx (
  58. .clk (clk),
  59. .reset_n(reset_n),
  60. .start (uart_start),
  61. .data (uart_data),
  62. .tx (uart_tx),
  63. .busy (uart_busy)
  64. );
  65. function [7:0] crc8_next;
  66. input [7:0] crc_in;
  67. input [7:0] data_in;
  68. integer i;
  69. reg [7:0] c;
  70. begin
  71. c = crc_in ^ data_in;
  72. for (i = 0; i < 8; i = i + 1) begin
  73. if (c[7])
  74. c = (c << 1) ^ 8'h07;
  75. else
  76. c = (c << 1);
  77. end
  78. crc8_next = c;
  79. end
  80. endfunction
  81. function [7:0] packet_byte;
  82. input [4:0] idx;
  83. begin
  84. case (idx)
  85. 5'd0: packet_byte = 8'hA5;
  86. 5'd1: packet_byte = 8'h5A;
  87. 5'd2: packet_byte = type_latched;
  88. 5'd3: packet_byte = seq_latched[15:8];
  89. 5'd4: packet_byte = seq_latched[7:0];
  90. 5'd5: packet_byte = event_latched[15:8];
  91. 5'd6: packet_byte = event_latched[7:0];
  92. 5'd7: packet_byte = time_latched[31:24];
  93. 5'd8: packet_byte = time_latched[23:16];
  94. 5'd9: packet_byte = time_latched[15:8];
  95. 5'd10: packet_byte = time_latched[7:0];
  96. 5'd11: packet_byte = data_latched[31:24];
  97. 5'd12: packet_byte = data_latched[23:16];
  98. 5'd13: packet_byte = data_latched[15:8];
  99. 5'd14: packet_byte = data_latched[7:0];
  100. default: packet_byte = 8'h00;
  101. endcase
  102. end
  103. endfunction
  104. reg [7:0] current_byte;
  105. always @(posedge clk or negedge reset_n) begin
  106. if (!reset_n) begin
  107. uart_start <= 1'b0;
  108. uart_data <= 8'd0;
  109. type_latched <= 8'd0;
  110. seq_latched <= 16'd0;
  111. event_latched <= 16'd0;
  112. time_latched <= 32'd0;
  113. data_latched <= 32'd0;
  114. byte_index <= 5'd0;
  115. crc_reg <= 8'd0;
  116. current_byte <= 8'd0;
  117. state <= S_IDLE;
  118. busy <= 1'b0;
  119. end else begin
  120. uart_start <= 1'b0;
  121. case (state)
  122. S_IDLE: begin
  123. busy <= 1'b0;
  124. if (start) begin
  125. type_latched <= packet_type;
  126. seq_latched <= seq;
  127. event_latched <= event_id;
  128. time_latched <= timestamp;
  129. data_latched <= data;
  130. byte_index <= 5'd0;
  131. crc_reg <= 8'd0;
  132. busy <= 1'b1;
  133. state <= S_SEND;
  134. end
  135. end
  136. S_SEND: begin
  137. busy <= 1'b1;
  138. if (!uart_busy) begin
  139. if (byte_index == 5'd15)
  140. current_byte = crc_reg;
  141. else
  142. current_byte = packet_byte(byte_index);
  143. uart_data <= current_byte;
  144. uart_start <= 1'b1;
  145. if ((byte_index >= 5'd2) &&
  146. (byte_index <= 5'd14))
  147. crc_reg <= crc8_next(crc_reg, current_byte);
  148. state <= S_WAIT;
  149. end
  150. end
  151. S_WAIT: begin
  152. busy <= 1'b1;
  153. // Wait until the just-launched UART byte has fully left.
  154. if (!uart_busy && !uart_start) begin
  155. if (byte_index == 5'd15) begin
  156. busy <= 1'b0;
  157. state <= S_IDLE;
  158. end else begin
  159. byte_index <= byte_index + 1'b1;
  160. state <= S_SEND;
  161. end
  162. end
  163. end
  164. default: begin
  165. busy <= 1'b0;
  166. state <= S_IDLE;
  167. end
  168. endcase
  169. end
  170. end
  171. endmodule