请教:8266做SPI从机,可以正常接收数据,但无法发送数据

xliu
Posts: 2
Joined: Thu Aug 30, 2018 3:19 pm

请教:8266做SPI从机,可以正常接收数据,但无法发送数据

Postby xliu » Thu Aug 30, 2018 3:35 pm

最近在使用esp8266的HSPI做通信实验时,遇到一些问题:
SPI主可以正常发送数据,SPI从可以正常接收数据。但SPI主读回来的数据,并不是SPI从发送出来的数据,甚至有时候主端读回的数据一直为0xff。
实验环境为:
开发板为两块ESP-LAUNCHER
SDK使用 FREERTOS_2.0.0
参考代码为:https://github.com/espressif/esp8266-rtos-sample-code/blob/master/02Peripheral/HSPI_Slave/user/user_main.c
SPI slave 运行的代码为:

Code: Select all

LOCAL  void spi_slave_task(void *prg)
{
   //
       SpiAttr hSpiAttr;
       hSpiAttr.bitOrder = SpiBitOrder_MSBFirst;
       hSpiAttr.speed = 0;
       hSpiAttr.mode = SpiMode_Slave;
       hSpiAttr.subMode = SpiSubMode_0;

       // Init HSPI GPIO
       WRITE_PERI_REG(PERIPHS_IO_MUX, 0x105);
       PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 2);//configure io to spi mode
       PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 2);//configure io to spi mode
       PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 2);//configure io to spi mode
       PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 2);//configure io to spi mode

       printf("\r\n ============= spi init slave =============\r\n");
       SPIInit(SpiNum_HSPI, &hSpiAttr);
       //CLEAR_PERI_REG_MASK(SPI_SLAVE(SpiNum_HSPI), 0x3FF);

       SPISlaveRecvData(SpiNum_HSPI,spi_slave_isr_sta);
       uint32 sndData[8] = { 0 };
      sndData[0] = 0x35343332;
      sndData[1] = 0x39383736;
      sndData[2] = 0x3d3c3b3a;
      sndData[3] = 0x11103f3e;
      sndData[4] = 0x15141312;
      sndData[5] = 0x19181716;
      sndData[6] = 0x1d1c1b1a;
      sndData[7] = 0x21201f1e;

      SPISlaveSendData(SpiNum_HSPI, sndData, 8);
      WRITE_PERI_REG(SPI_RD_STATUS(SpiNum_HSPI), 0x8A);
      WRITE_PERI_REG(SPI_WR_STATUS(SpiNum_HSPI), 0x83);

       while(1)
       {

          vTaskDelay(1000/portTICK_RATE_MS);
          //printf("\r\n spi slave still alive!!!");
       }

}


SPI master 运行的代码为:

Code: Select all

void spi_master_test()
{
    SpiAttr hSpiAttr;
    hSpiAttr.bitOrder = SpiBitOrder_MSBFirst;
    hSpiAttr.speed = SpiSpeed_2MHz;
    hSpiAttr.mode = SpiMode_Master;
    hSpiAttr.subMode = SpiSubMode_0;

    // Init HSPI GPIO
    WRITE_PERI_REG(PERIPHS_IO_MUX, 0x105);
    PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 2);//configure io to spi mode
    PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 2);//configure io to spi mode
    PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 2);//configure io to spi mode
    PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 2);//configure io to spi mode

    SPIInit(SpiNum_HSPI, &hSpiAttr);
    uint32 value = 0x00;
    uint32 sendData[8] ={ 0 };
    SpiData spiData;

    printf("\r\n =============   spi init master   ============= \r\n");

//  Test 8266 slave.Communication format: 1byte command + 1bytes address + x bytes Data.

    while(1)
    {

       printf("\r\n Master send 32 bytes data to slave(8266)\r\n");
      memset(sendData, 0, sizeof(sendData));
      sendData[0] = 0x55565758;
      sendData[1] = 0x595a5b5c;
      sendData[2] = 0x5d5e5f60;
      sendData[3] = 0x61626364;
      sendData[4] = 0x65666768;
      sendData[5] = 0x696a6b6c;
      sendData[6] = 0x6d6e6f70;
      sendData[7] = 0x71727374;
      spiData.cmd = MASTER_WRITE_DATA_TO_SLAVE_CMD;
      spiData.cmdLen = 1;
      spiData.addr = &value;
      spiData.addrLen = 1;
      spiData.data = sendData;
      spiData.dataLen = 32;
       SPIMasterSendData(SpiNum_HSPI, &spiData);

      printf("\r\n Master receive 24 bytes data from slave(8266)\r\n");
      spiData.cmd = MASTER_READ_DATA_FROM_SLAVE_CMD;
      spiData.cmdLen = 1;
      spiData.addr = &value;
      spiData.addrLen = 1;
      spiData.data = sendData;
      spiData.dataLen = 24;
      memset(sendData, 0, sizeof(sendData));
      SPIMasterRecvData(SpiNum_HSPI, &spiData);
      printf(" Recv Slave data0[0x%08x]\r\n", sendData[0]);
      printf(" Recv Slave data1[0x%08x]\r\n", sendData[1]);
      printf(" Recv Slave data2[0x%08x]\r\n", sendData[2]);
      printf(" Recv Slave data3[0x%08x]\r\n", sendData[3]);
      printf(" Recv Slave data4[0x%08x]\r\n", sendData[4]);
      printf(" Recv Slave data5[0x%08x]\r\n", sendData[5]);

      /*
      value = SPIMasterRecvStatus(SpiNum_HSPI);
      printf("\r\n Master read slave(8266) status[0x%02x]\r\n", value);

      SPIMasterSendStatus(SpiNum_HSPI, 0x99);
      printf("\r\n Master write status[0x99] to slavue(8266).\r\n");
      */
      vTaskDelay(1000/portTICK_RATE_MS);
    }


}


如果一切正常,spi master 收到的数据应该为:

Code: Select all

sndData[0] = 0x35343332;
sndData[1] = 0x39383736;
sndData[2] = 0x3d3c3b3a;
sndData[3] = 0x11103f3e;
sndData[4] = 0x15141312;
sndData[5] = 0x19181716;
sndData[6] = 0x1d1c1b1a;
sndData[7] = 0x21201f1e;

但实际收到的为:

Code: Select all

 
 Master receive 24 bytes data from slave(8266)
 Recv Slave data0[0x6a686664]
 Recv Slave data1[0x72706e6c]
 Recv Slave data2[0x7a787674]
 Recv Slave data3[0xff3f7e7c]
 Recv Slave data4[0xffffffff]
 Recv Slave data5[0xffffffff]
 


请知道的大神指点一下思路,谢谢。

Who is online

Users browsing this forum: No registered users and 9 guests