~aleteoryx/muditaos

ref: 3a793437eaab640986f503e1244c6f0cf2cb37a5 muditaos/module-sys/SystemManager/PowerManager.cpp -rw-r--r-- 4.2 KiB
3a793437 — Mateusz Grzywacz [EGD-4835] Fix HIL Readme, code, so it works, rename, random GCC 10 fix 5 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <log/log.hpp>

#include "PowerManager.hpp"

namespace sys
{
    PowerManager::PowerManager()
    {
        lowPowerControl = bsp::LowPowerMode::Create().value_or(nullptr);
    }

    PowerManager::~PowerManager()
    {}

    int32_t PowerManager::PowerOff()
    {
        return lowPowerControl->PowerOff();
    }

    int32_t PowerManager::Reboot()
    {
        return lowPowerControl->Reboot();
    }

    void PowerManager::UpdateCpuFrequency(uint32_t cpuLoad)
    {
        const auto freq = lowPowerControl->GetCurrentFrequency();

        if (cpuLoad > frequencyShiftUpperThreshold && freq < bsp::LowPowerMode::CpuFrequency::Level_6) {
            aboveThresholdCounter++;
            belowThresholdCounter = 0;
        }
        else if (cpuLoad < frequencyShiftLowerThreshold && freq > bsp::LowPowerMode::CpuFrequency::Level_1) {
            belowThresholdCounter++;
            aboveThresholdCounter = 0;
        }
        else {
            ResetFrequencyShiftCounter();
        }

        if (aboveThresholdCounter >= maxAboveThresholdCount) {
            ResetFrequencyShiftCounter();
            IncreaseCpuFrequency();
        }
        if (belowThresholdCounter >= maxBelowThresholdCount) {
            ResetFrequencyShiftCounter();
            DecreaseCpuFrequency();
        }
    }

    void PowerManager::IncreaseCpuFrequency() const
    {
        const auto freq      = lowPowerControl->GetCurrentFrequency();
        const auto oscSource = lowPowerControl->GetCurrentOscillatorSource();
        const auto pll2State = lowPowerControl->GetCurrentPll2State();

        // switch osc source first
        if (freq == bsp::LowPowerMode::CpuFrequency::Level_1 &&
            oscSource == bsp::LowPowerMode::OscillatorSource::Internal) {
            lowPowerControl->SwitchOscillatorSource(bsp::LowPowerMode::OscillatorSource::External);
        }

        // then turn on pll2
        if (pll2State == bsp::LowPowerMode::Pll2State::Disable) {
            lowPowerControl->SwitchPll2State(bsp::LowPowerMode::Pll2State::Enable);
        }

        // and increase frequency
        if (freq < bsp::LowPowerMode::CpuFrequency::Level_6) {
            lowPowerControl->SetCpuFrequency(bsp::LowPowerMode::CpuFrequency::Level_6);
        }
    }

    void PowerManager::DecreaseCpuFrequency() const
    {
        const auto freq = lowPowerControl->GetCurrentFrequency();
        auto level      = bsp::LowPowerMode::CpuFrequency::Level_1;

        switch (freq) {
        case bsp::LowPowerMode::CpuFrequency::Level_6:
            level = bsp::LowPowerMode::CpuFrequency::Level_5;
            break;
        case bsp::LowPowerMode::CpuFrequency::Level_5:
            level = bsp::LowPowerMode::CpuFrequency::Level_4;
            break;
        case bsp::LowPowerMode::CpuFrequency::Level_4:
            level = bsp::LowPowerMode::CpuFrequency::Level_3;
            break;
        case bsp::LowPowerMode::CpuFrequency::Level_3:
            level = bsp::LowPowerMode::CpuFrequency::Level_2;
            break;
        case bsp::LowPowerMode::CpuFrequency::Level_2:
            level = bsp::LowPowerMode::CpuFrequency::Level_1;
            break;
        case bsp::LowPowerMode::CpuFrequency::Level_1:
            break;
        }

        // decrease frequency first
        if (level != freq) {
            lowPowerControl->SetCpuFrequency(level);
        }

        if (level == bsp::LowPowerMode::CpuFrequency::Level_1) {
            const auto oscSource = lowPowerControl->GetCurrentOscillatorSource();
            const auto pll2State = lowPowerControl->GetCurrentPll2State();

            // then switch osc source
            if (oscSource == bsp::LowPowerMode::OscillatorSource::External) {
                lowPowerControl->SwitchOscillatorSource(bsp::LowPowerMode::OscillatorSource::Internal);
            }

            // and turn off pll2
            if (pll2State == bsp::LowPowerMode::Pll2State::Enable) {
                lowPowerControl->SwitchPll2State(bsp::LowPowerMode::Pll2State::Disable);
            }
        }
    }

    void PowerManager::ResetFrequencyShiftCounter()
    {
        aboveThresholdCounter = 0;
        belowThresholdCounter = 0;
    }
} // namespace sys