lshachar / Arduino_Fanatec_Wheel

A do-it-yourself steering wheel to Fanatec's wheel base
86 stars 13 forks source link

ESP32 Inconsistent Connection #33

Open hickmanz opened 1 month ago

hickmanz commented 1 month ago

Im trying to get this code to work on an ESP32 and have frustrating results. Occasionally it connects and works exactly as intended. But most of the time, it seems to crash the wheel base (CSL DD) and I need to power cycle the wheelbase.

I also have experienced it working fine, temporarily the computer losing site of the wheel base and then it returning again.

Below is the code I am using to get basic functionality going. I have stripped out almost everything except the emulation of the wheel while I try and solve this problem:

#include <SPI.h>

#define CS_PIN 5
#define DATA_LENGTH 33

uint8_t returnData[DATA_LENGTH] = {
  0xA5, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x83, 0x87, 0x47, 0x7B, 0x9A,
  0x79, 0xA0, 0xC9, 0xB5, 0xD8, 0x1D, 0x72, 0x20, 0x2C, 0xFA, 0x62, 0xFF, 0x93, 0x04,
  0xCB, 0x98, 0xD0, 0x12, 0xAF
};
volatile uint32_t sequence = 0;

void cableSelect() {
  if (digitalRead(CS_PIN) == LOW) {  // Check if CS is low
      SPI.transfer(returnData, DATA_LENGTH);  // Send data back to master
  }
}
void setup() {
  pinMode(CS_PIN, INPUT_PULLUP);
  SPI.begin();  // Initialize SPI port
  SPI.setDataMode(SPI_MODE1);  // Set the SPI mode to mode 0
  SPI.setBitOrder(MSBFIRST);  // Set MSB first for data order
  attachInterrupt(digitalPinToInterrupt(CS_PIN), cableSelect, FALLING);  // Attach interrupt for CS pin
}

void loop() {
  // Since the SPI transactions are handled in the ISR, the loop can remain empty
}

I am running out of things to try next and the inconsistency of working vs not working is making it hard to troubleshoot.