sparkfun / SparkFun_AS3935_Lightning_Detector_Arduino_Library

Other
37 stars 19 forks source link

SPI port speed increases after lightning.beginSPI #13

Closed Greece2 closed 4 years ago

Greece2 commented 4 years ago

Using an ATmega328P, internal 1 MHZ osc., the SPI bus works fine with all other components like LCD-displays etc. up to the moment where the statement lightning.beginSPI(spiCS, 2000000) seems to mess up the speed of the SPI port, checked that with an oscilloscope. Much too fast now, i.e. for the LCD-display, only garbled characters. After having a look at the source file of this lib, I found this routine:

bool SparkFun_AS3935::beginSPI(uint8_t user_CSPin, uint32_t spiPortSpeed, SPIClass &spiPort) 
{
  // Startup time requires 2ms for the LCO and 2ms more for the RC oscillators
  // which occurs only after the LCO settles. See "Timing" under "Electrical
  // Characteristics" in the datasheet.  
  delay(4);
  // I'll be using this as my indicator that SPI is to be used and not I2C.   
  _i2cPort = NULL; 
  _spiPort = &spiPort; 
  _spiPortSpeed = spiPortSpeed; // Make sure it's not 500kHz or it will cause feedback with antenna.
  _cs = user_CSPin;
  pinMode(_cs, OUTPUT); 
  digitalWrite(_cs, HIGH);// Deselect the Lightning Detector. 
  _spiPort->begin(); // Set up the SPI pins. 

  // Bit order is different for ESP32
#ifdef ESP32 
    SPI.setBitOrder(SPI_MSBFIRST);
#else
    SPI.setBitOrder(MSBFIRST);
#endif
  return true; 

So, what is "spiPortSpeed" ? 2000000 (like in the expample) meaning 2000000 Hz -> 2MHz ?! What are valid values for this parameter ? And the third parameter of the function call "SPIClass &spiPort" is never mentioned in the examples nor explained. Trying to slow the SPI down again with SPI.setClockDivider(SPI_CLOCK_DIV64); (or values of 8, 16, 32, ... 128) doesn't seem to have any effect now. Works fine before lightning.beginSPI(spiCS, 2000000) is issued.

Any idea how not to mess up the SPIport (speed) or how to set it correctly in this lib ?

Regards, Greece2

edspark commented 4 years ago

I'm a bit late to the party, but hopefully I can shed some light for someone in the future. I did not like how the SPI port had to be explicitly called in the begin function so that has been changed. Now you can do something a little less confusing: lightning.beginSPI(spiCS).

The way its' setup now is that it will default to 1MHz (down from 2MHz just in case there are slower peripherals), but the user can also pass it a speed like it was setup previously to get a different...speed.

As to your question, the divider you're using will work until lightning.beginSPI is called because it's setting the speed SPI port relative to the internal clock and then beginSPI is setting it to something different: 2000000 and yes that is 2MHz. Valid SPI port speed parameters depend on the upstream controller chip - your ATmega 328 in this case. You'll have to do some googling to figure that out, I don't know it off the top of my head.

I suggest trying something smaller, perhaps 1000000. One thing to note: do not set it to 500 kHz because that speed will interfere with the lightning detector's antenna.