Hello,
I have implemented an application for the nRF52832 that uses peripheral NUS with LittleFS. The application receives sensor data via UART and stores it to a file using LittleFS. The central can then send a command via NUS which triggers reading the file with sensor data. All the file operations like reading, writting, erasing, and listing directory are implemented in a workqueue like recommended by Littlefs thread safe to make it thread safe.
Here is the file read handler:
static void littleFS_read_handler() { int err; struct fs_dirent entry; size_t pos = 0; int bytes_read = 0; char inChar; char file_read_buf[100]; err = fs_stat("/lfs/test.txt", &entry); if (err) { bt_nus_send(NULL, "No file to read\n", 16); return; } err = fs_open(&myFile, "/lfs/test.txt", FS_O_READ); if (err < 0) { LOG_INF("Error opening file [%d]\n", err); return; } bt_nus_send(NULL, "---Reading File---", strlen("---Reading File---")); while (pos < 100 - 1) { bytes_read = fs_read(&myFile, &inChar, 1); if (bytes_read == 0) { break; } if (inChar == '\n') { file_read_buf[pos] = '\0'; bt_nus_send(NULL, file_read_buf, strlen(file_read_buf)); pos = 0; file_read_buf[pos] = '\0'; } file_read_buf[pos++] = inChar; } bt_nus_send(NULL, "---Closing File---", strlen("---Closing File---")); fs_close(&myFile); }
The sensor data is stored in lines separated by '\n' in the file (Note: no line is expected to exceed 50 chars), so I read each line and send it via NUS. I have allocated a 256 kB storage partition in the nRF52832 NVM, so this line-by-line NUS send approach becomes more time consuming as data fills up. It takes around 1 minute and 30 seconds to transfer a full file from LittleFS through NUS.
Is there a better approach for transfering the data from LittleFS to the BLE central? I want to keep NUS functionality for this application, but I know it has a 251 char limit per transfer, so I am also open to adding another service.
Thank you!