Hi! I'm experiencing some strange behavior with my Tang Nano 9K
Required: press the button to make the LED light up
Fact: the LED lights up only after the
second press
After a long search and debugging with a logic analyzer, I got it working. It turned out that to fix it you just need to add a line "assign debugPin1 = s1Out;"
Why?module main (
input bit clk,
input bit btn1,
output bit led1,
output bit debugPin1
);
bit s1Out;// = 1'b0;
debounce key1Debounce(
.clk,
.in(btn1),
.out(s1Out)
);
initial begin
led1 = 1'b1;
end
//assign debugPin1 = s1Out; // <-------- Fix
always_ff @(posedge s1Out) begin
led1 <= 1'b0;
end
endmodule
debounce:
module debounce #(
parameter CLK_F = 27000000)
(
input bit clk,
input bit in,
output bit out
);
localparam deadPeriodTicks = int'(CLK_F/100) - 1;
bit debouncedIn, debouncedInShifted;
bit [$clog2((deadPeriodTicks)):0] cnt = 1'b0;
assign out = debouncedIn & ~debouncedInShifted;
always_ff @(posedge clk)
begin
if (~in && ~&cnt)
cnt <= cnt + 1'b1;
else
if (in && |cnt)
cnt <= cnt - 1'b1;
if (~|cnt)
debouncedIn <= 1'b0;
else
if (&cnt)
debouncedIn <= 1'b1;
debouncedInShifted <= debouncedIn;
end
endmodule
cst-file
IO_LOC "debugPin1" 70;
IO_PORT "debugPin1" IO_TYPE=LVCMOS33 PULL_MODE=NONE DRIVE=8 BANK_VCCIO=3.3;
IO_LOC "led1" 10;
IO_PORT "led1" PULL_MODE=UP DRIVE=8 BANK_VCCIO=1.8;
IO_LOC "btn1" 4;
IO_PORT "btn1" PULL_MODE=UP HYSTERESIS=NONE BANK_VCCIO=1.8;
IO_LOC "clk" 52;
IO_PORT "clk" IO_TYPE=LVCMOS33 PULL_MODE=UP BANK_VCCIO=3.3;