nano_dsi_data.v 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * nano_dsi_data.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2019 Sylvain Munaut <tnt@246tNt.com>
  7. * All rights reserved.
  8. *
  9. * BSD 3-clause, see LICENSE.bsd
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * * Neither the name of the <organization> nor the
  19. * names of its contributors may be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  26. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. `default_nettype none
  34. module nano_dsi_data (
  35. // nano-PMOD - DATA lane
  36. output wire data_lp,
  37. output wire data_hs_p,
  38. output wire data_hs_n,
  39. // Control/Packet interface
  40. input wire hs_start,
  41. input wire [7:0] hs_data,
  42. input wire hs_last,
  43. output wire hs_ack,
  44. output wire hs_rdy,
  45. // Clock/Data sync
  46. input wire clk_sync,
  47. // Config
  48. input wire [7:0] cfg_hs_prep,
  49. input wire [7:0] cfg_hs_zero,
  50. input wire [7:0] cfg_hs_trail,
  51. // Clock / Reset
  52. input wire clk,
  53. input wire rst
  54. );
  55. // Signals
  56. // -------
  57. // IO control
  58. reg io_lp;
  59. reg io_hs_active;
  60. reg io_hs_bit;
  61. // FSM
  62. localparam
  63. ST_LP11 = 0,
  64. ST_LP00 = 1,
  65. ST_HS_ZERO = 2,
  66. ST_HS_SYNC = 3,
  67. ST_HS_DATA = 4,
  68. ST_HS_TRAIL = 5;
  69. reg [2:0] fsm_state;
  70. reg [2:0] fsm_state_next;
  71. // Timer
  72. reg [7:0] timer_val;
  73. wire timer_trig;
  74. // Shift register
  75. reg [7:0] shift_reg;
  76. reg [3:0] shift_cnt;
  77. reg shift_last;
  78. reg hs_bit_final;
  79. // IOBs
  80. // ----
  81. // LP drivers
  82. SB_IO #(
  83. .PIN_TYPE(6'b100100),
  84. .PULLUP(1'b0),
  85. .NEG_TRIGGER(1'b0),
  86. .IO_STANDARD("SB_LVCMOS")
  87. ) iob_data_lp_I (
  88. .PACKAGE_PIN(data_lp),
  89. .CLOCK_ENABLE(1'b1),
  90. // .INPUT_CLK(1'b0),
  91. .OUTPUT_CLK(clk),
  92. .OUTPUT_ENABLE(1'b1),
  93. .D_OUT_0(io_lp),
  94. .D_OUT_1(1'b0),
  95. .D_IN_0(),
  96. .D_IN_1()
  97. );
  98. // HS drivers
  99. SB_IO #(
  100. .PIN_TYPE(6'b100100),
  101. .PULLUP(1'b0),
  102. .NEG_TRIGGER(1'b0),
  103. .IO_STANDARD("SB_LVCMOS")
  104. ) iob_data_hs_p_I (
  105. .PACKAGE_PIN(data_hs_p),
  106. .CLOCK_ENABLE(1'b1),
  107. // .INPUT_CLK(1'b0),
  108. .OUTPUT_CLK(clk),
  109. .OUTPUT_ENABLE(io_hs_active),
  110. .D_OUT_0(io_hs_bit),
  111. .D_OUT_1(1'b0),
  112. .D_IN_0(),
  113. .D_IN_1()
  114. );
  115. SB_IO #(
  116. .PIN_TYPE(6'b100100),
  117. .PULLUP(1'b0),
  118. .NEG_TRIGGER(1'b0),
  119. .IO_STANDARD("SB_LVCMOS")
  120. ) iob_data_hs_n_I (
  121. .PACKAGE_PIN(data_hs_n),
  122. .CLOCK_ENABLE(1'b1),
  123. // .INPUT_CLK(1'b0),
  124. .OUTPUT_CLK(clk),
  125. .OUTPUT_ENABLE(io_hs_active),
  126. .D_OUT_0(~io_hs_bit),
  127. .D_OUT_1(1'b0),
  128. .D_IN_0(),
  129. .D_IN_1()
  130. );
  131. // FSM
  132. // ---
  133. // State register
  134. always @(posedge clk or posedge rst)
  135. if (rst)
  136. fsm_state <= ST_LP11;
  137. else
  138. fsm_state <= fsm_state_next;
  139. // Next State logic
  140. always @(*)
  141. begin
  142. // Default is to not move
  143. fsm_state_next = fsm_state;
  144. // Transitions ?
  145. case (fsm_state)
  146. ST_LP11:
  147. if (hs_start)
  148. fsm_state_next = ST_LP00;
  149. ST_LP00:
  150. if (timer_trig)
  151. fsm_state_next = ST_HS_ZERO;
  152. ST_HS_ZERO:
  153. if (timer_trig)
  154. fsm_state_next = ST_HS_SYNC;
  155. ST_HS_SYNC:
  156. if (clk_sync)
  157. fsm_state_next = ST_HS_DATA;
  158. ST_HS_DATA:
  159. if (shift_cnt[3] && shift_last)
  160. fsm_state_next = ST_HS_TRAIL;
  161. ST_HS_TRAIL:
  162. if (timer_trig)
  163. fsm_state_next = ST_LP11;
  164. endcase
  165. end
  166. // Timer
  167. // -----
  168. always @(posedge clk)
  169. begin
  170. if (fsm_state != fsm_state_next) begin
  171. // Default is to trigger all the time
  172. timer_val <= 8'h80;
  173. // Preload for next state
  174. case (fsm_state_next)
  175. ST_LP00: timer_val <= cfg_hs_prep;
  176. ST_HS_ZERO: timer_val <= cfg_hs_zero;
  177. ST_HS_TRAIL: timer_val <= cfg_hs_trail;
  178. endcase
  179. end else begin
  180. timer_val <= timer_val - 1;
  181. end
  182. end
  183. assign timer_trig = timer_val[7];
  184. // Shift register
  185. // --------------
  186. always @(posedge clk)
  187. if (fsm_state == ST_HS_SYNC) begin
  188. shift_reg <= 8'hB8; // SoT
  189. shift_last <= 1'b0;
  190. end else if (fsm_state == ST_HS_DATA) begin
  191. if (shift_cnt[3]) begin
  192. shift_reg <= hs_data; // Load
  193. shift_last <= hs_last;
  194. end else begin
  195. shift_reg <= { 1'b0, shift_reg[7:1] }; // Shift LSB out
  196. shift_last <= shift_last;
  197. end
  198. end
  199. always @(posedge clk)
  200. if ((fsm_state != ST_HS_DATA) || shift_cnt[3])
  201. shift_cnt <= 4'h1;
  202. else
  203. shift_cnt <= shift_cnt + 1;
  204. assign hs_ack = shift_cnt[3];
  205. assign hs_rdy = (fsm_state == ST_LP11);
  206. always @(posedge clk)
  207. if (shift_cnt[3] & shift_last)
  208. hs_bit_final <= ~shift_reg[0];
  209. // IO control
  210. // ----------
  211. always @(posedge clk)
  212. begin
  213. io_lp <= (fsm_state == ST_LP11);
  214. io_hs_active <=
  215. (fsm_state == ST_HS_ZERO) ||
  216. (fsm_state == ST_HS_SYNC) ||
  217. (fsm_state == ST_HS_DATA) ||
  218. (fsm_state == ST_HS_TRAIL);
  219. if (fsm_state == ST_HS_DATA)
  220. io_hs_bit <= shift_reg[0];
  221. else if (fsm_state == ST_HS_TRAIL)
  222. io_hs_bit <= hs_bit_final;
  223. else
  224. io_hs_bit <= 1'b0;
  225. end
  226. endmodule // nano_dsi_data