dfu_helper.v 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * dfu_helper.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 dfu_helper #(
  35. parameter integer TIMER_WIDTH = 24,
  36. parameter integer BTN_MODE = 3, // [1] Include IO buffer, [0] Invert
  37. parameter integer DFU_MODE = 0 // 0 = For user app, 1 = For bootloader
  38. )(
  39. // External control
  40. input wire [1:0] boot_sel,
  41. input wire boot_now,
  42. // Button
  43. input wire btn_pad,
  44. // Outputs
  45. output wire btn_val,
  46. output reg rst_req,
  47. // Clock
  48. input wire clk,
  49. input wire rst
  50. );
  51. // Signals
  52. // -------
  53. // Button
  54. wire btn_iob;
  55. wire btn_v;
  56. wire btn_r;
  57. wire btn_f;
  58. // Timer and arming logic
  59. reg armed;
  60. reg [TIMER_WIDTH-1:0] timer;
  61. (* keep="true" *) wire timer_act;
  62. // Boot logic
  63. reg [1:0] wb_sel;
  64. reg wb_req;
  65. reg wb_now;
  66. // Button logic
  67. // ------------
  68. generate
  69. if (BTN_MODE[1])
  70. SB_IO #(
  71. .PIN_TYPE(6'b000000),
  72. .PULLUP(1'b1),
  73. .IO_STANDARD("SB_LVCMOS")
  74. ) btn_iob_I (
  75. .PACKAGE_PIN(btn_pad),
  76. .INPUT_CLK(clk),
  77. .D_IN_0(btn_iob)
  78. );
  79. else
  80. assign btn_iob = btn_pad;
  81. endgenerate
  82. glitch_filter #(
  83. .L(4)
  84. ) btn_flt_I (
  85. .pin_iob_reg(btn_iob ^ BTN_MODE[0]),
  86. .cond(1'b1),
  87. .val(btn_v),
  88. .rise(btn_r),
  89. .fall(btn_f),
  90. .clk(clk),
  91. `ifdef SIM
  92. .rst(rst)
  93. `else
  94. .rst(1'b0) // Ensure the glitch filter has settled
  95. // before logic here engages
  96. `endif
  97. );
  98. assign btn_val = btn_v;
  99. // Arming & Timer
  100. // --------------
  101. assign timer_act = btn_v ^ armed;
  102. always @(posedge clk or posedge rst)
  103. if (rst)
  104. armed <= 1'b0;
  105. else
  106. armed <= armed | timer[TIMER_WIDTH-2];
  107. always @(posedge clk or posedge rst)
  108. if (rst)
  109. timer <= 0;
  110. else
  111. timer <= timer_act ? { TIMER_WIDTH{1'b0} } : (timer + { { (TIMER_WIDTH-1){1'b0} }, ~timer[TIMER_WIDTH-1] });
  112. // Boot Logic
  113. // ----------
  114. // Decision
  115. always @(posedge clk or posedge rst)
  116. if (rst) begin
  117. wb_sel <= 2'b00;
  118. wb_req <= 1'b0;
  119. rst_req <= 1'b0;
  120. end else if (~wb_req) begin
  121. if (boot_now) begin
  122. // External boot request
  123. wb_sel <= boot_sel;
  124. wb_req <= 1'b1;
  125. rst_req <= 1'b0;
  126. end else begin
  127. if (DFU_MODE == 1) begin
  128. // We're in a DFU bootloader, any button press results in
  129. // boot to application
  130. wb_sel <= 2'b10;
  131. wb_req <= wb_now | (armed & btn_f);
  132. rst_req <= 1'b0;
  133. end else begin
  134. // We're in user application, short press resets the
  135. // logic, long press triggers DFU reboot
  136. wb_sel <= 2'b01;
  137. wb_req <= wb_now | (armed & btn_f & timer[TIMER_WIDTH-1]);
  138. rst_req <= rst_req | (armed & btn_f & ~timer[TIMER_WIDTH-1]);
  139. end
  140. end
  141. end
  142. // Ensure select bits are set before the boot pulse
  143. always @(posedge clk or posedge rst)
  144. if (rst)
  145. wb_now <= 1'b0;
  146. else
  147. wb_now <= wb_req;
  148. // IP core
  149. SB_WARMBOOT warmboot (
  150. .BOOT(wb_now),
  151. .S0(wb_sel[0]),
  152. .S1(wb_sel[1])
  153. );
  154. endmodule // dfu_helper