From 80cb2cf9c628df5e8d4ab62a996cbcd39d3e94c3 Mon Sep 17 00:00:00 2001 From: Jukka Laitinen Date: Thu, 14 Aug 2025 08:23:06 +0300 Subject: [PATCH] arch/xtensa/esp32,esp32s3: Start the "spiflash_op" thread with correct affinity Set the affinity of the task before activating it. There is no parameter or other interface in "kthread_create" to set the affinity mask, like in "pthread_create". Signed-off-by: Jukka Laitinen --- arch/xtensa/src/esp32/esp32_spiflash.c | 52 +++++++++++++--------- arch/xtensa/src/esp32s3/esp32s3_spiflash.c | 52 +++++++++++++--------- 2 files changed, 64 insertions(+), 40 deletions(-) diff --git a/arch/xtensa/src/esp32/esp32_spiflash.c b/arch/xtensa/src/esp32/esp32_spiflash.c index f2763202c72..0c4d097450e 100644 --- a/arch/xtensa/src/esp32/esp32_spiflash.c +++ b/arch/xtensa/src/esp32/esp32_spiflash.c @@ -2560,8 +2560,8 @@ static int spi_flash_op_block_task(int argc, char *argv[]) int spiflash_init_spi_flash_op_block_task(int cpu) { - int pid; - int ret = OK; + FAR struct tcb_s *tcb; + int ret; char *argv[2]; char arg1[32]; cpu_set_t cpuset; @@ -2570,29 +2570,41 @@ int spiflash_init_spi_flash_op_block_task(int cpu) argv[0] = arg1; argv[1] = NULL; - pid = kthread_create("spiflash_op", - SCHED_PRIORITY_MAX, - CONFIG_ESP32_SPIFLASH_OP_TASK_STACKSIZE, - spi_flash_op_block_task, - argv); - if (pid > 0) + /* Allocate a TCB for the new task. */ + + tcb = kmm_zalloc(sizeof(struct tcb_s)); + if (!tcb) { - if (cpu < CONFIG_SMP_NCPUS) - { - CPU_ZERO(&cpuset); - CPU_SET(cpu, &cpuset); - ret = nxsched_set_affinity(pid, sizeof(cpuset), &cpuset); - if (ret < 0) - { - return ret; - } - } + serr("ERROR: Failed to allocate TCB\n"); + return -ENOMEM; } - else + + /* Setup the task type */ + + tcb->flags = TCB_FLAG_TTYPE_KERNEL | TCB_FLAG_FREE_TCB; + + /* Initialize the task */ + + ret = nxtask_init((FAR struct task_tcb_s *)tcb, "spiflash_op", + SCHED_PRIORITY_MAX, + NULL, CONFIG_ESP32_SPIFLASH_OP_TASK_STACKSIZE, + spi_flash_op_block_task, argv, environ, NULL); + if (ret < OK) { - return -EPERM; + kmm_free(tcb); + return ret; } + /* Set the affinity */ + + CPU_ZERO(&cpuset); + CPU_SET(cpu, &cpuset); + tcb->affinity = cpuset; + + /* Activate the task */ + + nxtask_activate(tcb); + return ret; } #endif /* CONFIG_SMP */ diff --git a/arch/xtensa/src/esp32s3/esp32s3_spiflash.c b/arch/xtensa/src/esp32s3/esp32s3_spiflash.c index 7440b7b26cf..4eb99822fc7 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_spiflash.c +++ b/arch/xtensa/src/esp32s3/esp32s3_spiflash.c @@ -999,8 +999,8 @@ static int spi_flash_op_block_task(int argc, char *argv[]) static int spiflash_init_spi_flash_op_block_task(int cpu) { - int pid; - int ret = OK; + FAR struct tcb_s *tcb; + int ret; char *argv[2]; char arg1[32]; cpu_set_t cpuset; @@ -1009,29 +1009,41 @@ static int spiflash_init_spi_flash_op_block_task(int cpu) argv[0] = arg1; argv[1] = NULL; - pid = kthread_create("spiflash_op", - SCHED_PRIORITY_MAX, - CONFIG_ESP32S3_SPIFLASH_OP_TASK_STACKSIZE, - spi_flash_op_block_task, - argv); - if (pid > 0) + /* Allocate a TCB for the new task. */ + + tcb = kmm_zalloc(sizeof(struct tcb_s)); + if (!tcb) { - if (cpu < CONFIG_SMP_NCPUS) - { - CPU_ZERO(&cpuset); - CPU_SET(cpu, &cpuset); - ret = nxsched_set_affinity(pid, sizeof(cpuset), &cpuset); - if (ret < 0) - { - return ret; - } - } + serr("ERROR: Failed to allocate TCB\n"); + return -ENOMEM; } - else + + /* Setup the task type */ + + tcb->flags = TCB_FLAG_TTYPE_KERNEL | TCB_FLAG_FREE_TCB; + + /* Initialize the task */ + + ret = nxtask_init((FAR struct task_tcb_s *)tcb, "spiflash_op", + SCHED_PRIORITY_MAX, + NULL, CONFIG_ESP32S3_SPIFLASH_OP_TASK_STACKSIZE, + spi_flash_op_block_task, argv, environ, NULL); + if (ret < OK) { - return -EPERM; + kmm_free(tcb); + return ret; } + /* Set the affinity */ + + CPU_ZERO(&cpuset); + CPU_SET(cpu, &cpuset); + tcb->affinity = cpuset; + + /* Activate the task */ + + nxtask_activate(tcb); + return ret; } #endif /* CONFIG_SMP */