Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages   Examples  

/opentcp/system.c

Go to the documentation of this file.
00001 /*
00002  *Copyright (c) 2000-2002 Viola Systems Ltd.
00003  *All rights reserved.
00004  *
00005  *Redistribution and use in source and binary forms, with or without 
00006  *modification, are permitted provided that the following conditions 
00007  *are met:
00008  *
00009  *1. Redistributions of source code must retain the above copyright 
00010  *notice, this list of conditions and the following disclaimer.
00011  *
00012  *2. Redistributions in binary form must reproduce the above copyright 
00013  *notice, this list of conditions and the following disclaimer in the 
00014  *documentation and/or other materials provided with the distribution.
00015  *
00016  *3. The end-user documentation included with the redistribution, if 
00017  *any, must include the following acknowledgment:
00018  *      "This product includes software developed by Viola 
00019  *      Systems (http://www.violasystems.com/)."
00020  *
00021  *Alternately, this acknowledgment may appear in the software itself, 
00022  *if and wherever such third-party acknowledgments normally appear.
00023  *
00024  *4. The names "OpenTCP" and "Viola Systems" must not be used to 
00025  *endorse or promote products derived from this software without prior 
00026  *written permission. For written permission, please contact 
00027  *opentcp@opentcp.org.
00028  *
00029  *5. Products derived from this software may not be called "OpenTCP", 
00030  *nor may "OpenTCP" appear in their name, without prior written 
00031  *permission of the Viola Systems Ltd.
00032  *
00033  *THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED 
00034  *WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00035  *MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
00036  *IN NO EVENT SHALL VIOLA SYSTEMS LTD. OR ITS CONTRIBUTORS BE LIABLE 
00037  *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00038  *CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
00039  *SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
00040  *BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00041  *WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
00042  *OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
00043  *EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00044  *====================================================================
00045  *
00046  *OpenTCP is the unified open source TCP/IP stack available on a series 
00047  *of 8/16-bit microcontrollers, please see <http://www.opentcp.org>.
00048  *
00049  *For more information on how to network-enable your devices, or how to 
00050  *obtain commercial technical support for OpenTCP, please see 
00051  *<http://www.violasystems.com/>.
00052  */
00053 
00069 #include <inet/arch/config.h>
00070 #include <inet/datatypes.h>
00071 #include <inet/system.h>
00072 #include <inet/debug.h>
00073 
00074 UINT32 base_timer;              
00076 UINT8 sleep_mode = 0;   
00099 UINT8 net_buf[NETWORK_TX_BUFFER_SIZE];  /* Network transmit buffer      */
00100 
00101 /********************************************************************************
00102 Function:               strlen
00103 
00104 Parameters:             UINT8* str - start address of string buffer
00105                                 UINT16 len - buffer length
00106                                 
00107 Return val:             INT16 - (-1) Not a string
00108                                                 (>=0) Length of string
00109                                 
00110 Date:                   12.8.2002
00111 
00112 Desc:                   Calculates the length of given string
00113 *********************************************************************************/
00114 
00115 
00116 INT16 strlen (UINT8* buf, UINT16 len)
00117 {
00118         UINT16 i;
00119         
00120         for(i=0; i<len; i++) {
00121                 if(*buf == '\0')
00122                         return( i );
00123                 
00124                 buf++;
00125         }
00126         
00127         /* Not found    */
00128         
00129         return(-1);
00130 
00131 
00132 }
00133 
00134 
00135 /********************************************************************************
00136 Function:               bufsearch
00137 
00138 Parameters:             UINT8* startadr - start address of given buffer
00139                                 UINT16 len - buffer length
00140                                 UINT8* str - given searchstring
00141                                 
00142 Return val:             INT16 - (-1) Not found
00143                                                 (>=0) Start of matched string from startadr
00144                                 
00145 Date:                   12.7.2002
00146 
00147 Desc:                   Seeks given string from given buffer
00148 *********************************************************************************/
00149 
00150 INT16 bufsearch (UINT8* startadr, UINT16 len, UINT8* str)
00151 {
00152         UINT16 i;
00153         INT16 position;
00154         UINT8 matchesneeded;
00155         UINT8 matchesnow;
00156         UINT8* target;
00157         UINT8* key;
00158         
00159         target = startadr;
00160         position = -1;
00161         key = str;
00162         matchesnow = 0;
00163         matchesneeded = 0;
00164         
00165         /* How many matches we need?    */
00166         
00167         while( *key++ != '\0' ) {
00168                 /* Break possible deadlock      */
00169                 
00170                 matchesneeded++;
00171                 if(matchesneeded > 30)
00172                         return(-1);
00173         }
00174         
00175         /* Search for first mark and continue searching if found        */
00176         
00177         key = str;
00178         
00179         for(i=0; i<len; i++) {
00180                 if( *target == *key) {
00181                         /* We found matching character          */
00182                         
00183                         matchesnow++;
00184                         
00185                         /* Move to next character of key        */
00186                         
00187                         key++;
00188                         target++;
00189                         
00190                         if(matchesnow == 1) {
00191                                 /* First character match        */
00192                                 
00193                                 position = i;
00194                         }
00195                         
00196                         if(matchesneeded == matchesnow) {
00197                                 /* Whole string matched */
00198                                 
00199                                 return(position);
00200                         }
00201                         
00202                 } else {
00203                 
00204                         if( matchesnow != 0) {
00205                                 /* It wasn't a complete match...                                */
00206                                 /* Initialize counters and start again                  */
00207                         
00208                                 matchesnow = 0;
00209                                 key = str;
00210                         
00211                                 /* Move to next character of target after               */
00212                                 /* previous matching character                                  */
00213                         
00214                                 target = startadr;
00215                                 target += position;
00216                                 target += 1;
00217                         
00218                                 i = position;
00219                         } else {
00220                                 /* Just continue searching the first match              */
00221                                 target++;
00222                         }
00223                 }
00224         
00225         }
00226         
00227         /* No matches found...  */
00228         
00229         return(-1);
00230         
00231 }
00232 
00233 
00234 /********************************************************************************
00235 Function:               tolower
00236 
00237 Parameters:             UINT8 ch - ASCII character to be converted lowercase
00238                                 
00239 Return val:             UINT8 - converted character
00240                                 
00241 Date:                   21.8.2002
00242 
00243 Desc:                   If ch is UPPERCASE letter it is converted to lowercase and 
00244                                 returned. Otherwise original character is returned
00245 *********************************************************************************/
00246 
00247 UINT8 tolower (UINT8 ch)
00248 {
00249         if( (ch < 91) && (ch > 64) )
00250                 return(ch + 32);
00251         
00252         return(ch); 
00253 
00254 }
00255 
00256 
00257 /********************************************************************************
00258 Function:               toupper
00259 
00260 Parameters:             UINT8 ch - ASCII character to be converted UPPERCASE
00261                                 
00262 Return val:             UINT8 - converted character
00263                                 
00264 Date:                   21.8.2002
00265 
00266 Desc:                   If ch is lowercase letter it is converted to UPPERCASE and 
00267                                 returned. Otherwise original character is returned
00268 *********************************************************************************/
00269 
00270 UINT8 toupper (UINT8 ch)
00271 {
00272         if( (ch < 123) && (ch > 96) )
00273                 return(ch - 32);
00274         
00275         return(ch); 
00276 
00277 }
00278 
00279 /* Is the given ASCII code numerical    */
00280 /* e.g. '0','1','2' ... '9'                             */
00281 
00282 UINT8 isnumeric (UINT8 ch)
00283 {
00284         if( (ch < 58) && (ch > 47) )
00285                 return(1);
00286         return(0);
00287 }
00288 
00289 
00290 /* HexToAscii - Take one byte and return its two ASCII  */
00291 /* values for both nibbles                                                              */
00292 
00293 UINT16 hextoascii (UINT8 c)
00294 {
00295         UINT8 ch = c;
00296         UINT8 as1;
00297         UINT8 as2;
00298 
00299         /* take the char and turn it to ASCII */
00300         
00301         as1 = ch;
00302         as1 >>= 4;
00303         as1 &= 0x0F;
00304         if ( as1<10 )
00305                 as1 += 48;
00306         else
00307                 as1 += 55;
00308                 
00309         as2 = ch;
00310         as2 &= 0x0F;
00311         if ( as2<10 )
00312                 as2 += 48;
00313         else
00314                 as2 += 55;
00315                 
00316         return( ((UINT16)(as1)<<8) + as2 );
00317         
00318         
00319 }
00320 
00321 
00322 /* Convert ASCII character to numerical */
00323 /* e.g. '1' -> 0x01, 'A' ->0x0A                 */
00324 
00325 UINT8 asciitohex (UINT8 ch)
00326 {
00327         if( (ch < 58) && (ch > 47) )
00328                 return(ch - 48);
00329         
00330         if( (ch < 71 ) && (ch > 64) )
00331                 return(ch - 55); 
00332 }
00333 
00334 
00335 void ltoa (UINT32 nmbr, UINT8 *ch )
00336 {
00337         /* Transforms value of long word to ASCII string */
00338         /* Makes it iterative                                                    */
00339         
00340         UINT16 multiple;
00341         UINT32 decade,comp;
00342         UINT8 i,found;
00343         
00344         /* Init String */       
00345         
00346         for( i=0; i<10;i++ )
00347                 *ch++ = '0';
00348         
00349         ch -= 10;
00350         
00351         /* See if Zero */
00352         
00353         if(nmbr == 0) {
00354                 *ch++ = '0';
00355                 *ch = '\0';
00356         }
00357         
00358         
00359         decade = 1000000000;
00360         
00361         found = FALSE;
00362         
00363         for( i=0; i<10; i++) {
00364                 
00365                 if(i != 0)
00366                         decade /= 10;
00367                 
00368                 for( multiple=9; multiple>0; multiple--) {      
00369                         if( (i==0) && (multiple > 2) )
00370                                 continue;
00371                 
00372                         comp = decade * multiple;
00373                         
00374                         if(nmbr >= comp) {
00375                                 *ch = hextoascii(multiple);
00376                                 nmbr -= comp; 
00377                                 found = TRUE;
00378                                         
00379                                 break;                          /* Still processing */
00380                         }
00381                 }
00382                 
00383                 if( found == TRUE)
00384                         ch++;
00385         
00386         }       
00387 
00388         *ch = '\0';                     /* EOL */
00389         
00390 }
00391 
00392 
00393 
00394 
00395 void itoa (UINT16 nmbr, UINT8* ch )
00396 {
00397         /* Transforms value of word to ASCII string */
00398         /* Makes it iterative                                           */
00399 
00400         UINT16 decade, multiple;
00401         UINT32 comp;
00402         UINT8 i,found;
00403 
00404         /* Init String */
00405         
00406         
00407         for( i=0; i<5;i++)
00408                 *ch++ = '0';
00409         
00410         ch -= 5;
00411         
00412         /* See if Zero */
00413         
00414         if(nmbr == 0) {
00415                 *ch++ = '0';
00416                 *ch = '\0';
00417         }
00418         
00419         decade = 10000;
00420         
00421         found = FALSE;
00422         
00423         for( i=0; i<5; i++) {
00424                 
00425                 
00426                 if(i != 0)
00427                         decade /= 10;
00428                 
00429                 for( multiple=9; multiple>0; multiple--) {      
00430                         if( (i==0) && (multiple > 6) )
00431                                 continue;
00432                 
00433                         comp = decade * multiple;
00434                         
00435                         if(nmbr >= comp) {
00436                                 *ch = hextoascii(multiple);
00437                                 nmbr -= comp; 
00438                                 found = TRUE;
00439                                         
00440                                 break;                          /* Still processing */
00441                         }
00442                 }
00443                 
00444                 if( found == TRUE)
00445                         ch++;
00446         
00447         }       
00448 
00449         *ch = '\0';                     /* EOL */
00450         
00451 }
00452 
00453 
00454 /* Convert given buffer containing ASCII numbers        */
00455 /* to numerical positive INT16 value (max. 32767)       */
00456 
00457 INT16 atoi (UINT8 *buf, UINT8 buflen)
00458 {
00459         INT16 oval = 0;
00460         UINT8 nval = 0;
00461         
00462         while(buflen--) {
00463         
00464                 if(*buf == '\0')
00465                         break;
00466                 
00467                 if( isnumeric(*buf) == 0 )
00468                         return(-1);
00469         
00470                 nval = asciitohex(*buf++);
00471                 
00472                 oval = oval * 10;
00473                 oval += nval;   
00474                 
00475                 /* Overflow?    */
00476                 
00477                 if(oval < nval)
00478                         return(-1);
00479         
00480         }
00481         
00482         return(oval);
00483 
00484 }
00485 
00486 
00487 /* Debug/String output  */
00488 
00489 void mputs (UINT8* msg)
00490 {
00491         
00492         while( *msg !='\0')     {
00493                 sendchar(0,*msg);
00494                 msg++;
00495         }
00496 }
00497 
00498 /* Debug/Hex output a number*/
00499 void mputhex(UINT8 nbr) {
00500 
00501         UINT16 i;
00502         
00503         i=hextoascii(nbr);
00504         sendchar(0,i>>8);
00505         sendchar(0,(UINT8)i);
00506 }
00507 
00508 /*      Watchdog refresh        */
00509 
00510 void kick_WD (void) {
00511         WDTC_WTE=0;                                     
00512 }
00513 
00514 /* Wait for unaccurate use      */
00515 
00516 void wait (INT16 i)
00517 {
00518         for(;i;i--) kick_WD();
00519 }
00520 
00521 
00522 /* Return "Random" Number       */
00523 
00524 UINT32 random (void)
00525 {
00526         /* TODO: Return REAL random number      */
00527         return(0x345A2890);
00528 }
00529 
00530 /* Do nothing   */
00531 
00532 void dummy (void)
00533 {
00534         /* That's it    */
00535 }
00536 
00537 /*      Power saving mode       */
00538 
00539 void enter_power_save (void)
00540 {
00541         /* Are we on sleep mode already?        */
00542 
00543         if (sleep_mode)
00544                 return;
00545                 
00546         sleep_mode = 1;
00547         
00548         /* Shut down the RS transmitter chip    */
00549         
00550         PDR8_P82 = 0;
00551         
00552         /* Set the CPU to intermitted operation mode    */
00553         
00554         LPMCR = 0x1E;
00555         
00556         return;
00557 }
00558 
00559 
00560 
00561 void exit_power_save (void)
00562 {
00563         UINT8 i;
00564 
00565         if (sleep_mode) {
00566                 /* Release RS transmitter chip  */
00567 
00568                 PDR8_P82 = 1;
00569 
00570                 /* Set CPU to normal mode               */
00571 
00572                 LPMCR = 0x18;
00573                 
00574                 /* Wait for a while     */
00575                 
00576                 for( i=0; i<128; i++)
00577                         sleep_mode = 0;
00578                 
00579         }
00580         
00581 }
00582 
00583 

Generated on Sun Aug 3 20:33:00 2003 for OpenTCP by doxygen1.2.18