Author Topic: MoteinoM0 absent from Arduino IDE when SPI device is connected [~solved]  (Read 3529 times)

jdheinzmann

  • NewMember
  • *
  • Posts: 40
  • Country: us
I am just starting with a Moteino M0 R1 with which came with RFM69HCW - 868/915Mh module installed.  It works fine when I run the Arduino blink sketch installed from the Arduino 1.8.10 IDE and the M0 is powered via USB.  When I unplug and re-plug the USB cable from/to the PC, the COM port for the M0 disappears and reappears and the LED stops and blinks again as expected.  But when I connect a SparkFun LIS3DH IMU breakout board to the M0 via SPI, the COM port never shows up in the IDE and the LED does not blink.

Here is how I connected it:

Moteino M0LIS3DH
10!CS
GNDGND
3.3VVCC
MISOSDO/MISO
SCKSCK
MOSISDA/SDI/MOSI

I tried connecting a 100 kΩ pullup from each of the SPI lines to 3.3 V (one at a time, only one resistor that I switched around) then tried the same pulling them down to GND.  No joy.

I did find that if I break the MISO connection, blink will work and the port will show up in the IDE.  If I break just !CS, SCK, or MOSI, no joy.

Any help you can give would be appreciated (of course!)  Thanks!
« Last Edit: March 03, 2020, 04:23:12 PM by Felix »

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6867
  • Country: us
    • LowPowerLab
Re: MoteinoM0 absent from Arduino IDE when SPI device is connected
« Reply #1 on: February 04, 2020, 03:39:25 PM »
Did you write any code to initialize the SPI port?
Are we sure there is no short or fault in the MISO wiring?

jdheinzmann

  • NewMember
  • *
  • Posts: 40
  • Country: us
Re: MoteinoM0 absent from Arduino IDE when SPI device is connected
« Reply #2 on: February 08, 2020, 02:57:36 PM »
Hi, Felix.  I checked for shorts on all the SPI lines and !CS  to GND.  With the assembly unpowered, all are open.  When checking to 3.3 V, MOSI and !CS are 10 kΩ to 3.3 V which I assume are pull-ups not to be concerned with.

I was trying this only with the blink.ino code loaded into the M0 (no initialization of SPI).  So, with the accelerometer disconnected, I loaded the code I had been using with this accelerometer on an Arduino Pro Mini onto the Moteino M0.  With the accelerometer still disconnected, the M0 runs and displays what I would expect on the serial monitor as shown in the attached (I assume the values for theta are from noise occasionally picked up on the open SPI lines).  So the sketch is running. 

However, when I unplug the M0 USB, connect the accelerometer board, and re-plug the USB, the M0 is no longer seen by the Arduino IDE (as happened with blink.ino). 

Here is the beginning of my code to see if it is initializing the SPI port as you suggest:

Code: [Select]
/*
  Test out LIS3DH digital XYZ accelerometer board from Sparkfun.  Get
  it running on Moteino M0 with output up serial monitor or RF link.
*/

#if defined (MOTEINO_M0) && defined(SERIAL_PORT_USBVIRTUAL)
  #define Serial SERIAL_PORT_USBVIRTUAL // Required for Serial on Zero based boards
#endif

#include "SparkFunLIS3DH.h"
#include "SPI.h"

LIS3DH myIMU(SPI_MODE, 10); // constructed with parameters for SPI and cs pin number
 
#define RUN_CSV        (0) // stream data for debug out USB in CSV format
#define CAL            (1) // put SW in calibration mode
#define DEV1           (2) // send option 1 development data out USB port
#define DEV2           (3) // send option 2 development data out USB port
#define RUN_BIN        (4) // stream data for debug out USB in binary format

unsigned long SerialBaudRate = 115200; // 4800 19200 57600 115200 230400 460800

#define RUN_MODE      (RUN_CSV)  // RUN_CSV, CAl, DEV1, DEV2 or RUN_BIN

// Calibration values (determined experimentally)
#define X_ACC_OS      (363.0) // [count]
#define Y_ACC_OS      (351.5) // [count]
#define X_ACC_GAIN    (1.0/76.0) // [g/count]
#define Y_ACC_GAIN    (1.0/75.0) // [g/count]

// Units
#define U_RAD  (1.0)
#define U_REV  (U_RAD*2.0*PI)
#define U_DEG  (U_REV/360)
#define IU_REV (1.0/U_REV)
#define IU_DEG (1.0/U_DEG)

#define DELTA_THETA_THRESH (2.0*U_DEG) // Angle change required to update data printed (or outout over radio?)

float last_theta = 0.0; // for determing when to output next data point
String PrintStr;

unsigned int X_Acc_Raw_Max = 0;
unsigned int Y_Acc_Raw_Max = 0;
unsigned int X_Acc_Raw_Min = 1024;
unsigned int Y_Acc_Raw_Min = 1024;

unsigned int LoopCnt = 0;

unsigned long StrtTime_ms = 0;
unsigned long DeltaT_ms;

void setup()
{
  Serial.begin(SerialBaudRate);
  while (!Serial); // wait for serial port to connect. Needed for native USB port only
  Serial.println("Serial port has connected.");

  myIMU.settings.adcEnabled = 0;
  myIMU.settings.tempEnabled = 0;
  myIMU.settings.accelSampleRate = 5000;  // [Hz], Can be: 0,1,10,25,50,100,200,400,1600,5000 Hz
  myIMU.settings.accelRange = 2;      // [g], Max acceleration readable.  Can be: 2, 4, 8, 16 g
  myIMU.settings.xAccelEnabled = 1;
  myIMU.settings.yAccelEnabled = 1;
  myIMU.settings.zAccelEnabled = 0;
 
  //Call .begin() to configure the IMU
  Serial.print("myIMU.begin() returned: ");
  Serial.println(myIMU.begin()); // configure IMU and print status returned

  Serial.print("Delta theta threahold: ");
  Serial.println(DELTA_THETA_THRESH);
}

void loop() {
  //float X_Acc_Raw, Y_Acc_Raw; // [g]
 
  int16_t Raw_X, Raw_Y; // [count],
  float X, Y; // [g], floating point accelerometer readings
  float theta; // [rad], calculated angle
  unsigned long Time_ms = millis();
 
  StrtTime_ms = millis();
  X = myIMU.readFloatAccelX();
  Y = myIMU.readFloatAccelY();
  Raw_X = myIMU.readRawAccelX();
  Raw_Y = myIMU.readRawAccelY();
  DeltaT_ms = millis() - StrtTime_ms;

[snip]

Aren't these lines what is needed to initialize SPI?  First this in the global section:

Code: [Select]
#if defined (MOTEINO_M0) && defined(SERIAL_PORT_USBVIRTUAL)
  #define Serial SERIAL_PORT_USBVIRTUAL // Required for Serial on Zero based boards
#endif

#include "SparkFunLIS3DH.h"
#include "SPI.h"

LIS3DH myIMU(SPI_MODE, 10);
and then this in setup():

Code: [Select]
  Serial.println(myIMU.begin()); // configure IMU and print status returned

It seems this code is never even being run.

To ensure  my accelerometer board is still working, I took it back over to the Arduino Pro Mini and it runs fine there using the above code.


Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6867
  • Country: us
    • LowPowerLab
Re: MoteinoM0 absent from Arduino IDE when SPI device is connected
« Reply #3 on: February 08, 2020, 04:59:10 PM »
In situations like this we need to try to isolate the problem, use a divide-et-impera approach.
It does not sound like the SPI device is the problem.  BTW you dont normally need any pullups with SPI on the data lines.
I assume you only have 1 M0 to test with?
How about any other SAMD21 based device you can test the setup on?

The FLASH-MEM SS is on D8. Does your M0 have the FLASH?
The bootloader tries to interact with the FLASH during boot up. So there's a chance that the LIS3DH somehow interjects this process and causes the M0 to loop in the bootloader, never entering the sketch program. No way to tell that without a logic analyzer, now you surely must have one of those around right?  ;D
« Last Edit: February 17, 2020, 10:41:43 AM by Felix »

jdheinzmann

  • NewMember
  • *
  • Posts: 40
  • Country: us
Re: MoteinoM0 absent from Arduino IDE when SPI device is connected
« Reply #4 on: February 11, 2020, 08:36:48 AM »
Hi, Felix,

I have another M0.  I'll try it just to make sure the problem is not with the M0. 

My M0s do not have the optional 4 mbit flash.

I do not have a logic analyzer.  Time to get one.  What shall I look for on bootup?  I do have a 2 cha DSO.  (So far, I've been lucky enough to get through all my Arduino work without needing a logic analyzer.)

The 10 kΩ pull-ups appear to be built into the LIS3DH.

Thanks,
JD




Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6867
  • Country: us
    • LowPowerLab
Re: MoteinoM0 absent from Arduino IDE when SPI device is connected
« Reply #5 on: February 11, 2020, 09:19:20 AM »
I do not have a logic analyzer.  Time to get one.
Indeed, an essential tool in doing embedded systems work.

What shall I look for on bootup?  I do have a 2 cha DSO.
A SPI signal, which you decode and see if it simply stuck in a loop somehow. It could reveal if the LIS device intervenes when the MCU tries to look for the SPI Flash chip.
The idea is to determine where the M0 gets stuck, if at all. It is obvious that without the LIS the M0 does its job, but with the LIS it does not enter the sketch part anymore (or at least not so far as to enumerate the USB CDC which causes a serial port to show up).

The 10 kΩ pull-ups appear to be built into the LIS3DH.
Appear is pretty good, but being sure is better. The Datasheet, usually not as long as the Bible, but like it, is very helpful to reveal if you're on the good path or not.  ;)

jdheinzmann

  • NewMember
  • *
  • Posts: 40
  • Country: us
Re: MoteinoM0 absent from Arduino IDE when SPI device is connected
« Reply #6 on: February 15, 2020, 10:42:15 AM »
Well...

I bought one of the cheap 24 MHz 8-bit logic analyzers and set it up via USB using PulseView on my Win10 machine.  Monitoring the 3 SPI lines and the M0's 10, 9 and 8 pins, first without the LIS3DH connected, I found that there was lots of SPI activity, but the USB port does not show up on the Arduino IDE.  But if I disconnect the Logic Analyzer probe from MISO, the USB port appears.  So just the presence of the L.A. causes the problem -- during which MISO stays high.  So I tried pulling MISO low with 10 kΩ and the serial port shows up!  Then there is very little activity on SPI.

But with the 10 kΩ from MISO to GND, it doesn't work with the LIS3DH connected.  Looking further into the LIS2DH, with it unpowered, the resistance from SDO to 3.3V is 10 kΩ.  (I am connecting SDO to MISO on the M0.) The IC's datasheet says that SDO has an internal pull-up of around 20 kΩ when powered at 3.6 V.  (It is higher at lower voltages - huh?).  And the breakout board schematic shows a 10 kΩ resistor to 3.3V.  So if the M0 has trouble with MISO being pulled up, perhaps that is the problem.  But why would the M0 stumble over this?

So it only makes sense to try pulling MISO down with a 1 kΩ resistor, right?  Now the serial port shows up with the LIS3DH connected! 

Here is the (minimal) SPI activity with 1 kΩ from MISO to GND when I power up the M0 by plugging in the USB.

(See: SPI_1kPullDownLisConnected_Part2.PNG attached)

Then a bit later there is this little bit of activity:

  (See: SPI_1kPullDownLisConnected_Part1.PNG attached)

With the pull-down removed, here is what the very busy SPI activity looks like after pluging in USB.  It seems to go on forever:

  (See: SPI_NoPullDownLisConnected_Part2.PNG attached)

That's all I have for now.

-JD

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6867
  • Country: us
    • LowPowerLab
Re: MoteinoM0 absent from Arduino IDE when SPI device is connected
« Reply #7 on: February 17, 2020, 10:44:17 AM »
Did you try using a pullup on CS only? That is very reasonable. I have seen unstable SPI devices that require a CS pullup to keep it from interfering with other SPI datastreams.
Like i said, I would never expect needing pull resistors on any of the data lines. You never see that anywhere in datasheets or app notes.

jdheinzmann

  • NewMember
  • *
  • Posts: 40
  • Country: us
Re: MoteinoM0 absent from Arduino IDE when SPI device is connected
« Reply #8 on: February 21, 2020, 12:49:24 PM »
Hi, Felix.  I took the bare M0 and pulled MSIO up to 3.3 V with 470 kΩ and the serial port would not show up in the Arduino IDE.  At 675 kΩ it still would not work.  At 820 kΩ it worked.  With the 470 kΩ pullup on MISO I added a 10 kΩ pull-up to D10 (the pin I am using for !CS) and of course it made no difference with no SPI device connected.  With the 470 kΩ pullup removed from MISO and the SPI device connected 10 kΩ to 3.3 V made no difference.

Curious to see if I just slowed down the pulling up of MISO, I put a 47 nF cap in series with a 10 kΩ resistor between GND and MISO.  With that I found I could lower the pull-up on MISO to 20 kΩ and have it start up okay.  With a 10 kΩ pull-up it would not.

Why is the M0 so sensitive to a resistance as high as even 675 kΩ from MISO to 3.3 V?  And to connecting only the logic analyzer to MISO?  It seems that the M0 has MISO in a high impedance state when the M0 is not expecting MISO voltage level to be high during the M0 boot-up process.  And it is assuming that MISO is being driven high by a SPI device at that point.  675 kΩ is hardly a "driving" source impedance.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6867
  • Country: us
    • LowPowerLab
Re: MoteinoM0 absent from Arduino IDE when SPI device is connected
« Reply #9 on: February 21, 2020, 06:41:04 PM »
Hard to answer the question without doing more empirical analysis.
But a simple answer could be to replace the bootloader with one that does not contain the SPI activity used by FLASHBOOT (aka DualOptiboot in for AVR Moteinos).
I am working on a new UF2 bootloader which will allow replacing itself very easily (for any new boards shipped with it).
If you have Atmel ICE or want to return M0's to me to reflash them with new bootloader, then we can find the answer quickly if the bootloader has anything to do with it.

jdheinzmann

  • NewMember
  • *
  • Posts: 40
  • Country: us
Re: MoteinoM0 absent from Arduino IDE when SPI device is connected
« Reply #10 on: February 22, 2020, 05:08:15 PM »
Hi, Felix.  I do not have an Atmel ICE.  It would be easiest for me to just order another M0 with the new bootloader.  But I don't see how to add a note to the order page to request that.  How best to proceed?

Update:  Actually, I see a place to comment on the order's shipping page.  I'll request it there.
« Last Edit: February 25, 2020, 09:29:20 AM by Felix »

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6867
  • Country: us
    • LowPowerLab
Re: MoteinoM0 absent from Arduino IDE when SPI device is connected
« Reply #11 on: February 24, 2020, 09:19:22 AM »
Got it, once you receive the board let me know how it goes.

jdheinzmann

  • NewMember
  • *
  • Posts: 40
  • Country: us
Re: MoteinoM0 absent from Arduino IDE when SPI device is connected
« Reply #12 on: February 24, 2020, 10:16:35 PM »
Will do, Felix. 

It occurs to me that you could easily reproduce the problem yourself by connecting 470 kΩ from MISO to 3.3 V on a flashless M0. Now you surely have one of those around right?  ;D

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6867
  • Country: us
    • LowPowerLab
Re: MoteinoM0 absent from Arduino IDE when SPI device is connected
« Reply #13 on: February 25, 2020, 11:18:41 AM »
Well now that you have a board going your way, let's see what you find and I can take it from there.

jdheinzmann

  • NewMember
  • *
  • Posts: 40
  • Country: us
Re: MoteinoM0 absent from Arduino IDE when SPI device is connected
« Reply #14 on: March 01, 2020, 05:19:10 PM »
Hi, Felix.  I've received the new M0 from you with the new UF2 bootloader.  I was expecting it to be flashless but I notice that it has a flash chip installed.  Does the UF2 bootloader require the flash chip?

With a 10 kΩ pull-up from MISO to 3.3 V the USB port shows up in the IDE just fine.  With the LIS3DH accelerometer connected, it also boots and enumerates the serial port just fine.  I am even able to load and run my sketch which successfully reads the accelerometer and sends data across the radio link without adding the MISO 1 kΩ pull-down I needed before.

Does the presence of the flash chip have anything to do with this?  My first two M0s do not have the flash option.

What does this tell you?  Where shall we go from here?

Thanks. -JD