Koduino
Public Member Functions | List of all members
TwoWire Class Reference

Wire / I2C / SMBus library (call with global object Wire) More...

#include <Wire.h>

Detailed Description

Wire / I2C / SMBus library (call with global object Wire)

Usage

  1. (Optionally) call setSpeed()
  2. Call begin()
  3. See below for reading/writing

Writing a register

  1. Call beginTransmission()
  2. Call write() as many times as bytes to write
  3. Call endTransmission() (without an argument to send a STOP–recommended).

Reading a register

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:

  1. Call beginTransmission()
  2. write() the register name
  3. Call endTransmission() with the argument false (don't sent STOP)
  4. Call requestFrom() with the 7-bit slave address and a length of 1 (this sends STOP)
  5. Call read() to get the register value

Example: MPU6050

#include <Wire.h>
const int led = PC13;
const uint8_t kMPU = 0x68;
void setup() {
pinMode(led, OUTPUT);
Serial1.begin(115200);
Wire.setSpeed(WIRE_1M);
Wire.begin();
}
void loop() {
uint8_t magic;
uint32_t tic = micros();
Wire.beginTransmission(kMPU);
Wire.write(0x75);
Wire.endTransmission(false);
Wire.requestFrom(kMPU, 1);
magic = Wire.read();
tic = micros() - tic;
Serial1 << "WHO_AM_I = 0x" << _HEX(magic) << ", took " << tic << "us.\n";
digitalWrite(led, TOGGLE);
delay(1000);
}

Example: Nunchuck

#include <Wire.h>
const int led = PC13;
void setup() {
pinMode(led, OUTPUT);
Serial1.begin(115200);
Wire.begin();
Wire.beginTransmission(0x52); // device address
Wire.write(0xF0);
Wire.write(0x55);
delay(1);
Wire.beginTransmission(0x52);
Wire.write(0xFB);
Wire.write(0x00);
}
uint8_t data[6];
void loop() {
uint32_t tic = micros();
Wire.requestFrom(0x52, 6); // request data from nunchuck
int cnt = 0;
while (Wire.available()) {
// receive byte as an integer
data[cnt] = Wire.read(); //
cnt++;
}
tic = micros() - tic;
Serial1 << tic << "\t";
for (int i = 0; i < 6; ++i)
Serial1 << _HEX(data[i]) << "\t";
Serial1 << !(data[5] & 0B00000001) << "\t" << !((data[5] & 0B00000010) >> 1);
Serial1 << "\n";
digitalWrite(led, TOGGLE);
delay(100);
Wire.beginTransmission(0x52); // transmit to device 0x52
Wire.write(0x00); // sends one byte
Wire.endTransmission(false); // stop transmitting
}

Example: AS5048B

#include <Wire.h>
const int led = PC13;
const uint8_t kAMS = 0x40;
void setup() {
pinMode(led, OUTPUT);
Serial1.begin(115200);
Wire.setSpeed(WIRE_400K);
Wire.begin();
}
void loop() {
uint32_t tic = micros();
Wire.beginTransmission(kAMS);
Wire.write(255);
Wire.endTransmission(false);
Wire.requestFrom(kAMS, 1);
uint8_t l = Wire.read() & 0x3f;
Wire.beginTransmission(kAMS);
Wire.write(254);
Wire.endTransmission(false);
Wire.requestFrom(kAMS, 1);
uint8_t h = Wire.read();
tic = micros() - tic;
Serial1 << "Read position = " << ((uint16_t)h<<6) + (uint16_t)l << ", took " << tic << "us.\n";
digitalWrite(led, TOGGLE);
delay(50);
}

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.

Constructor & Destructor Documentation

TwoWire::TwoWire ( I2C_TypeDef *  I2Cx)
Authors
Avik De avikd.nosp@m.e@gm.nosp@m.ail.c.nosp@m.om

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/.

Member Function Documentation

int TwoWire::available ( void  )
virtual

Return the number of bytes in the receive buffer.

Returns

Implements Stream.

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

Parameters
address7-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.

Parameters
stop(Default: true.) Whether or not to send a STOP condition. Warning: the bus will hang unless a STOP is (eventually) sent.
Returns
One of
  • 0:success
  • 1:data too long to fit in transmit buffer
  • 2:received NACK on transmit of address
  • 3:received NACK on transmit of data
  • 4:other error
uint8_t TwoWire::endTransmission ( )
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

int TwoWire::peek ( void  )
virtual

Reads a single byte from the receive buffer.

Does not increment the buffer pointer

Returns

Implements Stream.

int TwoWire::read ( void  )
virtual

Reads a single byte from the receive buffer.

Increments the buffer pointer

Returns

Implements Stream.

uint8_t TwoWire::requestFrom ( uint8_t  address,
uint8_t  quantity 
)
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.

Parameters
address7-bit slave address
quantityNumber 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.
Returns
Number of bytes received before a timeout.
void TwoWire::setSpeed ( WireClockSpeed  clockSpeed)

Set SCL clock speed.

Call this before begin()

Parameters
Oneof 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.

Parameters
flagtrue to enable, false to disable.
size_t TwoWire::write ( uint8_t  b)
virtual

Add a single byte to the write buffer.

Queues a byte to be sent

Parameters
b
Returns
1 if successfull, 0 if write buffer is full

The documentation for this class was generated from the following files: