1 //---------------------------------------------------------------------------
2 // Copyright (C) 2000 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 //  VisowSes.C - Acquire and release a Session on the 1-Wire Net.
28 
29 #include <SystemMgr.h>
30 #include "SauthPalm.h"
31 
32 // external function prototypes
33 extern SMALLINT owSpeed(int,SMALLINT);
34 extern void output_status(int, char *);
35 extern void msDelay(int);
36 
37 // local function prototypes
38 SMALLINT  owAcquire(int,char *,char *);
39 void owRelease(int,char *);
40 int RetPort(int);
41 
42 // keep port name for later message when closing
43 char portname[MAX_PORTNUM][128];
44 int CntAcqRel=0;
45 int port[MAX_PORTNUM];
46 
47 // Code to save batteries
48 static  void (*g_lpTrapLCDSleep)();
HandleLCDSleepTrap()49 static void HandleLCDSleepTrap()
50 {
51   AdapterPowerManagement(0);
52 
53   if(g_lpTrapLCDSleep)
54     g_lpTrapLCDSleep();
55 }
56 // Code to save batteries
57 
58 
59 //---------------------------------------------------------------------------
60 // Attempt to acquire a 1-Wire net using a com port and a DS2480 based
61 // adapter.
62 //
63 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number was provided to
64 //                indicate the port number.
65 // 'port_zstr'  - zero terminated port name.  For this platform
66 //                use format COMX where X is the port number.
67 // 'return_msg' - zero terminated return message.
68 //
69 // Returns: TRUE - success, COM port opened
70 //
owAcquire(int portnum,char * port_zstr,char * return_msg)71 SMALLINT owAcquire(int portnum, char *port_zstr, char *return_msg)
72 {
73    int input;
74 
75    if(!CntAcqRel)
76    {
77    		g_lpTrapLCDSleep = SysGetTrapAddress(sysTrapHwrDisplaySleep);
78    		SysSetTrapAddress(sysTrapHwrDisplaySleep, HandleLCDSleepTrap);
79 
80    		AdapterPowerManagement(1);
81    		msDelay(50);
82    }
83 
84    if(port_zstr[0] == '0')
85    {
86    		port[portnum] = VISOR_INT;
87    		input = VISOR_INT;
88    }
89    else if(port_zstr[0] == '1')
90    {
91    		port[portnum] = VISOR_EX;
92    		input = VISOR_EX;
93    }
94 
95    iBSetup(input);
96    iBKeyOpen();
97 
98    if(owSpeed(portnum,MODE_NORMAL) != MODE_NORMAL)
99    		return FALSE;
100 
101    CntAcqRel++;
102 
103    return TRUE;
104 }
105 
106 //---------------------------------------------------------------------------
107 // Release the previously acquired a 1-Wire net.
108 //
109 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number was provided to
110 //                indicate the port number.
111 // 'return_msg' - zero terminated return message.
112 //
owRelease(int portnum,char * return_msg)113 void owRelease(int portnum, char *return_msg)
114 {
115 
116    if(CntAcqRel)
117    {
118 
119    		iBSetup(port[portnum]);
120    		owSpeed(port[portnum],MODE_NORMAL);
121 
122    		CntAcqRel--;
123 
124    		// close the communications port
125    		iBKeyClose();
126 
127    		if(!CntAcqRel)
128    		{
129    			if(g_lpTrapLCDSleep)
130 	  			SysSetTrapAddress(sysTrapHwrDisplaySleep, g_lpTrapLCDSleep);
131 
132 	  		AdapterPowerManagement(0);
133 
134 	  	}
135    }
136 
137 }
138 
139 
140 //------------------------------------------------------------------------
141 // Used to get the correct port for the Visor
142 //
143 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number was provided to
144 //                indicate the port number.
RetPort(int portnum)145 int RetPort(int portnum)
146 {
147 	return port[portnum];
148 }