|
@@ -0,0 +1,238 @@
|
|
1
|
+/*
|
|
2
|
+ TwoWire.cpp - TWI/I2C library for Wiring & Arduino
|
|
3
|
+ Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
4
|
+
|
|
5
|
+ This library is free software; you can redistribute it and/or
|
|
6
|
+ modify it under the terms of the GNU Lesser General Public
|
|
7
|
+ License as published by the Free Software Foundation; either
|
|
8
|
+ version 2.1 of the License, or (at your option) any later version.
|
|
9
|
+
|
|
10
|
+ This library is distributed in the hope that it will be useful,
|
|
11
|
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13
|
+ Lesser General Public License for more details.
|
|
14
|
+
|
|
15
|
+ You should have received a copy of the GNU Lesser General Public
|
|
16
|
+ License along with this library; if not, write to the Free Software
|
|
17
|
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
18
|
+*/
|
|
19
|
+
|
|
20
|
+#ifdef TARGET_LPC1768
|
|
21
|
+
|
|
22
|
+extern "C" {
|
|
23
|
+ #include <stdlib.h>
|
|
24
|
+ #include <string.h>
|
|
25
|
+ #include <inttypes.h>
|
|
26
|
+ #include <lpc17xx_i2c.h>
|
|
27
|
+ #include <lpc17xx_pinsel.h>
|
|
28
|
+ #include <lpc17xx_libcfg_default.h>
|
|
29
|
+}
|
|
30
|
+
|
|
31
|
+#include "Wire.h"
|
|
32
|
+
|
|
33
|
+#define USEDI2CDEV_M 1
|
|
34
|
+
|
|
35
|
+#if (USEDI2CDEV_M == 0)
|
|
36
|
+ #define I2CDEV_M LPC_I2C0
|
|
37
|
+#elif (USEDI2CDEV_M == 1)
|
|
38
|
+ #define I2CDEV_M LPC_I2C1
|
|
39
|
+#elif (USEDI2CDEV_M == 2)
|
|
40
|
+ #define I2CDEV_M LPC_I2C2
|
|
41
|
+#else
|
|
42
|
+ #error "Master I2C device not defined!"
|
|
43
|
+#endif
|
|
44
|
+
|
|
45
|
+// Initialize Class Variables //////////////////////////////////////////////////
|
|
46
|
+
|
|
47
|
+uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
|
|
48
|
+uint8_t TwoWire::rxBufferIndex = 0;
|
|
49
|
+uint8_t TwoWire::rxBufferLength = 0;
|
|
50
|
+
|
|
51
|
+uint8_t TwoWire::txAddress = 0;
|
|
52
|
+uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
|
|
53
|
+uint8_t TwoWire::txBufferIndex = 0;
|
|
54
|
+uint8_t TwoWire::txBufferLength = 0;
|
|
55
|
+
|
|
56
|
+uint8_t TwoWire::transmitting = 0;
|
|
57
|
+
|
|
58
|
+// Constructors ////////////////////////////////////////////////////////////////
|
|
59
|
+
|
|
60
|
+TwoWire::TwoWire() {
|
|
61
|
+}
|
|
62
|
+
|
|
63
|
+// Public Methods //////////////////////////////////////////////////////////////
|
|
64
|
+
|
|
65
|
+void TwoWire::begin(void) {
|
|
66
|
+ rxBufferIndex = 0;
|
|
67
|
+ rxBufferLength = 0;
|
|
68
|
+
|
|
69
|
+ txBufferIndex = 0;
|
|
70
|
+ txBufferLength = 0;
|
|
71
|
+
|
|
72
|
+ /*
|
|
73
|
+ * Init I2C pin connect
|
|
74
|
+ */
|
|
75
|
+ PINSEL_CFG_Type PinCfg;
|
|
76
|
+ PinCfg.OpenDrain = 0;
|
|
77
|
+ PinCfg.Pinmode = 0;
|
|
78
|
+ #if ((USEDI2CDEV_M == 0))
|
|
79
|
+ PinCfg.Funcnum = 1;
|
|
80
|
+ PinCfg.Pinnum = 27;
|
|
81
|
+ PinCfg.Portnum = 0;
|
|
82
|
+ PINSEL_ConfigPin(&PinCfg); // SDA0 / D57 AUX-1
|
|
83
|
+ PinCfg.Pinnum = 28;
|
|
84
|
+ PINSEL_ConfigPin(&PinCfg); // SCL0 / D58 AUX-1
|
|
85
|
+ #endif
|
|
86
|
+ #if ((USEDI2CDEV_M == 1))
|
|
87
|
+ PinCfg.Funcnum = 3;
|
|
88
|
+ PinCfg.Pinnum = 0;
|
|
89
|
+ PinCfg.Portnum = 0;
|
|
90
|
+ PINSEL_ConfigPin(&PinCfg); // SDA1 / D20 SCA
|
|
91
|
+ PinCfg.Pinnum = 1;
|
|
92
|
+ PINSEL_ConfigPin(&PinCfg); // SCL1 / D21 SCL
|
|
93
|
+ #endif
|
|
94
|
+ #if ((USEDI2CDEV_M == 2))
|
|
95
|
+ PinCfg.Funcnum = 2;
|
|
96
|
+ PinCfg.Pinnum = 10;
|
|
97
|
+ PinCfg.Portnum = 0;
|
|
98
|
+ PINSEL_ConfigPin(&PinCfg); // SDA2 / D38 X_ENABLE_PIN
|
|
99
|
+ PinCfg.Pinnum = 11;
|
|
100
|
+ PINSEL_ConfigPin(&PinCfg); // SCL2 / D55 X_DIR_PIN
|
|
101
|
+ #endif
|
|
102
|
+
|
|
103
|
+ // Initialize I2C peripheral
|
|
104
|
+ I2C_Init(I2CDEV_M, 100000);
|
|
105
|
+
|
|
106
|
+ // Enable Master I2C operation
|
|
107
|
+ I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE);
|
|
108
|
+}
|
|
109
|
+
|
|
110
|
+uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) {
|
|
111
|
+ // clamp to buffer length
|
|
112
|
+ if(quantity > BUFFER_LENGTH){
|
|
113
|
+ quantity = BUFFER_LENGTH;
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ // perform blocking read into buffer
|
|
117
|
+ I2C_M_SETUP_Type transferMCfg;
|
|
118
|
+ transferMCfg.sl_addr7bit = address >> 1; // not sure about the right shift
|
|
119
|
+ transferMCfg.tx_data = NULL;
|
|
120
|
+ transferMCfg.tx_length = 0;
|
|
121
|
+ transferMCfg.rx_data = rxBuffer;
|
|
122
|
+ transferMCfg.rx_length = quantity;
|
|
123
|
+ transferMCfg.retransmissions_max = 3;
|
|
124
|
+ I2C_MasterTransferData(I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING);
|
|
125
|
+
|
|
126
|
+ // set rx buffer iterator vars
|
|
127
|
+ rxBufferIndex = 0;
|
|
128
|
+ rxBufferLength = transferMCfg.rx_count;
|
|
129
|
+
|
|
130
|
+ return transferMCfg.rx_count;
|
|
131
|
+}
|
|
132
|
+
|
|
133
|
+uint8_t TwoWire::requestFrom(int address, int quantity) {
|
|
134
|
+ return requestFrom((uint8_t)address, (uint8_t)quantity);
|
|
135
|
+}
|
|
136
|
+
|
|
137
|
+void TwoWire::beginTransmission(uint8_t address) {
|
|
138
|
+ // indicate that we are transmitting
|
|
139
|
+ transmitting = 1;
|
|
140
|
+ // set address of targeted slave
|
|
141
|
+ txAddress = address;
|
|
142
|
+ // reset tx buffer iterator vars
|
|
143
|
+ txBufferIndex = 0;
|
|
144
|
+ txBufferLength = 0;
|
|
145
|
+}
|
|
146
|
+
|
|
147
|
+void TwoWire::beginTransmission(int address) {
|
|
148
|
+ beginTransmission((uint8_t)address);
|
|
149
|
+}
|
|
150
|
+
|
|
151
|
+uint8_t TwoWire::endTransmission(void) {
|
|
152
|
+ // transmit buffer (blocking)
|
|
153
|
+ I2C_M_SETUP_Type transferMCfg;
|
|
154
|
+ transferMCfg.sl_addr7bit = txAddress >> 1; // not sure about the right shift
|
|
155
|
+ transferMCfg.tx_data = txBuffer;
|
|
156
|
+ transferMCfg.tx_length = txBufferLength;
|
|
157
|
+ transferMCfg.rx_data = NULL;
|
|
158
|
+ transferMCfg.rx_length = 0;
|
|
159
|
+ transferMCfg.retransmissions_max = 3;
|
|
160
|
+ Status status = I2C_MasterTransferData(I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING);
|
|
161
|
+
|
|
162
|
+ // reset tx buffer iterator vars
|
|
163
|
+ txBufferIndex = 0;
|
|
164
|
+ txBufferLength = 0;
|
|
165
|
+
|
|
166
|
+ // indicate that we are done transmitting
|
|
167
|
+ transmitting = 0;
|
|
168
|
+
|
|
169
|
+ if (status == SUCCESS)
|
|
170
|
+ return 0; // success
|
|
171
|
+ else
|
|
172
|
+ return 4; // other error
|
|
173
|
+}
|
|
174
|
+
|
|
175
|
+// must be called after beginTransmission(address)
|
|
176
|
+size_t TwoWire::write(uint8_t data) {
|
|
177
|
+ if (transmitting) {
|
|
178
|
+ // don't bother if buffer is full
|
|
179
|
+ if (txBufferLength >= BUFFER_LENGTH) {
|
|
180
|
+ return 0;
|
|
181
|
+ }
|
|
182
|
+
|
|
183
|
+ // put byte in tx buffer
|
|
184
|
+ txBuffer[txBufferIndex] = data;
|
|
185
|
+ ++txBufferIndex;
|
|
186
|
+
|
|
187
|
+ // update amount in buffer
|
|
188
|
+ txBufferLength = txBufferIndex;
|
|
189
|
+ }
|
|
190
|
+
|
|
191
|
+ return 1;
|
|
192
|
+}
|
|
193
|
+
|
|
194
|
+// must be called after beginTransmission(address)
|
|
195
|
+size_t TwoWire::write(const uint8_t *data, size_t quantity) {
|
|
196
|
+ size_t sent = 0;
|
|
197
|
+ if (transmitting)
|
|
198
|
+ for(sent = 0; sent < quantity; ++sent)
|
|
199
|
+ if (!write(data[sent]))
|
|
200
|
+ break;
|
|
201
|
+
|
|
202
|
+ return sent;
|
|
203
|
+}
|
|
204
|
+
|
|
205
|
+// must be called after requestFrom(address, numBytes)
|
|
206
|
+int TwoWire::available(void) {
|
|
207
|
+ return rxBufferLength - rxBufferIndex;
|
|
208
|
+}
|
|
209
|
+
|
|
210
|
+// must be called after requestFrom(address, numBytes)
|
|
211
|
+int TwoWire::read(void) {
|
|
212
|
+ int value = -1;
|
|
213
|
+
|
|
214
|
+ // get each successive byte on each call
|
|
215
|
+ if(rxBufferIndex < rxBufferLength) {
|
|
216
|
+ value = rxBuffer[rxBufferIndex];
|
|
217
|
+ ++rxBufferIndex;
|
|
218
|
+ }
|
|
219
|
+
|
|
220
|
+ return value;
|
|
221
|
+}
|
|
222
|
+
|
|
223
|
+// must be called after requestFrom(address, numBytes)
|
|
224
|
+int TwoWire::peek(void) {
|
|
225
|
+ int value = -1;
|
|
226
|
+
|
|
227
|
+ if(rxBufferIndex < rxBufferLength){
|
|
228
|
+ value = rxBuffer[rxBufferIndex];
|
|
229
|
+ }
|
|
230
|
+
|
|
231
|
+ return value;
|
|
232
|
+}
|
|
233
|
+
|
|
234
|
+// Preinstantiate Objects //////////////////////////////////////////////////////
|
|
235
|
+
|
|
236
|
+TwoWire Wire = TwoWire();
|
|
237
|
+
|
|
238
|
+#endif // TARGET_LPC1768
|