id.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*******************************************************************
  2. * id.c
  3. *
  4. ********************************************************************/
  5. #define THIS_FILE_ID 3
  6. /* Includes ------------------------------------------------------------------*/
  7. #include <includes.h>
  8. #define ID_BASE_ADDR (0x1FFFF7E8ul)
  9. //
  10. //unsigned char aucSTM32ID1[12];
  11. //unsigned char aucSTM32SN1[12];
  12. //unsigned char aucSTM32ID2[12];
  13. //unsigned char aucSTM32SN2[12];
  14. //unsigned char aucSTM32ID3[12];
  15. //unsigned char aucSTM32SN3[12];
  16. //unsigned char g_aucSTM32ID[12];
  17. //
  18. ///***************************************************************
  19. //*
  20. //***************************************************************/
  21. //static const u32 apulSTM32IDAddr1[3]={
  22. // 0x115a7ef8ul,0x1F00123ul,ID_BASE_ADDR
  23. //};
  24. //static const unsigned char apucSNTab1[12]={
  25. // 0x12,0x98,0x32,0x76,0xa1,0x8b,0xf1,0x13,0x5a,0x21,0x01,0x32
  26. //};
  27. ///*
  28. //index must be 1
  29. //*/
  30. //void GetSTM32ID1(int index)
  31. //{
  32. // int i=index;
  33. // int j=i+1;
  34. // unsigned char data;
  35. // unsigned char *p;
  36. // u32 addr=apulSTM32IDAddr1[j];
  37. // for(i=0;i<12;i++){
  38. // p= (unsigned char *)addr;
  39. // data=*p;
  40. // aucSTM32ID1[i]=data ^ apucSNTab1[i];
  41. // p++;
  42. // }
  43. //}
  44. //void CheckSTM32ID1(void)
  45. //{
  46. // int i;
  47. // //read from eeprom
  48. //// EEPROM_ReadS(EEPROM_ID_SN1,aucSTM32SN1,12);
  49. // //check
  50. // for(i=0;i<12;i++){
  51. // if(aucSTM32SN1[i]!=aucSTM32ID1[i])break;
  52. // }
  53. // if(i!=12){
  54. // //while(1);
  55. // for(i=0;i<1000;i++){
  56. // if(i==900)i=0;
  57. // }
  58. // }
  59. //}
  60. //
  61. //
  62. //
  63. //
  64. //void GetSTM32ID(unsigned char *pID);
  65. //void CreateKey(unsigned char *pKey, unsigned char *pID);
  66. //unsigned char SaveKey(unsigned char *pKey);
  67. /***********************************************************
  68. * 函数:GetSTM32ID
  69. * 参数:1、*pID 读取的ID存放指针,12字节ID
  70. * 功能:获取本芯片内部出厂绑定的ID
  71. ***********************************************************/
  72. void GetSTM32ID(unsigned char *pID)
  73. {
  74. unsigned char i = 0;
  75. u32 addr = ID_BASE_ADDR;
  76. for(i = 0; i < 12; i++)
  77. {
  78. *pID = *(u32 *)addr;
  79. pID++;
  80. addr++;
  81. }
  82. }
  83. /************************************************************
  84. *
  85. *************************************************************/
  86. unsigned char GetRandBySTM32ID(void)
  87. {
  88. unsigned char id[12];
  89. unsigned char rand=0x58;//种子
  90. int i;
  91. GetSTM32ID(id);
  92. for(i=0;i<12;i++){
  93. rand^=id[i];
  94. }
  95. if(rand==0x00)rand=0x58;
  96. if(rand==0xff)rand=0x85;
  97. return rand;
  98. }
  99. /*********************************************************************/