spiram_task.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Helpers for creating FreeRTOS tasks.
  3. *
  4. * On ESP32 with SPIRAM, task stacks must remain in internal RAM because
  5. * SPI flash operations disable the cache, making SPIRAM inaccessible.
  6. * Placing stacks there causes esp_task_stack_is_sane_cache_disabled()
  7. * asserts. These wrappers keep the call-site API stable regardless.
  8. *
  9. * Usage:
  10. * Permanent tasks (never deleted):
  11. * task_create_spiram(fn, "name", depth, param, prio, &handle, NULL);
  12. *
  13. * Transient tasks (deleted later):
  14. * spiram_task_mem_t mem;
  15. * task_create_spiram(fn, "name", depth, param, prio, &handle, &mem);
  16. * // ... later, after the task has exited:
  17. * task_free_spiram(&mem);
  18. */
  19. #pragma once
  20. #include "freertos/FreeRTOS.h"
  21. #include "freertos/task.h"
  22. typedef struct {
  23. void *stack;
  24. void *tcb;
  25. } spiram_task_mem_t;
  26. static inline BaseType_t task_create_spiram(TaskFunction_t fn, const char *name,
  27. uint32_t depth, void *param,
  28. UBaseType_t prio,
  29. TaskHandle_t *handle,
  30. spiram_task_mem_t *mem) {
  31. if (mem) {
  32. mem->stack = NULL;
  33. mem->tcb = NULL;
  34. }
  35. return xTaskCreate(fn, name, depth, param, prio, handle);
  36. }
  37. static inline BaseType_t
  38. task_create_pinned_spiram(TaskFunction_t fn, const char *name, uint32_t depth,
  39. void *param, UBaseType_t prio, TaskHandle_t *handle,
  40. BaseType_t core, spiram_task_mem_t *mem) {
  41. if (mem) {
  42. mem->stack = NULL;
  43. mem->tcb = NULL;
  44. }
  45. return xTaskCreatePinnedToCore(fn, name, depth, param, prio, handle, core);
  46. }
  47. static inline void task_free_spiram(spiram_task_mem_t *mem) {
  48. (void)mem;
  49. }