X508_IicTestDrv.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //*********************************************
  2. // iic 通讯测试库函数v22
  3. //*********************************************
  4. //用户需要将本程序编译为库函数调用,以便后期处理加密驱动。
  5. #include<linux/fs.h>
  6. #define XS508_DT1 1 //1ms
  7. int XS_flag=0;
  8. //!!!用户需要完成以下4个函数,以便X508调用。
  9. //XS508I2C地址为0x29,对应的写指令为0x52,读指令为0x53;通讯速度100K。
  10. //请用户将buffer中25个数据,自XS508的0x07处连续写出,并检测每次XS508的有效应答。
  11. extern int HI2C_write25Byte( unsigned char *buffer);
  12. //请用户自XS508的0x00处连续读入32个字节数据,存入buffer,
  13. //期间在发写指令0x52、xs508内指针0x00和读指令0x53时,应检测XS508的3次有效应答。
  14. extern int HI2C_read32Byte(unsigned char *buffer);
  15. //ms延时偏差不得超过10%
  16. extern void XS508_delay_ms(int D_TIME);
  17. //请用户结合主控生成随机数
  18. extern int get_xs_srand(void);
  19. *********************************************/
  20. //extern int printf(char *frm,...);
  21. /*********************************************
  22. // XS508 Communication Test Driver
  23. // IN: 16BYTE KEY & 16BYTE ID
  24. // OUT: 0->PASS
  25. // -1->I2C WRITE OR READ ERROR
  26. // -2->NG
  27. *********************************************/
  28. int XS508_I2C_Handshake(unsigned char *XS508_16B_Ukey,unsigned char *XS508_16B_ID)
  29. {
  30. unsigned char wr_data_25_byte[25];
  31. unsigned char rd_data_32_byte[32]={0};
  32. int i,check_cnt;
  33. volatile int xs_curr_time;
  34. int debugset = 0;//0xa8;
  35. int t1;
  36. if (XS_flag ==0)
  37. {
  38. t1 = xs_curr_time & 0xFF;
  39. //xs_curr_time += get_xs_srand();
  40. //srand(xs_curr_time);
  41. while (t1--)
  42. {
  43. //rand();
  44. get_xs_srand();
  45. }
  46. XS_flag=1;
  47. }
  48. /*
  49. if (XS_flag ==0)
  50. {
  51. SysTick->CTRL&=0xfffffffb;
  52. xs_curr_time+=SysTick->VAL;
  53. srand(xs_curr_time);
  54. XS_flag=1;
  55. }
  56. */
  57. //--------- initilize ------------------
  58. if (debugset==0xa8)
  59. printk(KERN_ALERT "T_I2C write BYTES =\r\n");
  60. for(i=1;i<25;i++)
  61. {
  62. //wr_data_25_byte[i]=i;
  63. //wr_data_25_byte[i]+=(char)rand();
  64. wr_data_25_byte[i]+=(char)get_xs_srand();
  65. }
  66. for(i=0;i<16;i++)
  67. {
  68. wr_data_25_byte[i+4]+=XS508_16B_Ukey[i];
  69. }
  70. if ((wr_data_25_byte[23]==0x5a)|| (wr_data_25_byte[23]==0xa5) )
  71. wr_data_25_byte[23]=0x7a;
  72. wr_data_25_byte[0]=0xF2;
  73. if (debugset==0xa8)
  74. {
  75. for(i=0;i<25;i++)
  76. printk(KERN_ALERT "0x%2x,",wr_data_25_byte[i]);
  77. printk(KERN_ALERT "\n");
  78. }
  79. //------ step1: prapare 25 bytes data wr_data_25_byte[] -----------------------
  80. check_cnt=0;
  81. while(HI2C_write25Byte(wr_data_25_byte)) //0: i2c ACK , others: NACK
  82. {
  83. XS508_delay_ms(XS508_DT1);
  84. check_cnt++;
  85. if (check_cnt>100)
  86. {
  87. if (debugset==0xa8)
  88. printk(KERN_ALERT "X508 I2C Write error \n");
  89. return -1;
  90. }
  91. }
  92. //------- step2: write 25 bytes to xs508 -----
  93. XS508_delay_ms(10);//delay 10ms
  94. //------- step3: wait xs508 dispose -----
  95. check_cnt=0;
  96. while(HI2C_read32Byte(rd_data_32_byte)) //0: i2c ACK , others: NACK
  97. {
  98. XS508_delay_ms(XS508_DT1);//
  99. check_cnt++;
  100. if (check_cnt > 100)
  101. {
  102. if (debugset==0xa8)
  103. printk(KERN_ALERT "XS508 I2C Read error \n");
  104. return -1;
  105. }
  106. }
  107. //------- step4: read 32 bytes data from xs508 -----
  108. XS508_delay_ms(100); //delay 100ms
  109. if (debugset==0xa8)
  110. {
  111. printk(KERN_ALERT "XS508_I2C READ 32 BYTES =\r\n");
  112. for(i=0;i<32;i++)
  113. printk(KERN_ALERT "0x%2x,",rd_data_32_byte[i]);
  114. printk(KERN_ALERT "\n");
  115. }
  116. for(i=0;i<24;i++)
  117. {
  118. if (wr_data_25_byte[1+i]!=(rd_data_32_byte[8+i]^0xaa) )
  119. {
  120. if (debugset==0xa8)
  121. printk(KERN_ALERT "no(%d): 0x%2x _xor_0xaa != 0x%2x ",i,wr_data_25_byte[1+i],rd_data_32_byte[8+i]);
  122. if (debugset==0xa8)
  123. printk(KERN_ALERT "IIC_DATA_ERR\r\n");
  124. return -2; //verify error
  125. }
  126. }
  127. if (debugset==0xa8)
  128. printk(KERN_ALERT "---XS508E_I2C success!---\r\n");
  129. return 0;
  130. }