esp.sv 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // Communication with ESP32
  3. //
  4. // Components are:
  5. // a. serial interface
  6. // b. SPI interface (not yet implemented)
  7. // c. common open drain IRQ line
  8. // d. ESP32 EN and IO0 lines
  9. //
  10. module esp (
  11. input rst_n,
  12. input clk,
  13. input cpu_valid,
  14. input [4:0] cpu_addr,
  15. input [3:0] cpu_wstrb,
  16. input [31:0] cpu_wdata,
  17. output reg [31:0] cpu_rdata,
  18. output reg irq,
  19. input tty_rx,
  20. output tty_tx,
  21. output esp_en, // ESP reset#
  22. inout esp_int,
  23. inout esp_io0,
  24. inout spi_clk,
  25. inout spi_miso,
  26. inout spi_mosi,
  27. inout spi_cs_esp_n,
  28. inout spi_cs_flash_n
  29. );
  30. wire [31:0] cpu_reg = cpu_valid << cpu_addr;
  31. wire cpu_read = ~|cpu_wstrb;
  32. reg tx_break_q;
  33. wire tx_full;
  34. wire tx_empty;
  35. wire rx_full;
  36. wire rx_empty;
  37. wire rx_break;
  38. reg tx_flush;
  39. reg rx_flush;
  40. wire [7:0] tty_rdata;
  41. wire [31:0] tty_divisor;
  42. serial #(
  43. .BAUDRATE_SETTABLE ( 1'b1 )
  44. )
  45. esptty (
  46. .rst_n ( rst_n ),
  47. .clk ( clk ),
  48. .tty_tx ( tty_tx ),
  49. .tty_rx ( tty_rx ),
  50. .tx_wstrb ( cpu_wstrb[0] & cpu_reg[0] ),
  51. .tx_data ( cpu_wdata[7:0] ),
  52. .tx_break ( tx_break_q ),
  53. .tx_full ( tx_full ),
  54. .tx_empty ( tx_empty ),
  55. .tx_flush ( tx_flush ),
  56. .rx_rstrb ( cpu_read & cpu_reg[0] ),
  57. .rx_data ( tty_rdata ),
  58. .rx_break ( rx_break ),
  59. .rx_full ( rx_full ),
  60. .rx_empty ( rx_empty ),
  61. .rx_flush ( rx_flush ),
  62. .divisor_wdata ( cpu_wdata ),
  63. .divisor_wstrb ( cpu_wstrb[0] & cpu_reg[1] ),
  64. .divisor ( tty_divisor )
  65. );
  66. // Control/clear status bits
  67. reg rx_break_q;
  68. reg esp_rst_q;
  69. reg esp_dl_q; // Force download boot
  70. reg esp_int_q;
  71. assign esp_en = esp_rst_q ? 1'b0 : 1'bz;
  72. assign esp_io0 = esp_dl_q ? 1'b0 : 1'bz;
  73. assign esp_int = esp_int_q ? 1'b0 : 1'bz;
  74. always @(negedge rst_n or posedge clk)
  75. if (~rst_n)
  76. begin
  77. rx_break_q <= 1'b0;
  78. tx_break_q <= 1'b0;
  79. esp_rst_q <= 1'b0;
  80. esp_dl_q <= 1'b0;
  81. esp_int_q <= 1'b0;
  82. end
  83. else
  84. begin
  85. rx_break_q <= rx_break | rx_break_q;
  86. if (cpu_wstrb[0] & cpu_reg[2])
  87. begin
  88. if (cpu_wdata[2])
  89. rx_break_q <= rx_break;
  90. tx_break_q <= cpu_wdata[6];
  91. end
  92. if (cpu_wstrb[1] & cpu_reg[2])
  93. esp_int_q <= cpu_wdata[8];
  94. if (cpu_wstrb[2] & cpu_reg[2])
  95. begin
  96. esp_rst_q <= cpu_wdata[16];
  97. esp_dl_q <= cpu_wdata[17];
  98. end
  99. end // else: !if(~rst_n)
  100. // Not used yet
  101. assign irq = 1'b0;
  102. // Output data MUX
  103. always @(*)
  104. case (cpu_addr)
  105. 5'd0: cpu_rdata = { 24'b0, tty_rdata };
  106. 5'd1: cpu_rdata = tty_divisor;
  107. 5'd2: cpu_rdata = {
  108. 8'b0,
  109. 6'b0, esp_dl_q, esp_rst_q,
  110. 7'b0, ~esp_int,
  111. 1'b0, tx_break_q, tx_full, ~tx_empty,
  112. 1'b0, rx_break_q, rx_full, ~rx_empty
  113. };
  114. default: cpu_rdata = 32'bx;
  115. endcase // case (cpu_addr)
  116. endmodule // esp