~aleteoryx/muditaos

ref: 3cbbeff551230786ae13c23a7bf4fa8c50099896 muditaos/module-bsp/board/rt1051/bsp/lpm/Bandgap.cpp -rw-r--r-- 1.3 KiB
3cbbeff5 — Lefucjusz [MOS-1011] Fix frequency switching stability 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "Bandgap.hpp"
#include <cstdint>
#include <fsl_common.h>
#include <critical.hpp>

namespace
{
    constexpr std::uint32_t REFTOP_LOWPOWER_MASK = (1U << 2);
}

namespace bsp::bandgap
{
    void SwitchToRegularMode()
    {
        cpp_freertos::CriticalSection::Enter();

        /* Enable regular bandgap and wait for it to stabilize */
        CCM_ANALOG->MISC0_CLR = CCM_ANALOG_MISC0_REFTOP_PWD_MASK;
        while ((CCM_ANALOG->MISC0 & CCM_ANALOG_MISC0_REFTOP_VBGUP_MASK) == 0) {}

        /* Disable low power bandgap */
        XTALOSC24M->LOWPWR_CTRL_CLR = XTALOSC24M_LOWPWR_CTRL_LPBG_SEL_MASK;
        PMU->MISC0_CLR              = REFTOP_LOWPOWER_MASK;

        cpp_freertos::CriticalSection::Exit();
    }

    void SwitchToLowPowerMode()
    {
        cpp_freertos::CriticalSection::Enter();

        /* Enable low power bandgap */
        PMU->MISC0_SET = REFTOP_LOWPOWER_MASK;
        XTALOSC24M->LOWPWR_CTRL_SET = XTALOSC24M_LOWPWR_CTRL_LPBG_SEL_MASK;

        /* Disable regular bandgap */
        PMU->MISC0_SET              = CCM_ANALOG_MISC0_REFTOP_PWD_MASK;

        cpp_freertos::CriticalSection::Exit();
    }
} // namespace bsp::bandgap