@@ -70,6 +70,7 @@ typedef struct {
70
70
pwm_mode_t mode [NRF_PWM_CHANNEL_COUNT ];
71
71
pwm_duty_t duty_mode [NRF_PWM_CHANNEL_COUNT ];
72
72
uint32_t duty [NRF_PWM_CHANNEL_COUNT ];
73
+ uint16_t pwm_seq [4 ];
73
74
pwm_run_t active ;
74
75
bool defer_start ;
75
76
int8_t freq_div ;
@@ -137,7 +138,7 @@ STATIC int hard_pwm_find() {
137
138
return j * NRF_PWM_CHANNEL_COUNT ;
138
139
}
139
140
}
140
- mp_raise_ValueError (MP_ERROR_TEXT ("no free PWM object " ));
141
+ mp_raise_ValueError (MP_ERROR_TEXT ("all PWM devices in use " ));
141
142
}
142
143
143
144
STATIC void mp_machine_pwm_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
@@ -166,12 +167,12 @@ static const mp_arg_t allowed_args[] = {
166
167
{ MP_QSTR_duty_u16 , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = -1 } },
167
168
{ MP_QSTR_duty_ns , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = -1 } },
168
169
{ MP_QSTR_invert , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = -1 } },
169
- { MP_QSTR_id , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = -1 } },
170
+ { MP_QSTR_device , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = -1 } },
170
171
{ MP_QSTR_channel , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = -1 } },
171
172
};
172
173
173
174
STATIC void mp_machine_pwm_init_helper (const machine_pwm_obj_t * self , size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
174
- enum { ARG_pin , ARG_freq , ARG_duty , ARG_duty_u16 , ARG_duty_ns , ARG_invert , ARG_id , ARG_channel };
175
+ enum { ARG_pin , ARG_freq , ARG_duty , ARG_duty_u16 , ARG_duty_ns , ARG_invert , ARG_device , ARG_channel };
175
176
176
177
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
177
178
mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
@@ -199,7 +200,7 @@ STATIC void mp_machine_pwm_init_helper(const machine_pwm_obj_t *self, size_t n_a
199
200
200
201
201
202
STATIC mp_obj_t mp_machine_pwm_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * all_args ) {
202
- enum { ARG_pin , ARG_freq , ARG_duty , ARG_duty_u16 , ARG_duty_ns , ARG_invert , ARG_id , ARG_channel };
203
+ enum { ARG_pin , ARG_freq , ARG_duty , ARG_duty_u16 , ARG_duty_ns , ARG_invert , ARG_device , ARG_channel };
8000
203
204
204
205
// parse args
205
206
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
@@ -217,9 +218,9 @@ STATIC mp_obj_t mp_machine_pwm_make_new(const mp_obj_type_t *type, size_t n_args
217
218
// If just the ID is given, use channel 0
218
219
// If none is given, attempt to find an unused object.
219
220
int pwm_id = -1 ;
220
- if (args [ARG_id ].u_int != -1 ) {
221
- if (args [ARG_id ].u_int >= 0 && args [ARG_id ].u_int < MP_ARRAY_SIZE (machine_hard_pwm_instances )) {
222
- pwm_id = args [ARG_id ].u_int * NRF_PWM_CHANNEL_COUNT ;
221
+ if (args [ARG_device ].u_int != -1 ) {
222
+ if (args [ARG_device ].u_int >= 0 && args [ARG_device ].u_int < MP_ARRAY_SIZE (machine_hard_pwm_instances )) {
223
+ pwm_id = args [ARG_device ].u_int * NRF_PWM_CHANNEL_COUNT ;
223
224
if (args [ARG_channel ].u_int != -1 ) {
224
225
if (args [ARG_channel ].u_int >= 0 && args [ARG_channel ].u_int < NRF_PWM_CHANNEL_COUNT ) {
225
226
pwm_id += args [ARG_channel ].u_int ;
@@ -363,28 +364,25 @@ STATIC void machine_hard_pwm_start(const machine_pwm_obj_t *self) {
363
364
364
365
nrfx_pwm_init (self -> p_pwm , & config , NULL , NULL );
365
366
366
- volatile static uint16_t pwm_seq [4 ];
367
-
368
367
for (int i = 0 ; i < NRF_PWM_CHANNEL_COUNT ; i ++ ) {
369
368
uint16_t pulse_width = 0 ;
370
369
if (self -> p_config -> duty_mode [i ] == DUTY_PERCENT ) {
371
370
pulse_width = ((period * self -> p_config -> duty [i ]) / 100 );
372
371
} else if (self -> p_config -> duty_mode [i ] == DUTY_U16 ) {
373
372
pulse_width = ((period * self -> p_config -> duty [i ]) / 65536 );
374
- }
375
- if (self -> p_config -> duty_mode [i ] == DUTY_NS ) {
373
+ } else if (self -> p_config -> duty_mode [i ] == DUTY_NS ) {
376
374
pulse_width = (uint64_t )self -> p_config -> duty [i ] * tick_freq / 1000000000ULL ;
377
375
}
378
376
379
377
if (self -> p_config -> mode [i ] == MODE_HIGH_LOW ) {
380
- pwm_seq [i ] = 0x8000 | pulse_width ;
378
+ self -> p_config -> pwm_seq [i ] = 0x8000 | pulse_width ;
381
379
} else {
382
- pwm_seq [i ] = pulse_width ;
380
+ self -> p_config -> pwm_seq [i ] = pulse_width ;
383
381
}
384
382
}
385
383
386
384
const nrf_pwm_sequence_t pwm_sequence = {
387
- .values .p_raw = (const uint16_t * )& pwm_seq ,
385
+ .values .p_raw = (const uint16_t * )& self -> p_config -> pwm_seq ,
388
386
.length = 4 ,
389
387
.repeats = 0 ,
390
388
.end_delay = 0
0 commit comments