【问题标题】:SPI Data write/readSPI 数据写入/读取
【发布时间】:2016-03-24 17:50:56
【问题描述】:

我使用以下命令将数据从 SPI Core 传递到闪存或 SD 卡等。

XSpi_Transfer(&Spi, SendData, ResData, 1);

我知道为了在内存中保存\读取数据,我们需要提供一个地址。

但我不明白如何在上面的命令中指定地址。

【问题讨论】:

    标签: memory sd-card spi


    【解决方案1】:

    这个github page 显示了一个名为SpiAtmelFlashRead 的示例函数,语法如下:

    SPI_Status = SpiAtmelFlashRead(&spi, Address, ByteCount);
    

    read函数里面的代码是:

    /*****************************************************************************/
    /**
    *
    * This function reads data from the Atmel Flash device connected to the SPI
    * interface.
    *
    * @param    SpiPtr is a pointer to the SPI driver component to use.
    * @param    Addr is the address from which the data is to be read from
    *       the Flash.
    * @param    ByteCount is the number of bytes to read.
    *
    * @return   XST_SUCCESS if successful else XST_FAILURE.
    *
    * @note     None.
    *
    ******************************************************************************/
    int SpiAtmelFlashRead(XSpi *SpiPtr, u32 Address, u16 ByteCount)
    {
        u16 Index;
    
        /*
         * Setup the read command with the specified address and data for the
         * Atmel Flash.
         */
        WriteBuffer[ATMEL_COMMAND_OFFSET]   = ATMEL_COMMAND_READ;
        WriteBuffer[ATMEL_ADDRESS_BYTE1_OFFSET] = (u8) (Address >> 16);
        WriteBuffer[ATMEL_ADDRESS_BYTE2_OFFSET] = (u8) (Address >> 8);
        WriteBuffer[ATMEL_ADDRESS_BYTE3_OFFSET] = (u8) Address;
    
        /*
         * Prepare the write buffer. Fill in some dummy data.
         */
        for(Index = 4; Index < (ByteCount + ATMEL_READ_WRITE_EXTRA_BYTES);
                    Index++) {
            WriteBuffer[Index] = ATMEL_DUMMYBYTE;
        }
    
        /*
         * Prepare the Read Buffer. Fill in some initialization data into the
         * the buffer.
         */
        for(Index = 0; Index < (ByteCount +
                    ATMEL_READ_WRITE_EXTRA_BYTES); Index++) {
            ReadBuffer[Index] = ATMEL_INITBYTE;
        }
    
        /*
         * Send the read command to the Atmel Flash to read the specified number
         * of bytes.
         */
        TransferInProgress = TRUE;
        XSpi_Transfer(SpiPtr, WriteBuffer, ReadBuffer,
                    ByteCount + ATMEL_READ_WRITE_EXTRA_BYTES);
    
        /*
         * Wait till the Transfer is complete and check if there are any errors
         * in the transaction.
         */
        while (TransferInProgress);
        if(ErrorCount != 0) {
            ErrorCount = 0;
            return XST_FAILURE;
        }
    
        return XST_SUCCESS;
    }
    

    这个github page 显示了一个名为SpiAtmelFlashWrite 的示例函数,语法如下:

    SPI_Status = SpiAtmelFlashWrite(&spi, Address, chunk, ByteCount);
    

    write函数里面的代码是:

    /*****************************************************************************/
    /**
    *
    * This function writes to the Atmel Flash device connected to the SPI interface.
    *
    * @param    SpiPtr is a pointer to the SPI driver component to use.
    * @param    Address is the address to which the data is written.
    * @param    ByteCount contains the number of bytes to write.
    *
    * @return   XST_SUCCESS if successful else XST_FAILURE.
    *
    * @note     None.
    *
    ******************************************************************************/
    int SpiAtmelFlashWrite(XSpi *SpiPtr, u32 Address, u16 ByteCount)
    {
        u16 Index;
    
        /*
         * Setup the write command with the specified address, and data to be
         * written to the flash.
         */
        WriteBuffer[ATMEL_COMMAND_OFFSET]     = ATMEL_COMMAND_WRITE;
        WriteBuffer[ATMEL_ADDRESS_BYTE1_OFFSET] = (u8) (Address >> 16);
        WriteBuffer[ATMEL_ADDRESS_BYTE2_OFFSET] = (u8) (Address >> 8);
        WriteBuffer[ATMEL_ADDRESS_BYTE3_OFFSET] = (u8) (Address);
    
        /*
         * Prepare the write buffer. Fill in the data that is to be written into
         * the Flash.
         */
        for(Index = 4; Index < (ByteCount + ATMEL_READ_WRITE_EXTRA_BYTES);
                Index++) {
            WriteBuffer[Index] = (u8)(ATMEL_TEST_BYTE + Index);
        }
    
        /*
         * Send the write command, address, and data to the Flash.
         * No receive buffer is specified since there is nothing to receive.
         */
        TransferInProgress = TRUE;
        XSpi_Transfer(SpiPtr, WriteBuffer, NULL,
                ByteCount + ATMEL_READ_WRITE_EXTRA_BYTES)
                                ;
    
        /*
         * Wait till the Transfer is complete and check if there are any errors
         * in the transaction.
         */
        while (TransferInProgress);
        if(ErrorCount != 0) {
            ErrorCount = 0;
            return XST_FAILURE;
        }
    
        return XST_SUCCESS;
    }
    

    【讨论】:

    • 如 Atmel Flash 示例中所述:XSpi_Transfer(SpiPtr, WriteBuffer, NULL, ByteCount + ATMEL_READ_WRITE_EXTRA_BYTES);我不明白的是(在上面的 XSpi_transfer 函数示例中),如果 WriteBuffer 包含地址(很明显),那么数据存储在哪个变量中。
    【解决方案2】:

    与您的外部存储设备通信的实际协议将在其数据表中指定(即,您向芯片发送地址和命令的方式会因制造商和产品线而异)。看起来您的函数只是启动单个 SPI 传输。您很可能必须多次调用该函数来发送特定的字节/字序列,这些字节/字共同构成写命令、地址和数据。同样,这一切都取决于数据表。查看XSpi_Transfer 中到底发生了什么也很有帮助。

    还记得确保在您的 SPI 配置例程中正确设置 SPI 极性、相位和数据位。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      • 2011-10-18
      • 2022-07-27
      • 2021-05-28
      • 2013-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多