ESPHome 2026.3.0
Loading...
Searching...
No Matches
static_task.cpp
Go to the documentation of this file.
2
3#ifdef USE_ESP32
4
6
7namespace esphome {
8
9bool StaticTask::create(TaskFunction_t fn, const char *name, uint32_t stack_size, void *param, UBaseType_t priority,
10 bool use_psram) {
11 if (this->handle_ != nullptr) {
12 // Task is already created; must call destroy() first
13 return false;
14 }
15
16 if (this->stack_buffer_ != nullptr && (stack_size > this->stack_size_ || use_psram != this->use_psram_)) {
17 // Existing buffer is too small or wrong memory type; deallocate to reallocate below
20 allocator.deallocate(this->stack_buffer_, this->stack_size_);
21 this->stack_buffer_ = nullptr;
22 }
23
24 if (this->stack_buffer_ == nullptr) {
25 this->stack_size_ = stack_size;
26 this->use_psram_ = use_psram;
29 this->stack_buffer_ = allocator.allocate(stack_size);
30 }
31 if (this->stack_buffer_ == nullptr) {
32 return false;
33 }
34
35 this->handle_ = xTaskCreateStatic(fn, name, this->stack_size_, param, priority, this->stack_buffer_, &this->tcb_);
36 if (this->handle_ == nullptr) {
37 this->deallocate();
38 return false;
39 }
40 return true;
41}
42
44 if (this->handle_ != nullptr) {
45 TaskHandle_t handle = this->handle_;
46 this->handle_ = nullptr;
47 vTaskDelete(handle);
48 }
49}
50
52 this->destroy();
53 if (this->stack_buffer_ != nullptr) {
56 allocator.deallocate(this->stack_buffer_, this->stack_size_);
57 this->stack_buffer_ = nullptr;
58 this->stack_size_ = 0;
59 }
60}
61
62} // namespace esphome
63
64#endif // USE_ESP32
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:1899
void deallocate(T *p, size_t n)
Definition helpers.h:1954
T * allocate(size_t n)
Definition helpers.h:1916
bool create(TaskFunction_t fn, const char *name, uint32_t stack_size, void *param, UBaseType_t priority, bool use_psram)
Allocate stack and create task.
void deallocate()
Delete the task (if running) and free the stack buffer.
TaskHandle_t handle_
Definition static_task.h:41
void destroy()
Delete the task but keep the stack buffer allocated for reuse by a subsequent create() call.
StackType_t * stack_buffer_
Definition static_task.h:43
StaticTask_t tcb_
Definition static_task.h:42
uint8_t priority
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
static void uint32_t