M module-bsp/board/rt1051/drivers/RT1051DriverPWM.cpp => module-bsp/board/rt1051/drivers/RT1051DriverPWM.cpp +17 -3
@@ 2,6 2,7 @@
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#include "RT1051DriverPWM.hpp"
+#include "RT1051DriverPWMhelper.hpp"
#include "log/log.hpp"
#include "../board.h"
#include <algorithm>
@@ 92,6 93,16 @@ namespace drivers
ForceLowOutput();
}
+ RT1051DriverPWM::PwmState RT1051DriverPWM::GetPwmState()
+ {
+ if (PWM_GetPwmGeneratorState(base, 1 << pwmModule)) {
+ return PwmState::On;
+ }
+ else {
+ return PwmState::Off;
+ }
+ }
+
void RT1051DriverPWM::SetupPWMChannel(PWMChannel channel)
{
switch (parameters.channel) {
@@ 141,10 152,13 @@ namespace drivers
void RT1051DriverPWM::UpdateClockFrequency(std::uint32_t newFrequency)
{
cpp_freertos::LockGuard lock(frequencyChangeMutex);
- Stop();
+
SetupPWMInstance(newFrequency);
- SetDutyCycle(pwmSignalConfig.dutyCyclePercent);
- Start();
+ if (GetPwmState() == PwmState::On) {
+ Stop();
+ SetDutyCycle(pwmSignalConfig.dutyCyclePercent);
+ Start();
+ }
}
} // namespace drivers
M module-bsp/board/rt1051/drivers/RT1051DriverPWM.hpp => module-bsp/board/rt1051/drivers/RT1051DriverPWM.hpp +9 -1
@@ 1,4 1,4 @@
-// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
+// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#pragma once
@@ 27,7 27,15 @@ namespace drivers
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);
A module-bsp/board/rt1051/drivers/RT1051DriverPWMhelper.hpp => module-bsp/board/rt1051/drivers/RT1051DriverPWMhelper.hpp +11 -0
@@ 0,0 1,11 @@
+// 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 "module-bsp/board/rt1051/common/fsl_drivers/fsl_common.h"
+
+static inline bool PWM_GetPwmGeneratorState(PWM_Type *base, uint8_t subModulesToStop)
+{
+ return (base->MCTRL & PWM_MCTRL_RUN(subModulesToStop)) != 0;
+}