This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to read&Write External Flash with NRF52832 SPI0 ?

Hi,

My problems are as follows:

SDK :nRF5_SDK_15.0.0_a53641a.

IDE:MDK 5.2.6

Chip:nrf52832  SPI0

External Flash :W25Q64

Now ,I can read the chip ID  correctly ,but when I transfer 256 bytes to External Flash ,as if only can write 251 bytes successfully, 

  • Is there something wrong with my code oprtion?
    Thank  you!

  • ...
  • void SPI_FLASH_PageWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite)

    {

        uint16_t  len = NumByteToWrite;

        uint16_t len_cut = 0;  

        uint32_t  addr = WriteAddr;

        uint8_t   *pic_point_w = pBuffer;

    SPI_FLASH_WriteEnable();

       Flash_Delsy_ms(500);

        do {

     memset(FLASH_SPI_Tx_Buf,0x00,255);

            FLASH_SPI_Tx_Buf[0] = WRITE;

            FLASH_SPI_Tx_Buf[1] = (uint8_t)((addr & 0x00FF0000) >> 16);

            FLASH_SPI_Tx_Buf[2] = (uint8_t)((addr & 0x0000FF00) >> 8);

            FLASH_SPI_Tx_Buf[3] = (uint8_t)  addr ;

            len_cut = (len >= (0xFB - 4)) ? (0xFB) : (len + 4);

            memcpy(&FLASH_SPI_Tx_Buf[4], pic_point_w, (len_cut - 4));

            SPI_FLASH_CS_LOW;

            FLASH_spi_xfer_done = false;

            APP_ERROR_CHECK(nrf_drv_spi_transfer(&FLASH_spi, FLASH_SPI_Tx_Buf,len_cut, FLASH_SPI_Rx_Buf, len_cut));

             while(FLASH_spi_xfer_done == false);

            SPI_FLASH_CS_HIGH;

            addr += (len_cut - 4);

            pic_point_w += (len_cut - 4);

     len -= (len_cut - 4);

       Flash_Delsy_ms(10);

           } while(len > 0);

        SPI_FLASH_CS_HIGH;

        SPI_FLASH_WaitForWriteEnd();

    }

  • Hi,

    The tx_buffer_length and rx_buffer_length parameters in nrf_drv_spi_transfer is declared as uint8_t, meaning it can only hold a length of 255 bytes. 

    ret_code_t nrf_drv_spi_transfer(nrf_drv_spi_t const * const p_instance,
                                    uint8_t const * p_tx_buffer,
                                    uint8_t         tx_buffer_length,
                                    uint8_t       * p_rx_buffer,
                                    uint8_t         rx_buffer_length);

    There is a limitation of 255 bytes for the MAXCNT registers in SPIM peripherals with EasyDMA support, but legacy SPI peripheral should be able to send longer transfers (1 byte at the time, but the driver will support long transfers).

    If you want to use longer buffers, you can use the nrfx_spi driver. Note that the SPIM peripheral is limited to 255 bytes transfers in nRF52832, so you can not use the nrfx_spim driver.

    Best regards,
    Jørgen

  • Hi,thank you for your answer!As it has a limit of 255。may I transmit these 256 bytes twice or more?

  • Yes, the EasyDMA in SPIM peripheral support an ArrayList feature, allowing you to send multiple buffers after each other.

Related