forked from rankec/LPC-2124-NFC-PN532
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c.c
103 lines (89 loc) · 1.71 KB
/
i2c.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
/*
The I2C driver.
*/
#include "i2c.h"
#include <targets/LPC2000.h>
/**
Initializes the I2C driver.
*/
void i2c_Init(void)
{
PINSEL0 |= 0x50;
I2SCLL = I2SCLH = 400; // 75kBits @ 58,98 <MHz
I2CONSET = 0x40; // enable I2C bus interface
}
/**
Handles timeouts and interrupts.
*/
static void wait_for_SI(void)
{
unsigned long timeout = 1600000;
I2CONCLR = 8;
while (timeout-- && !(I2CONSET & 8));
}
/**
Sends the START condition.
@param addr The address.
*/
int i2c_Start(int addr)
{
I2CONCLR = 0x14;
I2CONSET = 0x28;
wait_for_SI();
I2CONCLR = 0x20;
if (I2STAT > 0x10) {
return -1;
}
I2DAT = addr;
wait_for_SI();
return (I2STAT != 0x40 && I2STAT != 0x18);
}
/**
Writes on the I2C bus.
@param buf The buffer to be written.
@param count How many times to try.
*/
int i2c_Write(unsigned char *buf, unsigned count)
{
while (count--) {
I2DAT = *buf++;
wait_for_SI();
if (I2STAT != 0x28) {
return 1;
}
}
return 0;
}
/**
Reads from the I2C bus.
*/
int i2c_Read(void)
{
I2CONSET = AA;
wait_for_SI();
if (I2STAT != 0x50 && I2STAT != 0x40) {
return -1;
} else {
return I2DAT;
}
}
/**
Reads the last byte from the NFC bus.
*/
int i2c_ReadLast(void)
{
I2CONCLR = AA;
wait_for_SI();
if (I2STAT != 0x58) {
return -1;
} else {
return I2DAT;
}
}
/**
Sends the STOP condition.
*/
void i2c_Stop(void)
{
I2CONSET = 0x40;
}