Skip to content

Commit c381fee

Browse files
committed
Merge branch 'feature/esp_system_linux' into 'master'
esp_system partially buildable on Linux Closes IDF-5983 See merge request espressif/esp-idf!21055
2 parents fbef416 + 5b3a645 commit c381fee

File tree

19 files changed

+305
-60
lines changed

19 files changed

+305
-60
lines changed

.gitlab/ci/host-test.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,14 @@ test_hello_world_linux_compatible_example:
370370
- timeout 15 build/hello_world.elf | tee test.txt
371371
- grep "Hello world!" test.txt
372372

373+
test_esp_system:
374+
extends: .host_test_template
375+
script:
376+
- cd ${IDF_PATH}/components/esp_system/host_test/test_esp_system/
377+
- idf.py build
378+
- timeout 5 build/test_esp_system.elf | tee log.txt || true
379+
- grep "6 Tests 0 Failures 0 Ignored" log.txt
380+
373381
test_esp_timer_cxx:
374382
extends: .host_test_template
375383
script:

components/esp_system/.build-test-rules.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Documentation: .gitlab/ci/README.md#manifest-file-to-control-the-buildtest-apps
22

3+
components/esp_system/host_test/esp_system:
4+
enable:
5+
- if: IDF_TARGET == "linux"
36
components/esp_system/test_apps/rtc_8md256:
47
disable:
58
- if: SOC_RTC_SLOW_CLK_SUPPORT_RC_FAST_D256 != 1

components/esp_system/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
idf_build_get_property(target IDF_TARGET)
22

3+
# On Linux, we only support a few features, hence this simple component registration
4+
if(${target} STREQUAL "linux")
5+
idf_component_register(SRCS "esp_system.c"
6+
"port/soc/linux/reset_reason.c"
7+
"port/soc/linux/system_internal.c"
8+
"port/esp_system_linux.c"
9+
INCLUDE_DIRS "include")
10+
return()
11+
endif()
12+
313
set(srcs "esp_err.c")
414

515
if(CONFIG_IDF_ENV_FPGA)

components/esp_system/esp_system.c

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,8 @@
66

77
#include "esp_system.h"
88
#include "esp_private/system_internal.h"
9-
#include "esp_heap_caps.h"
109
#include "freertos/FreeRTOS.h"
1110
#include "freertos/task.h"
12-
#include "esp_cpu.h"
13-
#include "soc/rtc.h"
14-
#include "esp_private/panic_internal.h"
15-
#include "esp_rom_uart.h"
16-
#include "esp_rom_sys.h"
1711
#if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
1812
#if CONFIG_IDF_TARGET_ESP32S2
1913
#include "esp32s2/memprot.h"
@@ -28,26 +22,6 @@
2822

2923
static shutdown_handler_t shutdown_handlers[SHUTDOWN_HANDLERS_NO];
3024

31-
void IRAM_ATTR esp_restart_noos_dig(void)
32-
{
33-
// make sure all the panic handler output is sent from UART FIFO
34-
if (CONFIG_ESP_CONSOLE_UART_NUM >= 0) {
35-
esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
36-
}
37-
38-
// switch to XTAL (otherwise we will keep running from the PLL)
39-
rtc_clk_cpu_freq_set_xtal();
40-
41-
#if CONFIG_IDF_TARGET_ESP32
42-
esp_cpu_unstall(PRO_CPU_NUM);
43-
#endif
44-
// reset the digital part
45-
esp_rom_software_reset_system();
46-
while (true) {
47-
;
48-
}
49-
}
50-
5125
esp_err_t esp_register_shutdown_handler(shutdown_handler_t handler)
5226
{
5327
for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++) {
@@ -109,28 +83,3 @@ void IRAM_ATTR esp_restart(void)
10983
}
11084
esp_restart_noos();
11185
}
112-
113-
uint32_t esp_get_free_heap_size( void )
114-
{
115-
return heap_caps_get_free_size( MALLOC_CAP_DEFAULT );
116-
}
117-
118-
uint32_t esp_get_free_internal_heap_size( void )
119-
{
120-
return heap_caps_get_free_size( MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL );
121-
}
122-
123-
uint32_t esp_get_minimum_free_heap_size( void )
124-
{
125-
return heap_caps_get_minimum_free_size( MALLOC_CAP_DEFAULT );
126-
}
127-
128-
const char *esp_get_idf_version(void)
129-
{
130-
return IDF_VER;
131-
}
132-
133-
void __attribute__((noreturn)) esp_system_abort(const char *details)
134-
{
135-
panic_abort(details);
136-
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# For more information about build system see
2+
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
3+
# The following five lines of boilerplate have to be in your project's
4+
# CMakeLists in this exact order for cmake to work correctly
5+
cmake_minimum_required(VERSION 3.16)
6+
7+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
8+
set(COMPONENTS main)
9+
project(test_esp_system)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| Supported Targets | Linux |
2+
| ----------------- | ----- |
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
idf_component_register(SRCS "esp_system_test.c"
2+
INCLUDE_DIRS "."
3+
PRIV_REQUIRES unity esp_system)
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <setjmp.h>
9+
#include "unity.h"
10+
11+
#include "esp_system.h"
12+
13+
static jmp_buf env;
14+
static uint32_t token;
15+
16+
static void jump_back_shutdown_handler(void)
17+
{
18+
longjmp(env, 1);
19+
}
20+
21+
static void dummy_shutdown_handler_0(void) { }
22+
static void dummy_shutdown_handler_1(void) { }
23+
static void dummy_shutdown_handler_2(void) { }
24+
static void dummy_shutdown_handler_3(void) { }
25+
static void dummy_shutdown_handler_4(void) { }
26+
27+
static void action(void)
28+
{
29+
token++;
30+
}
31+
32+
static void cleanup(void)
33+
{
34+
esp_unregister_shutdown_handler(jump_back_shutdown_handler);
35+
esp_unregister_shutdown_handler(dummy_shutdown_handler_0);
36+
esp_unregister_shutdown_handler(dummy_shutdown_handler_1);
37+
esp_unregister_shutdown_handler(dummy_shutdown_handler_2);
38+
esp_unregister_shutdown_handler(dummy_shutdown_handler_3);
39+
esp_unregister_shutdown_handler(dummy_shutdown_handler_4);
40+
esp_unregister_shutdown_handler(action);
41+
}
42+
43+
void test_reset_reason(void)
44+
{
45+
TEST_ASSERT_EQUAL(ESP_RST_POWERON, esp_reset_reason());
46+
}
47+
48+
void test_unregister_handler_works(void)
49+
{
50+
token = 0;
51+
// for some reason, the handlers are executed in reverse order of adding handlers, so we always
52+
// register the jumping handler at first to make it execute last
53+
TEST_ASSERT_EQUAL(ESP_OK, esp_register_shutdown_handler(jump_back_shutdown_handler));
54+
TEST_ASSERT_EQUAL(ESP_OK, esp_register_shutdown_handler(action));
55+
TEST_ASSERT_EQUAL(ESP_OK, esp_unregister_shutdown_handler(action));
56+
57+
if (setjmp(env) == 0) {
58+
esp_restart();
59+
}
60+
61+
// fist unregister before any assert to avoid skipping by assert's longjmp
62+
cleanup();
63+
64+
TEST_ASSERT_EQUAL(0, token);
65+
}
66+
67+
void test_register_shutdown_handler_twice_fails(void)
68+
{
69+
TEST_ASSERT_EQUAL(ESP_OK, esp_register_shutdown_handler(jump_back_shutdown_handler));
70+
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, esp_register_shutdown_handler(jump_back_shutdown_handler));
71+
72+
cleanup();
73+
}
74+
75+
void test_register_shutdown_handler_works(void)
76+
{
77+
token = 0;
78+
TEST_ASSERT_EQUAL(esp_register_shutdown_handler(jump_back_shutdown_handler), ESP_OK);
79+
TEST_ASSERT_EQUAL(esp_register_shutdown_handler(action), ESP_OK);
80+
81+
if (setjmp(env) == 0) {
82+
esp_restart();
83+
}
84+
85+
cleanup();
86+
87+
TEST_ASSERT_EQUAL(1, token);
88+
}
89+
90+
void test_register_too_many_shutdown_handler_fails(void)
91+
{
92+
TEST_ASSERT_EQUAL(ESP_OK, esp_register_shutdown_handler(dummy_shutdown_handler_0));
93+
TEST_ASSERT_EQUAL(ESP_OK, esp_register_shutdown_handler(dummy_shutdown_handler_1));
94+
TEST_ASSERT_EQUAL(ESP_OK, esp_register_shutdown_handler(dummy_shutdown_handler_2));
95+
TEST_ASSERT_EQUAL(ESP_OK, esp_register_shutdown_handler(dummy_shutdown_handler_3));
96+
TEST_ASSERT_EQUAL(ESP_OK, esp_register_shutdown_handler(dummy_shutdown_handler_4));
97+
98+
TEST_ASSERT_EQUAL(esp_register_shutdown_handler(jump_back_shutdown_handler), ESP_ERR_NO_MEM);
99+
100+
cleanup();
101+
}
102+
103+
void test_heap_size_stubs(void)
104+
{
105+
TEST_ASSERT_EQUAL(47000, esp_get_free_heap_size());
106+
TEST_ASSERT_EQUAL(47000, esp_get_free_internal_heap_size());
107+
TEST_ASSERT_EQUAL(47000, esp_get_minimum_free_heap_size());
108+
}
109+
110+
void app_main(void)
111+
{
112+
UNITY_BEGIN();
113+
RUN_TEST(test_reset_reason);
114+
RUN_TEST(test_unregister_handler_works);
115+
RUN_TEST(test_register_shutdown_handler_twice_fails);
116+
RUN_TEST(test_register_shutdown_handler_works);
117+
RUN_TEST(test_register_too_many_shutdown_handler_fails);
118+
RUN_TEST(test_heap_size_stubs);
119+
UNITY_END();
120+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_IDF_TARGET="linux"
2+
CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=n

components/esp_system/linker.lf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ entries:
77
panic_arch (noflash)
88

99
esp_err (noflash)
10-
esp_system:esp_system_abort (noflash)
10+
esp_system_chip:esp_system_abort (noflash)
1111
ubsan (noflash)
1212

1313
if ESP_CONSOLE_USB_CDC_SUPPORT_ETS_PRINTF:

components/esp_system/port/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ endif()
66

77
target_include_directories(${COMPONENT_LIB} PRIVATE ${INCLUDE_FILES} include/private)
88

9-
set(srcs "cpu_start.c" "panic_handler.c" "brownout.c")
9+
set(srcs "cpu_start.c" "panic_handler.c" "brownout.c" "esp_system_chip.c")
10+
1011
add_prefix(srcs "${CMAKE_CURRENT_LIST_DIR}/" ${srcs})
1112

1213
target_sources(${COMPONENT_LIB} PRIVATE ${srcs})
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdint.h>
8+
#include "esp_cpu.h"
9+
#include "soc/rtc.h"
10+
#include "esp_private/panic_internal.h"
11+
#include "esp_private/system_internal.h"
12+
#include "esp_heap_caps.h"
13+
#include "esp_rom_uart.h"
14+
#include "esp_rom_sys.h"
15+
#include "sdkconfig.h"
16+
17+
void IRAM_ATTR esp_restart_noos_dig(void)
18+
{
19+
// make sure all the panic handler output is sent from UART FIFO
20+
if (CONFIG_ESP_CONSOLE_UART_NUM >= 0) {
21+
esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
22+
}
23+
24+
// switch to XTAL (otherwise we will keep running from the PLL)
25+
rtc_clk_cpu_freq_set_xtal();
26+
27+
#if CONFIG_IDF_TARGET_ESP32
28+
esp_cpu_unstall(PRO_CPU_NUM);
29+
#endif
30+
// reset the digital part
31+
esp_rom_software_reset_system();
32+
while (true) {
33+
;
34+
}
35+
}
36+
37+
uint32_t esp_get_free_heap_size( void )
38+
{
39+
return heap_caps_get_free_size( MALLOC_CAP_DEFAULT );
40+
}
41+
42+
uint32_t esp_get_free_internal_heap_size( void )
43+
{
44+
return heap_caps_get_free_size( MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL );
45+
}
46+
47+
uint32_t esp_get_minimum_free_heap_size( void )
48+
{
49+
return heap_caps_get_minimum_free_size( MALLOC_CAP_DEFAULT );
50+
}
51+
52+
const char *esp_get_idf_version(void)
53+
{
54+
return IDF_VER;
55+
}
56+
57+
void __attribute__((noreturn)) esp_system_abort(const char *details)
58+
{
59+
panic_abort(details);
60+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*
8+
* All functions presented here are stubs for the POSIX/Linux implementation of FReeRTOS.
9+
* They are meant to allow to compile, but they DO NOT return any meaningful value.
10+
*/
11+
12+
#include <stdlib.h>
13+
#include <stdint.h>
14+
#include "esp_private/system_internal.h"
15+
16+
static const uint32_t MAGIC_HEAP_SIZE = 47000;
17+
18+
// dummy, we should never get here on Linux
19+
void esp_restart_noos_dig(void)
20+
{
21+
abort();
22+
}
23+
24+
uint32_t esp_get_free_heap_size( void )
25+
{
26+
return MAGIC_HEAP_SIZE;
27+
}
28+
29+
uint32_t esp_get_free_internal_heap_size( void )
30+
{
31+
return MAGIC_HEAP_SIZE;
32+
}
33+
34+
uint32_t esp_get_minimum_free_heap_size( void )
35+
{
36+
return MAGIC_HEAP_SIZE;
37+
}
38+
39+
const char *esp_get_idf_version(void)
40+
{
41+
return IDF_VER;
42+
}
43+
44+
void __attribute__((noreturn)) esp_system_abort(const char *details)
45+
{
46+
exit(1);
47+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# The CPU frequency is never really used, except in some xtensa timer headers, so it's empty for Linux.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Original Kconfig has settings related to brownout, rom and cache. All non-existent on Linux.

0 commit comments

Comments
 (0)