Skip to content

Commit fa637de

Browse files
author
Richard Unger
committed
RP2040 PWM driver now supports pre-scaler
1 parent 3cee9cf commit fa637de

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/drivers/hardware_specific/rp2040/rp2040_mcu.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
#include "../../hardware_api.h"
1010
#include "./rp2040_mcu.h"
1111
#include "hardware/pwm.h"
12+
#include "hardware/clocks.h"
1213

1314
#define _PWM_FREQUENCY 24000
1415
#define _PWM_FREQUENCY_MAX 66000
15-
#define _PWM_FREQUENCY_MIN 5000
16+
#define _PWM_FREQUENCY_MIN 1
1617

1718

1819

@@ -30,12 +31,12 @@ void setupPWM(int pin, long pwm_frequency, bool invert, RP2040DriverParams* para
3031
params->pins[index] = pin;
3132
params->slice[index] = slice;
3233
params->chan[index] = chan;
33-
uint32_t sysclock_hz = machine.freq();
34+
uint32_t sysclock_hz = frequency_count_khz(CLOCKS_FC0_SRC_VALUE_CLK_SYS) * 1000;
3435
uint32_t factor = 4096 * 2 * pwm_frequency;
3536
uint32_t div = sysclock_hz / factor;
3637
if (sysclock_hz % factor !=0) div+=1;
3738
if (div < 16) div = 16;
38-
uint32_t wrapvalue = sysclock_hz *16 / div / pwm_frequency - 1;
39+
uint32_t wrapvalue = (sysclock_hz * 8) / div / pwm_frequency - 1;
3940
#ifdef SIMPLEFOC_DEBUG_RP2040
4041
SimpleFOCDebug::print("Configuring pin ");
4142
SimpleFOCDebug::print(pin);
@@ -50,7 +51,7 @@ void setupPWM(int pin, long pwm_frequency, bool invert, RP2040DriverParams* para
5051
SimpleFOCDebug::print(".");
5152
SimpleFOCDebug::print((int)(div&0xF));
5253
SimpleFOCDebug::print(" top value ");
53-
SimpleFOCDebug::println(wrapvalue);
54+
SimpleFOCDebug::println((int)wrapvalue);
5455
#endif
5556
if (wrapvalue < 999)
5657
SimpleFOCDebug::println("Warning: PWM resolution is low.");

0 commit comments

Comments
 (0)