Using NRFX SPIM and Zephyr SPI API's simultaneously

Hi - I am seeking clarification on a topic as I am a little unsure on the correct way to do things.

I'm using an nRF52840 and porting an old Firmware from nRF5 SDK to NCS.

One SPI is used in a very simple and standard way. Another SPI is used via the PPI to sample an Accelerometer accurately at a high data rate.

The SPI that uses the PPI will obviously use the NRFX SPIM and for the other simple SPI I want to use the Zephyr API.

I'm seeing mixed reports in my research as to how best achieve this. In this example I see that the SPI to be used via NRFX SPIM is still defined in the device tree as disabled.


/* The following two nodes (corresponding to the SPIM2 and UARTE2 peripherals,
* respectively) need to be disabled so that Zephyr drivers won't initialize
* those instances; the application will use them via nrfx drivers. But their
* pins must be specified, as the application expects to get this information
* from devicetree.
*/
&spi2 {
compatible = "nordic,nrf-spim";
status = "disabled";
pinctrl-0 = <&spi2_default_alt>;
/delete-property/ pinctrl-1;
pinctrl-names = "default";
cs-gpios = <&gpio1 6 GPIO_ACTIVE_LOW>;
};


Then, in the code, it seems to install a shared interrupt handler:

/* Install a shared interrupt handler for peripherals used via
* nrfx drivers. It will dispatch the interrupt handling to the
* driver for the currently initialized peripheral.
*/
BUILD_ASSERT(
DT_IRQ(SPIM_NODE, priority) == DT_IRQ(UARTE_NODE, priority),
"Interrupt priorities for SPIM_NODE and UARTE_NODE need to be equal.");
IRQ_CONNECT(DT_IRQN(SPIM_NODE), DT_IRQ(SPIM_NODE, priority),
nrfx_isr, nrfx_prs_box_2_irq_handler, 0);

Is this the correct way to do things?

This example also seems to suggest that the two devices with different API's cannot be used simultaneously? Can you clarify that also?

Parents
  • Hi Matt,

    You should be able to use them simultaneously, provided you use separate SPI instances for both and you do not try to use both APIs on the same hardware instance at the same time. .

    You see the status disabled for the  SPI instance you want to use with the nrfx driver because we don't want Zephyr to initialize and claim that peripheral. Though status is disabled, you still specify the pins so your application can retrieve them from the devicetree. This i what is shown in the nrfx_prs sample that you mentioned.

    So use Zephyr API for one SPI instance (which is enabled in devicetree). And use nrfx API for another SPI instance (disabled in devicetree, but pins specified). 

    Now, in the sample that you linked, it uses IRQ_CONNECT to connect the interrupt to the nrfx handler. This is necessary for the nrfx driver to function correctly:

    Some peripherals (e.g., SPIM2, UARTE2) share the same interrupt line. Zephyr can't manage this dynamically, so nrfx_prs lets multiple NRFX drivers safely share an IRQ.

    -Priyanka

Reply
  • Hi Matt,

    You should be able to use them simultaneously, provided you use separate SPI instances for both and you do not try to use both APIs on the same hardware instance at the same time. .

    You see the status disabled for the  SPI instance you want to use with the nrfx driver because we don't want Zephyr to initialize and claim that peripheral. Though status is disabled, you still specify the pins so your application can retrieve them from the devicetree. This i what is shown in the nrfx_prs sample that you mentioned.

    So use Zephyr API for one SPI instance (which is enabled in devicetree). And use nrfx API for another SPI instance (disabled in devicetree, but pins specified). 

    Now, in the sample that you linked, it uses IRQ_CONNECT to connect the interrupt to the nrfx handler. This is necessary for the nrfx driver to function correctly:

    Some peripherals (e.g., SPIM2, UARTE2) share the same interrupt line. Zephyr can't manage this dynamically, so nrfx_prs lets multiple NRFX drivers safely share an IRQ.

    -Priyanka

Children
No Data
Related