Choose the UARTE API (Async/interrupt driven) during runtime

Hello,

I need a way to decide on which Api to use for UARTE (configured as uart1) once in main(). I am not intending to switch between them nor to use them at the same time. I am aware that it is not possible to use both simultaneously on the same uart. I am using the the nRF52840 with the following configuration

/ {
    chosen {
        zephyr,console = &uart0;
    };
};


&uart0 {
    compatible = "nordic,nrf-uart";
    current-speed = <115200>;
    status = "okay";
    pinctrl-0 = <&uart0_default_alt>;
    pinctrl-1 = <&uart0_sleep_alt>;
    pinctrl-names = "default", "sleep";
};


&uart1 {
    compatible = "nordic,nrf-uarte";
    current-speed = <115200>;
    status = "okay";
    pinctrl-0 = <&uart1_default_alt>;
    pinctrl-1 = <&uart1_sleep_alt>;
    pinctrl-names = "default", "sleep";
    
    /* Used for communication on modbus sensor */
    test_dev: modbus0 {
		compatible = "zephyr,modbus-serial";
		status = "okay";
	};
};

  • Uart0 is chosen for the console debugging.
  • Uart1 needs to work using
    • the interrupt driven api if the sensor is of type x
    • and work with the async api if the sensor is of type y.

Any help, even a hacky workaround, will be much appreciated. 

  • This ended up my hack/workaround to be able to choose the api at bootup and not during build -> see attached file.

    Basically, the idea is to create a uarte_nrfx_data struct that includes both int_drv and async configs and then in a SYS_INIT function decide on which api to use and overwrite the other one to NULL.

    -> Do a diff check to mainstrem version (zephyr v3.2.0) to follow the diffs.

    CONFIG_SERIAL=y
    CONFIG_UART_INTERRUPT_DRIVEN=y
    CONFIG_UART_ASYNC_API=y
    
    # Int-drviven needs to be disabled, as it overwrites async config and its dependent configs (such as timer/ HW_ASYNC)
    # Int driven will be enabled (forced enable in uart_nrfx_uarte.c) 
    CONFIG_UART_1_INTERRUPT_DRIVEN=n
    CONFIG_UART_1_ASYNC=y
    
    # Necessary for the bytes counting to work correctly, otherwise the communication won't be robust 
    CONFIG_UART_1_NRF_HW_ASYNC=y
    CONFIG_UART_1_NRF_HW_ASYNC_TIMER=2

    uart_nrfx_uarte_modified.c

Related