Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

passing SPIClass for ESP32 pin remapping #11

Open
wBrhy2 opened this issue Apr 24, 2023 · 14 comments
Open

passing SPIClass for ESP32 pin remapping #11

wBrhy2 opened this issue Apr 24, 2023 · 14 comments

Comments

@wBrhy2
Copy link

wBrhy2 commented Apr 24, 2023

There does not seem to be support for passing along SPIClass to define which pins to use on the ESP32.

@wollewald
Copy link
Owner

@wBrhy2 , you are right. I thought it was a good idea to "outsource" SPI.begin() to the library, but maybe it wasn't.

I have updated the library (version 2.2.3) and now you can pass the changed SPI pins when creating the ADXL345_WE object. You find the details in the example sketch ADXL345_SPI_basic_data.ino. It works fine on my side.

@wBrhy2
Copy link
Author

wBrhy2 commented Apr 26, 2023

That's amazing, thank you, because my SPI.h was throwing up errors when I was trying to give it different default pins for some reason, so this is very much appreciated!

@Ricardosgeral
Copy link

Ricardosgeral commented Apr 27, 2023

Hi @wollewald

There is also a problem when using the esp8266.
I've tried the basic example with I2C on a Wemos D1 mini and I'm getting this error:

**...\EletroProjects\libraries\ADXL345_WE\src\ADXL345_WE.cpp:27:56: error: no matching function for call to SPIClass::begin(int&, int&, int&, int&)'
27 | _spi->begin(sckPin, misoPin, mosiPin, csPin);

...\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\SPI/SPI.h:56:8: note: candidate: 'void SPIClass::begin()'
56 | void begin();**

The program is looking for SPI i think... What's happening?

thanks in advance

@wollewald
Copy link
Owner

@Ricardosgeral , you are right. This was an unwanted side-effect of my last release. I had added the function:

_spi->begin(sckPin, misoPin, mosiPin, csPin);

...to enable users to change the SPI pins for ESP32 boards. This causes a compiler error on non-ESP32 boards, eventhough it's not used on these boards. I have now embraced this function by an #ifdef ESP32 clause. I have tried it and it works.

Thanks for raising the issue and sorry for the inconvenience!

@Ricardosgeral
Copy link

Ricardosgeral commented Apr 27, 2023 via email

@yukiloh
Copy link

yukiloh commented Jun 21, 2023

Maybe I had same problem?But I mixed other lib and solved it....
Using SPI and conifg CS,MOSI,MISO,SCL pins did not work in my ESP32(not dev kit).

// #define MOSI_PIN 23
// #define MISO_PIN 19
// #define SCK_PIN 18
//ADXL345_WE myAcc = ADXL345_WE(&SPI, CS_PIN, spi, MOSI_PIN, MISO_PIN, SCK_PIN);

But when I use "tft_espi" lib,it will config SPI pins too.
https://github.com/Bodmer/TFT_eSPI
Magically,they worked so good...
Just need config the CS pin,likeADXL345_WE myAcc = ADXL345_WE(CS_PIN, true);
I have no clue about it,just report this thing...

@wollewald
Copy link
Owner

@yukiloh , this should be a different issue. Basically the solution for the original issue related with ESP32 boards worked, but it had a side effect on other boards. I also fixed that for the other boards (tried with an Arduino Nano) and it still also worked on the ESP32. So, which ESP32 are you using (i.e. which board do you select in the Ardino IDE)? And what is the problem you experienced? Is it the error message that Ricardosgeral received? I need this information to solve the issue, because otherwise it's difficult to to work on a problem that I can''t reproduce.

@yukiloh
Copy link

yukiloh commented Jun 21, 2023

I used ESP32-WROOM-32D modules and customed pcb.
Chips guide is:
https://www.espressif.com.cn/sites/default/files/documentation/esp32-wroom-32d_esp32-wroom-32u_datasheet_en.pdf
from Espressif homepage:
https://www.espressif.com.cn/en/support/documents/technical-documents?keys=&field_type_tid%5B%5D=266


Error message is:

ADXL345_Sketch - Basic Data
ADXL345 not connected!
Data rate: 0.10 Hz  /  g-Range: 2g
//Try to change config but failed
//myAcc.setDataRate(ADXL345_DATA_RATE_25);
//myAcc.setRange(ADXL345_RANGE_4G);

I check the code

    if (!myAcc.init()) {
        Serial.println("ADXL345 not connected!");
    }

// go to myAcc.init(),and find:
    if(!((readRegister8(ADXL345_DATA_FORMAT)) & (1<<ADXL345_FULL_RES))){
        return false;
    }

Try to print the result:

    Serial.println("result:");
    bool result1 = readRegister8(ADXL345_DATA_FORMAT);         //false
    Serial.println(result1);

    bool result2 = (1<<ADXL345_FULL_RES);                                 //true
    Serial.println(result2);

Not very familiar for these code so I gived up...


And I checked the TFT_eSPI code,

// TFT_eSPI.h

//...

// Some ST7789 boards do not work with Mode 0    //In my case: #define ILI9341_2_DRIVER
#ifndef TFT_SPI_MODE
  #if defined(ST7789_DRIVER) || defined(ST7789_2_DRIVER)
    #define TFT_SPI_MODE SPI_MODE3                //not this
  #else
    #define TFT_SPI_MODE SPI_MODE0                //enabled here
  #endif
#endif

//...

So I try to changed the code in ADXL345_WE::init()
mySPISettings = SPISettings(4000000, MSBFIRST, SPI_MODE0)
It's still not good,print ADXL345 not connected!

If mix two lib,it will work nice, looks like:
1687354979727 jpg


And...I just report this problem(not a new issue)
Hope someone encounter the same phenomenon will get some hit...

@wollewald
Copy link
Owner

@yukiloh , thank you for all the information, I really appreciate that. And I don't want to steal more of your time than necessary.

Believe me, it is a new issue. The original issue was to add a new feature and unfortunately this lead to another issue on non-ESP32 boards. I fixed it and it works at least on ESP32, ESP8266 and ATmega328P based boards.

However, it does not work on your side, so there must be a difference between what I do and what you do. The hint that it works if you mix the libs will probably be very helpful, but only after I have reproduced the issue. Otherwise it's difficult for me to work on that.

I have just tried it again and the following sketch works on an ESP32-WROOM-32 Board:

#include<ADXL345_WE.h>
#include<SPI.h>
#define CS_PIN 5   
#define MOSI_PIN 23
#define MISO_PIN 19
#define SCK_PIN 18

bool spi = true;    

ADXL345_WE myAcc = ADXL345_WE(&SPI, CS_PIN, spi, MOSI_PIN, MISO_PIN, SCK_PIN);

void setup(){
  Serial.begin(9600);
  Serial.println("ADXL345_Sketch - Basic Data");
  if(!myAcc.init()){
    Serial.println("ADXL345 not connected!");
  }
}

void loop() {
  xyzFloat raw = myAcc.getRawValues();
  xyzFloat g = myAcc.getGValues();
  
  Serial.print("Raw-x = ");
  Serial.print(raw.x);
  Serial.print("  |  Raw-y = ");
  Serial.print(raw.y);
  Serial.print("  |  Raw-z = ");
  Serial.println(raw.z);

  Serial.println();  
  delay(1000); 
}

Do you have the chance to try this one?

For my trials I used the Arduino IDE 1.8.19 and 2.1.0. Both use the ESP32 Package 1.0.6. The Board I have chosen is "ESP32 Dev Module":

Screenshot 2023-06-21 184648

Can you provide the complete sketch that is not working on your side? Or at least the first part from the beginning to the point when you have include all libraries and initiated all objects?

I know you only report the problem. But need your help to reproduce the issue.

@yukiloh
Copy link

yukiloh commented Jun 22, 2023

@wollewald
I tested those code, it still not work...

Serial print: (I moved the device,so Raw-x is changing)

ADXL345_Sketch - Basic Data
ADXL345 not connected!
Raw-x = -1928.00  |  Raw-y = 0.00  |  Raw-z = 0.00

Raw-x = -1946.00  |  Raw-y = 0.00  |  Raw-z = 0.00

Raw-x = -2024.00  |  Raw-y = 0.00  |  Raw-z = 0.00

Raw-x = -1997.00  |  Raw-y = 0.00  |  Raw-z = 0.00

Raw-x = -1955.00  |  Raw-y = 0.00  |  Raw-z = 0.00

Raw-x = 82.00  |  Raw-y = 0.00  |  Raw-z = 0.00

Raw-x = 98.00  |  Raw-y = 0.00  |  Raw-z = 0.00

Raw-x = 72.00  |  Raw-y = 0.00  |  Raw-z = 0.00

Raw-x = 10.00  |  Raw-y = 0.00  |  Raw-z = 0.00

Raw-x = -1930.00  |  Raw-y = 0.00  |  Raw-z = 0.00

Raw-x = -1930.00  |  Raw-y = 0.00  |  Raw-z = 0.00

It looks the sensor is working,just got the wrong data?
I don't know how does SPI works.
I guess tft_espi used a different way to init SPI?
Likes ↓,in "TFT_eSPI.cpp",line 640
spi.begin(TFT_SCLK, TFT_MISO, TFT_MOSI, -1); // This will set MISO to input


And here is my code,set#define USE_TFT 1 to enable TFT_eSPI

#define USE_TFT 01      // set 0 to OFF,1 to enable TFT_eSPI

#if USE_TFT
#include "TFT_eSPI.h"

#define POW_PIN 12
#define ROTATION 2

TFT_eSPI tft;

//to show the message on the screen
void show_msg(const String &msg, uint8_t order, bool is_first = false);

#endif

#include "ADXL345_WE.h"
#include <SPI.h>

#define CS_PIN 2

#if USE_TFT
ADXL345_WE myAcc = ADXL345_WE(CS_PIN, true);
#else
#define MOSI_PIN 23
#define MISO_PIN 19
#define SCK_PIN 18
bool spi = true;
ADXL345_WE myAcc = ADXL345_WE(&SPI, CS_PIN, spi, MOSI_PIN, MISO_PIN, SCK_PIN);

#endif

void setup() {
    Serial.begin(115200);

    delay(1000);

#if USE_TFT
    /* tft settings,in file "User_Setup.h"
     * #define TFT_MISO 19     //SDO
     * #define TFT_MOSI 23      //SDI
     * #define TFT_SCLK 18
     * #define TFT_CS   5       // Chip select control pin
     * #define TFT_DC   4       // Data Command control pin
     * #define TFT_RST  17      // Reset pin (could connect to RST pin)
     * #define TFT_BL   15      // LED back-light
     * */
    tft.init();
    pinMode(TFT_BL, OUTPUT);            //screen LED light
    digitalWrite(TFT_BL, HIGH);
    tft.setRotation(ROTATION);
    Serial.println("tft init done");
#endif

    Serial.println("ADXL345_Sketch - Basic Data");
    if (!myAcc.init()) {
        Serial.println("ADXL345 not connected!");
    }

    myAcc.setCorrFactors(-259.0, 243.0, -86.0, 425.0, -68.0, 439.0);
    myAcc.setDataRate(ADXL345_DATA_RATE_25);
    delay(100);
    Serial.print("Data rate: ");
    Serial.print(myAcc.getDataRateAsString());

    myAcc.setRange(ADXL345_RANGE_4G);
    delay(1000);
    Serial.print("  /  g-Range: ");
    Serial.println(myAcc.getRangeAsString());
    Serial.println();
}

void loop() {
    xyzFloat raw = myAcc.getRawValues();
    xyzFloat g = myAcc.getGValues();

    Serial.print("Raw-x = ");
    Serial.print(raw.x);
    Serial.print("  |  Raw-y = ");
    Serial.print(raw.y);
    Serial.print("  |  Raw-z = ");
    Serial.println(raw.z);

    Serial.print("g-x   = ");
    Serial.print(g.x);
    Serial.print("  |  g-y   = ");
    Serial.print(g.y);
    Serial.print("  |  g-z   = ");
    Serial.println(g.z);

    Serial.println();

#if USE_TFT
    uint8_t order = 0;

    show_msg(String(raw.x), ++order, true);
    show_msg(String(raw.y), ++order);
    show_msg(String(raw.z), ++order);

    show_msg(String(g.x), ++order);
    show_msg(String(g.y), ++order);
    show_msg(String(g.z), ++order);
#endif

    delay(1000);
}


void show_msg(const String &msg, uint8_t order, bool is_first) {
    uint8_t span = 20;  //px,font height is 16px

    if (is_first) {
        tft.fillScreen(TFT_BLACK);
        tft.setTextDatum(MC_DATUM);
        tft.setTextColor(TFT_WHITE, TFT_BLACK, true);
    }
    tft.drawString(msg, TFT_HEIGHT / 2, 10 + span * order, 2);
}

I designed the PCB,so I have no adxl345 module(only the chip)...
Here is my PCB schematic,and 3D model
image
image

I'll buy a new adxl345 module to test it,and if it's still not good I will create a new issue.
It will take some days...
Sorry for taking up a lot of your time| ᐕ)⁾⁾

@wollewald
Copy link
Owner

Hi @yukiloh , this is a miracle to me, but I like challenges! It will be interesting to see if the ADXL345 you ordered will work. Let me think in the meantime about this issue. Initial thoughts: Sometimes incompatibilities of libraries could occur, but then at least the code I sent should have worked. So it might be related to the wiring. Not saying you did anything wrong! But maybe there is something different between your wiring and the wiring of the modules which requires different settings for SPI.

@wollewald
Copy link
Owner

Only one small question in between: When you tested the code I sent, you have probably changed the CS pin to 2, right? I ask because I think this is hardwired on your PCB. Anything else you changed?

@wollewald
Copy link
Owner

I tried a bit. Still I can't say why the code I sent is not working on your PCB as long as you took the right CS pin and didn't change or added anything else.

Things I found:

  1. If you use the ADXL345 and the TFT Display then ADXL345_WE myAcc = ADXL345_WE(&SPI, CS_PIN, spi, MOSI_PIN, MISO_PIN, SCK_PIN) for creating the myAcc object will not work. "spi" (don't mix with "SPI") is also used by the TFT_eSPI library. Just replace it by "true" and delete bool spi = true or rename spi to use_spi or something else.
  2. If I use GPIO 2 as CS Pin I can't upload any sketch to the ESP32. This is because it's one of the strapping pins (https://www.esp32.com/viewtopic.php?t=5970#p25820). Strangely it works on your side and I don't say it's related with the issue, but could somehow.
  3. I needed to add a #define for TFT_BL to your sketch

@yukiloh
Copy link

yukiloh commented Jun 24, 2023

Sorry for the slow reply,I am enjoying a public holiday...(dragon-boat-festival)
1.Yes,I only changed CS pin (from GPIO5) to GPIO2,I've used up all the GPIO,maybe I should swap someone...

2.CS will inactive when it is HIGH,and downloading mode need it be DOWN,maybe this is why you can't upload sketch?
esp32_datasheet_en(in page 9)
image
I will try to avoid these pins next time...MTDI(RXD0), GPIO0, GPIO2, GPIO4, MTDO(TXD0), GPIO5

  1. It defined in TFT_eSPI setting file,I had to comment it to avoid redefine error,sorry about it.
    #define TFT_BL 15 // LED back-light

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants