1 |
80 |
olivier.gi |
//----------------------------------------------------------------------------
|
2 |
|
|
// Copyright (C) 2001 Authors
|
3 |
|
|
//
|
4 |
|
|
// Redistribution and use in source and binary forms, with or without
|
5 |
|
|
// modification, are permitted provided that the following conditions
|
6 |
|
|
// are met:
|
7 |
|
|
// * Redistributions of source code must retain the above copyright
|
8 |
|
|
// notice, this list of conditions and the following disclaimer.
|
9 |
|
|
// * Redistributions in binary form must reproduce the above copyright
|
10 |
|
|
// notice, this list of conditions and the following disclaimer in the
|
11 |
|
|
// documentation and/or other materials provided with the distribution.
|
12 |
|
|
// * Neither the name of the authors nor the names of its contributors
|
13 |
|
|
// may be used to endorse or promote products derived from this software
|
14 |
|
|
// without specific prior written permission.
|
15 |
|
|
//
|
16 |
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
17 |
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
18 |
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
19 |
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
20 |
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
21 |
|
|
// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
22 |
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
23 |
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
24 |
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
25 |
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
26 |
|
|
// THE POSSIBILITY OF SUCH DAMAGE
|
27 |
|
|
//
|
28 |
|
|
//----------------------------------------------------------------------------
|
29 |
|
|
//
|
30 |
|
|
// *File Name: dac_spi_if.v
|
31 |
|
|
//
|
32 |
|
|
// *Module Description:
|
33 |
|
|
// SPI interface for National's DAC121S101 12 bit DAC
|
34 |
|
|
//
|
35 |
|
|
// *Author(s):
|
36 |
|
|
// - Olivier Girard, olgirard@gmail.com
|
37 |
|
|
//
|
38 |
|
|
//----------------------------------------------------------------------------
|
39 |
|
|
// $Rev: 66 $
|
40 |
|
|
// $LastChangedBy: olivier.girard $
|
41 |
|
|
// $LastChangedDate: 2010-03-07 09:09:38 +0100 (Sun, 07 Mar 2010) $
|
42 |
|
|
//----------------------------------------------------------------------------
|
43 |
|
|
|
44 |
|
|
module dac_spi_if (
|
45 |
|
|
|
46 |
|
|
// OUTPUTs
|
47 |
|
|
cntrl1, // Control value 1
|
48 |
|
|
cntrl2, // Control value 2
|
49 |
|
|
din, // SPI Serial Data
|
50 |
|
|
per_dout, // Peripheral data output
|
51 |
|
|
sclk, // SPI Serial Clock
|
52 |
|
|
sync_n, // SPI Frame synchronization signal (low active)
|
53 |
|
|
|
54 |
|
|
// INPUTs
|
55 |
|
|
mclk, // Main system clock
|
56 |
|
|
per_addr, // Peripheral address
|
57 |
|
|
per_din, // Peripheral data input
|
58 |
|
|
per_en, // Peripheral enable (high active)
|
59 |
107 |
olivier.gi |
per_we, // Peripheral write enable (high active)
|
60 |
111 |
olivier.gi |
puc_rst // Main system reset
|
61 |
80 |
olivier.gi |
);
|
62 |
|
|
|
63 |
|
|
// PARAMETERs
|
64 |
|
|
//============
|
65 |
|
|
parameter SCLK_DIV = 0; // Serial clock divider (Tsclk=Tmclk*(SCLK_DIV+1)*2)
|
66 |
|
|
parameter BASE_ADDR = 9'h190; // Registers base address
|
67 |
|
|
|
68 |
|
|
// OUTPUTs
|
69 |
|
|
//=========
|
70 |
|
|
output [3:0] cntrl1; // Control value 1
|
71 |
|
|
output [3:0] cntrl2; // Control value 2
|
72 |
|
|
output din; // SPI Serial Data
|
73 |
|
|
output [15:0] per_dout; // Peripheral data output
|
74 |
|
|
output sclk; // SPI Serial Clock
|
75 |
|
|
output sync_n; // SPI Frame synchronization signal (low active)
|
76 |
|
|
|
77 |
|
|
// INPUTs
|
78 |
|
|
//=========
|
79 |
|
|
input mclk; // Main system clock
|
80 |
111 |
olivier.gi |
input [13:0] per_addr; // Peripheral address
|
81 |
80 |
olivier.gi |
input [15:0] per_din; // Peripheral data input
|
82 |
|
|
input per_en; // Peripheral enable (high active)
|
83 |
107 |
olivier.gi |
input [1:0] per_we; // Peripheral write enable (high active)
|
84 |
111 |
olivier.gi |
input puc_rst; // Main system reset
|
85 |
80 |
olivier.gi |
|
86 |
|
|
|
87 |
|
|
//=============================================================================
|
88 |
|
|
// 1) PARAMETER DECLARATION
|
89 |
|
|
//=============================================================================
|
90 |
|
|
|
91 |
111 |
olivier.gi |
// Decoder bit width (defines how many bits are considered for address decoding)
|
92 |
|
|
parameter DEC_WD = 3;
|
93 |
|
|
|
94 |
|
|
// Register addresses offset
|
95 |
|
|
parameter [DEC_WD-1:0] DAC_VAL = 'h0,
|
96 |
|
|
DAC_STAT = 'h2,
|
97 |
|
|
CNTRL1 = 'h4,
|
98 |
|
|
CNTRL2 = 'h6;
|
99 |
|
|
|
100 |
|
|
// Register one-hot decoder utilities
|
101 |
|
|
parameter DEC_SZ = 2**DEC_WD;
|
102 |
|
|
parameter [DEC_SZ-1:0] BASE_REG = {{DEC_SZ-1{1'b0}}, 1'b1};
|
103 |
|
|
|
104 |
80 |
olivier.gi |
// Register one-hot decoder
|
105 |
111 |
olivier.gi |
parameter [DEC_SZ-1:0] DAC_VAL_D = (BASE_REG << DAC_VAL),
|
106 |
|
|
DAC_STAT_D = (BASE_REG << DAC_STAT),
|
107 |
|
|
CNTRL1_D = (BASE_REG << CNTRL1),
|
108 |
|
|
CNTRL2_D = (BASE_REG << CNTRL2);
|
109 |
|
|
|
110 |
80 |
olivier.gi |
|
111 |
|
|
//============================================================================
|
112 |
|
|
// 2) REGISTER DECODER
|
113 |
|
|
//============================================================================
|
114 |
|
|
|
115 |
111 |
olivier.gi |
// Local register selection
|
116 |
|
|
wire reg_sel = per_en & (per_addr[13:DEC_WD-1]==BASE_ADDR[14:DEC_WD]);
|
117 |
|
|
|
118 |
|
|
// Register local address
|
119 |
|
|
wire [DEC_WD-1:0] reg_addr = {per_addr[DEC_WD-2:0], 1'b0};
|
120 |
|
|
|
121 |
80 |
olivier.gi |
// Register address decode
|
122 |
111 |
olivier.gi |
wire [DEC_SZ-1:0] reg_dec = (DAC_VAL_D & {DEC_SZ{(reg_addr == DAC_VAL )}}) |
|
123 |
|
|
(DAC_STAT_D & {DEC_SZ{(reg_addr == DAC_STAT)}}) |
|
124 |
|
|
(CNTRL1_D & {DEC_SZ{(reg_addr == CNTRL1 )}}) |
|
125 |
|
|
(CNTRL2_D & {DEC_SZ{(reg_addr == CNTRL2 )}});
|
126 |
|
|
|
127 |
80 |
olivier.gi |
// Read/Write probes
|
128 |
111 |
olivier.gi |
wire reg_write = |per_we & reg_sel;
|
129 |
|
|
wire reg_read = ~|per_we & reg_sel;
|
130 |
|
|
|
131 |
80 |
olivier.gi |
// Read/Write vectors
|
132 |
111 |
olivier.gi |
wire [DEC_SZ-1:0] reg_wr = reg_dec & {DEC_SZ{reg_write}};
|
133 |
|
|
wire [DEC_SZ-1:0] reg_rd = reg_dec & {DEC_SZ{reg_read}};
|
134 |
|
|
|
135 |
80 |
olivier.gi |
|
136 |
|
|
//============================================================================
|
137 |
|
|
// 3) REGISTERS
|
138 |
|
|
//============================================================================
|
139 |
|
|
|
140 |
|
|
// DAC_VAL Register
|
141 |
|
|
//------------------
|
142 |
|
|
reg [11:0] dac_val;
|
143 |
|
|
reg dac_pd0;
|
144 |
|
|
reg dac_pd1;
|
145 |
|
|
|
146 |
|
|
wire dac_val_wr = reg_wr[DAC_VAL];
|
147 |
|
|
|
148 |
111 |
olivier.gi |
always @ (posedge mclk or posedge puc_rst)
|
149 |
|
|
if (puc_rst)
|
150 |
80 |
olivier.gi |
begin
|
151 |
|
|
dac_val <= 12'h000;
|
152 |
|
|
dac_pd0 <= 1'b0;
|
153 |
|
|
dac_pd1 <= 1'b0;
|
154 |
|
|
end
|
155 |
|
|
else if (dac_val_wr)
|
156 |
|
|
begin
|
157 |
|
|
dac_val <= per_din[11:0];
|
158 |
|
|
dac_pd0 <= per_din[12];
|
159 |
|
|
dac_pd1 <= per_din[13];
|
160 |
|
|
end
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
// CNTRL1 Register
|
164 |
|
|
//------------------
|
165 |
|
|
reg [3:0] cntrl1;
|
166 |
|
|
|
167 |
|
|
wire cntrl1_wr = reg_wr[CNTRL1];
|
168 |
|
|
|
169 |
111 |
olivier.gi |
always @ (posedge mclk or posedge puc_rst)
|
170 |
|
|
if (puc_rst) cntrl1 <= 4'h0;
|
171 |
80 |
olivier.gi |
else if (cntrl1_wr) cntrl1 <= per_din;
|
172 |
|
|
|
173 |
|
|
|
174 |
|
|
// CNTRL2 Register
|
175 |
|
|
//------------------
|
176 |
|
|
reg [3:0] cntrl2;
|
177 |
|
|
|
178 |
|
|
wire cntrl2_wr = reg_wr[CNTRL2];
|
179 |
|
|
|
180 |
111 |
olivier.gi |
always @ (posedge mclk or posedge puc_rst)
|
181 |
|
|
if (puc_rst) cntrl2 <= 4'h0;
|
182 |
80 |
olivier.gi |
else if (cntrl2_wr) cntrl2 <= per_din;
|
183 |
|
|
|
184 |
|
|
|
185 |
|
|
|
186 |
|
|
//============================================================================
|
187 |
|
|
// 4) DATA OUTPUT GENERATION
|
188 |
|
|
//============================================================================
|
189 |
|
|
|
190 |
|
|
// Data output mux
|
191 |
|
|
wire [15:0] dac_val_rd = { 2'b00, dac_pd1, dac_pd0, dac_val} & {16{reg_rd[DAC_VAL]}};
|
192 |
|
|
wire [15:0] dac_stat_rd = {15'h0000, ~sync_n} & {16{reg_rd[DAC_STAT]}};
|
193 |
|
|
wire [15:0] cntrl1_rd = {12'h000, cntrl1} & {16{reg_rd[CNTRL1]}};
|
194 |
|
|
wire [15:0] cntrl2_rd = {12'h000, cntrl2} & {16{reg_rd[CNTRL2]}};
|
195 |
|
|
|
196 |
|
|
wire [15:0] per_dout = dac_val_rd |
|
197 |
|
|
dac_stat_rd |
|
198 |
|
|
cntrl1_rd |
|
199 |
|
|
cntrl2_rd;
|
200 |
|
|
|
201 |
|
|
|
202 |
|
|
//============================================================================
|
203 |
|
|
// 5) SPI INTERFACE
|
204 |
|
|
//============================================================================
|
205 |
|
|
|
206 |
|
|
// SPI Clock divider
|
207 |
|
|
reg [3:0] spi_clk_div;
|
208 |
111 |
olivier.gi |
always @ (posedge mclk or posedge puc_rst)
|
209 |
|
|
if (puc_rst) spi_clk_div <= SCLK_DIV;
|
210 |
80 |
olivier.gi |
else if (spi_clk_div==0) spi_clk_div <= SCLK_DIV;
|
211 |
|
|
else spi_clk_div <= spi_clk_div-1;
|
212 |
|
|
|
213 |
|
|
// SPI Clock generation
|
214 |
|
|
reg sclk;
|
215 |
111 |
olivier.gi |
always @ (posedge mclk or posedge puc_rst)
|
216 |
|
|
if (puc_rst) sclk <= 1'b0;
|
217 |
80 |
olivier.gi |
else if (spi_clk_div==0) sclk <= ~sclk;
|
218 |
|
|
|
219 |
|
|
wire sclk_re = (spi_clk_div==0) & ~sclk;
|
220 |
|
|
|
221 |
|
|
// SPI Transfer trigger
|
222 |
|
|
reg spi_tfx_trig;
|
223 |
111 |
olivier.gi |
always @ (posedge mclk or posedge puc_rst)
|
224 |
|
|
if (puc_rst) spi_tfx_trig <= 1'b0;
|
225 |
80 |
olivier.gi |
else if (dac_val_wr) spi_tfx_trig <= 1'b1;
|
226 |
|
|
else if (sclk_re & sync_n) spi_tfx_trig <= 1'b0;
|
227 |
|
|
|
228 |
|
|
wire spi_tfx_init = spi_tfx_trig & sync_n;
|
229 |
|
|
|
230 |
|
|
// Data counter
|
231 |
|
|
reg [3:0] spi_cnt;
|
232 |
|
|
wire spi_cnt_done = (spi_cnt==4'hf);
|
233 |
111 |
olivier.gi |
always @ (posedge mclk or posedge puc_rst)
|
234 |
|
|
if (puc_rst) spi_cnt <= 4'hf;
|
235 |
80 |
olivier.gi |
else if (sclk_re)
|
236 |
|
|
if (spi_tfx_init) spi_cnt <= 4'he;
|
237 |
|
|
else if (~spi_cnt_done) spi_cnt <= spi_cnt-1;
|
238 |
|
|
|
239 |
|
|
|
240 |
|
|
// Frame synchronization signal (low active)
|
241 |
|
|
reg sync_n;
|
242 |
111 |
olivier.gi |
always @ (posedge mclk or posedge puc_rst)
|
243 |
|
|
if (puc_rst) sync_n <= 1'b1;
|
244 |
80 |
olivier.gi |
else if (sclk_re)
|
245 |
|
|
if (spi_tfx_init) sync_n <= 1'b0;
|
246 |
|
|
else if (spi_cnt_done) sync_n <= 1'b1;
|
247 |
|
|
|
248 |
|
|
|
249 |
|
|
// Value to be shifted_out
|
250 |
|
|
reg [15:0] dac_shifter;
|
251 |
111 |
olivier.gi |
always @ (posedge mclk or posedge puc_rst)
|
252 |
|
|
if (puc_rst) dac_shifter <= 16'h000;
|
253 |
80 |
olivier.gi |
else if (sclk_re)
|
254 |
|
|
if (spi_tfx_init) dac_shifter <= {2'b00, dac_pd1, dac_pd0, dac_val[11:0]};
|
255 |
|
|
else dac_shifter <= {dac_shifter[14:0], 1'b0};
|
256 |
|
|
|
257 |
|
|
assign din = dac_shifter[15];
|
258 |
|
|
|
259 |
|
|
|
260 |
|
|
endmodule // dac_spi_if
|
261 |
|
|
|