1 | /*
|
---|
2 | * TwoWire.h - TWI/I2C library for Arduino Due
|
---|
3 | * Copyright (c) 2011 Cristian Maglie <c.maglie@bug.st>.
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * This library is free software; you can redistribute it and/or
|
---|
7 | * modify it under the terms of the GNU Lesser General Public
|
---|
8 | * License as published by the Free Software Foundation; either
|
---|
9 | * version 2.1 of the License, or (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This library is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | * Lesser General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU Lesser General Public
|
---|
17 | * License along with this library; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
19 | */
|
---|
20 |
|
---|
21 | #ifndef TwoWire_h
|
---|
22 | #define TwoWire_h
|
---|
23 |
|
---|
24 | //#include <include/twi.h>
|
---|
25 |
|
---|
26 | #include "Stream.h"
|
---|
27 | #include "variant.h"
|
---|
28 |
|
---|
29 | #include "SERCOM.h"
|
---|
30 | #include "RingBuffer.h"
|
---|
31 |
|
---|
32 | #define BUFFER_LENGTH 32
|
---|
33 |
|
---|
34 |
|
---|
35 | class TwoWire : public Stream
|
---|
36 | {
|
---|
37 | public:
|
---|
38 | TwoWire(SERCOM *s);
|
---|
39 | void begin();
|
---|
40 | void begin(uint8_t);
|
---|
41 |
|
---|
42 | void beginTransmission(uint8_t);
|
---|
43 | uint8_t endTransmission(bool stopBit);
|
---|
44 | uint8_t endTransmission(void);
|
---|
45 |
|
---|
46 | uint8_t requestFrom(uint8_t address, size_t quantity, bool stopBit);
|
---|
47 | uint8_t requestFrom(uint8_t address, size_t quantity);
|
---|
48 |
|
---|
49 | size_t write(uint8_t data);
|
---|
50 | size_t write(const uint8_t * data, size_t quantity);
|
---|
51 |
|
---|
52 | virtual int available(void);
|
---|
53 | virtual int read(void);
|
---|
54 | virtual int peek(void);
|
---|
55 | virtual void flush(void);
|
---|
56 | void onReceive(void(*)(int));
|
---|
57 | void onRequest(void(*)(void));
|
---|
58 |
|
---|
59 | using Print::write;
|
---|
60 |
|
---|
61 | void onService(void);
|
---|
62 |
|
---|
63 | private:
|
---|
64 | SERCOM * sercom;
|
---|
65 | bool transmissionBegun;
|
---|
66 |
|
---|
67 | // RX Buffer
|
---|
68 | RingBuffer rxBuffer;
|
---|
69 |
|
---|
70 | //TX buffer
|
---|
71 | RingBuffer txBuffer;
|
---|
72 | uint8_t txAddress;
|
---|
73 |
|
---|
74 |
|
---|
75 | // Service buffer
|
---|
76 | //uint8_t srvBuffer[BUFFER_LENGTH];
|
---|
77 | //uint8_t srvBufferIndex;
|
---|
78 | //uint8_t srvBufferLength;
|
---|
79 |
|
---|
80 | // Callback user functions
|
---|
81 | void (*onRequestCallback)(void);
|
---|
82 | void (*onReceiveCallback)(int);
|
---|
83 |
|
---|
84 | // TWI state
|
---|
85 | //enum TwoWireStatus
|
---|
86 | //{
|
---|
87 | // UNINITIALIZED,
|
---|
88 | // MASTER_IDLE,
|
---|
89 | // MASTER_SEND,
|
---|
90 | // MASTER_RECV,
|
---|
91 | // SLAVE_IDLE,
|
---|
92 | // SLAVE_RECV,
|
---|
93 | // SLAVE_SEND
|
---|
94 | //};
|
---|
95 | //TwoWireStatus status;
|
---|
96 |
|
---|
97 | // TWI clock frequency
|
---|
98 | static const uint32_t TWI_CLOCK = 100000;
|
---|
99 |
|
---|
100 | // Timeouts
|
---|
101 | //static const uint32_t RECV_TIMEOUT = 100000;
|
---|
102 | //static const uint32_t XMIT_TIMEOUT = 100000;
|
---|
103 | };
|
---|
104 |
|
---|
105 | extern TwoWire Wire;
|
---|
106 |
|
---|
107 | #endif
|
---|