请教,arduino如何开发ESP32上面的双核?-Arduino中文社区 - Powered by Discuz! Archiver

kylongmu 发表于 2018-11-29 16:19

请教,arduino如何开发ESP32上面的双核?

本帖最后由 kylongmu 于 2018-12-4 22:10 编辑

ESP32具有两个核心,arduino开发的程序如何用到它们呢?由于只有一个loop入口,不知道如何放多核代码。

D:\Arduino\hardware\arduino-esp32\esp32\tools\partitions
从分区文件看有app0、app1两个应用程序分段,但是如何开发两个app并把他们写入到各自的flash,又是如何共享交互数据呢?//追加-----------------------------------------------
现在搞清楚了app0、app1是用来做OTA升级用的,不是写两个core的app。


kylongmu 发表于 2018-12-21 09:08

#ifndef LED_BUILTIN
#define LED_BUILTIN 33
#endif

// define two tasks for Blink & AnalogRead
void TaskBlink( void *pvParameters );
void TaskAnalogReadA3( void *pvParameters );

// the setup function runs once when you press reset or power the board
void setup() {

// initialize serial communication at 115200 bits per second:
Serial.begin(115200);

// Now set up two tasks to run independently.
xTaskCreatePinnedToCore(
    TaskBlink
    ,"TaskBlink"   // A name just for humans
    ,1024// This stack size can be checked & adjusted by reading the Stack Highwater
    ,NULL
    ,2// Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
    ,NULL
    ,0);

xTaskCreatePinnedToCore(
    TaskAnalogReadA3
    ,"AnalogReadA3"
    ,1024// Stack size
    ,NULL
    ,1// Priority
    ,NULL
    ,1);

// Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
}

void loop()
{
// Empty. Things are done in Tasks.
}

/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/

void TaskBlink(void *pvParameters)// This is a task.
{
(void) pvParameters;

/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
   
If you want to know what pin the on-board LED is connected to on your ESP32 model, check
the Technical Specs of your board.
*/
    String taskMessage = "Task running on core ";
    taskMessage = taskMessage + xPortGetCoreID();


// initialize digital LED_BUILTIN on pin 13 as an output.
pinMode(LED_BUILTIN, OUTPUT);

for (;;) // A Task shall never return or exit.
{
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    vTaskDelay(500);// one tick delay (15ms) in between reads for stability
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    vTaskDelay(500);// one tick delay (15ms) in between reads for stability
    Serial.println(taskMessage);
}
}

void TaskAnalogReadA3(void *pvParameters)// This is a task.
{
(void) pvParameters;

/*
AnalogReadSerial
Reads an analog input on pin A3, prints the result to the serial monitor.
Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
Attach the center pin of a potentiometer to pin A3, and the outside pins to +5V and ground.

This example code is in the public domain.
*/
    String taskMessage = "Task running on core ";
    taskMessage = taskMessage + xPortGetCoreID();

for (;;)
{
    // read the input on analog pin A3:
    int sensorValueA3 = analogRead(A3);
    // print out the value you read:
    Serial.println(sensorValueA3);
    Serial.println(taskMessage);
    vTaskDelay(1000);// one tick delay (15ms) in between reads for stability
}
}
自测的代码,实现了双核运行。

奈何col 发表于 2018-11-29 17:09

arduino应该是不行的

kylongmu 发表于 2018-11-29 21:02

这么看来arduino的发展落后了,esp32用单核版的就可以了,双核版纯浪费啊。
以后需要有loop_01()、loop_02()来做多核心的支持。

kylongmu 发表于 2018-12-4 21:57

本帖最后由 kylongmu 于 2018-12-4 22:07 编辑

找到了一篇说明:
https://techtutorialsx.com/2017/05/09/esp32-running-code-on-a-specific-core/
看意思应该是只需要启动FreeRtOS的多TASK就能利用多核。在tools\sdk\include\freertos\freertos\task.h中有如下函数:
/*-----------------------------------------------------------
* TASK CREATION API
*----------------------------------------------------------*/

/**
* Create a new task with a specified affinity.
*
* This function is similar to xTaskCreate, but allows setting task affinity
* in SMP system.
*
* @param pvTaskCode Pointer to the task entry function.Tasks
* must be implemented to never return (i.e. continuous loop).
*
* @param pcName A descriptive name for the task.This is mainly used to
* facilitate debugging.Max length defined by configMAX_TASK_NAME_LEN - default
* is 16.
*
* @param usStackDepth The size of the task stack specified as the number of
* variables the stack can hold - not the number of bytes.For example, if
* the stack is 16 bits wide and usStackDepth is defined as 100, 200 bytes
* will be allocated for stack storage.
*
* @param pvParameters Pointer that will be used as the parameter for the task
* being created.
*
* @param uxPriority The priority at which the task should run.Systems that
* include MPU support can optionally create tasks in a privileged (system)
* mode by setting bit portPRIVILEGE_BIT of the priority parameter.For
* example, to create a privileged task at priority 2 the uxPriority parameter
* should be set to ( 2 | portPRIVILEGE_BIT ).
*
* @param pvCreatedTask Used to pass back a handle by which the created task
* can be referenced.
*
* @param xCoreID If the value is tskNO_AFFINITY, the created task is not
* pinned to any CPU, and the scheduler can run it on any core available.
* Other values indicate the index number of the CPU which the task should
* be pinned to. Specifying values larger than (portNUM_PROCESSORS - 1) will
* cause the function to fail.
*
* @return pdPASS if the task was successfully created and added to a ready
* list, otherwise an error code defined in the file projdefs.h
*
* \ingroup Tasks
*/
#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
      BaseType_t xTaskCreatePinnedToCore(      TaskFunction_t pvTaskCode,
                                                                              const char * const pcName,
                                                                              const uint32_t usStackDepth,
                                                                              void * const pvParameters,
                                                                              UBaseType_t uxPriority,
                                                                              TaskHandle_t * const pvCreatedTask,
                                                                              const BaseType_t xCoreID);

#endif

hebo230 发表于 2018-12-18 11:09

两个核创建任务的函数是不同的用对应函数使用对应的核
页: [1]
查看完整版本: 请教,arduino如何开发ESP32上面的双核?