8000 rp2/machine_pwm: Fix default TOP value of 65535 by changing it to 65534. · micropython/micropython@27df56f · GitHub
[go: up one dir, main page]

Skip to content

Commit 27df56f

Browse files
author
Paul Grayson
committed
rp2/machine_pwm: Fix default TOP value of 65535 by changing it to 65534.
1 parent 9e6885a commit 27df56f

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

ports/rp2/machine_pwm.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ enum {
4848
DUTY_NS
4949
};
5050

51+
// Maximum "top" is set at 65534 to be able to achieve 100% duty with 65535.
52+
#define TOP_MAX 65534
53+
5154
STATIC machine_pwm_obj_t machine_pwm_obj[] = {
5255
{{&machine_pwm_type}, 0, PWM_CHAN_A, DUTY_NOT_SET, 0},
5356
{{&machine_pwm_type}, 0, PWM_CHAN_B, DUTY_NOT_SET, 0},
@@ -132,8 +135,6 @@ STATIC mp_obj_t mp_machine_pwm_freq_get(machine_pwm_obj_t *self) {
132135

133136
STATIC void mp_machine_pwm_freq_set(machine_pwm_obj_t *self, mp_int_t freq) {
134137
// Set the frequency, making "top" as large as possible for maximum resolution.
135-
// Maximum "top" is set at 65534 to be able to achieve 100% duty with 65535.
136-
#define TOP_MAX 65534
137138
uint32_t source_hz = clock_get_hz(clk_sys);
138139
uint32_t div16;
139140
uint32_t top;
@@ -185,6 +186,13 @@ STATIC mp_obj_t mp_machine_pwm_duty_get_u16(machine_pwm_obj_t *self) {
185186
STATIC void mp_machine_pwm_duty_set_u16(machine_pwm_obj_t *self, mp_int_t duty_u16) {
186187
uint32_t top = pwm_hw->slice[self->slice].top;
187188

189+
if(top > TOP_MAX) {
190+
// Fix the RP2040 default top of 65535, which does not allow
191+
// 100% duty cycle.
192+
top = TOP_MAX;
193+
pwm_hw->slice[self->slice].top = TOP_MAX;
194+
}
195+
188196
// Use rounding here to set it as accurately as possible.
189197
uint32_t cc = (duty_u16 * (top + 1) + 65535 / 2) / 65535;
190198
pwm_set_chan_level(self->slice, self->channel, cc);

0 commit comments

Comments
 (0)
0