Skip to content

Commit e4e06e4

Browse files
committed
mbed: add pwm patch for rp2040
1 parent 84268ae commit e4e06e4

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
From c3103e282514054f521dbad2a3956a7eef846fad Mon Sep 17 00:00:00 2001
2+
From: Alexander Entinger <[email protected]>
3+
Date: Fri, 10 Feb 2023 09:41:36 +0100
4+
Subject: [PATCH] Fix PWM initialisation when using both channel A and channel
5+
B of a single PWM unit.
6+
7+
---
8+
.../pico-sdk/rp2_common/hardware_pwm/include/hardware/pwm.h | 6 ++++--
9+
targets/TARGET_RASPBERRYPI/TARGET_RP2040/pwmout_api.c | 4 ++--
10+
2 files changed, 6 insertions(+), 4 deletions(-)
11+
12+
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_pwm/include/hardware/pwm.h b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_pwm/include/hardware/pwm.h
13+
index 634375e764..201be3c7dd 100644
14+
--- a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_pwm/include/hardware/pwm.h
15+
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pico-sdk/rp2_common/hardware_pwm/include/hardware/pwm.h
16+
@@ -177,6 +177,8 @@ static inline void pwm_config_set_wrap(pwm_config *c, uint16_t wrap) {
17+
c->top = wrap;
18+
}
19+
20+
+static inline void pwm_set_chan_level(uint slice_num, uint chan, uint16_t level);
21+
+
22+
/** \brief Initialise a PWM with settings from a configuration object
23+
* \ingroup hardware_pwm
24+
*
25+
@@ -188,12 +190,12 @@ static inline void pwm_config_set_wrap(pwm_config *c, uint16_t wrap) {
26+
* \param start If true the PWM will be started running once configured. If false you will need to start
27+
* manually using \ref pwm_set_enabled() or \ref pwm_set_mask_enabled()
28+
*/
29+
-static inline void pwm_init(uint slice_num, pwm_config *c, bool start) {
30+
+static inline void pwm_init(uint slice_num, uint chan, pwm_config *c, bool start) {
31+
check_slice_num_param(slice_num);
32+
pwm_hw->slice[slice_num].csr = 0;
33+
34+
pwm_hw->slice[slice_num].ctr = PWM_CH0_CTR_RESET;
35+
- pwm_hw->slice[slice_num].cc = PWM_CH0_CC_RESET;
36+
+ pwm_set_chan_level(slice_num, chan, PWM_CH0_CC_A_RESET);
37+
pwm_hw->slice[slice_num].top = c->top;
38+
pwm_hw->slice[slice_num].div = c->div;
39+
pwm_hw->slice[slice_num].csr = c->csr | (bool_to_bit(start) << PWM_CH0_CSR_EN_LSB);
40+
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pwmout_api.c b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pwmout_api.c
41+
index e4bc4daa80..46c5b51df1 100644
42+
--- a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pwmout_api.c
43+
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pwmout_api.c
44+
@@ -66,7 +66,7 @@ void pwmout_init(pwmout_t *obj, PinName pin)
45+
obj->cfg = pwm_get_default_config();
46+
pwm_config_set_wrap(&(obj->cfg), count_top);
47+
48+
- pwm_init(obj->slice, &(obj->cfg), false);
49+
+ pwm_init(obj->slice, obj->channel, &(obj->cfg), false);
50+
gpio_set_function(pin, GPIO_FUNC_PWM);
51+
}
52+
53+
@@ -142,7 +142,7 @@ void pwmout_period_us(pwmout_t *obj, int period)
54+
uint32_t min_period = 1000000 * count_top / clock_get_hz(clk_sys);
55+
56+
pwm_config_set_clkdiv(&(obj->cfg), (float)period / (float)min_period);
57+
- pwm_init(obj->slice, &(obj->cfg), false);
58+
+ pwm_init(obj->slice, obj->channel, &(obj->cfg), false);
59+
}
60+
61+
int pwmout_read_period_us(pwmout_t *obj)
62+
--
63+
2.39.1
64+

0 commit comments

Comments
 (0)