Skip to content

Commit 59e3a1f

Browse files
ShenWeilongweiyuhannnn
authored andcommitted
feat(ble): Support for putting code in flash on ESP32-C2
(cherry picked from commit 192f13135488a2f8a386ace2dad69da59e81f5b5) Co-authored-by: Shen Weilong <shenweilong@espressif.com>
1 parent 8ea7ae7 commit 59e3a1f

File tree

13 files changed

+2833
-2307
lines changed

13 files changed

+2833
-2307
lines changed

components/bt/CMakeLists.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ if(CONFIG_BT_ENABLED)
8585
elseif(CONFIG_IDF_TARGET_ESP32S3)
8686
list(APPEND ldscripts "linker_rw_bt_controller.lf")
8787
elseif(CONFIG_IDF_TARGET_ESP32C2)
88+
if(CONFIG_BT_CTRL_RUN_IN_FLASH_ONLY)
89+
list(APPEND srcs "controller/esp32c2/dummy.c")
90+
endif()
8891
set(ldscripts "linker_esp32c2.lf")
8992
else()
9093
list(APPEND ldscripts "linker_esp_ble_controller.lf")
@@ -886,8 +889,13 @@ if(CONFIG_BT_ENABLED)
886889
add_prebuilt_library(libble_app
887890
"${CMAKE_CURRENT_LIST_DIR}/controller/lib_esp32c6/esp32c6-bt-lib/esp32c61/libble_app.a")
888891
else()
889-
add_prebuilt_library(libble_app
890-
"controller/lib_${target_name}/${target_name}-bt-lib/libble_app.a")
892+
if(CONFIG_BT_CTRL_RUN_IN_FLASH_ONLY)
893+
add_prebuilt_library(libble_app
894+
"controller/lib_${target_name}/${target_name}-bt-lib/libble_app_flash.a")
895+
else()
896+
add_prebuilt_library(libble_app
897+
"controller/lib_${target_name}/${target_name}-bt-lib/libble_app.a")
898+
endif()
891899
endif()
892900
target_link_libraries(${COMPONENT_LIB} PRIVATE libble_app)
893901
endif()

components/bt/controller/esp32c2/Kconfig.in

Lines changed: 110 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -109,85 +109,99 @@ menuconfig BT_LE_50_FEATURE_SUPPORT
109109
help
110110
Enable BLE 5 feature
111111

112-
config BT_LE_LL_CFG_FEAT_LE_2M_PHY
113-
bool "Enable 2M Phy"
114-
depends on BT_LE_50_FEATURE_SUPPORT
115-
default y
116-
help
117-
Enable 2M-PHY
118-
119-
config BT_LE_LL_CFG_FEAT_LE_CODED_PHY
120-
bool "Enable coded Phy"
121-
depends on BT_LE_50_FEATURE_SUPPORT
122-
default y
123-
help
124-
Enable coded-PHY
125-
126-
config BT_LE_EXT_ADV
127-
bool "Enable extended advertising"
128-
depends on BT_LE_50_FEATURE_SUPPORT
129-
default y
130-
help
131-
Enable this option to do extended advertising. Extended advertising
132-
will be supported from BLE 5.0 onwards.
133-
134-
if BT_LE_EXT_ADV
135-
config BT_LE_MAX_EXT_ADV_INSTANCES
136-
int "Maximum number of extended advertising instances."
137-
range 0 4
138-
default 1
139-
depends on BT_LE_EXT_ADV
140-
help
141-
Change this option to set maximum number of extended advertising
142-
instances. Minimum there is always one instance of
143-
advertising. Enter how many more advertising instances you
144-
want.
145-
Each extended advertising instance will take about 0.5k DRAM.
146-
147-
config BT_LE_EXT_ADV_MAX_SIZE
148-
int "Maximum length of the advertising data."
149-
range 0 1650
150-
default 1650
151-
depends on BT_LE_EXT_ADV
112+
if BT_LE_50_FEATURE_SUPPORT
113+
config BT_LE_LL_CFG_FEAT_LE_2M_PHY
114+
bool "Enable 2M Phy"
115+
depends on BT_LE_50_FEATURE_SUPPORT
116+
default y
152117
help
153-
Defines the length of the extended adv data. The value should not
154-
exceed 1650.
118+
Enable 2M-PHY
155119

156-
config BT_LE_ENABLE_PERIODIC_ADV
157-
bool "Enable periodic advertisement."
120+
config BT_LE_LL_CFG_FEAT_LE_CODED_PHY
121+
bool "Enable coded Phy"
122+
depends on BT_LE_50_FEATURE_SUPPORT
158123
default y
159-
depends on BT_LE_EXT_ADV
160124
help
161-
Enable this option to start periodic advertisement.
125+
Enable coded-PHY
162126

163-
config BT_LE_PERIODIC_ADV_SYNC_TRANSFER
164-
bool "Enable Transfer Sync Events"
165-
depends on BT_LE_ENABLE_PERIODIC_ADV
127+
config BT_LE_EXT_ADV
128+
bool "Enable extended advertising"
129+
depends on BT_LE_50_FEATURE_SUPPORT
166130
default y
167131
help
168-
This enables controller transfer periodic sync events to host
132+
Enable this option to do extended advertising. Extended advertising
133+
will be supported from BLE 5.0 onwards.
134+
135+
if BT_LE_EXT_ADV
136+
config BT_LE_MAX_EXT_ADV_INSTANCES
137+
int "Maximum number of extended advertising instances."
138+
range 0 4
139+
default 1
140+
depends on BT_LE_EXT_ADV
141+
help
142+
Change this option to set maximum number of extended advertising
143+
instances. Minimum there is always one instance of
144+
advertising. Enter how many more advertising instances you
145+
want.
146+
Each extended advertising instance will take about 0.5k DRAM.
147+
148+
config BT_LE_EXT_ADV_MAX_SIZE
149+
int "Maximum length of the advertising data."
150+
range 0 1650
151+
default 1650
152+
depends on BT_LE_EXT_ADV
153+
help
154+
Defines the length of the extended adv data. The value should not
155+
exceed 1650.
169156

170-
endif
157+
config BT_LE_ENABLE_PERIODIC_ADV
158+
bool "Enable periodic advertisement."
159+
default y
160+
depends on BT_LE_EXT_ADV
161+
help
162+
Enable this option to start periodic advertisement.
171163

172-
config BT_LE_MAX_PERIODIC_SYNCS
173-
int "Maximum number of periodic advertising syncs"
174-
depends on BT_LE_50_FEATURE_SUPPORT && !BT_NIMBLE_ENABLED
164+
config BT_LE_PERIODIC_ADV_SYNC_TRANSFER
165+
bool "Enable Transfer Sync Events"
166+
depends on BT_LE_ENABLE_PERIODIC_ADV
167+
default y
168+
help
169+
This enables controller transfer periodic sync events to host
170+
endif
175171

176-
range 0 3
177-
default 1 if BT_LE_ENABLE_PERIODIC_ADV
178-
default 0
179-
help
180-
Set this option to set the upper limit for number of periodic sync
181-
connections. This should be less than maximum connections allowed by
182-
controller.
172+
config BT_LE_EXT_SCAN
173+
bool "Enable extended scanning"
174+
depends on BT_LE_50_FEATURE_SUPPORT && BT_LE_ROLE_OBSERVER_ENABLE
175+
default y
176+
help
177+
Enable this option to do extended scanning.
183178

184-
config BT_LE_MAX_PERIODIC_ADVERTISER_LIST
185-
int "Maximum number of periodic advertiser list"
186-
depends on BT_LE_50_FEATURE_SUPPORT && !BT_NIMBLE_ENABLED
187-
range 1 5
188-
default 5
189-
help
190-
Set this option to set the upper limit for number of periodic advertiser list.
179+
config BT_LE_ENABLE_PERIODIC_SYNC
180+
bool "Enable periodic sync"
181+
default y
182+
depends on BT_LE_EXT_SCAN
183+
help
184+
Enable this option to receive periodic advertisement.
185+
186+
if BT_LE_ENABLE_PERIODIC_SYNC
187+
config BT_LE_MAX_PERIODIC_SYNCS
188+
int "Maximum number of periodic advertising syncs"
189+
range 0 3
190+
default 1 if BT_LE_ENABLE_PERIODIC_ADV
191+
default 0
192+
help
193+
Set this option to set the upper limit for number of periodic sync
194+
connections. This should be less than maximum connections allowed by
195+
controller.
196+
197+
config BT_LE_MAX_PERIODIC_ADVERTISER_LIST
198+
int "Maximum number of periodic advertiser list"
199+
range 1 5
200+
default 5
201+
help
202+
Set this option to set the upper limit for number of periodic advertiser list.
203+
endif
204+
endif
191205

192206
menu "Memory Settings"
193207
depends on !BT_NIMBLE_ENABLED
@@ -523,6 +537,10 @@ config BT_LE_TX_CCA_ENABLED
523537
help
524538
Enable CCA feature to cancel sending the packet if the signal power is stronger than CCA threshold.
525539

540+
config BT_LE_DTM_ENABLED
541+
bool "Enable Direct Test Mode (DTM) feature"
542+
default n
543+
526544
config BT_LE_CCA_RSSI_THRESH
527545
int "CCA RSSI threshold value"
528546
depends on BT_LE_TX_CCA_ENABLED
@@ -531,6 +549,10 @@ config BT_LE_CCA_RSSI_THRESH
531549
help
532550
Power threshold of CCA in unit of -1 dBm.
533551

552+
config BT_LE_FEAT_LL_ENCRYPTION
553+
bool "Enable controller ACL encryption"
554+
default y
555+
534556
config BT_LE_ROLE_CENTROL_ENABLE
535557
bool "Enable BLE Centrol role function"
536558
depends on !BT_NIMBLE_ENABLED
@@ -617,3 +639,23 @@ config BT_LE_DFT_TX_POWER_LEVEL_DBM_EFF
617639
default 18 if BT_LE_DFT_TX_POWER_LEVEL_P18
618640
default 20 if BT_LE_DFT_TX_POWER_LEVEL_P20
619641
default 0
642+
643+
config BT_CTRL_RUN_IN_FLASH_ONLY
644+
bool "Reduce BLE IRAM usage (READ DOCS FIRST) (EXPERIMENTAL)"
645+
default n
646+
help
647+
Move most IRAM into flash. This will increase the usage of flash and reduce ble performance.
648+
Because the code is moved to the flash, the execution speed of the code is reduced.
649+
To have a small impact on performance, you need to enable flash suspend (SPI_FLASH_AUTO_SUSPEND).
650+
651+
- Only one Tx-Rx can be performed in each connection interval. Therefore, reduce the connection interval
652+
as much as possible to improve the throughput. If you want higher connection performance, you can
653+
enable BT_LE_PLACE_CONN_RELATED_INTO_IRAM to put the connection-related code into iram.
654+
- For HCI_LE_Extended_Create_Connection command, only 1M phy's connection parameters will be applied.
655+
Other phys' will be ignored.
656+
- For extended scanning, we may be unable to receive the extended adv with 300us MAFS.
657+
658+
config BT_LE_PLACE_CONN_RELATED_INTO_IRAM
659+
bool "Place the connection-related code into IRAM"
660+
depends on BT_CTRL_RUN_IN_FLASH_ONLY
661+
default n

components/bt/controller/esp32c2/bt.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ extern int ble_get_npl_element_info(esp_bt_controller_config_t *cfg, ble_npl_cou
156156
extern void bt_track_pll_cap(void);
157157
extern char *ble_controller_get_compile_version(void);
158158
extern const char *r_ble_controller_get_rom_compile_version(void);
159+
#if CONFIG_BT_CTRL_RUN_IN_FLASH_ONLY
160+
extern void ble_ll_supported_features_init(void);
161+
#endif //CONFIG_BT_CTRL_RUN_IN_FLASH_ONLY
162+
159163
#if CONFIG_BT_RELEASE_IRAM
160164
extern uint32_t _iram_bt_text_start;
161165
extern uint32_t _bss_bt_end;
@@ -534,7 +538,12 @@ static int esp_ecc_gen_dh_key(const uint8_t *peer_pub_key_x, const uint8_t *peer
534538

535539
static int esp_intr_alloc_wrapper(int source, int flags, intr_handler_t handler, void *arg, void **ret_handle_in)
536540
{
541+
#if CONFIG_BT_CTRL_RUN_IN_FLASH_ONLY
542+
int rc = esp_intr_alloc(source, flags, handler, arg, (intr_handle_t *)ret_handle_in);
543+
#else
537544
int rc = esp_intr_alloc(source, flags | ESP_INTR_FLAG_IRAM, handler, arg, (intr_handle_t *)ret_handle_in);
545+
#endif
546+
538547
return rc;
539548
}
540549

@@ -743,6 +752,8 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
743752
return ret;
744753
}
745754

755+
/* If we place the ble code into flash, don't need to initialize ROM. */
756+
#if !CONFIG_BT_CTRL_RUN_IN_FLASH_ONLY
746757
#if DEFAULT_BT_LE_50_FEATURE_SUPPORT || DEFAULT_BT_LE_ROLE_CENTROL || DEFAULT_BT_LE_ROLE_OBSERVER
747758
extern int esp_ble_rom_func_ptr_init_all(void);
748759
esp_ble_rom_func_ptr_init_all();
@@ -751,6 +762,7 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
751762
extern int esp_ble_rom_func_ptr_init_legacy_adv_and_slave(void);
752763
esp_ble_rom_func_ptr_init_legacy_adv_and_slave();
753764
#endif
765+
#endif //!CONFIG_BT_CTRL_RUN_IN_FLASH_ONLY
754766

755767
/* Initialize the function pointers for OS porting */
756768
npl_freertos_funcs_init();
@@ -800,6 +812,11 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
800812
#if CONFIG_SW_COEXIST_ENABLE
801813
coex_init();
802814
#endif
815+
816+
#if CONFIG_BT_CTRL_RUN_IN_FLASH_ONLY
817+
ble_ll_supported_features_init();
818+
#endif //CONFIG_BT_CTRL_RUN_IN_FLASH_ONLY
819+
803820
ret = ble_controller_init(cfg);
804821
if (ret != ESP_OK) {
805822
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "ble_controller_init failed %d", ret);

0 commit comments

Comments
 (0)