Skip to content

Commit 4d3de47

Browse files
committed
feat: Add initial chip structure
1 parent 2b2dbe2 commit 4d3de47

File tree

22 files changed

+1348
-1
lines changed

22 files changed

+1348
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ add_subdirectory(${CMAKE_SOURCE_DIR}/src)
6262

6363
set(ESP_TARGET ${TARGET_CHIP} CACHE INTERNAL "Pass TARGET_CHIP as ESP_TARGET")
6464
add_subdirectory(${CMAKE_SOURCE_DIR}/esp-stub-lib)
65-
target_link_libraries(${TARGET_NAME} PRIVATE esp-stub-lib)
65+
add_subdirectory(${CMAKE_SOURCE_DIR}/soc)
66+
target_link_libraries(${TARGET_NAME} PRIVATE esp-stub-lib soc)
6667

6768
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
6869
COMMAND ${CMAKE_SOURCE_DIR}/tools/elf2json.py ${CMAKE_BINARY_DIR}/${TARGET_NAME}${CMAKE_EXECUTABLE_SUFFIX_C} ${CMAKE_BINARY_DIR}/${TARGET_CHIP}.json

soc/CMakeLists.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
cmake_minimum_required(VERSION 3.28)
2+
3+
add_library(soc STATIC)
4+
5+
target_sources(soc PRIVATE
6+
common/soc.c
7+
common/uart_common.c
8+
common/watchdog_common.c
9+
)
10+
11+
if(TARGET_CHIP STREQUAL "esp32")
12+
target_sources(soc PRIVATE targets/esp32.c)
13+
elseif(TARGET_CHIP STREQUAL "esp32s2")
14+
target_sources(soc PRIVATE targets/esp32s2.c)
15+
elseif(TARGET_CHIP STREQUAL "esp32s3")
16+
target_sources(soc PRIVATE targets/esp32s3.c)
17+
elseif(TARGET_CHIP STREQUAL "esp32c2")
18+
target_sources(soc PRIVATE targets/esp32c2.c)
19+
elseif(TARGET_CHIP STREQUAL "esp32c3")
20+
target_sources(soc PRIVATE targets/esp32c3.c)
21+
elseif(TARGET_CHIP STREQUAL "esp32c5")
22+
target_sources(soc PRIVATE targets/esp32c5.c)
23+
elseif(TARGET_CHIP STREQUAL "esp32c6")
24+
target_sources(soc PRIVATE targets/esp32c6.c)
25+
elseif(TARGET_CHIP STREQUAL "esp32c61")
26+
target_sources(soc PRIVATE targets/esp32c61.c)
27+
elseif(TARGET_CHIP STREQUAL "esp32h2")
28+
target_sources(soc PRIVATE targets/esp32h2.c)
29+
elseif(TARGET_CHIP STREQUAL "esp32p4")
30+
target_sources(soc PRIVATE targets/esp32p4.c)
31+
elseif(TARGET_CHIP STREQUAL "esp8266")
32+
target_sources(soc PRIVATE targets/esp8266.c)
33+
else()
34+
message(FATAL_ERROR "Unsupported TARGET_CHIP: ${TARGET_CHIP}")
35+
endif()
36+
37+
# Include directories
38+
target_include_directories(soc PUBLIC
39+
${CMAKE_CURRENT_SOURCE_DIR}/include
40+
)

soc/common/soc.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0 OR MIT
5+
*/
6+
7+
#include <stddef.h>
8+
#include "soc.h"
9+
10+
/*
11+
* Single SoC info structure - chip-specific file provides this
12+
* Chip selected at compile time
13+
*/
14+
extern const soc_info_t soc_info;
15+
16+
const soc_info_t* soc_get_info(void)
17+
{
18+
return &soc_info;
19+
}

soc/common/uart_common.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0 OR MIT
5+
*/
6+
7+
#include "soc.h"
8+
9+
void uart_init(void)
10+
{
11+
// Common UART initialization logic, especially interrupt handling
12+
}

soc/common/watchdog_common.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0 OR MIT
5+
*/
6+
7+
#include "soc.h"
8+
9+
void disable_watchdogs(void)
10+
{
11+
const rtc_t *rtc = soc_get_peripherals()->rtc;
12+
if (rtc->wdt_disable) {
13+
rtc->wdt_disable();
14+
}
15+
}
16+
17+
void enable_watchdogs(void)
18+
{
19+
const rtc_t *rtc = soc_get_peripherals()->rtc;
20+
if (rtc->wdt_enable) {
21+
rtc->wdt_enable();
22+
}
23+
}

soc/include/peripherals/rtc.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0 OR MIT
5+
*/
6+
7+
#pragma once
8+
9+
#include <stdint.h>
10+
11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
14+
15+
/* RTC/Power Management peripheral structure */
16+
typedef struct {
17+
/* Register offsets */
18+
uint32_t option1_reg;
19+
uint32_t wdtconfig0_reg;
20+
uint32_t wdtwprotect_reg;
21+
uint32_t swd_conf_reg;
22+
uint32_t swd_wprotect_reg;
23+
uint32_t cpu_per_conf_reg;
24+
uint32_t sysclk_conf_reg;
25+
26+
/* Chip-specific values */
27+
uint32_t swd_wkey;
28+
uint32_t wdt_wkey;
29+
uint32_t swd_auto_feed_en_bit;
30+
uint32_t force_download_boot_bit;
31+
uint32_t cpuperiod_sel_mask;
32+
uint32_t cpuperiod_sel_shift;
33+
uint32_t cpuperiod_max;
34+
uint32_t soc_clk_sel_mask;
35+
uint32_t soc_clk_sel_shift;
36+
uint32_t soc_clk_max;
37+
38+
/* Function pointers for RTC-specific operations */
39+
void (*wdt_disable)(void);
40+
void (*wdt_enable)(void);
41+
} rtc_t;
42+
43+
#ifdef __cplusplus
44+
}
45+
#endif

soc/include/peripherals/system.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0 OR MIT
5+
*/
6+
7+
#pragma once
8+
9+
#include <stdint.h>
10+
#include <stdbool.h>
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif
15+
16+
/* SYSTEM registers structure (older chips - more registers) */
17+
typedef struct {
18+
uint32_t base_reg;
19+
uint32_t cpu_per_conf_reg;
20+
uint32_t sysclk_conf_reg;
21+
uint32_t cpuperiod_sel_mask;
22+
uint32_t cpuperiod_sel_shift;
23+
uint32_t cpuperiod_max;
24+
uint32_t soc_clk_sel_mask;
25+
uint32_t soc_clk_sel_shift;
26+
uint32_t soc_clk_max;
27+
} system_registers_t;
28+
29+
/* PCR registers structure (newer chips - fewer registers) */
30+
typedef struct {
31+
uint32_t base_reg;
32+
uint32_t sysclk_conf_reg;
33+
uint32_t soc_clk_sel_mask;
34+
uint32_t soc_clk_sel_shift;
35+
uint32_t soc_clk_max;
36+
} pcr_registers_t;
37+
38+
/* Combined system control peripheral registers structure */
39+
typedef struct {
40+
const system_registers_t *system; /* For older chips, NULL for newer chips */
41+
const pcr_registers_t *pcr; /* For newer chips, NULL for older chips */
42+
/* Function pointers for system-specific operations */
43+
void (*set_cpu_freq_max)(void);
44+
void (*restore_cpu_freq)(void);
45+
void (*configure_clocks)(void);
46+
} system_t;
47+
48+
#ifdef __cplusplus
49+
}
50+
#endif

soc/include/peripherals/uart.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0 OR MIT
5+
*/
6+
7+
#pragma once
8+
9+
#include <stdint.h>
10+
11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
14+
15+
/* UART peripheral structure */
16+
typedef struct {
17+
uint32_t fifo_reg;
18+
uint32_t int_st_reg;
19+
uint32_t int_ena_reg;
20+
uint32_t int_clr_reg;
21+
uint32_t clkdiv_reg;
22+
uint32_t status_reg;
23+
uint32_t clkdiv_mask;
24+
uint32_t clkdiv_frag_s;
25+
uint32_t clkdiv_frag_v;
26+
uint32_t rxfifo_cnt_mask;
27+
uint32_t uart_inum;
28+
uint32_t uart_usb_otg_num;
29+
uint32_t uart_usb_jtag_serial_num;
30+
} uart_t;
31+
32+
#ifdef __cplusplus
33+
}
34+
#endif

soc/include/peripherals/usb.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0 OR MIT
5+
*/
6+
7+
#pragma once
8+
9+
#include <stdint.h>
10+
11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
14+
15+
/* USB peripheral structure */
16+
typedef struct {
17+
uint32_t usb_intr_map_reg; /* USB JTAG Serial interrupt mapping */
18+
uint32_t usb_otg_intr_map_reg; /* USB OTG interrupt mapping */
19+
uint32_t hp_sys_usbotg20_ctrl_reg; /* USB OTG control register */
20+
uint32_t usb_inum; /* USB interrupt number */
21+
uint32_t clic_ext_intr_num_offset; /* RISC-V CLIC interrupt offset */
22+
} usb_t;
23+
24+
#ifdef __cplusplus
25+
}
26+
#endif

soc/include/soc.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0 OR MIT
5+
*/
6+
7+
#pragma once
8+
9+
#include "peripherals/uart.h"
10+
#include "peripherals/usb.h"
11+
#include "peripherals/rtc.h"
12+
#include "peripherals/system.h"
13+
14+
#ifdef __cplusplus
15+
extern "C" {
16+
#endif
17+
18+
/* Essential SoC Capabilities for flasher stub */
19+
typedef struct {
20+
bool is_riscv; /* RISC-V architecture (affects interrupt handling) */
21+
bool use_max_cpu_freq; /* Can safely use maximum CPU frequency */
22+
bool config_efuse_spi; /* Can configure SPI flash through efuse */
23+
} soc_capabilities_t;
24+
25+
/* Essential peripherals for flasher stub */
26+
typedef struct {
27+
const uart_t *uart; /* UART communication with host */
28+
const usb_t *usb; /* USB communication (JTAG Serial / OTG) */
29+
const rtc_t *rtc; /* Watchdog and power management */
30+
const system_t *system; /* Clock and system control */
31+
} soc_peripherals_t;
32+
33+
/* Main SoC structure */
34+
typedef struct {
35+
const soc_capabilities_t *capabilities;
36+
const soc_peripherals_t *peripherals;
37+
uint8_t security_info_bytes;
38+
uint32_t rom_spiflash_legacy_addr;
39+
} soc_info_t;
40+
41+
const soc_info_t* soc_get_info(void);
42+
43+
/* Convenience accessors */
44+
static inline const soc_capabilities_t* soc_get_capabilities(void)
45+
{
46+
return soc_get_info()->capabilities;
47+
}
48+
49+
static inline const soc_peripherals_t* soc_get_peripherals(void)
50+
{
51+
return soc_get_info()->peripherals;
52+
}
53+
54+
#ifdef __cplusplus
55+
}
56+
#endif

0 commit comments

Comments
 (0)