Esp32s3 qtpy platformio SPI problem

Hi guys,

I’m having a lot of trouble programming the qtpy esp32s3 using the platformio. More precisely I cannot get the SPI to work.

Here is my current ini file:

[env:adafruit_qtpy_esp32s3_nopsram]
board = adafruit_qtpy_esp32s3_nopsram
board_build.mcu = esp32s3
board_build.f_cpu = 240000000L
framework = arduino
platform = espressif32
upload_protocol = esptool
platform_packages =
    framework-arduinoespressif32@3.2.0
lib_archive = false

I’m trying to upload the sensor test example

#include <Arduino.h>
#include <SimpleFOC.h>
#include <SPI.h>

// alternative pinout
#define SPI_MISO MISO
#define SPI_MOSI MOSI
#define SPI_SCLK SCK
#define SPI_SS RX

// MagneticSensorSPI(int cs, float _cpr, int _angle_register)
// config           - SPI config
//  cs              - SPI chip select pin 
MagneticSensorSPI sensor = MagneticSensorSPI(AS5048_SPI, SPI_SS);

void setup() {

  _delay(5000); // wait for the serial to be ready

  // monitoring port
  Serial.begin(115200);

  // initialise magnetic sensor hardware
  sensor.init();

  Serial.println("Sensor ready");
  _delay(1000);
}

void loop() {
  // iterative function updating the sensor internal variables
  // it is usually called in motor.loopFOC()
  // this function reads the sensor hardware and 
  // has to be called before getAngle nad getVelocity
  sensor.update();
  // display the angle and the angular velocity to the terminal
  Serial.print(sensor.getAngle());
  Serial.print("\t");
  Serial.println(sensor.getVelocity());
  _delay(100); // Add a small delay to avoid flooding the serial output
}

I can upload the code and do get an output but its always zero.
I’ve checked with the scope and there is no SCK signals sent, I can see the MOSI and the CS being sent but no SCK. I am not really sure what does that mean. I imagine it’s some kind of misconfiguration.

The same code works with the Arduino IDE. I am not sure what am I missing.

If some of you have experienced something similar I would appreciate any pointers :smiley:

Huh, I just realised that if I use the constructor

MagneticSensorSPI sensor = MagneticSensorSPI(SPI_SS, 14, 0x3FFF);

instead of

MagneticSensorSPI sensor = MagneticSensorSPI(AS5048_SPI, SPI_SS);

everything works.

We need to investigate this. It might be an issue with difference compilers and these lines

/** Typical configuration for the 14bit AMS AS5147 magnetic sensor over SPI interface */
MagneticSensorSPIConfig_s AS5147_SPI = {
  .spi_mode = SPI_MODE1,
  .clock_speed = 1000000,
  .bit_resolution = 14,
  .angle_register = 0x3FFF,
  .data_start_bit = 13,
  .command_rw_bit = 14,
  .command_parity_bit = 15
};
// AS5048 and AS5047 are the same as AS5147
MagneticSensorSPIConfig_s AS5048_SPI = AS5147_SPI;
MagneticSensorSPIConfig_s AS5047_SPI = AS5147_SPI;

Maybe associating the structure with the new variable does not go well in all cases.

Hey,
i also recently tried the MagneticSensorSPI (not for AS5048 but for 47) from the main repo and could not get it to work easily. I then just swapped to the non-generic version from the drivers repo without investigating:

MagneticSensorAS5047 encoder_absolute(18);

there could be something wrong with the confi :thinking:

I’ve observed this problem before… IIRC the problem was that the struct was not initialized when it was used.
You can try with your own (re-)definition of MagneticSensorSPIConfig_s. If it works with your own struct that you defined in main.cpp but not the standard one then you’re seeing the same error I did.