-
Notifications
You must be signed in to change notification settings - Fork 1
/
i2c.c
134 lines (120 loc) · 3.02 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
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
#include "lpc17xx_i2c.h"
#include "lpc17xx_pinsel.h"
#include "lpc_types.h"
#include "utils.h"
char output[128];
int len;
void setupI2C(void)
{
//Configure pins for I2C1
PINSEL_CFG_Type PinCfg;
PinCfg.Funcnum = 3;
PinCfg.OpenDrain = 0;
PinCfg.Pinmode = 0;
PinCfg.Portnum = 0;
PinCfg.Pinnum = 0;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 1;
PINSEL_ConfigPin(&PinCfg);
//Init I2C1
I2C_Init(LPC_I2C1, 10000);
I2C_Cmd(LPC_I2C1, ENABLE);
serialWrite("I2C setup");
}
int i2cWrite(int addr, char* data, int length)
{
//Setup Packet
I2C_M_SETUP_Type packet;
packet.sl_addr7bit = addr;
packet.tx_data = data;
packet.tx_length = length;
packet.tx_count = 0;
packet.rx_data = NULL;
packet.rx_length = 0;
packet.rx_count = 0;
packet.retransmissions_max = 1;
packet.retransmissions_count = 0;
//Transfer Packet
if(I2C_MasterTransferData(LPC_I2C1, &packet, (I2C_TRANSFER_OPT_Type) I2C_TRANSFER_POLLING) == SUCCESS)
{ //if successful return 1
return(1);
}
else
{ //Else return 0
return(0);
}
}
int i2cRead(int addr, char* data, int length)
{
//Setup Packet
I2C_M_SETUP_Type packet;
packet.sl_addr7bit = addr;
packet.tx_data = NULL;
packet.tx_length = 0;
packet.tx_count = 0;
packet.rx_data = data;
packet.rx_length = length;
packet.rx_count = 0;
packet.retransmissions_max = 1;
packet.retransmissions_count = 0;
//Transfer Packet
if(I2C_MasterTransferData(LPC_I2C1, &packet, (I2C_TRANSFER_OPT_Type) I2C_TRANSFER_POLLING) == SUCCESS)
{ //if successful return 1
return(1);
}
else
{ //Else return 0
return(0);
}
}
int i2cReadWrite(int addr, char* writeData, int writeLength, char* readData, int readLength)
{
//Setup Packet
I2C_M_SETUP_Type packet;
packet.sl_addr7bit = addr;
packet.tx_data = writeData;
packet.tx_length = writeLength;
packet.tx_count = 0;
packet.rx_data = readData;
packet.rx_length = readLength;
packet.rx_count = 0;
packet.retransmissions_max = 1;
packet.retransmissions_count = 0;
//Transfer Packet
if(I2C_MasterTransferData(LPC_I2C1, &packet, (I2C_TRANSFER_OPT_Type) I2C_TRANSFER_POLLING) == SUCCESS)
{ //if successful return 1
return(1);
}
else
{ //Else return 0
return(0);
}
}
int i2cScan(int* addressArray)
{
int counter = 0;
int testData [1] = {0x01};
for(int addr = 0; addr < 128; addr++)
{
if(i2cWrite(addr, testData, 1) == 1)
{
addressArray[counter] = addr;
counter ++;
}
}
return(counter);
}
void i2cScanAll(void)
{
//Scanning
int* addresses[128];
int num = i2cScan(addresses);
len = sprintf(output, "%03d devices connected to i2c bus", num);
serialWrite(output);
for(int i = 0; i<num; i++)
{
len = sprintf(output, "Device at 0x%03x %03d", addresses[i], addresses[i]);
serialWrite(output);
}
serialWrite("End of devices\n");
}