multi_uart_ip.v 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. module multi_uart_ip #(parameter
  2. UART_GROUP = 2, // Number of UART groups
  3. UART_COUNT = 6, // Number of UART instances in each group
  4. UART_TOTAL = UART_GROUP * UART_COUNT
  5. ) (
  6. // input [UART_TOTAL-1:0] uart_rxd,
  7. // output [UART_TOTAL-1:0] uart_txd,
  8. output tri0 SIM_CLK,
  9. inout [11:0] SIM_IO,
  10. inout SIM_IO_12,
  11. inout SIM_IO_13,
  12. inout SIM_IO_14,
  13. inout SIM_IO_15,
  14. input uart14_rx,
  15. output tri0 uart14_tx,
  16. input uart15_rx,
  17. output tri0 uart15_tx,
  18. output tri0 [3:0] ext_int_in,
  19. output tri0 [5:0] gpio_int_g0_in,
  20. output tri0 [5:0] gpio_int_g1_in,
  21. output tri0 rxd_12_ip_in,
  22. output tri0 rxd_13_ip_in,
  23. output tri0 rxd_14_ip_in,
  24. output tri0 rxd_15_ip_in,
  25. input txd_12_ip_out_data,
  26. input txd_12_ip_out_en,
  27. input txd_13_ip_out_data,
  28. input txd_13_ip_out_en,
  29. input txd_14_ip_out_data,
  30. input txd_14_ip_out_en,
  31. input txd_15_ip_out_data,
  32. input txd_15_ip_out_en,
  33. input txen_12_ip_out_data,
  34. input txen_12_ip_out_en,
  35. input txen_13_ip_out_data,
  36. input txen_13_ip_out_en,
  37. input txen_14_ip_out_data,
  38. input txen_14_ip_out_en,
  39. input txen_15_ip_out_data,
  40. input txen_15_ip_out_en,
  41. input sys_clock,
  42. input bus_clock,
  43. input resetn,
  44. input stop,
  45. input [1:0] mem_ahb_htrans,
  46. input mem_ahb_hready,
  47. input mem_ahb_hwrite,
  48. input [31:0] mem_ahb_haddr,
  49. input [2:0] mem_ahb_hsize,
  50. input [2:0] mem_ahb_hburst,
  51. input [31:0] mem_ahb_hwdata,
  52. output tri1 mem_ahb_hreadyout,
  53. output tri0 mem_ahb_hresp,
  54. output tri0 [31:0] mem_ahb_hrdata,
  55. output tri0 slave_ahb_hsel,
  56. output tri1 slave_ahb_hready,
  57. input slave_ahb_hreadyout,
  58. output tri0 [1:0] slave_ahb_htrans,
  59. output tri0 [2:0] slave_ahb_hsize,
  60. output tri0 [2:0] slave_ahb_hburst,
  61. output tri0 slave_ahb_hwrite,
  62. output tri0 [31:0] slave_ahb_haddr,
  63. output tri0 [31:0] slave_ahb_hwdata,
  64. input slave_ahb_hresp,
  65. input [31:0] slave_ahb_hrdata,
  66. output tri0 [3:0] ext_dma_DMACBREQ,
  67. output tri0 [3:0] ext_dma_DMACLBREQ,
  68. output tri0 [3:0] ext_dma_DMACSREQ,
  69. output tri0 [3:0] ext_dma_DMACLSREQ,
  70. input [3:0] ext_dma_DMACCLR,
  71. input [3:0] ext_dma_DMACTC,
  72. output tri0 [3:0] local_int
  73. );
  74. reg [UART_TOTAL-1:0] uart_tx_busy;
  75. wire [UART_TOTAL-1:0] uart_rxd;
  76. reg [UART_TOTAL-1:0] uart_txd;
  77. assign uart_rxd = uart_tx_busy | SIM_IO; // 接收
  78. genvar i;
  79. generate
  80. for (i = 0; i < UART_TOTAL; i = i + 1) begin : gen_sim_io
  81. assign SIM_IO[i] = uart_tx_busy[i] ? uart_txd[i] : 1'bz; // 发送
  82. end
  83. endgenerate
  84. assign SIM_IO_12 = (txen_12_ip_out_en && txen_12_ip_out_data) ? (txd_12_ip_out_en ? txd_12_ip_out_data : 1'bz) : 1'bz;
  85. assign rxd_12_ip_in = (txen_12_ip_out_en && txen_12_ip_out_data) ? 1'bz : SIM_IO_12;
  86. assign SIM_IO_13 = (txen_13_ip_out_en && txen_13_ip_out_data) ? (txd_13_ip_out_en ? txd_13_ip_out_data : 1'bz) : 1'bz;
  87. assign rxd_13_ip_in = (txen_13_ip_out_en && txen_13_ip_out_data) ? 1'bz : SIM_IO_13;
  88. // assign SIM_IO_14 = (txen_14_ip_out_en && txen_14_ip_out_data) ? (txd_14_ip_out_en ? txd_14_ip_out_data : 1'bz) : 1'bz;
  89. // assign rxd_14_ip_in = (txen_14_ip_out_en && txen_14_ip_out_data) ? 1'bz : SIM_IO_14;
  90. assign SIM_IO_14 = (txen_14_ip_out_en && txen_14_ip_out_data) ? (txd_14_ip_out_en ? txd_14_ip_out_data : 1'bz) : 1'bz;
  91. assign rxd_14_ip_in = txen_14_ip_out_en ? ((txen_14_ip_out_data) ? 1'bz : SIM_IO_14) : uart14_rx;
  92. assign uart14_tx = (!txen_14_ip_out_en) ? txd_14_ip_out_data : 1'bz;
  93. assign SIM_IO_15 = (txen_15_ip_out_en && txen_15_ip_out_data) ? (txd_15_ip_out_en ? txd_15_ip_out_data : 1'bz) : 1'bz;
  94. assign rxd_15_ip_in = txen_15_ip_out_en ? ((txen_15_ip_out_data) ? 1'bz : SIM_IO_15) : uart15_rx;
  95. // assign uart15_tx = (!txen_15_ip_out_en) ? (txd_15_ip_out_en ? txd_15_ip_out_data : 1'bz) : 1'bz;
  96. assign uart15_tx = (!txen_15_ip_out_en) ? txd_15_ip_out_data : 1'bz;
  97. parameter DATA_WIDTH = 8;
  98. parameter FIFO_DEPTH = 1;
  99. wire [UART_TOTAL-1:0] tx_dma_clr;
  100. wire [UART_TOTAL-1:0] tx_dma_req;
  101. wire [UART_TOTAL-1:0] rx_dma_clr;
  102. wire [UART_TOTAL-1:0] rx_dma_req;
  103. wire [UART_TOTAL-1:0] interrupts;
  104. // Different ways to generate interrupts.
  105. // assign local_int = interrupts[3:0];
  106. // assign ext_int_in = ~interrupts[7:4]; // External interrupts are active low.
  107. // assign gpio_int_in = interrupts[11:8];
  108. assign gpio_int_g0_in = interrupts[ 0+:UART_COUNT];
  109. assign gpio_int_g1_in = interrupts[UART_COUNT+:UART_COUNT];
  110. // Only 4 DMA requests are available. Can be used for any UART instance.
  111. assign ext_dma_DMACBREQ = { tx_dma_req[1:0], rx_dma_req[1:0] };
  112. assign { tx_dma_clr[1:0], rx_dma_clr[1:0] } = ext_dma_DMACCLR ;
  113. parameter APB_SEL_BITS = $clog2(UART_GROUP);
  114. parameter APB_ADDR_BITS = 12;
  115. parameter AHB_ADDR_BITS = APB_ADDR_BITS + APB_SEL_BITS;
  116. wire mem_apb_psel;
  117. wire mem_apb_penable;
  118. wire mem_apb_pwrite;
  119. wire [AHB_ADDR_BITS-1:0] mem_apb_paddr;
  120. wire [31:0] mem_apb_pwdata;
  121. wire [3:0] mem_apb_pstrb;
  122. wire [2:0] mem_apb_pprot;
  123. wire mem_apb_pready;
  124. wire mem_apb_pslverr;
  125. wire [31:0] mem_apb_prdata;
  126. wire [APB_ADDR_BITS-1:0] mem_apb_address = mem_apb_paddr[APB_ADDR_BITS-1:0];
  127. wire [UART_GROUP-1:0] mem_apb_onehot = 1'b1 << (mem_apb_paddr >> APB_ADDR_BITS);
  128. wire [UART_GROUP-1:0] uart_apb_psel;
  129. wire [UART_GROUP-1:0] uart_apb_penable;
  130. wire [UART_GROUP-1:0] uart_apb_pwrite;
  131. wire [UART_GROUP*APB_ADDR_BITS-1:0] uart_apb_paddr;
  132. wire [UART_GROUP*32-1:0] uart_apb_pwdata;
  133. wire [UART_GROUP*32-1:0] uart_apb_prdata;
  134. wire [UART_GROUP-1:0] uart_apb_pready;
  135. wire [UART_GROUP-1:0] uart_apb_pslverr = 0;
  136. ahb2apb #(AHB_ADDR_BITS) u_ahb2apb(
  137. .reset (!resetn ),
  138. .ahb_clock (sys_clock ),
  139. .ahb_hmastlock(1'b0 ),
  140. .ahb_htrans (mem_ahb_htrans ),
  141. .ahb_hsel (1'b1 ),
  142. .ahb_hready (mem_ahb_hready ),
  143. .ahb_hwrite (mem_ahb_hwrite ),
  144. .ahb_haddr (mem_ahb_haddr[AHB_ADDR_BITS-1:0]),
  145. .ahb_hsize (mem_ahb_hsize ),
  146. .ahb_hburst (mem_ahb_hburst ),
  147. .ahb_hprot (4'b0011 ),
  148. .ahb_hwdata (mem_ahb_hwdata ),
  149. .ahb_hrdata (mem_ahb_hrdata ),
  150. .ahb_hreadyout(mem_ahb_hreadyout ),
  151. .ahb_hresp (mem_ahb_hresp ),
  152. .apb_clock (bus_clock ),
  153. .apb_psel (mem_apb_psel ),
  154. .apb_penable (mem_apb_penable ),
  155. .apb_pwrite (mem_apb_pwrite ),
  156. .apb_paddr (mem_apb_paddr ),
  157. .apb_pwdata (mem_apb_pwdata ),
  158. .apb_pstrb (mem_apb_pstrb ),
  159. .apb_pprot (mem_apb_pprot ),
  160. .apb_pready (mem_apb_pready ),
  161. .apb_pslverr (mem_apb_pslverr ),
  162. .apb_prdata (mem_apb_prdata )
  163. );
  164. apb_mux #(UART_GROUP, APB_ADDR_BITS) u_apb_mux(
  165. .apb_clock (bus_clock ),
  166. .apb_resetn (resetn ),
  167. .apb_in_psel (mem_apb_psel ),
  168. .apb_in_penable (mem_apb_penable ),
  169. .apb_in_pwrite (mem_apb_pwrite ),
  170. .apb_in_paddr (mem_apb_address ),
  171. .apb_in_pwdata (mem_apb_pwdata ),
  172. .apb_in_prdata (mem_apb_prdata ),
  173. .apb_in_pready (mem_apb_pready ),
  174. .apb_in_pslverr (mem_apb_pslverr ),
  175. .apb_out_psel (uart_apb_psel ),
  176. .apb_out_penable(uart_apb_penable ),
  177. .apb_out_pwrite (uart_apb_pwrite ),
  178. .apb_out_paddr (uart_apb_paddr ),
  179. .apb_out_pwdata (uart_apb_pwdata ),
  180. .apb_out_prdata (uart_apb_prdata ),
  181. .apb_out_pready (uart_apb_pready ),
  182. .apb_out_pslverr(uart_apb_pslverr ),
  183. .apb_select (mem_apb_onehot )
  184. );
  185. multi_uart #(.CNT(UART_COUNT), .DATA_WIDTH(DATA_WIDTH), .FIFO_DEPTH(FIFO_DEPTH)) u_uart[UART_GROUP-1:0](
  186. .apb_clock (bus_clock ),
  187. .apb_resetn (resetn ),
  188. .apb_psel (uart_apb_psel ),
  189. .apb_penable(uart_apb_penable),
  190. .apb_pwrite (uart_apb_pwrite ),
  191. .apb_paddr (uart_apb_paddr ),
  192. .apb_pwdata (uart_apb_pwdata ),
  193. .apb_prdata (uart_apb_prdata ),
  194. .apb_pready (uart_apb_pready ),
  195. .uart_txd (uart_txd ),
  196. .uart_rxd (uart_rxd ),
  197. .tx_dma_clr (tx_dma_clr ),
  198. .tx_dma_req (tx_dma_req ),
  199. .rx_dma_clr (rx_dma_clr ),
  200. .rx_dma_req (rx_dma_req ),
  201. .interrupts (interrupts ),
  202. .uart_tx_busy(uart_tx_busy )
  203. );
  204. assign slave_ahb_hready = 1'b1;
  205. // 串口clk时钟1.5MHz(对应波特率4000)
  206. // 串口clk时钟15MHz (对应波特率40000)
  207. parameter SYSCLK_FREQ = 240_000_000;
  208. parameter SIMCLK_FREQ = 15_000_000;
  209. parameter SIM_CLK_PER = (SYSCLK_FREQ/SIMCLK_FREQ/2);
  210. reg [7:0] sim_clk_cnt;
  211. reg sim_clk_reg;
  212. always @(posedge sys_clock or negedge resetn) begin
  213. if (!resetn) begin
  214. sim_clk_cnt <= 8'h0;
  215. sim_clk_reg <= 8'h0;
  216. end else if (sim_clk_cnt < (SIM_CLK_PER - 1)) begin
  217. sim_clk_cnt <= sim_clk_cnt + 1'b1;
  218. end else begin
  219. sim_clk_reg <= !sim_clk_reg;
  220. sim_clk_cnt <= 8'h0;
  221. end
  222. end
  223. assign SIM_CLK = sim_clk_reg;
  224. endmodule