Skip to content

Commit f070041

Browse files
authored
Merge branch 'espressif:release/v3.1.x' into release/v3.1.x
2 parents 3d26d04 + 9aeb1ba commit f070041

File tree

8 files changed

+255
-68
lines changed

8 files changed

+255
-68
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ set(CORE_SRCS
4747
cores/esp32/esp32-hal-uart.c
4848
cores/esp32/esp32-hal-rmt.c
4949
cores/esp32/Esp.cpp
50+
cores/esp32/freertos_stats.cpp
5051
cores/esp32/FunctionalInterrupt.cpp
5152
cores/esp32/HardwareSerial.cpp
5253
cores/esp32/HEXBuilder.cpp

cores/esp32/Arduino.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
199199
#include "Udp.h"
200200
#include "HardwareSerial.h"
201201
#include "Esp.h"
202+
#include "freertos_stats.h"
202203

203204
// Use float-compatible stl abs() and round(), we don't use Arduino macros to avoid issues with the C++ libraries
204205
using std::abs;

cores/esp32/HardwareSerial.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,11 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
313313
// map logical pins to GPIO numbers
314314
rxPin = digitalPinToGPIONumber(rxPin);
315315
txPin = digitalPinToGPIONumber(txPin);
316+
int8_t _rxPin = uart_get_RxPin(_uart_nr);
317+
int8_t _txPin = uart_get_TxPin(_uart_nr);
318+
319+
rxPin = rxPin < 0 ? _rxPin : rxPin;
320+
txPin = txPin < 0 ? _txPin : txPin;
316321

317322
HSERIAL_MUTEX_LOCK();
318323
// First Time or after end() --> set default Pins
@@ -341,14 +346,51 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
341346
case UART_NUM_2:
342347
if (rxPin < 0 && txPin < 0) {
343348
// do not change RX2/TX2 if it has already been set before
349+
#ifdef RX2
344350
rxPin = _rxPin < 0 ? (int8_t)RX2 : _rxPin;
351+
#endif
352+
#ifdef TX2
345353
txPin = _txPin < 0 ? (int8_t)TX2 : _txPin;
354+
#endif
355+
}
356+
break;
357+
#endif
358+
#if SOC_UART_HP_NUM > 3 // may save some flash bytes...
359+
case UART_NUM_3:
360+
if (rxPin < 0 && txPin < 0) {
361+
// do not change RX2/TX2 if it has already been set before
362+
#ifdef RX3
363+
rxPin = _rxPin < 0 ? (int8_t)RX3 : _rxPin;
364+
#endif
365+
#ifdef TX3
366+
txPin = _txPin < 0 ? (int8_t)TX3 : _txPin;
367+
#endif
368+
}
369+
break;
370+
#endif
371+
#if SOC_UART_HP_NUM > 4 // may save some flash bytes...
372+
case UART_NUM_4:
373+
if (rxPin < 0 && txPin < 0) {
374+
// do not change RX2/TX2 if it has already been set before
375+
#ifdef RX4
376+
rxPin = _rxPin < 0 ? (int8_t)RX4 : _rxPin;
377+
#endif
378+
#ifdef TX4
379+
txPin = _txPin < 0 ? (int8_t)TX4 : _txPin;
380+
#endif
346381
}
347382
break;
348383
#endif
349384
}
350385
}
351386

387+
// if no RX/TX pins are defined, it will not start the UART driver
388+
if (rxPin < 0 && txPin < 0) {
389+
log_e("No RX/TX pins defined. Please set RX/TX pins.");
390+
HSERIAL_MUTEX_UNLOCK();
391+
return;
392+
}
393+
352394
// IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified.
353395
// it will detach previous UART attached pins
354396

cores/esp32/HardwareSerial.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,6 @@ typedef enum {
202202
#define RX2 (gpio_num_t)4
203203
#elif CONFIG_IDF_TARGET_ESP32S3
204204
#define RX2 (gpio_num_t)19
205-
#elif CONFIG_IDF_TARGET_ESP32P4
206-
#define RX2 (gpio_num_t)15
207205
#endif
208206
#endif
209207

@@ -212,8 +210,6 @@ typedef enum {
212210
#define TX2 (gpio_num_t)25
213211
#elif CONFIG_IDF_TARGET_ESP32S3
214212
#define TX2 (gpio_num_t)20
215-
#elif CONFIG_IDF_TARGET_ESP32P4
216-
#define TX2 (gpio_num_t)14
217213
#endif
218214
#endif
219215
#endif /* SOC_UART_HP_NUM > 2 */

cores/esp32/freertos_stats.cpp

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "freertos_stats.h"
16+
#include "sdkconfig.h"
17+
18+
#if CONFIG_FREERTOS_USE_TRACE_FACILITY
19+
#include "freertos/FreeRTOS.h"
20+
#include "freertos/task.h"
21+
#include "freertos/portable.h"
22+
#endif /* CONFIG_FREERTOS_USE_TRACE_FACILITY */
23+
24+
void printRunningTasks(Print &printer) {
25+
#if CONFIG_FREERTOS_USE_TRACE_FACILITY
26+
#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
27+
#define FREERTOS_TASK_NUMBER_MAX_NUM 256 // RunTime stats for how many Tasks to be stored
28+
static configRUN_TIME_COUNTER_TYPE ulRunTimeCounters[FREERTOS_TASK_NUMBER_MAX_NUM];
29+
static configRUN_TIME_COUNTER_TYPE ulLastRunTime = 0;
30+
configRUN_TIME_COUNTER_TYPE ulCurrentRunTime = 0, ulTaskRunTime = 0;
31+
#endif
32+
configRUN_TIME_COUNTER_TYPE ulTotalRunTime = 0;
33+
TaskStatus_t *pxTaskStatusArray = NULL;
34+
volatile UBaseType_t uxArraySize = 0, x = 0;
35+
const char *taskStates[] = {"Running", "Ready", "Blocked", "Suspended", "Deleted", "Invalid"};
36+
37+
// Take a snapshot of the number of tasks in case it changes while this function is executing.
38+
uxArraySize = uxTaskGetNumberOfTasks();
39+
40+
// Allocate a TaskStatus_t structure for each task.
41+
pxTaskStatusArray = (TaskStatus_t *)pvPortMalloc(uxArraySize * sizeof(TaskStatus_t));
42+
43+
if (pxTaskStatusArray != NULL) {
44+
// Generate raw status information about each task.
45+
uxArraySize = uxTaskGetSystemState(pxTaskStatusArray, uxArraySize, &ulTotalRunTime);
46+
47+
#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
48+
ulCurrentRunTime = ulTotalRunTime - ulLastRunTime;
49+
ulLastRunTime = ulTotalRunTime;
50+
#endif
51+
printer.printf(
52+
"Tasks: %u"
53+
#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
54+
", Runtime: %lus, Period: %luus"
55+
#endif
56+
"\n",
57+
uxArraySize
58+
#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
59+
,
60+
ulTotalRunTime / 1000000, ulCurrentRunTime
61+
#endif
62+
);
63+
printer.printf("Num\t Name"
64+
#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
65+
"\tLoad"
66+
#endif
67+
"\tPrio\t Free"
68+
#if CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID
69+
"\tCore"
70+
#endif
71+
"\tState\r\n");
72+
for (x = 0; x < uxArraySize; x++) {
73+
#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
74+
if (pxTaskStatusArray[x].xTaskNumber < FREERTOS_TASK_NUMBER_MAX_NUM) {
75+
ulTaskRunTime = (pxTaskStatusArray[x].ulRunTimeCounter - ulRunTimeCounters[pxTaskStatusArray[x].xTaskNumber]);
76+
ulRunTimeCounters[pxTaskStatusArray[x].xTaskNumber] = pxTaskStatusArray[x].ulRunTimeCounter;
77+
ulTaskRunTime = (ulTaskRunTime * 100) / ulCurrentRunTime; // in percentage
78+
} else {
79+
ulTaskRunTime = 0;
80+
}
81+
#endif
82+
printer.printf(
83+
"%3u\t%16s"
84+
#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
85+
"\t%3lu%%"
86+
#endif
87+
"\t%4u\t%5lu"
88+
#if CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID
89+
"\t%4c"
90+
#endif
91+
"\t%s\r\n",
92+
pxTaskStatusArray[x].xTaskNumber, pxTaskStatusArray[x].pcTaskName,
93+
#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
94+
ulTaskRunTime,
95+
#endif
96+
pxTaskStatusArray[x].uxCurrentPriority, pxTaskStatusArray[x].usStackHighWaterMark,
97+
#if CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID
98+
(pxTaskStatusArray[x].xCoreID == tskNO_AFFINITY) ? '*' : ('0' + pxTaskStatusArray[x].xCoreID),
99+
#endif
100+
taskStates[pxTaskStatusArray[x].eCurrentState]
101+
);
102+
}
103+
104+
// The array is no longer needed, free the memory it consumes.
105+
vPortFree(pxTaskStatusArray);
106+
printer.println();
107+
}
108+
#else
109+
printer.println("FreeRTOS trace facility is not enabled.");
110+
#endif /* CONFIG_FREERTOS_USE_TRACE_FACILITY */
111+
}

cores/esp32/freertos_stats.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#ifdef __cplusplus
18+
19+
#include "Print.h"
20+
21+
/*
22+
* Executing this function will cause interrupts and
23+
* the scheduler to be blocked for some time.
24+
* Please use only for debugging purposes.
25+
*/
26+
void printRunningTasks(Print &printer);
27+
28+
#endif

0 commit comments

Comments
 (0)