Skip to content

Commit 898407d

Browse files
robert-hhdpgeorge
authored andcommitted
ports: Make PWM duty_u16 have an upper value of 65535 across all ports.
The following ports used 65536 as the upper value (100% duty cycle) and are changed in this commit to use 65535: esp8266, mimxrt, nrf, samd. Tested that output is high at `duty_u16(65535)` and low at `duty_u16(0)`. Also verified that at `duty_u16(32768)` the high and low pulse have the same length. Partially reverts micropython#10850, commits 9c7ad68 and 2ac643c. Signed-off-by: robert-hh <[email protected]>
1 parent 5dc9eda commit 898407d

File tree

7 files changed

+16
-16
lines changed

7 files changed

+16
-16
lines changed

docs/mimxrt/quickref.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ PWM Constructor
193193

194194
- *freq* should be an integer which sets the frequency in Hz for the
195195
PWM cycle. The valid frequency range is 15 Hz resp. 18Hz resp. 24Hz up to > 1 MHz.
196-
- *duty_u16* sets the duty cycle as a ratio ``duty_u16 / 65536``.
196+
- *duty_u16* sets the duty cycle as a ratio ``duty_u16 / 65535``.
197197
The duty cycle of a X channel can only be changed, if the A and B channel
198198
of the respective submodule is not used. Otherwise the duty_16 value of the
199199
X channel is 32768 (50%).
@@ -231,7 +231,7 @@ is created by dividing the pwm_clk signal by an integral factor, according to th
231231

232232
f = pwm_clk / (2**n * m)
233233

234-
with n being in the range of 0..7, and m in the range of 2..65536. pmw_clk is 125Mhz
234+
with n being in the range of 0..7, and m in the range of 2..65535. pmw_clk is 125Mhz
235235
for MIMXRT1010/1015/1020, 150 MHz for MIMXRT1050/1060/1064 and 160MHz for MIMXRT1170.
236236
The lowest frequency is pwm_clk/2**23 (15, 18, 20Hz). The highest frequency with
237237
U16 resolution is pwm_clk/2**16 (1907, 2288, 2441 Hz), the highest frequency
@@ -255,7 +255,7 @@ Use the :ref:`machine.ADC <machine.ADC>` class::
255255
from machine import ADC
256256

257257
adc = ADC(Pin('A2')) # create ADC object on ADC pin
258-
adc.read_u16() # read value, 0-65536 across voltage range 0.0v - 3.3v
258+
adc.read_u16() # read value, 0-65535 across voltage range 0.0v - 3.3v
259259

260260
The resolution of the ADC is 12 bit with 10 to 11 bit accuracy, irrespective of the
261261
value returned by read_u16(). If you need a higher resolution or better accuracy, use

docs/samd/quickref.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ PWM Constructor
215215

216216
- *freq* should be an integer which sets the frequency in Hz for the
217217
PWM cycle. The valid frequency range is 1 Hz to 24 MHz.
218-
- *duty_u16* sets the duty cycle as a ratio ``duty_u16 / 65536``.
218+
- *duty_u16* sets the duty cycle as a ratio ``duty_u16 / 65535``.
219219
- *duty_ns* sets the pulse width in nanoseconds. The limitation for X channels
220220
apply as well.
221221
- *invert*\=True|False. Setting a bit inverts the respective output.
@@ -246,7 +246,7 @@ Use the :ref:`machine.ADC <machine.ADC>` class::
246246
from machine import ADC
247247

248248
adc0 = ADC(Pin('A0')) # create ADC object on ADC pin, average=16
249-
adc0.read_u16() # read value, 0-65536 across voltage range 0.0v - 3.3v
249+
adc0.read_u16() # read value, 0-65535 across voltage range 0.0v - 3.3v
250250
adc1 = ADC(Pin('A1'), average=1) # create ADC object on ADC pin, average=1
251251

252252
The resolution of the ADC is 12 bit with 12 bit accuracy, irrespective of the

ports/esp8266/machine_pwm.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static void mp_machine_pwm_init_helper(machine_pwm_obj_t *self, size_t n_args, c
8080
pwm_set_duty(args[ARG_duty].u_int, self->channel);
8181
}
8282
if (args[ARG_duty_u16].u_int != -1) {
83-
pwm_set_duty(args[ARG_duty_u16].u_int * 1000 / 65536, self->channel);
83+
pwm_set_duty(args[ARG_duty_u16].u_int * 1000 / 65535, self->channel);
8484
}
8585
if (args[ARG_duty_ns].u_int != -1) {
8686
uint32_t freq = pwm_get_freq(0);
@@ -164,13 +164,13 @@ static void mp_machine_pwm_duty_set(machine_pwm_obj_t *self, mp_int_t duty) {
164164

165165
static mp_obj_t mp_machine_pwm_duty_get_u16(machine_pwm_obj_t *self) {
166166
set_active(self, true);
167-
return MP_OBJ_NEW_SMALL_INT(pwm_get_duty(self->channel) * 65536 / 1024);
167+
return MP_OBJ_NEW_SMALL_INT(pwm_get_duty(self->channel) * 65535 / 1024);
168168
}
169169

170170
static void mp_machine_pwm_duty_set_u16(machine_pwm_obj_t *self, mp_int_t duty) {
171171
set_active(self, false);
172172
self->duty_ns = -1;
173-
pwm_set_duty(duty * 1024 / 65536, self->channel);
173+
pwm_set_duty(duty * 1024 / 65535, self->channel);
174174
pwm_start();
175175
}
176176

ports/mimxrt/hal/pwm_backport.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ void PWM_UpdatePwmDutycycle_u16(
3636

3737
// Setup the PWM dutycycle of channel A or B
3838
if (pwmSignal == kPWM_PwmA) {
39-
if (dutyCycle >= 65536) {
39+
if (dutyCycle >= PWM_FULL_SCALE) {
4040
base->SM[subModule].VAL2 = 0;
4141
base->SM[subModule].VAL3 = pulseCnt;
4242
} else {
4343
base->SM[subModule].VAL2 = center - pwmHighPulse / 2;
4444
base->SM[subModule].VAL3 = base->SM[subModule].VAL2 + pwmHighPulse;
4545
}
4646
} else {
47-
if (dutyCycle >= 65536) {
47+
if (dutyCycle >= PWM_FULL_SCALE) {
4848
base->SM[subModule].VAL4 = 0;
4949
base->SM[subModule].VAL5 = pulseCnt;
5050
} else {
@@ -160,7 +160,7 @@ status_t QTMR_SetupPwm_u16(TMR_Type *base, qtmr_channel_selection_t channel, uin
160160
if (dutyCycleU16 == 0) {
161161
// Clear the output at the next compare
162162
reg |= (TMR_CTRL_LENGTH_MASK | TMR_CTRL_OUTMODE(kQTMR_ClearOnCompare));
163-
} else if (dutyCycleU16 >= 65536) {
163+
} else if (dutyCycleU16 >= PWM_FULL_SCALE) {
164164
// Set the output at the next compare
165165
reg |= (TMR_CTRL_LENGTH_MASK | TMR_CTRL_OUTMODE(kQTMR_SetOnCompare));
166166
} else {

ports/mimxrt/hal/pwm_backport.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ typedef struct _pwm_signal_param_u16
2424
uint16_t deadtimeValue; // The deadtime value; only used if channel pair is operating in complementary mode
2525
} pwm_signal_param_u16_t;
2626

27-
#define PWM_FULL_SCALE (65536UL)
27+
#define PWM_FULL_SCALE (65535UL)
2828

2929
void PWM_UpdatePwmDutycycle_u16(PWM_Type *base, pwm_submodule_t subModule,
3030
pwm_channels_t pwmSignal, uint32_t dutyCycle, uint16_t center);

ports/nrf/modules/machine/pwm.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ static mp_obj_t mp_machine_pwm_duty_get(machine_pwm_obj_t *self) {
285285
if (self->p_config->duty_mode[self->channel] == DUTY_PERCENT) {
286286
return MP_OBJ_NEW_SMALL_INT(self->p_config->duty[self->channel]);
287287
} else if (self->p_config->duty_mode[self->channel] == DUTY_U16) {
288-
return MP_OBJ_NEW_SMALL_INT(self->p_config->duty[self->channel] * 100 / 65536);
288+
return MP_OBJ_NEW_SMALL_INT(self->p_config->duty[self->channel] * 100 / 65535);
289289
} else {
290290
return MP_OBJ_NEW_SMALL_INT(-1);
291291
}
@@ -301,7 +301,7 @@ static mp_obj_t mp_machine_pwm_duty_get_u16(machine_pwm_obj_t *self) {
301301
if (self->p_config->duty_mode[self->channel] == DUTY_U16) {
302302
return MP_OBJ_NEW_SMALL_INT(self->p_config->duty[self->channel]);
303303
} else if (self->p_config->duty_mode[self->channel] == DUTY_PERCENT) {
304-
return MP_OBJ_NEW_SMALL_INT(self->p_config->duty[self->channel] * 65536 / 100);
304+
return MP_OBJ_NEW_SMALL_INT(self->p_config->duty[self->channel] * 65535 / 100);
305305
} else {
306306
return MP_OBJ_NEW_SMALL_INT(-1);
307307
}
@@ -365,7 +365,7 @@ static void machine_hard_pwm_start(const machine_pwm_obj_t *self) {
365365
if (self->p_config->duty_mode[i] == DUTY_PERCENT) {
366366
pulse_width = ((period * self->p_config->duty[i]) / 100);
367367
} else if (self->p_config->duty_mode[i] == DUTY_U16) {
368-
pulse_width = ((period * self->p_config->duty[i]) / 65536);
368+
pulse_width = ((period * self->p_config->duty[i]) / 65535);
369369
} else if (self->p_config->duty_mode[i] == DUTY_NS) {
370370
pulse_width = (uint64_t)self->p_config->duty[i] * tick_freq / 1000000000ULL;
371371
}

ports/samd/machine_pwm.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ typedef struct _machine_pwm_obj_t {
5454
#define PWM_CLK_READY (1)
5555
#define PWM_TCC_ENABLED (2)
5656
#define PWM_MASTER_CLK (get_peripheral_freq())
57-
#define PWM_FULL_SCALE (65536)
57+
#define PWM_FULL_SCALE (65535)
5858
#define PWM_UPDATE_TIMEOUT (2000)
5959

6060
#define VALUE_NOT_SET (-1)

0 commit comments

Comments
 (0)