~aleteoryx/muditaos

ref: 8d96f2d3543c2bea59610e6b65edc842a6b2d42b muditaos/module-bsp/board/rt1051/drivers/RT1051DriverPWM.hpp -rw-r--r-- 2.0 KiB
8d96f2d3 — Lefucjusz [MOS-993] Logger bug fixes and optimizations 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
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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "drivers/pwm/DriverPWM.hpp"

#include "fsl_common.h"
#include "fsl_pwm.h"

#include <mutex.hpp>

#include <vector>
#include <map>
#include <array>
namespace drivers
{
    class RT1051DriverPWM : public DriverPWM
    {
      public:
        RT1051DriverPWM(PWMInstances inst, PWMModules mod, const DriverPWMParams &params);

        ~RT1051DriverPWM() final;

        void InitNextChannel(const DriverPWMParams &params) final;

        void SetDutyCycle(std::uint8_t dutyCyclePercent, PWMChannel channel) final;

        void Start(PWMChannel channel) final;

        void Stop(PWMChannel channel) final;

        void UpdateClockFrequency(bsp::CpuFrequencyMHz newFrequency) final;

        enum class PwmState
        {
            Off,
            On,
        };

      private:
        PwmState GetPwmState();

        void SetupPWMChannel(PWMChannel channel);

        void SetupPWMInstance(pwm_signal_param_t *config, unsigned numOfChannels, std::uint32_t clockFrequency);

        void ForceLowOutput(PWMChannel channel);

        void RestorePwmOutput(PWMChannel channel);

        pwm_channels_t getChannelMask(PWMChannel channel);

        bool channelNotEnabled(PWMChannel channel);

        bool otherChannelRunning(PWMChannel channel);

        void stopAll();

        void startAll();

        void restoreDutyCycle();

        PWM_Type *base = nullptr;

        pwm_submodule_t pwmModule = kPWM_Module_0;

        std::uint8_t lastDutyCycle = 0;

        std::array<pwm_signal_param_t, 2> pwmSignalsConfig;

        std::vector<PWMChannel> enabledChannels;
        std::map<PWMChannel, PwmState> pwmChannelState = {
            {PWMChannel::A, PwmState::Off}, {PWMChannel::B, PwmState::Off}, {PWMChannel::X, PwmState::Off}};

        cpp_freertos::MutexStandard frequencyChangeMutex;

        std::uint32_t clockFrequency = 0;
    };

} // namespace drivers