-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcapsense_sys.v
105 lines (84 loc) · 2.76 KB
/
capsense_sys.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/***********************************************************************
Capsense system controller
This file is part FPGA Libre project http://fpgalibre.sf.net/
Description:
Core used to periodically sample capsense buttons.
This version includes the frequency dividers and the toggle logic.
To Do:
-
Author:
- Salvador E. Tropea, salvador en inti.gob.ar
----------------------------------------------------------------------
Copyright (c) 2016-2017 Salvador E. Tropea <salvador en inti.gob.ar>
Copyright (c) 2016-2017 Instituto Nacional de Tecnología Industrial
This file can be distributed under the terms of the GPL 2.0 license
or newer.
----------------------------------------------------------------------
Design unit: CapSense_Sys
File name: capsense_sys.v
Note: None
Limitations: None known
Errors: None known
Library: None
Dependencies: None
Target FPGA: iCE40HX4K-TQ144
Language: Verilog
Wishbone: None
Synthesis tools: iCEcube2 2016.02
Simulation tools: GHDL [Sokcho edition] (0.2x)
Text editor: SETEdit 0.5.x
************************************************************************/
module CapSense_Sys #(
parameter DIRECT=1, // Direct status, else: toggle
parameter FREQUENCY=24, // Clock in MHz
parameter N=4 // How many buttons
) (
input clk_i, // System clock
input rst_i, // System reset
input [N-1:0] capsense_i, // Buttons inputs
output capsense_o, // Buttons OE
output [N-1:0] buttons_o, // Last sample result
output [N-1:0] debug_o // Used to measure the button timing
);
localparam integer MOD_SAMP=FREQUENCY/1.5;
localparam integer MOD_BITS=$clog2(MOD_SAMP);
// CapSense sampling rate
wire clkSamp;
reg [MOD_BITS-1:0] cntSamp=0;
// CapSese polling rate
wire clkPoll;
reg [16:0] cntPoll=0;
// Buttons
wire [N-1:0] cur_btns;
reg [N-1:0] prev_btn_r=0;
reg [N-1:0] cur_btn_r=0;
// 1.5 MHz capacitors sample
always @(posedge clk_i)
if (cntSamp==MOD_SAMP-1)
cntSamp=0;
else
cntSamp=cntSamp+1;
assign clkSamp=!cntSamp ? 1 : 0;
// aprox. 87 ms
always @(posedge clk_i)
if (clkSamp)
cntPoll=cntPoll+1;
assign clkPoll=!cntPoll ? 1 : 0;
CapSense #(.N(N)) TheCS
(.clk_i(clk_i), .rst_i(rst_i),
.ena_i(clkSamp),
.start_i(clkPoll),
.buttons_i(capsense_i),
.but_oe_o(capsense_o),
.sampled_o(cur_btns),
.debug_o(debug_o));
integer i;
always @(posedge clk_i)
begin
for (i=0; i<4; i=i+1)
if (!prev_btn_r[i] && cur_btns[i]) // pressed?
cur_btn_r[i]=~cur_btn_r[i]; // toggle
prev_btn_r=cur_btns;
end
assign buttons_o=DIRECT ? cur_btns : cur_btn_r;
endmodule