nRF5340 Peripheral BLE connections always fail with reason 0x3e (62)

I solved this but the solution wasn't obvious and it didn't show up on google so I'm going to leave a quick writeup here.

nRF Connect SDK 1.7.1 on windows and linux

We have a custom board using the nRF5340 module from ublox (Nora-B1) testing using the stock peripheral_uart application. Board target nrf5340dk_nrf5340_cpuapp. It builds correctly for both cores and flashes, but hangs on startup.  'west attach' shows it looping waiting on the external crystal to come up, but we don't have an external crystal on this board.

I created <app>/boards/nrf5340dk_nrf5340_cpuapp.conf with the line CONFIG_SOC_ENABLE_LFXO=n, and did the same for hci_rpmsg/boards/nrf5340dk_nrf5340_cpunet.conf.  I also enabled the RTT debug overlays for both processors. The board started up, and "NORDIC_UART_SERVICE" showed up in the nRF Connect app.  Whenever I tried to connect to it, i was immediately disconnected.  debug log showed:

[00:00:40.568,634] <inf> peripheral_uart: Connected 55:54:83:BC:F7:2A (random)
...
[00:00:40.793,273] <dbg> bt_hci_core.hci_disconn_complete_prio: status 0x00 handle 15 reason 0x3e
[00:00:40.793,273] <dbg> bt_hci_core.hci_disconn_complete: status 0x00 handle 15 reason 0x3e
...
[00:00:40.793,273] <inf> peripheral_uart: Disconnected: 55:54:83:BC:F7:2A (random) (reason 62)

The configuration is misleading: While CONFIG_SOC_ENABLE_LFXO=n does skip the wait for the external crystal it does NOT correctly set the low frequency clock source.  The result is firmware that boots and advertises but cannot be connected to.

You have to use CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC=y in both your app and the stock hci_rpmsg configuration overlays. Or create a full board definition from scratch, but there's a bunch of pitfalls related to the tfm build process expecting specific boards so watch out if you go that route.

I hope this helps somebody!  Took me a few days to figure out because it ends up half-working so I was chasing my tail looking for potential signal problems with the external antenna.

Related