Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

PSoC™ 4 Forum Discussions

kain_4812361
Level 1
Level 1
5 questions asked 5 sign-ins First reply posted

Hello. I am doing a document and try work on how to connect with

SPI Flash with CY8CKIT-042, but I have a question because it did not work well...

I am trying to read the SPI ID..

Please tell us if there is anything wrong with the source code!

Thank you.

SPI Info.

SPI Target : AT45DB041D

Expected result:

pastedImage_0.png

SPI Comonent

pastedImage_3.png

Source Code

#define BUF1_WRITE 0x84

#define BUF2_WRITE 0x87

#define BUF1_READ 0xD4

#define BUF2_READ 0xD6

#define BBUF1_TO_MM_PAGE_PROG_WITH_ERASE 0x83

#define BBUF2_TO_MM_PAGE_PROG_WITH_ERASE 0x86

#define MM_PAGE_TO_B1_XFER 0x53

#define MM_PAGE_TO_B2_XFER 0x55

#define PAGE_ERASE 0x81   // 512/528 bytes per page

#define SECTOR_ERASE 0x7C // 128k bytes per sector

#define READ_STATE_REGISTER 0xD7

#define READ_ID 0x9F

void spi_id_read(void)

{

    uint8 tmpBuffer[4]={READ_ID,0xff,0xff,0xff};

    uint8 status;

    uint32 i;

    /* Start transfer */

    SPI_SpiUartPutArray(tmpBuffer, 1);

    /* Wait for the end of the transfer. The number of transmitted data

    * elements has to be equal to the number of received data elements.

    */

    while (01u != SPI_SpiUartGetRxBufferSize());

    /* Clear dummy bytes from TX buffer */

    SPI_SpiUartClearTxBuffer();

    /* Read data from the RX buffer */

    i = 0u;

    while (0u != SPI_SpiUartGetRxBufferSize())

    {

        tmpBuffer = SPI_SpiUartReadRxData();

        i++;

        printf("\r\n tmpBuffer[%lx]=[%x]",i,tmpBuffer);

    }

}

int main(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

    /* Place your initialization/startup code here (e.g. MyInst_Start()) */

    UART_Start();

    SPI_Start();

  

    printf("\r\n FW[%s]",__DATE__);

    for(;;)

    {

        /* Place your application code here. */

        spi_id_read();

    }

}

0 Likes
1 Solution
AS_36
Moderator
Moderator
Moderator
500 replies posted 250 solutions authored 250 replies posted

Hi,

I went through the datasheet of AT45DB041D and the waveform for Read ID looks like this -

pastedImage_0.png

Please note that the CS# line is pulled LOW through out the SPI transaction, means the CS# line should be LOW starting from before sending command till the time all the bytes of the device ID have been transmitted on SO. However, when you use 'SPI_SpiUartPutArray(tmpBuffer, 1);' to send the opcode, the master will immediately pull the CS# line up after sending the opcode.

Another important point is, you need to send a dummy byte of data (to provide the required clock cycles) to the slave so that it can provide the output data on the SO line.

There are two ways to implement this SPI operation -

  • If your flash device does not have any additional timing requirements between sending the opcode from master side and then receiving the data from the slave side then, you can increase the tmpBuffer size to 5 and use 'SPI_SpiUartPutArray(tmpBuffer, 5);' and then read the rx data.
  • Other option is to disable the slave select line of the SPI block, and use the same pin as a GPIO and manually toggle it to LOW and HIGH through out the application as per your requirement. We have a sample implementation for this option and I am going to paste the code and other relevant information below (It is just an example and not specific for Read ID command). Go through it and let me know if you have any questions.

Implementation :

pastedImage_3.png

pastedImage_4.png

pastedImage_5.png

Code :

SS_Write(0);

SPI_SpiUartClearTxBuffer();

SPI_SpiUartClearRxBuffer();

SPI_SpiUartWriteTxData(0x03);//opcode

while(!SPI_SpiUartGetRxBufferSize());

//storing data

for(i=0;i<100;i++)

{

        SPI_SpiUartClearRxBuffer();

        SPI_SpiUartWriteTxData(0x00);//dummy

        while(!SPI_SpiUartGetRxBufferSize());

        data = SPI_SpiUartReadRxData();

}

SS_Write(1);

- Apurva

View solution in original post

2 Replies
AS_36
Moderator
Moderator
Moderator
500 replies posted 250 solutions authored 250 replies posted

Hi,

I went through the datasheet of AT45DB041D and the waveform for Read ID looks like this -

pastedImage_0.png

Please note that the CS# line is pulled LOW through out the SPI transaction, means the CS# line should be LOW starting from before sending command till the time all the bytes of the device ID have been transmitted on SO. However, when you use 'SPI_SpiUartPutArray(tmpBuffer, 1);' to send the opcode, the master will immediately pull the CS# line up after sending the opcode.

Another important point is, you need to send a dummy byte of data (to provide the required clock cycles) to the slave so that it can provide the output data on the SO line.

There are two ways to implement this SPI operation -

  • If your flash device does not have any additional timing requirements between sending the opcode from master side and then receiving the data from the slave side then, you can increase the tmpBuffer size to 5 and use 'SPI_SpiUartPutArray(tmpBuffer, 5);' and then read the rx data.
  • Other option is to disable the slave select line of the SPI block, and use the same pin as a GPIO and manually toggle it to LOW and HIGH through out the application as per your requirement. We have a sample implementation for this option and I am going to paste the code and other relevant information below (It is just an example and not specific for Read ID command). Go through it and let me know if you have any questions.

Implementation :

pastedImage_3.png

pastedImage_4.png

pastedImage_5.png

Code :

SS_Write(0);

SPI_SpiUartClearTxBuffer();

SPI_SpiUartClearRxBuffer();

SPI_SpiUartWriteTxData(0x03);//opcode

while(!SPI_SpiUartGetRxBufferSize());

//storing data

for(i=0;i<100;i++)

{

        SPI_SpiUartClearRxBuffer();

        SPI_SpiUartWriteTxData(0x00);//dummy

        while(!SPI_SpiUartGetRxBufferSize());

        data = SPI_SpiUartReadRxData();

}

SS_Write(1);

- Apurva

Thank you!!

I was able to solve this problem! 

0 Likes