Skip to content

Commit 18f441a

Browse files
authored
Merge pull request #2083 from dhalbert/no-32khz-xtal
Fix CPBlue LFCLKSRC; CPB has no status neopixel
2 parents 6aa311a + 0b7291d commit 18f441a

File tree

5 files changed

+27
-40
lines changed

5 files changed

+27
-40
lines changed

ports/nrf/boards/circuitplayground_bluefruit/mpconfigboard.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434
#define FLASH_SIZE (0x100000)
3535
#define FLASH_PAGE_SIZE (4096)
3636

37-
#define MICROPY_HW_NEOPIXEL (&pin_P0_13)
38-
3937
#define MICROPY_HW_LED_STATUS (&pin_P1_14)
4038

39+
// Unusually, board does not have a 32 kHz xtal. Nearly all boards do.
40+
#define BOARD_HAS_32KHZ_XTAL (0)
41+
4142
#if QSPI_FLASH_FILESYSTEM
4243
#define MICROPY_QSPI_DATA0 NRF_GPIO_PIN_MAP(0, 21)
4344
#define MICROPY_QSPI_DATA1 NRF_GPIO_PIN_MAP(0, 23)
@@ -60,8 +61,6 @@
6061

6162
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE)
6263

63-
#define BOARD_HAS_CRYSTAL 1
64-
6564
#define DEFAULT_I2C_BUS_SCL (&pin_P0_04)
6665
#define DEFAULT_I2C_BUS_SDA (&pin_P0_05)
6766

ports/nrf/common-hal/bleio/Adapter.c

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,26 @@
3939
#include "supervisor/usb.h"
4040
#include "shared-bindings/bleio/Adapter.h"
4141
#include "shared-bindings/bleio/Address.h"
42-
#include "mpconfigboard.h" // for BOARD_HAS_CRYSTAL
4342

4443
STATIC void softdevice_assert_handler(uint32_t id, uint32_t pc, uint32_t info) {
4544
mp_raise_msg_varg(&mp_type_AssertionError,
4645
translate("Soft device assert, id: 0x%08lX, pc: 0x%08lX"), id, pc);
4746
}
4847

49-
static inline bool board_has_crystal(void) {
50-
#ifdef BOARD_HAS_CRYSTAL
51-
return BOARD_HAS_CRYSTAL == 1;
48+
STATIC uint32_t ble_stack_enable(void) {
49+
nrf_clock_lf_cfg_t clock_config = {
50+
#if BOARD_HAS_32KHZ_XTAL
51+
.source = NRF_CLOCK_LF_SRC_XTAL,
52+
.rc_ctiv = 0,
53+
.rc_temp_ctiv = 0,
54+
.accuracy = NRF_CLOCK_LF_ACCURACY_20_PPM,
5255
#else
53-
return false;
56+
.source = NRF_CLOCK_LF_SRC_RC,
57+
.rc_ctiv = 16,
58+
.rc_temp_ctiv = 2,
59+
.accuracy = NRF_CLOCK_LF_ACCURACY_250_PPM,
5460
#endif
55-
}
56-
57-
STATIC uint32_t ble_stack_enable(void) {
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-
}
61+
};
7362

7463
uint32_t err_code = sd_softdevice_enable(&clock_config, softdevice_assert_handler);
7564
if (err_code != NRF_SUCCESS)

ports/nrf/common-hal/bleio/Characteristic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self,
183183
(self->props & CHAR_PROP_WRITE_NO_RESPONSE));
184184
} else {
185185
if (self->fixed_length && bufinfo->len != self->max_length) {
186-
mp_raise_ValueError(translate("Value length required fixed length"));
186+
mp_raise_ValueError(translate("Value length != required fixed length"));
187187
}
188188
if (bufinfo->len > self->max_length) {
189189
mp_raise_ValueError(translate("Value length > max_length"));

ports/nrf/mpconfigport.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@
5353

5454
#include "py/circuitpy_mpconfig.h"
5555

56+
#ifndef BOARD_HAS_32KHZ_XTAL
57+
// Assume crystal is present, which is the most common case.
58+
#define BOARD_HAS_32KHZ_XTAL (1)
59+
#endif
60+
5661
#define MICROPY_PORT_ROOT_POINTERS \
5762
CIRCUITPY_COMMON_ROOT_POINTERS \
5863
ble_drv_evt_handler_entry_t* ble_drv_evt_handler_entries; \

ports/nrf/peripherals/nrf/clocks.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,15 @@
2626
*/
2727

2828
#include "nrfx.h"
29-
#include "mpconfigboard.h" // for BOARD_HAS_CRYSTAL
29+
#include "mpconfigport.h"
3030

31-
static inline bool board_has_crystal(void) {
32-
#ifdef BOARD_HAS_CRYSTAL
33-
return BOARD_HAS_CRYSTAL == 1;
31+
void nrf_peripherals_clocks_init(void) {
32+
33+
#if BOARD_HAS_32KHZ_XTAL
34+
NRF_CLOCK->LFCLKSRC = (uint32_t)((CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos) & CLOCK_LFCLKSRC_SRC_Msk);
3435
#else
35-
return false;
36+
NRF_CLOCK->LFCLKSRC = (uint32_t)((CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos) & CLOCK_LFCLKSRC_SRC_Msk);
3637
#endif
37-
}
38-
39-
void nrf_peripherals_clocks_init(void) {
40-
// Set low-frequency clock source to be either an external 32.768kHz crystal if one exists on the board
41-
// or an internal 32.768 kHz RC oscillator otherwise
42-
uint32_t clock_src = board_has_crystal() ? CLOCK_LFCLKSRC_SRC_Xtal : CLOCK_LFCLKSRC_SRC_RC;
43-
NRF_CLOCK->LFCLKSRC = (uint32_t)((clock_src << CLOCK_LFCLKSRC_SRC_Pos) & CLOCK_LFCLKSRC_SRC_Msk);
4438
NRF_CLOCK->TASKS_LFCLKSTART = 1UL;
4539

4640
// Wait for clocks to start.

0 commit comments

Comments
 (0)