[SOLVED] I2C requestFrom() - how to read > 32 bytes?

@UMD, the only catch is the need for dynamic allocation of the buffer. @mdma or @BDub need to advise.

I’d definetly like to see that.
That was also an idea for serial buffer and is how SPI can deal with bigger data.

Have updated Github issue #1112 with @peekay123’s suggestion. Fingers crossed!

1 Like

For the record, now working fine with (modified) SoftI2CMaster library.

I like the way that SoftI2CMaster gives finer control to the application - requestFrom() below just sets up the read, it does not buffer, ie “n” can be greater than 32 now:

    // Note SoftI2CMaster operates differently to Arduino code - there is no internal buffering
    // which is exactly what we need
    status = i2c.requestFrom(PN532_I2C_ADDRESS);
    
    if (status != I2C_ACK)
    {   
        Serial.printf("requestFrom() Read setup was NAK'd (0x%02X)\r\n", status);
        // Did not accept the read setup (ie write of address+READ BIT), try again at a higher level
        return(false);
    }

    // ** Status ** byte is the first returned
    // 0 = NOT ready
    // 1 = READY
    // ---------------------------------
    status = i2c.read();

    if (status != 0x01)
    {
        // PN532 has indicated that it is not ready, no point carrying on
        return(false);     
    }

    for (i = 0;  i < n - 1; i++)                // NOTE n - 1, ie don't read the last byte in this loop
        buff[i] = i2c.read();                   // read+ACK

    // This is **necessary** for PN532!!!
    buff[i] = i2c.readLast();                   // read+NAK

** Conclusion ** - if you want to read > 32 bytes using I2C, use a I2C library that gives what you need.

Case closed!

PS - I will post the ported SoftI2CLibrary with @peekay123’s help sometime soon.

4 Likes

@UMD @peekay123 Did you ever get this SoftI2CLibrary ported?

I have a device that has 1 command that returns 34 bytes and the current wire library is keeping me from reading those last 2 bytes :disappointed_relieved:

I searched for the SoftI2C library on the Build IDE but it's not there.

What is the easiest way to get this working on a Photon?

@RWB, yes, I ported it okay, but have not turned into a library (I must learn how to do that one day)!

You just need to make these changes:

// HR's port
#define i2c_scl_release()                           \
                    pinMode(_sclPin, INPUT);
#define i2c_sda_release()                           \
                    pinMode(_sdaPin, INPUT);

// sets SCL low and drives output
#define i2c_scl_lo()                                \
                    pinResetFast(_sclPin);          \
                    pinMode(_sclPin, OUTPUT);  



// sets SDA low and drives output
#define i2c_sda_lo()                                \
                    pinResetFast(_sdaPin);          \
                    pinMode(_sdaPin, OUTPUT);  

// Setting SCL high and to input releases pin ie floating
// HR NOTE - not implementing PULLUP as is pulled up externally
#define i2c_scl_hi()                                \
                    pinSetFast(_sclPin);            \
                    pinMode(_sclPin, INPUT);  


// Setting SDA high and to input releases pin ie floating
// HR NOTE - not implementing PULLUP as is pulled up externally
#define i2c_sda_hi()                                \
                    pinSetFast(_sdaPin);            \
                    pinMode(_sdaPin, INPUT);      

Also follow these comments:

2016-09-19 
- Removed i2c_init() from being called in SoftI2CMaster() constructor(s)
- Renamed i2c_init() to begin()

Hope this helps.

@UMD Is this the same library source you used? https://github.com/todbot/SoftI2CMaster

Just to be sure I’m working with the same library that you used to avoid problems :smiley:

Yep, I sourced Tod E. Kurt via this mention http://todbot.com/blog/

Out of interest, you might want to check out this chap who built on Todbot;'s:

Note that I just took the source and included into my app and modified from there, ie did not make it into a library.

@UMD Thanks!

I’m going to go ahead and use this library since it comes with example code.

Looks simple to use but I’ll report back as I try to get this working.

@peekay123 Can you use the same I2C pins on the Photon with this library or do you have to use 2 separate pins other than the default I2C pins?

@RWB as long as you don’t call Wire.begin () you can use the same pins. :slight_smile:

@peekay123 Great news.

Should I be able to just change my current Wire. calls to myWire. for the new library ?

And if I never call Wire.begin(); does that mean that the Wire library does not get loaded on my Photon and therefore save some memory space?

@RWB, yes and no.

1 Like

@UMD @peekay123 @ScruffR

I went ahead and used the same library @UMD used to keep things simple.

I added the SoftI2CMaster .cpp & .h files to my program and changed the lines of code as @UMD suggested.

I figure once we get this worked out we can add it to the new library system for others :smiley:

SoftI2CMaster.cpp

/*
 * SoftI2CMaster.cpp -- Multi-instance software I2C Master library
 *
 *
 * 2010-12 Tod E. Kurt, http://todbot.com/blog/
 *
 * This code takes some tricks from:
 *  http://codinglab.blogspot.com/2008/10/i2c-on-avr-using-bit-banging.html
 *
 * 2014, by Testato: update library and examples for follow Wire’s API of Arduino IDE 1.x
 *
 */

#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

#include "SoftI2CMaster.h"

#include <util/delay.h>
#include <string.h>

#define  i2cbitdelay 50

#define  I2C_ACK  1
#define  I2C_NAK  0


// HR's port
#define i2c_scl_release()                           \
                    pinMode(_sclPin, INPUT);
#define i2c_sda_release()                           \
                    pinMode(_sdaPin, INPUT);

// sets SCL low and drives output
#define i2c_scl_lo()                                \
                    pinResetFast(_sclPin);          \
                    pinMode(_sclPin, OUTPUT);



// sets SDA low and drives output
#define i2c_sda_lo()                                \
                    pinResetFast(_sdaPin);          \
                    pinMode(_sdaPin, OUTPUT);

// Setting SCL high and to input releases pin ie floating
// HR NOTE - not implementing PULLUP as is pulled up externally
#define i2c_scl_hi()                                \
                    pinSetFast(_sclPin);            \
                    pinMode(_sclPin, INPUT);


// Setting SDA high and to input releases pin ie floating
// HR NOTE - not implementing PULLUP as is pulled up externally
#define i2c_sda_hi()                                \
                    pinSetFast(_sdaPin);            \
                    pinMode(_sdaPin, INPUT);


//
// Constructor
//
SoftI2CMaster::SoftI2CMaster()
{
    // do nothing, use setPins() later
}
//
SoftI2CMaster::SoftI2CMaster(uint8_t sclPin, uint8_t sdaPin)
{
    setPins(sclPin, sdaPin, true);
    //i2c_init();  Removed via HR's sugesstion.
}

//
SoftI2CMaster::SoftI2CMaster(uint8_t sclPin, uint8_t sdaPin, uint8_t pullups)
{
    setPins(sclPin, sdaPin, pullups);
    //i2c_init();  Removed via HR's sugesstion.
}

//
// Turn Arduino pin numbers into PORTx, DDRx, and PINx
//
void SoftI2CMaster::setPins(uint8_t sclPin, uint8_t sdaPin, uint8_t pullups)
{
    uint8_t port;

    usePullups = pullups;

    _sclPin = sclPin;
    _sdaPin = sdaPin;

    _sclBitMask = digitalPinToBitMask(sclPin);
    _sdaBitMask = digitalPinToBitMask(sdaPin);

    port = digitalPinToPort(sclPin);
    _sclPortReg  = portOutputRegister(port);
    _sclDirReg   = portModeRegister(port);

    port = digitalPinToPort(sdaPin);
    _sdaPortReg  = portOutputRegister(port);
    _sdaDirReg   = portModeRegister(port);

    initialized = 255;
}

//
//
//
uint8_t SoftI2CMaster::beginTransmission(uint8_t address)
{
    i2c_start();
    uint8_t rc = i2c_write((address<<1) | 0); // clr read bit
    // The standard Wire library returns a status in endTransmission(), not beginTransmission().
    // So we will return the status here but also remember the result so we can return it in endTransmission().
    // It also allows us to disable other I2C functions until beginTransmission has been called, if we want.
    initialized = rc;
    return rc;
}

//
uint8_t SoftI2CMaster::requestFrom(uint8_t address)
{
    i2c_start();
    uint8_t rc = i2c_write((address<<1) | 1); // set read bit
    return rc;
}
//
uint8_t SoftI2CMaster::requestFrom(int address)
{
    return requestFrom( (uint8_t) address);
}
// Added for compatibility with the standard Wire library.
uint8_t SoftI2CMaster::requestFrom(int address, int quantity)
{
    return requestFrom( (uint8_t) address);

    // Ignore 'quantity', since SoftI2CMaster::requestFrom() just sets the start of read adresses,
    // so it's the same for any number of bytes.
    (void)quantity;
}
// Added for compatibility with the standard Wire library.
uint8_t SoftI2CMaster::requestFrom(uint8_t address, uint8_t quantity)
{
    return requestFrom( (uint8_t) address);

    // Ignore 'quantity', since SoftI2CMaster::requestFrom() just sets the start of read adresses,
    // so it's the same for any number of bytes.
    (void)quantity;
}

//
uint8_t SoftI2CMaster::beginTransmission(int address)
{
    return beginTransmission((uint8_t)address);
}

//
//
//
uint8_t SoftI2CMaster::endTransmission(void)
{
    i2c_stop();
    return initialized;   // Use the result of beginTransmission()
}

// must be called in:
// slave tx event callback
// or after beginTransmission(address)
uint8_t SoftI2CMaster::write(uint8_t data)
{
    return i2c_write(data);
}

// must be called in:
// slave tx event callback
// or after beginTransmission(address)
void SoftI2CMaster::write(uint8_t* data, uint8_t quantity)
{
    for(uint8_t i = 0; i < quantity; ++i){
        write(data[i]);
    }
}

// must be called in:
// slave tx event callback
// or after beginTransmission(address)
void SoftI2CMaster::write(char* data)
{
    write((uint8_t*)data, strlen(data));
}

// must be called in:
// slave tx event callback
// or after beginTransmission(address)
void SoftI2CMaster::write(int data)
{
    write((uint8_t)data);
}

//--------------------------------------------------------------------


void SoftI2CMaster::i2c_writebit( uint8_t c )
{
    if ( c > 0 ) {
        i2c_sda_hi();
    } else {
        i2c_sda_lo();
    }

    i2c_scl_hi();
    _delay_us(i2cbitdelay);

    i2c_scl_lo();
    _delay_us(i2cbitdelay);

    if ( c > 0 ) {
        i2c_sda_lo();
    }
    _delay_us(i2cbitdelay);
}

//
uint8_t SoftI2CMaster::i2c_readbit(void)
{
    i2c_sda_hi();
    i2c_scl_hi();
    _delay_us(i2cbitdelay);

    uint8_t port = digitalPinToPort(_sdaPin);
    volatile uint8_t* pinReg = portInputRegister(port);
    uint8_t c = *pinReg;  // I2C_PIN;

    i2c_scl_lo();
    _delay_us(i2cbitdelay);

    return ( c & _sdaBitMask) ? 1 : 0;
}

// Inits bitbanging port, must be called before using the functions below
//
void SoftI2CMaster::i2c_init(void)
{
    //I2C_PORT &=~ (_BV( I2C_SDA ) | _BV( I2C_SCL ));
    //*_sclPortReg &=~ (_sdaBitMask | _sclBitMask);
    i2c_sda_hi();
    i2c_scl_hi();

    _delay_us(i2cbitdelay);
}

// Send a START Condition
//
void SoftI2CMaster::i2c_start(void)
{
    // set both to high at the same time
    //I2C_DDR &=~ (_BV( I2C_SDA ) | _BV( I2C_SCL ));
    //*_sclDirReg &=~ (_sdaBitMask | _sclBitMask);
    i2c_sda_hi();
    i2c_scl_hi();

    _delay_us(i2cbitdelay);

    i2c_sda_lo();
    _delay_us(i2cbitdelay);

    i2c_scl_lo();
    _delay_us(i2cbitdelay);
}

void SoftI2CMaster::i2c_repstart(void)
{
    // set both to high at the same time (releases drive on both lines)
    //I2C_DDR &=~ (_BV( I2C_SDA ) | _BV( I2C_SCL ));
    //*_sclDirReg &=~ (_sdaBitMask | _sclBitMask);
    i2c_sda_hi();
    i2c_scl_hi();

    i2c_scl_lo();                           // force SCL low
    _delay_us(i2cbitdelay);

    i2c_sda_release();                      // release SDA
    _delay_us(i2cbitdelay);

    i2c_scl_release();                      // release SCL
    _delay_us(i2cbitdelay);

    i2c_sda_lo();                           // force SDA low
    _delay_us(i2cbitdelay);
}

// Send a STOP Condition
//
void SoftI2CMaster::i2c_stop(void)
{
    i2c_scl_hi();
    _delay_us(i2cbitdelay);

    i2c_sda_hi();
    _delay_us(i2cbitdelay);
}

// write a byte to the I2C slave device
//
uint8_t SoftI2CMaster::i2c_write( uint8_t c )
{
    for ( uint8_t i=0;i<8;i++) {
        i2c_writebit( c & 128 );
        c<<=1;
    }

    return i2c_readbit();
}

// read a byte from the I2C slave device
//
uint8_t SoftI2CMaster::i2c_read( uint8_t ack )
{
    uint8_t res = 0;

    for ( uint8_t i=0;i<8;i++) {
        res <<= 1;
        res |= i2c_readbit();
    }

    if ( ack )
        i2c_writebit( 0 );
    else
        i2c_writebit( 1 );

    _delay_us(i2cbitdelay);

    return res;
}

// FIXME: this isn't right, surely
uint8_t SoftI2CMaster::read( uint8_t ack )
{
  return i2c_read( ack );
}

//
uint8_t SoftI2CMaster::read()
{
    return i2c_read( I2C_ACK );
}

//
uint8_t SoftI2CMaster::readLast()
{
    return i2c_read( I2C_NAK );
}

SoftI2CMaster.h

/*
 * SoftI2CMaster.h -- Multi-instance software I2C Master library
 *
 * 2010-2012 Tod E. Kurt, http://todbot.com/blog/
 * 2014, by Testato: update library and examples for follow Wire’s API of Arduino IDE 1.x
 *
 */

#ifndef SoftI2CMaster_h
#define SoftI2CMaster_h

#include <inttypes.h>

#define _SOFTI2CMASTER_VERSION 13  // software version of this library


class SoftI2CMaster
{

private:
  // per object data
  uint8_t _sclPin;
  uint8_t _sdaPin;
  uint8_t _sclBitMask;
  uint8_t _sdaBitMask;
  volatile uint8_t *_sclPortReg;
  volatile uint8_t *_sdaPortReg;
  volatile uint8_t *_sclDirReg;
  volatile uint8_t *_sdaDirReg;

  uint8_t usePullups;

  // 'initialized' will be:
  //    255 on startup,
  //    0 if beginTransmission() was called and successful,
  //    any other value if there was an error during beginTransmission().
  uint8_t initialized;

  // private methods

  void i2c_writebit( uint8_t c );
  uint8_t i2c_readbit(void);
  void i2c_init(void);
  void i2c_start(void);
  void i2c_repstart(void);
  void i2c_stop(void);
  uint8_t i2c_write( uint8_t c );
  uint8_t i2c_read( uint8_t ack );

public:
  // public methods
  SoftI2CMaster();
  SoftI2CMaster(uint8_t sclPin, uint8_t sdaPin);
  SoftI2CMaster(uint8_t sclPin, uint8_t sdaPin, uint8_t usePullups);

  void setPins(uint8_t sclPin, uint8_t sdaPin, uint8_t usePullups);

  uint8_t beginTransmission(uint8_t address);
  uint8_t beginTransmission(int address);
  uint8_t endTransmission(void);
  uint8_t write(uint8_t);
  void write(uint8_t*, uint8_t);
  void write(int);
  void write(char*);
  void begin(void) {return;};
  uint8_t requestFrom(int address);
  uint8_t requestFrom(uint8_t address);
  uint8_t requestFrom(int address, int quantity);
  uint8_t requestFrom(uint8_t address, uint8_t quantity);
  uint8_t read( uint8_t ack );
  uint8_t read();
  uint8_t readLast();

};

#endif

My .ino code is below:

#include "application.h"
#include "BQ20Z45.h"            //Include BQ78350 Header File
#include "SoftI2CMaster.h"




void setup(void)
{
  // We start the serial library to output our messages.
  Serial.begin(115200);

  // Start i2c communication.
  Wire.begin();
}


void loop(void)
{

delay(4000);

}

I only get 1 error so far when I compile the code as shown below and I’m guessing I do not need that file on the Photon and it should be removed but I wanted to ask before assuming. :wink:

@UMD Can you show me the code you used in your main .ino file for setting the I2C pins properly and then calling begin for the SoftI2CMaster bus? That would keep me from guessing :blush:

WProgram.h is the predicessor of Arduino.h and hence is only required for really old Arduinos.

But you can target 0.6.1-rc.2, which has improved Arduino portability features,

@ScruffR I removed this block and that error went away:

#if (ARDUINO >= 100)
        #include <Arduino.h>
        #else
        #include <WProgram.h>
        #endif

Now I get this error:

Is this util/delay.h also something not needed on the Photon? @UMD never mentioned changing any of this so I’m asking about all this to be sure.

If I remove the util/delay.h I get tons of other compile errors relating to delays.

@UMD can you post a complete .cpp, .h, and .ino code breakdown of the port.

@RWB, have you seen my update above

So you should be able to put that block back and try again.

BTW, @UMD said

@ScruffR Just now saw it. I’m working in the latest Particle DEV. Would I need to compile in CLI to target that firmware right?

I’m sure @UMD did not do this and got it working so I think I’ll wait and see how he got this working before compiling in the CLI since I prefer to work in DEV vs the CLI when possible.

We seem to reply too quickly :wink:

I'm not aware of a new feature in Dev to target a specific version, but I don't see the problem in using Dev and CLI in parallel. You can do all the editing in Dev and only have a console open next to it to start the CLI build/flash script.
There even is an Atom extension to have a console within Dev.

When using CLI I have a batch script that does the build, puts the device in DFU Mode and flashes --usb. That's ideal when developing non-connected projects.

@ScruffR Thanks for the quick replies :+1:

I'm sure he was not using the new library system, so before I waste to much time trying to figure out what he already has fixed I'll just go to bed and hopefully wake up to his working code :smiley:

Yes, that's not a problem at all. I've just never needed to compile in CLI due to having to use the latest beta firmware yet. I do flash my Electrons in DFU mode using the CLI on the regular, though.

I would like to install that. Can you link to it please?

There are loads, but this seems to be the one used most

But you can install it just from within Dev.
File -> Settings -> Install enter atom-terminal-panel, hit Packages and Install

You may have to install some depenencies

1 Like