[136] | 1 | /*
|
---|
[175] | 2 | * TWI/I2C library for Arduino Zero
|
---|
| 3 | * Copyright (c) 2015 Arduino LLC. All rights reserved.
|
---|
[136] | 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 |
|
---|
[175] | 31 | // WIRE_HAS_END means Wire has end()
|
---|
| 32 | #define WIRE_HAS_END 1
|
---|
[136] | 33 |
|
---|
| 34 | class TwoWire : public Stream
|
---|
| 35 | {
|
---|
[175] | 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);
|
---|
[136] | 42 |
|
---|
[175] | 43 | void beginTransmission(uint8_t);
|
---|
| 44 | uint8_t endTransmission(bool stopBit);
|
---|
| 45 | uint8_t endTransmission(void);
|
---|
[136] | 46 |
|
---|
[175] | 47 | uint8_t requestFrom(uint8_t address, size_t quantity, bool stopBit);
|
---|
| 48 | uint8_t requestFrom(uint8_t address, size_t quantity);
|
---|
[136] | 49 |
|
---|
[175] | 50 | size_t write(uint8_t data);
|
---|
| 51 | size_t write(const uint8_t * data, size_t quantity);
|
---|
[136] | 52 |
|
---|
[175] | 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));
|
---|
[136] | 59 |
|
---|
[175] | 60 | using Print::write;
|
---|
[136] | 61 |
|
---|
[175] | 62 | void onService(void);
|
---|
[136] | 63 |
|
---|
[175] | 64 | private:
|
---|
| 65 | SERCOM * sercom;
|
---|
| 66 | uint8_t _uc_pinSDA;
|
---|
| 67 | uint8_t _uc_pinSCL;
|
---|
[136] | 68 |
|
---|
[175] | 69 | bool transmissionBegun;
|
---|
[136] | 70 |
|
---|
[175] | 71 | // RX Buffer
|
---|
| 72 | RingBuffer rxBuffer;
|
---|
[136] | 73 |
|
---|
[175] | 74 | //TX buffer
|
---|
| 75 | RingBuffer txBuffer;
|
---|
| 76 | uint8_t txAddress;
|
---|
[136] | 77 |
|
---|
| 78 |
|
---|
[175] | 79 | // Service buffer
|
---|
| 80 | //uint8_t srvBuffer[BUFFER_LENGTH];
|
---|
| 81 | //uint8_t srvBufferIndex;
|
---|
| 82 | //uint8_t srvBufferLength;
|
---|
[136] | 83 |
|
---|
[175] | 84 | // Callback user functions
|
---|
| 85 | void (*onRequestCallback)(void);
|
---|
| 86 | void (*onReceiveCallback)(int);
|
---|
| 87 |
|
---|
| 88 | // TWI state
|
---|
| 89 | //enum TwoWireStatus
|
---|
[136] | 90 | //{
|
---|
[175] | 91 | // UNINITIALIZED,
|
---|
| 92 | // MASTER_IDLE,
|
---|
| 93 | // MASTER_SEND,
|
---|
| 94 | // MASTER_RECV,
|
---|
| 95 | // SLAVE_IDLE,
|
---|
| 96 | // SLAVE_RECV,
|
---|
| 97 | // SLAVE_SEND
|
---|
| 98 | //};
|
---|
| 99 | //TwoWireStatus status;
|
---|
[136] | 100 |
|
---|
[175] | 101 | // TWI clock frequency
|
---|
| 102 | static const uint32_t TWI_CLOCK = 100000;
|
---|
[136] | 103 |
|
---|
[175] | 104 | // Timeouts
|
---|
| 105 | //static const uint32_t RECV_TIMEOUT = 100000;
|
---|
| 106 | //static const uint32_t XMIT_TIMEOUT = 100000;
|
---|
[136] | 107 | };
|
---|
| 108 |
|
---|
[175] | 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
|
---|
[136] | 127 |
|
---|
| 128 | #endif
|
---|