-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathpwm.cpp
163 lines (127 loc) · 3.9 KB
/
pwm.cpp
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "Arduino.h"
#include "pwm.h"
#include "bsp_api.h"
PwmOut::PwmOut(int pinNumber) :
_pin(pinNumber),
_enabled(false)
{
}
PwmOut::~PwmOut() {
}
bool PwmOut::cfg_pin(int max_index) {
/* verify index are good */
if(_pin < 0 || _pin >= max_index) {
return false;
}
/* getting configuration from table */
auto pin_cgf = getPinCfgs(_pin, PIN_CFG_REQ_PWM);
/* verify configuration are good */
if(pin_cgf[0] == 0) {
return false;
}
timer_channel = GET_CHANNEL(pin_cgf[0]);
_is_agt = IS_PIN_AGT_PWM(pin_cgf[0]);
_pwm_channel = IS_PWM_ON_A(pin_cgf[0]) ? CHANNEL_A : CHANNEL_B;
/* actually configuring PIN function */
R_IOPORT_PinCfg(&g_ioport_ctrl, g_pin_cfg[_pin].pin, (uint32_t) (IOPORT_CFG_PERIPHERAL_PIN | (_is_agt ? IOPORT_PERIPHERAL_AGT : IOPORT_PERIPHERAL_GPT1)));
return true;
}
/* default begin function, starts the timers with default pwm configuration (490Hz and 50%) */
bool PwmOut::begin() {
bool rv = true;
int max_index = PINS_COUNT;
rv &= cfg_pin(max_index);
if(rv) {
/* extended PWM CFG*/
if(_is_agt) {
rv &= timer.begin_pwm(AGT_TIMER, timer_channel, _pwm_channel);
}
else {
rv &= timer.begin_pwm(GPT_TIMER, timer_channel, _pwm_channel);
}
}
_enabled = rv;
return rv;
}
/* begin "standard", period and pulse when raw is false are supposed to be expressed in usec */
/* -------------------------------------------------------------------------- */
bool PwmOut::begin(uint32_t period_width, uint32_t pulse_width, bool raw /*= false */, timer_source_div_t sd /*= TIMER_SOURCE_DIV_1*/) {
/* -------------------------------------------------------------------------- */
_enabled = true;
int max_index = PINS_COUNT;
_enabled &= cfg_pin(max_index);
if(_enabled) {
if(raw) {
_enabled &= timer.begin(TIMER_MODE_PWM, (_is_agt) ? AGT_TIMER : GPT_TIMER, timer_channel, period_width, pulse_width, sd);
}
else {
/* NOTE: I suppose period and pulse are expressed in us */
float freq_hz = (1000000.0 / (float)period_width);
float duty_perc = ((float)pulse_width * 100.0 / (float)period_width);
_enabled &= timer.begin(TIMER_MODE_PWM, (_is_agt) ? AGT_TIMER : GPT_TIMER, timer_channel, freq_hz, duty_perc);
}
timer.add_pwm_extended_cfg();
timer.enable_pwm_channel(_pwm_channel);
}
if(_enabled) {
_enabled &= timer.open();
_enabled &= timer.start();
}
return _enabled;
}
/* -------------------------------------------------------------------------- */
bool PwmOut::begin(float freq_hz, float duty_perc) {
/* -------------------------------------------------------------------------- */
_enabled = true;
int max_index = PINS_COUNT;
_enabled &= cfg_pin(max_index);
if(_enabled) {
_enabled &= timer.begin(TIMER_MODE_PWM, (_is_agt) ? AGT_TIMER : GPT_TIMER, timer_channel, freq_hz, duty_perc);
}
if(_enabled) {
timer.add_pwm_extended_cfg();
timer.enable_pwm_channel(_pwm_channel);
_enabled &= timer.open();
_enabled &= timer.start();
}
return _enabled;
}
bool PwmOut::period(int ms) {
return timer.set_period_ms((double)ms);
}
bool PwmOut::pulseWidth(int ms) {
return timer.set_pulse_ms((double)ms,_pwm_channel);
}
bool PwmOut::period_us(int us) {
return timer.set_period_us((double)us);
}
bool PwmOut::pulseWidth_us(int us) {
return timer.set_pulse_us((double)us,_pwm_channel);
}
bool PwmOut::period_raw(int period) {
return timer.set_period(period);
}
bool PwmOut::pulseWidth_raw(int pulse) {
return timer.set_duty_cycle(pulse, _pwm_channel);
}
bool PwmOut::pulse_perc(float duty) {
float period = (float)timer.get_period_raw();
float pulse = period * duty / 100.0;
return pulseWidth_raw((int)pulse);
}
void PwmOut::suspend() {
if (_enabled) {
timer.stop();
_enabled = false;
}
}
void PwmOut::resume() {
if (!_enabled) {
timer.start();
_enabled = true;
}
}
void PwmOut::end() {
timer.end();
_enabled = false;
}