This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

gpio_nrfx_port_toggle_bits() not task / interrupt save

Dear Support

I think gpio_nrfx_port_toggle_bits() is not task / interrupt save.

When between nrf_gpio_port_out_read() and nrf_gpio_port_out_write() a task switch or interrupt happend and in an the same port is modified nrf_gpio_port_out_write() restore the previous value and the modification is lost.

static int gpio_nrfx_port_toggle_bits(const struct device *port,
uint32_t mask)
{
NRF_GPIO_Type *reg = get_port_cfg(port)->port;
uint32_t value;

value = nrf_gpio_port_out_read(reg);
nrf_gpio_port_out_write(reg, value ^ mask);

return 0;
}

Can you confirm this?

RetoFelix

Parents
  • Hello RetoFelix,

    I think gpio_nrfx_port_toggle_bits() is not task / interrupt save.

    When between nrf_gpio_port_out_read() and nrf_gpio_port_out_write() a task switch or interrupt happend and in an the same port is modified nrf_gpio_port_out_write() restore the previous value and the modification is lost.

    Can you confirm this?

    You are correct that this function is not interrupt safe - you must account for it being interrupted at any time by higher-priority interrupts.
    If it is important for your application that there is no interruptions at this particular place you can either increase the module's interrupt priority level, or add guards to the instructions (place them in a critical region using CRITICAL_SECTION_ENTER and CRITICAL_SECTION_EXIT - keep these sections short in execution time, since interrupts are disabled for their duration) so they finish without being interrupted. 

    Best regards,
    Karl

Reply
  • Hello RetoFelix,

    I think gpio_nrfx_port_toggle_bits() is not task / interrupt save.

    When between nrf_gpio_port_out_read() and nrf_gpio_port_out_write() a task switch or interrupt happend and in an the same port is modified nrf_gpio_port_out_write() restore the previous value and the modification is lost.

    Can you confirm this?

    You are correct that this function is not interrupt safe - you must account for it being interrupted at any time by higher-priority interrupts.
    If it is important for your application that there is no interruptions at this particular place you can either increase the module's interrupt priority level, or add guards to the instructions (place them in a critical region using CRITICAL_SECTION_ENTER and CRITICAL_SECTION_EXIT - keep these sections short in execution time, since interrupts are disabled for their duration) so they finish without being interrupted. 

    Best regards,
    Karl

Children
Related