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 5

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 3 iztok
   ALT_SEM_PEND (sockit_owm.cyc, 0);
80 2 iztok
 
81 3 iztok
   // reset pulse
82
   IOWR_SOCKIT_OWM_CTL (sockit_owm.base, (sockit_owm.pwr << SOCKIT_OWM_CTL_POWER_OFST    )
83
                                       | (portnum        << SOCKIT_OWM_CTL_SEL_OFST      )
84
                                       | (sockit_owm.ien  ? SOCKIT_OWM_CTL_IEN_MSK : 0x00)
85
                                       | (                  SOCKIT_OWM_CTL_CYC_MSK       )
86
                                       | (ovd             ? SOCKIT_OWM_CTL_OVD_MSK : 0x00)
87
                                       | (                  SOCKIT_OWM_CTL_RST_MSK       ));
88 2 iztok
 
89
   // wait for irq to set the transfer end flag
90
   ALT_FLAG_PEND (sockit_owm.irq, 0x1, OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME, 0);
91
   // wait for STX (end of transfer cycle) and read the presence status
92 3 iztok
   while ((reg = IORD_SOCKIT_OWM_CTL (sockit_owm.base)) & SOCKIT_OWM_CTL_CYC_MSK);
93 2 iztok
 
94
   // release transfer lock
95 3 iztok
   ALT_SEM_POST (sockit_owm.cyc);
96 2 iztok
 
97 3 iztok
   // return negated DAT (presence detect)
98
   return (~reg & SOCKIT_OWM_CTL_DAT_MSK);  // NOTE the shortcut
99 2 iztok
}
100
 
101
//--------------------------------------------------------------------------
102
// Send 1 bit of communication to the 1-Wire Net and return the
103
// result 1 bit read from the 1-Wire Net.  The parameter 'sendbit'
104
// least significant bit is used and the least significant bit
105
// of the result is the return bit.
106
//
107
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
108
//                indicate the symbolic port number.
109
// 'sendbit'    - the least significant bit is the bit to send
110
//
111
// Returns: 0:   0 bit read from sendbit
112
//          1:   1 bit read from sendbit
113
//
114
SMALLINT owTouchBit(int portnum, SMALLINT sendbit)
115
{
116
   int reg;
117
   int ovd = (sockit_owm.ovd >> portnum) & 0x1;
118
 
119
   // lock transfer
120 3 iztok
   ALT_SEM_PEND (sockit_owm.cyc, 0);
121 2 iztok
 
122 3 iztok
   // read/write data
123
   IOWR_SOCKIT_OWM_CTL (sockit_owm.base, (sockit_owm.pwr << SOCKIT_OWM_CTL_POWER_OFST    )
124
                                       | (portnum        << SOCKIT_OWM_CTL_SEL_OFST      )
125
                                       | (sockit_owm.ien  ? SOCKIT_OWM_CTL_IEN_MSK : 0x00)
126
                                       | (                  SOCKIT_OWM_CTL_CYC_MSK       )
127
                                       | (ovd             ? SOCKIT_OWM_CTL_OVD_MSK : 0x00)
128
                                       | (sendbit         & SOCKIT_OWM_CTL_DAT_MSK       ));  // NOTE the shortcut
129 2 iztok
 
130
   // wait for irq to set the transfer end flag
131
   ALT_FLAG_PEND (sockit_owm.irq, 0x1, OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME, 0);
132
   // wait for STX (end of transfer cycle) and read the read data bit
133 3 iztok
   while ((reg = IORD_SOCKIT_OWM_CTL (sockit_owm.base)) & SOCKIT_OWM_CTL_CYC_MSK);
134 2 iztok
 
135
   // release transfer lock
136 3 iztok
   ALT_SEM_POST (sockit_owm.cyc);
137 2 iztok
 
138 3 iztok
   // return DAT (read bit)
139
   return (reg & SOCKIT_OWM_CTL_DAT_MSK);  // NOTE the shortcut
140 2 iztok
}
141
 
142
//--------------------------------------------------------------------------
143
// Send 8 bits of communication to the 1-Wire Net and return the
144
// result 8 bits read from the 1-Wire Net.  The parameter 'sendbyte'
145
// least significant 8 bits are used and the least significant 8 bits
146
// of the result is the return byte.
147
//
148
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
149
//                indicate the symbolic port number.
150
// 'sendbyte'   - 8 bits to send (least significant byte)
151
//
152
// Returns:  8 bytes read from sendbyte
153
//
154
SMALLINT owTouchByte(int portnum, SMALLINT sendbyte)
155
{
156
   int i;
157
   SMALLINT dat = 0;
158
   for (i=0; i<8; i++)
159
   {
160
      dat |= owTouchBit(portnum,sendbyte & 0x1) << i;
161
      sendbyte >>= 1;
162
   }
163
   return dat;
164
}
165
 
166
//--------------------------------------------------------------------------
167
// Send 8 bits of communication to the 1-Wire Net and verify that the
168
// 8 bits read from the 1-Wire Net is the same (write operation).
169
// The parameter 'sendbyte' least significant 8 bits are used.
170
//
171
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
172
//                indicate the symbolic port number.
173
// 'sendbyte'   - 8 bits to send (least significant byte)
174
//
175
// Returns:  TRUE: bytes written and echo was the same
176
//           FALSE: echo was not the same
177
//
178
SMALLINT owWriteByte(int portnum, SMALLINT sendbyte)
179
{
180
   return (owTouchByte(portnum,sendbyte) == sendbyte) ? TRUE : FALSE;
181
}
182
 
183
//--------------------------------------------------------------------------
184
// Send 8 bits of read communication to the 1-Wire Net and and return the
185
// result 8 bits read from the 1-Wire Net.
186
//
187
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
188
//                indicate the symbolic port number.
189
//
190
// Returns:  8 bytes read from 1-Wire Net
191
//
192
SMALLINT owReadByte(int portnum)
193
{
194
   return owTouchByte(portnum,0xFF);
195
}
196
 
197
//--------------------------------------------------------------------------
198
// Set the 1-Wire Net communication speed.
199
//
200
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
201
//                indicate the symbolic port number.
202
// 'new_speed'  - new speed defined as
203
//                MODE_NORMAL     0x00
204
//                MODE_OVERDRIVE  0x01
205
//
206
// Returns:  current 1-Wire Net speed
207
//
208
SMALLINT owSpeed(int portnum, SMALLINT new_speed)
209
{
210 3 iztok
   int select;
211
   select = 0x1 << portnum;
212
   // if overdrive is implemented use it
213
   if (sockit_owm.ovd_e) {
214
      if (new_speed == MODE_OVERDRIVE)  sockit_owm.ovd |=  select;
215
      if (new_speed == MODE_NORMAL   )  sockit_owm.ovd &= ~select;
216
   }
217 2 iztok
   // return the current port state
218 3 iztok
   return (sockit_owm.ovd & select) ? MODE_OVERDRIVE : MODE_NORMAL;
219 2 iztok
}
220
 
221
//--------------------------------------------------------------------------
222
// Set the 1-Wire Net line level.  The values for NewLevel are
223
// as follows:
224
//
225
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
226
//                indicate the symbolic port number.
227
// 'new_level'  - new level defined as
228
//                MODE_NORMAL     0x00
229
//                MODE_STRONG5    0x02
230
//                MODE_PROGRAM    0x04
231
//                MODE_BREAK      0x08
232
//
233
// Returns:  current 1-Wire Net level
234
//
235
SMALLINT owLevel(int portnum, SMALLINT new_level)
236
{
237
   if (new_level == MODE_STRONG5) {
238
      // set the power bit
239
      sockit_owm.pwr |=  (1 << portnum);
240 3 iztok
      IOWR_SOCKIT_OWM_CTL (sockit_owm.base, (sockit_owm.pwr << SOCKIT_OWM_CTL_POWER_OFST) | SOCKIT_OWM_CTL_PWR_MSK);
241 2 iztok
   }
242
   if (new_level == MODE_NORMAL) {
243
      // clear the power bit
244
      sockit_owm.pwr &= ~(1 << portnum);
245 3 iztok
      IOWR_SOCKIT_OWM_CTL (sockit_owm.base, (sockit_owm.pwr << SOCKIT_OWM_CTL_POWER_OFST));
246 2 iztok
   }
247
   // return the current port state
248
   return ((sockit_owm.pwr >> portnum) & 0x1) ? MODE_STRONG5 : MODE_NORMAL;
249
}
250
 
251
//--------------------------------------------------------------------------
252
// This procedure creates a fixed 480 microseconds 12 volt pulse
253
// on the 1-Wire Net for programming EPROM iButtons.
254
//
255
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
256
//                indicate the symbolic port number.
257
//
258
// Returns:  TRUE  successful
259
//           FALSE program voltage not available
260
//
261
SMALLINT owProgramPulse(int portnum)
262
{
263
   return owHasProgramPulse(portnum);
264
}
265
 
266
//--------------------------------------------------------------------------
267
//  Description:
268
//     Delay for at least 'len' ms
269
//
270
void msDelay(int len)
271
{
272
#if SOCKIT_OWM_HW_DLY
273
   int i;
274
 
275 3 iztok
   // compute the number delay cycles depending on delay time
276
   len = (len * sockit_owm.f_dly) >> 16;
277
 
278 2 iztok
   // lock transfer
279 3 iztok
   ALT_SEM_PEND (sockit_owm.cyc, 0);
280 2 iztok
 
281
   for (i=0; i<len; i++) {
282
      // create a 960us pause
283 3 iztok
      IOWR_SOCKIT_OWM_CTL (sockit_owm.base, ( sockit_owm.pwr        << SOCKIT_OWM_CTL_POWER_OFST    )
284
                                          | ( sockit_owm.ien         ? SOCKIT_OWM_CTL_IEN_MSK : 0x00)
285
                                          | ((sockit_owm.pwr & 0x1)  ? SOCKIT_OWM_CTL_PWR_MSK : 0x00)
286
                                          | (                          SOCKIT_OWM_CTL_CYC_MSK       )
287
                                          | (                          SOCKIT_OWM_CTL_DLY_MSK       ));
288 2 iztok
 
289
     // wait for irq to set the transfer end flag
290
     ALT_FLAG_PEND (sockit_owm.irq, 0x1, OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME, 0);
291
     // wait for STX (end of transfer cycle)
292 3 iztok
     while (IORD_SOCKIT_OWM_CTL (sockit_owm.base) & SOCKIT_OWM_CTL_CYC_MSK);
293 2 iztok
 
294
     // release transfer lock
295 3 iztok
     ALT_SEM_POST (sockit_owm.cyc);
296 2 iztok
   }
297
#else
298
#ifdef UCOS_II
299
   // uCOS-II timed delay
300
   OSTimeDlyHMSM(0,0,0,len);
301
#else
302
   // Altera HAL us delay
303
   usleep (1000*len);
304
#endif
305
#endif
306
}
307
 
308
//--------------------------------------------------------------------------
309
// Get the current millisecond tick count.  Does not have to represent
310
// an actual time, it just needs to be an incrementing timer.
311
//
312
long msGettick(void)
313
{
314
#ifdef UCOS_II
315
   // uCOS-II tick counter
316
        OSTimeGet();
317
#else
318
   // TODO add platform specific code here
319
   return 0;
320
#endif
321
}
322
 
323
//--------------------------------------------------------------------------
324
// Send 8 bits of communication to the 1-Wire Net and verify that the
325
// 8 bits read from the 1-Wire Net is the same (write operation).
326
// The parameter 'sendbyte' least significant 8 bits are used.  After the
327
// 8 bits are sent change the level of the 1-Wire net.
328
//
329
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
330
//              OpenCOM to indicate the port number.
331
// 'sendbyte' - 8 bits to send (least significant byte)
332
//
333
// Returns:  TRUE: bytes written and echo was the same
334
//           FALSE: echo was not the same
335
//
336
SMALLINT owWriteBytePower(int portnum, SMALLINT sendbyte)
337
{
338
   if (!owHasPowerDelivery(portnum))
339
      return FALSE;
340
 
341
   if(owTouchByte(portnum,sendbyte) != sendbyte)
342
      return FALSE;
343
 
344
   if(owLevel(portnum,MODE_STRONG5) != MODE_STRONG5)
345
      return FALSE;
346
 
347
   return TRUE;
348
}
349
 
350
//--------------------------------------------------------------------------
351
// Send 1 bit of communication to the 1-Wire Net and verify that the
352
// response matches the 'applyPowerResponse' bit and apply power delivery
353
// to the 1-Wire net.  Note that some implementations may apply the power
354
// first and then turn it off if the response is incorrect.
355
//
356
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
357
//              OpenCOM to indicate the port number.
358
// 'applyPowerResponse' - 1 bit response to check, if correct then start
359
//                        power delivery
360
//
361
// Returns:  TRUE: bit written and response correct, strong pullup now on
362
//           FALSE: response incorrect
363
//
364
SMALLINT owReadBitPower(int portnum, SMALLINT applyPowerResponse)
365
{
366
   if (!owHasPowerDelivery(portnum))
367
      return FALSE;
368
 
369
   if(owTouchBit(portnum,0x01) != applyPowerResponse)
370
      return FALSE;
371
 
372
   if(owLevel(portnum,MODE_STRONG5) != MODE_STRONG5)
373
      return FALSE;
374
 
375
   return TRUE;
376
}
377
 
378
//--------------------------------------------------------------------------
379
// Read 8 bits of communication to the 1-Wire Net and then turn on
380
// power delivery.
381
//
382
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
383
//              OpenCOM to indicate the port number.
384
//
385
// Returns:  byte read
386
//           FALSE: power delivery failed
387
//
388
SMALLINT owReadBytePower(int portnum)
389
{
390
   SMALLINT getbyte;
391
 
392
   if (!owHasPowerDelivery(portnum))
393
      return FALSE;
394
 
395
   getbyte = owTouchByte(portnum,0xFF);
396
 
397
   if (owLevel(portnum,MODE_STRONG5) != MODE_STRONG5)
398
      return FALSE;
399
 
400
   return getbyte;
401
}
402
 
403
//--------------------------------------------------------------------------
404
// This procedure indicates whether the adapter can deliver power.
405
//
406
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
407
//              OpenCOM to indicate the port number.
408
//
409
// Returns:  TRUE  if adapter is capable of delivering power.
410
//
411
SMALLINT owHasPowerDelivery(int portnum)
412
{
413
   return TRUE;
414
}
415
 
416
//--------------------------------------------------------------------------
417
// This procedure indicates whether the adapter can deliver power.
418
//
419
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
420
//              OpenCOM to indicate the port number.
421
//
422
// Returns:  TRUE  if adapter is capable of over drive.
423
//
424
SMALLINT owHasOverDrive(int portnum)
425
{
426
   return sockit_owm.ovd_e;
427
}
428
 
429
//--------------------------------------------------------------------------
430
// This procedure creates a fixed 480 microseconds 12 volt pulse
431
// on the 1-Wire Net for programming EPROM iButtons.
432
//
433
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
434
//              OpenCOM to indicate the port number.
435
//
436
// Returns:  TRUE  program voltage available
437
//           FALSE program voltage not available
438
SMALLINT owHasProgramPulse(int portnum)
439
{
440
   return FALSE;
441
}

powered by: WebSVN 2.1.0

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