summaryrefslogtreecommitdiff
path: root/src/pmdas/roomtemp/mlan/mlanllu.c
blob: 91b23f0e6a248cb7c46911bf6a23ee0c77684816 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//---------------------------------------------------------------------------
// Copyright (C) 1999 Dallas Semiconductor Corporation, All Rights Reserved.
// 
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the 
// Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
// MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES 
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
// OTHER DEALINGS IN THE SOFTWARE.
// 
// Except as contained in this notice, the name of Dallas Semiconductor 
// shall not be used except as stated in the Dallas Semiconductor 
// Branding Policy. 
//---------------------------------------------------------------------------
//
//  MLanLLU.C - Link Layer MicroLAN functions using the DS2480 (U)
//              serial interface chip.
//
//  Version: 1.03
//
//  History: 1.00 -> 1.01  DS2480 version number now ignored in 
//                         MLanTouchReset.
//           1.02 -> 1.03  Removed caps in #includes for Linux capatibility
//                         Removed #include <windows.h> 
//                         Add #include "mlan.h" to define TRUE,FALSE

#include "ds2480.h"
#include "mlan.h"

// external COM functions required
extern void FlushCOM(void);
extern int  WriteCOM(int, uchar *);
extern int  ReadCOM(int, uchar *);
//extern void SetBaudCOM(int);

// external DS2480 utility function
extern int DS2480Detect(void);
//extern int DS2480ChangeBaud(int);

// local functions
int  MLanTouchReset(void);
int  MLanTouchBit(int sendbit);
int  MLanTouchByte(int sendbyte);
int  MLanWriteByte(int sendbyte);
int  MLanReadByte(void);
int  MLanSpeed(int);
int  MLanLevel(int);
int  MLanProgramPulse(void);

// external globals
extern int UMode;   // current DS2480 command or data mode state
extern int UBaud;   // current DS2480 baud rate
extern int USpeed;  // current DS2480 MicroLAN communication speed
extern int ULevel;  // current DS2480 MicroLAN level

// local varable flag, true if program voltage available
static int ProgramAvailable=FALSE;


//--------------------------------------------------------------------------
// Reset all of the devices on the MicroLAN and return the result.
//
// Returns: TRUE(1):  presense pulse(s) detected, device(s) reset
//          FALSE(0): no presense pulses detected
//
// WARNING: This routine will not function correctly on some
//          Alarm reset types of the DS1994/DS1427/DS2404 with
//          Rev 1 and 2 of the DS2480.
//
int MLanTouchReset(void)
{
   uchar readbuffer[10],sendpacket[10];
   int sendlen=0;

   // make sure normal level
   MLanLevel(MODE_NORMAL);

   // check if correct mode 
   if (UMode != MODSEL_COMMAND)
   {
      UMode = MODSEL_COMMAND;
      sendpacket[sendlen++] = MODE_COMMAND;
   }

   // construct the command
   sendpacket[sendlen++] = (uchar)(CMD_COMM | FUNCTSEL_RESET | USpeed);

   // flush the buffers
   FlushCOM();

   // send the packet 
   if (WriteCOM(sendlen,sendpacket)) 
   {
      // read back the 1 byte response 
      if (ReadCOM(1,readbuffer) == 1)
      {
         // make sure this byte looks like a reset byte
         if (((readbuffer[0] & RB_RESET_MASK) == RB_PRESENCE) ||
             ((readbuffer[0] & RB_RESET_MASK) == RB_ALARMPRESENCE)) 
         {
            // check if programming voltage available
            ProgramAvailable = ((readbuffer[0] & 0x20) == 0x20); 
            return TRUE;
         }
         else
            return FALSE;
      }
   }

   // an error occured so re-sync with DS2480
   DS2480Detect();

   return FALSE;
}


//--------------------------------------------------------------------------
// Send 1 bit of communication to the MicroLAN and return the
// result 1 bit read from the MicroLAN.  The parameter 'sendbit'
// least significant bit is used and the least significant bit
// of the result is the return bit.
//
// 'sendbit' - the least significant bit is the bit to send
//
// Returns: 0:   0 bit read from sendbit
//          1:   1 bit read from sendbit
//
int MLanTouchBit(int sendbit)
{
   uchar readbuffer[10],sendpacket[10];
   int sendlen=0;

   // make sure normal level
   MLanLevel(MODE_NORMAL);

   // check if correct mode 
   if (UMode != MODSEL_COMMAND)
   {
      UMode = MODSEL_COMMAND;
      sendpacket[sendlen++] = MODE_COMMAND;
   }

   // construct the command
   sendpacket[sendlen] = (sendbit != 0) ? BITPOL_ONE : BITPOL_ZERO;
   sendpacket[sendlen++] |= CMD_COMM | FUNCTSEL_BIT | USpeed;

   // flush the buffers
   FlushCOM();

   // send the packet 
   if (WriteCOM(sendlen,sendpacket)) 
   {
      // read back the response 
      if (ReadCOM(1,readbuffer) == 1)
      {
         // interpret the response 
         if (((readbuffer[0] & 0xE0) == 0x80) &&
             ((readbuffer[0] & RB_BIT_MASK) == RB_BIT_ONE))
            return 1;
         else
            return 0;
      }
   }

   // an error occured so re-sync with DS2480
   DS2480Detect();

   return 0;
}


//--------------------------------------------------------------------------
// Send 8 bits of communication to the MicroLAN and verify that the
// 8 bits read from the MicroLAN is the same (write operation).  
// The parameter 'sendbyte' least significant 8 bits are used.
//
// 'sendbyte' - 8 bits to send (least significant byte)
//
// Returns:  TRUE: bytes written and echo was the same
//           FALSE: echo was not the same 
//
int MLanWriteByte(int sendbyte)
{
   return (MLanTouchByte(sendbyte) == sendbyte) ? TRUE : FALSE;
}


//--------------------------------------------------------------------------
// Send 8 bits of read communication to the MicroLAN and and return the
// result 8 bits read from the MicroLAN.   
//
// Returns:  8 bytes read from MicroLAN
//
int MLanReadByte(void)
{
   return MLanTouchByte(0xFF);
}


//--------------------------------------------------------------------------
// Send 8 bits of communication to the MicroLAN and return the
// result 8 bits read from the MicroLAN.  The parameter 'sendbyte'
// least significant 8 bits are used and the least significant 8 bits
// of the result is the return byte.
//
// 'sendbyte' - 8 bits to send (least significant byte)
//
// Returns:  8 bytes read from sendbyte
//
int MLanTouchByte(int sendbyte)
{
   uchar readbuffer[10],sendpacket[10];
   int sendlen=0;

   // make sure normal level
   MLanLevel(MODE_NORMAL);

   // check if correct mode 
   if (UMode != MODSEL_DATA)
   {
      UMode = MODSEL_DATA;
      sendpacket[sendlen++] = MODE_DATA;
   }

   // add the byte to send
   sendpacket[sendlen++] = (uchar)sendbyte;

   // check for duplication of data that looks like COMMAND mode 
   if (sendbyte == MODE_COMMAND) 
      sendpacket[sendlen++] = (uchar)sendbyte;

   // flush the buffers
   FlushCOM();

   // send the packet 
   if (WriteCOM(sendlen,sendpacket)) 
   {
      // read back the 1 byte response 
      if (ReadCOM(1,readbuffer) == 1)
      {
          // return the response 
          return (int)readbuffer[0];
      }
   }

   // an error occured so re-sync with DS2480
   DS2480Detect();

   return 0;
}


//--------------------------------------------------------------------------
// Set the MicroLAN communucation speed.  
//
// 'NewSpeed' - new speed defined as
//                MODE_NORMAL     0x00
//                MODE_OVERDRIVE  0x01
//
// Returns:  current MicroLAN speed 
//
int MLanSpeed(int NewSpeed)
{
   uchar sendpacket[5];
   short sendlen=0;
   int rt = FALSE;
   
   // check if change from current mode
   if (((NewSpeed == MODE_OVERDRIVE) &&
        (USpeed != SPEEDSEL_OD)) ||
       ((NewSpeed == MODE_NORMAL) &&
        (USpeed != SPEEDSEL_FLEX)))
   {
      if (NewSpeed == MODE_OVERDRIVE) 
      {
         // if overdrive then switch to 115200 baud
         if (DS2480ChangeBaud(PARMSET_57600) == PARMSET_57600)
         {
            USpeed = SPEEDSEL_OD;
            rt = TRUE;
         }
      }
      else if (NewSpeed == MODE_NORMAL) 
      {
         // else normal so set to 9600 baud
         if (DS2480ChangeBaud(PARMSET_9600) == PARMSET_9600)
         {
            USpeed = SPEEDSEL_FLEX;
            rt = TRUE;
         }
      }

      // if baud rate is set correctly then change DS2480 speed
      if (rt)
      {
         // check if correct mode 
         if (UMode != MODSEL_COMMAND)
         {
            UMode = MODSEL_COMMAND;
            sendpacket[sendlen++] = MODE_COMMAND;
         }

         // proceed to set the DS2480 communication speed
         sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_SEARCHOFF | USpeed;

         // send the packet 
         if (!WriteCOM(sendlen,sendpacket)) 
         {
            rt = FALSE;
            // lost communication with DS2480 then reset 
            DS2480Detect();
         }
      }
   }

   // return the current speed
   return (USpeed == SPEEDSEL_OD) ? MODE_OVERDRIVE : MODE_NORMAL;
}


//--------------------------------------------------------------------------
// Set the MicroLAN line level.  The values for NewLevel are
// as follows:
//
// 'NewLevel' - new level defined as
//                MODE_NORMAL     0x00
//                MODE_STRONG5    0x02
//                MODE_PROGRAM    0x04
//                MODE_BREAK      0x08 (not supported)
//
// Returns:  current MicroLAN level  
//
int MLanLevel(int NewLevel)
{
   uchar sendpacket[10],readbuffer[10];
   short sendlen=0;
   short rt=FALSE;

   // check if need to change level
   if (NewLevel != ULevel)
   {
      // check if just putting back to normal
      if (NewLevel == MODE_NORMAL)
      {
         // check if correct mode 
         if (UMode != MODSEL_COMMAND)
         {
            UMode = MODSEL_COMMAND;
            sendpacket[sendlen++] = MODE_COMMAND;
         }

         // stop pulse command
         sendpacket[sendlen++] = MODE_STOP_PULSE;
   
         // flush the buffers
         FlushCOM();

         // send the packet 
         if (WriteCOM(sendlen,sendpacket)) 
         {
            // read back the 1 byte response 
            if (ReadCOM(1,readbuffer) == 1)
            {
               // check response byte
               if ((readbuffer[0] & 0xE0) == 0xE0)
               {
                  rt = TRUE;
                  ULevel = MODE_NORMAL;
               }
            }
         }
      }
      // set new level
      else
      {
         // check if correct mode 
         if (UMode != MODSEL_COMMAND)
         {
            UMode = MODSEL_COMMAND;
            sendpacket[sendlen++] = MODE_COMMAND;
         }

         // strong 5 volts
         if (NewLevel == MODE_STRONG5)
         {
            // set the SPUD time value 
            sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_5VPULSE | PARMSET_infinite;
            // add the command to begin the pulse
            sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | SPEEDSEL_PULSE | BITPOL_5V;
         }
         // 12 volts
         else if (NewLevel == MODE_PROGRAM)
         {
            // check if programming voltage available
            if (!ProgramAvailable)
               return MODE_NORMAL;

            // set the PPD time value 
            sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_12VPULSE | PARMSET_infinite;
            // add the command to begin the pulse
            sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | SPEEDSEL_PULSE | BITPOL_12V;
         }

         // flush the buffers
         FlushCOM();

         // send the packet 
         if (WriteCOM(sendlen,sendpacket)) 
         {
            // read back the 1 byte response from setting time limit
            if (ReadCOM(1,readbuffer) == 1)
            {
               // check response byte
               if ((readbuffer[0] & 0x81) == 0)
               {
                  ULevel = NewLevel;
                  rt = TRUE;
               }
            }
         }
      }

      // if lost communication with DS2480 then reset 
      if (rt != TRUE)
         DS2480Detect();
   }

   // return the current level
   return ULevel;      
}


//--------------------------------------------------------------------------
// This procedure creates a fixed 480 microseconds 12 volt pulse 
// on the MicroLAN for programming EPROM iButtons.
//
// Returns:  TRUE  successful
//           FALSE program voltage not available  
//
int MLanProgramPulse(void)
{
   uchar sendpacket[10],readbuffer[10];
   short sendlen=0;

   // check if programming voltage available
   if (!ProgramAvailable)
      return FALSE;

   // make sure normal level
   MLanLevel(MODE_NORMAL);

   // check if correct mode 
   if (UMode != MODSEL_COMMAND)
   {
      UMode = MODSEL_COMMAND;
      sendpacket[sendlen++] = MODE_COMMAND;
   }

   // set the SPUD time value 
   sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_12VPULSE | PARMSET_512us;

   // pulse command
   sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | BITPOL_12V | SPEEDSEL_PULSE;
   
   // flush the buffers
   FlushCOM();

   // send the packet 
   if (WriteCOM(sendlen,sendpacket)) 
   {
      // read back the 2 byte response 
      if (ReadCOM(2,readbuffer) == 2)
      {
         // check response byte
         if (((readbuffer[0] | CMD_CONFIG) == 
                (CMD_CONFIG | PARMSEL_12VPULSE | PARMSET_512us)) &&
             ((readbuffer[1] & 0xFC) == 
                (0xFC & (CMD_COMM | FUNCTSEL_CHMOD | BITPOL_12V | SPEEDSEL_PULSE))))
            return TRUE;
      }
   }

   // an error occured so re-sync with DS2480
   DS2480Detect();

   return FALSE;
}