| 1 |
`default_nettype none |
| 2 |
// ---------------------------------------------------------- |
| 3 |
// -- PMOD-LEDx8 show the operation of the ADC on loc_left bus |
| 4 |
// -- (see UG299-1.0.5E_Arora Analog to Digital (ADC) Convertor) |
| 5 |
// -- Input are two diff inputs with dymanic selection. |
| 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 |
input adc_0_p, // first channel |
| 20 |
input adc_0_n, |
| 21 |
input adc_3_p, // second channel |
| 22 |
input adc_3_n |
| 23 |
); |
| 24 |
|
| 25 |
reg adcen; |
| 26 |
wire adcrdy; |
| 27 |
reg adcreqi; |
| 28 |
wire [13:0] adcvalue; |
| 29 |
reg [13:0] read_value; |
| 30 |
|
| 31 |
assign {mled, rled} = ~{state[2:1], read_value}; |
| 32 |
|
| 33 |
|
| 34 |
// ********************************************************** |
| 35 |
// ** RESET |
| 36 |
// ********************************************************** |
| 37 |
reg init_reset_cnt = 1; |
| 38 |
reg [7:0] reset_cnt = 0; |
| 39 |
wire reset_n = &reset_cnt; |
| 40 |
wire need_reset; |
| 41 |
assign need_reset = !(rst_i ^ `INV_BTN); |
| 42 |
|
| 43 |
always @(posedge clk) begin |
| 44 |
if (need_reset) begin |
| 45 |
reset_cnt <= 0; |
| 46 |
end else begin |
| 47 |
reset_cnt <= reset_cnt + !reset_n; |
| 48 |
end |
| 49 |
end |
| 50 |
|
| 51 |
// Since there is no explicit connection between the pins and the ADC, the |
| 52 |
// synthesis will happily discard them, so (* keep *) |
| 53 |
(* keep *) |
| 54 |
TLVDS_IBUF_ADC in0 ( |
| 55 |
.ADCEN(in0_en), |
| 56 |
.I(adc_0_p), |
| 57 |
.IB(adc_0_n) |
| 58 |
); |
| 59 |
|
| 60 |
(* keep *) |
| 61 |
TLVDS_IBUF_ADC in3 ( |
| 62 |
.ADCEN(in3_en), |
| 63 |
.I(adc_3_p), |
| 64 |
.IB(adc_3_n) |
| 65 |
); |
| 66 |
|
| 67 |
ADC adc( |
| 68 |
.ADCRDY(adcrdy), //output adcrdy |
| 69 |
.ADCVALUE(adcvalue), //output [13:0] adcvalue |
| 70 |
.MDRP_RDATA(), //output [7:0] mdrp_rdata |
| 71 |
.VSENCTL(3'b010), //input [2:0] vsenctl |
| 72 |
.ADCEN(adcen), //input adcen |
| 73 |
.CLK(1'b0), //input clk |
| 74 |
.DRSTN(reset_n), //input drstn |
| 75 |
.ADCREQI(adcreqi), //input adcreqi |
| 76 |
.ADCMODE(1'b1), //input adcmode |
| 77 |
.MDRP_CLK(1'b0), //input mdrp_clk |
| 78 |
.MDRP_WDATA(8'h0), //input [7:0] mdrp_wdata |
| 79 |
.MDRP_A_INC(1'b0), //input mdrp_a_inc |
| 80 |
.MDRP_OPCODE(2'b00) //input [1:0] mdrp_opcode |
| 81 |
); |
| 82 |
defparam adc.CLK_SEL = 1'b0; |
| 83 |
defparam adc.DIV_CTL = 2'd0; |
| 84 |
defparam adc.BUF_EN = 12'b000000000000; |
| 85 |
defparam adc.BUF_BK0_VREF_EN = 1'b0; |
| 86 |
defparam adc.BUF_BK1_VREF_EN = 1'b0; |
| 87 |
defparam adc.BUF_BK2_VREF_EN = 1'b0; |
| 88 |
defparam adc.BUF_BK3_VREF_EN = 1'b0; |
| 89 |
defparam adc.BUF_BK4_VREF_EN = 1'b0; |
| 90 |
defparam adc.BUF_BK5_VREF_EN = 1'b0; |
| 91 |
defparam adc.BUF_BK6_VREF_EN = 1'b0; |
| 92 |
defparam adc.BUF_BK7_VREF_EN = 1'b0; |
| 93 |
defparam adc.CSR_ADC_MODE = 1'b1; |
| 94 |
defparam adc.CSR_VSEN_CTRL = 3'b010; |
| 95 |
defparam adc.CSR_SAMPLE_CNT_SEL = 3'd4; |
| 96 |
defparam adc.CSR_RATE_CHANGE_CTRL = 3'd4; |
| 97 |
defparam adc.CSR_FSCAL = 10'd653; |
| 98 |
defparam adc.CSR_OFFSET = 12'd0; |
| 99 |
|
| 100 |
|
| 101 |
// pause between change channels |
| 102 |
localparam PAUSE = 50_000_000 * 2; |
| 103 |
localparam PAUSE_HIGH = $clog2(PAUSE) - 1; |
| 104 |
reg [PAUSE_HIGH:0] measure_pause; |
| 105 |
|
| 106 |
// request pusle 10 ADC cycles |
| 107 |
localparam REQ_PULSE = 10 * 50_000_000 / 2_500_000; |
| 108 |
localparam REQ_PULSE_HIGH = $clog2(REQ_PULSE) - 1; |
| 109 |
reg [REQ_PULSE_HIGH:0] req_pulse; |
| 110 |
|
| 111 |
// active channels |
| 112 |
reg in0_en = 1'b1; |
| 113 |
reg in3_en = 1'b0; |
| 114 |
|
| 115 |
// states |
| 116 |
reg [2:0] state; |
| 117 |
localparam s_idle = 0; |
| 118 |
localparam s_enable = 1; |
| 119 |
localparam s_enable_0 = 2; |
| 120 |
localparam s_req = 3; |
| 121 |
localparam s_read = 4; |
| 122 |
|
| 123 |
always @(posedge clk) begin |
| 124 |
if (!reset_n) begin |
| 125 |
adcen <= 1'b1; |
| 126 |
adcreqi <= 1'b0; |
| 127 |
|
| 128 |
// pause between measurements |
| 129 |
measure_pause <= PAUSE; |
| 130 |
state <= s_idle; |
| 131 |
end else begin |
| 132 |
case(state) |
| 133 |
// periodic measurement |
| 134 |
s_idle: begin |
| 135 |
if (measure_pause) begin |
| 136 |
measure_pause <= measure_pause - 1'b1; |
| 137 |
end else begin |
| 138 |
measure_pause <= PAUSE; |
| 139 |
// switch channels |
| 140 |
in0_en <= ~in0_en; |
| 141 |
in3_en <= in0_en; |
| 142 |
state <= s_enable; |
| 143 |
end |
| 144 |
end |
| 145 |
// pause before send request |
| 146 |
s_enable: begin |
| 147 |
state <= s_enable_0; |
| 148 |
end |
| 149 |
s_enable_0: begin |
| 150 |
adcreqi <= 1'b1; |
| 151 |
req_pulse <= REQ_PULSE; |
| 152 |
state <= s_req; |
| 153 |
end |
| 154 |
|
| 155 |
// pause after request |
| 156 |
s_req: begin |
| 157 |
if (req_pulse) begin |
| 158 |
req_pulse <= req_pulse - 1'b1; |
| 159 |
end else begin |
| 160 |
if (!adcrdy) begin |
| 161 |
adcreqi <= 1'b0; |
| 162 |
state <= s_read; |
| 163 |
end |
| 164 |
end |
| 165 |
end |
| 166 |
// read data |
| 167 |
s_read: begin |
| 168 |
if (adcrdy) begin |
| 169 |
read_value <= adcvalue; |
| 170 |
state <= s_idle; |
| 171 |
end |
| 172 |
end |
| 173 |
default: |
| 174 |
state <= s_idle; |
| 175 |
endcase |
| 176 |
end |
| 177 |
end |
| 178 |
|
| 179 |
endmodule |
| 180 |
// vim: set et sw=4 ts=4: |