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);
}
}