OpenCores
URL https://opencores.org/ocsvn/openmsp430/openmsp430/trunk

Subversion Repositories openmsp430

[/] [openmsp430/] [trunk/] [core/] [bench/] [verilog/] [dbg_i2c_tasks.v] - Blame information for rev 183

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 154 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: dbg_i2c_tasks.v
26
// 
27
// *Module Description:
28
//                      openMSP430 debug interface I2C tasks
29
//
30
// *Author(s):
31
//              - Olivier Girard,    olgirard@gmail.com
32
//
33
//----------------------------------------------------------------------------
34
// $Rev: 17 $
35
// $LastChangedBy: olivier.girard $
36
// $LastChangedDate: 2009-08-04 23:15:39 +0200 (Tue, 04 Aug 2009) $
37
//----------------------------------------------------------------------------
38
 
39
//----------------------------------------------------------------------------
40
// I2C COMMUNICATION CONFIGURATION
41
//----------------------------------------------------------------------------
42
 
43
// Data rate
44
parameter I2C_FREQ      = 2000000;
45
integer   I2C_PERIOD    = 1000000000/I2C_FREQ;
46
 
47
// Address
48
parameter I2C_ADDR      = 7'h45;
49
parameter I2C_BROADCAST = 7'h67;
50
 
51
 
52
//----------------------------------------------------------------------------
53
// Generate START / STOP conditions
54
//----------------------------------------------------------------------------
55
task dbg_i2c_start;
56
   begin
57
      dbg_i2c_string          = "Start";
58
      dbg_sda_master_out_pre  = 1'b0;
59
      #(I2C_PERIOD/2);
60
      dbg_scl_master_pre      = 1'b0;
61
      #(I2C_PERIOD/4);
62
      dbg_i2c_string          = "";
63
   end
64
endtask
65
 
66
task dbg_i2c_stop;
67
   begin
68
      dbg_i2c_string          = "Stop";
69
      dbg_sda_master_out_pre  = 1'b0;
70
      #(I2C_PERIOD/4);
71
      dbg_scl_master_pre      = 1'b1;
72
      #(I2C_PERIOD/4);
73
      dbg_sda_master_out_pre  = 1'b1;
74
      #(I2C_PERIOD/2);
75
      dbg_i2c_string          = "";
76
   end
77
endtask
78
 
79
//----------------------------------------------------------------------------
80
// Send a byte on the I2C bus
81
//----------------------------------------------------------------------------
82
task dbg_i2c_send;
83
   input  [7:0] txbuf;
84
 
85
   reg [9:0]     txbuf_full;
86
   integer      txcnt;
87
   begin
88
      #(1);
89
      txbuf_full = txbuf;
90
      for (txcnt = 0; txcnt < 8; txcnt = txcnt + 1)
91
        begin
92
           $sformat(dbg_i2c_string, "TX_%-d", txcnt);
93
           dbg_sda_master_out_pre = txbuf_full[7-txcnt];
94
           #(I2C_PERIOD/4);
95
           dbg_scl_master_pre     = 1'b1;
96
           #(I2C_PERIOD/2);
97
           dbg_scl_master_pre     = 1'b0;
98
           #(I2C_PERIOD/4);
99
        end
100
      dbg_sda_master_out_pre = 1'b1;
101
      dbg_i2c_string         = "";
102
   end
103
endtask
104
 
105
//----------------------------------------------------------------------------
106
// Read ACK / NACK
107
//----------------------------------------------------------------------------
108
task dbg_i2c_ack_rd;
109
   begin
110
      dbg_i2c_string      = "ACK (rd)";
111
      #(I2C_PERIOD/4);
112
      dbg_scl_master_pre  = 1'b1;
113
      #(I2C_PERIOD/2);
114
      dbg_scl_master_pre  = 1'b0;
115
      #(I2C_PERIOD/4);
116
      dbg_i2c_string      = "";
117
   end
118
endtask
119
 
120
 
121
//----------------------------------------------------------------------------
122
// Read a byte from the I2C bus
123
//----------------------------------------------------------------------------
124
task dbg_i2c_receive;
125
   output [7:0] rxbuf;
126
 
127
   reg [9:0]     rxbuf_full;
128
   integer      rxcnt;
129
   begin
130
      #(1);
131
      rxbuf_full = 0;
132
      for (rxcnt = 0; rxcnt < 8; rxcnt = rxcnt + 1)
133
        begin
134
           $sformat(dbg_i2c_string, "RX_%-d", rxcnt);
135
           #(I2C_PERIOD/4);
136
           dbg_scl_master_pre  = 1'b1;
137
           #(I2C_PERIOD/4);
138
           rxbuf_full[7-rxcnt] = dbg_sda;
139
           #(I2C_PERIOD/4);
140
           dbg_scl_master_pre  = 1'b0;
141
           #(I2C_PERIOD/4);
142
        end
143
      dbg_i2c_string      = "";
144
      rxbuf = rxbuf_full;
145
   end
146
endtask
147
 
148
 
149
//----------------------------------------------------------------------------
150
// Write ACK
151
//----------------------------------------------------------------------------
152
task dbg_i2c_ack_wr;
153
   begin
154
      dbg_i2c_string          = "ACK (wr)";
155
      dbg_sda_master_out_pre  = 1'b0;
156
      #(I2C_PERIOD/4);
157
      dbg_scl_master_pre      = 1'b1;
158
      #(I2C_PERIOD/2);
159
      dbg_scl_master_pre      = 1'b0;
160
      #(I2C_PERIOD/4);
161
      dbg_sda_master_out_pre  = 1'b1;
162
      dbg_i2c_string          = "";
163
   end
164
endtask
165
 
166
//----------------------------------------------------------------------------
167
// Write NACK
168
//----------------------------------------------------------------------------
169
task dbg_i2c_nack_wr;
170
   begin
171
      dbg_i2c_string          = "NACK (wr)";
172
      dbg_sda_master_out_pre  = 1'b1;
173
      #(I2C_PERIOD/4);
174
      dbg_scl_master_pre      = 1'b1;
175
      #(I2C_PERIOD/2);
176
      dbg_scl_master_pre      = 1'b0;
177
      #(I2C_PERIOD/4);
178
      dbg_sda_master_out_pre  = 1'b1;
179
      dbg_i2c_string          = "";
180
   end
181
endtask
182
 
183
//----------------------------------------------------------------------------
184
// Start Burst
185
//----------------------------------------------------------------------------
186
task dbg_i2c_burst_start;
187
   input        read;
188
   begin
189
      dbg_i2c_start;                     // START
190
      dbg_i2c_send({I2C_ADDR, read});    // Device Address + Write access
191
      dbg_i2c_ack_rd;
192
   end
193
endtask
194
 
195
 
196
//----------------------------------------------------------------------------
197
// Read 16 bits
198
//----------------------------------------------------------------------------
199
task dbg_i2c_rx16;
200
   input        is_last;
201
 
202
   reg [7:0]     rxbuf_lo;
203
   reg [7:0]     rxbuf_hi;
204
   begin
205
      rxbuf_lo = 8'h00;
206
      rxbuf_hi = 8'h00;
207
 
208
      dbg_i2c_receive(rxbuf_lo);        // Data (low)
209
      dbg_i2c_ack_wr;
210
      dbg_i2c_receive(rxbuf_hi);        // Data (high)
211
      if (is_last)
212
        begin
213
           dbg_i2c_nack_wr;
214
           dbg_i2c_stop;                // STOP
215
        end
216
      else
217
        begin
218
           dbg_i2c_ack_wr;
219
        end
220
 
221
      dbg_i2c_buf = {rxbuf_hi, rxbuf_lo};
222
      end
223
endtask
224
 
225
//----------------------------------------------------------------------------
226
// Transmit 16 bits
227
//----------------------------------------------------------------------------
228
task dbg_i2c_tx16;
229
   input [15:0] dbg_data;
230
   input        is_last;
231
 
232
   begin
233
      dbg_i2c_send(dbg_data[7:0]);  // write LSB
234
      dbg_i2c_ack_rd;
235
      dbg_i2c_send(dbg_data[15:8]); // write MSB
236
      dbg_i2c_ack_rd;
237
      if (is_last)
238
        dbg_i2c_stop;               // STOP CONDITION
239
   end
240
endtask
241
 
242
//----------------------------------------------------------------------------
243
// Read 8 bits
244
//----------------------------------------------------------------------------
245
task dbg_i2c_rx8;
246
   input        is_last;
247
 
248
   reg [7:0]     rxbuf;
249
   begin
250
      rxbuf = 8'h00;
251
 
252
      dbg_i2c_receive(rxbuf);        // Data (low)
253
      if (is_last)
254
        begin
255
           dbg_i2c_nack_wr;
256
           dbg_i2c_stop;             // STOP
257
        end
258
      else
259
        begin
260
           dbg_i2c_ack_wr;
261
        end
262
 
263
      dbg_i2c_buf = {8'h00, rxbuf};
264
      end
265
endtask
266
 
267
//----------------------------------------------------------------------------
268
// Transmit 8 bits
269
//----------------------------------------------------------------------------
270
task dbg_i2c_tx8;
271
   input [7:0] dbg_data;
272
   input       is_last;
273
 
274
   begin
275
      dbg_i2c_send(dbg_data);  // write LSB
276
      dbg_i2c_ack_rd;
277
      if (is_last)
278
        dbg_i2c_stop;          // STOP CONDITION
279
   end
280
endtask
281
 
282
//----------------------------------------------------------------------------
283
// Write to Debug register
284
//----------------------------------------------------------------------------
285
task dbg_i2c_wr;
286
   input  [7:0] dbg_reg;
287
   input [15:0] dbg_data;
288
 
289
   begin
290
      dbg_i2c_start;                     // START
291
      dbg_i2c_tx8({I2C_ADDR, 1'b0}, 0);  // Device Address + Write access
292
      dbg_i2c_tx8(DBG_WR | dbg_reg, 0);  // Command
293
 
294
      if (~dbg_reg[6])
295
        dbg_i2c_tx16(dbg_data,      1);
296
      else
297
        dbg_i2c_tx8 (dbg_data[7:0], 1);
298
 
299
   end
300
endtask
301
 
302
//----------------------------------------------------------------------------
303
// Read Debug register
304
//----------------------------------------------------------------------------
305
task dbg_i2c_rd;
306
   input  [7:0] dbg_reg;
307
 
308
   reg [7:0]     rxbuf_lo;
309
   reg [7:0]     rxbuf_hi;
310
   begin
311
      rxbuf_lo = 8'h00;
312
      rxbuf_hi = 8'h00;
313
 
314
      dbg_i2c_start;                     // START
315
      dbg_i2c_tx8({I2C_ADDR, 1'b0}, 0);  // Device Address + Write access
316
      dbg_i2c_tx8(DBG_RD | dbg_reg, 1);  // Command
317
 
318
      dbg_i2c_start;                     // START
319
      dbg_i2c_tx8({I2C_ADDR, 1'b1}, 0);  // Device Address + Read access
320
 
321
      if (~dbg_reg[6])
322
        dbg_i2c_rx16(1);
323
      else
324
        dbg_i2c_rx8(1);
325
 
326
   end
327
endtask
328
 
329
//----------------------------------------------------------------------------
330
// Build random delay insertion on SCL_MASTER and SDA_MASTER_OUT in order to
331
// simulate synchronization mechanism
332
//----------------------------------------------------------------------------
333
 
334
always @(posedge mclk or posedge dbg_rst)
335
  if (dbg_rst)
336
    begin
337
       dbg_sda_master_out_sel <= 1'b0;
338
       dbg_sda_master_out_dly <= 1'b1;
339
 
340
       dbg_scl_master_sel     <= 1'b0;
341
       dbg_scl_master_dly     <= 1'b1;
342
    end
343
  else if (dbg_en)
344
    begin
345
       dbg_sda_master_out_sel <= dbg_sda_master_out_meta ? $random : 1'b0;
346
       dbg_sda_master_out_dly <= dbg_sda_master_out_pre;
347
 
348
       dbg_scl_master_sel     <= dbg_scl_master_meta     ? $random : 1'b0;
349
       dbg_scl_master_dly     <= dbg_scl_master_pre;
350
    end
351
 
352
assign dbg_sda_master_out = dbg_sda_master_out_sel ? dbg_sda_master_out_dly : dbg_sda_master_out_pre;
353
 
354
assign dbg_scl_master     = dbg_scl_master_sel     ? dbg_scl_master_dly     : dbg_scl_master_pre;
355
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.