Author Topic: SPI issue?  (Read 6028 times)

8thscale

  • Jr. Member
  • **
  • Posts: 3
    • View Profile
SPI issue?
« on: September 19, 2016, 05:29:39 AM »
Has anyone tried to use any other SPI devices while plugged into the Cleo?

I have a thermocouple board, CS can be pins 3, 4, 5 & 6. When unplugged (from the Cleo) all is well. I am not using any cleo code in this sketch.

I've started looking at signal differences between plugged and unplugged.
SCK (pin 13) seems ok. MISO (pin 12) is not. MOSI (pin 11) is not used. CS pins are ok.
Thank you!

prashanthb

  • Full Member
  • ***
  • Posts: 23
    • View Profile
Re: SPI issue?
« Reply #1 on: September 21, 2016, 05:14:47 PM »
Hi,

Kindly let us know the thermocouple board used. Kindly experiment setting D10 to be output GPIO and a value of 1.

it-argy

  • Full Member
  • ***
  • Posts: 10
    • View Profile
Re: SPI issue?
« Reply #2 on: November 10, 2016, 08:41:05 PM »
Hi all togehter,
I do have a similar problem like 8thscale: trying to use CleO and the BME280 sensor of Adafruit with SPI as a data logger, I do not succeed to get the BME280 SPI working while Cleo is connected to Nero.

I already learned by longer Google sessions to place

"pinmode(10, OUTPUT);
digitalWrite(10, HIGH)"

BEFORE any SPI call or initialization in a sketch. With that CLeO works and shows the bar graph with the adopted project 13. But the SPI activity of BME280 does not initialize.

From my understanding of SPI I think MOSI and MISO schould have tri-state ports for all slave devices.

So my first question, what is the situation with CleO?

If the tri-state is not necessary, what else has to be done to get the SPI work with multiple devices?

Thanks for any help.

Fred Dart

  • CEO of FTDI
  • Administrator
  • Full Member
  • *****
  • Posts: 74
  • Welcome...
    • View Profile
    • FTDI CleO Page
Re: SPI issue?
« Reply #3 on: November 17, 2016, 06:49:10 AM »
What pin are you using for SPI CS on the BME280. Note : it cannot be the same as CleO.

Fred Dart

  • CEO of FTDI
  • Administrator
  • Full Member
  • *****
  • Posts: 74
  • Welcome...
    • View Profile
    • FTDI CleO Page
Re: SPI issue?
« Reply #4 on: November 17, 2016, 02:48:19 PM »
To clarify -
"pinmode(10, OUTPUT);
digitalWrite(10, HIGH)"
This is necessary, no matter if you choose pin 10 as the CS or not. It's a feature of the AtMega and sets the AtMega SPI to Master mode. In theory you can use any spare IO pin as the CS control **but** regardless you must do the necessary with pin 10 first. All SPI peripherals share MISO, MOSI and SCLK **but** must have individual CS pins.
Hope this helps - if not post a code snippet of what you use to access the BME280. If needed we'll purchase one ( looks like a nice peripheral ) and try it out for ourselves.
Cheers,
Fred.

it-argy

  • Full Member
  • ***
  • Posts: 10
    • View Profile
Re: SPI issue?
« Reply #5 on: November 22, 2016, 04:34:16 PM »
Hi Fred,
thanks for your info. I think I did it already in that way.

The Software is a combination of your "Project 13" and the Adafruit "BME280-test" - I skipped the intros for saving code lines. Up to now there is no data logging feature implemented, that'll be the next step.

Thanks for your help.

Regards,
Axel

Code: [Select]
/* ARGY CLEO Datenlogger mit BME280, Temperatur, Luftdruck, Feuchte
 */
 
#include <SPI.h>
#include <CleO.h>
#include <Adafruit_BME280.h>

#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 8    //do not interfere with CS of CleO(10)

#define BME1_SCK 13
#define BME1_MISO 12
#define BME1_MOSI 11
#define BME1_CS 7    //do not interfere with CS of CleO(10)


Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK);  // Software SPI
Adafruit_BME280 bme1(BME1_CS, BME_MOSI, BME_MISO, BME_SCK);  // Software SPI
//Adafruit_BME280 bme;  //  I2C

#define SEALEVELPRESSURE_HPA (1013.25)
#define SCREEN_WIDTH     320

int nsamples;
static float samples[3][32] = {0.0};
static uint32_t colors[3] = {DEEP_SKY_BLUE, GOLD, WHITE};
char text[][20] = {"Hoehe", "Feuchte", "Temperatur"};
static float temperature = 0.0;
static float humidity = 0.0;
static float altitude = 0.0;
static float temperature1 = 0.0;
static float humidity1 = 0.0;
static float altitude1 = 0.0;

void setup() {

//  disable CleO SPI
  pinMode(10, OUTPUT);               //  set CS for CleO
  digitalWrite(10, HIGH);
//  disable BME SPI
  pinMode(BME_CS, OUTPUT);           //  set CS for BME
  digitalWrite(BME_CS, HIGH);
//  disable BME1 SPI
  pinMode(BME1_CS, OUTPUT);           //  set CS for BME
  digitalWrite(BME1_CS, HIGH);

/* Initialize Serial */
  Serial.begin(9600);
  Serial.println(F("BME280 test"));

  if (!bme.begin()) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
//    while (1);
  }
  if (!bme1.begin()) {
    Serial.println("Could not find a valid second BME280 sensor, check wiring!");
//    while (1);
  }
  if (!CleO.begin()) {
    Serial.println("CleO does not work, check wiring!");
//    while (1);
  }
  CleO.DisplayRotate(2, 0);
}

void loop() {
  /*    Stores all recording samples collected from 3 channels in an array
    Latest samples will be stored at highest index in an array */
 
  CollectTemperatureHumidityAltitude();
  addsamples(altitude, humidity, temperature);

  /* Start drawing the screen shot */
  CleO.Start();

  float mn, mx, scale;
  CleO.LineWidth(2);

  /* For each channel */
  for (int chan = 0; chan < 3; chan++) {
    int ybase = 107 + 107 * chan;

    /* Draw a rectangle section in which reading will be drawn */
    CleO.RectangleExt(0, ybase, 400, 90, 0x303030, BL, 0, 0);
    CleO.StringExt(FONT_SANS_1, 400, ybase - 105, colors[chan], TR, 0, 0, text[chan]);

    mn = mx = samples[chan][31];
    float *s = samples[chan];

    /* from all samples in a channel, get min and max */
    for (int i = (32 - nsamples); i < 32; i++) {
      mn = min(mn, s[i]);
      mx = max(mx, s[i]);
    }

    /* Get the scaling value */
    if (mx != mn)
      scale = 90 / (mx - mn);
    else
      scale = 0;

    /* Select differnet color for each channel */
    CleO.LineColor(colors[chan]);

    /* Connect all samples for a channel with a line */
    for (int i = (32 - nsamples); i < 31; i++) {
      float x0 = i * (400. / 31);
      float x1 = (i + 1) * (400. / 31);
      float y0 = ybase - scale * (s[i] - mn);
      float y1 = ybase - scale * (s[i + 1] - mn);
      CleO.Line(x0, y0, x1, y1);
    }

    /* Get the y coordinate for the last sample and use it for drawing reading
      as text */
    char val[9];
    sprintf(val, "%d", int(s[31]));
    float y = ybase - scale * (s[31] - mn);
    CleO.StringExt(FONT_SANS_3, 408, y, colors[chan], BL, 0, 0, val);
  }

  /* Show the screenshot */
  CleO.Show();

  delay(2500);
}
//--------------------------------------------

void addsamples(float s0, float s1, float s2) {
  for (int i = 0; i < 31; i++) {
    samples[0][i] = samples[0][i + 1];
    samples[1][i] = samples[1][i + 1];
    samples[2][i] = samples[2][i + 1];
  }
  samples[0][31] = s0;
  samples[1][31] = s1;
  samples[2][31] = s2;
  if (nsamples < 32)
    nsamples += 1;
}
//----------------------------------------------

void CollectTemperatureHumidityAltitude() {
  /* Collect temperature, Humidity and pressure */

    temperature = bme.readTemperature();
    altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
    humidity = bme.readHumidity();

    Serial.print("Temperature = ");
    Serial.print(temperature);
    Serial.println(" *C");

    Serial.print("Pressure = ");
    Serial.print(bme.readPressure() / 100.0F);
    Serial.println(" hPa");

    Serial.print("Approx. Altitude = ");
    Serial.print(altitude);
    Serial.println(" m");

    Serial.print("Humidity = ");
    Serial.print(humidity);
    Serial.println(" %");

delay(2000);
    temperature1 = bme1.readTemperature();
    altitude1 = bme1.readAltitude(SEALEVELPRESSURE_HPA);
    humidity1 = bme1.readHumidity();
   
    Serial.print("Temperature-1 = ");
    Serial.print(temperature1);
    Serial.println(" *C");

    Serial.print("Pressure-1 = ");
    Serial.print(bme1.readPressure() / 100.0F);
    Serial.println(" hPa");

    Serial.print("Approx. Altitude-1 = ");
    Serial.print(altitude1);
    Serial.println(" m");

    Serial.print("Humidity-1 = ");
    Serial.print(humidity1);
    Serial.println(" %");

    Serial.println();   
}


Fred Dart

  • CEO of FTDI
  • Administrator
  • Full Member
  • *****
  • Posts: 74
  • Welcome...
    • View Profile
    • FTDI CleO Page
Re: SPI issue?
« Reply #6 on: November 24, 2016, 05:54:26 AM »
Aha - thanks for the code snippet. I think the problem may be that you are using hardware SPI for the CleO, which is fine and software SPI for the BME280, which is also fine **but** I don't think you can share any pins between the two implementations. Try moving BME_SCK, BME_MISO and BME_MOSI to unused pins and see if that works. Alternatively, change the BME280 to use hardware SPI not software SPI.
Hope this helps - give it a try and let me know.

it-argy

  • Full Member
  • ***
  • Posts: 10
    • View Profile
Re: SPI issue?
« Reply #7 on: November 28, 2016, 04:42:35 PM »
Hi Dart,
thanks for your hint, it makes sense not mixing different implementations. And - not suprisingly - I separated as you suggested the BME280 SPI lines to other GPIOs and it does work.

Sorry to say your second alternative dos not work. Doing this, the BME280s are not reconized anymore and do not supply any values. If I draw off the CleO the BME280s work fine.

Unfortenately I do not have any other SPI device actually, so I cannot test other configurations.

What is the reason that hardware SPI does not work?

Thanks for your help.

Regards,
Axel


Fred Dart

  • CEO of FTDI
  • Administrator
  • Full Member
  • *****
  • Posts: 74
  • Welcome...
    • View Profile
    • FTDI CleO Page
Re: SPI issue?
« Reply #8 on: December 01, 2016, 03:16:56 AM »
Looking at the Adafruit Driver code in Adafruit_BME280.cpp,
 
Code: [Select]
   if (_sck == -1) {
      // hardware SPI
      SPI.begin();
    } else {
      // software SPI
      pinMode(_sck, OUTPUT);
      pinMode(_mosi, OUTPUT);
      pinMode(_miso, INPUT);
    }
to use hardware SPI, set _sclk to -1
Hope this works now - let me know...


it-argy

  • Full Member
  • ***
  • Posts: 10
    • View Profile
Re: SPI issue?
« Reply #9 on: December 19, 2016, 11:57:31 AM »
Hi Dart,
sorry for the late answer, working with CleO is only a small part of my professional engagement.

Yes, I understand the way, how to initialize the hardware SPI by just transferring the CS in the class definition. But that does not solve the problem.

Some posts before I asked, if the MISO line has a tri state output line. Meanwhile looking to the CleO documentation, Figure 11, sheet 3 you show the output circuit of the MISO line - and everything is clear: you use the tristate buffer 74hc125 not as such instead you use it as level shifter with fixed active state by grounding the enable input. Thus you block that SPI bus to CleO and no other device can use the bus. That's not the way it should work.

I separated Cleo from Nero and connected another 74hc125 to the MISO line enabled by CS pin 10 and now it works.

Suggestion: as you do have already a 74HC125 in use, instead of connecting the enable input permanetly to ground connect it to CS of CleO those who do want to use the SPI bus for other devices as well will be happy - now the additional BME280 also with a tri state buffer in the MISO line work well.

Best Regards,
Axel

Fred Dart

  • CEO of FTDI
  • Administrator
  • Full Member
  • *****
  • Posts: 74
  • Welcome...
    • View Profile
    • FTDI CleO Page
Re: SPI issue?
« Reply #10 on: January 03, 2017, 12:59:37 AM »
Hi Axel,
thank you for investigating the problem. I suspect you are correct, but we'll take a look at it next week when the team comes back from holiday.
Best Regards,
Fred

Rudolph

  • Full Member
  • ***
  • Posts: 9
    • View Profile
Re: SPI issue?
« Reply #11 on: May 12, 2017, 05:54:12 PM »
I just browsed the forum and found this thread with no definitive answer.
I checked the CleO35 and CleO50 schematics myself and I second the notion that this is a design flaw on CleOs side.

CleO35: U12.3 should be connected to U11.6 in order to make MISO tristate when SS is not low.
Cleo50: U9.3 should be connected to U8.6.
With of course disconnecting U12.3 or U9.3 from its pad first.

If at all this patch should be executed by a trained professional.

it-argy

  • Full Member
  • ***
  • Posts: 10
    • View Profile
Re: SPI issue?
« Reply #12 on: December 20, 2017, 02:43:19 PM »
I could focus to this subject again.

With the aid of a nice small electronic microscope, I had bought just for fun some month ago, and I managed to "pimp" both my CleOs the way I suggested some posts earlier: bending pin 1 (NOT pin 3!)  of U12 off its pcb pad and solderng a wire from that pin to pin 6 of U11.

Now CleO does not block the hardware SPI bus and one can use serveral SPI devices on the same bus, as you would expect normally.

@Fred Dart, CleO-Support
Would it be possible to change the pcb layouts CleO35 and CleO50? This would enable users to use CleO in a more professional way. Thanks for your answer

Axel