~aleteoryx/muditaos

ref: 8b3ae7b48c0f5935a07fc6d23b1ada067f4d6d6f muditaos/module-bsp/board/rt1051/drivers/RT1051DriverPWM.hpp -rw-r--r-- 1.9 KiB
8b3ae7b4 — Lefucjusz [BH-1857] Fix improper PWM module clock frequency computation 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
// Copyright (c) 2017-2024, 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 final : public DriverPWM
    {
      public:
        RT1051DriverPWM(PWMInstances inst, PWMModules mod, const DriverPWMParams &params);

        ~RT1051DriverPWM();

        void InitNextChannel(const DriverPWMParams &params);

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

        void Start(PWMChannel channel);

        void Stop(PWMChannel channel);

        void UpdateClockFrequency();

        enum class PwmState
        {
            Off,
            On
        };

      private:
        PwmState GetPwmState();

        void SetupPWMChannel(PWMChannel channel);

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

        void ForceLowOutput(PWMChannel channel);

        void RestorePwmOutput(PWMChannel channel);

        pwm_channels_t getChannelMask(PWMChannel channel);

        bool channelEnabled(PWMChannel channel);

        bool otherChannelRunning(PWMChannel channel);

        void stopAll();

        void startAll();

        void restoreDutyCycle();

        PWM_Type *base = nullptr;

        pwm_submodule_t pwmModule = kPWM_Module_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 pwmModuleClockFrequency = 0;
    };

} // namespace drivers