diff --git a/ion/src/device/n0100/drivers/config/timing.h b/ion/src/device/n0100/drivers/config/timing.h index 71d6c3f3d..27757c153 100644 --- a/ion/src/device/n0100/drivers/config/timing.h +++ b/ion/src/device/n0100/drivers/config/timing.h @@ -8,6 +8,8 @@ namespace Device { namespace Timing { namespace Config { +#warning N100-calibration +//TODO: calibrate LoopsPerMillisecond now that we decrease AHB clock while msleeping constexpr static int LoopsPerMillisecond = 8852; constexpr static int LoopsPerMicrosecond = 9; // CPU clock is 96 MHz, and systick clock source is divided by 8 diff --git a/ion/src/device/n0101/drivers/config/timing.h b/ion/src/device/n0101/drivers/config/timing.h index 0c0e48a4b..747611b5c 100644 --- a/ion/src/device/n0101/drivers/config/timing.h +++ b/ion/src/device/n0101/drivers/config/timing.h @@ -8,6 +8,7 @@ namespace Device { namespace Timing { namespace Config { +// TODO: calibrate msleep constexpr static int LoopsPerMillisecond = 8852; constexpr static int LoopsPerMicrosecond = 9; // CPU clock is 96 MHz, and systick clock source is divided by 8 diff --git a/ion/src/device/shared/drivers/board.cpp b/ion/src/device/shared/drivers/board.cpp index a93a5145c..f37af00e5 100644 --- a/ion/src/device/shared/drivers/board.cpp +++ b/ion/src/device/shared/drivers/board.cpp @@ -57,6 +57,21 @@ void shutdownPeripherals(bool keepLEDAwake) { Display::shutdown(); } +void setClockFrequency(Frequency f) { + switch (f) { + case Frequency::High: + RCC.CFGR()->setHPRE(RCC::CFGR::AHBPrescaler::SysClk); + return; + default: + assert(f == Frequency::Low); + /* We could divide the system clock by 512. However, the HCLK clock + * frequency must be >= 14.2MHz and <=216 MHz which forces the + * AHBPrescaler to be below 192MHz/14.2MHz~13.5MHz. */ + RCC.CFGR()->setHPRE(RCC::CFGR::AHBPrescaler::SysClkDividedBy8); + return; + } +} + } } } diff --git a/ion/src/device/shared/drivers/board.h b/ion/src/device/shared/drivers/board.h index 0e314c10f..372843077 100644 --- a/ion/src/device/shared/drivers/board.h +++ b/ion/src/device/shared/drivers/board.h @@ -15,6 +15,13 @@ void shutdownClocks(bool keepLEDAwake = false); void initPeripherals(); void shutdownPeripherals(bool keepLEDAwake = false); +enum class Frequency { + Low = 0, + High = 1 +}; + +void setClockFrequency(Frequency f); + } } } diff --git a/ion/src/device/shared/drivers/timing.cpp b/ion/src/device/shared/drivers/timing.cpp index 24be59ca9..e2a30d100 100644 --- a/ion/src/device/shared/drivers/timing.cpp +++ b/ion/src/device/shared/drivers/timing.cpp @@ -1,4 +1,5 @@ #include "timing.h" +#include "board.h" #include #include @@ -12,9 +13,12 @@ using namespace Device::Timing; * precision, we could use the controller cycle counter (Systick). */ void msleep(uint32_t ms) { + // We decrease the AHB clock frequency to save power while sleeping. + Device::Board::setClockFrequency(Device::Board::Frequency::Low); for (volatile uint32_t i=0; i