-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c-core.c
executable file
·715 lines (619 loc) · 15.8 KB
/
i2c-core.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
//#include <sys/stat.h>
//#include <sys/time.h>
#include <sys/ioctl.h>
#include "i2c-core.h"
void delay_us(int utime)
{
#if 0
usleep(utime);
#elif 0
volatile long int i, j;
for(j = 0; j <6000; )
j += 1;
#else
volatile long long i, j;
for(i = 0; i < utime; i++)
for(j = 0; j < 35; )
j += 1;
#endif
}
void delay_ms(int mtime)
{
#if 1
int i;
for(i = 0; i < 1000; i++)
delay_us(mtime);
#else
int i, j;
for(i = 0; i < utime*8; i++)
for(j = 0; j < 60*47; j++)
;
#endif
}
char gp_gpio_open_flag[24*8] = {0};
#if 0
int gpio_export(int gpio_index)
{
if( !gp_gpio_open_flag[gpio_index] )
{
char path_buf[64] = {0};
snprintf( path_buf, 64, "/sys/class/gpio/gpio%d", (unsigned int)gpio_index );
if(0 != access( path_buf, F_OK)){
FILE* pGpio = fopen("/sys/class/gpio/export", "w");
if( pGpio ){
char ibuf[16] = {0};
snprintf( ibuf, 16, "%d", gpio_index );
fwrite( ibuf, 1, strlen(ibuf), pGpio );
fclose( pGpio );
usleep(10);
if(0 == access( path_buf, F_OK)){
gp_gpio_open_flag[gpio_index] = 1;
}else{
KPRINT("error gpio path exist %s\n", path_buf );
}
}else{
KPRINT("error open path /sys/class/gpio/export\n" );
}
}
}
return 0;
}
/*
* Set the direction of the single bit for the specific path
* dirbit=1 means output
* dirbit=0 means input
*/
int gpio_dirsetbit(int gpio_index, unsigned int dirbit)
{
int ret = -1;
char path_buf[64] = {0};
FILE* pGpio = NULL;
gpio_export(gpio_index);
snprintf( path_buf, 64, "/sys/class/gpio/gpio%d/direction", (unsigned int)gpio_index );
pGpio = fopen(path_buf, "w");
if( pGpio ){
if( dirbit ){
char ibuf[8] = {"out"};
fwrite( ibuf, 1, 3, pGpio );
fclose( pGpio );
}else{
char ixbuf[8] = {"in"};
fwrite( ixbuf, 1, 2, pGpio );
fclose( pGpio );
}
}else{
KPRINT("error open path %s\n", path_buf );
return -1;
}
return 0;
}
/*
* Bit write the value from a specific bit of a gpio path
*/
int gpio_writebit(int gpio_index, unsigned int bitvalue)
{
char path_buf[64] = {0};
FILE* pGpio = NULL;
gpio_export(gpio_index);
snprintf( path_buf, 64, "/sys/class/gpio/gpio%d/value", (unsigned int)gpio_index );
pGpio = fopen(path_buf, "w");
if( pGpio ){
char ibuf[8] = {0};
snprintf( ibuf, 8, "%d", bitvalue ? 1 : 0 );
fwrite( ibuf, 1, 1, pGpio );
fclose( pGpio );
}else{
KPRINT("error open path %s\n", path_buf );
return -1;
}
return 0;
}
/*
* Bit read the value from a specific bit of a gpio path
*/
int gpio_readbit(int gpio_index, unsigned int * readbit)
{
unsigned int readvalue;
FILE* pGpio = NULL;
char path_buf[64] = {0};
gpio_export(gpio_index);
snprintf( path_buf, 64, "/sys/class/gpio/gpio%d/value", (unsigned int)gpio_index );
pGpio = fopen(path_buf, "r");
if( pGpio ){
char rbuf[1] = {0};
int rlenx = 0;
rlenx = fread( rbuf, 1, 1, pGpio );
fclose( pGpio );
if( 1 == rlenx ){
if( '1' == rbuf[0] ){
readvalue = 1;
}else{
readvalue = 0;
}
}else{
KPRINT("error read buf %s\n", path_buf );
return -1;
}
}else{
KPRINT("error open path %s\n", path_buf );
return -1;
}
usleep(5);
*readbit = readvalue;
return 0;
}
#else
static int gpio_export(int gpio_index)
{
int fd = -1;
//printf("set export \n");
if( !gp_gpio_open_flag[gpio_index] )
{
char path_buf[64] = {0};
snprintf( path_buf, 64, "/sys/class/gpio/gpio%d", (unsigned int)gpio_index );
if(0 != access( path_buf, F_OK)){
fd = open("/sys/class/gpio/export", O_WRONLY |O_NOCTTY | O_NONBLOCK);
if( fd > 0 ){
char ibuf[16] = {0};
snprintf( ibuf, 16, "%d", gpio_index );
write( fd, ibuf, strlen(ibuf));
close( fd );
//usleep(5);
delay_us(5);
if(0 == access( path_buf, F_OK)){
gp_gpio_open_flag[gpio_index] = 1;
}else{
KPRINT("error gpio path exist %s\n", path_buf );
}
}else{
KPRINT("error open path /sys/class/gpio/export\n" );
}
}
}
return 0;
}
/*
* Set the direction of the single bit for the specific path
* dirbit=1 means output
* dirbit=0 means input
*/
static int gpio_dirsetbit(int gpio_index, unsigned int dirbit)
{
int ret = -1;
int fd = -1;
char path_buf[64] = {0};
//printf("set dir \n");
gpio_export(gpio_index);
snprintf( path_buf, 64, "/sys/class/gpio/gpio%d/direction", (unsigned int)gpio_index );
fd= open(path_buf, O_RDWR |O_NOCTTY | O_NONBLOCK);
if( fd > 0 ){
if( dirbit ){
char ibuf[8] = {"out"};
write( fd, ibuf, strlen(ibuf) );
close( fd );
}else{
char ixbuf[8] = {"in"};
write( fd, ixbuf, strlen(ixbuf) );
close( fd );
}
}else{
KPRINT("error open path %s\n", path_buf );
return -1;
}
return 0;
}
/*
* Bit write the value from a specific bit of a gpio path
*/
static int gpio_writebit(int gpio_index, unsigned int bitvalue)
{
int fd = -1;
char path_buf[64] = {0};
//printf("set write\n");
gpio_export(gpio_index);
snprintf( path_buf, 64, "/sys/class/gpio/gpio%d/value", (unsigned int)gpio_index );
fd = open(path_buf, O_RDWR |O_NOCTTY | O_NONBLOCK);
if( fd > 0 ){
char ibuf[8] = {0};
snprintf( ibuf, 8, "%d", bitvalue ? 1 : 0 );
write(fd, ibuf, strlen(ibuf));
close( fd );
}else{
KPRINT("error open path %s\n", path_buf );
return -1;
}
return 0;
}
/*
* Bit read the value from a specific bit of a gpio path
*/
static int gpio_readbit(int gpio_index, unsigned int * readbit)
{
int fd = -1;
unsigned int readvalue;
char path_buf[64] = {0};
//printf("set read\n");
gpio_export(gpio_index);
snprintf( path_buf, 64, "/sys/class/gpio/gpio%d/value", (unsigned int)gpio_index );
fd = open(path_buf, O_RDWR |O_NOCTTY | O_NONBLOCK);
if( fd > 0 ){
char rbuf[1] = {0};
int rlenx = 0;
rlenx = read( fd, rbuf, 1);
close( fd );
if( 1 == rlenx ){
if( '1' == rbuf[0] ){
readvalue = 1;
}else{
readvalue = 0;
}
}else{
KPRINT("error read buf %s\n", path_buf );
return -1;
}
}else{
KPRINT("error open path %s\n", path_buf );
return -1;
}
delay_us(2);
//usleep(2);
*readbit = readvalue;
return 0;
}
#endif
int i2c_gpio_init(void)
{
int ret;
KPRINT( "version - 201506041126" );
KPRINT( "+->TD35xx\n" );
#if 0
struct timeval tvx={0};
struct timeval tvy={0};
unsigned long long ullx = 0;
unsigned long long ully = 0;
gettimeofday(&tvx, 0);
delay_us(1);
gettimeofday(&tvy, 0);
ullx = tvx.tv_sec;
ullx *= 1000;
ullx *= 1000;
ullx += tvx.tv_usec;
ully = tvy.tv_sec;
ully *= 1000;
ully *= 1000;
ully += tvy.tv_usec;
KPRINT("cm_Delay = %llu\n", (ully -ullx) );
#endif
#if 0
ret = system( "himm 0x1f0010e4 0x1400" );//set gpio0_1
if ( -1 == ret )
{
KPRINT("error himm 0x1f0010e4 0x1400\n");
}
ret = system( "himm 0x1f0010e8 0x1400" );//set gpio0_2
if ( -1 == ret )
{
KPRINT("error himm 0x1f0010e8 0x1400\n");
}
#else
ret = system( "himm 0x1f0000dc 0x1500" );
if ( -1 == ret )
{
KPRINT("error himm 0x1f0000dc 0x1500\n");
}
ret = system( "himm 0x1f0000e0 0x1400" );
if ( -1 == ret )
{
KPRINT("error himm 0x1f0000dc 0x1400\n");
}
#endif
KPRINT("ATSHA204A CryptoMemory Device Driver v1.0.0: %s %s\n", __DATE__, __TIME__);
return 0;
}
#define GPIO_NUB(gpio_grupo, gpio_bits) (gpio_grupo * 8 + gpio_bits)
#if 0
static unsigned int ENCRY_GPIO_SDA = GPIO_NUB(0, 2); //GPIO0_2
static unsigned int ENCRY_GPIO_SCL = GPIO_NUB(0, 1); //GPIO0_1
#else
static unsigned int ENCRY_GPIO_SDA = GPIO_NUB(13, 2); //GPIO13_2
static unsigned int ENCRY_GPIO_SCL = GPIO_NUB(13, 3); //GPIO13_3
#endif
#if 1
#define I2C_CLK_OUT gpio_dirsetbit(ENCRY_GPIO_SCL, 1)
#define I2C_CLK_H gpio_writebit(ENCRY_GPIO_SCL, 1)
#define I2C_CLK_L gpio_writebit(ENCRY_GPIO_SCL, 0)
#define I2C_SDA_OUT gpio_dirsetbit(ENCRY_GPIO_SDA, 1)
#define I2C_SDA_IN gpio_dirsetbit(ENCRY_GPIO_SDA, 0)
#define I2C_SDA_H gpio_writebit(ENCRY_GPIO_SDA, 1)
#define I2C_SDA_L gpio_writebit(ENCRY_GPIO_SDA, 0)
#else
void clk_out(void){gpio_dirsetbit(ENCRY_GPIO_SCL, 1);};
void clk_h(void){gpio_writebit(ENCRY_GPIO_SCL, 1);};
void clk_l(void){gpio_writebit(ENCRY_GPIO_SCL, 0);};
void sda_out(void){gpio_dirsetbit(ENCRY_GPIO_SDA, 1);};
void sda_in(void){gpio_dirsetbit(ENCRY_GPIO_SDA, 0);};
void sda_h(void){gpio_writebit(ENCRY_GPIO_SDA, 1);};
void sda_l(void){gpio_writebit(ENCRY_GPIO_SDA, 0);};
void I2C_CLK_OUT = clk_out();
void I2C_CLK_H = clk_h();
void I2C_CLK_L = clk_l();
void *(I2C_SDA_OUT)(void) = &sda_out();
void I2C_SDA_IN = sda_in();
void I2C_SDA_H = sda_h();
void I2C_SDA_L = sda_l();
#endif
// 1/2 Clock Cycle transition to HIGH
static void i2c_clk_high(void)
{
delay_us(2);
I2C_CLK_H;
delay_us(4);
}
// 1/2 Clock Cycle transition to LOW
static void i2c_clk_low(void)
{
delay_us(4);
I2C_CLK_L;
delay_us(2);
}
// Do one full clock cycle
// Changed 1/19/05 to eliminate one level of return stack requirements
static void i2c_clk_clock(void)
{
delay_us(2);
I2C_CLK_L;
delay_us(4);
I2C_CLK_H;
delay_us(2);
}
// Send a start sequence
// Modified 7-21-04 to correctly set SDA to be an output
static void i2c_start(void)
{
I2C_SDA_OUT; // Data line must be an output to send a start sequence
delay_us(10);
i2c_clk_low();
I2C_SDA_H;
delay_us(8);
i2c_clk_high();
delay_us(8);
I2C_SDA_L;
delay_us(8);
i2c_clk_low();
delay_us(8);
}
// Send a stop sequence
// Modified 7-21-04 to correctly set SDA to be an output
static void i2c_stop(void)
{
I2C_SDA_OUT; // Data line must be an output to send a stop sequence
delay_us(1020);
i2c_clk_low();
I2C_SDA_L;
delay_us(8);
i2c_clk_high();
delay_us(8);
I2C_SDA_H;
delay_us(8);
}
// Do a number of clock cycles
static void i2c_clk_clock_cycles(uint8_t ucCount)
{
uint8_t i;
for (i = 0; i < ucCount; ++i)
i2c_clk_clock();
}
static void i2c_wait_clock(uint8_t loop)
{
uint8_t i, j;
I2C_SDA_L;
for(j=0; j<loop; j++){
i2c_start();
for(i = 0; i<15; i++)
i2c_clk_clock();
i2c_stop();
}
}
// Send a ACK or NAK or to the device
static void back_ack_nak(uint8_t ucAck)
{
I2C_SDA_OUT; // Data line must be an output to send an ACK
delay_us(10);
i2c_clk_low();
if (ucAck)
I2C_SDA_L; // Low on data line indicates an ACK
else
I2C_SDA_H; // High on data line indicates an NACK
delay_us(10);
i2c_clk_high();
delay_us(10);
i2c_clk_low();
}
// Write a byte
// Returns 0 if write successed, 1 if write fails failure
// Modified 7-21-04 to correctly control SDA
static uint8_t i2c_write(uint8_t ucData)
{
uint8_t i;
unsigned int value;
I2C_SDA_OUT; // Set data line to be an output
delay_us(10);
for(i=0; i<8; i++){ // Send 8 bits of data
i2c_clk_low();
if (ucData & 0x80)
I2C_SDA_H;
else
I2C_SDA_L;
i2c_clk_high();
ucData = ucData<<1;
}
i2c_clk_low();
// wait for the ack
I2C_SDA_IN; // Set data line to be an input
delay_us(10);
i2c_clk_high();
while(i>1) { // loop waiting for ack (loop above left i == 8)
delay_us(10);
gpio_readbit(ENCRY_GPIO_SDA, &value);
//KPRINT("read bit %d,%d\n", i, value);
if (value == 1)
i--;
else
i = 0;
}
i2c_clk_low();
I2C_SDA_OUT; // Set data line to be an output
delay_us(20);
return i;
}
// Read a byte from device, MSB
// Modified 7-21-04 to correctly control SDA
static uint8_t i2c_read(void)
{
uint8_t i;
unsigned int value;
uint8_t rByte = 0;
I2C_SDA_IN; // Set data line to be an input
I2C_SDA_H;
delay_us(10);
for(i=0x80; i; i=i>>1){
i2c_clk_clock();
gpio_readbit(ENCRY_GPIO_SDA, &value);
if (value == 1)
rByte |= i;
i2c_clk_low();
}
I2C_SDA_OUT; // Set data line to be an output
delay_us(10);
return rByte;
}
static uint8_t i2c_receive_data(uint8_t ucLen, uint8_t* pucRecBuf)
{
int i;
for(i = 0; i < (ucLen-1); i++){
pucRecBuf[i] = i2c_read();
back_ack_nak(TRUE);
}
pucRecBuf[i] = i2c_read();
back_ack_nak(FALSE);
i2c_stop();
return SUCCESS;
}
// Send a byte
static uint8_t i2c_send_byte(uint8_t cmd)
{
uint8_t i;
i = CM_START_TRIES;
while (i) {
if (i2c_write(cmd) == 0)
break;
if (--i == 0)
return FAIL_CMDSTART;
}
return SUCCESS;
}
static uint8_t i2c_send_data(uint8_t ucLen, uint8_t* pucSendBuf)
{
int i;
for(i = 0; i< ucLen; i++) {
if (i2c_send_byte(pucSendBuf[i]) == 1)
return FAIL_WRDATA;
}
return SUCCESS;
}
/***************************************************************************************
功能:sha204-wakeup
说明:sha204-I2C模式唤醒。SDA由High变Low,并低保持>60us,后发送一字节0x00,SDA保持高电平>3MS
***************************************************************************************/
static void sha204p_wakeup(void)
{
i2c_start(); //开I2C通信,SDA拉低
delay_us(70); //SDA保持低电平>60us
i2c_send_byte(0x00); //发一字节数据
i2c_stop(); //关I2C通信,SDA拉高
delay_ms(50); //SDA保持低电平>3MS
}
/***************************************************************************************
功能:sha204-send_command
说明:sha204-I2C模式发数据包
***************************************************************************************/
uint8_t sha204p_send_command(uint8_t count, uint8_t *command)
{
uint8_t result=0; //开I2C通信
i2c_start();
while(i2c_send_byte(0xc8)); //sha204-写地址
while(i2c_send_byte(0x03)); //sha204-命令-正常数据通信方式
while(i2c_send_data(count,command)); //数据包发送
i2c_stop();
return result;
}
/***************************************************************************************
功能:sha204-read
说明:sha204-I2C模式读数据包
***************************************************************************************/
uint8_t sha204p_receive_response(uint8_t size, uint8_t *response)
{
uint8_t count=0;
uint8_t result=0;
i2c_start(); //开I2C通信
i2c_send_byte(0xc9); //sha204-读地址
response[0]=i2c_read(); //读第一个字节-数据长度
back_ack_nak(TRUE); //发应答
count = response[0]; //判断数据长度是否符合要求
if ((count < size) || (count > size)) {
i2c_stop();
result=1;
}else{
i2c_receive_data(count - 1, &response[1]); //接收剩余数据
}
return result;
}
/***************************************************************************************
功能:sha204-wakeup
说明:OUT-*response 为4字节唤醒响应值
***************************************************************************************/
uint8_t sha204c_wakeup(uint8_t *response)
{
sha204p_wakeup(); //唤醒
sha204p_receive_response(4, response); //读4字节响应值一次
return 0;
}
/***************************************************************************************
功能:sha204-sleep
说明:
***************************************************************************************/
uint8_t sha204p_sleep(void)
{
i2c_start(); //开I2C通信
i2c_send_byte(0xc8); //sha204-写地址
i2c_send_byte(0x01); //sha204-命令-睡眠
i2c_stop(); //关I2C通信
return 0;
}
// Power On Chip
// Returns 0 (SUCCESS) if no error
void ic_power_on(void)
{
I2C_CLK_OUT; //SCL
I2C_SDA_OUT ; //SDA
delay_us(1);
I2C_CLK_L;
delay_us(10);
I2C_SDA_H;
delay_ms(2);
// Chip should now be in sync mode and ready to operate
}
// Shut down secure memory
void ic_power_off(void)
{
delay_us(1);
I2C_CLK_L;
delay_us(6);
}