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

Subversion Repositories sockit_owm

[/] [sockit_owm/] [trunk/] [HAL/] [src/] [owlnk.c] - Blame information for rev 2

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

Line No. Rev Author Line
1 2 iztok
//---------------------------------------------------------------------------
2
// Copyright (C) 2001 Dallas Semiconductor Corporation, All Rights Reserved.
3
//
4
// Permission is hereby granted, free of charge, to any person obtaining a
5
// copy of this software and associated documentation files (the "Software"),
6
// to deal in the Software without restriction, including without limitation
7
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
// and/or sell copies of the Software, and to permit persons to whom the
9
// Software is furnished to do so, subject to the following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
18
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
// OTHER DEALINGS IN THE SOFTWARE.
21
//
22
// Except as contained in this notice, the name of Dallas Semiconductor
23
// shall not be used except as stated in the Dallas Semiconductor
24
// Branding Policy.
25
//---------------------------------------------------------------------------
26
//
27
//  TODO.C - Link Layer functions required by general 1-Wire drive
28
//           implementation.  Fill in the platform specific code.
29
//
30
//  Version: 3.00
31
//
32
//  History: 1.00 -> 1.01  Added function msDelay.
33
//           1.02 -> 1.03  Added function msGettick.
34
//           1.03 -> 2.00  Changed 'MLan' to 'ow'. Added support for
35
//                         multiple ports.
36
//           2.10 -> 3.00  Added owReadBitPower and owWriteBytePower
37
//
38
 
39
#include "ownet.h"
40
#include "sockit_owm_regs.h"
41
#include "sockit_owm.h"
42
#include <unistd.h>
43
 
44
extern sockit_owm_state sockit_owm;
45
 
46
// exportable link-level functions
47
SMALLINT owTouchReset(int);
48
SMALLINT owTouchBit(int,SMALLINT);
49
SMALLINT owTouchByte(int,SMALLINT);
50
SMALLINT owWriteByte(int,SMALLINT);
51
SMALLINT owReadByte(int);
52
SMALLINT owSpeed(int,SMALLINT);
53
SMALLINT owLevel(int,SMALLINT);
54
SMALLINT owProgramPulse(int);
55
void msDelay(int);
56
long msGettick(void);
57
SMALLINT owWriteBytePower(int,SMALLINT);
58
SMALLINT owReadBytePower(int);
59
SMALLINT owReadBitPower(int,SMALLINT);
60
SMALLINT owHasPowerDelivery(int);
61
SMALLINT owHasOverDrive(int);
62
SMALLINT owHasProgramPulse(int);
63
 
64
//--------------------------------------------------------------------------
65
// Reset all of the devices on the 1-Wire Net and return the result.
66
//
67
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
68
//                indicate the symbolic port number.
69
//
70
// Returns: TRUE(1):  presence pulse(s) detected, device(s) reset
71
//          FALSE(0): no presence pulses detected
72
//
73
SMALLINT owTouchReset(int portnum)
74
{
75
   int reg;
76
   int ovd = (sockit_owm.ovd >> portnum) & 0x1;
77
 
78
   // lock transfer
79
   ALT_SEM_PEND (sockit_owm.trn, 0);
80
 
81
   // write RST
82
   IOWR_SOCKIT_OWM (sockit_owm.base, (sockit_owm.pwr << SOCKIT_OWM_POWER_OFST)
83
                                   | (portnum        << SOCKIT_OWM_SEL_OFST)
84
                                   | (sockit_owm.ena << SOCKIT_OWM_ETX_OFST)
85
                                   | (ovd            << SOCKIT_OWM_OVD_OFST)
86
                                   |                    SOCKIT_OWM_RST_MSK);
87
 
88
   // wait for irq to set the transfer end flag
89
   ALT_FLAG_PEND (sockit_owm.irq, 0x1, OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME, 0);
90
   // wait for STX (end of transfer cycle) and read the presence status
91
   while ((reg = IORD_SOCKIT_OWM (sockit_owm.base)) & SOCKIT_OWM_TRN_MSK);
92
 
93
   // release transfer lock
94
   ALT_SEM_POST (sockit_owm.trn);
95
 
96
   // return DRX (presence detect)
97
   return (~reg >> SOCKIT_OWM_DAT_OFST) & 0x1;
98
}
99
 
100
//--------------------------------------------------------------------------
101
// Send 1 bit of communication to the 1-Wire Net and return the
102
// result 1 bit read from the 1-Wire Net.  The parameter 'sendbit'
103
// least significant bit is used and the least significant bit
104
// of the result is the return bit.
105
//
106
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
107
//                indicate the symbolic port number.
108
// 'sendbit'    - the least significant bit is the bit to send
109
//
110
// Returns: 0:   0 bit read from sendbit
111
//          1:   1 bit read from sendbit
112
//
113
SMALLINT owTouchBit(int portnum, SMALLINT sendbit)
114
{
115
   int reg;
116
   int ovd = (sockit_owm.ovd >> portnum) & 0x1;
117
 
118
   // lock transfer
119
   ALT_SEM_PEND (sockit_owm.trn, 0);
120
 
121
   // write RST
122
   IOWR_SOCKIT_OWM (sockit_owm.base, (sockit_owm.pwr  << SOCKIT_OWM_POWER_OFST)
123
                                   | (portnum         << SOCKIT_OWM_SEL_OFST)
124
                                   | (sockit_owm.ena  << SOCKIT_OWM_ETX_OFST)
125
                                   | (ovd             << SOCKIT_OWM_OVD_OFST)
126
                                   | ((sendbit & 0x1) << SOCKIT_OWM_DAT_OFST));
127
 
128
   // wait for irq to set the transfer end flag
129
   ALT_FLAG_PEND (sockit_owm.irq, 0x1, OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME, 0);
130
   // wait for STX (end of transfer cycle) and read the read data bit
131
   while ((reg = IORD_SOCKIT_OWM (sockit_owm.base)) & SOCKIT_OWM_TRN_MSK);
132
 
133
   // release transfer lock
134
   ALT_SEM_POST (sockit_owm.trn);
135
 
136
   // return DRX (read bit)
137
   return (reg >> SOCKIT_OWM_DAT_OFST) & 0x1;
138
}
139
 
140
//--------------------------------------------------------------------------
141
// Send 8 bits of communication to the 1-Wire Net and return the
142
// result 8 bits read from the 1-Wire Net.  The parameter 'sendbyte'
143
// least significant 8 bits are used and the least significant 8 bits
144
// of the result is the return byte.
145
//
146
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
147
//                indicate the symbolic port number.
148
// 'sendbyte'   - 8 bits to send (least significant byte)
149
//
150
// Returns:  8 bytes read from sendbyte
151
//
152
SMALLINT owTouchByte(int portnum, SMALLINT sendbyte)
153
{
154
   int i;
155
   SMALLINT dat = 0;
156
   for (i=0; i<8; i++)
157
   {
158
      dat |= owTouchBit(portnum,sendbyte & 0x1) << i;
159
      sendbyte >>= 1;
160
   }
161
   return dat;
162
}
163
 
164
//--------------------------------------------------------------------------
165
// Send 8 bits of communication to the 1-Wire Net and verify that the
166
// 8 bits read from the 1-Wire Net is the same (write operation).
167
// The parameter 'sendbyte' least significant 8 bits are used.
168
//
169
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
170
//                indicate the symbolic port number.
171
// 'sendbyte'   - 8 bits to send (least significant byte)
172
//
173
// Returns:  TRUE: bytes written and echo was the same
174
//           FALSE: echo was not the same
175
//
176
SMALLINT owWriteByte(int portnum, SMALLINT sendbyte)
177
{
178
   return (owTouchByte(portnum,sendbyte) == sendbyte) ? TRUE : FALSE;
179
}
180
 
181
//--------------------------------------------------------------------------
182
// Send 8 bits of read communication to the 1-Wire Net and and return the
183
// result 8 bits read from the 1-Wire Net.
184
//
185
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
186
//                indicate the symbolic port number.
187
//
188
// Returns:  8 bytes read from 1-Wire Net
189
//
190
SMALLINT owReadByte(int portnum)
191
{
192
   return owTouchByte(portnum,0xFF);
193
}
194
 
195
//--------------------------------------------------------------------------
196
// Set the 1-Wire Net communication speed.
197
//
198
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
199
//                indicate the symbolic port number.
200
// 'new_speed'  - new speed defined as
201
//                MODE_NORMAL     0x00
202
//                MODE_OVERDRIVE  0x01
203
//
204
// Returns:  current 1-Wire Net speed
205
//
206
SMALLINT owSpeed(int portnum, SMALLINT new_speed)
207
{
208
   if (new_speed == MODE_OVERDRIVE)  sockit_owm.ovd |=  (1 << portnum);
209
   if (new_speed == MODE_NORMAL   )  sockit_owm.ovd &= ~(1 << portnum);
210
   // return the current port state
211
   return ((sockit_owm.ovd >> portnum) & 0x1) ? MODE_OVERDRIVE : MODE_NORMAL;
212
}
213
 
214
//--------------------------------------------------------------------------
215
// Set the 1-Wire Net line level.  The values for NewLevel are
216
// as follows:
217
//
218
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
219
//                indicate the symbolic port number.
220
// 'new_level'  - new level defined as
221
//                MODE_NORMAL     0x00
222
//                MODE_STRONG5    0x02
223
//                MODE_PROGRAM    0x04
224
//                MODE_BREAK      0x08
225
//
226
// Returns:  current 1-Wire Net level
227
//
228
SMALLINT owLevel(int portnum, SMALLINT new_level)
229
{
230
   if (new_level == MODE_STRONG5) {
231
      // set the power bit
232
      sockit_owm.pwr |=  (1 << portnum);
233
      IOWR_SOCKIT_OWM (sockit_owm.base, (sockit_owm.pwr << SOCKIT_OWM_POWER_OFST)
234
                                      |                    SOCKIT_OWM_PWR_MSK
235
                                      |                    SOCKIT_OWM_IDL_MSK);
236
   }
237
   if (new_level == MODE_NORMAL) {
238
      // clear the power bit
239
      sockit_owm.pwr &= ~(1 << portnum);
240
      IOWR_SOCKIT_OWM (sockit_owm.base, (sockit_owm.pwr << SOCKIT_OWM_POWER_OFST)
241
                                      |                    SOCKIT_OWM_IDL_MSK);
242
   }
243
   // return the current port state
244
   return ((sockit_owm.pwr >> portnum) & 0x1) ? MODE_STRONG5 : MODE_NORMAL;
245
}
246
 
247
//--------------------------------------------------------------------------
248
// This procedure creates a fixed 480 microseconds 12 volt pulse
249
// on the 1-Wire Net for programming EPROM iButtons.
250
//
251
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
252
//                indicate the symbolic port number.
253
//
254
// Returns:  TRUE  successful
255
//           FALSE program voltage not available
256
//
257
SMALLINT owProgramPulse(int portnum)
258
{
259
   return owHasProgramPulse(portnum);
260
}
261
 
262
//--------------------------------------------------------------------------
263
//  Description:
264
//     Delay for at least 'len' ms
265
//
266
void msDelay(int len)
267
{
268
#if SOCKIT_OWM_HW_DLY
269
   int i;
270
 
271
   // lock transfer
272
   ALT_SEM_PEND (sockit_owm.trn, 0);
273
 
274
   for (i=0; i<len; i++) {
275
      // create a 960us pause
276
      IOWR_SOCKIT_OWM (sockit_owm.base, ( sockit_owm.pwr        << SOCKIT_OWM_POWER_OFST)
277
                                      | ( sockit_owm.ena        << SOCKIT_OWM_ETX_OFST)
278
                                      | ((sockit_owm.pwr & 0x1) << SOCKIT_OWM_PWR_OFST)
279
                                      |                            SOCKIT_OWM_DLY_MSK);
280
 
281
     // wait for irq to set the transfer end flag
282
     ALT_FLAG_PEND (sockit_owm.irq, 0x1, OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME, 0);
283
     // wait for STX (end of transfer cycle)
284
     while (IORD_SOCKIT_OWM (sockit_owm.base) & SOCKIT_OWM_TRN_MSK);
285
 
286
     // release transfer lock
287
     ALT_SEM_POST (sockit_owm.trn);
288
   }
289
#else
290
#ifdef UCOS_II
291
   // uCOS-II timed delay
292
   OSTimeDlyHMSM(0,0,0,len);
293
#else
294
   // Altera HAL us delay
295
   usleep (1000*len);
296
#endif
297
#endif
298
}
299
 
300
//--------------------------------------------------------------------------
301
// Get the current millisecond tick count.  Does not have to represent
302
// an actual time, it just needs to be an incrementing timer.
303
//
304
long msGettick(void)
305
{
306
#ifdef UCOS_II
307
   // uCOS-II tick counter
308
        OSTimeGet();
309
#else
310
   // TODO add platform specific code here
311
   return 0;
312
#endif
313
}
314
 
315
//--------------------------------------------------------------------------
316
// Send 8 bits of communication to the 1-Wire Net and verify that the
317
// 8 bits read from the 1-Wire Net is the same (write operation).
318
// The parameter 'sendbyte' least significant 8 bits are used.  After the
319
// 8 bits are sent change the level of the 1-Wire net.
320
//
321
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
322
//              OpenCOM to indicate the port number.
323
// 'sendbyte' - 8 bits to send (least significant byte)
324
//
325
// Returns:  TRUE: bytes written and echo was the same
326
//           FALSE: echo was not the same
327
//
328
SMALLINT owWriteBytePower(int portnum, SMALLINT sendbyte)
329
{
330
   if (!owHasPowerDelivery(portnum))
331
      return FALSE;
332
 
333
   if(owTouchByte(portnum,sendbyte) != sendbyte)
334
      return FALSE;
335
 
336
   if(owLevel(portnum,MODE_STRONG5) != MODE_STRONG5)
337
      return FALSE;
338
 
339
   return TRUE;
340
}
341
 
342
//--------------------------------------------------------------------------
343
// Send 1 bit of communication to the 1-Wire Net and verify that the
344
// response matches the 'applyPowerResponse' bit and apply power delivery
345
// to the 1-Wire net.  Note that some implementations may apply the power
346
// first and then turn it off if the response is incorrect.
347
//
348
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
349
//              OpenCOM to indicate the port number.
350
// 'applyPowerResponse' - 1 bit response to check, if correct then start
351
//                        power delivery
352
//
353
// Returns:  TRUE: bit written and response correct, strong pullup now on
354
//           FALSE: response incorrect
355
//
356
SMALLINT owReadBitPower(int portnum, SMALLINT applyPowerResponse)
357
{
358
   if (!owHasPowerDelivery(portnum))
359
      return FALSE;
360
 
361
   if(owTouchBit(portnum,0x01) != applyPowerResponse)
362
      return FALSE;
363
 
364
   if(owLevel(portnum,MODE_STRONG5) != MODE_STRONG5)
365
      return FALSE;
366
 
367
   return TRUE;
368
}
369
 
370
//--------------------------------------------------------------------------
371
// Read 8 bits of communication to the 1-Wire Net and then turn on
372
// power delivery.
373
//
374
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
375
//              OpenCOM to indicate the port number.
376
//
377
// Returns:  byte read
378
//           FALSE: power delivery failed
379
//
380
SMALLINT owReadBytePower(int portnum)
381
{
382
   SMALLINT getbyte;
383
 
384
   if (!owHasPowerDelivery(portnum))
385
      return FALSE;
386
 
387
   getbyte = owTouchByte(portnum,0xFF);
388
 
389
   if (owLevel(portnum,MODE_STRONG5) != MODE_STRONG5)
390
      return FALSE;
391
 
392
   return getbyte;
393
}
394
 
395
//--------------------------------------------------------------------------
396
// This procedure indicates whether the adapter can deliver power.
397
//
398
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
399
//              OpenCOM to indicate the port number.
400
//
401
// Returns:  TRUE  if adapter is capable of delivering power.
402
//
403
SMALLINT owHasPowerDelivery(int portnum)
404
{
405
   return TRUE;
406
}
407
 
408
//--------------------------------------------------------------------------
409
// This procedure indicates whether the adapter can deliver power.
410
//
411
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
412
//              OpenCOM to indicate the port number.
413
//
414
// Returns:  TRUE  if adapter is capable of over drive.
415
//
416
SMALLINT owHasOverDrive(int portnum)
417
{
418
   return sockit_owm.ovd_e;
419
}
420
 
421
//--------------------------------------------------------------------------
422
// This procedure creates a fixed 480 microseconds 12 volt pulse
423
// on the 1-Wire Net for programming EPROM iButtons.
424
//
425
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
426
//              OpenCOM to indicate the port number.
427
//
428
// Returns:  TRUE  program voltage available
429
//           FALSE program voltage not available
430
SMALLINT owHasProgramPulse(int portnum)
431
{
432
   return FALSE;
433
}

powered by: WebSVN 2.1.0

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