~aleteoryx/muditaos

ref: 317baa04e949a32ef1e72b2dd2b61c1e7a97ca25 muditaos/module-bsp/board/rt1051/drivers/RT1051DriverPWM.hpp -rw-r--r-- 1.6 KiB
317baa04 — Wojtek Rzepecki [BH-872] Fix encoder hadnling 4 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
// 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_drivers/fsl_common.h"
#include "../fsl_drivers/fsl_pwm.h"

#include "mutex.hpp"

namespace drivers
{
    class RT1051DriverPWM : public DriverPWM
    {
      public:
        RT1051DriverPWM(PWMInstances inst, PWMModules mod, const DriverPWMParams &params);

        ~RT1051DriverPWM() final;

        void SetDutyCycle(std::uint8_t dutyCyclePercent) final;

        void Start() final;

        void Stop() final;

        void UpdateClockFrequency(std::uint32_t newFrequency) final;

        enum class PwmState
        {
            Off,
            On,
        };

      private:
        PwmState GetPwmState();

        void SetupPWMChannel(PWMChannel channel);

        void SetupPWMInstance(std::uint32_t clockFrequency);

        void ForceLowOutput();

        void RestorePwmOutput();

        PWM_Type *base = nullptr;

        pwm_submodule_t pwmModule = kPWM_Module_0;

        pwm_signal_param_t pwmSignalConfig = {.pwmChannel       = kPWM_PwmB,
                                              .dutyCyclePercent = 0,
                                              .level            = kPWM_HighTrue,
                                              .deadtimeValue    = 0,
                                              .faultState       = kPWM_PwmFaultState0};

        std::uint8_t lastDutyCycle = 0;

        cpp_freertos::MutexStandard frequencyChangeMutex;
    };

} // namespace drivers