1 |
80 |
olivier.gi |
//----------------------------------------------------------------------------
|
2 |
|
|
// Copyright (C) 2001 Authors
|
3 |
|
|
//
|
4 |
|
|
// This source file may be used and distributed without restriction provided
|
5 |
|
|
// that this copyright statement is not removed from the file and that any
|
6 |
|
|
// derivative work contains the original copyright notice and the associated
|
7 |
|
|
// disclaimer.
|
8 |
|
|
//
|
9 |
|
|
// This source file is free software; you can redistribute it and/or modify
|
10 |
|
|
// it under the terms of the GNU Lesser General Public License as published
|
11 |
|
|
// by the Free Software Foundation; either version 2.1 of the License, or
|
12 |
|
|
// (at your option) any later version.
|
13 |
|
|
//
|
14 |
|
|
// This source is distributed in the hope that it will be useful, but WITHOUT
|
15 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
16 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
17 |
|
|
// License for more details.
|
18 |
|
|
//
|
19 |
|
|
// You should have received a copy of the GNU Lesser General Public License
|
20 |
|
|
// along with this source; if not, write to the Free Software Foundation,
|
21 |
|
|
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
22 |
|
|
//
|
23 |
|
|
//----------------------------------------------------------------------------
|
24 |
|
|
//
|
25 |
|
|
// *File Name: omsp_clock_module.v
|
26 |
|
|
//
|
27 |
|
|
// *Module Description:
|
28 |
|
|
// Basic clock module implementation.
|
29 |
|
|
// Since the openMSP430 mainly targets FPGA and hobby
|
30 |
|
|
// designers. The clock structure has been greatly
|
31 |
|
|
// symplified in order to ease integration.
|
32 |
|
|
// See online wiki for more info.
|
33 |
|
|
//
|
34 |
|
|
// *Author(s):
|
35 |
|
|
// - Olivier Girard, olgirard@gmail.com
|
36 |
|
|
//
|
37 |
|
|
//----------------------------------------------------------------------------
|
38 |
|
|
// $Rev: 37 $
|
39 |
|
|
// $LastChangedBy: olivier.girard $
|
40 |
|
|
// $LastChangedDate: 2009-12-29 21:58:14 +0100 (Tue, 29 Dec 2009) $
|
41 |
|
|
//----------------------------------------------------------------------------
|
42 |
|
|
`include "timescale.v"
|
43 |
|
|
`include "openMSP430_defines.v"
|
44 |
|
|
|
45 |
|
|
module omsp_clock_module (
|
46 |
|
|
|
47 |
|
|
// OUTPUTs
|
48 |
|
|
aclk_en, // ACLK enable
|
49 |
|
|
mclk, // Main system clock
|
50 |
|
|
per_dout, // Peripheral data output
|
51 |
|
|
por, // Power-on reset
|
52 |
|
|
puc, // Main system reset
|
53 |
|
|
smclk_en, // SMCLK enable
|
54 |
|
|
|
55 |
|
|
// INPUTs
|
56 |
|
|
dbg_reset, // Reset CPU from debug interface
|
57 |
|
|
dco_clk, // Fast oscillator (fast clock)
|
58 |
|
|
lfxt_clk, // Low frequency oscillator (typ 32kHz)
|
59 |
|
|
oscoff, // Turns off LFXT1 clock input
|
60 |
|
|
per_addr, // Peripheral address
|
61 |
|
|
per_din, // Peripheral data input
|
62 |
|
|
per_en, // Peripheral enable (high active)
|
63 |
|
|
per_wen, // Peripheral write enable (high active)
|
64 |
|
|
reset_n, // Reset Pin (low active)
|
65 |
|
|
scg1, // System clock generator 1. Turns off the SMCLK
|
66 |
|
|
wdt_reset // Watchdog-timer reset
|
67 |
|
|
);
|
68 |
|
|
|
69 |
|
|
// OUTPUTs
|
70 |
|
|
//=========
|
71 |
|
|
output aclk_en; // ACLK enable
|
72 |
|
|
output mclk; // Main system clock
|
73 |
|
|
output [15:0] per_dout; // Peripheral data output
|
74 |
|
|
output por; // Power-on reset
|
75 |
|
|
output puc; // Main system reset
|
76 |
|
|
output smclk_en; // SMCLK enable
|
77 |
|
|
|
78 |
|
|
// INPUTs
|
79 |
|
|
//=========
|
80 |
|
|
input dbg_reset; // Reset CPU from debug interface
|
81 |
|
|
input dco_clk; // Fast oscillator (fast clock)
|
82 |
|
|
input lfxt_clk; // Low frequency oscillator (typ 32kHz)
|
83 |
|
|
input oscoff; // Turns off LFXT1 clock input
|
84 |
|
|
input [7:0] per_addr; // Peripheral address
|
85 |
|
|
input [15:0] per_din; // Peripheral data input
|
86 |
|
|
input per_en; // Peripheral enable (high active)
|
87 |
|
|
input [1:0] per_wen; // Peripheral write enable (high active)
|
88 |
|
|
input reset_n; // Reset Pin (low active)
|
89 |
|
|
input scg1; // System clock generator 1. Turns off the SMCLK
|
90 |
|
|
input wdt_reset; // Watchdog-timer reset
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
//=============================================================================
|
94 |
|
|
// 1) PARAMETER DECLARATION
|
95 |
|
|
//=============================================================================
|
96 |
|
|
|
97 |
|
|
// Register addresses
|
98 |
|
|
parameter BCSCTL1 = 9'h057;
|
99 |
|
|
parameter BCSCTL2 = 9'h058;
|
100 |
|
|
|
101 |
|
|
// Register one-hot decoder
|
102 |
|
|
parameter BCSCTL1_D = (256'h1 << (BCSCTL1 /2));
|
103 |
|
|
parameter BCSCTL2_D = (256'h1 << (BCSCTL2 /2));
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
//============================================================================
|
107 |
|
|
// 2) REGISTER DECODER
|
108 |
|
|
//============================================================================
|
109 |
|
|
|
110 |
|
|
// Register address decode
|
111 |
|
|
reg [255:0] reg_dec;
|
112 |
|
|
always @(per_addr)
|
113 |
|
|
case (per_addr)
|
114 |
|
|
(BCSCTL1 /2): reg_dec = BCSCTL1_D;
|
115 |
|
|
(BCSCTL2 /2): reg_dec = BCSCTL2_D;
|
116 |
|
|
default : reg_dec = {256{1'b0}};
|
117 |
|
|
endcase
|
118 |
|
|
|
119 |
|
|
// Read/Write probes
|
120 |
|
|
wire reg_lo_write = per_wen[0] & per_en;
|
121 |
|
|
wire reg_hi_write = per_wen[1] & per_en;
|
122 |
|
|
wire reg_read = ~|per_wen & per_en;
|
123 |
|
|
|
124 |
|
|
// Read/Write vectors
|
125 |
|
|
wire [255:0] reg_hi_wr = reg_dec & {256{reg_hi_write}};
|
126 |
|
|
wire [255:0] reg_lo_wr = reg_dec & {256{reg_lo_write}};
|
127 |
|
|
wire [255:0] reg_rd = reg_dec & {256{reg_read}};
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
//============================================================================
|
131 |
|
|
// 3) REGISTERS
|
132 |
|
|
//============================================================================
|
133 |
|
|
|
134 |
|
|
// BCSCTL1 Register
|
135 |
|
|
//--------------
|
136 |
|
|
reg [7:0] bcsctl1;
|
137 |
|
|
wire bcsctl1_wr = BCSCTL1[0] ? reg_hi_wr[BCSCTL1/2] : reg_lo_wr[BCSCTL1/2];
|
138 |
|
|
wire [7:0] bcsctl1_nxt = BCSCTL1[0] ? per_din[15:8] : per_din[7:0];
|
139 |
|
|
|
140 |
|
|
always @ (posedge mclk or posedge puc)
|
141 |
|
|
if (puc) bcsctl1 <= 8'h00;
|
142 |
|
|
else if (bcsctl1_wr) bcsctl1 <= bcsctl1_nxt & 8'h30; // Mask unused bits
|
143 |
|
|
|
144 |
|
|
|
145 |
|
|
// BCSCTL2 Register
|
146 |
|
|
//--------------
|
147 |
|
|
reg [7:0] bcsctl2;
|
148 |
|
|
wire bcsctl2_wr = BCSCTL2[0] ? reg_hi_wr[BCSCTL2/2] : reg_lo_wr[BCSCTL2/2];
|
149 |
|
|
wire [7:0] bcsctl2_nxt = BCSCTL2[0] ? per_din[15:8] : per_din[7:0];
|
150 |
|
|
|
151 |
|
|
always @ (posedge mclk or posedge puc)
|
152 |
|
|
if (puc) bcsctl2 <= 8'h00;
|
153 |
|
|
else if (bcsctl2_wr) bcsctl2 <= bcsctl2_nxt & 8'h0e; // Mask unused bits
|
154 |
|
|
|
155 |
|
|
|
156 |
|
|
//============================================================================
|
157 |
|
|
// 4) DATA OUTPUT GENERATION
|
158 |
|
|
//============================================================================
|
159 |
|
|
|
160 |
|
|
// Data output mux
|
161 |
|
|
wire [15:0] bcsctl1_rd = (bcsctl1 & {8{reg_rd[BCSCTL1/2]}}) << (8 & {4{BCSCTL1[0]}});
|
162 |
|
|
wire [15:0] bcsctl2_rd = (bcsctl2 & {8{reg_rd[BCSCTL2/2]}}) << (8 & {4{BCSCTL2[0]}});
|
163 |
|
|
|
164 |
|
|
wire [15:0] per_dout = bcsctl1_rd |
|
165 |
|
|
bcsctl2_rd;
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
//=============================================================================
|
169 |
|
|
// 5) CLOCK GENERATION
|
170 |
|
|
//=============================================================================
|
171 |
|
|
|
172 |
|
|
// Synchronize LFXT_CLK & edge detection
|
173 |
|
|
//---------------------------------------
|
174 |
|
|
reg [2:0] lfxt_clk_s;
|
175 |
|
|
|
176 |
|
|
always @ (posedge mclk or posedge puc)
|
177 |
|
|
if (puc) lfxt_clk_s <= 3'b000;
|
178 |
|
|
else lfxt_clk_s <= {lfxt_clk_s[1:0], lfxt_clk};
|
179 |
|
|
|
180 |
|
|
wire lfxt_clk_en = (lfxt_clk_s[1] & ~lfxt_clk_s[2]) & ~(oscoff & ~bcsctl2[`SELS]);
|
181 |
|
|
|
182 |
|
|
|
183 |
|
|
// Generate main system clock
|
184 |
|
|
//----------------------------
|
185 |
|
|
|
186 |
|
|
wire mclk = dco_clk;
|
187 |
|
|
wire mclk_n = !dco_clk;
|
188 |
|
|
|
189 |
|
|
|
190 |
|
|
// Generate ACLK
|
191 |
|
|
//----------------------------
|
192 |
|
|
|
193 |
|
|
reg [2:0] aclk_div;
|
194 |
|
|
|
195 |
|
|
wire aclk_en = lfxt_clk_en & ((bcsctl1[`DIVAx]==2'b00) ? 1'b1 :
|
196 |
|
|
(bcsctl1[`DIVAx]==2'b01) ? aclk_div[0] :
|
197 |
|
|
(bcsctl1[`DIVAx]==2'b10) ? &aclk_div[1:0] :
|
198 |
|
|
&aclk_div[2:0]);
|
199 |
|
|
|
200 |
|
|
always @ (posedge mclk or posedge puc)
|
201 |
|
|
if (puc) aclk_div <= 3'h0;
|
202 |
|
|
else if ((bcsctl1[`DIVAx]!=2'b00) & lfxt_clk_en) aclk_div <= aclk_div+3'h1;
|
203 |
|
|
|
204 |
|
|
|
205 |
|
|
// Generate SMCLK
|
206 |
|
|
//----------------------------
|
207 |
|
|
|
208 |
|
|
reg [2:0] smclk_div;
|
209 |
|
|
|
210 |
|
|
wire smclk_in = ~scg1 & (bcsctl2[`SELS] ? lfxt_clk_en : 1'b1);
|
211 |
|
|
|
212 |
|
|
wire smclk_en = smclk_in & ((bcsctl2[`DIVSx]==2'b00) ? 1'b1 :
|
213 |
|
|
(bcsctl2[`DIVSx]==2'b01) ? smclk_div[0] :
|
214 |
|
|
(bcsctl2[`DIVSx]==2'b10) ? &smclk_div[1:0] :
|
215 |
|
|
&smclk_div[2:0]);
|
216 |
|
|
|
217 |
|
|
always @ (posedge mclk or posedge puc)
|
218 |
|
|
if (puc) smclk_div <= 3'h0;
|
219 |
|
|
else if ((bcsctl2[`DIVSx]!=2'b00) & smclk_in) smclk_div <= smclk_div+3'h1;
|
220 |
|
|
|
221 |
|
|
|
222 |
|
|
//=============================================================================
|
223 |
|
|
// 6) RESET GENERATION
|
224 |
|
|
//=============================================================================
|
225 |
|
|
|
226 |
|
|
// Generate synchronized POR
|
227 |
|
|
wire por_reset = !reset_n;
|
228 |
|
|
|
229 |
|
|
reg [1:0] por_s;
|
230 |
|
|
always @(posedge mclk_n or posedge por_reset)
|
231 |
|
|
if (por_reset) por_s <= 2'b11;
|
232 |
|
|
else por_s <= {por_s[0], 1'b0};
|
233 |
|
|
wire por = por_s[1];
|
234 |
|
|
|
235 |
|
|
// Generate main system reset
|
236 |
|
|
wire puc_reset = por_reset | wdt_reset | dbg_reset;
|
237 |
|
|
|
238 |
|
|
reg [1:0] puc_s;
|
239 |
|
|
always @(posedge mclk_n or posedge puc_reset)
|
240 |
|
|
if (puc_reset) puc_s <= 2'b11;
|
241 |
|
|
else puc_s <= {puc_s[0], 1'b0};
|
242 |
|
|
wire puc = puc_s[1];
|
243 |
|
|
|
244 |
|
|
|
245 |
|
|
endmodule // omsp_clock_module
|
246 |
|
|
|
247 |
|
|
`include "openMSP430_undefines.v"
|