Skip to content

refactor(ble): Improve power consumption, code structure and documentation of subrating config - #1077

Merged
HaoboGu merged 6 commits into
rmk-rs:mainfrom
tompre:subrating
Aug 21, 2026
Merged

refactor(ble): Improve power consumption, code structure and documentation of subrating config#1077
HaoboGu merged 6 commits into
rmk-rs:mainfrom
tompre:subrating

Conversation

@tompre

@tompre tompre commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

This pull request:

  • adds detailed current draw measurements for central and peripheral during sleep.
  • reduces the subrate factor for host-disconnected sleep from 250 to 125 to balance power consumption between central and peripheral, according to the measurements.
  • Add const functions to calculate the optimal latency and timeout parameters for the connections.

@HaoboGu : Sorry for taking so long to respond to the comments in #1008, now the pull request is complete

tompre added 2 commits August 20, 2026 18:33
Add a table in `central.rs` detailing the current draw measurements for
different host connection latency and split connection subrate factors.

Extract the hardcoded max latency and supervision timeouts into `const fn`
helpers that automatically calculate the optimal values based on the
BLE specification limits.
@github-actions

github-actions Bot commented Aug 20, 2026

Copy link
Copy Markdown

Size Report

Example main PR Diff .text .data .bss
use_config/nrf52832_ble 399.5 KiB 399.5 KiB +0.00% ⬇️ -4 0 0
use_config/nrf52840_ble 441.5 KiB 441.5 KiB +0.00% ⬆️ +4 0 0
use_config/nrf52840_ble_split (central) 511.0 KiB 510.9 KiB -0.01% ⬇️ -72 0 0
use_config/nrf52840_ble_split (peripheral) 320.8 KiB 320.8 KiB +0.00% 0 0 0
use_config/pi_pico_w_ble 674.8 KiB 674.8 KiB +0.00% ⬆️ +4 0 0
use_config/rp2040 146.0 KiB 146.0 KiB +0.00% ⬆️ +4 0 0
use_config/rp2040_split (central) 159.6 KiB 159.6 KiB +0.00% ⬆️ +4 0 0
use_config/rp2040_split (peripheral) 27.8 KiB 27.8 KiB +0.00% 0 0 0
use_config/stm32f1 62.5 KiB 62.5 KiB +0.00% ⬆️ +4 0 0
use_config/stm32h7 100.7 KiB 100.7 KiB +0.00% ⬆️ +4 0 0
use_rust/nrf52832_ble 387.6 KiB 387.6 KiB +0.00% ⬇️ -4 0 0
use_rust/nrf52840_ble 436.9 KiB 436.9 KiB +0.00% 0 0 0
use_rust/nrf52840_ble_split (central) 510.3 KiB 510.3 KiB +0.00% ⬆️ +36 0 0
use_rust/nrf52840_ble_split (peripheral) 316.7 KiB 316.6 KiB +0.00% ⬇️ -4 0 0
use_rust/pi_pico_w_ble 674.2 KiB 674.2 KiB +0.00% ⬇️ -4 0 0
use_rust/rp2040 145.5 KiB 145.5 KiB +0.00% 0 0 0
use_rust/rp2040_split (central) 158.3 KiB 158.3 KiB +0.00% ⬇️ -4 0 0
use_rust/rp2040_split (peripheral) 28.1 KiB 28.1 KiB +0.00% 0 0 0
use_rust/stm32f1 61.7 KiB 61.7 KiB +0.00% 0 0 0
use_rust/stm32h7 119.1 KiB 119.1 KiB +0.00% 0 0 0
use_config/nrf52832_ble — 399.5 KiB → 399.5 KiB (+0.00% ⬇️)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
 366468	   8620	  33952	 409040	  63dd0	rmk-nrf52832

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
 366472	   8620	  33952	 409044	  63dd4	rmk-nrf52832

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%    +118  [ = ]       0    .debug_loc
  +0.0%     +61  [ = ]       0    .debug_info
  +0.0%     +33  [ = ]       0    .debug_str
  +0.0%     +14  [ = ]       0    .debug_line
  +4.9%      +3  [ = ]       0    [Unmapped]
  -0.0%      -4  -0.0%      -4    .text
  -0.0%     -21  [ = ]       0    .strtab
  +0.0%    +204  -0.0%      -4    TOTAL
use_config/nrf52840_ble — 441.5 KiB → 441.5 KiB (+0.00% ⬆️)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
 399504	   8628	  43984	 452116	  6e614	rmk-nrf52840

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
 399500	   8628	  43984	 452112	  6e610	rmk-nrf52840

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%     +98  [ = ]       0    .debug_loc
  +0.0%     +51  [ = ]       0    .debug_line
  +0.0%      +4  +0.0%      +4    .text
  -0.0%     -83  [ = ]       0    .debug_info
  -0.1%    -473  [ = ]       0    .strtab
  -0.1% -4.44Ki  [ = ]       0    .debug_str
  -0.1% -4.83Ki  +0.0%      +4    TOTAL
use_config/nrf52840_ble_split (central) — 511.0 KiB → 510.9 KiB (-0.01% ⬇️)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
 464444	   8684	  50072	 523200	  7fbc0	central

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
 464516	   8684	  50072	 523272	  7fc08	central

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%    +553  [ = ]       0    .debug_str
  +0.0%    +188  [ = ]       0    .debug_info
  +0.0%     +96  [ = ]       0    .debug_ranges
   +11%      +7  [ = ]       0    [Unmapped]
  -0.0%      -6  [ = ]       0    .strtab
  -0.0%     -16  -0.0%     -16    .rodata
  -0.0%     -56  -0.0%     -56    .text
  -0.0%    -186  [ = ]       0    .debug_line
  -0.1%    -460  [ = ]       0    .debug_loc
  +0.0%    +120  -0.0%     -72    TOTAL
use_config/nrf52840_ble_split (peripheral) — 320.8 KiB → 320.8 KiB (+0.00%)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
 294636	   8508	  25328	 328472	  50318	peripheral

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
 294636	   8508	  25328	 328472	  50318	peripheral

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%      +6  [ = ]       0    .debug_info
  -0.0%      -3  [ = ]       0    .debug_str
  -0.0%      -5  [ = ]       0    .strtab
  -0.0%     -10  [ = ]       0    .debug_loc
  -0.0%     -12  [ = ]       0    TOTAL
use_config/pi_pico_w_ble — 674.8 KiB → 674.8 KiB (+0.00% ⬆️)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
 636956	      0	  54016	 690972	  a8b1c	rmk-pi-pico-w

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
 636952	      0	  54016	 690968	  a8b18	rmk-pi-pico-w

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.1% +4.97Ki  [ = ]       0    .debug_str
  +0.2%    +664  [ = ]       0    .strtab
  +0.0%    +180  [ = ]       0    .debug_loc
  +0.0%      +4  +0.0%      +4    .text
  +0.0%      +2  [ = ]       0    .debug_line
 -11.5%      -7  [ = ]       0    [Unmapped]
  -0.0%     -11  [ = ]       0    .debug_info
  +0.1% +5.79Ki  +0.0%      +4    TOTAL
use_config/rp2040 — 146.0 KiB → 146.0 KiB (+0.00% ⬆️)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
 135608	      0	  13900	 149508	  24804	rmk-rp2040

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
 135604	      0	  13900	 149504	  24800	rmk-rp2040

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%      +7  [ = ]       0    .strtab
  +0.0%      +6  [ = ]       0    .debug_info
  +0.0%      +4  +0.0%      +4    .text
  -3.4%      -2  [ = ]       0    [Unmapped]
  -0.0%     -29  [ = ]       0    .debug_str
  -0.1%    -182  [ = ]       0    .debug_line
  -0.0%    -196  +0.0%      +4    TOTAL
use_config/rp2040_split (central) — 159.6 KiB → 159.6 KiB (+0.00% ⬆️)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
 148620	      0	  14844	 163464	  27e88	central

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
 148616	      0	  14844	 163460	  27e84	central

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%      +4  +0.0%      +4    .text
  +0.0%      +3  [ = ]       0    .debug_info
  -0.0%      -1  [ = ]       0    .strtab
 -23.3%     -10  [ = ]       0    [Unmapped]
  -0.0%     -69  [ = ]       0    .debug_str
  -0.1%    -143  [ = ]       0    .debug_line
  -0.0%    -216  +0.0%      +4    TOTAL
use_config/rp2040_split (peripheral) — 27.8 KiB → 27.8 KiB (+0.00%)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
  25592	     60	   2804	  28456	   6f28	peripheral

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
  25592	     60	   2804	  28456	   6f28	peripheral

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  [ = ]       0  [ = ]       0    TOTAL
use_config/stm32f1 — 62.5 KiB → 62.5 KiB (+0.00% ⬆️)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
  57572	     28	   6360	  63960	   f9d8	rmk-stm32f1

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
  57568	     28	   6360	  63956	   f9d4	rmk-stm32f1

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%      +4  +0.0%      +4    .text
  -7.4%      -4  [ = ]       0    [Unmapped]
  -0.0%      -9  [ = ]       0    .strtab
  -0.0%     -52  [ = ]       0    .debug_info
  -0.0%     -63  [ = ]       0    .debug_str
  -0.0%    -124  +0.0%      +4    TOTAL
use_config/stm32h7 — 100.7 KiB → 100.7 KiB (+0.00% ⬆️)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
  94724	    264	   8112	 103100	  192bc	rmk-stm32h7

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
  94720	    264	   8112	 103096	  192b8	rmk-stm32h7

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%      +4  +0.0%      +4    .text
 -19.0%      -8  [ = ]       0    [Unmapped]
  -0.0%      -9  [ = ]       0    .strtab
  -0.0%     -52  [ = ]       0    .debug_info
  -0.0%     -63  [ = ]       0    .debug_str
  -0.0%    -128  +0.0%      +4    TOTAL
use_rust/nrf52832_ble — 387.6 KiB → 387.6 KiB (+0.00% ⬇️)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
 354336	   8620	  33896	 396852	  60e34	rmk-nrf52832

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
 354340	   8620	  33896	 396856	  60e38	rmk-nrf52832

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%     +98  [ = ]       0    .debug_loc
  +0.0%     +61  [ = ]       0    .debug_info
  +0.0%     +15  [ = ]       0    .debug_line
  +9.4%      +6  [ = ]       0    [Unmapped]
  -0.0%      -4  -0.0%      -4    .text
  -0.2%    -468  [ = ]       0    .strtab
  -0.1% -3.65Ki  [ = ]       0    .debug_str
  -0.1% -3.93Ki  -0.0%      -4    TOTAL
use_rust/nrf52840_ble — 436.9 KiB → 436.9 KiB (+0.00%)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
 397588	   8628	  41216	 447432	  6d3c8	rmk-nrf52840

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
 397588	   8628	  41216	 447432	  6d3c8	rmk-nrf52840

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%    +118  [ = ]       0    .debug_loc
  +0.0%     +95  [ = ]       0    .debug_line
  +0.0%     +62  [ = ]       0    .debug_info
  +0.5%     +39  [ = ]       0    .debug_abbrev
  +0.0%     +32  [ = ]       0    .strtab
  -0.0%     -30  [ = ]       0    .debug_str
  +0.0%    +316  [ = ]       0    TOTAL
use_rust/nrf52840_ble_split (central) — 510.3 KiB → 510.3 KiB (+0.00% ⬆️)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
 462416	   8684	  51488	 522588	  7f95c	central

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
 462380	   8684	  51488	 522552	  7f938	central

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%    +544  [ = ]       0    .debug_str
  +0.0%    +188  [ = ]       0    .debug_info
  +0.0%     +52  +0.0%     +52    .text
  -8.5%      -5  [ = ]       0    [Unmapped]
  -0.0%     -13  [ = ]       0    .debug_line
  -0.0%     -15  [ = ]       0    .strtab
  -0.0%     -16  -0.0%     -16    .rodata
  -0.0%     -32  [ = ]       0    .symtab
  -0.1%    -240  [ = ]       0    .debug_ranges
  -0.0%    -387  [ = ]       0    .debug_loc
  +0.0%     +76  +0.0%     +36    TOTAL
use_rust/nrf52840_ble_split (peripheral) — 316.7 KiB → 316.6 KiB (+0.00% ⬇️)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
 290780	   8508	  24960	 324248	  4f298	peripheral

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
 290784	   8508	  24960	 324252	  4f29c	peripheral

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%    +238  [ = ]       0    .debug_info
  +0.0%    +156  [ = ]       0    .debug_str
  +0.0%     +98  [ = ]       0    .debug_line
  +5.7%      +3  [ = ]       0    [Unmapped]
  -0.0%      -1  [ = ]       0    .strtab
  -0.0%      -4  -0.0%      -4    .text
  -0.0%     -10  [ = ]       0    .debug_loc
  +0.0%    +480  -0.0%      -4    TOTAL
use_rust/pi_pico_w_ble — 674.2 KiB → 674.2 KiB (+0.00% ⬇️)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
 636660	      0	  53696	 690356	  a88b4	rmk-pi-pico-w

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
 636664	      0	  53696	 690360	  a88b8	rmk-pi-pico-w

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%    +180  [ = ]       0    .debug_loc
  +7.0%      +4  [ = ]       0    [Unmapped]
  -0.0%      -2  [ = ]       0    .debug_line
  -0.0%      -4  -0.0%      -4    .text
  -0.0%      -6  [ = ]       0    .debug_info
  -0.0%     -15  [ = ]       0    .strtab
  -0.0%     -37  [ = ]       0    .debug_str
  +0.0%    +120  -0.0%      -4    TOTAL
use_rust/rp2040 — 145.5 KiB → 145.5 KiB (+0.00%)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
 135268	      0	  13732	 149000	  24608	rmk-rp2040

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
 135268	      0	  13732	 149000	  24608	rmk-rp2040

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%     +14  [ = ]       0    .debug_line
  -0.0%      -2  [ = ]       0    .debug_loc
 -11.1%      -5  [ = ]       0    [Unmapped]
  -0.0%      -8  [ = ]       0    .strtab
  -0.0%     -43  [ = ]       0    .debug_info
  -0.0%     -72  [ = ]       0    .debug_str
  -0.0%    -116  [ = ]       0    TOTAL
use_rust/rp2040_split (central) — 158.3 KiB → 158.3 KiB (+0.00% ⬇️)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
 147512	      0	  14588	 162100	  27934	central

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
 147516	      0	  14588	 162104	  27938	central

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%    +816  [ = ]       0    .debug_str
  +0.0%     +60  [ = ]       0    .strtab
  +0.0%     +10  [ = ]       0    .debug_loc
   +12%      +6  [ = ]       0    [Unmapped]
  -0.0%      -4  -0.0%      -4    .text
  +0.0%    +888  -0.0%      -4    TOTAL
use_rust/rp2040_split (peripheral) — 28.1 KiB → 28.1 KiB (+0.00%)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
  25692	     60	   3068	  28820	   7094	peripheral

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
  25692	     60	   3068	  28820	   7094	peripheral

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.0%     +92  [ = ]       0    .debug_str
  +0.0%     +92  [ = ]       0    TOTAL
use_rust/stm32f1 — 61.7 KiB → 61.7 KiB (+0.00%)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
  56904	     28	   6280	  63212	   f6ec	rmk-stm32f1

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
  56904	     28	   6280	  63212	   f6ec	rmk-stm32f1

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  [ = ]       0  [ = ]       0    TOTAL
use_rust/stm32h7 — 119.1 KiB → 119.1 KiB (+0.00%)

cargo size (PR):

   text	   data	    bss	    dec	    hex	filename
 107624	    320	  14012	 121956	  1dc64	rmk-stm32h7

cargo size (main):

   text	   data	    bss	    dec	    hex	filename
 107624	    320	  14012	 121956	  1dc64	rmk-stm32h7

Bloaty diff (PR vs main):

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  +0.1% +3.56Ki  [ = ]       0    .debug_str
  +0.4%    +398  [ = ]       0    .strtab
  +0.0%     +10  [ = ]       0    .debug_loc
  +4.9%      +2  [ = ]       0    [Unmapped]
  +0.1% +3.96Ki  [ = ]       0    TOTAL

Comment thread rmk/src/ble/mod.rs Outdated
@HaoboGu

HaoboGu commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

Nice, thanks a lot!

@HaoboGu
HaoboGu merged commit 6fa9805 into rmk-rs:main Aug 21, 2026
7 checks passed
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

Successfully merging this pull request may close these issues.

2 participants