libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::MSPM0PWM Class Reference
Inheritance diagram for LibXR::MSPM0PWM:
[legend]
Collaboration diagram for LibXR::MSPM0PWM:
[legend]

Classes

struct  Resources
 

Public Member Functions

 MSPM0PWM (Resources res)
 
ErrorCode SetDutyCycle (float value)
 设置 PWM 信号的占空比 / Sets the duty cycle of the PWM signal.
 
ErrorCode SetConfig (Configuration config)
 配置 PWM 参数 / Configures the PWM settings.
 
ErrorCode Enable ()
 启用 PWM 输出 / Enables the PWM output.
 
ErrorCode Disable ()
 禁用 PWM 输出 / Disables the PWM output.
 
- Public Member Functions inherited from LibXR::PWM

Static Public Member Functions

static uint32_t SourceClockFromSysCfg (GPTIMER_Regs *timer, uint32_t timer_clock_hz)
 由 SysConfig 的定时器时钟反推源时钟 / Source clock from SysConfig's timer clock.
 

Detailed Description

Definition at line 9 of file mspm0_pwm.hpp.

Constructor & Destructor Documentation

◆ MSPM0PWM()

MSPM0PWM::MSPM0PWM ( MSPM0PWM::Resources res)

Definition at line 9 of file mspm0_pwm.cpp.

10 : timer_(res.timer), channel_(res.channel), clock_freq_(res.clock_freq)
11{
12 ASSERT(timer_ != nullptr);
13 ASSERT(clock_freq_ > 0);
14}

Member Function Documentation

◆ Disable()

ErrorCode MSPM0PWM::Disable ( )
virtual

禁用 PWM 输出 / Disables the PWM output.

Returns
返回操作结果的错误码 / ErrorCode indicating success or failure.

Implements LibXR::PWM.

Definition at line 120 of file mspm0_pwm.cpp.

121{
122 DL_Timer_stopCounter(timer_);
123 return ErrorCode::OK;
124}
@ OK
操作成功 / Operation successful

◆ Enable()

ErrorCode MSPM0PWM::Enable ( )
virtual

启用 PWM 输出 / Enables the PWM output.

Returns
返回操作结果的错误码 / ErrorCode indicating success or failure.

Implements LibXR::PWM.

Definition at line 114 of file mspm0_pwm.cpp.

115{
116 DL_Timer_startCounter(timer_);
117 return ErrorCode::OK;
118}

◆ SetConfig()

ErrorCode MSPM0PWM::SetConfig ( Configuration config)
virtual

配置 PWM 参数 / Configures the PWM settings.

Parameters
config配置结构体,包含 PWM 设置。 The configuration structure containing PWM settings.
Returns
返回操作结果的错误码 / ErrorCode indicating success or failure.

Implements LibXR::PWM.

Definition at line 64 of file mspm0_pwm.cpp.

65{
66 const uint32_t SOURCE_CLOCK = clock_freq_;
67 if (config.frequency > SOURCE_CLOCK || config.frequency == 0)
68 {
69 return ErrorCode::ARG_ERR;
70 }
71
72 const uint32_t TOTAL_CYCLES_NEEDED = SOURCE_CLOCK / config.frequency;
73 uint32_t min_total_prescale = (TOTAL_CYCLES_NEEDED + MAX_TIMER_LOAD) >> 16;
74 if (min_total_prescale == 0)
75 {
76 min_total_prescale = 1;
77 }
78
79 uint32_t best_div = (min_total_prescale + MAX_PRESCALER - 1) >> 8;
80 if (best_div > MAX_DIVIDE_RATIO)
81 {
82 return ErrorCode::NOT_SUPPORT; // Frequency too low
83 }
84 if (best_div == 0)
85 {
86 best_div = 1;
87 }
88
89 uint32_t best_prescaler = (min_total_prescale + best_div - 1) / best_div;
90 if (best_prescaler == 0)
91 {
92 best_prescaler = 1;
93 }
94
95 uint32_t best_load = (SOURCE_CLOCK / (best_div * best_prescaler * config.frequency));
96 if (best_load == 0)
97 {
98 return ErrorCode::NOT_SUPPORT; // Frequency too high to represent
99 }
100 best_load -= 1;
101
102 DL_Timer_ClockConfig clock_config;
103 DL_Timer_getClockConfig(timer_, &clock_config);
104
105 clock_config.divideRatio = static_cast<DL_TIMER_CLOCK_DIVIDE>(best_div - 1);
106 clock_config.prescale = static_cast<uint8_t>(best_prescaler - 1);
107
108 DL_Timer_setClockConfig(timer_, &clock_config);
109 DL_Timer_setLoadValue(timer_, best_load);
110
111 return ErrorCode::OK;
112}
@ NOT_SUPPORT
不支持 / Not supported
@ ARG_ERR
参数错误 / Argument error

◆ SetDutyCycle()

ErrorCode MSPM0PWM::SetDutyCycle ( float value)
virtual

设置 PWM 信号的占空比 / Sets the duty cycle of the PWM signal.

Parameters
value占空比,浮点值(0.0 到 1.0)。 The duty cycle as a floating-point value (0.0 to 1.0).
Returns
返回操作结果的错误码 / ErrorCode indicating success or failure.

Implements LibXR::PWM.

Definition at line 16 of file mspm0_pwm.cpp.

17{
18 if (value < 0.0f)
19 {
20 value = 0.0f;
21 }
22 if (value > 1.0f)
23 {
24 value = 1.0f;
25 }
26
27 uint32_t period = DL_Timer_getLoadValue(timer_);
28 uint32_t compare_value = 0;
29 DL_TIMER_COUNT_MODE count_mode = DL_Timer_getCounterMode(timer_);
30
31 switch (count_mode)
32 {
33 case DL_TIMER_COUNT_MODE_DOWN:
34 compare_value = static_cast<uint32_t>(static_cast<float>(period) * (1.0f - value));
35 break;
36
37 case DL_TIMER_COUNT_MODE_UP:
38 case DL_TIMER_COUNT_MODE_UP_DOWN:
39 compare_value = static_cast<uint32_t>(static_cast<float>(period) * value);
40 break;
41
42 default:
44 }
45
46 if (compare_value > period)
47 {
48 compare_value = period;
49 }
50
51 DL_Timer_setCaptureCompareValue(timer_, compare_value, channel_);
52
53 return ErrorCode::OK;
54}

◆ SourceClockFromSysCfg()

uint32_t MSPM0PWM::SourceClockFromSysCfg ( GPTIMER_Regs * timer,
uint32_t timer_clock_hz )
static

由 SysConfig 的定时器时钟反推源时钟 / Source clock from SysConfig's timer clock.

Parameters
timer定时器实例 / Timer instance.
timer_clock_hzSysConfig 的 NAME_INST_CLK_FREQ(分频后)/ SysConfig's NAME_INST_CLK_FREQ (after the dividers).
Returns
分频前的定时器时钟 / Timer clock before the dividers.

Definition at line 56 of file mspm0_pwm.cpp.

57{
58 DL_Timer_ClockConfig config;
59 DL_Timer_getClockConfig(timer, &config);
60 return timer_clock_hz * (static_cast<uint32_t>(config.divideRatio) + 1U) *
61 (static_cast<uint32_t>(config.prescale) + 1U);
62}

The documentation for this class was generated from the following files: