Helper.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <string.h>
  2. #include "sys.h"
  3. #include "Helper.h"
  4. u32 Crc32Table[256];
  5. void Helper_CRC32_Init(void){
  6. u32 Crc;
  7. int i,j;
  8. for(i = 0;i < 256; i++)
  9. {
  10. Crc = (u32)i;
  11. for (j = 8; j > 0; j--)
  12. {
  13. if ((Crc & 1) == 1)
  14. Crc = (Crc >> 1) ^ 0xEDB88320;
  15. else
  16. Crc >>= 1;
  17. }
  18. Crc32Table[i] = Crc;
  19. }
  20. }
  21. uint32_t Helper_CRC_CalcBlockCRC8(uint8_t pBuffer[], uint32_t BufferLength){
  22. u32 value = 0xffffffff;
  23. for (int i = 0; i < BufferLength; i++)
  24. {
  25. value = (value >> 8) ^ Crc32Table[(value & 0xFF)^ pBuffer[i]];
  26. }
  27. return value ^ 0xffffffff;
  28. }
  29. uint32_t Helper_CRC_CalcFlashBlockCRC8(u32 flashStartAddr, uint32_t bytesLength)
  30. {
  31. u32 value = 0xffffffff;
  32. u32 u32Count=(bytesLength+sizeof(u32)-1)/sizeof(u32);
  33. u32 calcByteCount=0;
  34. u32Byte4 dwordData;
  35. for (int i = 0; i < u32Count; i++)
  36. {
  37. dwordData.u32_Data=(*(vu32*)(flashStartAddr+i*sizeof(u32)));
  38. for(int j=0;j<sizeof(u32);j++)
  39. {
  40. if (calcByteCount<bytesLength)
  41. {
  42. value = (value >> 8) ^ Crc32Table[(value & 0xFF)^ dwordData.bytes[j]];
  43. calcByteCount++;
  44. }
  45. else
  46. break;
  47. }
  48. }
  49. return value ^ 0xffffffff;
  50. }
  51. uint16_t Helper_Sum16(uint8_t *buffer, uint32_t length){
  52. uint16_t result=0;
  53. for(int i=0;i<length;i++){
  54. result+=(uint16_t)buffer[i];
  55. }
  56. return result;
  57. }
  58. u8 Helper_CheckInvertBytesIsRegist(u8 * pData,u16 totalLength)
  59. {
  60. if (totalLength==0)
  61. return 1;
  62. if ((totalLength % 2)!=0)
  63. return 0;
  64. u16 bytes=totalLength/2;
  65. for(int i=0;i<bytes;i++)
  66. {
  67. if (((~pData[i]) & 0xff)!=pData[i+bytes])
  68. return 0;
  69. }
  70. return 1;
  71. }
  72. void Helper_ConvertInvert(u8 * pData,u16 sourceBytes)
  73. {
  74. for(int i=0;i<sourceBytes;i++)
  75. pData[i+sourceBytes]=(~pData[i]) & 0xff;
  76. }
  77. u8 Helper_CheckRightRecvedData(u8 *pData,u16 dataLength,u16 exceptLength,u8 bIsContentBitInverted_Recv)
  78. {
  79. if (dataLength==0)
  80. return 1;
  81. u8 bDataOK=1;
  82. u8 validDataLength=dataLength;
  83. if (bIsContentBitInverted_Recv)
  84. {
  85. validDataLength=dataLength/2;
  86. if (!Helper_CheckInvertBytesIsRegist(pData,dataLength))
  87. bDataOK=0;
  88. }
  89. if (validDataLength!=exceptLength)
  90. bDataOK=0;
  91. return bDataOK;
  92. }