Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

I'm using the Segger nRF52840-DK and I want to add support for SPI?

I'm using the nRF52840-DK evaluation software from Segger. I want to add NAND Flash support, so I want to add an SPI interface. I have looked at the example in nRF5SDK160098a08e2 and that works nicely on my board, but I'm having difficulty moving that SPI example to the nRF52840-DK project as there are dependencies on Integration, module and components folders. isn't there a much simpler way to just add SPI support like there is when using ST Micro Cube IDE? I did manage to have a successful build with nrfx version of SPI, but the driver won't work correctly on my board.

I'm using the latest version of Segger Embedded Studio.

Regards,

John

  • Hello John,

    but I'm having difficulty moving that SPI example to the nRF52840-DK project as there are dependencies on Integration, module and components folders.

    Yes, the drivers and libraries of the SDK is located in these folders, and you will have to add them to your "preprocessor included directories" so that they are available to the preprosessor when you are going to compile your project. If you are working in Segger Embedded Studios, this is done by right-clicking the project name in the project explorer view and selecting options..->preprocessor->User Include Directories while in the common configuration.
    In your case, you mention that you are using the ST Micro Cube IDE, which I personally have never worked with - a brief google search on the topic yielded this forum post where they seem to discuss the relevant process for the ST Micro Cube IDE. In essence, you need to provide the IDE with the paths to your included files(*.h files) and you will have to include the source code files(*.c) in the project.

    I did manage to have a successful build with nrfx version of SPI, but the driver won't work correctly on my board.

    Could you elaborate what you mean by this - you are able to compile and flash the project, but it does not behave as expected? Or does something else happen before you are able to run it? Please be as specific as possible.

    Please let me know if this does not answer your question,

    Best regards,
    Karl

     

  • Hi Karl,

    Thank you for your response.

    When using ST MIcro Cube IDE, if I want SPI support, I open the config, and simply add a check mark next to the specific SPI peripheral, select if I want interrupt or DMA support and then click on generate code. This adds the required HAL libraries and also generates the necessary initialization and function calls to make the SPI hardware work. Here is an example of starting an SPI project, but this works equally well when adding SPI to an existing project.

    https://www.youtube.com/watch?v=5MYlSn-wR5g&list=PLnMKNibPkDnGtuIl5v0CvC81Am7SKpj02&index=50

    With nRF52840, I have two projects, one with a USB-MSD running on a ram disk, and the other project has nrf SPI driver example. To add NAND Flash connected via SPI to the USB-MSD project, I have to copy several folders (integration, module, components, etc) from the nrf SPI example, add the preprocessor include folders under options/common and then resolve any compile error incrementally. This is so cumbersome and slow and I was looking for a much simpler process, like the one used by ST Micro Cube IDE tool.

    Regarding the nfrx SPI driver, here is some of the relevant code.

    /*********************************************************************
     * @brief SPI user event handler.
     * @param event
    **********************************************************************
    */
    
    static void spi_event_handler(nrfx_spi_evt_t const * p_event,
                           void *                    p_context)
    {
      FS_USE_PARA(p_event);
      FS_USE_PARA(p_context);
      spi_xfer_done = true;
      FS_X_Log("Transfer completed.");
    }
    
    /*********************************************************************
    *
    *       _HW_Init
    */
    static int _HW_Init(U8 Unit) {
      FS_USE_PARA(Unit);
      BSP_ClrWP();
      BSP_SetHOLD();
      nrfx_spi_config_t spi_config = NRFX_SPI_DEFAULT_CONFIG;
      spi_config.ss_pin = SPI_SS_PIN;
      spi_config.miso_pin = SPI_MISO_PIN;
      spi_config.mosi_pin = SPI_MOSI_PIN;
      spi_config.sck_pin = SPI_SCK_PIN;
      spi_config.frequency = NRF_SPI_FREQ_8M;
      nrfx_spi_init(&spi, &spi_config, spi_event_handler, NULL);
      return 0;
    }
    
    /*********************************************************************
    *
    *       _HW_Read
    */
    static int _HW_Read(U8 Unit, void * pData, unsigned NumBytes) {
      FS_USE_PARA(Unit);
      FS_USE_PARA(pData);
      FS_USE_PARA(NumBytes);
    
      const nrfx_spi_xfer_desc_t xferd = { .p_tx_buffer = NULL,
                                           .tx_length = 0,
                                           .p_rx_buffer = pData,
                                           .rx_length = NumBytes,
                                           };
    
      return (nrfx_spi_xfer(&spi, &xferd, 0));
    }
    
    /*********************************************************************
    *
    *       _HW_Write
    */
    static int _HW_Write(U8 Unit, const void * pData, unsigned NumBytes) {
      FS_USE_PARA(Unit);
      FS_USE_PARA(pData);
      FS_USE_PARA(NumBytes);
    
      const nrfx_spi_xfer_desc_t xferd = { .p_tx_buffer = pData,
                                           .tx_length = NumBytes,
                                           .p_rx_buffer = NULL,
                                           .rx_length = 0,
                                           };
    
      return (nrfx_spi_xfer(&spi, &xferd, 0));
    }

    Here is what I see on the SPI protocol analyzer.

    CS goes low

    SPI MOSI 0xFF

    SPI MISO 0x00

    CS never returns high, so subsequent SPI calls fail because SPI is on use.

    Regards,

    John

  • BTW, working with Cypress Semiconductor Modus Toolbox or PSOC Creator tools also generates the required libraries, initialization code and call functions for selected peripherals. Working with Segger Embedded Studio and nRF is like going back 10 years where I have to write everything myself. I cannot believe this is true, so what am I missing?

    Regards,

    John

  • Let's make this simple. Start a new nRF52840 project with all the default settings. Show me how you add the required code to support for comms with an SPI device. Assume that no SPI example project existed.

    Regards,

    John

  • Is there no one from Nordic technical support who can help me here?

    Regards,

    John

Related