-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWire_due32.cpp
420 lines (349 loc) · 10.8 KB
/
Wire_due32.cpp
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
/*
project_Quad 32 bit Arduino Due
//GPS
1. stabilized quadrotor
by: tinnakon kheowree
0860540582
http://quad3d-tin.lnwshop.com/
https://www.facebook.com/tinnakonza
* TwoWire.h - TWI/I2C library for Arduino Due
* Copyright (c) 2011 Cristian Maglie <[email protected]>.
* All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
extern "C" {
#include <string.h>
}
#include "Wire_due32.h"
static inline bool TWI_FailedAcknowledge(Twi *pTwi) {
return pTwi->TWI_SR & TWI_SR_NACK;
}
static inline bool TWI_WaitTransferComplete(Twi *_twi, uint32_t _timeout) {
uint32_t _status_reg = 0;
while ((_status_reg & TWI_SR_TXCOMP) != TWI_SR_TXCOMP) {
_status_reg = TWI_GetStatus(_twi);
if (_status_reg & TWI_SR_NACK)
return false;
if (--_timeout == 0)
return false;
}
return true;
}
static inline bool TWI_WaitByteSent(Twi *_twi, uint32_t _timeout) {
uint32_t _status_reg = 0;
while ((_status_reg & TWI_SR_TXRDY) != TWI_SR_TXRDY) {
_status_reg = TWI_GetStatus(_twi);
if (_status_reg & TWI_SR_NACK)
return false;
if (--_timeout == 0)
return false;
}
return true;
}
static inline bool TWI_WaitByteReceived(Twi *_twi, uint32_t _timeout) {
uint32_t _status_reg = 0;
while ((_status_reg & TWI_SR_RXRDY) != TWI_SR_RXRDY) {
_status_reg = TWI_GetStatus(_twi);
if (_status_reg & TWI_SR_NACK)
return false;
if (--_timeout == 0)
return false;
}
return true;
}
static inline bool TWI_STATUS_SVREAD(uint32_t status) {
return (status & TWI_SR_SVREAD) == TWI_SR_SVREAD;
}
static inline bool TWI_STATUS_SVACC(uint32_t status) {
return (status & TWI_SR_SVACC) == TWI_SR_SVACC;
}
static inline bool TWI_STATUS_GACC(uint32_t status) {
return (status & TWI_SR_GACC) == TWI_SR_GACC;
}
static inline bool TWI_STATUS_EOSACC(uint32_t status) {
return (status & TWI_SR_EOSACC) == TWI_SR_EOSACC;
}
static inline bool TWI_STATUS_NACK(uint32_t status) {
return (status & TWI_SR_NACK) == TWI_SR_NACK;
}
TwoWire::TwoWire(Twi *_twi, void(*_beginCb)(void)) :
twi(_twi), rxBufferIndex(0), rxBufferLength(0), txAddress(0),
txBufferLength(0), srvBufferIndex(0), srvBufferLength(0), status(
UNINITIALIZED), onBeginCallback(_beginCb), twiClock(TWI_CLOCK) {
}
void TwoWire::begin(void) {
if (onBeginCallback)
onBeginCallback();
// Disable PDC channel
twi->TWI_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS;
TWI_ConfigureMaster(twi, twiClock, VARIANT_MCK);
status = MASTER_IDLE;
}
void TwoWire::begin(uint8_t address) {
if (onBeginCallback)
onBeginCallback();
// Disable PDC channel
twi->TWI_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS;
TWI_ConfigureSlave(twi, address);
status = SLAVE_IDLE;
TWI_EnableIt(twi, TWI_IER_SVACC);
//| TWI_IER_RXRDY | TWI_IER_TXRDY | TWI_IER_TXCOMP);
}
void TwoWire::begin(int address) {
begin((uint8_t) address);
}
void TwoWire::setClock(uint32_t frequency) {
twiClock = frequency;
TWI_SetClock(twi, twiClock, VARIANT_MCK);
}
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) {
if (quantity > BUFFER_LENGTH)
quantity = BUFFER_LENGTH;
// perform blocking read into buffer
int readed = 0;
TWI_StartRead(twi, address, 0, 0);
do {
// Stop condition must be set during the reception of last byte
if (readed + 1 == quantity)
TWI_SendSTOPCondition( twi);
TWI_WaitByteReceived(twi, RECV_TIMEOUT);
rxBuffer[readed++] = TWI_ReadByte(twi);
} while (readed < quantity);
TWI_WaitTransferComplete(twi, RECV_TIMEOUT);
// set rx buffer iterator vars
rxBufferIndex = 0;
rxBufferLength = readed;
return readed;
}
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) {
return requestFrom((uint8_t) address, (uint8_t) quantity, (uint8_t) true);
}
uint8_t TwoWire::requestFrom(int address, int quantity) {
return requestFrom((uint8_t) address, (uint8_t) quantity, (uint8_t) true);
}
uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop) {
return requestFrom((uint8_t) address, (uint8_t) quantity, (uint8_t) sendStop);
}
void TwoWire::beginTransmission(uint8_t address) {
status = MASTER_SEND;
// save address of target and empty buffer
txAddress = address;
txBufferLength = 0;
}
void TwoWire::beginTransmission(int address) {
beginTransmission((uint8_t) address);
}
//
// Originally, 'endTransmission' was an f(void) function.
// It has been modified to take one parameter indicating
// whether or not a STOP should be performed on the bus.
// Calling endTransmission(false) allows a sketch to
// perform a repeated start.
//
// WARNING: Nothing in the library keeps track of whether
// the bus tenure has been properly ended with a STOP. It
// is very possible to leave the bus in a hung state if
// no call to endTransmission(true) is made. Some I2C
// devices will behave oddly if they do not see a STOP.
//
uint8_t TwoWire::endTransmission(uint8_t sendStop) {
uint8_t error = 0;
// transmit buffer (blocking)
TWI_StartWrite(twi, txAddress, 0, 0, txBuffer[0]);
if (!TWI_WaitByteSent(twi, XMIT_TIMEOUT))
error = 2; // error, got NACK on address transmit
if (error == 0) {
uint16_t sent = 1;
while (sent < txBufferLength) {
TWI_WriteByte(twi, txBuffer[sent++]);
if (!TWI_WaitByteSent(twi, XMIT_TIMEOUT))
error = 3; // error, got NACK during data transmmit
}
}
if (error == 0) {
TWI_Stop(twi);
if (!TWI_WaitTransferComplete(twi, XMIT_TIMEOUT))
error = 4; // error, finishing up
}
txBufferLength = 0; // empty buffer
status = MASTER_IDLE;
return error;
}
// This provides backwards compatibility with the original
// definition, and expected behaviour, of endTransmission
//
uint8_t TwoWire::endTransmission(void)
{
return endTransmission(true);
}
size_t TwoWire::write(uint8_t data) {
if (status == MASTER_SEND) {
if (txBufferLength >= BUFFER_LENGTH)
return 0;
txBuffer[txBufferLength++] = data;
return 1;
} else {
if (srvBufferLength >= BUFFER_LENGTH)
return 0;
srvBuffer[srvBufferLength++] = data;
return 1;
}
}
size_t TwoWire::write(const uint8_t *data, size_t quantity) {
if (status == MASTER_SEND) {
for (size_t i = 0; i < quantity; ++i) {
if (txBufferLength >= BUFFER_LENGTH)
return i;
txBuffer[txBufferLength++] = data[i];
}
} else {
for (size_t i = 0; i < quantity; ++i) {
if (srvBufferLength >= BUFFER_LENGTH)
return i;
srvBuffer[srvBufferLength++] = data[i];
}
}
return quantity;
}
int TwoWire::available(void) {
return rxBufferLength - rxBufferIndex;
}
int TwoWire::read(void) {
if (rxBufferIndex < rxBufferLength)
return rxBuffer[rxBufferIndex++];
return -1;
}
int TwoWire::peek(void) {
if (rxBufferIndex < rxBufferLength)
return rxBuffer[rxBufferIndex];
return -1;
}
void TwoWire::flush(void) {
// Do nothing, use endTransmission(..) to force
// data transfer.
}
void TwoWire::onReceive(void(*function)(int)) {
onReceiveCallback = function;
}
void TwoWire::onRequest(void(*function)(void)) {
onRequestCallback = function;
}
void TwoWire::onService(void) {
// Retrieve interrupt status
uint32_t sr = TWI_GetStatus(twi);
if (status == SLAVE_IDLE && TWI_STATUS_SVACC(sr)) {
TWI_DisableIt(twi, TWI_IDR_SVACC);
TWI_EnableIt(twi, TWI_IER_RXRDY | TWI_IER_GACC | TWI_IER_NACK
| TWI_IER_EOSACC | TWI_IER_SCL_WS | TWI_IER_TXCOMP);
srvBufferLength = 0;
srvBufferIndex = 0;
// Detect if we should go into RECV or SEND status
// SVREAD==1 means *master* reading -> SLAVE_SEND
if (!TWI_STATUS_SVREAD(sr)) {
status = SLAVE_RECV;
} else {
status = SLAVE_SEND;
// Alert calling program to generate a response ASAP
if (onRequestCallback)
onRequestCallback();
else
// create a default 1-byte response
write((uint8_t) 0);
}
}
if (status != SLAVE_IDLE) {
if (TWI_STATUS_TXCOMP(sr) && TWI_STATUS_EOSACC(sr)) {
if (status == SLAVE_RECV && onReceiveCallback) {
// Copy data into rxBuffer
// (allows to receive another packet while the
// user program reads actual data)
for (uint8_t i = 0; i < srvBufferLength; ++i)
rxBuffer[i] = srvBuffer[i];
rxBufferIndex = 0;
rxBufferLength = srvBufferLength;
// Alert calling program
onReceiveCallback( rxBufferLength);
}
// Transfer completed
TWI_EnableIt(twi, TWI_SR_SVACC);
TWI_DisableIt(twi, TWI_IDR_RXRDY | TWI_IDR_GACC | TWI_IDR_NACK
| TWI_IDR_EOSACC | TWI_IDR_SCL_WS | TWI_IER_TXCOMP);
status = SLAVE_IDLE;
}
}
if (status == SLAVE_RECV) {
if (TWI_STATUS_RXRDY(sr)) {
if (srvBufferLength < BUFFER_LENGTH)
srvBuffer[srvBufferLength++] = TWI_ReadByte(twi);
}
}
if (status == SLAVE_SEND) {
if (TWI_STATUS_TXRDY(sr) && !TWI_STATUS_NACK(sr)) {
uint8_t c = 'x';
if (srvBufferIndex < srvBufferLength)
c = srvBuffer[srvBufferIndex++];
TWI_WriteByte(twi, c);
}
}
}
#if WIRE_INTERFACES_COUNT > 0
static void Wire_Init(void) {
pmc_enable_periph_clk(WIRE_INTERFACE_ID);
PIO_Configure(
g_APinDescription[PIN_WIRE_SDA].pPort,
g_APinDescription[PIN_WIRE_SDA].ulPinType,
g_APinDescription[PIN_WIRE_SDA].ulPin,
g_APinDescription[PIN_WIRE_SDA].ulPinConfiguration);
PIO_Configure(
g_APinDescription[PIN_WIRE_SCL].pPort,
g_APinDescription[PIN_WIRE_SCL].ulPinType,
g_APinDescription[PIN_WIRE_SCL].ulPin,
g_APinDescription[PIN_WIRE_SCL].ulPinConfiguration);
NVIC_DisableIRQ(WIRE_ISR_ID);
NVIC_ClearPendingIRQ(WIRE_ISR_ID);
NVIC_SetPriority(WIRE_ISR_ID, 0);
NVIC_EnableIRQ(WIRE_ISR_ID);
}
TwoWire Wire = TwoWire(WIRE_INTERFACE, Wire_Init);
void WIRE_ISR_HANDLER(void) {
Wire.onService();
}
#endif
#if WIRE_INTERFACES_COUNT > 1
static void Wire1_Init(void) {
pmc_enable_periph_clk(WIRE1_INTERFACE_ID);
PIO_Configure(
g_APinDescription[PIN_WIRE1_SDA].pPort,
g_APinDescription[PIN_WIRE1_SDA].ulPinType,
g_APinDescription[PIN_WIRE1_SDA].ulPin,
g_APinDescription[PIN_WIRE1_SDA].ulPinConfiguration);
PIO_Configure(
g_APinDescription[PIN_WIRE1_SCL].pPort,
g_APinDescription[PIN_WIRE1_SCL].ulPinType,
g_APinDescription[PIN_WIRE1_SCL].ulPin,
g_APinDescription[PIN_WIRE1_SCL].ulPinConfiguration);
NVIC_DisableIRQ(WIRE1_ISR_ID);
NVIC_ClearPendingIRQ(WIRE1_ISR_ID);
NVIC_SetPriority(WIRE1_ISR_ID, 0);
NVIC_EnableIRQ(WIRE1_ISR_ID);
}
TwoWire Wire1 = TwoWire(WIRE1_INTERFACE, Wire1_Init);
void WIRE1_ISR_HANDLER(void) {
Wire1.onService();
}
#endif