blob: 48f067debb76d18fc2a1dbc219719b90baf82ecc [file] [log] [blame]
Damien George9b908822020-09-23 15:55:55 +10001# Set location of base MicroPython directory.
Damien George0009a7d2021-06-24 16:03:25 +10002if(NOT MICROPY_DIR)
Damien Georgee4650122023-05-09 09:52:54 +10003 get_filename_component(MICROPY_DIR ${CMAKE_CURRENT_LIST_DIR}/../.. ABSOLUTE)
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -07004endif()
5
Damien Georgee4650122023-05-09 09:52:54 +10006# Set location of the ESP32 port directory.
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -07007if(NOT MICROPY_PORT_DIR)
8 get_filename_component(MICROPY_PORT_DIR ${MICROPY_DIR}/ports/esp32 ABSOLUTE)
Damien George0009a7d2021-06-24 16:03:25 +10009endif()
Damien George9b908822020-09-23 15:55:55 +100010
11# Include core source components.
12include(${MICROPY_DIR}/py/py.cmake)
Damien George9b908822020-09-23 15:55:55 +100013
Jim Mussaredad123ed2023-07-31 17:24:38 +100014# CMAKE_BUILD_EARLY_EXPANSION is set during the component-discovery phase of
15# `idf.py build`, so none of the extmod/usermod (and in reality, most of the
16# micropython) rules need to happen. Specifically, you cannot invoke add_library.
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -040017if(NOT CMAKE_BUILD_EARLY_EXPANSION)
Damien Georgebd375df2022-06-08 12:21:51 +100018 # Enable extmod components that will be configured by extmod.cmake.
19 # A board may also have enabled additional components.
20 set(MICROPY_PY_BTREE ON)
21
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -040022 include(${MICROPY_DIR}/py/usermod.cmake)
Damien George212fe7f2021-04-08 23:45:28 +100023 include(${MICROPY_DIR}/extmod/extmod.cmake)
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -040024endif()
25
Damien Georgee4650122023-05-09 09:52:54 +100026list(APPEND MICROPY_QSTRDEFS_PORT
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -070027 ${MICROPY_PORT_DIR}/qstrdefsport.h
Damien George75db0b92021-02-21 11:33:15 +110028)
29
Damien Georgee4650122023-05-09 09:52:54 +100030list(APPEND MICROPY_SOURCE_SHARED
Damien George136369d2021-07-09 14:19:15 +100031 ${MICROPY_DIR}/shared/readline/readline.c
32 ${MICROPY_DIR}/shared/netutils/netutils.c
33 ${MICROPY_DIR}/shared/timeutils/timeutils.c
34 ${MICROPY_DIR}/shared/runtime/interrupt_char.c
robert-hha04a1412024-03-10 15:14:20 +010035 ${MICROPY_DIR}/shared/runtime/mpirq.c
Damien George136369d2021-07-09 14:19:15 +100036 ${MICROPY_DIR}/shared/runtime/stdout_helpers.c
37 ${MICROPY_DIR}/shared/runtime/sys_stdio_mphal.c
38 ${MICROPY_DIR}/shared/runtime/pyexec.c
39)
40
Damien Georgee4650122023-05-09 09:52:54 +100041list(APPEND MICROPY_SOURCE_LIB
Damien George9b908822020-09-23 15:55:55 +100042 ${MICROPY_DIR}/lib/littlefs/lfs1.c
43 ${MICROPY_DIR}/lib/littlefs/lfs1_util.c
44 ${MICROPY_DIR}/lib/littlefs/lfs2.c
45 ${MICROPY_DIR}/lib/littlefs/lfs2_util.c
Damien Georgec9eb6bc2023-12-12 16:05:03 +110046 ${MICROPY_DIR}/lib/mbedtls_errors/esp32_mbedtls_errors.c
Damien George9b908822020-09-23 15:55:55 +100047 ${MICROPY_DIR}/lib/oofatfs/ff.c
48 ${MICROPY_DIR}/lib/oofatfs/ffunicode.c
Damien George9b908822020-09-23 15:55:55 +100049)
50
Damien Georgee4650122023-05-09 09:52:54 +100051list(APPEND MICROPY_SOURCE_DRIVERS
Damien George9b908822020-09-23 15:55:55 +100052 ${MICROPY_DIR}/drivers/bus/softspi.c
53 ${MICROPY_DIR}/drivers/dht/dht.c
54)
55
Andrew Leech42479212024-05-23 17:23:41 +100056string(CONCAT GIT_SUBMODULES "${GIT_SUBMODULES} " lib/tinyusb)
57if(MICROPY_PY_TINYUSB)
58 set(TINYUSB_SRC "${MICROPY_DIR}/lib/tinyusb/src")
59 string(TOUPPER OPT_MCU_${IDF_TARGET} tusb_mcu)
60
61 list(APPEND MICROPY_DEF_TINYUSB
62 CFG_TUSB_MCU=${tusb_mcu}
63 )
64
65 list(APPEND MICROPY_SOURCE_TINYUSB
66 ${TINYUSB_SRC}/tusb.c
67 ${TINYUSB_SRC}/common/tusb_fifo.c
68 ${TINYUSB_SRC}/device/usbd.c
69 ${TINYUSB_SRC}/device/usbd_control.c
70 ${TINYUSB_SRC}/class/cdc/cdc_device.c
71 ${TINYUSB_SRC}/portable/synopsys/dwc2/dcd_dwc2.c
72 ${MICROPY_DIR}/shared/tinyusb/mp_usbd.c
73 ${MICROPY_DIR}/shared/tinyusb/mp_usbd_cdc.c
74 ${MICROPY_DIR}/shared/tinyusb/mp_usbd_descriptor.c
75 )
76
77 list(APPEND MICROPY_INC_TINYUSB
78 ${TINYUSB_SRC}
79 ${MICROPY_DIR}/shared/tinyusb/
80 )
81
82 list(APPEND MICROPY_LINK_TINYUSB
83 -Wl,--wrap=dcd_event_handler
84 )
85endif()
86
Damien Georgee4650122023-05-09 09:52:54 +100087list(APPEND MICROPY_SOURCE_PORT
Daniƫl van de Giessenc10a74b2024-03-13 18:23:17 +010088 panichandler.c
Damien George03eae482023-10-25 19:13:11 +110089 adc.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -070090 main.c
Damien George1db40ed2023-06-21 15:09:26 +100091 ppp_set_auth.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -070092 uart.c
93 usb.c
94 usb_serial_jtag.c
95 gccollect.c
96 mphalport.c
97 fatfs_port.c
98 help.c
99 machine_bitstream.c
100 machine_timer.c
101 machine_pin.c
102 machine_touchpad.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700103 machine_dac.c
104 machine_i2c.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700105 network_common.c
106 network_lan.c
107 network_ppp.c
108 network_wlan.c
109 mpnimbleport.c
110 modsocket.c
Angus Gratton82e69df2024-10-02 14:49:49 +1000111 lwip_patch.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700112 modesp.c
113 esp32_nvs.c
114 esp32_partition.c
115 esp32_rmt.c
116 esp32_ulp.c
117 modesp32.c
118 machine_hw_spi.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700119 mpthreadport.c
120 machine_rtc.c
121 machine_sdcard.c
122 modespnow.c
Damien George9b908822020-09-23 15:55:55 +1000123)
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700124list(TRANSFORM MICROPY_SOURCE_PORT PREPEND ${MICROPY_PORT_DIR}/)
Damien Georgecb31c0a2023-07-15 00:03:59 +1000125list(APPEND MICROPY_SOURCE_PORT ${CMAKE_BINARY_DIR}/pins.c)
Damien George9b908822020-09-23 15:55:55 +1000126
Damien Georgee4650122023-05-09 09:52:54 +1000127list(APPEND MICROPY_SOURCE_QSTR
Damien George9b908822020-09-23 15:55:55 +1000128 ${MICROPY_SOURCE_PY}
129 ${MICROPY_SOURCE_EXTMOD}
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -0400130 ${MICROPY_SOURCE_USERMOD}
Damien George136369d2021-07-09 14:19:15 +1000131 ${MICROPY_SOURCE_SHARED}
Damien George9b908822020-09-23 15:55:55 +1000132 ${MICROPY_SOURCE_LIB}
133 ${MICROPY_SOURCE_PORT}
Damien Georgef046b502021-09-22 00:00:26 +1000134 ${MICROPY_SOURCE_BOARD}
Andrew Leech42479212024-05-23 17:23:41 +1000135 ${MICROPY_SOURCE_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000136)
137
Damien Georgee4650122023-05-09 09:52:54 +1000138list(APPEND IDF_COMPONENTS
Damien George9b908822020-09-23 15:55:55 +1000139 app_update
140 bootloader_support
Damien Georgeda2b5fa2021-02-14 01:36:30 +1100141 bt
Damien George9b908822020-09-23 15:55:55 +1000142 driver
Damien Georgee4650122023-05-09 09:52:54 +1000143 esp_adc
144 esp_app_format
Damien George9b908822020-09-23 15:55:55 +1000145 esp_common
146 esp_eth
147 esp_event
Damien Georgee4650122023-05-09 09:52:54 +1000148 esp_hw_support
149 esp_netif
150 esp_partition
151 esp_pm
152 esp_psram
Damien George9b908822020-09-23 15:55:55 +1000153 esp_ringbuf
154 esp_rom
Damien Georgee4650122023-05-09 09:52:54 +1000155 esp_system
156 esp_timer
Damien George9b908822020-09-23 15:55:55 +1000157 esp_wifi
158 freertos
Damien Georgee4650122023-05-09 09:52:54 +1000159 hal
Damien George9b908822020-09-23 15:55:55 +1000160 heap
161 log
162 lwip
163 mbedtls
Damien George9b908822020-09-23 15:55:55 +1000164 newlib
165 nvs_flash
166 sdmmc
167 soc
168 spi_flash
Damien George9b908822020-09-23 15:55:55 +1000169 ulp
Andrew Leech42479212024-05-23 17:23:41 +1000170 usb
Damien George9b908822020-09-23 15:55:55 +1000171 vfs
Damien George9b908822020-09-23 15:55:55 +1000172)
173
Damien George9b908822020-09-23 15:55:55 +1000174# Register the main IDF component.
175idf_component_register(
176 SRCS
177 ${MICROPY_SOURCE_PY}
178 ${MICROPY_SOURCE_EXTMOD}
Damien George136369d2021-07-09 14:19:15 +1000179 ${MICROPY_SOURCE_SHARED}
Damien George9b908822020-09-23 15:55:55 +1000180 ${MICROPY_SOURCE_LIB}
181 ${MICROPY_SOURCE_DRIVERS}
182 ${MICROPY_SOURCE_PORT}
Damien Georgef046b502021-09-22 00:00:26 +1000183 ${MICROPY_SOURCE_BOARD}
Andrew Leech42479212024-05-23 17:23:41 +1000184 ${MICROPY_SOURCE_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000185 INCLUDE_DIRS
Damien George5dcc9b32021-04-09 00:59:16 +1000186 ${MICROPY_INC_CORE}
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -0400187 ${MICROPY_INC_USERMOD}
Andrew Leech42479212024-05-23 17:23:41 +1000188 ${MICROPY_INC_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000189 ${MICROPY_PORT_DIR}
190 ${MICROPY_BOARD_DIR}
191 ${CMAKE_BINARY_DIR}
Jim Mussaredacbdbcd2024-03-21 15:46:41 +1100192 LDFRAGMENTS
193 linker.lf
Damien George9b908822020-09-23 15:55:55 +1000194 REQUIRES
195 ${IDF_COMPONENTS}
196)
197
198# Set the MicroPython target as the current (main) IDF component target.
199set(MICROPY_TARGET ${COMPONENT_TARGET})
200
201# Define mpy-cross flags, for use with frozen code.
Andrew Leech6d799372023-06-26 11:53:28 +1000202if(CONFIG_IDF_TARGET_ARCH STREQUAL "xtensa")
Damien George9b908822020-09-23 15:55:55 +1000203set(MICROPY_CROSS_FLAGS -march=xtensawin)
Alessandro Gatti95ce61d2023-10-31 13:28:25 +0100204endif()
Damien George9b908822020-09-23 15:55:55 +1000205
206# Set compile options for this port.
207target_compile_definitions(${MICROPY_TARGET} PUBLIC
Damien Georgeab9d47e2021-04-08 23:56:28 +1000208 ${MICROPY_DEF_CORE}
Jim Mussared2fbf42d2023-07-25 15:03:30 +1000209 ${MICROPY_DEF_BOARD}
Andrew Leech42479212024-05-23 17:23:41 +1000210 ${MICROPY_DEF_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000211 MICROPY_ESP_IDF_4=1
212 MICROPY_VFS_FAT=1
213 MICROPY_VFS_LFS2=1
214 FFCONF_H=\"${MICROPY_OOFATFS_DIR}/ffconf.h\"
215 LFS1_NO_MALLOC LFS1_NO_DEBUG LFS1_NO_WARN LFS1_NO_ERROR LFS1_NO_ASSERT
216 LFS2_NO_MALLOC LFS2_NO_DEBUG LFS2_NO_WARN LFS2_NO_ERROR LFS2_NO_ASSERT
217)
218
219# Disable some warnings to keep the build output clean.
220target_compile_options(${MICROPY_TARGET} PUBLIC
221 -Wno-clobbered
222 -Wno-deprecated-declarations
223 -Wno-missing-field-initializers
224)
225
Andrew Leech42479212024-05-23 17:23:41 +1000226target_link_options(${MICROPY_TARGET} PUBLIC
227 ${MICROPY_LINK_TINYUSB}
228)
229
Damien George5dbb8222021-03-17 12:35:59 +1100230# Additional include directories needed for private NimBLE headers.
231target_include_directories(${MICROPY_TARGET} PUBLIC
232 ${IDF_PATH}/components/bt/host/nimble/nimble
233)
234
Damien Georgeab9d47e2021-04-08 23:56:28 +1000235# Add additional extmod and usermod components.
236target_link_libraries(${MICROPY_TARGET} micropy_extmod_btree)
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -0400237target_link_libraries(${MICROPY_TARGET} usermod)
238
Angus Grattonff70a912024-10-22 15:02:32 +1100239# Enable the panic handler wrapper
240idf_build_set_property(LINK_OPTIONS "-Wl,--wrap=esp_panic_handler" APPEND)
241
242# Patch LWIP memory pool allocators (see lwip_patch.c)
243idf_build_set_property(LINK_OPTIONS "-Wl,--wrap=memp_malloc" APPEND)
244idf_build_set_property(LINK_OPTIONS "-Wl,--wrap=memp_free" APPEND)
245
Luca Burelli31ef7c12023-07-05 18:03:11 +0200246# Collect all of the include directories and compile definitions for the IDF components,
247# including those added by the IDF Component Manager via idf_components.yaml.
248foreach(comp ${__COMPONENT_NAMES_RESOLVED})
Damien George0fabda32021-04-08 23:42:22 +1000249 micropy_gather_target_properties(__idf_${comp})
Damien Georgee4650122023-05-09 09:52:54 +1000250 micropy_gather_target_properties(${comp})
Damien George9b908822020-09-23 15:55:55 +1000251endforeach()
252
Damien George9b908822020-09-23 15:55:55 +1000253# Include the main MicroPython cmake rules.
254include(${MICROPY_DIR}/py/mkrules.cmake)
Damien Georgecb31c0a2023-07-15 00:03:59 +1000255
256# Generate source files for named pins (requires mkrules.cmake for MICROPY_GENHDR_DIR).
257
258set(GEN_PINS_PREFIX "${MICROPY_PORT_DIR}/boards/pins_prefix.c")
259set(GEN_PINS_MKPINS "${MICROPY_PORT_DIR}/boards/make-pins.py")
260set(GEN_PINS_SRC "${CMAKE_BINARY_DIR}/pins.c")
261set(GEN_PINS_HDR "${MICROPY_GENHDR_DIR}/pins.h")
262
263if(EXISTS "${MICROPY_BOARD_DIR}/pins.csv")
264 set(GEN_PINS_BOARD_CSV "${MICROPY_BOARD_DIR}/pins.csv")
265 set(GEN_PINS_BOARD_CSV_ARG --board-csv "${GEN_PINS_BOARD_CSV}")
266endif()
267
268target_sources(${MICROPY_TARGET} PRIVATE ${GEN_PINS_HDR})
269
270add_custom_command(
271 OUTPUT ${GEN_PINS_SRC} ${GEN_PINS_HDR}
272 COMMAND ${Python3_EXECUTABLE} ${GEN_PINS_MKPINS} ${GEN_PINS_BOARD_CSV_ARG}
273 --prefix ${GEN_PINS_PREFIX} --output-source ${GEN_PINS_SRC} --output-header ${GEN_PINS_HDR}
274 DEPENDS
275 ${MICROPY_MPVERSION}
276 ${GEN_PINS_MKPINS}
277 ${GEN_PINS_BOARD_CSV}
278 ${GEN_PINS_PREFIX}
279 VERBATIM
280 COMMAND_EXPAND_LISTS
281)