dfu_helper.v 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. .in(btn_iob ^ BTN_MODE[0]),
  86. .val(btn_v),
  87. .rise(btn_r),
  88. .fall(btn_f),
  89. .clk(clk),
  90. `ifdef SIM
  91. .rst(rst)
  92. `else
  93. .rst(1'b0) // Ensure the glitch filter has settled
  94. // before logic here engages
  95. `endif
  96. );
  97. assign btn_val = btn_v;
  98. // Arming & Timer
  99. // --------------
  100. assign timer_act = btn_v ^ armed;
  101. always @(posedge clk or posedge rst)
  102. if (rst)
  103. armed <= 1'b0;
  104. else
  105. armed <= armed | timer[TIMER_WIDTH-2];
  106. always @(posedge clk or posedge rst)
  107. if (rst)
  108. timer <= 0;
  109. else
  110. timer <= timer_act ? { TIMER_WIDTH{1'b0} } : (timer + { { (TIMER_WIDTH-1){1'b0} }, ~timer[TIMER_WIDTH-1] });
  111. // Boot Logic
  112. // ----------
  113. // Decision
  114. always @(posedge clk or posedge rst)
  115. if (rst) begin
  116. wb_sel <= 2'b00;
  117. wb_req <= 1'b0;
  118. rst_req <= 1'b0;
  119. end else if (~wb_req) begin
  120. if (boot_now) begin
  121. // External boot request
  122. wb_sel <= boot_sel;
  123. wb_req <= 1'b1;
  124. rst_req <= 1'b0;
  125. end else begin
  126. if (DFU_MODE == 1) begin
  127. // We're in a DFU bootloader, any button press results in
  128. // boot to application
  129. wb_sel <= 2'b10;
  130. wb_req <= wb_now | (armed & btn_f);
  131. rst_req <= 1'b0;
  132. end else begin
  133. // We're in user application, short press resets the
  134. // logic, long press triggers DFU reboot
  135. wb_sel <= 2'b01;
  136. wb_req <= wb_now | (armed & btn_f & timer[TIMER_WIDTH-1]);
  137. rst_req <= rst_req | (armed & btn_f & ~timer[TIMER_WIDTH-1]);
  138. end
  139. end
  140. end
  141. // Ensure select bits are set before the boot pulse
  142. always @(posedge clk or posedge rst)
  143. if (rst)
  144. wb_now <= 1'b0;
  145. else
  146. wb_now <= wb_req;
  147. // IP core
  148. SB_WARMBOOT warmboot (
  149. .BOOT(wb_now),
  150. .S0(wb_sel[0]),
  151. .S1(wb_sel[1])
  152. );
  153. endmodule // dfu_helper