Which macro to get sck, mosi and miso pin on spi master

Hello,

I works with Zephyr, VCS, SDK 2.0.0

I don't find how to get pin number from the DTS pinctrl .

In this folowing code, I want to replace magic number (uplighted in yellow) by macro to acces data of the DTS 

Thank's by advance for your help,

    nrfx_spim_config_t spim_config = NRFX_SPIM_DEFAULT_CONFIG(NRFX_SPIM_PIN_NOT_USED,NRFX_SPIM_PIN_NOT_USED,NRFX_SPIM_PIN_NOT_USED,NRFX_SPIM_PIN_NOT_USED/*NRF_DT_GPIOS_TO_PSEL(SPIM_NODE, cs_gpios)*/);
    spim_config.sck_pin=6+32*1;
    spim_config.mosi_pin=7+32*1;
    spim_config.miso_pin=17+32*0;
    spim_config.frequency = NRF_SPIM_FREQ_1M;
    spim_config.skip_gpio_cfg = true;
    spim_config.skip_psel_cfg = false;
    nrfx_spim_init(&spi_rfid_instance, &spim_config, spi_rfid_handler, NULL);

And my DTS are

&spi1 {
    compatible = "nordic,nrf-spim";
    status = "okay";
    pinctrl-0 = <&spi1_default>;
    pinctrl-1 = <&spi1_sleep>;
    pinctrl-names = "default", "sleep";
    //cs-gpios = <&gpio0 27 GPIO_ACTIVE_LOW>;
};

    spi1_default: spi1_default {
        group1 {
            psels = <NRF_PSEL(SPIM_SCK, 1, 6)>,
                <NRF_PSEL(SPIM_MOSI, 1, 7)>,
                <NRF_PSEL(SPIM_MISO, 0, 17)>;
        };
    };

    spi1_sleep: spi1_sleep {
        group1 {
            psels = <NRF_PSEL(SPIM_SCK, 1, 6)>,
                <NRF_PSEL(SPIM_MOSI, 1, 7)>,
                <NRF_PSEL(SPIM_MISO, 0, 17)>;
            low-power-enable;
        };
    };
Parents
  • To answer your original question (for cases like mine where I use PINCTRL but also had to use the NRFX SPI driver for one of my SPI peripherals in order to utilize PPI), I did not find any DT macros that would just do this. Understanding the Device Tree, you can use a combination of other macros to access pin info. Here's an example:

    dtsi file:

    ...    
        spi1_default: spi1_default {
            group1 {
                psels = <NRF_PSEL(SPIM_SCK, 1, 9)>,
                        <NRF_PSEL(SPIM_MOSI, 1, 8)>,
                        <NRF_PSEL(SPIM_MISO, 0, 4)>;
            };
        };
    ...

    dts file:

    ...
    &spi1 {
        compatible = "nordic,nrf-spim";
        status = "okay";
        pinctrl-0 = <&spi1_default>;

        pinctrl-1 = <&spi1_sleep>;
        pinctrl-names = "default", "sleep";
        cs-gpios = <&gpio0 31 GPIO_ACTIVE_LOW>;
    };

    ...

    application:

    #define SPI_NODE DT_NODELABEL(spi1)
    #define SPI_PINCTRL_NODE DT_CHILD(DT_PINCTRL_0(SPI_NODE, 0), group1)

    Then in your SPI Configuration structure, you can grab the property like this:
    nrfx_spim_config_t xSpiConfig = {
    .sck_pin        = (uint8_t)DT_PROP_BY_IDX(SPI_PINCTRL_NODE, psels, 0) & 0x3F,
    .mosi_pin       = (uint8_t)DT_PROP_BY_IDX(SPI_PINCTRL_NODE, psels, 1) & 0x3F,
    .miso_pin       = (uint8_t)DT_PROP_BY_IDX(SPI_PINCTRL_NODE, psels, 2) & 0x3F,
    .ss_pin         = DT_GPIO_PIN_BY_IDX(SPI_NODE, cs_gpios, 0),
    ...

    The psel property will return a 32-bit configuration (defined in nrf-pinctrl.h), but you just need the first 6 bits.

    If anyone knows an easier way, please share. 

Reply
  • To answer your original question (for cases like mine where I use PINCTRL but also had to use the NRFX SPI driver for one of my SPI peripherals in order to utilize PPI), I did not find any DT macros that would just do this. Understanding the Device Tree, you can use a combination of other macros to access pin info. Here's an example:

    dtsi file:

    ...    
        spi1_default: spi1_default {
            group1 {
                psels = <NRF_PSEL(SPIM_SCK, 1, 9)>,
                        <NRF_PSEL(SPIM_MOSI, 1, 8)>,
                        <NRF_PSEL(SPIM_MISO, 0, 4)>;
            };
        };
    ...

    dts file:

    ...
    &spi1 {
        compatible = "nordic,nrf-spim";
        status = "okay";
        pinctrl-0 = <&spi1_default>;

        pinctrl-1 = <&spi1_sleep>;
        pinctrl-names = "default", "sleep";
        cs-gpios = <&gpio0 31 GPIO_ACTIVE_LOW>;
    };

    ...

    application:

    #define SPI_NODE DT_NODELABEL(spi1)
    #define SPI_PINCTRL_NODE DT_CHILD(DT_PINCTRL_0(SPI_NODE, 0), group1)

    Then in your SPI Configuration structure, you can grab the property like this:
    nrfx_spim_config_t xSpiConfig = {
    .sck_pin        = (uint8_t)DT_PROP_BY_IDX(SPI_PINCTRL_NODE, psels, 0) & 0x3F,
    .mosi_pin       = (uint8_t)DT_PROP_BY_IDX(SPI_PINCTRL_NODE, psels, 1) & 0x3F,
    .miso_pin       = (uint8_t)DT_PROP_BY_IDX(SPI_PINCTRL_NODE, psels, 2) & 0x3F,
    .ss_pin         = DT_GPIO_PIN_BY_IDX(SPI_NODE, cs_gpios, 0),
    ...

    The psel property will return a 32-bit configuration (defined in nrf-pinctrl.h), but you just need the first 6 bits.

    If anyone knows an easier way, please share. 

Children
Related