Skip to content

Commit f0ae279

Browse files
maidang-xingshiliu-yang
authored andcommitted
Support esp32s3 create thread on psram
1 parent 9a499a5 commit f0ae279

3 files changed

Lines changed: 129 additions & 75 deletions

File tree

tuya_open_sdk/main/main.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@
44

55
#include "nvs_flash.h"
66

7+
#if CONFIG_IDF_TARGET_ESP32S3
8+
#include "freertos/FreeRTOS.h"
9+
#include "freertos/task.h"
10+
11+
extern void tkl_flash_agent_init(void);
12+
extern void tkl_flash_agent_process(void);
13+
14+
static void flash_agent_task(void *arg)
15+
{
16+
while (1) {
17+
tkl_flash_agent_process();
18+
}
19+
}
20+
#endif
21+
722
extern void tuya_app_main(void);
823
void app_main(void)
924
{
@@ -15,5 +30,10 @@ void app_main(void)
1530
}
1631
ESP_ERROR_CHECK(ret);
1732

33+
#if CONFIG_IDF_TARGET_ESP32S3
34+
tkl_flash_agent_init();
35+
xTaskCreate(flash_agent_task, "flash_agent", 2048, NULL, configMAX_PRIORITIES - 1, NULL);
36+
#endif
37+
1838
tuya_app_main();
1939
}

tuya_open_sdk/tuyaos_adapter/src/drivers/tkl_flash.c

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,82 @@ typedef struct {
4141
char *uuid;
4242
char *authkey;
4343
} tuya_iot_license_t;
44+
45+
#if CONFIG_IDF_TARGET_ESP32S3
46+
#include "freertos/FreeRTOS.h"
47+
#include "freertos/task.h"
48+
#include "freertos/queue.h"
49+
50+
typedef enum {
51+
FLASH_OP_READ = 0,
52+
FLASH_OP_WRITE,
53+
FLASH_OP_ERASE,
54+
} FLASH_OP_TYPE_E;
55+
56+
typedef struct {
57+
FLASH_OP_TYPE_E op;
58+
uint32_t addr;
59+
union {
60+
const uint8_t *src;
61+
uint8_t *dst;
62+
};
63+
uint32_t size;
64+
TaskHandle_t caller;
65+
OPERATE_RET result;
66+
} FLASH_REQ_T;
67+
68+
static QueueHandle_t s_flash_queue = NULL;
69+
70+
static OPERATE_RET __flash_dispatch(FLASH_REQ_T *req)
71+
{
72+
req->caller = xTaskGetCurrentTaskHandle();
73+
FLASH_REQ_T *preq = req;
74+
xQueueSend(s_flash_queue, &preq, portMAX_DELAY);
75+
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
76+
return req->result;
77+
}
78+
79+
void tkl_flash_agent_init(void)
80+
{
81+
s_flash_queue = xQueueCreate(8, sizeof(FLASH_REQ_T *));
82+
}
83+
84+
void tkl_flash_agent_process(void)
85+
{
86+
FLASH_REQ_T *req = NULL;
87+
88+
if (xQueueReceive(s_flash_queue, &req, portMAX_DELAY) != pdTRUE || req == NULL) {
89+
return;
90+
}
91+
92+
const esp_partition_t *partition = GET_TUYA_DATA_PARTITION();
93+
if (partition == NULL) {
94+
req->result = OPRT_COM_ERROR;
95+
xTaskNotifyGive(req->caller);
96+
return;
97+
}
98+
99+
switch (req->op) {
100+
case FLASH_OP_READ:
101+
req->result = (esp_partition_read(partition, req->addr, req->dst, req->size) == ESP_OK)
102+
? OPRT_OK : OPRT_COM_ERROR;
103+
break;
104+
case FLASH_OP_WRITE:
105+
req->result = (esp_partition_write(partition, req->addr, req->src, req->size) == ESP_OK)
106+
? OPRT_OK : OPRT_COM_ERROR;
107+
break;
108+
case FLASH_OP_ERASE:
109+
req->result = (esp_partition_erase_range(partition, req->addr, req->size) == ESP_OK)
110+
? OPRT_OK : OPRT_COM_ERROR;
111+
break;
112+
default:
113+
req->result = OPRT_INVALID_PARM;
114+
break;
115+
}
116+
117+
xTaskNotifyGive(req->caller);
118+
}
119+
#endif
44120
// --- END: user defines and implements ---
45121

46122
/**
@@ -57,13 +133,18 @@ typedef struct {
57133
OPERATE_RET tkl_flash_read(uint32_t addr, uint8_t *dst, uint32_t size)
58134
{
59135
// --- BEGIN: user implements ---
60-
const esp_partition_t *partition;
61-
62136
if (NULL == dst) {
63137
return OPRT_INVALID_PARM;
64138
}
65139

66-
partition = GET_TUYA_DATA_PARTITION();
140+
#if CONFIG_IDF_TARGET_ESP32S3
141+
if (s_flash_queue != NULL) {
142+
FLASH_REQ_T req = { .op = FLASH_OP_READ, .addr = addr, .dst = dst, .size = size };
143+
return __flash_dispatch(&req);
144+
}
145+
#endif
146+
147+
const esp_partition_t *partition = GET_TUYA_DATA_PARTITION();
67148
if (NULL == partition) {
68149
return OPRT_COM_ERROR;
69150
}
@@ -90,13 +171,18 @@ OPERATE_RET tkl_flash_read(uint32_t addr, uint8_t *dst, uint32_t size)
90171
OPERATE_RET tkl_flash_write(uint32_t addr, const uint8_t *src, uint32_t size)
91172
{
92173
// --- BEGIN: user implements ---
93-
const esp_partition_t *partition;
94-
95174
if (NULL == src) {
96175
return OPRT_INVALID_PARM;
97176
}
98-
99-
partition = GET_TUYA_DATA_PARTITION();
177+
178+
#if CONFIG_IDF_TARGET_ESP32S3
179+
if (s_flash_queue != NULL) {
180+
FLASH_REQ_T req = { .op = FLASH_OP_WRITE, .addr = addr, .src = src, .size = size };
181+
return __flash_dispatch(&req);
182+
}
183+
#endif
184+
185+
const esp_partition_t *partition = GET_TUYA_DATA_PARTITION();
100186
if (NULL == partition) {
101187
return OPRT_COM_ERROR;
102188
}
@@ -122,9 +208,14 @@ OPERATE_RET tkl_flash_write(uint32_t addr, const uint8_t *src, uint32_t size)
122208
OPERATE_RET tkl_flash_erase(uint32_t addr, uint32_t size)
123209
{
124210
// --- BEGIN: user implements ---
125-
const esp_partition_t *partition;
211+
#if CONFIG_IDF_TARGET_ESP32S3
212+
if (s_flash_queue != NULL) {
213+
FLASH_REQ_T req = { .op = FLASH_OP_ERASE, .addr = addr, .size = size };
214+
return __flash_dispatch(&req);
215+
}
216+
#endif
126217

127-
partition = GET_TUYA_DATA_PARTITION();
218+
const esp_partition_t *partition = GET_TUYA_DATA_PARTITION();
128219
if (NULL == partition) {
129220
return OPRT_COM_ERROR;
130221
}
@@ -133,7 +224,6 @@ OPERATE_RET tkl_flash_erase(uint32_t addr, uint32_t size)
133224
return OPRT_COM_ERROR;
134225
}
135226

136-
137227
return OPRT_OK;
138228
// --- END: user implements ---
139229
}

tuya_open_sdk/tuyaos_adapter/src/system/tkl_thread.c

Lines changed: 9 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@ typedef struct {
2929

3030
typedef struct {
3131
TaskHandle_t task;
32-
} THREAD_HANDLE_T;
33-
34-
typedef struct {
35-
THREAD_FUNC_T entry;
36-
void *arg;
37-
} THREAD_ENTRY_CTX_T;
32+
} RAM_THREAD_HANDLE_T;
3833

3934
#define PSRAM_THREAD_MAGIC 0x5053524DU
4035

@@ -71,27 +66,9 @@ static TaskHandle_t __tkl_thread_get_task_handle(const TKL_THREAD_HANDLE thread)
7166
return psram_handle->task;
7267
}
7368

74-
return ((THREAD_HANDLE_T *)thread)->task;
69+
return ((RAM_THREAD_HANDLE_T *)thread)->task;
7570
}
7671

77-
static void __tkl_thread_entry(void *arg)
78-
{
79-
THREAD_ENTRY_CTX_T *ctx = (THREAD_ENTRY_CTX_T *)arg;
80-
THREAD_FUNC_T entry = NULL;
81-
void *entry_arg = NULL;
82-
83-
if (ctx != NULL) {
84-
entry = ctx->entry;
85-
entry_arg = ctx->arg;
86-
free(ctx);
87-
}
88-
89-
if (entry != NULL) {
90-
entry(entry_arg);
91-
}
92-
93-
vTaskDelete(NULL);
94-
}
9572
// --- END: user defines and implements ---
9673

9774
/**
@@ -116,31 +93,20 @@ OPERATE_RET tkl_thread_create(TKL_THREAD_HANDLE* thread,
11693
void* const arg)
11794
{
11895
// --- BEGIN: user implements ---
119-
THREAD_HANDLE_T *handle = NULL;
120-
THREAD_ENTRY_CTX_T *entry_ctx = NULL;
96+
RAM_THREAD_HANDLE_T *handle = NULL;
12197

12298
if ((thread == NULL) || (func == NULL)) {
12399
return OPRT_INVALID_PARM;
124100
}
125101

126-
handle = (THREAD_HANDLE_T *)malloc(sizeof(THREAD_HANDLE_T));
102+
handle = (RAM_THREAD_HANDLE_T *)malloc(sizeof(RAM_THREAD_HANDLE_T));
127103
if (handle == NULL) {
128104
return OPRT_MALLOC_FAILED;
129105
}
130106

131-
entry_ctx = (THREAD_ENTRY_CTX_T *)malloc(sizeof(THREAD_ENTRY_CTX_T));
132-
if (entry_ctx == NULL) {
133-
free(handle);
134-
return OPRT_MALLOC_FAILED;
135-
}
136-
137-
entry_ctx->entry = func;
138-
entry_ctx->arg = arg;
139-
140107
stack_size = __tkl_thread_adjust_stack_size(stack_size);
141108

142-
if (xTaskCreate(__tkl_thread_entry, name, stack_size / sizeof(portSTACK_TYPE), (void *const)entry_ctx, priority, &handle->task) != pdPASS) {
143-
free(entry_ctx);
109+
if (xTaskCreate(func, name, stack_size / sizeof(portSTACK_TYPE), arg, priority, &handle->task) != pdPASS) {
144110
free(handle);
145111
return OPRT_OS_ADAPTER_THRD_CREAT_FAILED;
146112
}
@@ -163,7 +129,7 @@ OPERATE_RET tkl_thread_release(const TKL_THREAD_HANDLE thread)
163129
{
164130
// --- BEGIN: user implements ---
165131
PSRAM_THREAD_HANDLE_T *psram_handle = __tkl_thread_get_psram_handle(thread);
166-
THREAD_HANDLE_T *handle = (THREAD_HANDLE_T *)thread;
132+
RAM_THREAD_HANDLE_T *handle = (RAM_THREAD_HANDLE_T *)thread;
167133

168134
if (psram_handle != NULL) {
169135
vTaskDelete(psram_handle->task);
@@ -226,13 +192,13 @@ OPERATE_RET tkl_thread_get_watermark(const TKL_THREAD_HANDLE thread, uint32_t* w
226192
OPERATE_RET tkl_thread_get_id(TKL_THREAD_HANDLE *thread)
227193
{
228194
// --- BEGIN: user implements ---
229-
THREAD_HANDLE_T *handle = NULL;
195+
RAM_THREAD_HANDLE_T *handle = NULL;
230196

231197
if (thread == NULL) {
232198
return OPRT_INVALID_PARM;
233199
}
234200

235-
handle = (THREAD_HANDLE_T *)malloc(sizeof(THREAD_HANDLE_T));
201+
handle = (RAM_THREAD_HANDLE_T *)malloc(sizeof(RAM_THREAD_HANDLE_T));
236202
if (handle == NULL) {
237203
return OPRT_MALLOC_FAILED;
238204
}
@@ -400,7 +366,6 @@ OPERATE_RET tkl_thread_create_in_psram(TKL_THREAD_HANDLE* thread,
400366
uint32_t adjusted_stack_size = __tkl_thread_adjust_stack_size(stack_size);
401367
uint32_t stack_depth = adjusted_stack_size / sizeof(StackType_t);
402368
PSRAM_THREAD_HANDLE_T *handle = NULL;
403-
THREAD_ENTRY_CTX_T *entry_ctx = NULL;
404369

405370
if (stack_depth == 0) {
406371
return OPRT_INVALID_PARM;
@@ -411,48 +376,27 @@ OPERATE_RET tkl_thread_create_in_psram(TKL_THREAD_HANDLE* thread,
411376
return OPRT_MALLOC_FAILED;
412377
}
413378

414-
entry_ctx = (THREAD_ENTRY_CTX_T *)malloc(sizeof(THREAD_ENTRY_CTX_T));
415-
if (entry_ctx == NULL) {
416-
free(handle);
417-
return OPRT_MALLOC_FAILED;
418-
}
419-
420-
entry_ctx->entry = func;
421-
entry_ctx->arg = arg;
422-
423379
handle->stack_buffer = (StackType_t *)heap_caps_malloc(stack_depth * sizeof(StackType_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
424380
if (handle->stack_buffer == NULL) {
425-
free(entry_ctx);
426381
free(handle);
427382
return OPRT_MALLOC_FAILED;
428383
}
429384

430385
handle->task_buffer = (StaticTask_t *)malloc(sizeof(StaticTask_t));
431386
if (handle->task_buffer == NULL) {
432387
free(handle->stack_buffer);
433-
free(entry_ctx);
434388
free(handle);
435389
return OPRT_MALLOC_FAILED;
436390
}
437391

438-
handle->task = xTaskCreateStatic(__tkl_thread_entry, name, stack_depth, (void *const)entry_ctx, priority, handle->stack_buffer,
392+
handle->task = xTaskCreateStatic(func, name, stack_depth, arg, priority, handle->stack_buffer,
439393
handle->task_buffer);
440394
if (handle->task == NULL) {
441395
free(handle->task_buffer);
442396
free(handle->stack_buffer);
443-
free(entry_ctx);
444397
free(handle);
445398
return OPRT_OS_ADAPTER_THRD_CREAT_FAILED;
446399
}
447-
printf("create in psram success\n");
448-
printf("handle->task: %p\n", handle->task);
449-
printf("handle->stack_buffer: %p\n", handle->stack_buffer);
450-
printf("handle->task_buffer: %p\n", handle->task_buffer);
451-
printf("entry_ctx: %p\n", entry_ctx);
452-
printf("handle: %p\n", handle);
453-
printf("name: %s\n", name);
454-
printf("stack_size: %lu\n", stack_size);
455-
printf("priority: %lu\n", priority);
456400

457401
handle->magic = PSRAM_THREAD_MAGIC;
458402
*thread = (TKL_THREAD_HANDLE)handle;

0 commit comments

Comments
 (0)