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

implement 2 hardware SPI

i want to implement 2 hardware SPI communication in my code. i have one already working. talking with LCD ILI9341. the functions are using spi_write /  nrf_drv_spi_transfer for a single interface.  what do i need to do to add another (4 pin) SPI interface to communicate with accelerometer. do i need to change it to SPI1 and then duplicate everything to SPI2 ?

thanks

Yuval

  • You do not need to add another 4 pins, you can still use the same 4 pins to communicate with the accelerometer, but you need a new slave select pin for sending the data  to this slave. Please take a look at slave configuration at the wiki

  • i already have another 4 pins connected to the acc chip:
    #define LSM6DS3_MOSI_PIN 12
    #define LSM6DS3_MISO_PIN 13
    #define LSM6DS3_SCK_PIN 14
    #define LSM6DS3_SS_PIN 15

    my question is , does anyone have a code example of using 2 different SPI ?

  • If you are using standard Nordic libraries and so on then you just need to use a new instance and duplicate the code. ie if you are doing something like this:

    APP_ERROR_CHECK(nrf_drv_spi_init(&mAccSpiInstance, &spi_config, acc_spi_event_handler, NULL));
    

    Then all you need to do is duplicate the code - typically in a separate c module - and use a new instance thus:

    #define SPI_INSTANCE  1   // SPI instance index for 2nd SPI
    static const nrf_drv_spi_t mAccSpiInstance = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);
    

    Ensure SPI1 is also enabled in sdk_config.h, of course, assuming you use SPI0 and SPI1.

    As an aside, I recommend your choice of hardware implementation as a physically separate SPI interconnect if you want the best analogue performance as any noise on the SPI lines - when updating the display, for instance - will inject some noise into the accelerometer unless you synchronously run the display interface outside of times when the accelerometer (or other SPI ADC or AFE) is being clocked

  • As Aryan mentioned you'd need to implement a method of selecting/deselecting the chip select pin on each of your SPI ICs.  I found the easier method for this is to writer a wrapper class around nrf_spi_drv  where you pass the chip select pins in at initialization.

  • I notice I am using pull-up on MISO, not sure if that is relevant. Did you get your setup working?

    // > P0.07 ACC-INT1    CNF: 0x00000000 [Sense: Off] [Drive: Normal] [Pull: None] [Input: Connected ] [Dir: Input ] (now 0) (no map)
    // > P0.09 ACC-MISO    CNF: 0x0000000C [Sense: Off] [Drive: Normal] [Pull: P-Up] [Input: Connected ] [Dir: Input ] (now 1) (SPI2 MISO)
    // > P0.17 ACC-SCK     CNF: 0x00000003 [Sense: Off] [Drive: Normal] [Pull: None] [Input: Disconnect] [Dir: Output] (now 0) (SPI2 SCK)
    // > P0.19 ACC-MOSI    CNF: 0x00000003 [Sense: Off] [Drive: Normal] [Pull: None] [Input: Disconnect] [Dir: Output] (now 0) (SPI2 MOSI)
    // > P0.20 ACC-CS      CNF: 0x00000003 [Sense: Off] [Drive: Normal] [Pull: None] [Input: Disconnect] [Dir: Output] (now 0) (no map)
    

Related