Changing interrupt priority

Hi.

I am using a custom board based on the nRF52820 (SDK v2.7.0). 

I have a task in front of me to design a battery charger.  

In the program I use two interrupts to solve this task:
1. from USB (events NRFX_POWER_USB_EVT_DETECTED and NRFX_POWER_USB_EVT_REMOVED).
2. from GPIO (the microcontroller pin is connected to the MCP73831 battery charge chip and reads the STAT pin value).

If USB is connected and chg_status_pin is low, it means the battery is charging (I output the message “USB_CONNECTED --> CHARGING”) and adding a callback function for chg_status_pin. The GPIO callback will occur when the GPIO_INT_EDGE_RISING event occurs (the pin will change state from 0 to 1). 

USB callback code -->

void usb_event_handler(nrfx_power_usb_evt_t event)
{
    switch (event)
    {
        case NRFX_POWER_USB_EVT_DETECTED:
			bool pin_state_val;
			pin_state_val = gpio_pin_get_dt(&chg_status_pin);
			if (pin_state_val) {
			    gpio_add_callback(chg_status_pin.port, &chg_status_pin_cb_data);
			    LOG_DBG("USB_CONNECTED --> CHARGING");
			} else {
				LOG_DBG("USB_CONNECTED --> NO BATTERY");
			}
            break;
            
		case NRFX_POWER_USB_EVT_REMOVED:
			gpio_remove_callback(chg_status_pin.port, &chg_status_pin_cb_data);
			LOG_DBG("USB_DISCONNECTED");
            break;
		default:
		break;
    }
}

GPIO callback code -->

void chg_status_pin_rising(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
{
    LOG_DBG(" 100% - CHARGING COMPLETE");
    gpio_remove_callback(chg_status_pin.port, &chg_status_pin_cb_data);
}
If I plug in the USB and wait for the battery to finish charging (waiting for the GPIO interrupt to trigger), and then unplug the USB, everything works correctly. 
Screen -->

But if I plug in the USB and disconnect the USB without waiting for the charge to finish, I see the same picture. Although the GPIO interrupt should not have triggered because I remove this interrupt (gpio_remove_callback) when NRFX_POWER_USB_EVT_REMOVED event occurs. 

Screen -->

Both callbacks (from USB and from GPIO) depend on the VBUS state. But for some reason the GPIO callback is processed faster than the USB callback.


Could it be related to interrupt priorities?
Is there any way to increase the USB interrupt priority or “slow down” the GPIO voltage response speed?


Thanks.

Parents Reply Children
Related