Debian Codegraph @

examples/gw5a/adc-temp.v

raw
1 `default_nettype none
2 // ----------------------------------------------------------
3 // -- We use two PMOD-LEDx8 to show the operation of the ADC with an internal
4 // -- thermometer as the signal source. The result is displayed on the LEDs.
5 // -- According to the documentation, the number should be divided by 4.
6 // -- We do not do this automatically because the chip temperature changes
7 // -- slowly and without fluctuations in the lower bits, it is not interesting to
8 // -- observe the operation of the ADC.
9 // -- The two most significant bits are reserved for displaying the FSM state.
10 // ----------------------------------------------------------
11
12 module top(
13 input clk,
14 input rst_i,
15 output [7:0] rled, // right LED PMOD
16 output [7:0] mled // middle LED PMOD
17 );
18
19 reg adcen;
20 wire adcrdy;
21 reg adcreqi;
22 wire [13:0] adcvalue;
23 reg [13:0] read_value;
24
25 assign {mled, rled} = ~{state[2:1], read_value};
26
27
28 // **********************************************************
29 // ** RESET
30 // **********************************************************
31 reg init_reset_cnt = 1;
32 reg [7:0] reset_cnt = 0;
33 wire reset_n = &reset_cnt;
34 wire need_reset;
35 assign need_reset = !(rst_i ^ `INV_BTN);
36
37 always @(posedge clk) begin
38 if (need_reset) begin
39 reset_cnt <= 0;
40 end else begin
41 reset_cnt <= reset_cnt + !reset_n;
42 end
43 end
44
45 ADC adc(
46 .ADCRDY(adcrdy), //output adcrdy
47 .ADCVALUE(adcvalue), //output [13:0] adcvalue
48 .MDRP_RDATA(), //output [7:0] mdrp_rdata
49 .VSENCTL(3'b000), //input [2:0] vsenctl
50 .ADCEN(adcen), //input adcen
51 .CLK(1'b0), //input clk
52 .DRSTN(reset_n), //input drstn
53 .ADCREQI(adcreqi), //input adcreqi
54 .ADCMODE(1'b0), //input adcmode
55 .MDRP_CLK(1'b0), //input mdrp_clk
56 .MDRP_WDATA(8'h0), //input [7:0] mdrp_wdata
57 .MDRP_A_INC(1'b0), //input mdrp_a_inc
58 .MDRP_OPCODE(2'b00) //input [1:0] mdrp_opcode
59 );
60 defparam adc.CLK_SEL = 1'b0;
61 defparam adc.DIV_CTL = 2'd0;
62 defparam adc.BUF_EN = 12'b000000000000;
63 defparam adc.BUF_BK0_VREF_EN = 1'b0;
64 defparam adc.BUF_BK1_VREF_EN = 1'b0;
65 defparam adc.BUF_BK2_VREF_EN = 1'b0;
66 defparam adc.BUF_BK3_VREF_EN = 1'b0;
67 defparam adc.BUF_BK4_VREF_EN = 1'b0;
68 defparam adc.BUF_BK5_VREF_EN = 1'b0;
69 defparam adc.BUF_BK6_VREF_EN = 1'b0;
70 defparam adc.BUF_BK7_VREF_EN = 1'b0;
71 defparam adc.CSR_ADC_MODE = 1'b0;
72 defparam adc.CSR_VSEN_CTRL = 3'd0;
73 defparam adc.CSR_SAMPLE_CNT_SEL = 3'd4;
74 defparam adc.CSR_RATE_CHANGE_CTRL = 3'd4;
75 defparam adc.CSR_FSCAL = 10'd730;
76 defparam adc.CSR_OFFSET = -12'd1180;
77
78
79 // pause between measurements
80 localparam PAUSE = 50_000_000 / 2;
81 localparam PAUSE_HIGH = $clog2(PAUSE) - 1;
82 reg [PAUSE_HIGH:0] measure_pause;
83
84 // request pusle 10 ADC cycles
85 localparam REQ_PULSE = 10 * 50_000_000 / 2_500_000;
86 localparam REQ_PULSE_HIGH = $clog2(REQ_PULSE) - 1;
87 reg [REQ_PULSE_HIGH:0] req_pulse;
88
89 // states
90 reg [2:0] state;
91 localparam s_idle = 0;
92 localparam s_enable = 1;
93 localparam s_enable_0 = 2;
94 localparam s_req = 3;
95 localparam s_read = 4;
96
97 always @(posedge clk) begin
98 if (!reset_n) begin
99 adcen <= 1'b1;
100 adcreqi <= 1'b0;
101
102 // pause between measurements
103 measure_pause <= PAUSE;
104 state <= s_idle;
105 end else begin
106 case(state)
107 // periodic measurement
108 s_idle: begin
109 if (measure_pause) begin
110 measure_pause <= measure_pause - 1'b1;
111 end else begin
112 measure_pause <= PAUSE;
113 state <= s_enable;
114 end
115 end
116 // pause before send request
117 s_enable: begin
118 state <= s_enable_0;
119 end
120 s_enable_0: begin
121 adcreqi <= 1'b1;
122 req_pulse <= REQ_PULSE;
123 state <= s_req;
124 end
125
126 // pause after request
127 s_req: begin
128 if (req_pulse) begin
129 req_pulse <= req_pulse - 1'b1;
130 end else begin
131 if (!adcrdy) begin
132 adcreqi <= 1'b0;
133 state <= s_read;
134 end
135 end
136 end
137 // read data
138 s_read: begin
139 if (adcrdy) begin
140 read_value <= adcvalue;
141 state <= s_idle;
142 end
143 end
144 default:
145 state <= s_idle;
146 endcase
147 end
148 end
149
150 endmodule
151 // vim: set et sw=4 ts=4: