Koduino
|
Wire / I2C / SMBus library (call with global object Wire
)
More...
#include <Wire.h>
Wire / I2C / SMBus library (call with global object Wire
)
One of the most common I2C operations is to read a register from a slave (e.g. MPU6050 and AS5048 examples below). The Arduino API makes this a bit tedious, but the "official" way to do this (fully compatible with Arduino) is to:
false
(don't sent STOP)Public Member Functions | |
TwoWire (I2C_TypeDef *I2Cx) | |
void | begin () |
Start the I2C peripheral and configure the SDA and SCL pins. More... | |
void | beginTransmission (uint8_t address) |
Command to begin a write operation. More... | |
uint8_t | endTransmission (bool stop) |
Transfer the I2C commands queued in the write buffer. More... | |
uint8_t | endTransmission () |
virtual size_t | write (uint8_t b) |
Add a single byte to the write buffer. More... | |
uint8_t | requestFrom (uint8_t address, uint8_t quantity) |
uint8_t | requestFrom (uint8_t address, uint8_t quantity, bool stop) |
Begin an I2C receive transfer operation. More... | |
virtual int | available (void) |
Return the number of bytes in the receive buffer. More... | |
virtual int | read (void) |
Reads a single byte from the receive buffer. More... | |
virtual int | peek (void) |
Reads a single byte from the receive buffer. More... | |
virtual void | flush (void) |
Waits till the previous write operation is finished. | |
void | setSpeed (WireClockSpeed) |
Set SCL clock speed. More... | |
void | stretchClock (bool flag) |
Enable I2C clock stretching. More... | |
Public Member Functions inherited from Stream | |
void | setTimeout (uint32_t timeout) |
Sets maximum milliseconds to wait for stream data. More... | |
bool | find (char *target) |
Reads data from the stream until the target string is found. More... | |
bool | find (char *target, size_t length) |
Reads data from the stream until the target string of given length is found. More... | |
bool | findUntil (char *target, char *terminator) |
Same as find() but search ends if the terminator string is found. More... | |
int | parseInt () |
Returns the first valid (long) integer value from the current position. More... | |
float | parseFloat () |
Float version of parseInt() More... | |
size_t | readBytes (char *buffer, size_t length) |
Read chars from stream into buffer. More... | |
size_t | readBytesUntil (char terminator, char *buffer, size_t length) |
Same as readBytes() with terminator character. More... | |
Inherits Stream.
TwoWire::TwoWire | ( | I2C_TypeDef * | I2Cx | ) |
This file is part of koduino https://github.com/avikde/koduino
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 3 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, see http://www.gnu.org/licenses/.
|
virtual |
void TwoWire::begin | ( | void | ) |
Start the I2C peripheral and configure the SDA
and SCL
pins.
This must be called before any transfer commands
void TwoWire::beginTransmission | ( | uint8_t | address | ) |
Command to begin a write operation.
Queues the slave address in the write buffer
address | 7-bit slave address |
uint8_t TwoWire::endTransmission | ( | bool | stop | ) |
Transfer the I2C commands queued in the write buffer.
This command actually does the I2C transfer, i.e. will consume a large amount of time. It blocks till the transfer is finished.
stop | (Default: true .) Whether or not to send a STOP condition. Warning: the bus will hang unless a STOP is (eventually) sent. |
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
virtual |
Reads a single byte from the receive buffer.
Does not increment the buffer pointer
Implements Stream.
|
virtual |
Reads a single byte from the receive buffer.
Increments the buffer pointer
Implements Stream.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
uint8_t TwoWire::requestFrom | ( | uint8_t | address, |
uint8_t | quantity, | ||
bool | stop | ||
) |
Begin an I2C receive transfer operation.
This function opens the I2C line, sends the slave address and blocks and waits till quantity bytes are received. There is a timeout of ~600us and if nothing is received in that time, the function will return prematurely with the number of bytes received. The received bytes are left in the receive buffer.
address | 7-bit slave address |
quantity | Number of bytes to request |
stop | (Default: true .) Whether or not to send a STOP condition. Warning: the bus will hang unless a STOP is (eventually) sent. |
void TwoWire::setSpeed | ( | WireClockSpeed | clockSpeed | ) |
Set SCL clock speed.
Call this before begin()
One | of WIRE_100K , WIRE_200K , WIRE_400K , or WIRE_1M (with units of Hz) |
void TwoWire::stretchClock | ( | bool | flag | ) |
Enable I2C clock stretching.
Clock stretching is an I2C bus feature which may help slow slave devices keep to the timing enforced by the master. This needs to be called before begin() Warning: untested.
flag | true to enable, false to disable. |
|
virtual |
Add a single byte to the write buffer.
Queues a byte to be sent
b |
1
if successfull, 0
if write buffer is full