Debian Codegraph @

examples/gw5a/adc-glo-right.v

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