Skip to content

Commit

Permalink
Allow lengthening xosc startup delay with a compile option (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhalbert authored Jun 1, 2021
1 parent 42cbdcb commit d026118
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/boards/include/boards/adafruit_feather_rp2040.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
// For board detection
#define ADAFRUIT_FEATHER_RP2040

// On some samples, the xosc can take longer to stabilize than is usual
#ifndef PICO_XOSC_STARTUP_DELAY_MULTIPLIER
#define PICO_XOSC_STARTUP_DELAY_MULTIPLIER 64
#endif

//------------- UART -------------//
#ifndef PICO_DEFAULT_UART
#define PICO_DEFAULT_UART 0
Expand Down
5 changes: 5 additions & 0 deletions src/boards/include/boards/adafruit_itsybitsy_rp2040.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
// For board detection
#define ADAFRUIT_ITSYBITSY_RP2040

// On some samples, the xosc can take longer to stabilize than is usual
#ifndef PICO_XOSC_STARTUP_DELAY_MULTIPLIER
#define PICO_XOSC_STARTUP_DELAY_MULTIPLIER 64
#endif

//------------- UART -------------//
#ifndef PICO_DEFAULT_UART
#define PICO_DEFAULT_UART 0
Expand Down
5 changes: 5 additions & 0 deletions src/boards/include/boards/adafruit_qtpy_rp2040.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
// For board detection
#define ADAFRUIT_QTPY_RP2040

// On some samples, the xosc can take longer to stabilize than is usual
#ifndef PICO_XOSC_STARTUP_DELAY_MULTIPLIER
#define PICO_XOSC_STARTUP_DELAY_MULTIPLIER 64
#endif

//------------- UART -------------//
#ifndef PICO_DEFAULT_UART
#define PICO_DEFAULT_UART 1
Expand Down
9 changes: 9 additions & 0 deletions src/rp2_common/hardware_xosc/include/hardware/xosc.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
#include "pico.h"
#include "hardware/structs/xosc.h"


// Allow lengthening startup delay to accommodate slow-starting oscillators

// PICO_CONFIG: PICO_XOSC_STARTUP_DELAY_MULTIPLIER, Multiplier to lengthen xosc startup delay to accommodate slow-starting oscillators, type=int, min=1, default=1, group=hardware_xosc
#ifndef PICO_XOSC_STARTUP_DELAY_MULTIPLIER
#define PICO_XOSC_STARTUP_DELAY_MULTIPLIER 1
#endif


#ifdef __cplusplus
extern "C" {
#endif
Expand Down
21 changes: 15 additions & 6 deletions src/rp2_common/hardware_xosc/xosc.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@

#include "hardware/platform_defs.h"
#include "hardware/regs/xosc.h"
#include "hardware/structs/xosc.h"
#include "hardware/xosc.h"

#if XOSC_MHZ < 1 || XOSC_MHZ > 15
#error XOSC_MHZ must be in the range 1-15
#endif

#define STARTUP_DELAY (((((XOSC_MHZ * MHZ) / 1000) + 128) / 256) * PICO_XOSC_STARTUP_DELAY_MULTIPLIER)

// The DELAY field in xosc_hw->startup is 14 bits wide.
#if STARTUP_DELAY >= (1 << 13)
#error PICO_XOSC_STARTUP_DELAY_MULTIPLIER is too large: XOSC STARTUP.DELAY must be < 8192
#endif

void xosc_init(void) {
// Assumes 1-15 MHz input
assert(XOSC_MHZ <= 15);
// Assumes 1-15 MHz input, checked above.
xosc_hw->ctrl = XOSC_CTRL_FREQ_RANGE_VALUE_1_15MHZ;

// Set xosc startup delay
uint32_t startup_delay = (((12 * MHZ) / 1000) + 128) / 256;
xosc_hw->startup = startup_delay;
xosc_hw->startup = STARTUP_DELAY;

// Set the enable bit now that we have set freq range and startup delay
hw_set_bits(&xosc_hw->ctrl, XOSC_CTRL_ENABLE_VALUE_ENABLE << XOSC_CTRL_ENABLE_LSB);
Expand All @@ -43,4 +52,4 @@ void xosc_dormant(void) {
xosc_hw->dormant = XOSC_DORMANT_VALUE_DORMANT;
// Wait for it to become stable once woken up
while(!(xosc_hw->status & XOSC_STATUS_STABLE_BITS));
}
}

0 comments on commit d026118

Please sign in to comment.