Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

133 rader
3.2 KiB

  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief This module contains common APIs declarations.
  6. *
  7. * Copyright (c) 2015 Atmel Corporation. All rights reserved.
  8. *
  9. * \asf_license_start
  10. *
  11. * \page License
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. *
  23. * 3. The name of Atmel may not be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  27. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  29. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  30. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  34. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * \asf_license_stop
  39. *
  40. */
  41. #include "common/include/nm_common.h"
  42. void m2m_memcpy(uint8 *pDst, uint8 *pSrc, uint32 sz)
  43. {
  44. if (sz == 0)
  45. return;
  46. do {
  47. *pDst = *pSrc;
  48. pDst++;
  49. pSrc++;
  50. } while (--sz);
  51. }
  52. uint8 m2m_checksum(uint8 *buf, int sz)
  53. {
  54. uint8 cs = 0;
  55. while (--sz) {
  56. cs ^= *buf;
  57. buf++;
  58. }
  59. return cs;
  60. }
  61. void m2m_memset(uint8 *pBuf, uint8 val, uint32 sz)
  62. {
  63. if (sz == 0)
  64. return;
  65. do {
  66. *pBuf = val;
  67. pBuf++;
  68. } while (--sz);
  69. }
  70. uint16 m2m_strlen(uint8 *pcStr)
  71. {
  72. uint16 u16StrLen = 0;
  73. while (*pcStr) {
  74. u16StrLen++;
  75. pcStr++;
  76. }
  77. return u16StrLen;
  78. }
  79. uint8 m2m_strncmp(uint8 *pcS1, uint8 *pcS2, uint16 u16Len)
  80. {
  81. for (; u16Len > 0; pcS1++, pcS2++, --u16Len)
  82. if (*pcS1 != *pcS2)
  83. return ((*(uint8 *)pcS1 < *(uint8 *)pcS2) ? -1 : +1);
  84. else if (*pcS1 == '\0')
  85. return 0;
  86. return 0;
  87. }
  88. /* Finds the occurance of pcStr in pcIn.
  89. If pcStr is part of pcIn it returns a valid pointer to the start of pcStr within pcIn.
  90. Otherwise a NULL Pointer is returned.
  91. */
  92. uint8 *m2m_strstr(uint8 *pcIn, uint8 *pcStr)
  93. {
  94. uint8 u8c;
  95. uint16 u16StrLen;
  96. u8c = *pcStr++;
  97. if (!u8c)
  98. return (uint8 *)pcIn; // Trivial empty string case
  99. u16StrLen = m2m_strlen(pcStr);
  100. do {
  101. uint8 u8Sc;
  102. do {
  103. u8Sc = *pcIn++;
  104. if (!u8Sc)
  105. return (uint8 *)0;
  106. } while (u8Sc != u8c);
  107. } while (m2m_strncmp(pcIn, pcStr, u16StrLen) != 0);
  108. return (uint8 *)(pcIn - 1);
  109. }
  110. sint8 m2m_memcmp(uint8 *pu8Buff1, uint8 *pu8Buff2, uint32 u32Size)
  111. {
  112. uint32 i;
  113. sint8 s8Result = 0;
  114. for (i = 0; i < u32Size; i++) {
  115. if (pu8Buff1[i] != pu8Buff2[i]) {
  116. s8Result = 1;
  117. break;
  118. }
  119. }
  120. return s8Result;
  121. }