|
39 | 39 | #include "supervisor/usb.h" |
40 | 40 | #include "shared-bindings/bleio/Adapter.h" |
41 | 41 | #include "shared-bindings/bleio/Address.h" |
| 42 | +#include "mpconfigboard.h" // for BOARD_HAS_CRYSTAL |
42 | 43 |
|
43 | 44 | STATIC void softdevice_assert_handler(uint32_t id, uint32_t pc, uint32_t info) { |
44 | 45 | mp_raise_msg_varg(&mp_type_AssertionError, |
45 | 46 | translate("Soft device assert, id: 0x%08lX, pc: 0x%08lX"), id, pc); |
46 | 47 | } |
47 | 48 |
|
| 49 | +static inline bool board_has_crystal(void) { |
| 50 | +#ifdef BOARD_HAS_CRYSTAL |
| 51 | + return BOARD_HAS_CRYSTAL == 1; |
| 52 | +#else |
| 53 | + return false; |
| 54 | +#endif |
| 55 | +} |
| 56 | + |
48 | 57 | STATIC uint32_t ble_stack_enable(void) { |
49 | | - nrf_clock_lf_cfg_t clock_config = { |
50 | | - .source = NRF_CLOCK_LF_SRC_XTAL, |
51 | | - .accuracy = NRF_CLOCK_LF_ACCURACY_20_PPM |
52 | | - }; |
| 58 | + nrf_clock_lf_cfg_t clock_config; |
| 59 | + |
| 60 | + // Set low-frequency clock source to be either an external 32.768kHz crystal if one exists on the board |
| 61 | + // or an internal 32.768 kHz RC oscillator otherwise |
| 62 | + if (board_has_crystal()) { |
| 63 | + clock_config = (nrf_clock_lf_cfg_t){ |
| 64 | + .source = NRF_CLOCK_LF_SRC_XTAL, |
| 65 | + .accuracy = NRF_CLOCK_LF_ACCURACY_20_PPM}; |
| 66 | + } else { |
| 67 | + clock_config = (nrf_clock_lf_cfg_t){ |
| 68 | + .source = NRF_CLOCK_LF_SRC_RC, |
| 69 | + .rc_ctiv = 16, |
| 70 | + .rc_temp_ctiv = 2, |
| 71 | + .accuracy = NRF_CLOCK_LF_ACCURACY_250_PPM}; |
| 72 | + } |
53 | 73 |
|
54 | 74 | uint32_t err_code = sd_softdevice_enable(&clock_config, softdevice_assert_handler); |
55 | 75 | if (err_code != NRF_SUCCESS) |
|
0 commit comments