Filesystem does not work with BLE Advertise

Hello everyone!

I want to use BLE with the filesystem (FS), but I haven’t found any examples. When I enable BLE advertising, any filesystem operations stop working.

(As a base, I used this example: https://github.com/Toastee0/Seeed-Xiao-nRF54L15/tree/main/xiao_expanded/sd_card_hw  and added the BLE part in main.)

int main(void)
{
	LOG_INF("========================================");
	LOG_INF("XIAO nRF54L15 SD Card + ZMS Sample");
	LOG_INF("========================================");

    int err;

    err = bt_enable(NULL);
    if (err) {
        LOG_ERR("Bluetooth init failed (err %d)", err);
        return err;
    }

//    LOG_INF("Bluetooth initialized");

    // 3. Start advertising
    err = bt_le_adv_start(&adv_param, ad, ARRAY_SIZE(ad), NULL, 0);
    if (err) {
        LOG_ERR("Advertising failed to start (err %d)", err);
        return err;
    }

#ifdef CONFIG_ZMS
	/* Initialize ZMS for persistent storage */
	LOG_INF("Initializing ZMS...");
	int zms_rc = zms_init();
	if (zms_rc == 0) {
		zms_demo();
	} else {
		LOG_ERR("ZMS initialization failed, continuing without persistent storage");
	}
#endif

	/* raw disk i/o */
	LOG_INF("Initializing SD card...");
	do {
		static const char *disk_pdrv = DISK_DRIVE_NAME;
		uint64_t memory_size_mb;
		uint32_t block_count;
		uint32_t block_size;

		if (disk_access_ioctl(disk_pdrv,
				DISK_IOCTL_CTRL_INIT, NULL) != 0) {
			LOG_ERR("SD card init ERROR! Please check:");
			LOG_ERR("  - SD card is inserted");
			LOG_ERR("  - SD card is formatted (FAT32/exFAT)");
			LOG_ERR("  - Connections are correct");
			break;
		}

		if (disk_access_ioctl(disk_pdrv,
				DISK_IOCTL_GET_SECTOR_COUNT, &block_count)) {
			LOG_ERR("Unable to get sector count");
			break;
		}
		LOG_INF("SD Card - Block count: %u", block_count);

		if (disk_access_ioctl(disk_pdrv,
				DISK_IOCTL_GET_SECTOR_SIZE, &block_size)) {
			LOG_ERR("Unable to get sector size");
			break;
		}
		LOG_INF("SD Card - Sector size: %u bytes", block_size);

		memory_size_mb = (uint64_t)block_count * block_size;
		LOG_INF("SD Card - Total size: %u MB", (uint32_t)(memory_size_mb >> 20));

#ifdef CONFIG_ZMS
		/* Update SD card access counter in ZMS */
		zms_update_sd_count();
#endif

		if (disk_access_ioctl(disk_pdrv,
				DISK_IOCTL_CTRL_DEINIT, NULL) != 0) {
			LOG_ERR("Storage deinit ERROR!");
			break;
		}
	} while (0);

	mp.mnt_point = disk_mount_pt;

	LOG_INF("Mounting SD card filesystem...");
	int res = fs_mount(&mp);

	if (res == FS_RET_OK) {
		LOG_INF("SD card mounted at %s", disk_mount_pt);
		
		/* Test unmount and remount */
		LOG_INF("Testing unmount/remount...");
		res = fs_unmount(&mp);
		if (res != FS_RET_OK) {
			LOG_ERR("Error unmounting disk");
			return res;
		}
		
		res = fs_mount(&mp);
		if (res != FS_RET_OK) {
			LOG_ERR("Error remounting disk");
			return res;
		}
		LOG_INF("Unmount/remount test successful");

		/* List directory contents */
		if (lsdir(disk_mount_pt) >= 0) {
			LOG_INF("Creating sample files and directories...");
			if (create_some_entries(disk_mount_pt)) {
				LOG_INF("Sample entries created, listing again:");
				lsdir(disk_mount_pt);
			}
		}
		
		/* Write a test file with timestamp */
		char test_file_path[64];
		snprintf(test_file_path, sizeof(test_file_path), "%s/test.txt", disk_mount_pt);
		struct fs_file_t test_file;
		fs_file_t_init(&test_file);
		
		res = fs_open(&test_file, test_file_path, FS_O_CREATE | FS_O_WRITE);
		if (res == 0) {
			const char *test_data = "XIAO nRF54L15 SD Card Test\n";
			fs_write(&test_file, test_data, strlen(test_data));
			fs_close(&test_file);
			LOG_INF("Created test file: %s", test_file_path);
		} else {
			LOG_WRN("Could not create test file (err %d)", res);
		}
		
		/* Unmount before exiting */
//		fs_unmount(&mp);
//		LOG_INF("SD card unmounted");
	} else {
		LOG_ERR("Failed to mount SD card (err %d)", res);
		LOG_ERR("Common issues:");
		LOG_ERR("  - SD card not formatted (use FAT32)");
		LOG_ERR("  - SD card not inserted properly");
		LOG_ERR("  - Incompatible SD card");
	}

	LOG_INF("========================================");
	LOG_INF("Sample completed. System will idle.");
	LOG_INF("========================================");

	while (1) {
		k_sleep(K_SECONDS(1));
	}
	return 0;
}

Without BLE advertising, everything works fine, but with it, the system can't mount or open any files, and similar operations fail. Maybe someone can suggest something?

Related