0

Project description:

1.CY8CKIT-059 is master and ESP8266 mini d1 is slave.

2.Master communicate with slave via SPI per 2 seconds and each time the value increases 1.

Question:

I don't know why my slave device always receive Master : 0.The value doesn't increase in each time. But when I apply ESP8266 as master instead of CY8CKIT-059 which means both master and slave are ESP8266, the slave device will receive data and the value will increase in each time.

The result printed to terminal from slave device when master is CY8CKIT-059.

The result printed to terminal from slave device when master is ESP8266.

Logic analyzer for SPI when master is CY8CKIT-059

I tried to apply 4 SPI modes with MSB or LSB first and apply a level shifter between CY8CKIT-059 and ESP8266, the results of all combinations are not what I expected.

Master code:

#include "project.h"
#include <stdio.h>
#include <stdbool.h>
#include <string.h>


void spi_ss_for_esp8266() {
    SS_1_Write(1);
    CyDelayUs(5);
    SS_1_Write(0);
}

void spi_write(uint8_t *data, uint8_t len) {
    
    if(SPIM_1_ReadTxStatus() & (SPIM_1_STS_SPI_IDLE | SPIM_1_STS_SPI_DONE)){
        SS_1_Write(0);
        //spi_ss_for_esp8266();
        
        SPIM_1_WriteTxData(0x02);
        SPIM_1_WriteTxData(0x00);
        SPIM_1_PutArray(data, len);
        while(len++ < 32) {
            SPIM_1_WriteTxData(0x00);       
        }

        SS_1_Write(1);
        //spi_ss_for_esp8266();
    }
}

int main(void)
{
    uint data = 0;
    char str[32];
    
    CyGlobalIntEnable; /* Enable global interrupts. */

    /* Place your initialization/startup code here (e.g. MyInst_Start()) */
    SPIM_1_Start();
    UART_1_Start();
    CyDelay(1000);

    for(;;)
    {
        /* Place your application code here. */
        //while(SW_Read());
        snprintf(str, sizeof(str), "Master : %u", data++);
        spi_write((uint8_t *)str, strlen(str));
        LED_Write(!LED_Read());
        UART_1_PutString(str);
        CyDelay(2000);
    }
}

/* [] END OF FILE */

Slave code:

#include <Arduino.h>
#include <Wire.h>
#include <SPISlave.h>

#define led 2

char str[32];
uint8_t status = 0;
uint8_t buf[32] = {0};
bool flag = false;

void spi_rx(uint8_t * data, size_t len) {
  for(uint8_t i = 0; i < len ; i++) {
    buf[i] = data[i];
  }
  flag = true;
  digitalWrite(led, status);
  status = !status;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(74880);
  pinMode(led, OUTPUT);
  pinMode(16, INPUT);
  SPISlave.begin();
  SPISlave.onData(&spi_rx);
}

void loop() {
  // put your main code here, to run repeatedly:

  if(flag) {
    flag = false;
    snprintf(str, sizeof(str), "%s", (char *)buf);
    Serial.println(str);
  }
}

Visit this website for more detail

甚麼甚
  • 3
  • 6
  • Do you have a logical analyzer to check the lines? – Kacper Mar 16 '23 at 12:50
  • @Kacper, Yes, I checked it before and it is okey. – 甚麼甚 Mar 16 '23 at 12:59
  • Also, does it mean that "Master : " part is transmitted correctly, but not the number? What if you check the UART output on CY8CKIT-059. Does it increase the numbers? – Kacper Mar 16 '23 at 13:12
  • @Kacper, The string will be shown "Master : 0", "Master : 1", "Master : 2" and so on with UART output from CY8CKIT-059 and logical analyzer as well. – 甚麼甚 Mar 16 '23 at 13:22
  • What if you modify the code so that the first SPI transmission is "Master : 2"? So set the `uint data = 2;` before anything happens? – Kacper Mar 16 '23 at 13:27
  • @Kacper, If I set data=2, then the result printed to terminal from slave device is always "Master : 2". – 甚麼甚 Mar 16 '23 at 13:40
  • Then this is most probably an issue with the handling of buffers somewhere, possibly data arriving partially (as in, not all of the message was received; try to just transmit the number, "%u", and that probably will transmit up until 99?) before it is printed, or the data not being fed into the buffers correctly at all. The SPI transmission parameters are most probably correct, though the SS getting high in the middle of the transmission does not look good. – Kacper Mar 16 '23 at 13:49
  • You can also try to dump the whole buf with a for loop and %x in the printf and see how it changes. – Kacper Mar 16 '23 at 13:52
  • @Kacper, I tried to print number only and it works, but sometimes the value will be duplicated. And I will check why SS will be set high in the middle of transmission. – 甚麼甚 Mar 16 '23 at 14:13

0 Answers0