Skip to content

Commit 7248d3d

Browse files
authored
Make BLE work on ESP32-C2 with 26 MHz Xtal (#4062)
* Make BLE work on ESP32-C2 with 26 MHz Xtal * CHANGELOG.md
1 parent 445f6fc commit 7248d3d

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

esp-hal/src/clock/clocks_ll/esp32c2.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,14 @@ pub(super) fn ble_rtc_clk_init() {
188188
w.lp_timer_sel_rtc_slow().clear_bit()
189189
});
190190

191-
// assume 40MHz xtal
191+
let divider = match crate::rtc_cntl::RtcClock::xtal_freq() {
192+
XtalClock::_26M => 129,
193+
XtalClock::_40M => 249,
194+
};
195+
192196
MODEM_CLKRST::regs()
193197
.modem_lp_timer_conf()
194-
.modify(|_, w| unsafe { w.lp_timer_clk_div_num().bits(249) });
198+
.modify(|_, w| unsafe { w.lp_timer_clk_div_num().bits(divider) });
195199

196200
MODEM_CLKRST::regs().etm_clk_conf().modify(|_, w| {
197201
w.etm_clk_active().set_bit();

esp-radio/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4747
- Fixed a BLE panic caused by unimplemented functions (#3762)
4848
- Fixed the BLE stack crashing in certain cases (#3854)
4949
- `ADC2` now cannot be used simultaneously with `radio` on ESP32 (#3876)
50+
- BLE on ESP32-C2 with 26MHz xtal (#4062)
5051

5152
### Removed
5253

esp-radio/src/ble/npl.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,8 +1055,27 @@ pub(crate) fn ble_init() {
10551055

10561056
self::ble_os_adapter_chip_specific::ble_rtc_clk_init();
10571057

1058+
#[cfg(esp32c2)]
1059+
let mut cfg = ble_os_adapter_chip_specific::BLE_CONFIG;
1060+
1061+
#[cfg(not(esp32c2))]
10581062
let cfg = ble_os_adapter_chip_specific::BLE_CONFIG;
10591063

1064+
#[cfg(esp32c2)]
1065+
{
1066+
use esp_hal::clock::Clock;
1067+
1068+
let xtal = crate::hal::rtc_cntl::RtcClock::xtal_freq();
1069+
let mhz = xtal.mhz() as u8;
1070+
1071+
cfg.main_xtal_freq = mhz;
1072+
1073+
if mhz == 26 {
1074+
cfg.rtc_freq = 40000;
1075+
cfg.main_xtal_freq = 26;
1076+
}
1077+
}
1078+
10601079
let res = esp_register_ext_funcs(&G_OSI_FUNCS as *const ExtFuncsT);
10611080
if res != 0 {
10621081
panic!("esp_register_ext_funcs returned {}", res);

0 commit comments

Comments
 (0)