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