I wrote a code to interface ADXL355 to ESP32 using SPI. Previously, I set up the sensor using ARM STM32 H7 and it worked properly. now it seems that the SPI doesn't work because I can not read the values are sent by Sensor. Any Idea where is the problem?
Thanks for your response in advance.
Here is the code I wrote:
#include <SPI.h>
#include <Arduino.h>
#define HSPI_MISO 12
#define HSPI_MOSI 13
#define HSPI_SCLK 14
#define HSPI_SS 15
//function prototypes;
uint8_t hspiSend_ReceiveByte(byte stuff);
//uninitalised pointers to SPI objects
SPIClass * hspi = NULL;
static const int spiClk = 1000000; // 1 MHz
//Address for the Range Register
const uint8_t RANGE = 0x2C; //Considered Reset value for this Register is 0x81.
const uint8_t RANGE_2G = 0x01; //value to set the 2G_range of the sensor
const uint8_t POWER_CTL = 0x2D; //Power control Register's Address.
const uint8_t MEASURE_MODE = 0x06; // Only accelerometer
const uint8_t ADXL_Reset_Reg_Address= 0x2F; //this Reg is only Writable. put 0x00 to this Reg onorder to reset it.
//Senors's reset register address
uint8_t READ_BYTE = 0x01;
uint8_t WRITE_BYTE = 0x00;
uint8_t reg_adress = 0x00;
//address of the X axis
const uint8_t XDATA3 = 0x08;
const uint8_t XDATA2 = 0x09;
const uint8_t XDATA1 = 0x0A;
//X axis variables
int8_t xdata[3]={0,0,0};
int32_t Xdata_i=0;
float Xdata_d=0;
char uart_buff[50];
void setup() {
//initialise two instances of the SPIClass attached to VSPI and HSPI respectively
hspi = new SPIClass(HSPI);
//#ifndef ALTERNATE_PINS
//initialise hspi with default pins
//SCLK = 14, MISO = 12, MOSI = 13, SS = 15
//hspi->begin();
//#else
//alternatively route through GPIO pins
hspi->begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_SS); //SCLK, MISO, MOSI, SS
//#endif
//set up slave select pins as outputs as the Arduino API
pinMode(HSPI_SS, OUTPUT); //HSPI SS
//resetting the ADXL
reg_adress = (ADXL_Reset_Reg_Address << 1) | WRITE_BYTE; //reset reg address : 0x2F, value : 0x52
hspiSend_ReceiveByte(reg_adress);
hspiSend_ReceiveByte(0x52);
//Setting the ADXL Range to 2G
reg_adress = (RANGE << 1) | WRITE_BYTE;
hspiSend_ReceiveByte(reg_adress);
hspiSend_ReceiveByte(RANGE_2G);
//Setting the ADXL to the measurment mode.
reg_adress = (POWER_CTL << 1) | WRITE_BYTE;
hspiSend_ReceiveByte(reg_adress);
hspiSend_ReceiveByte(MEASURE_MODE);
Serial.begin(921600);
}
// the loop function runs over and over again until power down or reset
void loop() {
//use the SPI buses
//vspiCommand();
reg_adress = (XDATA1 << 1) | READ_BYTE;
xdata[0] = hspiSend_ReceiveByte(reg_adress);
reg_adress = (XDATA2 << 1) | READ_BYTE;
xdata[1] = hspiSend_ReceiveByte(reg_adress);
reg_adress = (XDATA3 << 1) | READ_BYTE;
xdata[2] = hspiSend_ReceiveByte(reg_adress);
Xdata_i = ((int32_t)xdata[0] >> 4) + ((int32_t)xdata[1] << 4) + ((int32_t)xdata[2] << 12);
Xdata_d = (float)Xdata_i/256000;
sprintf(uart_buff , "Sensor data: %f", Xdata_d);
Serial.println(uart_buff);
//sprintf(uart_buff , "Sensor data: %d", xdata[2]);
//Serial.println(uart_buff);
delay(2000);
}
uint8_t hspiSend_ReceiveByte(byte stuff) {
uint8_t retnd_result;
hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
digitalWrite(HSPI_SS, LOW);
retnd_result = hspi->transfer(stuff);
digitalWrite(HSPI_SS, HIGH);
hspi->endTransaction();
return retnd_result;
}