Skip to content

Commit 1e9d45f

Browse files
author
LN882H Fix
committed
[lightning-ln882h] Add BLE SDK support gated on CFG_SUPPORT_BLE
LN882H is a WiFi+BLE combo SoC; this wires the BLE stack into the build system using the same queue.AddLibrary() pattern as the rest of the file. proj_config.h - Add CFG_SUPPORT_BLE 0 (default off; BLE builds enable it via custom_options.proj_config: "CFG_SUPPORT_BLE=1"). - Guard with #ifndef so the command-line -D from custom_options wins without a redefinition warning on every compiled file. builder/utils/config.py - Fix env_load_config parser: split(None,2) -> split(None,1) + skip bare defines (include guards, macros with no value). Old code crashed on #define _PROJ_CONFIG_H_ and multi-word values like ((size_t)(160*1024)). builder/utils/env.py - Add "proj_config": "proj_config.h" alias to env_parse_custom_options so custom_options.proj_config is recognised alongside the existing lwip / freertos aliases. builder/family/lightning-ln882h.py - Load proj_config.h via env.LoadConfig() (same pattern as beken-72xx.py). - Merge custom_options.proj_config overrides into env["CONFIG"] so env.Cfg("CFG_SUPPORT_BLE") reflects user overrides at build time. - New ln882h_ble library (active when CFG_SUPPORT_BLE=1): - 16 C sources from $SDK_DIR/components/ble/ (arch, port, GAP, GATT, connection/device manager, SMP, store, event, import). - +<.> include so #include "ble_arch/arch.h" resolves from base_dir. - All BLE SDK subdirectory include paths. - libln882h_ble_full_stack.a via -Wl,--whole-archive. - CFG_SUPPORT_BLE=1 in public CPPDEFINES. - Default ble_app_user_cfg.h in cores/lightning-ln882h/ble/ (Central role, no auto-adv, plain-brace BLE_DEFAULT_PUBLIC_ADDR to avoid a GNU compound expression expanding inside a brace-initialiser). Applications that prepend their own include path shadow this default automatically. Flash / RAM (generic-ln882hki, ESPHome switch firmware): CFG_SUPPORT_BLE=0: 530,640 B flash / 100,328 B RAM CFG_SUPPORT_BLE=1: 535,600 B flash / 100,768 B RAM (+4.9 KB / +440 B) libln882h_ble_full_stack.a is built with -ffunction-sections; --gc-sections strips BLE symbols not reached from the entry point, so the overhead in a build that actively uses the BLE stack will be correspondingly larger.
1 parent 9598759 commit 1e9d45f

5 files changed

Lines changed: 137 additions & 2 deletions

File tree

builder/family/lightning-ln882h.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@
1111
queue = env.AddLibraryQueue("lightning-ln882h")
1212
env.ConfigureFamily()
1313

14+
# Load chip config (CFG_SUPPORT_BLE, etc.) — same pattern as beken-72xx.py
15+
env.LoadConfig(join("$FAMILY_DIR", "base", "config", "proj_config.h"))
16+
# Merge any user overrides from custom_options.proj_config into CONFIG so
17+
# env.Cfg() reflects them (ParseCustomOptions already ran in base.py).
18+
_proj_overrides = (
19+
env.PioPlatform().custom_opts.get("options", {}).get("proj_config#h", {})
20+
)
21+
if _proj_overrides:
22+
env["CONFIG"].update(
23+
{
24+
k: int(v) if str(v).lstrip("-").isdigit() else v.encode()
25+
for k, v in _proj_overrides.items()
26+
}
27+
)
28+
1429
# Flags
1530
queue.AppendPublic(
1631
CCFLAGS=[
@@ -178,6 +193,95 @@
178193
),
179194
)
180195

196+
# Sources - BLE SDK
197+
# Compiled when CFG_SUPPORT_BLE=1. libln882h_ble_full_stack.a is built with
198+
# -ffunction-sections, so --gc-sections removes all BLE functions that are not
199+
# reachable from the application entry point. A WiFi-only build with no BLE
200+
# application code adds only ~5 KB (startup hooks); a build with active BLE
201+
# usage retains all scanner/GAP/GATT code it calls.
202+
if env.Cfg("CFG_SUPPORT_BLE"):
203+
queue.AddLibrary(
204+
name="ln882h_ble",
205+
base_dir=join("$SDK_DIR", "components", "ble"),
206+
srcs=[
207+
"+<ble_lib_import/ble_port.c>",
208+
"+<ble_arch/ble_arch_main.c>",
209+
"+<ble_profiles/prf_common/prf_ble.c>",
210+
"+<ble_profiles/prf_common/prf_utils.c>",
211+
"+<ble_app/ble_store/ln_ble_app_kv.c>",
212+
"+<ble_app/ble_event/ln_ble_event_manager.c>",
213+
"+<ble_app/ble_gap/gap_misc/ln_ble_gap.c>",
214+
"+<ble_app/ble_gap/gap_misc/ln_ble_gap_ind_handler.c>",
215+
"+<ble_app/ble_gap/gap_scan/ln_ble_scan.c>",
216+
"+<ble_app/ble_gap/gap_advertising/*.c>",
217+
"+<ble_app/ble_gatt/gatt_common/ln_ble_gatt.c>",
218+
"+<ble_app/ble_gatt/gatt_common/ln_ble_gatt_ind_handler.c>",
219+
"+<ble_app/ble_connection_manager/ln_ble_connection_manager.c>",
220+
"+<ble_app/ble_device_manager/*.c>",
221+
"+<ble_app/ble_smp/ln_ble_smp.c>",
222+
"+<ble_app/ble_import/ln_ble_rw_app_task.c>",
223+
],
224+
includes=[
225+
# base_dir itself so that #include "ble_arch/arch.h" resolves correctly
226+
"+<.>",
227+
"+<ble_arch>",
228+
"+<ble_lib_import>",
229+
"+<ble_profiles/prf_common>",
230+
"+<ble_app/ble_common>",
231+
"+<ble_app/ble_connection_manager>",
232+
"+<ble_app/ble_device_manager>",
233+
"+<ble_app/ble_event>",
234+
"+<ble_app/ble_gap/gap_advertising>",
235+
"+<ble_app/ble_gap/gap_misc>",
236+
"+<ble_app/ble_gap/gap_scan>",
237+
"+<ble_app/ble_gatt/gatt_client>",
238+
"+<ble_app/ble_gatt/gatt_common>",
239+
"+<ble_app/ble_gatt/gatt_server>",
240+
"+<ble_app/ble_import>",
241+
"+<ble_app/ble_smp>",
242+
"+<ble_app/ble_store>",
243+
"+<ble_app/ble_test>",
244+
"+<mac/ble/hl/api>",
245+
"+<mac/ble/hl/inc>",
246+
"+<mac/ble/ll/api>",
247+
"+<mac/ble/ll/import>",
248+
"+<mac/ble/ll/src>",
249+
"+<mac/ble/ll/src/llm>",
250+
"+<mac/em/api>",
251+
"+<mac/hci/api>",
252+
"+<mac/sch/api>",
253+
"+<mac/sch/import>",
254+
"+<modules/aes/api>",
255+
"+<modules/common/api>",
256+
"+<modules/dbg/api>",
257+
"+<modules/ecc_p256/api>",
258+
"+<modules/h4tl/api>",
259+
"+<modules/ke/api>",
260+
"+<modules/lib_ver/api>",
261+
"+<modules/nvds/api>",
262+
"+<modules/rf/api>",
263+
"+<modules/rwip/api>",
264+
],
265+
options=dict(
266+
CPPDEFINES=["LN882H_SDK", "CFG_SUPPORT_BLE=1"],
267+
CFLAGS=["-w"],
268+
),
269+
)
270+
# Provide a default ble_app_user_cfg.h so the SDK compiles without an
271+
# application-supplied header. Application components (e.g. ln882h_ble_tracker)
272+
# that prepend their own include path will shadow this default automatically.
273+
queue.AppendPublic(
274+
CPPPATH=[join("$FAMILY_DIR", "ble")],
275+
)
276+
queue.AppendPublic(
277+
CPPDEFINES=["CFG_SUPPORT_BLE=1"],
278+
LINKFLAGS=[
279+
"-Wl,--whole-archive",
280+
"-lln882h_ble_full_stack",
281+
"-Wl,--no-whole-archive",
282+
],
283+
)
284+
181285

182286
# Sources - FreeRTOS
183287
env.Replace(FREERTOS_PORT=env["FAMILY_NAME"], FREERTOS_PORT_DEFINE="LIGHTNING_LN882H")

builder/utils/config.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ def env_load_config(env: Environment, path: str):
5353
if not line.startswith("#define"):
5454
continue
5555
line = line[7:].strip(STRIP_CHARS)
56-
key, value = line.split(None, 2)
57-
value = value.strip(STRIP_CHARS)
56+
parts = line.split(None, 1)
57+
if len(parts) < 2:
58+
# bare define with no value (e.g. include guards, feature flags)
59+
continue
60+
key, value = parts[0], parts[1].strip(STRIP_CHARS)
5861
if value.isnumeric():
5962
value = int(value, 0)
6063
elif value.startswith('"') and value.endswith('"'):

builder/utils/env.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ def env_parse_custom_options(env: Environment, platform: PlatformBase):
138138
headers = {
139139
"lwip": "lwipopts.h",
140140
"freertos": "FreeRTOSConfig.h",
141+
"proj_config": "proj_config.h",
141142
}
142143
for header, options in list(opts.items()):
143144
if not isinstance(options, str):

cores/lightning-ln882h/base/config/proj_config.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@
5151
#define FLASH_IMAGE_VER_MINOR 1
5252
#define SOC_CRP_FLAG 0
5353

54+
/*
55+
* BLE support.
56+
* All LN882H variants have integrated BLE hardware. Set to 1 to include the
57+
* BLE SDK; leave at 0 for WiFi-only builds.
58+
* Guard allows custom_options.proj_config to override this via -D on the
59+
* command line without triggering a redefinition warning.
60+
*/
61+
#ifndef CFG_SUPPORT_BLE
62+
#define CFG_SUPPORT_BLE 0
63+
#endif
64+
5465
/*
5566
* Hardware config
5667
*/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
// Default BLE application user configuration for LibreTiny LN882H.
4+
// All settings here are defaults; ln_ble_app_default_cfg.h guards every value
5+
// with #if !defined, so applications that need different settings can prepend
6+
// their own include path and this file will be shadowed by their version.
7+
8+
// Observer / Central role — no advertising, scanning only.
9+
#define BLE_DEFAULT_ROLE BLE_ROLE_CENTRAL
10+
#define BLE_CONFIG_AUTO_ADV (0)
11+
12+
// Override the SDK default which uses a GNU compound expression ({...}) that
13+
// breaks when expanded inside a brace-initialiser: ln_bd_addr_t addr = {MACRO}.
14+
// Use a plain brace-list instead — the NIC bytes are a placeholder; applications
15+
// that need a real MAC must override this file via a higher-priority include path.
16+
#define BLE_DEFAULT_PUBLIC_ADDR {0x00, 0x50, 0xC2, 0x00, 0x00, 0x00}

0 commit comments

Comments
 (0)