@@ -120,16 +120,21 @@ void pwmout_write(pwmout_t* obj, float value)
120
120
value = 1.0f ;
121
121
}
122
122
uint32_t t_on = (uint32_t )((float )(obj -> pwm -> MATCHREL [obj -> pwm_ch * 2 ] + 1 ) * value );
123
- if (t_on > 0 ) {
124
- if (value != 1.0f ) {
123
+ if (t_on > 0 ) { // duty is not 0%
124
+ if (value != 1.0f ) { // duty is not 100%
125
125
obj -> pwm -> MATCHREL [(obj -> pwm_ch * 2 ) + 1 ] = t_on - 1 ;
126
+ // unhalt the counter
126
127
obj -> pwm -> CTRL &= ~(1 << 2 );
127
- } else {
128
+ } else { // duty is 100%
129
+ // halt and clear the counter
128
130
obj -> pwm -> CTRL |= (1 << 2 ) | (1 << 3 );
131
+ // output level tied to high
129
132
obj -> pwm -> OUTPUT |= (1 << obj -> pwm_ch );
130
133
}
131
- } else {
134
+ } else { // duty is 0%
135
+ // halt and clear the counter
132
136
obj -> pwm -> CTRL |= (1 << 2 ) | (1 << 3 );
137
+ // output level tied to low
133
138
obj -> pwm -> OUTPUT &= ~(1 << obj -> pwm_ch );
134
139
}
135
140
}
@@ -155,15 +160,19 @@ void pwmout_period_ms(pwmout_t* obj, int ms)
155
160
// Set the PWM period, keeping the duty cycle the same.
156
161
void pwmout_period_us (pwmout_t * obj , int us )
157
162
{
163
+ // The period are off by one for MATCHREL, so +1 to get actual value
158
164
uint32_t t_off = obj -> pwm -> MATCHREL [(obj -> pwm_ch * 2 ) + 0 ] + 1 ;
159
165
uint32_t t_on = obj -> pwm -> MATCHREL [(obj -> pwm_ch * 2 ) + 1 ] + 1 ;
160
166
float v = (float )t_on /(float )t_off ;
161
167
obj -> pwm -> MATCHREL [(obj -> pwm_ch * 2 ) + 0 ] = (uint32_t )us - 1 ;
162
- if (us > 0 ) {
168
+ if (us > 0 ) { // PWM period is not 0
163
169
obj -> pwm -> MATCHREL [(obj -> pwm_ch * 2 ) + 1 ] = (uint32_t )((float )us * (float )v ) - 1 ;
170
+ // unhalt the counter
164
171
obj -> pwm -> CTRL &= ~(1 << 2 );
165
- } else {
172
+ } else { // PWM period is 0
173
+ // halt and clear the counter
166
174
obj -> pwm -> CTRL |= (1 << 2 ) | (1 << 3 );
175
+ // output level tied to low
167
176
obj -> pwm -> OUTPUT &= ~(1 << obj -> pwm_ch );
168
177
}
169
178
}
@@ -180,11 +189,13 @@ void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
180
189
181
190
void pwmout_pulsewidth_us (pwmout_t * obj , int us )
182
191
{
183
- if (us > 0 ) {
192
+ if (us > 0 ) { // PWM peried is not 0
184
193
obj -> pwm -> MATCHREL [(obj -> pwm_ch * 2 ) + 1 ] = (uint32_t )us - 1 ;
185
194
obj -> pwm -> CTRL &= ~(1 << 2 );
186
- } else {
195
+ } else { //PWM period is 0
196
+ // halt and clear the counter
187
197
obj -> pwm -> CTRL |= (1 << 2 ) | (1 << 3 );
198
+ // output level tied to low
188
199
obj -> pwm -> OUTPUT &= ~(1 << obj -> pwm_ch );
189
200
}
190
201
}
0 commit comments