| 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 |
111 |
olivier.gi |
// $Rev: 103 $
|
| 39 |
80 |
olivier.gi |
// $LastChangedBy: olivier.girard $
|
| 40 |
111 |
olivier.gi |
// $LastChangedDate: 2011-03-05 15:44:48 +0100 (Sat, 05 Mar 2011) $
|
| 41 |
80 |
olivier.gi |
//----------------------------------------------------------------------------
|
| 42 |
104 |
olivier.gi |
`ifdef OMSP_NO_INCLUDE
|
| 43 |
|
|
`else
|
| 44 |
80 |
olivier.gi |
`include "openMSP430_defines.v"
|
| 45 |
104 |
olivier.gi |
`endif
|
| 46 |
80 |
olivier.gi |
|
| 47 |
|
|
module omsp_clock_module (
|
| 48 |
|
|
|
| 49 |
|
|
// OUTPUTs
|
| 50 |
|
|
aclk_en, // ACLK enable
|
| 51 |
107 |
olivier.gi |
cpu_en_s, // Enable CPU code execution (synchronous)
|
| 52 |
|
|
dbg_clk, // Debug unit clock
|
| 53 |
|
|
dbg_en_s, // Debug interface enable (synchronous)
|
| 54 |
|
|
dbg_rst, // Debug unit reset
|
| 55 |
80 |
olivier.gi |
mclk, // Main system clock
|
| 56 |
|
|
per_dout, // Peripheral data output
|
| 57 |
|
|
por, // Power-on reset
|
| 58 |
111 |
olivier.gi |
puc_rst, // Main system reset
|
| 59 |
80 |
olivier.gi |
smclk_en, // SMCLK enable
|
| 60 |
|
|
|
| 61 |
|
|
// INPUTs
|
| 62 |
107 |
olivier.gi |
cpu_en, // Enable CPU code execution (asynchronous)
|
| 63 |
|
|
dbg_cpu_reset, // Reset CPU from debug interface
|
| 64 |
|
|
dbg_en, // Debug interface enable (asynchronous)
|
| 65 |
80 |
olivier.gi |
dco_clk, // Fast oscillator (fast clock)
|
| 66 |
|
|
lfxt_clk, // Low frequency oscillator (typ 32kHz)
|
| 67 |
|
|
oscoff, // Turns off LFXT1 clock input
|
| 68 |
|
|
per_addr, // Peripheral address
|
| 69 |
|
|
per_din, // Peripheral data input
|
| 70 |
|
|
per_en, // Peripheral enable (high active)
|
| 71 |
107 |
olivier.gi |
per_we, // Peripheral write enable (high active)
|
| 72 |
|
|
reset_n, // Reset Pin (low active, asynchronous)
|
| 73 |
80 |
olivier.gi |
scg1, // System clock generator 1. Turns off the SMCLK
|
| 74 |
|
|
wdt_reset // Watchdog-timer reset
|
| 75 |
|
|
);
|
| 76 |
|
|
|
| 77 |
|
|
// OUTPUTs
|
| 78 |
|
|
//=========
|
| 79 |
|
|
output aclk_en; // ACLK enable
|
| 80 |
107 |
olivier.gi |
output cpu_en_s; // Enable CPU code execution (synchronous)
|
| 81 |
|
|
output dbg_clk; // Debug unit clock
|
| 82 |
|
|
output dbg_en_s; // Debug unit enable (synchronous)
|
| 83 |
|
|
output dbg_rst; // Debug unit reset
|
| 84 |
80 |
olivier.gi |
output mclk; // Main system clock
|
| 85 |
|
|
output [15:0] per_dout; // Peripheral data output
|
| 86 |
|
|
output por; // Power-on reset
|
| 87 |
111 |
olivier.gi |
output puc_rst; // Main system reset
|
| 88 |
80 |
olivier.gi |
output smclk_en; // SMCLK enable
|
| 89 |
|
|
|
| 90 |
|
|
// INPUTs
|
| 91 |
|
|
//=========
|
| 92 |
107 |
olivier.gi |
input cpu_en; // Enable CPU code execution (asynchronous)
|
| 93 |
|
|
input dbg_cpu_reset;// Reset CPU from debug interface
|
| 94 |
|
|
input dbg_en; // Debug interface enable (asynchronous)
|
| 95 |
80 |
olivier.gi |
input dco_clk; // Fast oscillator (fast clock)
|
| 96 |
|
|
input lfxt_clk; // Low frequency oscillator (typ 32kHz)
|
| 97 |
|
|
input oscoff; // Turns off LFXT1 clock input
|
| 98 |
111 |
olivier.gi |
input [13:0] per_addr; // Peripheral address
|
| 99 |
80 |
olivier.gi |
input [15:0] per_din; // Peripheral data input
|
| 100 |
|
|
input per_en; // Peripheral enable (high active)
|
| 101 |
107 |
olivier.gi |
input [1:0] per_we; // Peripheral write enable (high active)
|
| 102 |
|
|
input reset_n; // Reset Pin (low active, asynchronous)
|
| 103 |
80 |
olivier.gi |
input scg1; // System clock generator 1. Turns off the SMCLK
|
| 104 |
|
|
input wdt_reset; // Watchdog-timer reset
|
| 105 |
|
|
|
| 106 |
|
|
|
| 107 |
|
|
//=============================================================================
|
| 108 |
|
|
// 1) PARAMETER DECLARATION
|
| 109 |
|
|
//=============================================================================
|
| 110 |
|
|
|
| 111 |
111 |
olivier.gi |
// Register base address (must be aligned to decoder bit width)
|
| 112 |
|
|
parameter [14:0] BASE_ADDR = 15'h0050;
|
| 113 |
80 |
olivier.gi |
|
| 114 |
111 |
olivier.gi |
// Decoder bit width (defines how many bits are considered for address decoding)
|
| 115 |
|
|
parameter DEC_WD = 4;
|
| 116 |
|
|
|
| 117 |
|
|
// Register addresses offset
|
| 118 |
|
|
parameter [DEC_WD-1:0] BCSCTL1 = 'h7,
|
| 119 |
|
|
BCSCTL2 = 'h8;
|
| 120 |
|
|
|
| 121 |
|
|
// Register one-hot decoder utilities
|
| 122 |
|
|
parameter DEC_SZ = 2**DEC_WD;
|
| 123 |
|
|
parameter [DEC_SZ-1:0] BASE_REG = {{DEC_SZ-1{1'b0}}, 1'b1};
|
| 124 |
|
|
|
| 125 |
80 |
olivier.gi |
// Register one-hot decoder
|
| 126 |
111 |
olivier.gi |
parameter [DEC_SZ-1:0] BCSCTL1_D = (BASE_REG << BCSCTL1),
|
| 127 |
|
|
BCSCTL2_D = (BASE_REG << BCSCTL2);
|
| 128 |
80 |
olivier.gi |
|
| 129 |
|
|
|
| 130 |
|
|
//============================================================================
|
| 131 |
|
|
// 2) REGISTER DECODER
|
| 132 |
|
|
//============================================================================
|
| 133 |
|
|
|
| 134 |
111 |
olivier.gi |
// Local register selection
|
| 135 |
|
|
wire reg_sel = per_en & (per_addr[13:DEC_WD-1]==BASE_ADDR[14:DEC_WD]);
|
| 136 |
|
|
|
| 137 |
|
|
// Register local address
|
| 138 |
|
|
wire [DEC_WD-1:0] reg_addr = {1'b0, per_addr[DEC_WD-2:0]};
|
| 139 |
|
|
|
| 140 |
80 |
olivier.gi |
// Register address decode
|
| 141 |
111 |
olivier.gi |
wire [DEC_SZ-1:0] reg_dec = (BCSCTL1_D & {DEC_SZ{(reg_addr==(BCSCTL1 >>1))}}) |
|
| 142 |
|
|
(BCSCTL2_D & {DEC_SZ{(reg_addr==(BCSCTL2 >>1))}});
|
| 143 |
80 |
olivier.gi |
|
| 144 |
|
|
// Read/Write probes
|
| 145 |
111 |
olivier.gi |
wire reg_lo_write = per_we[0] & reg_sel;
|
| 146 |
|
|
wire reg_hi_write = per_we[1] & reg_sel;
|
| 147 |
|
|
wire reg_read = ~|per_we & reg_sel;
|
| 148 |
80 |
olivier.gi |
|
| 149 |
|
|
// Read/Write vectors
|
| 150 |
111 |
olivier.gi |
wire [DEC_SZ-1:0] reg_hi_wr = reg_dec & {DEC_SZ{reg_hi_write}};
|
| 151 |
|
|
wire [DEC_SZ-1:0] reg_lo_wr = reg_dec & {DEC_SZ{reg_lo_write}};
|
| 152 |
|
|
wire [DEC_SZ-1:0] reg_rd = reg_dec & {DEC_SZ{reg_read}};
|
| 153 |
80 |
olivier.gi |
|
| 154 |
|
|
|
| 155 |
|
|
//============================================================================
|
| 156 |
|
|
// 3) REGISTERS
|
| 157 |
|
|
//============================================================================
|
| 158 |
|
|
|
| 159 |
|
|
// BCSCTL1 Register
|
| 160 |
|
|
//--------------
|
| 161 |
|
|
reg [7:0] bcsctl1;
|
| 162 |
111 |
olivier.gi |
wire bcsctl1_wr = BCSCTL1[0] ? reg_hi_wr[BCSCTL1] : reg_lo_wr[BCSCTL1];
|
| 163 |
|
|
wire [7:0] bcsctl1_nxt = BCSCTL1[0] ? per_din[15:8] : per_din[7:0];
|
| 164 |
80 |
olivier.gi |
|
| 165 |
111 |
olivier.gi |
always @ (posedge mclk or posedge puc_rst)
|
| 166 |
|
|
if (puc_rst) bcsctl1 <= 8'h00;
|
| 167 |
80 |
olivier.gi |
else if (bcsctl1_wr) bcsctl1 <= bcsctl1_nxt & 8'h30; // Mask unused bits
|
| 168 |
|
|
|
| 169 |
|
|
|
| 170 |
|
|
// BCSCTL2 Register
|
| 171 |
|
|
//--------------
|
| 172 |
|
|
reg [7:0] bcsctl2;
|
| 173 |
111 |
olivier.gi |
wire bcsctl2_wr = BCSCTL2[0] ? reg_hi_wr[BCSCTL2] : reg_lo_wr[BCSCTL2];
|
| 174 |
|
|
wire [7:0] bcsctl2_nxt = BCSCTL2[0] ? per_din[15:8] : per_din[7:0];
|
| 175 |
80 |
olivier.gi |
|
| 176 |
111 |
olivier.gi |
always @ (posedge mclk or posedge puc_rst)
|
| 177 |
|
|
if (puc_rst) bcsctl2 <= 8'h00;
|
| 178 |
80 |
olivier.gi |
else if (bcsctl2_wr) bcsctl2 <= bcsctl2_nxt & 8'h0e; // Mask unused bits
|
| 179 |
|
|
|
| 180 |
|
|
|
| 181 |
|
|
//============================================================================
|
| 182 |
|
|
// 4) DATA OUTPUT GENERATION
|
| 183 |
|
|
//============================================================================
|
| 184 |
|
|
|
| 185 |
|
|
// Data output mux
|
| 186 |
111 |
olivier.gi |
wire [15:0] bcsctl1_rd = {8'h00, (bcsctl1 & {8{reg_rd[BCSCTL1]}})} << (8 & {4{BCSCTL1[0]}});
|
| 187 |
|
|
wire [15:0] bcsctl2_rd = {8'h00, (bcsctl2 & {8{reg_rd[BCSCTL2]}})} << (8 & {4{BCSCTL2[0]}});
|
| 188 |
80 |
olivier.gi |
|
| 189 |
|
|
wire [15:0] per_dout = bcsctl1_rd |
|
| 190 |
|
|
bcsctl2_rd;
|
| 191 |
|
|
|
| 192 |
|
|
|
| 193 |
|
|
//=============================================================================
|
| 194 |
|
|
// 5) CLOCK GENERATION
|
| 195 |
|
|
//=============================================================================
|
| 196 |
|
|
|
| 197 |
107 |
olivier.gi |
// Synchronize CPU_EN signal
|
| 198 |
|
|
//---------------------------------------
|
| 199 |
111 |
olivier.gi |
`ifdef SYNC_CPU_EN
|
| 200 |
|
|
omsp_sync_cell sync_cell_cpu_en (
|
| 201 |
|
|
.data_out (cpu_en_s),
|
| 202 |
|
|
.clk (mclk),
|
| 203 |
|
|
.data_in (cpu_en),
|
| 204 |
|
|
.rst (por)
|
| 205 |
|
|
);
|
| 206 |
|
|
`else
|
| 207 |
|
|
assign cpu_en_s = cpu_en;
|
| 208 |
|
|
`endif
|
| 209 |
107 |
olivier.gi |
|
| 210 |
80 |
olivier.gi |
// Synchronize LFXT_CLK & edge detection
|
| 211 |
|
|
//---------------------------------------
|
| 212 |
111 |
olivier.gi |
wire lfxt_clk_s;
|
| 213 |
|
|
|
| 214 |
|
|
omsp_sync_cell sync_cell_lfxt_clk (
|
| 215 |
|
|
.data_out (lfxt_clk_s),
|
| 216 |
|
|
.clk (mclk),
|
| 217 |
|
|
.data_in (lfxt_clk),
|
| 218 |
|
|
.rst (por)
|
| 219 |
|
|
);
|
| 220 |
|
|
|
| 221 |
|
|
reg lfxt_clk_dly;
|
| 222 |
80 |
olivier.gi |
|
| 223 |
107 |
olivier.gi |
always @ (posedge mclk or posedge por)
|
| 224 |
111 |
olivier.gi |
if (por) lfxt_clk_dly <= 1'b0;
|
| 225 |
|
|
else lfxt_clk_dly <= lfxt_clk_s;
|
| 226 |
80 |
olivier.gi |
|
| 227 |
111 |
olivier.gi |
wire lfxt_clk_en = (lfxt_clk_s & ~lfxt_clk_dly) & ~(oscoff & ~bcsctl2[`SELS]);
|
| 228 |
80 |
olivier.gi |
|
| 229 |
|
|
|
| 230 |
|
|
// Generate main system clock
|
| 231 |
|
|
//----------------------------
|
| 232 |
|
|
|
| 233 |
|
|
wire mclk = dco_clk;
|
| 234 |
|
|
wire mclk_n = !dco_clk;
|
| 235 |
|
|
|
| 236 |
|
|
|
| 237 |
|
|
// Generate ACLK
|
| 238 |
|
|
//----------------------------
|
| 239 |
|
|
|
| 240 |
85 |
olivier.gi |
reg aclk_en;
|
| 241 |
80 |
olivier.gi |
reg [2:0] aclk_div;
|
| 242 |
|
|
|
| 243 |
85 |
olivier.gi |
wire aclk_en_nxt = lfxt_clk_en & ((bcsctl1[`DIVAx]==2'b00) ? 1'b1 :
|
| 244 |
|
|
(bcsctl1[`DIVAx]==2'b01) ? aclk_div[0] :
|
| 245 |
|
|
(bcsctl1[`DIVAx]==2'b10) ? &aclk_div[1:0] :
|
| 246 |
|
|
&aclk_div[2:0]);
|
| 247 |
|
|
|
| 248 |
111 |
olivier.gi |
always @ (posedge mclk or posedge puc_rst)
|
| 249 |
|
|
if (puc_rst) aclk_en <= 1'b0;
|
| 250 |
|
|
else aclk_en <= aclk_en_nxt & cpu_en_s;
|
| 251 |
85 |
olivier.gi |
|
| 252 |
111 |
olivier.gi |
always @ (posedge mclk or posedge puc_rst)
|
| 253 |
|
|
if (puc_rst) aclk_div <= 3'h0;
|
| 254 |
80 |
olivier.gi |
else if ((bcsctl1[`DIVAx]!=2'b00) & lfxt_clk_en) aclk_div <= aclk_div+3'h1;
|
| 255 |
|
|
|
| 256 |
85 |
olivier.gi |
|
| 257 |
80 |
olivier.gi |
// Generate SMCLK
|
| 258 |
|
|
//----------------------------
|
| 259 |
|
|
|
| 260 |
85 |
olivier.gi |
reg smclk_en;
|
| 261 |
80 |
olivier.gi |
reg [2:0] smclk_div;
|
| 262 |
|
|
|
| 263 |
85 |
olivier.gi |
wire smclk_in = ~scg1 & (bcsctl2[`SELS] ? lfxt_clk_en : 1'b1);
|
| 264 |
80 |
olivier.gi |
|
| 265 |
85 |
olivier.gi |
wire smclk_en_nxt = smclk_in & ((bcsctl2[`DIVSx]==2'b00) ? 1'b1 :
|
| 266 |
|
|
(bcsctl2[`DIVSx]==2'b01) ? smclk_div[0] :
|
| 267 |
|
|
(bcsctl2[`DIVSx]==2'b10) ? &smclk_div[1:0] :
|
| 268 |
|
|
&smclk_div[2:0]);
|
| 269 |
80 |
olivier.gi |
|
| 270 |
111 |
olivier.gi |
always @ (posedge mclk or posedge puc_rst)
|
| 271 |
|
|
if (puc_rst) smclk_en <= 1'b0;
|
| 272 |
|
|
else smclk_en <= smclk_en_nxt & cpu_en_s;
|
| 273 |
85 |
olivier.gi |
|
| 274 |
111 |
olivier.gi |
always @ (posedge mclk or posedge puc_rst)
|
| 275 |
|
|
if (puc_rst) smclk_div <= 3'h0;
|
| 276 |
80 |
olivier.gi |
else if ((bcsctl2[`DIVSx]!=2'b00) & smclk_in) smclk_div <= smclk_div+3'h1;
|
| 277 |
|
|
|
| 278 |
|
|
|
| 279 |
107 |
olivier.gi |
// Generate DBG_CLK
|
| 280 |
|
|
//----------------------------
|
| 281 |
|
|
|
| 282 |
|
|
assign dbg_clk = mclk;
|
| 283 |
|
|
|
| 284 |
|
|
|
| 285 |
80 |
olivier.gi |
//=============================================================================
|
| 286 |
|
|
// 6) RESET GENERATION
|
| 287 |
|
|
//=============================================================================
|
| 288 |
|
|
|
| 289 |
|
|
// Generate synchronized POR
|
| 290 |
111 |
olivier.gi |
wire por_n;
|
| 291 |
107 |
olivier.gi |
wire por_reset_a = !reset_n;
|
| 292 |
80 |
olivier.gi |
|
| 293 |
111 |
olivier.gi |
omsp_sync_cell sync_cell_por (
|
| 294 |
|
|
.data_out (por_n),
|
| 295 |
|
|
.clk (mclk),
|
| 296 |
|
|
.data_in (1'b1),
|
| 297 |
|
|
.rst (por_reset_a)
|
| 298 |
|
|
);
|
| 299 |
80 |
olivier.gi |
|
| 300 |
111 |
olivier.gi |
wire por = ~por_n;
|
| 301 |
107 |
olivier.gi |
|
| 302 |
111 |
olivier.gi |
|
| 303 |
80 |
olivier.gi |
// Generate main system reset
|
| 304 |
111 |
olivier.gi |
wire puc_rst_comb = por | wdt_reset | dbg_cpu_reset;
|
| 305 |
|
|
reg puc_rst;
|
| 306 |
|
|
always @(posedge mclk or posedge puc_rst_comb)
|
| 307 |
|
|
if (puc_rst_comb) puc_rst <= 1'b1;
|
| 308 |
|
|
else puc_rst <= 1'b0;
|
| 309 |
80 |
olivier.gi |
|
| 310 |
|
|
|
| 311 |
107 |
olivier.gi |
// Generate debug unit reset
|
| 312 |
111 |
olivier.gi |
`ifdef DBG_EN
|
| 313 |
|
|
wire dbg_rst_n;
|
| 314 |
107 |
olivier.gi |
|
| 315 |
111 |
olivier.gi |
`ifdef SYNC_DBG_EN
|
| 316 |
|
|
omsp_sync_cell sync_cell_dbg_en (
|
| 317 |
|
|
.data_out (dbg_rst_n),
|
| 318 |
|
|
.clk (mclk),
|
| 319 |
|
|
.data_in (dbg_en),
|
| 320 |
|
|
.rst (por)
|
| 321 |
|
|
);
|
| 322 |
|
|
`else
|
| 323 |
|
|
assign dbg_rst_n = dbg_en;
|
| 324 |
|
|
`endif
|
| 325 |
|
|
|
| 326 |
107 |
olivier.gi |
`else
|
| 327 |
111 |
olivier.gi |
wire dbg_rst_n = 1'b0;
|
| 328 |
107 |
olivier.gi |
`endif
|
| 329 |
|
|
|
| 330 |
111 |
olivier.gi |
wire dbg_en_s = dbg_rst_n;
|
| 331 |
|
|
wire dbg_rst = ~dbg_rst_n;
|
| 332 |
107 |
olivier.gi |
|
| 333 |
|
|
|
| 334 |
80 |
olivier.gi |
endmodule // omsp_clock_module
|
| 335 |
|
|
|
| 336 |
104 |
olivier.gi |
`ifdef OMSP_NO_INCLUDE
|
| 337 |
|
|
`else
|
| 338 |
80 |
olivier.gi |
`include "openMSP430_undefines.v"
|
| 339 |
104 |
olivier.gi |
`endif
|