SPI reboot problem

Good morning, I am already feeling bad for asking my 2nd question on this topic, but I don't know much about the details of the SPI.

Thanks to GolamMostafa, I was able to create an SPI connection between an Arduino Nano (Slave) and an Esp8266 (Master) so that the Master can RECEIVE data from the Slave, this works perfectly fine.

But there is a problem, and that is that this project is portable, the Arduino Nano (Slave) must always be on (but when not in use it would be in Deep Sleep so as not to waste energy). And when I order you to wake up with a button, it will wake up and turn on the Esp8266 (but I will not turn it on via SPI, but by another pin, that is already resolved).

=============
Arduino Nano Code

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

char a;
const byte ROWS = 8;
const byte COLS = 5;
char keys[ROWS][COLS] = {
  {'A','B','C','D','E'},
  {'F','G','H','I','J'},
  {'K','L','M','N','O'},
  {'P','Q','R','S','T'},
  {'U','V','W','X','Y'},
  {'Z','a','b','c','d'},
  {'e','f','g','h','i'},
  {'j','k','l','m','n'},
};
byte rowPins[ROWS] = {3, 4, A5, A4, A3, A2, A1, A0};
byte colPins[COLS] = {5, 6, 7, 8, 9};

Adafruit_Keypad customKeypad = Adafruit_Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);


void setup() {
  //Serial.begin(115200);
  customKeypad.begin();
  
  /*SPI.setClockDivider(SPI_CLOCK_DIV16);
  pinMode(SS, INPUT_PULLUP);
  pinMode(MISO, OUTPUT);
  SPCR |= _BV(SPE);
  SPCR |= !(_BV(MSTR));
  SPI.attachInterrupt();*/
}

void loop() {
  customKeypad.tick();

  while(customKeypad.available()){
    keypadEvent e = customKeypad.read();
    if(e.bit.EVENT == KEY_JUST_PRESSED) {
      a = (char)e.bit.KEY;
    }
    else if(e.bit.EVENT == KEY_JUST_RELEASED) {
      a = 1;
    }
    if ((char)e.bit.KEY == 'A') {
      SPI.begin();
      SPI.setClockDivider(SPI_CLOCK_DIV16);
      pinMode(SS, INPUT_PULLUP);
      pinMode(MISO, OUTPUT);
      SPCR |= _BV(SPE);
      SPCR |= !(_BV(MSTR));
      SPI.attachInterrupt();
    } else if ((char)e.bit.KEY == 'B') {
      SPI.end();
    }
  }
  delay(10);
}

ISR(SPI_STC_vect)
{
  SPDR = a;
}

==========
Esp8266 Code

#include<SPI.h>

byte myData;
byte buff = 0;

void setup() {
  Serial.begin(115200);
  SPI.begin();
  delay(100);
  SPI.setClockDivider(SPI_CLOCK_DIV16);
  pinMode(SS, OUTPUT);
  digitalWrite(SS, LOW);
}

void loop() {
  myData = SPI.transfer(myData);
  if (myData != 0 && myData != 1) {
    if (buff != myData) {
      buff = myData;
      Serial.println(buff);
    }
  } else {
    buff = myData;
  }
  delayMicroseconds(1000);
}

=====
Circuit

Arduino Nano - Esp8266
10 <------> D8
11 <------> D7
12 <------> D6
13 <------> D5

I am going to describe exactly the things that happen so that there are no problems... To simulate things, first, nothing is connected (Arduino and Esp are without power), first I connect the Arduino Nano, then I connect the Esp8266 (no matter how much time elapses since I connect the Arduino and the Esp8266), then I press the "A" button (the Arduino is connected to a Matrix keyboard), this button causes the SPI to be configured (cut what was in "void setup () "to make him do that when he pressed this button). By doing all this it works perfectly fine, the Esp8266 receives the data of all the buttons read by the Arduino, but when I disconnect the Esp8266 (remember that the Arduino will always stay on, this simulates that the project is turned off and is not being used by the moment) and I reconnect it, SPI communication does not work, so I also tried to put a function by pressing button "B" to make it "SPI.end ()", but it does not work.

What I was trying to do was press the reset button on the Arduino Nano while the Esp8266 was off, I reconnected the Esp and it still didn't read anything, disconnected it, I pressed the reset button on the Nano again and this time if it worked. Simulate this over and over again, in the end I realized that since I connected it, this pattern continues, works - doesn't work - works - doesn't work - works. I don't know why it does that, I don't know much about this protocol, but I need to use SPI because I don't have extra pins left (this I say because the Esp8266 has to use SPI to read an SD and I run out of pins, so I can't use I2C).

I'm sure it has something to do with initializing the SPI, I have no idea how to solve it, it must be a technical detail that I can't see.

Thank you!!!

I'm not exactly sure what you are doing with these registers

  SPCR |= _BV(SPE);
  SPCR |= !(_BV(MSTR));

but that is NOT how you clear a bit (the second line). It is OR'ing the original value with a 0 which does nothing. To clear a bit, you need to AND the inverse

  SPCR |= _BV(SPE);
  SPCR &= ~(_BV(MSTR));

Change what you said, but it is exactly the same, it neither worsens nor improves it, it is the same.

Any idea?

Repost the entire revised sketch.

the code i am using is in the first post but it doesn't work when i disconnected and reconnected it only works when i reboot twice

I looked at some examples from the SPI library and they don't use port manipulation. Since you haven't commented your SPI port manipulation, can you please explain why you are using it, and what it is supposed to do?

It is because it is a portable project, I need to turn off as many devices to save battery power when turning the project off (but the Arduino will stay on in power saving mode to turn the entire project back on). For that I need to turn off the Esp8266, so the SPI communication closes, the problem is when I turn it on again, they will not re-communicate unless I restart it, but if I fly it to do it does not work, but it works in the following As I said, works, doesn't work, works, doesn't work.

hiperdoo:
It is because it is a portable project, I need to turn off as many devices to save battery power when turning the project off (but the Arduino will stay on in power saving mode to turn the entire project back on). For that I need to turn off the Esp8266, so the SPI communication closes, the problem is when I turn it on again, they will not re-communicate unless I restart it, but if I fly it to do it does not work, but it works in the following As I said, works, doesn't work, works, doesn't work.

That makes no sense. For example, I have myself connected an Arduino GPIO to the ESP8266 wake pin to do the same thing. But for that, a simple digitalWrite() to the wake pin was perfectly okay. I called what you did "SPI port manipulation", that is not correct, I was not thinking clearly, it is actually SPI register manipulation. Those registers don't control GPIO, so can not have anything to do with the ESP8266. Hence my statement that it makes no sense. If I am missing something, which does happen from time to time, please enlighten me.

Why are you tinkering with the SPI registers? If you think it somehow helps to re-establish communication with the ESP, then you should explain how it is supposed to work.