bluzDK / bluzDK-firmware

Firmware for the bluz development kit
Other
15 stars 14 forks source link

I2C sensors not found after using SPI #8

Closed Ibuprofen closed 8 years ago

Ibuprofen commented 9 years ago

https://github.com/Ibuprofen/wx-device01/tree/bluz_wx_shield_sd

This is using the sparkfun photon weather shield and an SD card directly wired into the SPI bus of the bluz.

The first iteration of the loop looks great. I see that the devices are detected and the sensor values are returned (looking at Serial1 output). The SD card stats are correctly printed to serial. After 1 minute the function to sample the sensors is again executed but it does not find the I2C sensors.

Apologies for the hacked-up libs and code. Lots of Serial output sprinkled in there too...

Ibuprofen commented 8 years ago

According to the Wire documentation a non-zero result from Wire.endTransmission() is no bueno and that is what I am seeing. It would seem like there is an underlying issue somewhere. Assuming this code is okay, of course.

#include <application.h>

void beginEndSPI() {
  Serial1.println("SPI.begin()");

  SPI.begin();
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV4);

  Serial1.println("SPI.end()");
  SPI.end();

  Serial1.println("");
}

void beginEndWire() {
  Serial1.println("Wire.begin()");

  Wire.begin();

  Wire.beginTransmission(0x40);
  Wire.write(0xFC);
  Wire.write(0xC9);
  byte result = Wire.endTransmission();

  Serial1.print("Wire.endTransmission() result: ");
  Serial1.println(result);

  if (result == 3) {
    Serial1.println(">>>>> 3: internal driver error");
  }

  Wire.requestFrom(0x40, 1);

  if (Wire.read() == 0x32) {
    Serial1.println("HTU21D Found");
  } else {
    Serial1.println("HTU21D NOT Found");
  }

  Serial1.println("Wire.end()");
  Wire.end();
}

void setup() {
  Serial1.begin(9600);

  Serial1.println("");
  Serial1.println("Hello World");
}

void loop()
{
  const unsigned long sampleInterval = 1 * 30 * 1000UL;
  static unsigned long lastSampleTime = 0 - sampleInterval; 

  unsigned long now = millis();
  if (now - lastSampleTime >= sampleInterval)
  {
    lastSampleTime += sampleInterval;

    beginEndWire();
    beginEndSPI();
  }

  System.sleep(SLEEP_MODE_DEEP);
}
Ibuprofen commented 8 years ago

I am not seeing errors from this code anymore. The only change I made was to change:

System.sleep(SLEEP_MODE_DEEP); to System.sleep(SLEEP_MODE_CPU);

While I haven't tried to do anymore than what is in that test code I think the specific problem here is solved! Thanks.