Esp32-wroom 32 similar code from arduino given code

Hi all.

Im trying to emulate a thrustmaster wheel using an ESP32.

I found this: https://rr-m.org/blog/thrustmaster-t300rs-steering-wheel-arduino-emulator/

With this code:

byte wheelState [8]; // local push-buttons state saved here
volatile byte pos;

void setup (void) {
  DDRB  &= B11110000; // digital pins 8,9,11 used as inputs. 10 - SS also input
  PORTB |= B00001011; // 8,9,11 - pull-up to +VCC via internal 100k resistors.

  DDRC  &= B11000000; // pins 14-19 (A0 - A5) also used as digital inputs
  PORTC |= B00111111; // pulled-up to +VCC via internal 100k resistors

  DDRD  &= B00100000; // digital pins 0,1,3,4,6,7 are inputs for buttons. 2 - also input - for external interrupt 0
  PORTD |= B11011011; // 0,1,3,4,6,7 pulled-up to +VCC via internal 100k resistors
  
  wheelState[0] = B11010001; // T300 PS wheel first data byte
  wheelState[1] = B11111111; // second data byte - buttons
  wheelState[2] = B11111111; // third data byte - buttons
  wheelState[3] = B11111111; // this and below - not used, but wheelbase reads all 8 bytes...
  wheelState[4] = B11111111;
  wheelState[5] = B11111111;
  wheelState[6] = B11111111;
  wheelState[7] = B11111111;
  
  //Serial.begin(9600);    // Arduino debug console - occupies pins RX (0) and TX (1) on Uno
  pinMode(MISO, OUTPUT); // arduino is a slave device
  SPCR |= _BV(SPE);      // Enables the SPI when 1
  SPCR |= _BV(SPIE);     // Enables the SPI interrupt when 1
  
  // interrupt for SS rising edge. Arduino Uno Pin10 must be connected to Pin2!!!
  attachInterrupt (0, ss_rising, RISING);
  
}

// Interrupt0 (external, pin 2) - prepare to start the transfer
void ss_rising () {
  SPDR = wheelState[0]; // load first byte into SPI data register
  pos = 1;
}

// SPI interrupt routine
ISR (SPI_STC_vect) {
  SPDR = wheelState[pos++]; // load the next byte to SPI output register and return.
} 

(I just copied arduino code, avoiding the large first comment, just for efficiency)

The first (and actually the one) question is, its possible to translate or rewrite that code to work with ESP32? (I dont want anyone to write it for me, maybe just hints, but i want to learn and achieve it, or unless, try it)

I have read in some pages that ESP32 cant act as SPI slave (only master) so, it is true? or there is anyway to do it?

In case that it could be possible, could you hint me where to search info on how to understand SPI on esp32? (I repeat myself, i wnat to learn, ask for coding its not my way)

Thanks!

That code is specific to an AVR processor like on an Uno or Mega board. It will need to be completely rewritten for ESP32.

Here's the reference for ESP32 SPI Slave:
https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/spi_slave.html

1 Like

Yes, I was expecting something like that... But the first point is to know it with the knowledge, it could be possible, or if ESP32 cant to it, its worthless to dig in.

if you follow (approximately) these steps:

    • HAL (Hardware Abstraction Level): replace direct hardware register access by Arduino functions.
    • Learn to use bus protocols and port expanders.
    • Update the code for use on the target machine

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.