How can I assign a new Slave Select Pin to the SPI?

if i am controlling two spi devices with one instance, can I just define a new SS in the same SPIconfiguration or do i have to close the connection and select a new one?

is it possible to control the second SPI while the first one is waiting to receive data?

if not, can i use another spi instance or do both Instances are clock connected?

Using

NRF52832
PCA10040

using SPIM and the newest library version

Parents
  • Hello,

    if i am controlling two spi devices with one instance, can I just define a new SS in the same SPIconfiguration or do i have to close the connection and select a new one?

    I am not entirely sure what you mean when you say close the connection, but you could toggle the SS pin as a GPIO, to control which device on the SPI bus that receives the transfer.

    is it possible to control the second SPI while the first one is waiting to receive data?

    Could you elaborate on what you mean by this as well?
    Only one slave may be active on MISO at the same time - so unless there is a waiting period in between transfers in which you release the SS of the first device, and then toggle the SS of the second device, then this is not possible, no.
    Slave devices will only listen for messages on the bus when their SS is active.

    if not, can i use another spi instance or do both Instances are clock connected?

    You may indeed use a separate SPI instance for each device, just duplicate the setup and configuration for your previous SPIM instance and change the pin configuration.
    You could also have multiple SPI devices on the same SPI bus, but as mentioned you will need to control which of the devices you are targeting by selecting their SS pin.

    Best regards,
    Karl

Reply
  • Hello,

    if i am controlling two spi devices with one instance, can I just define a new SS in the same SPIconfiguration or do i have to close the connection and select a new one?

    I am not entirely sure what you mean when you say close the connection, but you could toggle the SS pin as a GPIO, to control which device on the SPI bus that receives the transfer.

    is it possible to control the second SPI while the first one is waiting to receive data?

    Could you elaborate on what you mean by this as well?
    Only one slave may be active on MISO at the same time - so unless there is a waiting period in between transfers in which you release the SS of the first device, and then toggle the SS of the second device, then this is not possible, no.
    Slave devices will only listen for messages on the bus when their SS is active.

    if not, can i use another spi instance or do both Instances are clock connected?

    You may indeed use a separate SPI instance for each device, just duplicate the setup and configuration for your previous SPIM instance and change the pin configuration.
    You could also have multiple SPI devices on the same SPI bus, but as mentioned you will need to control which of the devices you are targeting by selecting their SS pin.

    Best regards,
    Karl

Children
  • Hey thanks for trying to answer the questions, i will explain a little bit more:
     

    1) I know I have to change the SS, the question is in which part does this take place then:
    In the initializeSPI_Instance you define which are the pins clock, miso, mosi and ss, plus the frequency, etc. Should i overwrite the structure with a function that makes something like:
    spi_config.ss_pin= NEW_SS_PIN

    I somehow think this is not possible since the structure is already defined

    2) Whenever you activate a sensor with spi you have to wait for the sensor to gather the data, normally you could just poll until it is done. What I want to do is to use this time to effectuate a second SPI call, with the same instance, to another sensor, so that it starts gathering information

    3) If i need a SPI Instance for each sensor, then i can only get two slaves with the NRFf5832 PCA1400....
    So yeah my question is precisely how: 

    you will need to control which of the devices you are targeting by selecting their SS pin


    and i know that i have to change the SS pin, the question is where? do you have a snippet where SPIM_0 addresses two slaves? 

    thank you! 

  • Hello again,

    Javiehm said:
    thanks for trying to answer the questions, i will explain a little bit more

    No problem at all, I am happy to help!
    Thank you for elaborating.

    Javiehm said:
    Should i overwrite the structure with a function that makes something like:
    spi_config.ss_pin= NEW_SS_PIN

    I somehow think this is not possible since the structure is already defined

    Oh, now I understand what you mean. My apologies, I should have been more explicit.
    Yes, the SPIM peripheral can not control the SS pin directly. Please see the answer by my colleague Håkon in this ticket. Furthermore, neither of the SPI drivers are made for having multiple slaves on the same bus, so in this case you will have to control the SS pins separately.
    You can read more about this in the driver description.
    I do not recommend that you modify the driver to incorporate this, since modifying of the driver may break it. Instead, you could create a separate function for initiating a transfer with either device, and toggle the appropriate SS pin from there.

    Javiehm said:
    2) Whenever you activate a sensor with spi you have to wait for the sensor to gather the data, normally you could just poll until it is done. What I want to do is to use this time to effectuate a second SPI call, with the same instance, to another sensor, so that it starts gathering information

    The first sensor will be listening in on the bus as long as its SS is active, so if you intend to communicate with another sensor you will need to deactivate the first SS so that it does not receive the commands meant for the second device.
    If the device you are working with does not require its SS to be high for the entire duration of the initial command + processing delay then I see no reason why this should not work.
    Essentially, you would then toggle the first SS, send the first command, toggle the first SS, toggle the second SS, send some command, toggle the second SS, toggle the first SS, and finally receive the data from the initial device.

    Javiehm said:
    and i know that i have to change the SS pin, the question is where? do you have a snippet where SPIM_0 addresses two slaves? 

    I do not have this, unfortunately, but I would suggest following the approach from the top of my answer - incorporating the SS toggling in a separate function for initiating transfers with a specific device. Note that you should create some kind of semaphore to avoid both SS being selected at the same time.

    Best regards,
    Karl

  • perfect! thank you very much! i will let you know how it goes! (this, of course is the answer to the last comment)

  • perfect! thank you very much!

    No problem at all, I am happy to help! :) 

    i will let you know how it goes!

    Great, I look forward to hearing about it!

    Please do not hesitate to open another ticket if you should encounter any issues or questions in the future.

    Good luck with your development!

    Best regards,
    Karl

Related