Skip to content

Commit

Permalink
Merge pull request #3411 from meshtastic/rp2040-slowclock
Browse files Browse the repository at this point in the history
(2/3) Add Slow Clock Support for RP2040 platform.
  • Loading branch information
caveman99 authored Mar 15, 2024
2 parents cb37407 + 34bc22f commit 4d0d82f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
4 changes: 2 additions & 2 deletions arch/rp2040/rp2040.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
; Common settings for rp2040 Processor based targets
[rp2040_base]
platform = https://github.com/maxgerhardt/platform-raspberrypi.git#612de5399d68b359053f1307ed223d400aea975c
platform = https://github.com/maxgerhardt/platform-raspberrypi.git#60d6ae81fcc73c34b1493ca9e261695e471bc0c2
extends = arduino_base
platform_packages = framework-arduinopico@https://github.com/earlephilhower/arduino-pico.git#3.6.2
platform_packages = framework-arduinopico@https://github.com/earlephilhower/arduino-pico.git#3.7.2

board_build.core = earlephilhower
board_build.filesystem_size = 0.5m
Expand Down
8 changes: 8 additions & 0 deletions src/SerialConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
#include "PowerFSM.h"
#include "configuration.h"

#ifdef RP2040_SLOW_CLOCK
#define Port Serial2
#else
#define Port Serial
#endif
// Defaulting to the formerly removed phone_timeout_secs value of 15 minutes
#define SERIAL_CONNECTION_TIMEOUT (15 * 60) * 1000UL

Expand Down Expand Up @@ -31,6 +35,10 @@ SerialConsole::SerialConsole() : StreamAPI(&Port), RedirectablePrint(&Port), con
canWrite = false; // We don't send packets to our port until it has talked to us first
// setDestination(&noopPrint); for testing, try turning off 'all' debug output and see what leaks

#ifdef RP2040_SLOW_CLOCK
Port.setTX(SERIAL2_TX);
Port.setRX(SERIAL2_RX);
#endif
Port.begin(SERIAL_BAUD);
#if defined(ARCH_NRF52) || defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3) || defined(ARCH_RP2040)
time_t timeout = millis();
Expand Down
10 changes: 10 additions & 0 deletions src/modules/SerialModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,13 @@ int32_t SerialModule::runOnce()
uint32_t baud = getBaudRate();

if (moduleConfig.serial.override_console_serial_port) {
#ifdef RP2040_SLOW_CLOCK
Serial2.flush();
serialPrint = &Serial2;
#else
Serial.flush();
serialPrint = &Serial;
#endif
// Give it a chance to flush out 💩
delay(10);
}
Expand All @@ -151,8 +156,13 @@ int32_t SerialModule::runOnce()
Serial2.begin(baud, SERIAL_8N1);
Serial2.setTimeout(moduleConfig.serial.timeout > 0 ? moduleConfig.serial.timeout : TIMEOUT);
} else {
#ifdef RP2040_SLOW_CLOCK
Serial2.begin(baud, SERIAL_8N1);
Serial2.setTimeout(moduleConfig.serial.timeout > 0 ? moduleConfig.serial.timeout : TIMEOUT);
#else
Serial.begin(baud, SERIAL_8N1);
Serial.setTimeout(moduleConfig.serial.timeout > 0 ? moduleConfig.serial.timeout : TIMEOUT);
#endif
}
#else
Serial.begin(baud, SERIAL_8N1);
Expand Down
52 changes: 51 additions & 1 deletion src/platform/rp2040/main-rp2040.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "configuration.h"
#include <hardware/clocks.h>
#include <hardware/pll.h>
#include <pico/stdlib.h>
#include <pico/unique_id.h>
#include <stdio.h>

Expand Down Expand Up @@ -35,9 +38,56 @@ void rp2040Setup()
Taken from CPU cycle counter and ROSC oscillator, so should be pretty random.
*/
randomSeed(rp2040.hwrand32());

#ifdef RP2040_SLOW_CLOCK
uint f_pll_sys = frequency_count_khz(CLOCKS_FC0_SRC_VALUE_PLL_SYS_CLKSRC_PRIMARY);
uint f_pll_usb = frequency_count_khz(CLOCKS_FC0_SRC_VALUE_PLL_USB_CLKSRC_PRIMARY);
uint f_rosc = frequency_count_khz(CLOCKS_FC0_SRC_VALUE_ROSC_CLKSRC);
uint f_clk_sys = frequency_count_khz(CLOCKS_FC0_SRC_VALUE_CLK_SYS);
uint f_clk_peri = frequency_count_khz(CLOCKS_FC0_SRC_VALUE_CLK_PERI);
uint f_clk_usb = frequency_count_khz(CLOCKS_FC0_SRC_VALUE_CLK_USB);
uint f_clk_adc = frequency_count_khz(CLOCKS_FC0_SRC_VALUE_CLK_ADC);
uint f_clk_rtc = frequency_count_khz(CLOCKS_FC0_SRC_VALUE_CLK_RTC);

LOG_INFO("Clock speed:\n");
LOG_INFO("pll_sys = %dkHz\n", f_pll_sys);
LOG_INFO("pll_usb = %dkHz\n", f_pll_usb);
LOG_INFO("rosc = %dkHz\n", f_rosc);
LOG_INFO("clk_sys = %dkHz\n", f_clk_sys);
LOG_INFO("clk_peri = %dkHz\n", f_clk_peri);
LOG_INFO("clk_usb = %dkHz\n", f_clk_usb);
LOG_INFO("clk_adc = %dkHz\n", f_clk_adc);
LOG_INFO("clk_rtc = %dkHz\n", f_clk_rtc);
#endif
}

void enterDfuMode()
{
reset_usb_boot(0, 0);
}
}

/* Init in early boot state. */
#ifdef RP2040_SLOW_CLOCK
void initVariant()
{
/* Set the system frequency to 18 MHz. */
set_sys_clock_khz(18 * KHZ, false);
/* The previous line automatically detached clk_peri from clk_sys, and
attached it to pll_usb. We need to attach clk_peri back to system PLL to keep SPI
working at this low speed.
For details see https://github.com/jgromes/RadioLib/discussions/938
*/
clock_configure(clk_peri,
0, // No glitchless mux
CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS, // System PLL on AUX mux
18 * MHZ, // Input frequency
18 * MHZ // Output (must be same as no divider)
);
/* Run also ADC on lower clk_sys. */
clock_configure(clk_adc, 0, CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS, 18 * MHZ, 18 * MHZ);
/* Run RTC from XOSC since USB clock is off */
clock_configure(clk_rtc, 0, CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_XOSC_CLKSRC, 12 * MHZ, 47 * KHZ);
/* Turn off USB PLL */
pll_deinit(pll_usb);
}
#endif

0 comments on commit 4d0d82f

Please sign in to comment.