Coder Social home page Coder Social logo

slowsofti2cmaster's Introduction

SlowSoftI2CMaster

Another bit-banging I2C library, very similar to SoftI2CMaster, allowing you to use any Arduino pins for SDA and SCL. The difference to the SoftI2CMaster library is that it is entirely written in C++, so you can use it also on ARM boards, such as Zero and Due.

Features

This library has the following features:

  • architecture independent, compatible with all MCUs supported by the Arduino IDE
  • can make use of any pin
  • supports multiple I2C buses
  • internal MCU pullup resistors can be used
  • timeout on ACK polling for busy devices (new!)
  • supports only master mode
  • no bus arbitration (i.e., only one master allowed on bus)
  • no clock stretching supported
  • slow (70 kHz on a Zero, 45 kHz on an UNO)
  • optional Wire library compatible interface
  • LGPL license

Usage

The library can be used in a similar way as the SoftI2CMaster library. See the description of the methods in the documentation of this library. However, here all parameters and options are not defined as compile-time constants, but they are passed as parameters when creating a new instance.

Example

// Simple sketch to read out one register of an I2C device
#include <SlowSoftI2CMaster.h>

// create new instance with A4 as SDA, A5 as SCL and enable internal pullups
SlowSoftI2CMaster si = SlowSoftI2CMaster(A4, A5, true);

#define I2C_7BITADDR 0x68 // DS1307
#define MEMLOC 0x0A
#define ADDRLEN 1


void setup(void) {
    Serial.begin(38400);
    if (!si.i2c_init()) // initialize I2C module
        Serial.println("I2C init failed");
}

void loop(void){
    if (!si.i2c_start((I2C_7BITADDR<<1)|I2C_WRITE)) { // init transfer
        Serial.println("I2C device busy");
        return;
    }
    si.i2c_write(MEMLOC); // send memory to device
    si.i2c_rep_start((I2C_7BITADDR<<1)|I2C_READ); // restart for reading
    byte val = si.i2c_read(true); // read one byte and send NAK afterwards
    si.i2c_stop(); // stop communication
    Serial.println(val);
    delay(1000);
}

You can use the library in a way such that the internal pullup resistors of the AVR are used (as shown in the example). However, note that this implies that when switching between HIGH and LOW the bus will temporarily in a high-impedance state, which is outside the I2C specification. Furthermore, the internal pull-ups are around 50k and so might not deliver enough current. Be careful when using this option and consider it as a potential source of errors.

Alternative interface

As in the case of the SoftI2CMaster library, there exists a wrapper library that emulates the functionality of the Wire class.

This software is published under the LGPL

slowsofti2cmaster's People

Contributors

felias-fogg avatar ivankravets avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

slowsofti2cmaster's Issues

NodeMCU timing and SMBus speed

Thank you for the excellent implementation. I am using the code on a NodeMCU 1.0 for reading LiOn battery controllers as in this project: https://powercartel.com/projects/packprobe/. I was getting a lot of implausible data until I changed the #define DELAY 4 // usec in the header to 64. SMBus is close enough to I2C that it works but I believe the timing is much slower.

Can we have Releases?

Hi, I use Platform IO with VSCode and it would be a lot easier to use your library if there was a release, since it can then be automatically installed and kept up-to-date. Any chance we can get you to press that button on this and the SoftI2CMaster library?

Pointer to the SoftSlowI2CMaster Wire Wrapper

Excited to use this for our project. Can you please point me to the Wrapper class that you mention in the README, to use the SoftSlowI2CMaster interface to emulate the Wire interface?

Small thing: AVR-g++ warning can be avoided

In AVR-g++ the warning
./libraries/SoftWire/SoftI2CMaster.h:566:1: warning: no return statement in function returning non-void [-Wreturn-type]
is easily removed by adding a line
return true;
on the indicated location.

Ger

Arduino Teensy probably needs additional delay in i2c_read()

I'm using the library to communicate with the Adafruit TSL2591 modules and code which was working satisfactory on an Arduino Nano did fail when run on Arduino Teensy 4.1. Not sure if that is an issue with that specific module, but after adding an additional delay things are back to working normally.

I pinpointed the failure to lines 115/116 of SlowSoftI2CMaster.cpp which I changed as shown below.
`
// Read one byte. If is true, we send a NAK after having received
// the byte in order to terminate the read sequence.
uint8_t SlowSoftI2CMaster::i2c_read(bool last) {
uint8_t b = 0;
setHigh(_sda);
for (uint8_t i = 0; i < 8; i++) {
b <<= 1;
delayMicroseconds(DELAY);
setHigh(_scl);
// CHANGE: Additional delay for reading
delayMicroseconds(DELAY);
// END CHANGE
if (digitalRead(_sda)) b |= 1;
setLow(_scl);
}
if (last) setHigh(_sda); else setLow(_sda);
setHigh(_scl);
delayMicroseconds(DELAY/2);
setLow(_scl);
delayMicroseconds(DELAY/2);
setLow(_sda);
return b;
}

`

License type?

Hi,
I just made a fork of your code,
and I'm wondering if I'm allowed to?

Best Regards,
Paweł

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.