Imported Upstream version 2.9.0
[debian/cc1111] / device / examples / ds390 / ow390 / tinilnk.c
1 #define DEBUG_OW_COM 0
2 #if DEBUG_OW_COM
3 #include <stdio.h>
4 #endif
5 //---------------------------------------------------------------------------
6 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
7 // 
8 // Permission is hereby granted, free of charge, to any person obtaining a 
9 // copy of this software and associated documentation files (the "Software"), 
10 // to deal in the Software without restriction, including without limitation 
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 
12 // and/or sell copies of the Software, and to permit persons to whom the 
13 // Software is furnished to do so, subject to the following conditions:
14 // 
15 // The above copyright notice and this permission notice shall be included 
16 // in all copies or substantial portions of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
19 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
20 // MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
21 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES 
22 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
23 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
24 // OTHER DEALINGS IN THE SOFTWARE.
25 // 
26 // Except as contained in this notice, the name of Dallas Semiconductor 
27 // shall not be used except as stated in the Dallas Semiconductor 
28 // Branding Policy. 
29 //---------------------------------------------------------------------------
30 //
31 //  TODO.C - COM functions required by MLANLL.C, MLANTRNU, MLANNETU.C and
32 //           MLanFile.C for MLANU to communicate with the DS2480 based 
33 //           Universal Serial Adapter 'U'.  Fill in the platform specific code.
34 //
35 //  Version: 1.02
36 //
37 //  History: 1.00 -> 1.01  Added function msDelay. 
38 //
39 //           1.01 -> 1.02  Changed to generic OpenCOM/CloseCOM for easier 
40 //                         use with other platforms.
41 //
42
43 //--------------------------------------------------------------------------
44 // Copyright (C) 1998 Andrea Chambers and University of Newcastle upon Tyne,
45 // All Rights Reserved.
46 //--------------------------------------------------------------------------
47 //
48 // Permission is hereby granted, free of charge, to any person obtaining a 
49 // copy of this software and associated documentation files (the "Software"), 
50 // to deal in the Software without restriction, including without limitation 
51 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 
52 // and/or sell copies of the Software, and to permit persons to whom the 
53 // Software is furnished to do so, subject to the following conditions:
54 // 
55 // The above copyright notice and this permission notice shall be included 
56 // in all copies or substantial portions of the Software.
57 // 
58 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
59 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
60 // MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
61 // IN NO EVENT SHALL THE UNIVERSITY OF NEWCASTLE UPON TYNE OR ANDREA CHAMBERS
62 // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
63 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
64 // THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
65 //---------------------------------------------------------------------------
66 //
67 //  LinuxLNK.C - COM functions required by MLANLLU.C, MLANTRNU.C, MLANNETU.C 
68 //             and MLanFile.C for MLANU to communicate with the DS2480 based
69 //             Universal Serial Adapter 'U'.  Platform specific code.
70 //
71 //  Version: 1.03
72 //  History: 1.00 -> 1.03 modifications by David Smiczek
73 //                        Changed to use generic OpenCOM/CloseCOM  
74 //                        Pass port name to OpenCOM instead of hard coded
75 //                        Changed msDelay to handle long delays 
76 //                        Reformatted to look like 'TODO.C' 
77 //                        Added #include "ds2480.h" to use constants.
78 //                        Added function SetBaudCOM() 
79 //                        Added function msGettick()
80 //                        Removed delay from WriteCOM(), used tcdrain()
81 //                        Added wait for byte available with timeout using
82 //                          select() in ReadCOM()
83 //
84 //           1.03 -> 2.00 Support for multiple ports. Include "ownet.h". Use
85 //                        'uchar'.  Reorder functions. Provide correct 
86 //                        return values to OpenCOM.  Replace 'makeraw' call.
87 //                        Should now be POSIX. 
88 //
89
90 #include <stdio.h>
91
92 #include "ownet.h"
93 #include "ds2480.h"
94
95 //---------------------------------------------------------------------------
96 // Attempt to open a com port.  
97 // Set the starting baud rate to 9600.
98 //
99 // 'portnum'   - number 0 to MAX_PORTNUM-1.  This number provided will 
100 //               be used to indicate the port number desired when calling
101 //               all other functions in this library.
102 //
103 // 'port_zstr' - zero terminate port name.  For this platform
104 //               ignored for now
105 //
106 //
107 // Returns: TRUE(1)  - success, COM port opened
108 //          FALSE(0) - failure, could not open specified port
109 //
110 int OpenCOM(int portnum, char *port_zstr)
111 {     
112   unsigned long baud=9600;
113
114   //printf ("OpenCOM(%d,\"%s\")\n", portnum, port_zstr);
115   
116   // hush the compiler
117   portnum;
118   port_zstr;
119   
120   Serial1Init(baud,1);
121   
122   return TRUE; // changed (2.00), used to return fd;
123 }
124
125 //---------------------------------------------------------------------------
126 // Closes the connection to the port.
127 //
128 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
129 //              OpenCOM to indicate the port number.
130 //
131 void CloseCOM(int portnum)
132 {
133   //printf ("CloseCOM(%d)\n", portnum);
134
135  // hush the compiler
136   portnum;
137 }
138
139
140 //--------------------------------------------------------------------------
141 // Write an array of bytes to the COM port, verify that it was
142 // sent out.  Assume that baud rate has been set.
143 //
144 // 'portnum'   - number 0 to MAX_PORTNUM-1.  This number provided will 
145 //               be used to indicate the port number desired when calling
146 //               all other functions in this library.
147 // Returns 1 for success and 0 for failure
148 //   
149 int WriteCOM(int portnum, int outlen, uchar *outbuf)
150 {
151   int i;
152
153 #if DEBUG_OW_COM
154   printf ("WriteCOM(%d, %d,...): ", portnum, outlen, outbuf);
155 #endif
156
157   // hush the compiler
158   portnum;
159
160   for (i=0; i<outlen; i++) {
161 #if DEBUG_OW_COM
162     printf ("%02x ", outbuf[i]);
163 #endif
164     Serial1PutChar(outbuf[i]);
165   }
166 #if DEBUG_OW_COM
167   printf ("\n");
168 #endif
169   return TRUE;
170 }  
171
172 //--------------------------------------------------------------------------
173 // Read an array of bytes to the COM port, verify that it was
174 // sent out.  Assume that baud rate has been set.
175 //
176 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
177 //              OpenCOM to indicate the port number.
178 // 'outlen'   - number of bytes to write to COM port
179 // 'outbuf'   - pointer ot an array of bytes to write
180 //
181 // Returns:  TRUE(1)  - success 
182 //           FALSE(0) - failure
183 //
184 int ReadCOM(int portnum, int inlen, uchar *inbuf)
185 {  
186   int i;
187
188 #if DEBUG_OW_COM
189   printf ("ReadCOM(%d,%d,...): ", portnum, inlen);
190 #endif
191
192   // hush the compiler
193   portnum;
194
195   for (i=0; i<inlen; i++) {
196     inbuf[i]=Serial1GetChar();
197 #if DEBUG_OW_COM
198     printf ("%02x ", inbuf[i]&0xff);
199 #endif
200   }
201 #if DEBUG_OW_COM
202   printf ("\n");
203 #endif
204   
205    // success, so return desired length
206    return i;
207 }
208
209
210 //---------------------------------------------------------------------------
211 //  Description:
212 //     flush the rx and tx buffers
213 //
214 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
215 //              OpenCOM to indicate the port number.
216 //
217 void FlushCOM(int portnum)    
218 {    
219   //printf ("FlushCOM(%d): ", portnum);
220
221   // hush the compiler
222   portnum;
223
224   Serial1Flush();
225 }  
226
227
228 //--------------------------------------------------------------------------
229 //  Description:
230 //     Send a break on the com port for at least 2 ms
231 // 
232 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
233 //              OpenCOM to indicate the port number.
234 //
235
236 void BreakCOM(int portnum)      
237 {
238   //printf ("BreakCOM(%d)\n", portnum);
239   // hush the compiler
240   portnum;
241
242   Serial1SendBreak();
243 }
244
245
246 //--------------------------------------------------------------------------
247 // Set the baud rate on the com port. 
248 //
249 // 'portnum'   - number 0 to MAX_PORTNUM-1.  This number was provided to
250 //               OpenCOM to indicate the port number.
251 // 'new_baud'  - new baud rate defined as
252 // PARMSET_9600     0x00
253 // PARMSET_19200    0x02
254 // PARMSET_57600    0x04
255 // PARMSET_115200   0x06
256 // 
257
258 void SetBaudCOM(int portnum, int new_baud)
259 {
260   unsigned long baud;
261
262   portnum; // hush the compiler
263
264   switch (new_baud) {
265   case PARMSET_9600: baud=9600; break;
266   case PARMSET_19200: baud=19200; break;
267   case PARMSET_57600: baud=57600; break;
268   case PARMSET_115200: baud=115200; break;
269   default:
270     return;
271   }
272   Serial1Baud(baud);
273 }
274
275 //--------------------------------------------------------------------------
276 // Get the current millisecond tick count.  Does not have to represent
277 // an actual time, it just needs to be an incrementing timer.
278 //
279 long msGettick(void)
280 {
281    return ClockTicks();
282 }
283
284
285 //--------------------------------------------------------------------------
286 //  Description:
287 //     Delay for at least 'len' ms
288 // 
289 void msDelay(int len)
290 {
291   ClockMilliSecondsDelay(len);
292 }
293