Suspending UART Async using PM_DEVICE ACTION_SUSPEND causes crash.

Hello, 

I am currently using the BLE NUS example from the Nordic Dev Academy course for BLE (https://github.com/NordicDeveloperAcademy/bt-fund/blob/main/lesson4/blefund_less4_exer3_solution/src/main.c).

However, the current consumption was around 3.5 mA, which is too high for my application.

Thus, I used the commands "pm_device_action_run(uart, PM_DEVICE_ACTION_SUSPEND);" on the ble connection callback and "pm_action_run(uart, PM_DEVICE_ACTION_RESUME);" to suspend the UART driver and hopefully reduce current consumption. After the device is suspended, the current consumption does go down to around 300 uA. The idea is that when the device is not connected on bluetooth, UART would be suspended to save power, and UART could resume as soon as the device is reconnected on bluetooth. I implemented the code as shown below:

static void connected(struct bt_conn *conn, uint8_t err)
{
	char addr[BT_ADDR_LE_STR_LEN];

	if (err) {
		LOG_ERR("Connection failed (err %u)", err);
		return;
	}

	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
	LOG_INF("Connected %s", addr);

	current_conn = bt_conn_ref(conn);

	dk_set_led_on(CON_STATUS_LED);
	
	pm_device_action_run(uart, PM_DEVICE_ACTION_RESUME);
	struct uart_data_t *buf;
	uart_rx_enable(uart, buf->data, sizeof(buf->data), UART_WAIT_FOR_RX);
}

static void disconnected(struct bt_conn *conn, uint8_t reason)
{
	char addr[BT_ADDR_LE_STR_LEN];

	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

	LOG_INF("Disconnected: %s (reason %u)", addr, reason);

	if (auth_conn) {
		bt_conn_unref(auth_conn);
		auth_conn = NULL;
	}

	if (current_conn) {
		bt_conn_unref(current_conn);
		current_conn = NULL;
		dk_set_led_off(CON_STATUS_LED);
	}
	
	uart_rx_disable(uart);
	pm_device_action_run(uart, PM_DEVICE_ACTION_SUSPEND);
}

However, whenever PM_DEVICE_ACTION_SUSPEND is executed, the device crashes and the RGB LED starts to flicker. The same occurs if I put the "pm_device_action_run(uart, PM_DEVICE_ACTION_SUSPEND);" statement in the main function after all initializations.

For context, I am using the EVK-ANNA-B112 development board that uses nrf52832. When I flash the device, the UART TX and RX pins are connected to a separate device (EmStat Pico Development Board) for communication. 

Is there anything I am getting wrong about suspending UART Async? 

Related