utils.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C) 2003-2007 by egnite Software GmbH
  3. * Copyright (C) 2010 by egnite GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. *
  35. */
  36. /*
  37. * $Id: utils.c 2935 2010-04-01 12:14:17Z haraldkipp $
  38. */
  39. #include "utils.h"
  40. /*!
  41. * \brief Delay by executing a given number of nops.
  42. */
  43. void MicroDelay(unsigned int nops)
  44. {
  45. while (nops--) {
  46. _NOP();
  47. _NOP();
  48. _NOP();
  49. _NOP();
  50. _NOP();
  51. _NOP();
  52. _NOP();
  53. _NOP();
  54. }
  55. }
  56. /*!
  57. * \brief Delay by executing a given number of nops, multiplied by 100000.
  58. */
  59. void Delay(unsigned char val)
  60. {
  61. unsigned char i;
  62. for (i = 0; i < val; i++) {
  63. MicroDelay(0xFFFF);
  64. MicroDelay(0xFFFF);
  65. MicroDelay(0xFFFF);
  66. }
  67. }
  68. void strcpy_(char *dst, const char *src)
  69. {
  70. while (*src) {
  71. *dst++ = *src++;
  72. }
  73. *dst = 0;
  74. }
  75. void memcpy_(unsigned char *dst, const unsigned char *src, unsigned char len)
  76. {
  77. while (len--) {
  78. *dst++ = *src++;
  79. }
  80. }
  81. int memcmp_(const unsigned char *dst, const unsigned char *src, unsigned char len)
  82. {
  83. while (len--) {
  84. if (*dst != *src) {
  85. return -1;
  86. }
  87. }
  88. return 0;
  89. }
  90. void memset_(unsigned char *dst, unsigned char val, unsigned char len)
  91. {
  92. while (len--) {
  93. *dst++ = val;
  94. }
  95. }
  96. int hex2bin(char c)
  97. {
  98. if (c >= '0' && c <= '9')
  99. return c - '0';
  100. if (c >= 'a' && c <= 'f')
  101. return c - 'a' + 10;
  102. if (c >= 'A' && c <= 'F')
  103. return c - 'A' + 10;
  104. return 0;
  105. }
  106. unsigned long inet_addr(char *str)
  107. {
  108. unsigned short num;
  109. unsigned long addr = 0;
  110. unsigned char parts = 0;
  111. unsigned char *ap;
  112. ap = (unsigned char *) &addr;
  113. while (parts < 4) {
  114. if (*str < '0' || *str > '9') {
  115. break;
  116. }
  117. for (num = 0; num <= 255;) {
  118. num = (num * 10) + (*str - '0');
  119. if (*++str < '0' || *str > '9') {
  120. break;
  121. }
  122. }
  123. if (num > 255) {
  124. break;
  125. }
  126. parts++;
  127. *ap++ = (unsigned char) num;
  128. if (*str != '.') {
  129. if (parts == 4) {
  130. return addr;
  131. }
  132. break;
  133. }
  134. str++;
  135. }
  136. return -1;
  137. }
  138. char *inet_ntoa(unsigned long addr)
  139. {
  140. static char str[16];
  141. char inv[3];
  142. char *rp;
  143. unsigned char *ap;
  144. unsigned char rem;
  145. unsigned char n;
  146. unsigned char i;
  147. rp = str;
  148. ap = (unsigned char *) &addr;
  149. for (n = 0; n < 4; n++) {
  150. i = 0;
  151. do {
  152. rem = *ap % (unsigned char) 10;
  153. *ap /= (unsigned char) 10;
  154. inv[i++] = '0' + rem;
  155. } while (*ap);
  156. while (i--)
  157. *rp++ = inv[i];
  158. *rp++ = '.';
  159. ap++;
  160. }
  161. *--rp = 0;
  162. return str;
  163. }