1 | /*
|
---|
2 | * TWI/I2C library for Arduino Zero
|
---|
3 | * Copyright (c) 2015 Arduino LLC. All rights 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 | #ifndef TwoWire_h
|
---|
21 | #define TwoWire_h
|
---|
22 |
|
---|
23 | #include "Stream.h"
|
---|
24 | #include "variant.h"
|
---|
25 |
|
---|
26 | #include "SERCOM.h"
|
---|
27 | #include "RingBuffer.h"
|
---|
28 |
|
---|
29 | #define BUFFER_LENGTH 32
|
---|
30 |
|
---|
31 | // WIRE_HAS_END means Wire has end()
|
---|
32 | #define WIRE_HAS_END 1
|
---|
33 |
|
---|
34 | class TwoWire : public Stream
|
---|
35 | {
|
---|
36 | public:
|
---|
37 | TwoWire(SERCOM *s, uint8_t pinSDA, uint8_t pinSCL);
|
---|
38 | void begin();
|
---|
39 | void begin(uint8_t);
|
---|
40 | void end();
|
---|
41 | void setClock(uint32_t);
|
---|
42 |
|
---|
43 | void beginTransmission(uint8_t);
|
---|
44 | uint8_t endTransmission(bool stopBit);
|
---|
45 | uint8_t endTransmission(void);
|
---|
46 |
|
---|
47 | uint8_t requestFrom(uint8_t address, size_t quantity, bool stopBit);
|
---|
48 | uint8_t requestFrom(uint8_t address, size_t quantity);
|
---|
49 |
|
---|
50 | size_t write(uint8_t data);
|
---|
51 | size_t write(const uint8_t * data, size_t quantity);
|
---|
52 |
|
---|
53 | virtual int available(void);
|
---|
54 | virtual int read(void);
|
---|
55 | virtual int peek(void);
|
---|
56 | virtual void flush(void);
|
---|
57 | void onReceive(void(*)(int));
|
---|
58 | void onRequest(void(*)(void));
|
---|
59 |
|
---|
60 | using Print::write;
|
---|
61 |
|
---|
62 | void onService(void);
|
---|
63 |
|
---|
64 | private:
|
---|
65 | SERCOM * sercom;
|
---|
66 | uint8_t _uc_pinSDA;
|
---|
67 | uint8_t _uc_pinSCL;
|
---|
68 |
|
---|
69 | bool transmissionBegun;
|
---|
70 |
|
---|
71 | // RX Buffer
|
---|
72 | RingBuffer rxBuffer;
|
---|
73 |
|
---|
74 | //TX buffer
|
---|
75 | RingBuffer txBuffer;
|
---|
76 | uint8_t txAddress;
|
---|
77 |
|
---|
78 |
|
---|
79 | // Service buffer
|
---|
80 | //uint8_t srvBuffer[BUFFER_LENGTH];
|
---|
81 | //uint8_t srvBufferIndex;
|
---|
82 | //uint8_t srvBufferLength;
|
---|
83 |
|
---|
84 | // Callback user functions
|
---|
85 | void (*onRequestCallback)(void);
|
---|
86 | void (*onReceiveCallback)(int);
|
---|
87 |
|
---|
88 | // TWI state
|
---|
89 | //enum TwoWireStatus
|
---|
90 | //{
|
---|
91 | // UNINITIALIZED,
|
---|
92 | // MASTER_IDLE,
|
---|
93 | // MASTER_SEND,
|
---|
94 | // MASTER_RECV,
|
---|
95 | // SLAVE_IDLE,
|
---|
96 | // SLAVE_RECV,
|
---|
97 | // SLAVE_SEND
|
---|
98 | //};
|
---|
99 | //TwoWireStatus status;
|
---|
100 |
|
---|
101 | // TWI clock frequency
|
---|
102 | static const uint32_t TWI_CLOCK = 100000;
|
---|
103 |
|
---|
104 | // Timeouts
|
---|
105 | //static const uint32_t RECV_TIMEOUT = 100000;
|
---|
106 | //static const uint32_t XMIT_TIMEOUT = 100000;
|
---|
107 | };
|
---|
108 |
|
---|
109 | #if WIRE_INTERFACES_COUNT > 0
|
---|
110 | extern TwoWire Wire;
|
---|
111 | #endif
|
---|
112 | #if WIRE_INTERFACES_COUNT > 1
|
---|
113 | extern TwoWire Wire1;
|
---|
114 | #endif
|
---|
115 | #if WIRE_INTERFACES_COUNT > 2
|
---|
116 | extern TwoWire Wire2;
|
---|
117 | #endif
|
---|
118 | #if WIRE_INTERFACES_COUNT > 3
|
---|
119 | extern TwoWire Wire3;
|
---|
120 | #endif
|
---|
121 | #if WIRE_INTERFACES_COUNT > 4
|
---|
122 | extern TwoWire Wire4;
|
---|
123 | #endif
|
---|
124 | #if WIRE_INTERFACES_COUNT > 5
|
---|
125 | extern TwoWire Wire5;
|
---|
126 | #endif
|
---|
127 |
|
---|
128 | #endif
|
---|