【FireBeetle 2 ESP32 C6开发板】-2 freertos测试
<div class='showpostmsg'> 本帖最后由 damiaa 于 2024-5-13 10:41 编辑<div><strong><span style="font-size:18px;"> 【FireBeetle 2 ESP32 C6开发板】-2 freertos测试</span></strong></div>
<div> </div>
<div> </div>
<div><span style="font-size:16px;"><strong>一、在setup()里面初始化串口,注意esp c6串口默认是IO16 IO 17</strong></span></div>
<div>Serial.begin(115200);</div>
<div></div>
<div>然后创建任务,这里一个闪灯,一个读adc</div>
<div>
<pre>
<code class="language-cpp">uint32_t blink_delay = 300; // Delay between changing state on LED pin
xTaskCreate(
TaskBlink
,"Task Blink" // A name just for humans
,2048 // The stack size can be checked by calling `uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);`
,(void*) &blink_delay // Task parameter which can modify the task behavior. This must be passed as pointer to void.
,2// Priority
,NULL // Task handle is not used here - simply pass NULL
);
// This variant of task creation can also specify on which core it will be run (only relevant for multi-core ESPs)
xTaskCreatePinnedToCore(
TaskAnalogRead
,"Analog Read"
,2048// Stack size
,NULL// When no parameter is used, simply pass NULL
,1// Priority
,&analog_read_task_handle // With task handle we will be able to manipulate with this task.
,ARDUINO_RUNNING_CORE // Core on which the task will run
);</code></pre>
</div>
<p> </p>
<p><strong><span style="font-size:16px;">二、任务函数实现</span></strong></p>
<pre>
<code class="language-cpp">void TaskBlink(void *pvParameters){// This is a task.
uint32_t blink_delay = *((uint32_t*)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.
*/
// 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)
// arduino-esp32 has FreeRTOS configured to have a tick-rate of 1000Hz and portTICK_PERIOD_MS
// refers to how many milliseconds the period between each ticks is, ie. 1ms.
delay(blink_delay);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(blink_delay);
}
}
void TaskAnalogRead(void *pvParameters){// This is a task.
(void) pvParameters;
// Check if the given analog pin is usable - if not - delete this task
if(digitalPinToAnalogChannel(ANALOG_INPUT_PIN) == -1){
Serial.printf("TaskAnalogRead cannot work because the given pin %d cannot be used for ADC - the task will delete itself.\n", ANALOG_INPUT_PIN);
analog_read_task_handle = NULL; // Prevent calling vTaskDelete on non-existing task
vTaskDelete(NULL); // Delete this task
}
analogReadResolution(12);
analogSetAttenuation(ADC_6db);
/*
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.
*/
for (;;){
// read the input on analog pin:
int sensorValue = analogRead(ANALOG_INPUT_PIN);
// print out the value you read:
Serial.println(sensorValue);
delay(100); // 100ms delay
}
}
</code></pre>
<p> </p>
<p><span style="font-size:16px;"><strong>四、填写loop()代码</strong></span><br />
</p>
<pre>
<code class="language-cpp">void loop(){
if(analog_read_task_handle != NULL){ // Make sure that the task actually exists
delay(10000);
vTaskDelete(analog_read_task_handle); // Delete task
analog_read_task_handle = NULL; // prevent calling vTaskDelete on non-existing task
}
}</code></pre>
<ol>
<li> </li>
</ol>
<div><strong><span style="font-size:16px;">五、总体代码</span></strong></div>
<div>
<pre>
<code class="language-cpp">#if CONFIG_FREERTOS_UNICORE
#define ARDUINO_RUNNING_CORE 0
#else
#define ARDUINO_RUNNING_CORE 1
#endif
#define ANALOG_INPUT_PIN 2
#ifndef LED_BUILTIN
#define LED_BUILTIN 15 // Specify the on which is your LED
#endif
// Define two tasks for Blink & AnalogRead.
void TaskBlink( void *pvParameters );
void TaskAnalogRead( void *pvParameters );
TaskHandle_t analog_read_task_handle; // You can (don't have to) use this to be able to manipulate a task from somewhere else.
// The setup function runs once when you press reset or power on the board.
void setup() {
// Initialize serial communication at 115200 bits per second:
Serial.begin(115200);
// Set up two tasks to run independently.
uint32_t blink_delay = 300; // Delay between changing state on LED pin
xTaskCreate(
TaskBlink
,"Task Blink" // A name just for humans
,2048 // The stack size can be checked by calling `uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);`
,(void*) &blink_delay // Task parameter which can modify the task behavior. This must be passed as pointer to void.
,2// Priority
,NULL // Task handle is not used here - simply pass NULL
);
// This variant of task creation can also specify on which core it will be run (only relevant for multi-core ESPs)
xTaskCreatePinnedToCore(
TaskAnalogRead
,"Analog Read"
,2048// Stack size
,NULL// When no parameter is used, simply pass NULL
,1// Priority
,&analog_read_task_handle // With task handle we will be able to manipulate with this task.
,ARDUINO_RUNNING_CORE // Core on which the task will run
);
Serial.printf("Basic Multi Threading Arduino Example\n");
// Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
}
void loop(){
if(analog_read_task_handle != NULL){ // Make sure that the task actually exists
delay(10000);
vTaskDelete(analog_read_task_handle); // Delete task
analog_read_task_handle = NULL; // prevent calling vTaskDelete on non-existing task
}
}
/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/
void TaskBlink(void *pvParameters){// This is a task.
uint32_t blink_delay = *((uint32_t*)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.
*/
// 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)
// arduino-esp32 has FreeRTOS configured to have a tick-rate of 1000Hz and portTICK_PERIOD_MS
// refers to how many milliseconds the period between each ticks is, ie. 1ms.
delay(blink_delay);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(blink_delay);
}
}
void TaskAnalogRead(void *pvParameters){// This is a task.
(void) pvParameters;
// Check if the given analog pin is usable - if not - delete this task
if(digitalPinToAnalogChannel(ANALOG_INPUT_PIN) == -1){
Serial.printf("TaskAnalogRead cannot work because the given pin %d cannot be used for ADC - the task will delete itself.\n", ANALOG_INPUT_PIN);
analog_read_task_handle = NULL; // Prevent calling vTaskDelete on non-existing task
vTaskDelete(NULL); // Delete this task
}
analogReadResolution(12);
analogSetAttenuation(ADC_6db);
/*
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.
*/
for (;;){
// read the input on analog pin:
int sensorValue = analogRead(ANALOG_INPUT_PIN);
// print out the value you read:
Serial.println(sensorValue);
delay(100); // 100ms delay
}
}</code></pre>
<p> </p>
</div>
<div>六、下载后灯闪了,串口也应该可以看到数据,但没焊接排针,后面补齐。</div>
<div> </div>
<div>谢谢。</div>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页:
[1]