57
57
#define MAX_PWM_PERIOD_MS (MAX_PWM_PERIOD_US / 1000)
58
58
#define MAX_PWM_PERIOD_S ((float) MAX_PWM_PERIOD_US / 1000000.0f)
59
59
60
+ /* Sequence bit that denotes the polarity of the pwm waveform. */
61
+ #define SEQ_POLARITY_BIT (0x8000)
62
+
60
63
/* Allocate PWM instances. */
61
64
static nrf_drv_pwm_t nordic_nrf5_pwm_instance [] = {
62
65
#if PWM0_ENABLED
@@ -98,9 +101,6 @@ static void nordic_pwm_init(pwmout_t *obj)
98
101
.step_mode = NRF_PWM_STEP_AUTO ,
99
102
};
100
103
101
- /* Make sure PWM instance is not running before making changes. */
102
- nrf_drv_pwm_uninit (& nordic_nrf5_pwm_instance [obj -> instance ]);
103
-
104
104
/* Initialize instance with new configuration. */
105
105
ret_code_t result = nrf_drv_pwm_init (& nordic_nrf5_pwm_instance [obj -> instance ],
106
106
& config ,
@@ -114,6 +114,9 @@ static void nordic_pwm_restart(pwmout_t *obj)
114
114
{
115
115
MBED_ASSERT (obj );
116
116
117
+ /* Uninitialize PWM instace */
118
+ nrf_drv_pwm_uninit (& nordic_nrf5_pwm_instance [obj -> instance ]);
119
+
117
120
/* (Re)initialize PWM instance. */
118
121
nordic_pwm_init (obj );
119
122
@@ -140,7 +143,7 @@ void pwmout_init(pwmout_t *obj, PinName pin)
140
143
/* Get hardware instance from pinmap. */
141
144
int instance = pin_instance_pwm (pin );
142
145
143
- MBED_ASSERT (instance < (int )(sizeof (nordic_nrf5_pwm_instance ) / sizeof (nrf_drv_pwm_t )));
146
+ MBED_ASSERT (instance < (int ) (sizeof (nordic_nrf5_pwm_instance ) / sizeof (nrf_drv_pwm_t )));
144
147
145
148
/* Populate PWM object with default values. */
146
149
obj -> instance = instance ;
@@ -153,6 +156,9 @@ void pwmout_init(pwmout_t *obj, PinName pin)
153
156
obj -> sequence .repeats = 0 ;
154
157
obj -> sequence .end_delay = 0 ;
155
158
159
+ /* Set active low logic. */
160
+ obj -> pulse |= SEQ_POLARITY_BIT ;
161
+
156
162
/* Initialize PWM instance. */
157
163
nordic_pwm_init (obj );
158
164
}
@@ -184,17 +190,20 @@ void pwmout_write(pwmout_t *obj, float percent)
184
190
/* Find counts based on period. */
185
191
uint16_t pulse = obj -> period * percent ;
186
192
193
+ /* Clear sequence, but keep the polarity bit */
194
+ obj -> pulse &= SEQ_POLARITY_BIT ;
195
+
187
196
/* Ensure we don't overcount. */
188
- obj -> pulse = (pulse > MAX_PWM_COUNTERTOP ) ? MAX_PWM_COUNTERTOP : pulse ;
197
+ obj -> pulse | = (pulse > MAX_PWM_COUNTERTOP ) ? MAX_PWM_COUNTERTOP : pulse ;
189
198
190
199
/* Store actual percentage passed as parameter to avoid floating point rounding errors. */
191
200
obj -> percent = percent ;
192
201
193
202
/* Set new duty-cycle. */
194
203
ret_code_t result = nrf_drv_pwm_simple_playback (& nordic_nrf5_pwm_instance [obj -> instance ],
195
- & obj -> sequence ,
196
- 1 ,
197
- NRF_DRV_PWM_FLAG_LOOP );
204
+ & obj -> sequence ,
205
+ 1 ,
206
+ NRF_DRV_PWM_FLAG_LOOP );
198
207
199
208
MBED_ASSERT (result == NRF_SUCCESS );
200
209
}
@@ -266,10 +275,11 @@ void pwmout_period_us(pwmout_t *obj, int period)
266
275
}
267
276
268
277
/* Scale new count based on stored duty-cycle and new period. */
269
- uint32_t pulse = (period * obj -> pulse ) / obj -> period ;
278
+ uint32_t pulse = (period * ( obj -> pulse & ~ SEQ_POLARITY_BIT ) ) / obj -> period ;
270
279
271
280
/* Store new values in object. */
272
- obj -> pulse = pulse ;
281
+ obj -> pulse &= SEQ_POLARITY_BIT ;
282
+ obj -> pulse |= pulse ;
273
283
obj -> period = period ;
274
284
obj -> percent = (float ) pulse / (float ) period ;
275
285
@@ -287,8 +297,9 @@ void pwmout_pulsewidth(pwmout_t *obj, float pulse)
287
297
DEBUG_PRINTF ("pwmout_pulsewidt: %f\r\n" , pulse );
288
298
289
299
/* Cap pulsewidth to period before setting it. */
290
- if ((pulse * 1000000 ) > (float ) obj -> pulse ) {
291
- obj -> pulse = obj -> period ;
300
+ if ((pulse * 1000000 ) > (float ) (obj -> pulse & ~SEQ_POLARITY_BIT )) {
301
+ obj -> pulse &= SEQ_POLARITY_BIT ;
302
+ obj -> pulse |= obj -> period ;
292
303
pwmout_pulsewidth_us (obj , obj -> pulse );
293
304
} else {
294
305
pwmout_pulsewidth_us (obj , pulse * 1000000 );
@@ -306,7 +317,8 @@ void pwmout_pulsewidth_ms(pwmout_t *obj, int pulse)
306
317
307
318
/* Cap pulsewidth to period before setting it. */
308
319
if ((pulse * 1000 ) > (int ) obj -> period ) {
309
- obj -> pulse = obj -> period ;
320
+ obj -> pulse &= SEQ_POLARITY_BIT ;
321
+ obj -> pulse |= obj -> period ;
310
322
pwmout_pulsewidth_us (obj , obj -> pulse );
311
323
} else {
312
324
pwmout_pulsewidth_us (obj , pulse * 1000 );
@@ -328,7 +340,8 @@ void pwmout_pulsewidth_us(pwmout_t *obj, int pulse)
328
340
}
329
341
330
342
/* Store new values in object. */
331
- obj -> pulse = pulse ;
343
+ obj -> pulse &= SEQ_POLARITY_BIT ;
344
+ obj -> pulse |= pulse ;
332
345
obj -> percent = (float ) pulse / (float ) obj -> period ;
333
346
334
347
/* Restart instance with new values. */
0 commit comments