App crashes when BLE write calls a lvgl screen to show

My app keep crashing when I call a screen change from a bluetooth write function. The central sends a message to the peripheral. The peripheral wakes up the display and tries to show a lvgl screen with a single label. I see it for a second. I keep thinking I need a message queue or a different thread for the display. I can try a few things but didn't want to guess.

This is the write callback on the peripheral:

static ssize_t write_vnd(struct bt_conn *conn, const struct bt_gatt_attr *attr,
			 const void *buf, uint16_t len, uint16_t offset,
			 uint8_t flags)
{
	//this means a write has happened to the peripheral, the controller has written
	uint8_t *value = attr->user_data;

	if (offset + len > VND_MAX_LEN) {
		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
	}

	memcpy(value + offset, buf, len);
	value[offset + len] = 0;

	screenmanager_show_token();
	eventhandler_reset();
	return len;
}
it calls the:

void screenmanager_show_token() {
    //clear the current screen and show the next
    lv_obj_clean(lv_scr_act());
    screen_token_init();
}
which is just:
void screen_token_init() {

	lv_obj_t *token_label;
	token_label = lv_label_create(lv_scr_act());
	lv_label_set_text(token_label, "Hello World");
	lv_obj_align(token_label, LV_ALIGN_TOP_MID, 0, 0);

}
  • Hi,

    When you say your app crashed, I assume there's some kind of fault being triggered? Does it print something like "MPU fault" or "hard fault"?

    I think that the main issue here is that too much work is being done in a callback function. Your intuition of using something like a workqueue and processing the screen drawing in another thread (if I understood you correctly) is probably the right way to go about this.

    If you decide to do the work in another thread, remember not to create that thread in the callback function, lest you meet this issue:  Starting a new thread from a BLE callback

    I'm sorry for the delay in answering, I had something of a workqueue myself these days. Thanks for your patience!

    Best regards,

    Raoul

Related