main.v 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // vim: ts=4 sw=4 noexpandtab
  2. /*
  3. * pyprofibus FPGA PHY
  4. *
  5. * Copyright (c) 2019 Michael Buesch <m@bues.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. `include "profibus_phy_mod.v"
  22. `include "led_blink_mod.v"
  23. `ifdef DEBUG
  24. `define DEBUGOUT output
  25. `else
  26. `define DEBUGOUT input
  27. `endif
  28. module common_main_module #(
  29. parameter CLK_HZ = 0,
  30. ) (
  31. input clk,
  32. input n_reset,
  33. /* SPI bus */
  34. input spi_mosi,
  35. inout spi_miso,
  36. input spi_sck,
  37. input spi_ss,
  38. /* Profibus and status */
  39. input pb_rx,
  40. output pb_rx_error,
  41. output pb_rx_irq_edge,
  42. output pb_rx_irq_level,
  43. output pb_tx,
  44. output pb_tx_active,
  45. output pb_tx_error,
  46. /* Status and debugging */
  47. output led,
  48. `ifdef DEBUG
  49. output debug,
  50. `endif
  51. );
  52. wire miso;
  53. wire sck;
  54. wire ss;
  55. wire rx_error;
  56. wire rx_irq_edge;
  57. wire rx_irq_level;
  58. wire tx;
  59. wire tx_error;
  60. wire rx_active;
  61. wire tx_active;
  62. `ifdef DEBUG
  63. wire debug_w;
  64. `endif
  65. profibus_phy pb(
  66. .clk(clk),
  67. .n_reset(n_reset),
  68. .rx_irq_edge(rx_irq_edge),
  69. .rx_irq_level(rx_irq_level),
  70. .mosi(spi_mosi),
  71. .miso(miso),
  72. .sck(spi_sck),
  73. .ss(spi_ss),
  74. .rx(pb_rx),
  75. .rx_active(rx_active),
  76. .rx_error(rx_error),
  77. .tx(tx),
  78. .tx_active(tx_active),
  79. .tx_error(tx_error),
  80. `ifdef DEBUG
  81. .debug(debug_w),
  82. `endif
  83. );
  84. bufif0(spi_miso, miso, spi_ss);
  85. bufif0(pb_rx_error, rx_error, 0);
  86. bufif0(pb_rx_irq_edge, rx_irq_edge, 0);
  87. bufif0(pb_rx_irq_level, rx_irq_level, 0);
  88. bufif0(pb_tx, tx, 0);
  89. bufif0(pb_tx_active, tx_active, 0);
  90. bufif0(pb_tx_error, tx_error, 0);
  91. `ifdef DEBUG
  92. bufif0(debug, debug_w, 0);
  93. `endif
  94. wire led_w;
  95. wire led_enable;
  96. assign led_enable = tx_active | rx_active;
  97. led_blink #(
  98. .BLINK_ON_CLKS(CLK_HZ / 10),
  99. .BLINK_OFF_CLKS(CLK_HZ / 35),
  100. ) led_blink (
  101. .clk(clk),
  102. .n_reset(n_reset),
  103. .enable(led_enable),
  104. .led(led_w),
  105. );
  106. bufif0(led, led_w, 0);
  107. endmodule
  108. `ifdef TARGET_TINYFPGA_BX
  109. `include "pll_mod.v"
  110. /* TinyFPGA BX:
  111. * +---------------+
  112. * |P|GND Vin|P|
  113. * not reset |O|1 GND|P|
  114. * debug |D|2 3.3V|P|
  115. * |N|3 T 24|N|
  116. * |N|4 i 23|N|
  117. * |N|5 n 22|N|
  118. * |N|6 y 21|N|
  119. * |N|7 F 20|O| PB RX IRQ level
  120. * |N|8 P 19|O| PB RX IRQ edge
  121. * |N|9 G 18|O| PB TX error
  122. * SPI MISO |O|10 A 17|O| PB RX error
  123. * SPI MOSI |I|11 16|O| PB TX active
  124. * SPI SCK |I|12 B 15|O| PB UART TX
  125. * SPI SS |I|13 X 14|I| PB UART RX
  126. * +---------------+
  127. * P = power
  128. * I = input
  129. * O = output
  130. * D = debug output. Only if DEBUG is enabled. Otherwise N.
  131. * N = not connected
  132. */
  133. module top_module(
  134. input CLK,
  135. input SPI_SS,
  136. input SPI_SCK,
  137. input SPI_IO0,
  138. input SPI_IO1,
  139. input SPI_IO2,
  140. input SPI_IO3,
  141. input USBP,
  142. input USBN,
  143. output USBPU,
  144. output LED,
  145. input PIN_1,
  146. `DEBUGOUT PIN_2,
  147. input PIN_3,
  148. input PIN_4,
  149. input PIN_5,
  150. input PIN_6,
  151. input PIN_7,
  152. input PIN_8,
  153. input PIN_9,
  154. inout PIN_10,
  155. input PIN_11,
  156. input PIN_12,
  157. input PIN_13,
  158. input PIN_14,
  159. output PIN_15,
  160. output PIN_16,
  161. output PIN_17,
  162. output PIN_18,
  163. output PIN_19,
  164. output PIN_20,
  165. input PIN_21,
  166. input PIN_22,
  167. input PIN_23,
  168. input PIN_24,
  169. input PIN_25,
  170. input PIN_26,
  171. input PIN_27,
  172. input PIN_28,
  173. input PIN_29,
  174. input PIN_30,
  175. input PIN_31,
  176. );
  177. wire pll_clk_out;
  178. wire pll_locked;
  179. pll_module pll(
  180. .clock_in(CLK),
  181. .clock_out(pll_clk_out),
  182. .locked(pll_locked),
  183. );
  184. wire n_reset;
  185. assign n_reset = PIN_1 & pll_locked;
  186. common_main_module #(
  187. .CLK_HZ(`PLL_HZ),
  188. ) common (
  189. .clk(pll_clk_out),
  190. .n_reset(n_reset),
  191. .spi_mosi(PIN_11),
  192. .spi_miso(PIN_10),
  193. .spi_sck(PIN_12),
  194. .spi_ss(PIN_13),
  195. .pb_rx(PIN_14),
  196. .pb_rx_error(PIN_17),
  197. .pb_rx_irq_edge(PIN_19),
  198. .pb_rx_irq_level(PIN_20),
  199. .pb_tx(PIN_15),
  200. .pb_tx_active(PIN_16),
  201. .pb_tx_error(PIN_18),
  202. .led(LED),
  203. `ifdef DEBUG
  204. .debug(PIN_2),
  205. `endif
  206. );
  207. assign USBPU = 0; /* Disable USB */
  208. endmodule
  209. `else /* TARGET */
  210. `ERROR____TARGET_is_not_known
  211. `endif /* TARGET */