I have been playing around with using LittleFS to access the internal flash for reading/writing to non-volatile/persistent storage. However, especially with writing data, I notice that the rest of my main thread's asynchronous operations (LED toggles that occur every 0.5ms due to a timer interrupt, outputting data thru UART every 1-2ms, sending ESB packets to another device, etc) seem to stop (for about 80ms) while the write is occurring. From what I've seen online, LittleFS is built on using blocking calls; I imagine that's probably what's causing this issue.
I was hoping to circumvent this by spinning a thread solely used for LittleFS operations; my goal is that this thread runs only when the main thread is not handling interrupts and such. I've verified the code in the thread runs because of the LED functions. But the same issue occurs once the thread gets to doing a LittleFS write operation. I would expect that the main thread preempts the other thread in scenarios like the 'LED toggle' timer interrupt, especially since I've already set the priority of the thread as low as possible, but I don't even see those occurring. Here's a snippet of what I've done:
#define DATA_SIZE 20 struct my_command_t { void *fifo_reserved; uint8_t data[DATA_SIZE]; uint16_t len; }; static K_FIFO_DEFINE(fifo_storage_write_data); int main() { /* ... */ LED_Yellow(LED_ON); /* own function */ struct my_command_t *cmd = k_malloc(sizeof(struct my_command_t)); memcpy(cmd->data, all_data->data, sizeof(all_data->data)); k_fifo_put(&fifo_storage_write_data, cmd); } static void storage_thread(void *arg1, void *arg2, void *arg3) { ARG_UNUSED(arg1); ARG_UNUSED(arg2); ARG_UNUSED(arg3); while (1) { struct my_command_t *buf = k_fifo_get(&fifo_storage_write_data, K_FOREVER); if (buf == NULL) { continue; } lfs_write(buf->data); /* own function */ k_free(buf); LED_Yellow(LED_OFF); /* own function */ } } K_THREAD_DEFINE(storage_thread_id, STACKSIZE, storage_thread, NULL, NULL, NULL, K_LOWEST_THREAD_PRIO, 0, 100);
Generally, I'm asking about what I can do to make the LittleFS calls non-blocking (if even possible). Specifically, I'm also asking about whether I've set up the threads correctly (I think there's a lot more besides the priority that affects how the behavior should be, but I'm relatively new to multithreading). Let me know if you need more details about my setup; thank you very much in advance!
Here are some other possibly important details:
- My development environment is:
- VS Code
- NCS 2.5.0
- Windows 10
- The spun thread has priority K_LOWEST_THREAD_PRIO, which is equal to 15.
- The main thread has priority 0
----------------------------------------
EDIT1: Tried out spinning one more thread ('printing thread' - repeatedly prints 1 character and calls k_msleep(1)). The LFS write seems to also prevent this new 'printing thread' from printing. Adjusting the thread's priority doesn't seem to help; I tested with 'K_LOWEST_THREAD_PRIO - 1' and with 0.