ESP32 I2C mapping

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: ESP32 I2C mapping

Postby WiFive » Wed Jan 03, 2018 6:58 am

It need to be output capable and not pulled down.

Duhjoker
Posts: 85
Joined: Mon Mar 20, 2017 8:09 am

Re: ESP32 I2C mapping

Postby Duhjoker » Wed Jan 03, 2018 7:30 am

Thankyou!!!

ok I was backtracking the code from seesaw to wire and now I see that nothing else needs to be changed I just need to add a second begin and bus address into my code. By pointing wire.begin to the two pins I chose as secondary i2c pins. And both pins need to be input/output capable pins.

what I don't understand is where and how these come into play...

TwoWire Wire = TwoWire(0);
TwoWire Wire2 = TwoWire(0);

and do I need to do this?

extern TwoWire Wire;
extern TwoWire Wire2;
Fear is the mind killer.......

GameR the DIY iot gaming device that does more......
https://github.com/Duhjoker/GameR-Iot_ESP

Duhjoker
Posts: 85
Joined: Mon Mar 20, 2017 8:09 am

Re: ESP32 I2C mapping

Postby Duhjoker » Wed Jan 03, 2018 8:13 am

the page says that dac1 and dac 2 are also analog and also a gpio. Does that mean I can comment out the dac defines and use those Pins?
Fear is the mind killer.......

GameR the DIY iot gaming device that does more......
https://github.com/Duhjoker/GameR-Iot_ESP

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: ESP32 I2C mapping

Postby WiFive » Wed Jan 03, 2018 8:17 am

Seesaw functions are hardcoded to use Wire.function so it will only work on first i2c unless you modify it to support Wire2.function also.

Duhjoker
Posts: 85
Joined: Mon Mar 20, 2017 8:09 am

Re: ESP32 I2C mapping

Postby Duhjoker » Wed Jan 03, 2018 9:22 am

ok so I don't need to modify wire any more but I do need to modify the seesaw library?
Fear is the mind killer.......

GameR the DIY iot gaming device that does more......
https://github.com/Duhjoker/GameR-Iot_ESP

Duhjoker
Posts: 85
Joined: Mon Mar 20, 2017 8:09 am

Re: ESP32 I2C mapping

Postby Duhjoker » Fri Jan 05, 2018 3:47 am

ok what was easiest for me to do was go into wire and add a second begin. The second one points to the second set of i2c pins

Code: Select all

void TwoWire::begin2(int sdaPin, int sclPin, uint32_t frequency2)
{
    if(sdaPin < 0) {
        if(num == 0) {
            sdaPin = SDA2;
        } else {
            return;
        }
    }

    if(sclPin < 0) {
        if(num == 0) {
            sclPin = SCL2;
        } else {
            return;
        }
    }

    if(i2c == NULL) {
        i2c = i2cInit(num, 0, false);
        if(i2c == NULL) {
            return;
        }
    }

    i2cSetFrequency(i2c, frequency2);

    if(sda >= 0 && sda != sdaPin) {
        i2cDetachSDA(i2c, sda2);
    }

    if(scl >= 0 && scl != sclPin) {
        i2cDetachSCL(i2c, scl2);
    }

    sda2 = sdaPin;
    scl2 = sclPin;

    i2cAttachSDA(i2c, sda2);
    i2cAttachSCL(i2c, scl2);

    flush();

    i2cInitFix(i2c);
}
then I made these changes to i2c_init in the seesaw library

Code: Select all

void Adafruit_seesaw::_i2c_init()
{
  Wire.begin();
  Wire.begin2();
}
I then changed begin in the seesaw library to this....

Code: Select all

bool Adafruit_seesaw::begin(uint8_t addr1, uint8_t addr2)
{
	_i2caddr = addr1;
	_i2caddr2 = addr2;

	_i2c_init();

	SWReset();
	delay(500);

	uint8_t c = this->read8(SEESAW_STATUS_BASE, SEESAW_STATUS_HW_ID);

	if(c != SEESAW_HW_ID_CODE) return false;
	
	return true;
}
Fear is the mind killer.......

GameR the DIY iot gaming device that does more......
https://github.com/Duhjoker/GameR-Iot_ESP

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: ESP32 I2C mapping

Postby WiFive » Fri Jan 05, 2018 6:32 am

Really you should modify seesaw so you can pass in a pointer to a TwoWire instance instead of hardcoded to Wire

Duhjoker
Posts: 85
Joined: Mon Mar 20, 2017 8:09 am

Re: ESP32 I2C mapping

Postby Duhjoker » Sat Jan 06, 2018 6:58 am

I decided to just use the same bus for both i2c devices. Much easier and I only had to modify the seesaws begin to open both device addresses. I was concerned about reaction time from when the button pressed to the point where it executes onto a tft screen. For Gaming. It was pointed out to though that the timing would probably be minute so no big deal.

I am having a problem picking two interrupt pins. They need to be digital I'm told. The pin outs for the huzzah 32 only say gpio and analog. Are all the pins digital and only some of those analog capable? it claims in pins Arduino that it has 40 digital pins but the huzzah doesn't have 40 pins lol.

https://github.com/Duhjoker/GameR-Iot_ESP
Fear is the mind killer.......

GameR the DIY iot gaming device that does more......
https://github.com/Duhjoker/GameR-Iot_ESP

Duhjoker
Posts: 85
Joined: Mon Mar 20, 2017 8:09 am

Re: ESP32 I2C mapping

Postby Duhjoker » Sun Jan 07, 2018 12:58 am

I cant get both i2c devices to work at the same time. I gat one side going or he other per compile. So I know they both work. What am I doing wrong here?

Code: Select all

#include "Adafruit_seesaw.h"
Adafruit_seesaw ss;
#define BUTTON_RIGHT 6  /////////////what are these integers
#define BUTTON_DOWN  7  /////////////"                      "
#define BUTTON_LEFT  9  /////////////"                      "
#define BUTTON_UP    10 /////////////"                      "
#define BUTTON_SEL   14 /////////////"                      "
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
uint32_t button_mask = (1 << BUTTON_RIGHT) | (1 << BUTTON_DOWN) | 
                (1 << BUTTON_LEFT) | (1 << BUTTON_UP) | (1 << BUTTON_SEL);
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
uint32_t button_mask2 = (1 << BUTTON_RIGHT) | (1 << BUTTON_DOWN) | 
                (1 << BUTTON_LEFT) | (1 << BUTTON_UP) | (1 << BUTTON_SEL);
                
#if defined(ESP8266)
  #define IRQ_PIN   2
#elif defined(ESP32)
  #define IRQ_PIN1   13
  #define IRQ_PIN2   27
#elif defined(NRF52)
  #define IRQ_PIN   27
#elif defined(TEENSYDUINO)
  #define IRQ_PIN   8
#elif defined(ARDUINO_ARCH_WICED)
  #define IRQ_PIN   PC5
#else
  #define IRQ_PIN   5
#endif
void setup() {
  Serial.begin(115200);
  while(!Serial);
  
  if(!ss.begin(0x49,0x4a)){
    Serial.println("ERROR!");
    while(1);
  }
  else{
    Serial.println("seesaw started");
    Serial.print("version: ");
    Serial.println(ss.getVersion(), HEX);
  }
  ss.pinModeBulk(button_mask, INPUT_PULLUP);
  ss.setGPIOInterrupts(button_mask, 1);
  pinMode(IRQ_PIN1, INPUT);
  
   ss.pinModeBulk(button_mask2, INPUT_PULLUP);
  ss.setGPIOInterrupts(button_mask2, 1);
  pinMode(IRQ_PIN2, INPUT);
}

int last_x = 0, last_y = 0;
int last_x2 = 0, last_y2 = 0;

void loop() {
  int x = ss.analogRead(2);
  int y = ss.analogRead(3);
  
  if ( (abs(x - last_x) > 3)  ||  (abs(y - last_y) > 3)) {
    Serial.print(x); Serial.print(", "); Serial.println(y);
    last_x = x;
    last_y = y;
  }
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
int x2 = ss.analogRead(2);
  int y2 = ss.analogRead(3);
  
  if ( (abs(x - last_x2) > 3)  ||  (abs(y - last_y2) > 3)) {
    Serial.print(x); Serial.print(", "); Serial.println(y);
    last_x2 = x2;
    last_y2 = y2;
  }
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
  if(!digitalRead(IRQ_PIN1)){
    uint32_t buttons = ss.digitalReadBulk(button_mask);
    //Serial.println(buttons, BIN);
    if (! (buttons & (1 << BUTTON_RIGHT))) {
      Serial.println("Button A pressed");
    }
    if (! (buttons & (1 << BUTTON_DOWN))) {
      Serial.println("Button B pressed");
    }
    if (! (buttons & (1 << BUTTON_LEFT))) {
      Serial.println("Button Y pressed");
    }
    if (! (buttons & (1 << BUTTON_UP))) {
      Serial.println("Button x pressed");
    }
    if (! (buttons & (1 << BUTTON_SEL))) {
      Serial.println("Button SEL pressed");
    }
  }
  if(!digitalRead(IRQ_PIN2)){
    uint32_t buttons = ss.digitalReadBulk(button_mask2);
    //Serial.println(buttons, BIN);
    if (! (buttons & (1 << BUTTON_RIGHT))) {
      Serial.println("Button A pressed");
    }
    if (! (buttons & (1 << BUTTON_DOWN))) {
      Serial.println("Button B pressed");
    }
    if (! (buttons & (1 << BUTTON_LEFT))) {
      Serial.println("Button Y pressed");
    }
    if (! (buttons & (1 << BUTTON_UP))) {
      Serial.println("Button x pressed");
    }
    if (! (buttons & (1 << BUTTON_SEL))) {
      Serial.println("Button SEL pressed");
    }
  }
  delay(10);
}
Fear is the mind killer.......

GameR the DIY iot gaming device that does more......
https://github.com/Duhjoker/GameR-Iot_ESP

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: ESP32 I2C mapping

Postby WiFive » Sun Jan 07, 2018 3:09 am

Everything?

If you are going to use the same i2c bus you should remove your changes to seesaw and do

Code: Select all

Adafruit_seesaw ss;
Adafruit_seesaw ss2;
ss.begin(0x49);
ss2.begin(0x4a);
Etc...

Who is online

Users browsing this forum: No registered users and 63 guests