~aleteoryx/muditaos

ref: 6b45d3b71dc1e09a16a3bc66cc54ced1c9de8d43 muditaos/module-bsp/board/rt1051/bellpx/board.cpp -rw-r--r-- 2.7 KiB
6b45d3b7 — Maciej Gibowicz [BH-2097] Adding and deleting a single custom quote 1 year, 9 days 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include "bsp.hpp"
#include "board.h"
#include "drivers/gpio/DriverGPIO.hpp"
#include "board/BoardDefinitions.hpp"
#include "bsp/lpm/RT1051LPM.hpp"
#include "log/log.hpp"

namespace
{
    using namespace drivers;

    constexpr auto powerOffFrequencyLevel = bsp::CpuFrequencyMHz::Level_0;
    constexpr auto powerOffFrequencyInHz  = static_cast<std::uint32_t>(powerOffFrequencyLevel) * 1'000'000;

    void power_off()
    {
        // No memory allocation here as this specific GPIO was initialized at the startup. We are just grabbing here a
        // reference to the already existing object
        auto gpio_wakeup =
            DriverGPIO::Create(static_cast<GPIOInstances>(BoardDefinitions::BELL_WAKEUP_GPIO), DriverGPIOParams{});

        gpio_wakeup->ConfPin(DriverGPIOPinParams{.dir      = DriverGPIOPinParams::Direction::Input,
                                                 .irqMode  = DriverGPIOPinParams::InterruptMode::IntRisingEdge,
                                                 .defLogic = 0,
                                                 .pin = static_cast<std::uint32_t>(BoardDefinitions::BELL_WAKEUP)});
        gpio_wakeup->ClearPortInterrupts(1 << static_cast<std::uint32_t>(BoardDefinitions::BELL_WAKEUP));
        gpio_wakeup->EnableInterrupt(1 << static_cast<std::uint32_t>(BoardDefinitions::BELL_WAKEUP));

        auto cpu = bsp::RT1051LPM();
        cpu.SetCpuFrequency(powerOffFrequencyLevel);

        // If the CPU frequency is wrong just skip the enter to the SNVS mode
        // and wait for the watchdog
        const auto frequency = cpu.GetCpuFrequency();
        if (frequency == powerOffFrequencyInHz) {
            SNVS->LPCR |= SNVS_LPCR_TOP(1); // Enter SNVS mode
        }
        else {
            LOG_FATAL("Can't enter into SNVS mode due to wrong CPU frequency! Current frequency: %ld", frequency);
        }
    }

    void reset()
    {
        NVIC_SystemReset();
    }
} // namespace

namespace bsp
{
    void board_configure()
    {
        /* See *
         * https://patchwork.kernel.org/project/linux-arm-kernel/patch/1471885400-9140-1-git-send-email-Anson.Huang@nxp.com/
         */
        CCM->CGPR |= CCM_CGPR_INT_MEM_CLK_LPM_MASK;

        /* ERR050143 */
        IOMUXC_GPR->GPR1 |= IOMUXC_GPR_GPR1_GINT_MASK;
    }

    void board_exit(RebootState state)
    {
        switch (state) {
        case RebootState::None:
            break;
        case RebootState::Poweroff:
            power_off();
            break;
        case RebootState::Reboot:
            reset();
            break;
        }

        while (true) {}
    }
} // namespace bsp