Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document the complete list of changes needed to compile #1

Open
ftylitak opened this issue Jul 14, 2023 · 5 comments
Open

Document the complete list of changes needed to compile #1

ftylitak opened this issue Jul 14, 2023 · 5 comments

Comments

@ftylitak
Copy link

Greetings,

first of all thank you very much for your work. It is a pitty this is not supported by default in micropython.

I am trying to compile micropython for esp32 using micropython 1.19 branch in github though I am having trouble compiling it.

Things that I did till now:

a) copied prod/src/bt_spp_client.c and prod/src/bt_spp_server.c at path: "micropython/port/esp32/"
b) added both files in main/CMakeLists.txt (this is for 1.19 version)
c) changed the sdkconfig.ble file to use the configurations that you include in "main/prod/README.md"
d) compiled.

Could you assist me on what other changes need to be made to be able to compile the code and use your module?

Thank you in advance!

@shariltumin
Copy link
Owner

This is my board/ESP32_BT/mpconfigboard.cmake


set(SDKCONFIG_DEFAULTS
    boards/sdkconfig.base
    boards/sdkconfig.240mhz
    boards/ESP32_BT/sdkconfig.board
)

set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/ESP32_BT/manifest.py)

You didn't say what and where the compilation fail. Try make V=1 board=YOUR-ESP32-BOARD
Hope this helps.

@shariltumin
Copy link
Owner

And one more thing. The firmware excludes both BLE and network, so you need to modify mpconfigport.h as well.
This is my mpconfigport.h

// Options to control how MicroPython is built for this port,
// overriding defaults in py/mpconfig.h.

// Board-specific definitions
#include "mpconfigboard.h"

#include <stdint.h>
#include <alloca.h>
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "driver/i2s.h"

#ifndef MICROPY_CONFIG_ROM_LEVEL
#define MICROPY_CONFIG_ROM_LEVEL            (MICROPY_CONFIG_ROM_LEVEL_EXTRA_FEATURES)
#endif

// object representation and NLR handling
#define MICROPY_OBJ_REPR                    (MICROPY_OBJ_REPR_A)
#define MICROPY_NLR_SETJMP                  (1)
#if CONFIG_IDF_TARGET_ESP32C3
#define MICROPY_GCREGS_SETJMP               (1)
#endif

// memory allocation policies
#define MICROPY_ALLOC_PATH_MAX              (128)

// emitters
#define MICROPY_PERSISTENT_CODE_LOAD        (1)
#if !CONFIG_IDF_TARGET_ESP32C3
#define MICROPY_EMIT_XTENSAWIN              (1)
#endif

// workaround for xtensa-esp32-elf-gcc esp-2020r3, which can generate wrong code for loops
// see https://github.com/espressif/esp-idf/issues/9130
// this was fixed in newer versions of the compiler by:
//   "gas: use literals/const16 for xtensa loop relaxation"
//   https://github.com/jcmvbkbc/binutils-gdb-xtensa/commit/403b0b61f6d4358aee8493cb1d11814e368942c9
#define MICROPY_COMP_CONST_FOLDING_COMPILER_WORKAROUND (1)

// optimisations
#define MICROPY_OPT_COMPUTED_GOTO           (1)

// Python internal features
#define MICROPY_READER_VFS                  (1)
#define MICROPY_ENABLE_GC                   (1)
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
#define MICROPY_LONGINT_IMPL                (MICROPY_LONGINT_IMPL_MPZ)
#define MICROPY_ERROR_REPORTING             (MICROPY_ERROR_REPORTING_NORMAL)
#define MICROPY_WARNINGS                    (1)
#define MICROPY_FLOAT_IMPL                  (MICROPY_FLOAT_IMPL_FLOAT)
#define MICROPY_STREAMS_POSIX_API           (1)
#define MICROPY_MODULE_FROZEN_STR           (0)
#define MICROPY_MODULE_FROZEN_MPY           (1)
#define MICROPY_QSTR_EXTRA_POOL             mp_qstr_frozen_const_pool
#define MICROPY_USE_INTERNAL_ERRNO          (0) // errno.h from xtensa-esp32-elf/sys-include/sys
#define MICROPY_USE_INTERNAL_PRINTF         (0) // ESP32 SDK requires its own printf
#define MICROPY_SCHEDULER_DEPTH             (8)
#define MICROPY_VFS                         (1)

// control over Python builtins
#define MICROPY_PY_STR_BYTES_CMP_WARN       (1)
#define MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS (1)
#define MICROPY_PY_BUILTINS_HELP            (0)
// -define MICROPY_PY_BUILTINS_HELP_TEXT       esp32_help_text
#define MICROPY_PY_IO_BUFFEREDWRITER        (1)
#define MICROPY_PY_UTIME_MP_HAL             (1)
#define MICROPY_PY_THREAD                   (1)
#define MICROPY_PY_THREAD_GIL               (1)
#define MICROPY_PY_THREAD_GIL_VM_DIVISOR    (32)

// extended modules
#define MICROPY_PY_UTIMEQ                   (1)
#define MICROPY_PY_UHASHLIB_SHA1            (0)
#define MICROPY_PY_UHASHLIB_SHA256          (0)
#define MICROPY_PY_UCRYPTOLIB               (0)
#define MICROPY_PY_URANDOM_SEED_INIT_FUNC   (esp_random())
#define MICROPY_PY_UOS_INCLUDEFILE          "ports/esp32/moduos.c"
#define MICROPY_PY_OS_DUPTERM               (1)
#define MICROPY_PY_UOS_DUPTERM_NOTIFY       (1)
#define MICROPY_PY_UOS_UNAME                (1)
#define MICROPY_PY_UOS_URANDOM              (1)
#define MICROPY_PY_MACHINE                  (1)
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW     mp_pin_make_new
#define MICROPY_PY_MACHINE_BITSTREAM        (1)
#define MICROPY_PY_MACHINE_PULSE            (1)
#define MICROPY_PY_MACHINE_PWM              (1)
#define MICROPY_PY_MACHINE_PWM_INIT         (1)
#define MICROPY_PY_MACHINE_PWM_DUTY         (1)
#define MICROPY_PY_MACHINE_PWM_DUTY_U16_NS  (1)
#define MICROPY_PY_MACHINE_PWM_INCLUDEFILE  "ports/esp32/machine_pwm.c"
#define MICROPY_PY_MACHINE_I2C              (1)
#define MICROPY_PY_MACHINE_I2C_TRANSFER_WRITE1 (1)
#define MICROPY_PY_MACHINE_SOFTI2C          (1)
#define MICROPY_PY_MACHINE_SPI              (1)
#define MICROPY_PY_MACHINE_SPI_MSB          (0)
#define MICROPY_PY_MACHINE_SPI_LSB          (1)
#define MICROPY_PY_MACHINE_SOFTSPI          (1)
#ifndef MICROPY_PY_MACHINE_DAC
#define MICROPY_PY_MACHINE_DAC              (1)
#endif
#ifndef MICROPY_PY_MACHINE_I2S
#define MICROPY_PY_MACHINE_I2S              (1)
#endif
#define MICROPY_PY_NETWORK                  (0)
#define MICROPY_PY_NETWORK_WLAN             (0)
#define MICROPY_HW_ENABLE_SDCARD            (1)
#define MICROPY_HW_SOFTSPI_MIN_DELAY        (0)
#define MICROPY_HW_SOFTSPI_MAX_BAUDRATE     (ets_get_cpu_frequency() * 1000000 / 200) // roughly
#define MICROPY_PY_USSL                     (0)
#define MICROPY_SSL_MBEDTLS                 (0)
#define MICROPY_PY_USSL_FINALISER           (0)
#define MICROPY_PY_UWEBSOCKET               (0)
#define MICROPY_PY_WEBREPL                  (0)
#define MICROPY_PY_ONEWIRE                  (1)
#define MICROPY_PY_UPLATFORM                (1)
// -define MICROPY_PY_USOCKET_EVENTS           (MICROPY_PY_WEBREPL)

// fatfs configuration
#define MICROPY_FATFS_ENABLE_LFN            (1)
#define MICROPY_FATFS_RPATH                 (2)
#define MICROPY_FATFS_MAX_SS                (4096)
#define MICROPY_FATFS_LFN_CODE_PAGE         437 /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */

#define MP_STATE_PORT MP_STATE_VM

// type definitions for the specific machine

#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p)))
void *esp_native_code_commit(void *, size_t, void *);
#define MP_PLAT_COMMIT_EXEC(buf, len, reloc) esp_native_code_commit(buf, len, reloc)
#define MP_SSIZE_MAX (0x7fffffff)

// Note: these "critical nested" macros do not ensure cross-CPU exclusion,
// the only disable interrupts on the current CPU.  To full manage exclusion
// one should use portENTER_CRITICAL/portEXIT_CRITICAL instead.
#include "freertos/FreeRTOS.h"
#define MICROPY_BEGIN_ATOMIC_SECTION() portENTER_CRITICAL_NESTED()
#define MICROPY_END_ATOMIC_SECTION(state) portEXIT_CRITICAL_NESTED(state)

#if MICROPY_PY_THREAD
#define MICROPY_EVENT_POLL_HOOK \
    do { \
        extern void mp_handle_pending(bool); \
        mp_handle_pending(true); \
        MP_THREAD_GIL_EXIT(); \
        ulTaskNotifyTake(pdFALSE, 1); \
        MP_THREAD_GIL_ENTER(); \
    } while (0);
#else
#define MICROPY_EVENT_POLL_HOOK \
    do { \
        extern void mp_handle_pending(bool); \
        mp_handle_pending(true); \
        MICROPY_PY_USOCKET_EVENTS_HANDLER \
        asm ("waiti 0"); \
    } while (0);
#endif

// Functions that should go in IRAM
#define MICROPY_WRAP_MP_BINARY_OP(f) IRAM_ATTR f
#define MICROPY_WRAP_MP_EXECUTE_BYTECODE(f) IRAM_ATTR f
#define MICROPY_WRAP_MP_LOAD_GLOBAL(f) IRAM_ATTR f
#define MICROPY_WRAP_MP_LOAD_NAME(f) IRAM_ATTR f
#define MICROPY_WRAP_MP_MAP_LOOKUP(f) IRAM_ATTR f
#define MICROPY_WRAP_MP_OBJ_GET_TYPE(f) IRAM_ATTR f
#define MICROPY_WRAP_MP_SCHED_EXCEPTION(f) IRAM_ATTR f
#define MICROPY_WRAP_MP_SCHED_KEYBOARD_INTERRUPT(f) IRAM_ATTR f

#define UINT_FMT "%u"
#define INT_FMT "%d"

typedef int32_t mp_int_t; // must be pointer size
typedef uint32_t mp_uint_t; // must be pointer size
typedef long mp_off_t;
// ssize_t, off_t as required by POSIX-signatured functions in stream.h
#include <sys/types.h>

// board specifics
#define MICROPY_PY_SYS_PLATFORM "esp32"

#define MICROPY_PY_NETWORK_LAN              (0)

#define MICROPY_PY_USOCKET_EVENTS_HANDLER

#ifndef MICROPY_BOARD_STARTUP
#define MICROPY_BOARD_STARTUP boardctrl_startup
#endif

void boardctrl_startup(void);

Sorry, I do not have time to write a document on how to customize your firmware. You just have to try to understand the build process. The build process for MicroPython changes all the time. A document for one version may not work for the next version.

@ftylitak
Copy link
Author

@shariltumin thank you very much for your valuable reply.

Following your settings/comments, I successfully compiled micropython 1.19.1 for ESP32 with your modules.

Your comment on the always changing build process of Micropython is very true. Nevertheless, I will post here the complete change list required to build a vanilla micropython v1.19.1 with the modules that you provide in case someone else needs it.

Changes to already existing files:

diff --git a/ports/esp32/main.c b/ports/esp32/main.c
index 14b7e14c6..5d924a4d1 100644
--- a/ports/esp32/main.c
+++ b/ports/esp32/main.c
@@ -215,7 +215,7 @@ soft_reset_exit:
     // TODO: machine_rmt_deinit_all();
     machine_pins_deinit();
     machine_deinit();
-    usocket_events_deinit();
+    //usocket_events_deinit();
 
     mp_deinit();
     fflush(stdout);
diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt
index 9ac6537f8..b9b2ff530 100644
--- a/ports/esp32/main/CMakeLists.txt
+++ b/ports/esp32/main/CMakeLists.txt
@@ -49,6 +49,8 @@ set(MICROPY_SOURCE_DRIVERS
 
 set(MICROPY_SOURCE_PORT
     ${PROJECT_DIR}/main.c
+    ${PROJECT_DIR}/bt_spp_client.c
+    ${PROJECT_DIR}/bt_spp_server.c
     ${PROJECT_DIR}/uart.c
     ${PROJECT_DIR}/usb.c
     ${PROJECT_DIR}/usb_serial_jtag.c
diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h
index 1877a0143..af0a6716c 100644
--- a/ports/esp32/mpconfigport.h
+++ b/ports/esp32/mpconfigport.h
@@ -60,7 +60,8 @@
 // control over Python builtins
 #define MICROPY_PY_STR_BYTES_CMP_WARN       (1)
 #define MICROPY_PY_ALL_INPLACE_SPECIAL_METHODS (1)
-#define MICROPY_PY_BUILTINS_HELP_TEXT       esp32_help_text
+#define MICROPY_PY_BUILTINS_HELP            (0)
+// -define MICROPY_PY_BUILTINS_HELP_TEXT       esp32_help_text
 #define MICROPY_PY_IO_BUFFEREDWRITER        (1)
 #define MICROPY_PY_UTIME_MP_HAL             (1)
 #define MICROPY_PY_THREAD                   (1)
@@ -68,16 +69,10 @@
 #define MICROPY_PY_THREAD_GIL_VM_DIVISOR    (32)
 
 // extended modules
-#ifndef MICROPY_PY_BLUETOOTH
-#define MICROPY_PY_BLUETOOTH                (1)
-#define MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE (1)
-#define MICROPY_BLUETOOTH_NIMBLE            (1)
-#define MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY (1)
-#endif
 #define MICROPY_PY_UTIMEQ                   (1)
-#define MICROPY_PY_UHASHLIB_SHA1            (1)
-#define MICROPY_PY_UHASHLIB_SHA256          (1)
-#define MICROPY_PY_UCRYPTOLIB               (1)
+#define MICROPY_PY_UHASHLIB_SHA1            (0)
+#define MICROPY_PY_UHASHLIB_SHA256          (0)
+#define MICROPY_PY_UCRYPTOLIB               (0)
 #define MICROPY_PY_URANDOM_SEED_INIT_FUNC   (esp_random())
 #define MICROPY_PY_UOS_INCLUDEFILE          "ports/esp32/moduos.c"
 #define MICROPY_PY_OS_DUPTERM               (1)
@@ -106,25 +101,19 @@
 #ifndef MICROPY_PY_MACHINE_I2S
 #define MICROPY_PY_MACHINE_I2S              (1)
 #endif
-#ifndef MICROPY_PY_NETWORK_WLAN
-#define MICROPY_PY_NETWORK_WLAN             (1)
-#endif
-#ifndef MICROPY_HW_ENABLE_SDCARD
+#define MICROPY_PY_NETWORK                  (0)
+#define MICROPY_PY_NETWORK_WLAN             (0)
 #define MICROPY_HW_ENABLE_SDCARD            (1)
-#endif
 #define MICROPY_HW_SOFTSPI_MIN_DELAY        (0)
 #define MICROPY_HW_SOFTSPI_MAX_BAUDRATE     (ets_get_cpu_frequency() * 1000000 / 200) // roughly
-#define MICROPY_PY_USSL                     (1)
-#define MICROPY_SSL_MBEDTLS                 (1)
-#define MICROPY_PY_USSL_FINALISER           (1)
-#define MICROPY_PY_UWEBSOCKET               (1)
-#define MICROPY_PY_WEBREPL                  (1)
-#define MICROPY_PY_BTREE                    (1)
+#define MICROPY_PY_USSL                     (0)
+#define MICROPY_SSL_MBEDTLS                 (0)
+#define MICROPY_PY_USSL_FINALISER           (0)
+#define MICROPY_PY_UWEBSOCKET               (0)
+#define MICROPY_PY_WEBREPL                  (0)
 #define MICROPY_PY_ONEWIRE                  (1)
 #define MICROPY_PY_UPLATFORM                (1)
-#define MICROPY_PY_USOCKET_EVENTS           (MICROPY_PY_WEBREPL)
-#define MICROPY_PY_BLUETOOTH_RANDOM_ADDR    (1)
-#define MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME ("ESP32")
+// -define MICROPY_PY_USOCKET_EVENTS           (MICROPY_PY_WEBREPL)
 
 // fatfs configuration
 #define MICROPY_FATFS_ENABLE_LFN            (1)
@@ -137,22 +126,12 @@
 #define MP_STATE_PORT MP_STATE_VM
 
 struct _machine_timer_obj_t;
-
-#if MICROPY_BLUETOOTH_NIMBLE
-struct mp_bluetooth_nimble_root_pointers_t;
-#define MICROPY_PORT_ROOT_POINTER_BLUETOOTH_NIMBLE struct _mp_bluetooth_nimble_root_pointers_t *bluetooth_nimble_root_pointers;
-#else
-#define MICROPY_PORT_ROOT_POINTER_BLUETOOTH_NIMBLE
-#endif
-
 #define MICROPY_PORT_ROOT_POINTERS \
     const char *readline_hist[8]; \
     mp_obj_t machine_pin_irq_handler[40]; \
     struct _machine_timer_obj_t *machine_timer_obj_head; \
     struct _machine_i2s_obj_t *machine_i2s_obj[I2S_NUM_MAX]; \
-    mp_obj_t native_code_pointers; \
-    MICROPY_PORT_ROOT_POINTER_BLUETOOTH_NIMBLE
-
+    mp_obj_t native_code_pointers;
 // type definitions for the specific machine
 
 #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p)))
@@ -167,18 +146,11 @@ void *esp_native_code_commit(void *, size_t, void *);
 #define MICROPY_BEGIN_ATOMIC_SECTION() portENTER_CRITICAL_NESTED()
 #define MICROPY_END_ATOMIC_SECTION(state) portEXIT_CRITICAL_NESTED(state)
 
-#if MICROPY_PY_USOCKET_EVENTS
-#define MICROPY_PY_USOCKET_EVENTS_HANDLER extern void usocket_events_handler(void); usocket_events_handler();
-#else
-#define MICROPY_PY_USOCKET_EVENTS_HANDLER
-#endif
-
 #if MICROPY_PY_THREAD
 #define MICROPY_EVENT_POLL_HOOK \
     do { \
         extern void mp_handle_pending(bool); \
         mp_handle_pending(true); \
-        MICROPY_PY_USOCKET_EVENTS_HANDLER \
         MP_THREAD_GIL_EXIT(); \
         ulTaskNotifyTake(pdFALSE, 1); \
         MP_THREAD_GIL_ENTER(); \
@@ -215,21 +187,12 @@ typedef long mp_off_t;
 // board specifics
 #define MICROPY_PY_SYS_PLATFORM "esp32"
 
-// ESP32-S3 extended IO for 47 & 48
-#ifndef MICROPY_HW_ESP32S3_EXTENDED_IO
-#define MICROPY_HW_ESP32S3_EXTENDED_IO      (1)
-#endif
+#define MICROPY_PY_NETWORK_LAN              (0)
 
-#ifndef MICROPY_HW_ENABLE_MDNS_QUERIES
-#define MICROPY_HW_ENABLE_MDNS_QUERIES      (1)
-#endif
-
-#ifndef MICROPY_HW_ENABLE_MDNS_RESPONDER
-#define MICROPY_HW_ENABLE_MDNS_RESPONDER    (1)
-#endif
+#define MICROPY_PY_USOCKET_EVENTS_HANDLER
 
 #ifndef MICROPY_BOARD_STARTUP
 #define MICROPY_BOARD_STARTUP boardctrl_startup
 #endif
 
-void boardctrl_startup(void);
+void boardctrl_startup(void);
\ No newline at end of file

New files:

  1. copied https://github.com/shariltumin/esp32-bluetooth-classic-micropython/tree/main/prod/src contents to micropython/ports/esp32
  2. created a folder micropython/ports/esp32/boards/ESP32_BT/ with the following 4 files:

boards/ESP32_BT/board.json

{
    "deploy": [
        "../deploy.md"
    ],
    "docs": "",
    "features": [
        "BT",
        "WiFi"
    ],
    "id": "esp32spiram_bt",
    "images": [
        "esp32_psram.jpg"
    ],
    "mcu": "esp32",
    "product": "ESP32_BT",
    "thumbnail": "",
    "url": "https://www.espressif.com/en/products/modules",
    "variants": {
        "idf3": "Compiled with IDF 3.x"
    },
    "vendor": "Espressif"
}

boards/ESP32_BT/mpconfigboard.cmake

set(SDKCONFIG_DEFAULTS
    boards/sdkconfig.base
    boards/sdkconfig.240mhz
    boards/ESP32_BT/sdkconfig.board
)

boards/ESP32_BT/mpconfigboard.h

#define MICROPY_HW_BOARD_NAME "ESP32_BT module"
#define MICROPY_HW_MCU_NAME "ESP32_BT"

boards/ESP32_BT/sdkconfig.board

# DEBUGGING
CONFIG_LOG_DEFAULT_LEVEL=none 

# Override some defaults so BT stack is enabled
CONFIG_BT_ENABLED=y
# CONFIG_BTDM_CONTROLLER_MODE_BLE_ONLY=
CONFIG_BTDM_CONTROLLER_MODE_BR_EDR_ONLY=y
# CONFIG_BTDM_CONTROLLER_MODE_BTDM=
CONFIG_CLASSIC_BT_ENABLED=y
CONFIG_BT_SPP_ENABLED=y
# disable Secure Simple Pairing
CONFIG_BT_SSP_ENABLED=
# Support ONE connection at a time
# don't work --> CONFIG_BTDM_CTRL_BR_EDR_MAX_ACL_CONN=1
# need to change esp_bt_gap_set_scan_mode to
# ESP_BT_NON_CONNECTABLE, ESP_BT_NON_DISCOVERABLE
# and WiFi is disabled
CONFIG_WIFI_ENABLED=n

CONFIG_BT_NIMBLE_ENABLED=n

And finally compiled with:

make BOARD=ESP32_BT

Thank you once again for your help!

@shariltumin
Copy link
Owner

Hi Nikos, nice! well done. May I suggest that you create a repository explaining what you have done. A lot of people might find it interesting. If you have one, I will link it from my landing page. It is ok if you do not have time and are not interested.

Sorry for the late reply

@shariltumin shariltumin reopened this Jul 20, 2023
@ftylitak
Copy link
Author

ftylitak commented Aug 7, 2023

Hello @shariltumin

my previous answer has the complete list of changes that need to be made on standard micropython 1.19.1 to enable your modules.

We have a branch of micropython 1.19.1 with these changes for our product which can be used directly:
https://github.com/insighio/micropython/tree/insighio-bluetooth-classic

by calling:

make BOARD=ESP32_BT_SPIRAM

The commit that added this functionality is:
insighio/micropython@210b816

Followed by the next commit that made a separation between ESP32_BT and ESP32_BT_SPIRAM for using the Classic Bluetooth for both ESP32 wroom and ESP32 wrover
insighio/micropython@086a8d4

Hope it helps :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants