1 |
2 |
MichaelA |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Copyright 2013 by Michael A. Morris, dba M. A. Morris & Associates
|
4 |
|
|
//
|
5 |
|
|
// All rights reserved. The source code contained herein is publicly released
|
6 |
|
|
// under the terms and conditions of the GNU Lesser Public License. No part of
|
7 |
|
|
// this source code may be reproduced or transmitted in any form or by any
|
8 |
|
|
// means, electronic or mechanical, including photocopying, recording, or any
|
9 |
|
|
// information storage and retrieval system in violation of the license under
|
10 |
|
|
// which the source code is released.
|
11 |
|
|
//
|
12 |
|
|
// The source code contained herein is free; it may be redistributed and/or
|
13 |
|
|
// modified in accordance with the terms of the GNU Lesser General Public
|
14 |
|
|
// License as published by the Free Software Foundation; either version 2.1 of
|
15 |
|
|
// the GNU Lesser General Public License, or any later version.
|
16 |
|
|
//
|
17 |
|
|
// The source code contained herein is freely released WITHOUT ANY WARRANTY;
|
18 |
|
|
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
19 |
|
|
// PARTICULAR PURPOSE. (Refer to the GNU Lesser General Public License for
|
20 |
|
|
// more details.)
|
21 |
|
|
//
|
22 |
|
|
// A copy of the GNU Lesser General Public License should have been received
|
23 |
|
|
// along with the source code contained herein; if not, a copy can be obtained
|
24 |
|
|
// by writing to:
|
25 |
|
|
//
|
26 |
|
|
// Free Software Foundation, Inc.
|
27 |
|
|
// 51 Franklin Street, Fifth Floor
|
28 |
|
|
// Boston, MA 02110-1301 USA
|
29 |
|
|
//
|
30 |
|
|
// Further, no use of this source code is permitted in any form or means
|
31 |
|
|
// without inclusion of this banner prominently in any derived works.
|
32 |
|
|
//
|
33 |
|
|
// Michael A. Morris
|
34 |
|
|
// Huntsville, AL
|
35 |
|
|
//
|
36 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
37 |
|
|
|
38 |
|
|
`timescale 1ns / 1ps
|
39 |
|
|
|
40 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
41 |
|
|
// Company: M. A. Morris & Associates
|
42 |
|
|
// Engineer: Michael A. Morris
|
43 |
|
|
//
|
44 |
|
|
// Create Date: 14:18:11 06/16/2013
|
45 |
|
|
// Design Name: M16C5x SPI Interface
|
46 |
|
|
// Module Name: M16C5x_SPI
|
47 |
|
|
// Project Name: C:\XProjects\ISE10.1i\M16C5x
|
48 |
|
|
// Target Devices: SRAM-based FPGAs
|
49 |
|
|
// Tool versions: Xilinx ISE 10.1i SP3
|
50 |
|
|
//
|
51 |
|
|
// Description:
|
52 |
|
|
//
|
53 |
|
|
// This module implements an SPI Master for the M16C5x microcontroller. The
|
54 |
|
|
// interface is mapped onto the TRISC and PORTA output/input registers. It pro-
|
55 |
|
|
// vides status outputs that can be read using the PORTA input register. Thus,
|
56 |
|
|
// a sophisticated SPI interface is implemented such that the M16C5x firmware
|
57 |
|
|
// is not required to manage the low level elements of the interface. This
|
58 |
|
|
// frees up the M16C5x core to perform other functions: servicing other peri-
|
59 |
|
|
// pherals or making computations.
|
60 |
|
|
//
|
61 |
|
|
// The control register for the SPI interface is mapped onto TRISC. This is a
|
62 |
|
|
// write-only register in the M16C5x core. The TRISC register will provide the
|
63 |
|
|
// following:
|
64 |
|
|
//
|
65 |
|
|
// (1). SPI Read Enable (1) - 0 - Disable Reads; 1 - Enable Reads
|
66 |
|
|
// (2). Slave Select (1) - 0 - nCS[0]; 1 - nCS[1]
|
67 |
|
|
// (3). SPI Mode Select (2) - 0, 1, 2, 3
|
68 |
|
|
// (4). Shift Clk Rate Select (3) - 2, 4, 8, 16, 32, 64 (default), 128
|
69 |
|
|
// (5). Shift Direction (1) - 0 - MSB first (default); 1 - LSB first
|
70 |
|
|
//
|
71 |
|
|
// A complete description of the Shift Clock Rate Select and SPI Mode Select
|
72 |
|
|
// fields is provided in the Description section of SPIxIF.v module. The Slave
|
73 |
|
|
// Select bit allows the M16C5x core to select between two SPI devices; the
|
74 |
|
|
// interface drives nCS[0] if the bit is set to 0 (default), or nCS[1] if the
|
75 |
|
|
// bit is set to 1. The SPI Read Enable bit is the value written to bit 9 of
|
76 |
|
|
// the transmit FIFO. If bit 9 is set, the SPI data received on MISO is written to
|
77 |
|
|
// the Receive FIFO. Refer to the Description section of SPIxIF.v for more
|
78 |
|
|
// information regarding the use of bit 9 of the transmit FIFO. The Shift
|
79 |
|
|
// Direction bit determines if the shift direction is MSB first (default) or
|
80 |
|
|
// LSB first. (Unless otherwise set, the default after Rst of the CR sets the
|
81 |
|
|
// SPIxIF to operate MSB first, divide Clk by 64, Mode 0, select slave 0, and
|
82 |
|
|
// disable SPI reads: 0x60.)
|
83 |
|
|
//
|
84 |
|
|
// Five bits are output by the module for use by the M16C5x core to manage the
|
85 |
|
|
// peripheral. The SS bit is set when the SPIxIF is performing an SPI shift. It
|
86 |
|
|
// is used, along with the Slave Select bit, to generate nCS[1:0]. If SS is a
|
87 |
|
|
// logic 1, the SPIxIF is busy. The FIFO Full and Empty flags for each FIFO are
|
88 |
|
|
// output. The M16C5x core firmware can their state along with information on
|
89 |
|
|
// their depth to manage the sending and receiving of data to/from an SPI com-
|
90 |
|
|
// ponent.
|
91 |
|
|
//
|
92 |
|
|
// Dependencies: SPIxIF.v - SPI Master Interface
|
93 |
|
|
// DPSFnmCE.v - LUT-based Synchronous Parameterizable FIFO
|
94 |
|
|
//
|
95 |
|
|
// Revision:
|
96 |
|
|
//
|
97 |
|
|
// 0.01 13F16 MAM Initial creation
|
98 |
|
|
//
|
99 |
|
|
// 1.00 13F26 MAM Included ClkEn in the transmit FIFO write enable and
|
100 |
|
|
// receive FIFO read enable signals.
|
101 |
|
|
//
|
102 |
|
|
// 1.10 13F06 MAM Changed polarity of chip select outputs from active
|
103 |
|
|
// low to active high.
|
104 |
|
|
//
|
105 |
|
|
// 1.20 13G14 MAM Improved parameterization. All relevant parameters
|
106 |
|
|
// can be set through instantiation interface.
|
107 |
|
|
//
|
108 |
|
|
// Additional Comments:
|
109 |
|
|
//
|
110 |
|
|
// CR[0] - Read FIFO Disable
|
111 |
|
|
//
|
112 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
113 |
|
|
|
114 |
|
|
module M16C5x_SPI #(
|
115 |
|
|
parameter pCR_Default = 8'b0_110_00_0_0, // Default SPI Interface Setting
|
116 |
|
|
parameter pTF_Depth = 4, // Default Transmit FIFO Depth: 2**pTF_Depth
|
117 |
|
|
parameter pRF_Depth = 4, // Default Receive FIFO Depth: 2**pRF_Depth
|
118 |
|
|
parameter pTF_Init = "Src/TF_Init.coe", // Tx FIFO Memory Initialization
|
119 |
|
|
parameter pRF_Init = "Src/RF_Init.coe" // Rx FIFO Memory Initialization
|
120 |
|
|
)(
|
121 |
|
|
input Rst, // System Reset
|
122 |
|
|
input Clk, // System Clk; SCK derived from Clk
|
123 |
|
|
|
124 |
|
|
input ClkEn, // System Clock Enable
|
125 |
|
|
|
126 |
|
|
input WE_CR, // Control Register Write Enable (WE_TRISx)
|
127 |
|
|
input WE_TF, // Transmit FIFO Write Enable (WE_PORTx)
|
128 |
|
|
input RE_RF, // Receive FIFO Read Enable (RE_PORTx)
|
129 |
|
|
input [7:0] DI, // Data Input (Cntl Reg/Transmit FIFO Data In)
|
130 |
|
|
output [7:0] DO, // Data Output (Receive FIFO Data Out)
|
131 |
|
|
|
132 |
|
|
output [1:0] CS, // SPI Interface Chip Select (active high)
|
133 |
|
|
output SCK, // SPI Interface Serial Clock (idle set by Mode)
|
134 |
|
|
output MOSI, // SPI Interface Master Out/Slave In Serial Out
|
135 |
|
|
input MISO, // SPI Interface Master In/Slave Out Serial In
|
136 |
|
|
|
137 |
|
|
output SS, // SPI Interface Slave Select Active
|
138 |
|
|
output TF_FF, // SPI Interface Transmit FIFO Full Flag
|
139 |
|
|
output TF_EF, // SPI Interface Transmit FIFO Empty Flag
|
140 |
|
|
output RF_FF, // SPI Interface Receive FIFO Full Flag
|
141 |
|
|
output RF_EF // SPI Interface Receive FIFO Empty Flag
|
142 |
|
|
);
|
143 |
|
|
|
144 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
145 |
|
|
//
|
146 |
|
|
// Declarations
|
147 |
|
|
//
|
148 |
|
|
|
149 |
|
|
reg [7:0] CR = pCR_Default; // Control Register
|
150 |
|
|
|
151 |
|
|
wire REn;
|
152 |
|
|
wire Sel;
|
153 |
|
|
wire [1:0] Mode;
|
154 |
|
|
wire [2:0] Rate;
|
155 |
|
|
wire Dir;
|
156 |
|
|
|
157 |
|
|
wire DAV;
|
158 |
|
|
wire FRE;
|
159 |
|
|
wire [8:0] TD;
|
160 |
|
|
|
161 |
|
|
wire FWE;
|
162 |
|
|
wire [7:0] RD;
|
163 |
|
|
|
164 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
165 |
|
|
//
|
166 |
|
|
// Implementation
|
167 |
|
|
//
|
168 |
|
|
|
169 |
|
|
always @(posedge Clk)
|
170 |
|
|
begin
|
171 |
|
|
if(Rst)
|
172 |
|
|
CR <= #1 pCR_Default;
|
173 |
|
|
else if(WE_CR & ClkEn)
|
174 |
|
|
CR <= #1 DI;
|
175 |
|
|
end
|
176 |
|
|
|
177 |
|
|
assign REn = CR[0];
|
178 |
|
|
assign Sel = CR[1];
|
179 |
|
|
assign Mode = CR[3:2];
|
180 |
|
|
assign Rate = CR[6:4];
|
181 |
|
|
assign Dir = CR[7];
|
182 |
|
|
|
183 |
|
|
// Instantiate the Transmit FIFO module
|
184 |
|
|
|
185 |
|
|
DPSFnmCE #(
|
186 |
|
|
.addr(pTF_Depth),
|
187 |
|
|
.width(9),
|
188 |
|
|
.init(pTF_Init)
|
189 |
|
|
) TF (
|
190 |
|
|
.Rst(Rst),
|
191 |
|
|
.Clk(Clk),
|
192 |
|
|
|
193 |
|
|
.WE(WE_TF & ClkEn),
|
194 |
|
|
.DI({REn, DI}),
|
195 |
|
|
|
196 |
|
|
.RE(FRE),
|
197 |
|
|
.DO(TD),
|
198 |
|
|
|
199 |
|
|
.FF(TF_FF),
|
200 |
|
|
.EF(TF_EF),
|
201 |
|
|
.HF(),
|
202 |
|
|
.Cnt()
|
203 |
|
|
);
|
204 |
|
|
|
205 |
|
|
assign DAV = ~TF_EF;
|
206 |
|
|
|
207 |
|
|
// Instantiate the Receive FIFO module
|
208 |
|
|
|
209 |
|
|
DPSFnmCE #(
|
210 |
|
|
.addr(pRF_Depth),
|
211 |
|
|
.width(8),
|
212 |
|
|
.init(pRF_Init)
|
213 |
|
|
) RF (
|
214 |
|
|
.Rst(Rst),
|
215 |
|
|
.Clk(Clk),
|
216 |
|
|
|
217 |
|
|
.WE(FWE),
|
218 |
|
|
.DI(RD),
|
219 |
|
|
|
220 |
|
|
.RE(RE_RF & ClkEn),
|
221 |
|
|
.DO(DO),
|
222 |
|
|
|
223 |
|
|
.FF(RF_FF),
|
224 |
|
|
.EF(RF_EF),
|
225 |
|
|
.HF(),
|
226 |
|
|
.Cnt()
|
227 |
|
|
);
|
228 |
|
|
|
229 |
|
|
// Instantiate the SPI Master Interface module
|
230 |
|
|
|
231 |
|
|
SPIxIF MSTR (
|
232 |
|
|
.Rst(Rst), // System Reset
|
233 |
|
|
.Clk(Clk), // System Clock
|
234 |
|
|
|
235 |
|
|
.LSB(Dir), // Shift Direction
|
236 |
|
|
.Mode(Mode), // SPI Operating Mode
|
237 |
|
|
.Rate(Rate), // SCK Rate Divider
|
238 |
|
|
|
239 |
|
|
.DAV(DAV), // Complement of TF_EF
|
240 |
|
|
|
241 |
|
|
.FRE(FRE), // Transmit FIFO Read Enable
|
242 |
|
|
.TD(TD), // Transmit Data; bit 8 enables receiver
|
243 |
|
|
|
244 |
|
|
.FWE(FWE), // Receive FIFO Write Enable
|
245 |
|
|
.RD(RD), // Receive Data
|
246 |
|
|
|
247 |
|
|
.SS(SS), // SPI Slave Select
|
248 |
|
|
.SCK(SCK), // SPI Serial Clock
|
249 |
|
|
.MOSI(MOSI), // SPI Master Out/Slave In
|
250 |
|
|
.MISO(MISO) // SPI Master In/Slave Out
|
251 |
|
|
);
|
252 |
|
|
|
253 |
|
|
// Generate Slave Device Chip Select based on SS and Sel bit in CR
|
254 |
|
|
|
255 |
|
|
assign CS[0] = ((~Sel) ? SS : 0);
|
256 |
|
|
assign CS[1] = (( Sel) ? SS : 0);
|
257 |
|
|
|
258 |
|
|
endmodule
|