id.c 2.5 KB

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