Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ATtiny3216 SPI master to ESP8266 SPI slave #1079

Open
Hai-Elec opened this issue Apr 4, 2024 · 0 comments
Open

ATtiny3216 SPI master to ESP8266 SPI slave #1079

Hai-Elec opened this issue Apr 4, 2024 · 0 comments

Comments

@Hai-Elec
Copy link

Hai-Elec commented Apr 4, 2024

Hi,
I can send data from ATtiny3216 (SPI master) to ESP8266 on ESP12F module (SPI slave).
But I do not understand why some first characters of string is bypass.
With MPLABXIDE-my coding it by pass 2 first characters.
With my Arduino coding, it by pass 7 first characters.
With Arduino example SPI coding, at first try, it can show first character, but after that, I do not understand why it by pass. Now it never give me the first character anymore.
I test with setting clock SPI DIV128, I try change internal clock ATtiny3216 from 20MHz to 1MHz, result is the same. I try to send "1234567abcdefghijklmnopqrstuvwyz12345!" It alway by pass first character, the serial print output of ESP8266 is shown below:

Question: abcdefghijklmnopqrstuvwyz12345!
Status: 892613426
Answer Sent
Question: abcdefghijklmnopqrstuvwyz12345!
Status: 892613426
Answer Sent

I connect directly 5V SPI pin to ESP-12F, because, if I connect SS pin of ESP8266 to level shifter IC TXS0108E, it can not boot, try set pin Low by ATtiny 3216 when start up, but it still not boot(I rechecked TXS0108E it still work OK when I control level of SS pin of ESP8266 3V3 by ATtiny3216 5V pin). Recheck information on Arduino forum, I directly connect SPI to ESP8266.
Now I can send data via SPI OK, but I must add some unnecessary first characters, so, I add here for more information.
Please share after check coding:
Arduino coding of ATtiny3216 - SPI master:


// inslude the SPI library:
#include <SPI.h>
  #define SSPIN  PIN_PA4
  #define MOSI  PIN_PA1
  #define MISO  PIN_PA2
  #define SCK  PIN_PA3
char txMsg[] = "1234567abcdefghijklmnopqrstuvwyz12345!";///it by pass first 7 character

void setup() {

  pinMode(SSPIN, OUTPUT);
  digitalWrite(SSPIN, LOW);
  pinMode(MOSI, OUTPUT);
  pinMode(MISO, INPUT);
  pinMode(SCK, OUTPUT);
  delay(1000);

  digitalWrite(SSPIN, HIGH);

  delay(3000);

  // initialize SPI:
 
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV128); 

}

void loop() {
 
  digitalWrite(SSPIN, LOW);

  for(long i=0; i< sizeof(txMsg); i++)
  {
    SPI.transfer(txMsg[i]); 
  }

  digitalWrite(SSPIN, HIGH);

  delay(1000);
 
}

For SPI slave Arduino coding of ESP12F, I use example code SPISlave_Test. Just re-up here for more convenient. Please check:

/*
    SPI Slave Demo Sketch
    Connect the SPI Master device to the following pins on the esp8266:

    GPIO    NodeMCU   Name  |   Uno
  ===================================
     15       D8       SS   |   D10
     13       D7      MOSI  |   D11
     12       D6      MISO  |   D12
     14       D5      SCK   |   D13

    Note: If the ESP is booting at a moment when the SPI Master has the Select line HIGH (deselected)
    the ESP8266 WILL FAIL to boot!
    See SPISlave_SafeMaster example for possible workaround

*/

#include "SPISlave.h"

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);

  // data has been received from the master. Beware that len is always 32
  // and the buffer is autofilled with zeroes if data is less than 32 bytes long
  // It's up to the user to implement protocol for handling data length
  SPISlave.onData([](uint8_t *data, size_t len) {
    String message = String((char *)data);
    (void)len;
    if (message.equals("Hello Slave!")) {
      SPISlave.setData("Hello Master!");
    } else if (message.equals("Are you alive?")) {
      char answer[33];
      sprintf(answer, "Alive for %lu seconds!", millis() / 1000);
      SPISlave.setData(answer);
    } else {
      SPISlave.setData("Say what?");
    }
    Serial.printf("Question: %s\n", (char *)data);
  });

  // The master has read out outgoing data buffer
  // that buffer can be set with SPISlave.setData
  SPISlave.onDataSent([]() {
    Serial.println("Answer Sent");
  });

  // status has been received from the master.
  // The status register is a special register that bot the slave and the master can write to and read from.
  // Can be used to exchange small data or status information
  SPISlave.onStatus([](uint32_t data) {
    Serial.printf("Status: %u\n", data);
    SPISlave.setStatus(millis());  // set next status
  });

  // The master has read the status register
  SPISlave.onStatusSent([]() {
    Serial.println("Status Sent");
  });

  // Setup SPI Slave registers and pins
  SPISlave.begin();

  // Set the status register (if the master reads it, it will read this value)
  SPISlave.setStatus(millis());

  // Sets the data registers. Limited to 32 bytes at a time.
  // SPISlave.setData(uint8_t * data, size_t len); is also available with the same limitation
  SPISlave.setData("Ask me a question!");
  Serial.println("on");
}

void loop() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant