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

asked Mar 16, 2023 at 12:05
9
  • Do you have a logical analyzer to check the lines? Commented Mar 16, 2023 at 12:50
  • @Kacper, Yes, I checked it before and it is okey. Commented Mar 16, 2023 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? Commented Mar 16, 2023 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. Commented Mar 16, 2023 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? Commented Mar 16, 2023 at 13:27

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.