Files
earlephilhower_arduino-pico/libraries/Wire/Wire.h
T
Earle F. Philhower, III 8016a932f3 Add astyle format, boards.txt, and package check (#166)
Miscellaneous CI checks for code style, boards.txt update, and that all
referenced packages are available.
2021-05-24 15:01:03 -07:00

115 lines
3.1 KiB
C++

/*
I2C Master/Slave library for the Raspberry Pi Pico RP2040
Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
Based off of TWI/I2C library for Arduino Zero
Copyright (c) 2015 Arduino LLC. 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
*/
#pragma once
#include <Arduino.h>
#include "api/HardwareI2C.h"
#include <hardware/i2c.h>
// WIRE_HAS_END means Wire has end()
#define WIRE_HAS_END 1
#ifndef WIRE_BUFFER_SIZE
#define WIRE_BUFFER_SIZE 128
#endif
class TwoWire : public HardwareI2C {
public:
TwoWire(i2c_inst_t *i2c, pin_size_t sda, pin_size_t scl);
// Start as Master
void begin() override;
// Start as Slave
void begin(uint8_t address) override;
// Shut down the I2C interface
void end() override;
// Select IO pins to use. Call before ::begin()
bool setSDA(pin_size_t sda);
bool setSCL(pin_size_t scl);
void setClock(uint32_t freqHz) override;
void beginTransmission(uint8_t) override;
uint8_t endTransmission(bool stopBit) override;
uint8_t endTransmission(void) override;
size_t requestFrom(uint8_t address, size_t quantity, bool stopBit) override;
size_t requestFrom(uint8_t address, size_t quantity) override;
size_t write(uint8_t data) override;
size_t write(const uint8_t * data, size_t quantity) override;
virtual int available(void) override;
virtual int read(void) override;
virtual int peek(void) override;
virtual void flush(void) override;
void onReceive(void(*)(int));
void onRequest(void(*)(void));
inline size_t write(unsigned long n) {
return write((uint8_t)n);
}
inline size_t write(long n) {
return write((uint8_t)n);
}
inline size_t write(unsigned int n) {
return write((uint8_t)n);
}
inline size_t write(int n) {
return write((uint8_t)n);
}
using Print::write;
// IRQ callback
void onIRQ();
private:
i2c_inst_t *_i2c;
pin_size_t _sda;
pin_size_t _scl;
int _clkHz;
bool _running;
bool _slave;
uint8_t _addr;
bool _txBegun;
uint8_t _buff[WIRE_BUFFER_SIZE];
int _buffLen;
int _buffOff;
// Callback user functions
void (*_onRequestCallback)(void);
void (*_onReceiveCallback)(int);
bool _slaveStartDet = false;
// TWI clock frequency
static const uint32_t TWI_CLOCK = 100000;
};
extern TwoWire Wire;
extern TwoWire Wire1;