[SOLVED] I2C not playing nice?

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

[SOLVED] I2C not playing nice?

Postby idahowalker » Sun Nov 25, 2018 7:58 pm

Code: Select all

#include "UTM.h"
#include <Wire.h>
#include <U8g2lib.h>
#include <stdio.h> //for function sprintf
#include <Adafruit_GPS.h>
#include <HardwareSerial.h>
//
/*
  Flash mode QI0 is the fastest
  Flash size = 4Mb
*/
#include "esp_system.h" //This inclusion configures the peripherals in the ESP system.
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/event_groups.h"
/* define event bits */
#define CalDistance                 ( 1 << 0 ) //1
#define AnalogVoltReadTask             ( 1 << 1 ) //10
#define DO_UTM_TASK_BIT                 ( 1 << 2 ) //100
#define UPDATE_DISPLAY_TASK_BIT    ( 1 << 3 ) //1000
#define GPS_Parse                 ( 1 << 4 ) //10000 
#define GPS_Signal                ( 1 << 5 ) //100000
// #define ALL_SYNC_BITS ( CalDistance | AnalogVoltReadTask | DO_UTM_TASK_BIT | UPDATE_DISPLAY_TASK_BIT | GPS_Parse | GPS_Signal ) //111111
/* create a hardware timer */
hw_timer_t * timer = NULL;
/* create event group */
EventGroupHandle_t eg;
////
////
SemaphoreHandle_t s_CalDistance;
////
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
////
#define TimerDivider 80
#define TaskCore1 1
#define TaskCore0 0
#define A0Pin 36 //analog pin
#define A1Pin 39 //analog pin
#define BlinkLED 2
#define FiveHundred 500
#define EightHundred 800
#define OneK 1000
#define OneKfive 1500
#define TwoK 2000
#define ThreeThreeK 3300
#define TenK 10000
#define Priority1 1
#define Priority2 2
#define Priority3 3
#define Priority4 4
#define Priority5 5
#define DEGREE 600000
//???????????????????????????????????????????
HardwareSerial GPSSerial(2);
// Connect to the GPS on the hardware port
Adafruit_GPS GPS(&GPSSerial);
// UltimateGPS rx to ESP32 I017 - Serial2
// UltimateGPS tx to ESP32 IO16 - Serial2
// pin 16=RX, pin 17=TX
////
//Adafruit_BMP085 bmp;
// Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);
//?????????????????????????????????????????
volatile int iTicCount = 0; //counts milliseconds
bool bLED_On = false;
long lGPS_Lat0 = 0;
long lGPS_Lon0 = 0;
unsigned int iDistance = 0; //meters
float floatMiles = 0.0;
volatile float floatVbatt = 0.0;
float BattPercent = 0;
/////////////////////////////////////
// from lat long to UTM conversion
float UTM_North = 0.0; // holds northing info
float UTM_West = 0.0; // holds easting info
int iZone = 0; // holds zone info
////////////////////////////////////////////////////////////////
//temperature, pressure, altitude, sealevelpressure
#define BMP085_ADDRESS 0x77  // I2C address of BMP085
#define BMP_Interval 1000
#define SDA 26
#define SCL 27
const unsigned char OSS = 0;  // Oversampling Setting
// Calibration values
int ac1;
int ac2;
int ac3;
unsigned int ac4;
unsigned int ac5;
unsigned int ac6;
int b1;
int b2;
int mb;
int mc;
int md;
// b5 is calculated in bmp085GetTemperature(...), this variable is also used in bmp085GetPressure(...)
// so ...Temperature(...) must be called before ...Pressure(...).
long b5;
short bmp_temperature;
long bmp_pressure;
////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////
// timer ISR callback set at 1000X a second
void IRAM_ATTR onTimer()
{
  BaseType_t xHigherPriorityTaskWoken;
  iTicCount++;
  //
  if ( iTicCount == OneK )
  {
    xEventGroupSetBitsFromISR(eg, UPDATE_DISPLAY_TASK_BIT, &xHigherPriorityTaskWoken); // trigger every 1X a second
  }
  //
  xEventGroupSetBitsFromISR(eg, GPS_Signal, &xHigherPriorityTaskWoken); // trigger every 1mS
  //
  if ( iTicCount == OneK )
  {
    // reset counter to start again
    iTicCount = 0;
  }
} // void IRAM_ATTR onTimer()
////
////
void setup()
{
  ////

  Serial.begin(115200);
  pinMode( BlinkLED, OUTPUT ); // for blink LED
  //
 Wire.begin ( SDA,SCL );
 // bmp085Calibration();
  u8g2.begin();
  ////
  eg = xEventGroupCreate();

  GPS.begin(9600);
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); // has speed n altitude
  //  // Note you must send both commands below to change both the output rate (how often the position is written to the serial line), and the position fix rate. 1 Hz update rate
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
  GPS.sendCommand(PMTK_API_SET_FIX_CTL_1HZ);
  //turn off antenna data being sent
  GPS.sendCommand("$PGCMD,33,0*6D/r/n");
  //speed threshold level
  //0/0.2/0.4/0.5/0.8/1.0/1.5/2.0 m/s
  //GPS.sendCommand("$PMTK397,0.2*3F/r/n");
  vTaskDelay( pdMS_TO_TICKS( OneK ) ); // delay one second

  /* Use 4th timer of 4.  1 tick take 1/(80MHZ/80) = 1us so we set divider 80 and count up */
  timer = timerBegin(3, TimerDivider, true);
  /* Attach onTimer function to our timer */
  timerAttachInterrupt(timer, &onTimer, true);
  /* Set alarm to call timer ISR, every 10000uS and repeat / reset ISR (true) after each alarm */
  timerAlarmWrite(timer,  OneK, true);
  /* Start an alarm */
  timerAlarmEnable(timer);
  //
  xTaskCreatePinnedToCore( fUpdateDisplay, "fUpdateDisplay", TenK, NULL, Priority5, NULL, TaskCore1 ); // assigned to core 1
  xTaskCreatePinnedToCore( fBlinkBuiltIn, "fBlinkBuiltIn", TenK, NULL, Priority1, NULL, TaskCore1 ); //assigned to core 1
  xTaskCreatePinnedToCore( fGPS_SIGNAL, "fGPS_SIGNAL", TenK, NULL, Priority4, NULL, TaskCore0 ); // assigned to core 0
  xTaskCreatePinnedToCore( fGPS_Parse, "fGPS_Parse", TenK, NULL, Priority4, NULL, TaskCore0 ); // assigned to core 0
  xTaskCreatePinnedToCore( TaskAnalogVoltRead, "fTaskAnalogVoltRead", TenK, NULL, Priority2, NULL, TaskCore1 ); // assigned to core 1
  xTaskCreatePinnedToCore( fGetUTM, "fGetUTM", TenK, NULL, Priority3, NULL, TaskCore0 ); // assigned to core 0
  xTaskCreatePinnedToCore( fCalDistance, "fCalDistance", TenK, NULL, Priority2, NULL, TaskCore1 ); // assigned to core 1
  s_CalDistance = xSemaphoreCreateBinary(); // create Semaphore
  xSemaphoreGive( s_CalDistance );
  // xTaskCreatePinnedToCore( fCheck_Pressure, "fCheck_Pressure", TenK, NULL, Priority2, NULL, TaskCore0 ); //assigned to core 1
  //  Task function, name of task, Stack size of task, parameter of the task, priority of the task, Task handle to keep track of created,
  //  ////
} //void setup()
///////////////////////////
void loop() {}
//////////////////////////
/////////////////////////
/* The display routine will trigger the UTN update when needed.
    The display runs once per second
    the display will only call on the UTM update as needed
    the UTM runs on the other core from the display
*/
void fGetUTM(  void * parameter )
{
  for (;;)
  {
    EventBits_t xbit = xEventGroupWaitBits (eg, DO_UTM_TASK_BIT, pdTRUE, pdTRUE, portMAX_DELAY) ;
    // int LatLonToUTMXY (FLOAT lat, FLOAT lon, int zone, FLOAT& x, FLOAT& y)
    if ( GPS.fix )
    {
      iZone = LatLonToUTMXY( GPS.latitudeDegrees, GPS.longitudeDegrees, 0, UTM_North, UTM_West );
    }
  }
}//
void fUpdateDisplay( void * parameter )
{
  static int iCount = 0;
  float f = 0.0;
  static char buf[20];
  int iYposition = 61;
  int iLeftPosition = 0;
  u8g2.setFont( u8g2_font_t0_11_mr );
  for (;;)
  {
    /* wait forever until event bit of task 1 is set */
    EventBits_t xbit = xEventGroupWaitBits (eg, UPDATE_DISPLAY_TASK_BIT, pdTRUE, pdTRUE, portMAX_DELAY) ;
    //    Serial.println( "fUpdateDisplay");
    //    Serial.flush();
    u8g2.clearBuffer(); // clear the internal memory
    //    //
    // Serial.println( "clearBuffer");
    // u8g2.drawStr( 0, 10, "Hello World!" );
    //    Serial.println( "drawStr");
    // u8g2.setCursor(0, 60); u8g2.print( iCount );
    sprintf(buf, "UTC %02d:%02d:%02d", GPS.hour, GPS.minute, GPS.seconds);
    u8g2.setCursor(0, 10); u8g2.print(buf);
    u8g2.drawStr(85, 10, "^");
    u8g2.setCursor(95, 10); u8g2.print(GPS.satellites);
    //
    sprintf(buf, "Date: %02d/%02d/%02d", GPS.day, GPS.month, GPS.year);
    u8g2.setCursor(0, 23); u8g2.print(buf);
    //
    u8g2.drawHLine(3, 27, 90);
    u8g2.drawVLine(3, 27, 22);
    u8g2.drawHLine(3, 49, 90);
    u8g2.drawVLine(93, 27, 23);
    //
    // Display Lat and long
    if ( (iCount >= 0) &&  (iCount <= 9) )
    {
      f = (GPS.latitude / 100);
      u8g2.setCursor(20, 37) ; u8g2.print(int(f));
      f = GPS.latitude - (int(f) * 100);
      u8g2.setCursor(40, 37); u8g2.print(f);
      u8g2.setCursor(83, 37); u8g2.print(GPS.lat);
      //
      f = (GPS.longitude / 100);
      u8g2.setCursor(20, 47); u8g2.print(int(f));
      f = GPS.longitude - (int(f) * 100);
      u8g2.setCursor(40, 47); u8g2.print(f);
      //u8g2.drawCircle(45, 47, 1,u8g2_DRAW_ALL);
      u8g2.setCursor(83, 47); u8g2.print(GPS.lon);
    }
    else
    {
      //display UTM northing, easting, and zone
      u8g2.setCursor(12, 37) ; u8g2.print( UTM_North );
      u8g2.setCursor(12, 47); u8g2.print( UTM_West);
      u8g2.setCursor(75, 37); u8g2.print( iZone);
    }
    //
    if ( (iCount >= 0) &&  (iCount <= 4) )
    {
      u8g2.drawStr(iLeftPosition, iYposition, "Hdg:");
      u8g2.setCursor(28, iYposition); u8g2.print(GPS.angle);
    }
    if ( (iCount >= 5) &&  (iCount <= 9) )
    {
      u8g2.drawStr(iLeftPosition, iYposition, "Alti: ");
      u8g2.setCursor(30, iYposition); u8g2.print((GPS.altitude * 3.2808));
    }
    if ( (iCount >= 10) &&  (iCount <= 14) )
    {
      if ( iDistance >= OneK )
      {
        sprintf( buf, "Dst:%03.1Km %03.1fMi", ( (float(iDistance) * 0.001), (floatMiles * 0.00018939)) ); // meters to K and feet to Miles.
      }
      else
      {
        sprintf( buf, "Dst:%03dm %04.1f", iDistance, floatMiles );
      }
      u8g2.setCursor( iLeftPosition, iYposition ); u8g2.print( buf );
    }
    if ( (iCount >= 15) &&  (iCount <= 19) )
    {
      sprintf( buf, "V: %.2f %.2f%", floatVbatt, BattPercent );
      u8g2.setCursor( iLeftPosition, iYposition ); u8g2.print( buf );
    }
    u8g2.sendBuffer(); // transfer internal memory to the display
    if ( (iCount >= 9) &&  (iCount <= 19) )
    {
      // trigger UTM update, update utm just before its used as a display value
      xEventGroupSetBits( eg, DO_UTM_TASK_BIT );
    }
    iCount++;
    xEventGroupSetBits( eg, AnalogVoltReadTask ); // trigger analog voltage read and calculation
    if ( iCount >= 20 )
    {
      iCount = 0;
    }
    ////
  }
  vTaskDelete( NULL );
} // void fUpdateDisplay( void * parameter )
//////
void fGPS_SIGNAL( void * parameter )
{
  for (;;)
  {
    xEventGroupWaitBits (eg, GPS_Signal, pdTRUE, pdTRUE, portMAX_DELAY) ;
    // casues a nema read from the gps module
    GPS.read();
    xEventGroupSetBits(eg, GPS_Parse); // trigger fGPS_Parse
  }
  vTaskDelete( NULL );
}
//////
//////
void fGPS_Parse( void * parameter )
{
  for (;;)
  {
    xEventGroupWaitBits (eg, GPS_Parse, pdTRUE, pdTRUE, portMAX_DELAY) ;
    //leave this code alone!!!!!!!!!!!!!!!!!!!!!
    //query GPS: has a new complete chunk of data been received?
    if (GPS.newNMEAreceived())
    {
      // Serial.println( "GPS.newNMEAreceived()" );
      //    //if received cause parse data
      if (GPS.parse(GPS.lastNMEA()))
        // if data is parsed do the thing if there is a fix
        if ( GPS.fix )
        {
          xEventGroupSetBits( eg, CalDistance ); // trinnger distance calculate
          // Serial.println( "GPS.parse(GPS.lastNMEA()))" );
        }
    }
  }
  vTaskDelete( NULL );
} // void fGPS_Parse( void *pdata )
//////////////////////////////////////////////////
void fBlinkBuiltIn( void* pvParameters )
{
  // toggle built in LED off/on
  for (;;)
  {
    vTaskDelay( pdMS_TO_TICKS( 10 ) );
    digitalWrite( BlinkLED, LOW );
    vTaskDelay( pdMS_TO_TICKS( OneK ) );
    digitalWrite( BlinkLED, HIGH );
  }
  vTaskDelete( NULL );
}
////////////////////////////////////////////////
// example of a ticks to wait use
void TaskAnalogVoltRead( void *pvParameters )
{
  float r1 = 27000.0;                       // R1 in ohm, 27k = 27000.0 //actual 27K
  float r2 = 10000.0;                       // R2 in ohm, 10k = 10000.0 //actual 10K
  // Wait a maximum of 2000ms for either bit to be set.
  const TickType_t TicksToWait = TwoK / portTICK_PERIOD_MS;
  //
  for (;;)
  {
    // vTaskDelay( pdMS_TO_TICKS( 7500 ) );
    /* wait until event bit of task 1 is set or time out*/
    // group handle, WaitForBits, ClearOnExitBit, WaitForAllBits, TicksToWait
    xEventGroupWaitBits( eg, AnalogVoltReadTask, pdTRUE, pdTRUE, TicksToWait );
    // Serial.print(  );
    // read the input on analog pin 0, A0Pin, GPIO36
    //  (5.0 * analogRead(A1) / 1023) / 10020 * ((100000 + 10020));
    // R1 in ohm, 47k = 47000.0  R2 in ohm, 10k = 10000.0
    // A1Pin input is ground, any non-ground readings get substracted from voltage reading
    floatVbatt = ( ( 3.3 * float((analogRead( A0Pin ) - analogRead( A1Pin ))) / 4095) / r2 * ( r1 + r2) );
    fBatteryPercentRemaingCharge();
    // print out the value read:
    //    Serial.print( floatVbatt );
    //    Serial.print ( " " );
    //            Serial.println( "Analog0Value " + String( float(analogRead( A0Pin )) * 3.3 ) );
    //            Serial.flush();
  }
  vTaskDelete( NULL );
}
/////////////////////////////////////////////////////////
void fBatteryPercentRemaingCharge()
{
  byte V_BattNominal = 8;
  byte V_MinRange = 6; //volts is lowest recomemded operating voltage
  ////
  if (floatVbatt >= V_BattNominal)
  {
    BattPercent = 100.00;
  }
  else
  {
    BattPercent = ((floatVbatt - V_MinRange) * 100.0) / (V_BattNominal - V_MinRange);
  }
}// end fBatteryPercentRemaingCharge()
//*****************************************************************************************
//
//*****************************************************************************************
void fCalDistance( void *pvParameters )
{
  int iTmpDist = 0;
  for (;;)
  {
    xEventGroupWaitBits (eg, CalDistance, pdTRUE, pdTRUE, portMAX_DELAY) ;
    //if able to take semaphore then take one and process distance
    // if not able to take a semaphore then skip, as distance calculation is already in progress.
    if ( xSemaphoreTake( s_CalDistance, 0 ) == pdTRUE )
    {
      // if (digitalRead(StartStopDistanceCounterPin) == 0)
      // {
      // if previous is zero then seed
      if ( lGPS_Lat0 == 0 )
      {
        // lGPS_Lat0 = fDegreeToDecimal( GPS.latitudeDegrees );
        lGPS_Lat0 = (GPS.latitudeDegrees * 1000) * 60;
        // lGPS_Lon0  = fDegreeToDecimal( GPS.longitudeDegrees );
        lGPS_Lon0 = (GPS.longitudeDegrees * 1000) * 60;
      }
      else
      {
        long tempLat =  (GPS.latitudeDegrees * 1000) * 60;
        long tempLon = (GPS.longitudeDegrees * 1000) * 60;
        // has lat or lon changed?
        if ( (tempLat != lGPS_Lat0) || (tempLon != lGPS_Lon0) )
        {
          iTmpDist = DistanceBetween( lGPS_Lat0, lGPS_Lon0, tempLat, tempLon );
          if ( iTmpDist <= 1000 )
          {
            if ( iTmpDist >= 250 )
            {
              // add new distance to old distance
              iDistance =  iDistance + iTmpDist;
              floatMiles = floatMiles + ( iTmpDist *  3.2808399 ); // meters to feet
            }
            else
            {
              iDistance = iTmpDist;
              floatMiles = ( iTmpDist *  3.2808399 ); // meters to feet
            }
            //            Serial.print( tempLat );
            //            Serial.print ( " " );
            //            Serial.println( lGPS_Lat0 );
            //reset to last used measuring point
            lGPS_Lat0 = tempLat;
            lGPS_Lon0 = tempLon;
            // }
          }
        }
      }
    }
    xSemaphoreGive( s_CalDistance ); // give up semaphore so process can be ran again, when triggered.
  }
  vTaskDelete( NULL );
}// end void fCalDistance()
////////////////////////////////////////////////////////////////
unsigned int CosFix (long angle)
{
  long u = labs(angle) >> 16;
  u = (u * u * 6086) >> 24;
  return 246 - u;
} // unsigned int CosFix (long angle)
/////////////////////////////////////////////////////////////
long Diff (long deg1, long deg2)
{
  long result = deg2 - deg1;
  if (result > 180 * DEGREE) return result - 360 * DEGREE;
  else if (result < -180 * DEGREE) return result + 360 * DEGREE;
  else return result;
} // long Diff (long deg1, long deg2)
//calculates distance between 2 points . using lat/lon. ignores earth curve, valid for distances of several 100K, returns meters
//www.technoblogy.com/show?LNQ
/////////////////////////////////////////////////////////////////////
unsigned int DistanceBetween (long lat1, long long1, long lat2, long long2)
{
  long dx = (Diff(long2, long1) * CosFix((lat1 + lat2) / 2)) / 256;
  long dy = Diff(lat2, lat1);
  unsigned long adx = labs(dx);
  unsigned long ady = labs(dy);
  unsigned long b = max(adx, ady);
  unsigned long a = min(adx, ady);
  if (b == 0) return 0;
  return 95 * (b + (110 * a / b * a + 128) / 256) / 512;
} // unsigned int DistanceBetween (long lat1, long long1, long lat2, long long2)
////////////////////////////////////////////////////////////////
// does not work all that well
//long fDegreeToDecimal(long floatDegree)
//{
//  return floatDegree  * 60 * 10000;
//} // long fDegreeToDecimal(long floatDegree)
//This returns 0=N, 1=NE, 2=E, 3=SE, 4=S, 5=SW, 6=W, or 7=NW.
//int Cardinal (unsigned int dir)
//{
//  return (((dir * 2 + 45) / 90) & 7);
//}
///////////////////////////////////////////////////////////////////////////////////////////
////******************************************
////      bmp085 functions
////******************************************
// Calculate pressure given up
// calibration values must be known
// b5 is also required so bmp085GetTemperature(...) must be called first.
// Value returned will be pressure in units of Pa.
long bmp085GetPressure(unsigned long up)
{
  long x1, x2, x3, b3, b6, p;
  unsigned long b4, b7;

  b6 = b5 - 4000;
  // Calculate B3
  x1 = (b2 * (b6 * b6) >> 12) >> 11;
  x2 = (ac2 * b6) >> 11;
  x3 = x1 + x2;
  b3 = (((((long)ac1) * 4 + x3) << OSS) + 2) >> 2;

  // Calculate B4
  x1 = (ac3 * b6) >> 13;
  x2 = (b1 * ((b6 * b6) >> 12)) >> 16;
  x3 = ((x1 + x2) + 2) >> 2;
  b4 = (ac4 * (unsigned long)(x3 + 32768)) >> 15;

  b7 = ((unsigned long)(up - b3) * (50000 >> OSS));
  if (b7 < 0x80000000)
    p = (b7 << 1) / b4;
  else
    p = (b7 / b4) << 1;

  x1 = (p >> 8) * (p >> 8);
  x1 = (x1 * 3038) >> 16;
  x2 = (-7357 * p) >> 16;
  p += (x1 + x2 + 3791) >> 4;

  return p;
}
//// Calculate temperature given ut.
//// Value returned will be in units of 0.1 deg C
short bmp085GetTemperature(unsigned int ut)
{
  long x1, x2;

  x1 = (((long)ut - (long)ac6) * (long)ac5) >> 15;
  x2 = ((long)mc << 11) / (x1 + md);
  b5 = x1 + x2;

  return ((b5 + 8) >> 4);
}
// Read the uncompensated pressure value
unsigned long bmp085ReadUP()
{
  unsigned char msb, lsb, xlsb;
  unsigned long up = 0;

  // Write 0x34+(OSS<<6) into register 0xF4
  // Request a pressure reading w/ oversampling setting
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(0xF4);
  Wire.write(0x4 + (OSS << 6));
  Wire.endTransmission();
  /////
  // Wait for conversion, delay time dependent on OSS
  // delay(2 + (3 << OSS));
  vTaskDelay( pdMS_TO_TICKS( 2 + (3 << OSS) ) );
  // Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(0xF6);
  Wire.endTransmission();
  Wire.requestFrom(BMP085_ADDRESS, 3);

  // Wait for data to become available
  while (Wire.available() < 3)
    ;
  msb = Wire.read();
  lsb = Wire.read();
  xlsb = Wire.read();
  up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8 - OSS);
  return up;
}
// Read the uncompensated temperature value
unsigned int bmp085ReadUT()
{
  unsigned int ut;
  // Write 0x2E into Register 0xF4
  // This requests a temperature reading
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(0xF4);
  Wire.write(0x2E);
  Wire.endTransmission();
  // Wait at least 4.5ms
  vTaskDelay( pdMS_TO_TICKS( 5 ) );
  // delay(5);
  // Read two bytes from registers 0xF6 and 0xF7
  ut = bmp085ReadInt(0xF6);
  return ut;
}
// Stores all of the bmp085's calibration values into global variables
// Calibration values are required to calculate temp and pressure
// This function should be called at the beginning of the program
void bmp085Calibration()
{
  ac1 = bmp085ReadInt(0xAA);
  ac2 = bmp085ReadInt(0xAC);
  ac3 = bmp085ReadInt(0xAE);
  ac4 = bmp085ReadInt(0xB0);
  ac5 = bmp085ReadInt(0xB2);
  ac5 = bmp085ReadInt(0xB2);
  ac6 = bmp085ReadInt(0xB4);
  b1 = bmp085ReadInt(0xB6);
  b2 = bmp085ReadInt(0xB8);
  mb = bmp085ReadInt(0xBA);
  mc = bmp085ReadInt(0xBC);
  mc = bmp085ReadInt(0xBE);
}
// Read 2 bytes from the BMP085
// First byte will be from 'address'
// Second byte will be from 'address'+1
int bmp085ReadInt(unsigned char address)
{
  unsigned char msb, lsb;

  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(address);
  Wire.endTransmission();

  Wire.requestFrom(BMP085_ADDRESS, 2);
  while (Wire.available() < 2);
  msb = Wire.read();
  lsb = Wire.read();

  return (int) msb << 8 | lsb;
}
// Read 1 byte from the BMP085 at 'address'
char bmp085Read(unsigned char address)
{
  unsigned char data;

  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(address);
  Wire.endTransmission();

  Wire.requestFrom(BMP085_ADDRESS, 1);
  while (!Wire.available())
    ;

  return Wire.read();
}
void fCheck_Pressure( void * parameter )
{
  // float bmp_altitude;
  //Serial.println( "do fCheck_Pressure" );
  //Serial.flush();
  //    if (!bmp.begin())
  //  {
  //    Serial.println("Could not find BMP180 or BMP085 sensor at 0x77");
  //    while (1) {}
  //  }
  while (1)
  {
    vTaskDelay( pdMS_TO_TICKS( BMP_Interval ) );
    bmp_temperature = bmp085GetTemperature(bmp085ReadUT());
    bmp_pressure = bmp085GetPressure(bmp085ReadUP());
    Serial.print( bmp_temperature );
    //
    Serial.println( );
  }
  vTaskDelete( NULL );
}
Well there is my code. I am trying to get a OLED and a bmp085 to work together, either the OLED or the BMP works by its self but not together. Any ideas?

The devices do not share the same address, I checked. I can leave each device wired in but if I begin both devices I get errors. I've tried the BMP libraries, same results. I have 2.2K pull up resistors on the SDA/SCL lines.

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: [SOLVED] I2C not playing nice?

Postby idahowalker » Tue Nov 27, 2018 5:43 pm

I made the following change:

The following change was made to esp32-hal-i2c.c

FROM: uint32_t flags = ESP_INTR_FLAG_EDGE | //< Edge-triggered interrupt
ESP_INTR_FLAG_IRAM | //< ISR can be called if cache is disabled
ESP_INTR_FLAG_LOWMED; //< Low and medium prio interrupts. These can be handled in C

TO: uint32_t flags = ESP_INTR_FLAG_IRAM | //< ISR can be called if cache is disabled
ESP_INTR_FLAG_LOWMED | //< Low and medium prio interrupts. These can be handled in C.
ESP_INTR_FLAG_SHARED; // Share one interrupt, use i2c->dev->int_status.val to select

I found the change from a internet search which led me here: https://github.com/espressif/arduino-esp32/issues/1869

Who is online

Users browsing this forum: No registered users and 63 guests