95e95e90f902162d2ae97fa6d5705afd39bfed17
[fw/sdcc] / device / examples / ds390 / ow390 / tinilnk.c
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 //  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.
30 //
31 //  Version: 1.02
32 //
33 //  History: 1.00 -> 1.01  Added function msDelay. 
34 //
35 //           1.01 -> 1.02  Changed to generic OpenCOM/CloseCOM for easier 
36 //                         use with other platforms.
37 //
38
39 //--------------------------------------------------------------------------
40 // Copyright (C) 1998 Andrea Chambers and University of Newcastle upon Tyne,
41 // All Rights Reserved.
42 //--------------------------------------------------------------------------
43 //
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:
50 // 
51 // The above copyright notice and this permission notice shall be included 
52 // in all copies or substantial portions of the Software.
53 // 
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 //---------------------------------------------------------------------------
62 //
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.
66 //
67 //  Version: 1.03
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()
79 //
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. 
84 //
85
86 #include <stdio.h>
87
88 #include "ownet.h"
89 #include "ds2480.h"
90
91 //---------------------------------------------------------------------------
92 // Attempt to open a com port.  
93 // Set the starting baud rate to 9600.
94 //
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.
98 //
99 // 'port_zstr' - zero terminate port name.  For this platform
100 //               ignored for now
101 //
102 //
103 // Returns: TRUE(1)  - success, COM port opened
104 //          FALSE(0) - failure, could not open specified port
105 //
106 int OpenCOM(int portnum, char *port_zstr)
107 {     
108   unsigned long baud=9600;
109
110   //printf ("OpenCOM(%d,\"%s\")\n", portnum, port_zstr);
111   
112   // hush the compiler
113   portnum;
114   port_zstr;
115   
116   Serial1Init(baud,1);
117   
118   return TRUE; // changed (2.00), used to return fd;
119 }
120
121 //---------------------------------------------------------------------------
122 // Closes the connection to the port.
123 //
124 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
125 //              OpenCOM to indicate the port number.
126 //
127 void CloseCOM(int portnum)
128 {
129   //printf ("CloseCOM(%d)\n", portnum);
130
131  // hush the compiler
132   portnum;
133 }
134
135
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.
139 //
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
144 //   
145 int WriteCOM(int portnum, int outlen, uchar *outbuf)
146 {
147   int i;
148
149   //printf ("WriteCOM(%d, %d,...): ", portnum, outlen, outbuf);
150
151   // hush the compiler
152   portnum;
153
154   for (i=0; i<outlen; i++) {
155     //printf ("%02x ", outbuf[i]);
156     Serial1PutChar(outbuf[i]);
157   }
158   //printf ("\n");
159   return TRUE;
160 }  
161
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.
165 //
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
170 //
171 // Returns:  TRUE(1)  - success 
172 //           FALSE(0) - failure
173 //
174 int ReadCOM(int portnum, int inlen, uchar *inbuf)
175 {  
176   int i;
177
178   //printf ("ReadCOM(%d,%d,...): ", portnum, inlen);
179
180   // hush the compiler
181   portnum;
182
183   for (i=0; i<inlen; i++) {
184     inbuf[i]=Serial1GetChar();
185     //printf ("%02x ", inbuf[i]);
186   }
187   //printf ("\n");
188   
189    // success, so return desired length
190    return i;
191 }
192
193
194 //---------------------------------------------------------------------------
195 //  Description:
196 //     flush the rx and tx buffers
197 //
198 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
199 //              OpenCOM to indicate the port number.
200 //
201 void FlushCOM(int portnum)    
202 {    
203   //printf ("FlushCOM(%d): ", portnum);
204
205   // hush the compiler
206   portnum;
207
208   Serial1Flush();
209 }  
210
211
212 //--------------------------------------------------------------------------
213 //  Description:
214 //     Send a break on the com port for at least 2 ms
215 // 
216 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
217 //              OpenCOM to indicate the port number.
218 //
219
220 void BreakCOM(int portnum)      
221 {
222   //printf ("BreakCOM(%d)\n", portnum);
223   // hush the compiler
224   portnum;
225
226   Serial1SendBreak();
227 }
228
229
230 //--------------------------------------------------------------------------
231 // Set the baud rate on the com port. 
232 //
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
236 // PARMSET_9600     0x00
237 // PARMSET_19200    0x02
238 // PARMSET_57600    0x04
239 // PARMSET_115200   0x06
240 // 
241
242 void SetBaudCOM(int portnum, int new_baud)
243 {
244   unsigned long baud;
245   //printf ("SetBaudCOM(%d,%d)\n", portnum, new_baud);
246
247   portnum; // hush the compiler
248
249   switch (new_baud) {
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;
254   default:
255     return;
256   }
257   Serial1Baud(baud);
258 }
259
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.
263 //
264 long msGettick(void)
265 {
266    return ClockTicks();
267 }
268
269
270 //--------------------------------------------------------------------------
271 //  Description:
272 //     Delay for at least 'len' ms
273 // 
274 void msDelay(int len)
275 {
276   ClockMilliSecondsDelay(len);
277 }
278