ESPHome
2026.3.0
Loading...
Searching...
No Matches
esphome
core
static_task.cpp
Go to the documentation of this file.
1
#include "
esphome/core/static_task.h
"
2
3
#ifdef USE_ESP32
4
5
#include "
esphome/core/helpers.h
"
6
7
namespace
esphome
{
8
9
bool
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
18
RAMAllocator<StackType_t>
allocator(this->
use_psram_
?
RAMAllocator<StackType_t>::ALLOC_EXTERNAL
19
:
RAMAllocator<StackType_t>::ALLOC_INTERNAL
);
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;
27
RAMAllocator<StackType_t>
allocator(use_psram ?
RAMAllocator<StackType_t>::ALLOC_EXTERNAL
28
:
RAMAllocator<StackType_t>::ALLOC_INTERNAL
);
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
43
void
StaticTask::destroy
() {
44
if
(this->
handle_
!=
nullptr
) {
45
TaskHandle_t handle = this->
handle_
;
46
this->
handle_
=
nullptr
;
47
vTaskDelete(handle);
48
}
49
}
50
51
void
StaticTask::deallocate
() {
52
this->
destroy
();
53
if
(this->
stack_buffer_
!=
nullptr
) {
54
RAMAllocator<StackType_t>
allocator(this->
use_psram_
?
RAMAllocator<StackType_t>::ALLOC_EXTERNAL
55
:
RAMAllocator<StackType_t>::ALLOC_INTERNAL
);
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
esphome::RAMAllocator
An STL allocator that uses SPI or internal RAM.
Definition
helpers.h:1899
esphome::RAMAllocator::deallocate
void deallocate(T *p, size_t n)
Definition
helpers.h:1954
esphome::RAMAllocator::allocate
T * allocate(size_t n)
Definition
helpers.h:1916
esphome::StaticTask::create
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.
Definition
static_task.cpp:9
esphome::StaticTask::use_psram_
bool use_psram_
Definition
static_task.h:45
esphome::StaticTask::deallocate
void deallocate()
Delete the task (if running) and free the stack buffer.
Definition
static_task.cpp:51
esphome::StaticTask::handle_
TaskHandle_t handle_
Definition
static_task.h:41
esphome::StaticTask::destroy
void destroy()
Delete the task but keep the stack buffer allocated for reuse by a subsequent create() call.
Definition
static_task.cpp:43
esphome::StaticTask::stack_size_
uint32_t stack_size_
Definition
static_task.h:44
esphome::StaticTask::stack_buffer_
StackType_t * stack_buffer_
Definition
static_task.h:43
esphome::StaticTask::tcb_
StaticTask_t tcb_
Definition
static_task.h:42
priority
uint8_t priority
Definition
e131_packet.cpp:12
helpers.h
esphome
Providing packet encoding functions for exchanging data with a remote host.
Definition
a01nyub.cpp:7
uint32_t
static void uint32_t
Definition
crash_handler.cpp:141
static_task.h
Generated by
1.12.0