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 |
|
|
`include "timescale.v"
|
44 |
|
|
|
45 |
|
|
module dac_spi_if (
|
46 |
|
|
|
47 |
|
|
// OUTPUTs
|
48 |
|
|
cntrl1, // Control value 1
|
49 |
|
|
cntrl2, // Control value 2
|
50 |
|
|
din, // SPI Serial Data
|
51 |
|
|
per_dout, // Peripheral data output
|
52 |
|
|
sclk, // SPI Serial Clock
|
53 |
|
|
sync_n, // SPI Frame synchronization signal (low active)
|
54 |
|
|
|
55 |
|
|
// INPUTs
|
56 |
|
|
mclk, // Main system clock
|
57 |
|
|
per_addr, // Peripheral address
|
58 |
|
|
per_din, // Peripheral data input
|
59 |
|
|
per_en, // Peripheral enable (high active)
|
60 |
|
|
per_wen, // Peripheral write enable (high active)
|
61 |
|
|
puc // Main system reset
|
62 |
|
|
);
|
63 |
|
|
|
64 |
|
|
// PARAMETERs
|
65 |
|
|
//============
|
66 |
|
|
parameter SCLK_DIV = 0; // Serial clock divider (Tsclk=Tmclk*(SCLK_DIV+1)*2)
|
67 |
|
|
parameter BASE_ADDR = 9'h190; // Registers base address
|
68 |
|
|
|
69 |
|
|
// OUTPUTs
|
70 |
|
|
//=========
|
71 |
|
|
output [3:0] cntrl1; // Control value 1
|
72 |
|
|
output [3:0] cntrl2; // Control value 2
|
73 |
|
|
output din; // SPI Serial Data
|
74 |
|
|
output [15:0] per_dout; // Peripheral data output
|
75 |
|
|
output sclk; // SPI Serial Clock
|
76 |
|
|
output sync_n; // SPI Frame synchronization signal (low active)
|
77 |
|
|
|
78 |
|
|
// INPUTs
|
79 |
|
|
//=========
|
80 |
|
|
input mclk; // Main system clock
|
81 |
|
|
input [7:0] per_addr; // Peripheral address
|
82 |
|
|
input [15:0] per_din; // Peripheral data input
|
83 |
|
|
input per_en; // Peripheral enable (high active)
|
84 |
|
|
input [1:0] per_wen; // Peripheral write enable (high active)
|
85 |
|
|
input puc; // Main system reset
|
86 |
|
|
|
87 |
|
|
|
88 |
|
|
//=============================================================================
|
89 |
|
|
// 1) PARAMETER DECLARATION
|
90 |
|
|
//=============================================================================
|
91 |
|
|
|
92 |
|
|
// Register addresses
|
93 |
|
|
parameter DAC_VAL = BASE_ADDR;
|
94 |
|
|
parameter DAC_STAT = BASE_ADDR+2;
|
95 |
|
|
parameter CNTRL1 = BASE_ADDR+4;
|
96 |
|
|
parameter CNTRL2 = BASE_ADDR+6;
|
97 |
|
|
|
98 |
|
|
|
99 |
|
|
// Register one-hot decoder
|
100 |
|
|
parameter DAC_VAL_D = (512'h1 << DAC_VAL);
|
101 |
|
|
parameter DAC_STAT_D = (512'h1 << DAC_STAT);
|
102 |
|
|
parameter CNTRL1_D = (512'h1 << CNTRL1);
|
103 |
|
|
parameter CNTRL2_D = (512'h1 << CNTRL2);
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
//============================================================================
|
107 |
|
|
// 2) REGISTER DECODER
|
108 |
|
|
//============================================================================
|
109 |
|
|
|
110 |
|
|
// Register address decode
|
111 |
|
|
reg [511:0] reg_dec;
|
112 |
|
|
always @(per_addr)
|
113 |
|
|
case ({per_addr,1'b0})
|
114 |
|
|
DAC_VAL : reg_dec = DAC_VAL_D;
|
115 |
|
|
DAC_STAT: reg_dec = DAC_STAT_D;
|
116 |
|
|
CNTRL1 : reg_dec = CNTRL1_D;
|
117 |
|
|
CNTRL2 : reg_dec = CNTRL2_D;
|
118 |
|
|
default : reg_dec = {512{1'b0}};
|
119 |
|
|
endcase
|
120 |
|
|
|
121 |
|
|
// Read/Write probes
|
122 |
|
|
wire reg_write = |per_wen & per_en;
|
123 |
|
|
wire reg_read = ~|per_wen & per_en;
|
124 |
|
|
|
125 |
|
|
// Read/Write vectors
|
126 |
|
|
wire [511:0] reg_wr = reg_dec & {512{reg_write}};
|
127 |
|
|
wire [511:0] reg_rd = reg_dec & {512{reg_read}};
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
//============================================================================
|
131 |
|
|
// 3) REGISTERS
|
132 |
|
|
//============================================================================
|
133 |
|
|
|
134 |
|
|
// DAC_VAL Register
|
135 |
|
|
//------------------
|
136 |
|
|
reg [11:0] dac_val;
|
137 |
|
|
reg dac_pd0;
|
138 |
|
|
reg dac_pd1;
|
139 |
|
|
|
140 |
|
|
wire dac_val_wr = reg_wr[DAC_VAL];
|
141 |
|
|
|
142 |
|
|
always @ (posedge mclk or posedge puc)
|
143 |
|
|
if (puc)
|
144 |
|
|
begin
|
145 |
|
|
dac_val <= 12'h000;
|
146 |
|
|
dac_pd0 <= 1'b0;
|
147 |
|
|
dac_pd1 <= 1'b0;
|
148 |
|
|
end
|
149 |
|
|
else if (dac_val_wr)
|
150 |
|
|
begin
|
151 |
|
|
dac_val <= per_din[11:0];
|
152 |
|
|
dac_pd0 <= per_din[12];
|
153 |
|
|
dac_pd1 <= per_din[13];
|
154 |
|
|
end
|
155 |
|
|
|
156 |
|
|
|
157 |
|
|
// CNTRL1 Register
|
158 |
|
|
//------------------
|
159 |
|
|
reg [3:0] cntrl1;
|
160 |
|
|
|
161 |
|
|
wire cntrl1_wr = reg_wr[CNTRL1];
|
162 |
|
|
|
163 |
|
|
always @ (posedge mclk or posedge puc)
|
164 |
|
|
if (puc) cntrl1 <= 4'h0;
|
165 |
|
|
else if (cntrl1_wr) cntrl1 <= per_din;
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
// CNTRL2 Register
|
169 |
|
|
//------------------
|
170 |
|
|
reg [3:0] cntrl2;
|
171 |
|
|
|
172 |
|
|
wire cntrl2_wr = reg_wr[CNTRL2];
|
173 |
|
|
|
174 |
|
|
always @ (posedge mclk or posedge puc)
|
175 |
|
|
if (puc) cntrl2 <= 4'h0;
|
176 |
|
|
else if (cntrl2_wr) cntrl2 <= per_din;
|
177 |
|
|
|
178 |
|
|
|
179 |
|
|
|
180 |
|
|
//============================================================================
|
181 |
|
|
// 4) DATA OUTPUT GENERATION
|
182 |
|
|
//============================================================================
|
183 |
|
|
|
184 |
|
|
// Data output mux
|
185 |
|
|
wire [15:0] dac_val_rd = { 2'b00, dac_pd1, dac_pd0, dac_val} & {16{reg_rd[DAC_VAL]}};
|
186 |
|
|
wire [15:0] dac_stat_rd = {15'h0000, ~sync_n} & {16{reg_rd[DAC_STAT]}};
|
187 |
|
|
wire [15:0] cntrl1_rd = {12'h000, cntrl1} & {16{reg_rd[CNTRL1]}};
|
188 |
|
|
wire [15:0] cntrl2_rd = {12'h000, cntrl2} & {16{reg_rd[CNTRL2]}};
|
189 |
|
|
|
190 |
|
|
wire [15:0] per_dout = dac_val_rd |
|
191 |
|
|
dac_stat_rd |
|
192 |
|
|
cntrl1_rd |
|
193 |
|
|
cntrl2_rd;
|
194 |
|
|
|
195 |
|
|
|
196 |
|
|
//============================================================================
|
197 |
|
|
// 5) SPI INTERFACE
|
198 |
|
|
//============================================================================
|
199 |
|
|
|
200 |
|
|
// SPI Clock divider
|
201 |
|
|
reg [3:0] spi_clk_div;
|
202 |
|
|
always @ (posedge mclk or posedge puc)
|
203 |
|
|
if (puc) spi_clk_div <= SCLK_DIV;
|
204 |
|
|
else if (spi_clk_div==0) spi_clk_div <= SCLK_DIV;
|
205 |
|
|
else spi_clk_div <= spi_clk_div-1;
|
206 |
|
|
|
207 |
|
|
// SPI Clock generation
|
208 |
|
|
reg sclk;
|
209 |
|
|
always @ (posedge mclk or posedge puc)
|
210 |
|
|
if (puc) sclk <= 1'b0;
|
211 |
|
|
else if (spi_clk_div==0) sclk <= ~sclk;
|
212 |
|
|
|
213 |
|
|
wire sclk_re = (spi_clk_div==0) & ~sclk;
|
214 |
|
|
|
215 |
|
|
// SPI Transfer trigger
|
216 |
|
|
reg spi_tfx_trig;
|
217 |
|
|
always @ (posedge mclk or posedge puc)
|
218 |
|
|
if (puc) spi_tfx_trig <= 1'b0;
|
219 |
|
|
else if (dac_val_wr) spi_tfx_trig <= 1'b1;
|
220 |
|
|
else if (sclk_re & sync_n) spi_tfx_trig <= 1'b0;
|
221 |
|
|
|
222 |
|
|
wire spi_tfx_init = spi_tfx_trig & sync_n;
|
223 |
|
|
|
224 |
|
|
// Data counter
|
225 |
|
|
reg [3:0] spi_cnt;
|
226 |
|
|
wire spi_cnt_done = (spi_cnt==4'hf);
|
227 |
|
|
always @ (posedge mclk or posedge puc)
|
228 |
|
|
if (puc) spi_cnt <= 4'hf;
|
229 |
|
|
else if (sclk_re)
|
230 |
|
|
if (spi_tfx_init) spi_cnt <= 4'he;
|
231 |
|
|
else if (~spi_cnt_done) spi_cnt <= spi_cnt-1;
|
232 |
|
|
|
233 |
|
|
|
234 |
|
|
// Frame synchronization signal (low active)
|
235 |
|
|
reg sync_n;
|
236 |
|
|
always @ (posedge mclk or posedge puc)
|
237 |
|
|
if (puc) sync_n <= 1'b1;
|
238 |
|
|
else if (sclk_re)
|
239 |
|
|
if (spi_tfx_init) sync_n <= 1'b0;
|
240 |
|
|
else if (spi_cnt_done) sync_n <= 1'b1;
|
241 |
|
|
|
242 |
|
|
|
243 |
|
|
// Value to be shifted_out
|
244 |
|
|
reg [15:0] dac_shifter;
|
245 |
|
|
always @ (posedge mclk or posedge puc)
|
246 |
|
|
if (puc) dac_shifter <= 16'h000;
|
247 |
|
|
else if (sclk_re)
|
248 |
|
|
if (spi_tfx_init) dac_shifter <= {2'b00, dac_pd1, dac_pd0, dac_val[11:0]};
|
249 |
|
|
else dac_shifter <= {dac_shifter[14:0], 1'b0};
|
250 |
|
|
|
251 |
|
|
assign din = dac_shifter[15];
|
252 |
|
|
|
253 |
|
|
|
254 |
|
|
endmodule // dac_spi_if
|
255 |
|
|
|