Skip to content

Commit b38132b

Browse files
committed
nrf/PWM: Rename the keyword argument id to device.
- Rename the keyword argument id to device. That's consistent with the SAMD port as the other port allowing to specify it. - Move the pwm_seq array to the p_config data structure. That prevents potential resource collisions between PWM devices.
1 parent 9faa704 commit b38132b

File tree

1 file changed

+12
-14
lines changed
  • ports/nrf/modules/machine

1 file changed

+12
-14
lines changed

ports/nrf/modules/machine/pwm.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ typedef struct {
7070
pwm_mode_t mode[NRF_PWM_CHANNEL_COUNT];
7171
pwm_duty_t duty_mode[NRF_PWM_CHANNEL_COUNT];
7272
uint32_t duty[NRF_PWM_CHANNEL_COUNT];
73+
uint16_t pwm_seq[4];
7374
pwm_run_t active;
7475
bool defer_start;
7576
int8_t freq_div;
@@ -137,7 +138,7 @@ STATIC int hard_pwm_find() {
137138
return j * NRF_PWM_CHANNEL_COUNT;
138139
}
139140
}
140-
mp_raise_ValueError(MP_ERROR_TEXT("no free PWM object"));
141+
mp_raise_ValueError(MP_ERROR_TEXT("all PWM devices in use"));
141142
}
142143

143144
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[] = {
166167
{ MP_QSTR_duty_u16, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
167168
{ MP_QSTR_duty_ns, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
168169
{ 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} },
170171
{ MP_QSTR_channel, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
171172
};
172173

173174
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 };
175176

176177
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
177178
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
199200

200201

201202
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 };
203204

204205
// parse args
205206
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
217218
// If just the ID is given, use channel 0
218219
// If none is given, attempt to find an unused object.
219220
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;
223224
if (args[ARG_channel].u_int != -1) {
224225
if (args[ARG_channel].u_int >= 0 && args[ARG_channel].u_int < NRF_PWM_CHANNEL_COUNT) {
225226
pwm_id += args[ARG_channel].u_int;
@@ -363,28 +364,25 @@ STATIC void machine_hard_pwm_start(const machine_pwm_obj_t *self) {
363364

364365
nrfx_pwm_init(self->p_pwm, &config, NULL, NULL);
365366

366-
volatile static uint16_t pwm_seq[4];
367-
368367
for (int i = 0; i < NRF_PWM_CHANNEL_COUNT; i++) {
369368
uint16_t pulse_width = 0;
370369
if (self->p_config->duty_mode[i] == DUTY_PERCENT) {
371370
pulse_width = ((period * self->p_config->duty[i]) / 100);
372371
} else if (self->p_config->duty_mode[i] == DUTY_U16) {
373372
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) {
376374
pulse_width = (uint64_t)self->p_config->duty[i] * tick_freq / 1000000000ULL;
377375
}
378376

379377
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;
381379
} else {
382-
pwm_seq[i] = pulse_width;
380+
self->p_config->pwm_seq[i] = pulse_width;
383381
}
384382
}
385383

386384
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,
388386
.length = 4,
389387
.repeats = 0,
390388
.end_delay = 0

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy