1 //---------------------------------------------------------------------------
2 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 // Except as contained in this notice, the name of Dallas Semiconductor
23 // shall not be used except as stated in the Dallas Semiconductor
25 //---------------------------------------------------------------------------
27 // TODO.C - COM functions required by MLANLL.C, MLANTRNU, MLANNETU.C and
28 // MLanFile.C for MLANU to communicate with the DS2480 based
29 // Universal Serial Adapter 'U'. Fill in the platform specific code.
33 // History: 1.00 -> 1.01 Added function msDelay.
35 // 1.01 -> 1.02 Changed to generic OpenCOM/CloseCOM for easier
36 // use with other platforms.
39 //--------------------------------------------------------------------------
40 // Copyright (C) 1998 Andrea Chambers and University of Newcastle upon Tyne,
41 // All Rights Reserved.
42 //--------------------------------------------------------------------------
44 // Permission is hereby granted, free of charge, to any person obtaining a
45 // copy of this software and associated documentation files (the "Software"),
46 // to deal in the Software without restriction, including without limitation
47 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
48 // and/or sell copies of the Software, and to permit persons to whom the
49 // Software is furnished to do so, subject to the following conditions:
51 // The above copyright notice and this permission notice shall be included
52 // in all copies or substantial portions of the Software.
54 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
55 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
56 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
57 // IN NO EVENT SHALL THE UNIVERSITY OF NEWCASTLE UPON TYNE OR ANDREA CHAMBERS
58 // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
59 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
60 // THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
61 //---------------------------------------------------------------------------
63 // LinuxLNK.C - COM functions required by MLANLLU.C, MLANTRNU.C, MLANNETU.C
64 // and MLanFile.C for MLANU to communicate with the DS2480 based
65 // Universal Serial Adapter 'U'. Platform specific code.
68 // History: 1.00 -> 1.03 modifications by David Smiczek
69 // Changed to use generic OpenCOM/CloseCOM
70 // Pass port name to OpenCOM instead of hard coded
71 // Changed msDelay to handle long delays
72 // Reformatted to look like 'TODO.C'
73 // Added #include "ds2480.h" to use constants.
74 // Added function SetBaudCOM()
75 // Added function msGettick()
76 // Removed delay from WriteCOM(), used tcdrain()
77 // Added wait for byte available with timeout using
78 // select() in ReadCOM()
80 // 1.03 -> 2.00 Support for multiple ports. Include "ownet.h". Use
81 // 'uchar'. Reorder functions. Provide correct
82 // return values to OpenCOM. Replace 'makeraw' call.
83 // Should now be POSIX.
91 //---------------------------------------------------------------------------
92 // Attempt to open a com port.
93 // Set the starting baud rate to 9600.
95 // 'portnum' - number 0 to MAX_PORTNUM-1. This number provided will
96 // be used to indicate the port number desired when calling
97 // all other functions in this library.
99 // 'port_zstr' - zero terminate port name. For this platform
103 // Returns: TRUE(1) - success, COM port opened
104 // FALSE(0) - failure, could not open specified port
106 int OpenCOM(int portnum, char *port_zstr)
108 unsigned long baud=9600;
110 //printf ("OpenCOM(%d,\"%s\")\n", portnum, port_zstr);
118 return TRUE; // changed (2.00), used to return fd;
121 //---------------------------------------------------------------------------
122 // Closes the connection to the port.
124 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
125 // OpenCOM to indicate the port number.
127 void CloseCOM(int portnum)
129 //printf ("CloseCOM(%d)\n", portnum);
136 //--------------------------------------------------------------------------
137 // Write an array of bytes to the COM port, verify that it was
138 // sent out. Assume that baud rate has been set.
140 // 'portnum' - number 0 to MAX_PORTNUM-1. This number provided will
141 // be used to indicate the port number desired when calling
142 // all other functions in this library.
143 // Returns 1 for success and 0 for failure
145 int WriteCOM(int portnum, int outlen, uchar *outbuf)
149 //printf ("WriteCOM(%d, %d,...): ", portnum, outlen, outbuf);
154 for (i=0; i<outlen; i++) {
155 //printf ("%02x ", outbuf[i]);
156 Serial1PutChar(outbuf[i]);
162 //--------------------------------------------------------------------------
163 // Read an array of bytes to the COM port, verify that it was
164 // sent out. Assume that baud rate has been set.
166 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
167 // OpenCOM to indicate the port number.
168 // 'outlen' - number of bytes to write to COM port
169 // 'outbuf' - pointer ot an array of bytes to write
171 // Returns: TRUE(1) - success
172 // FALSE(0) - failure
174 int ReadCOM(int portnum, int inlen, uchar *inbuf)
178 //printf ("ReadCOM(%d,%d,...): ", portnum, inlen);
183 for (i=0; i<inlen; i++) {
184 inbuf[i]=Serial1GetChar();
185 //printf ("%02x ", inbuf[i]);
189 // success, so return desired length
194 //---------------------------------------------------------------------------
196 // flush the rx and tx buffers
198 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
199 // OpenCOM to indicate the port number.
201 void FlushCOM(int portnum)
203 //printf ("FlushCOM(%d): ", portnum);
212 //--------------------------------------------------------------------------
214 // Send a break on the com port for at least 2 ms
216 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
217 // OpenCOM to indicate the port number.
220 void BreakCOM(int portnum)
222 //printf ("BreakCOM(%d)\n", portnum);
230 //--------------------------------------------------------------------------
231 // Set the baud rate on the com port.
233 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
234 // OpenCOM to indicate the port number.
235 // 'new_baud' - new baud rate defined as
237 // PARMSET_19200 0x02
238 // PARMSET_57600 0x04
239 // PARMSET_115200 0x06
242 void SetBaudCOM(int portnum, int new_baud)
245 //printf ("SetBaudCOM(%d,%d)\n", portnum, new_baud);
247 portnum; // hush the compiler
250 case PARMSET_9600: baud=9600; break;
251 case PARMSET_19200: baud=19200; break;
252 case PARMSET_57600: baud=57600; break;
253 case PARMSET_115200: baud=115200; break;
260 //--------------------------------------------------------------------------
261 // Get the current millisecond tick count. Does not have to represent
262 // an actual time, it just needs to be an incrementing timer.
270 //--------------------------------------------------------------------------
272 // Delay for at least 'len' ms
274 void msDelay(int len)
276 ClockMilliSecondsDelay(len);