-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy path0028-RP2040-add-pwm-driver.patch
251 lines (247 loc) · 7.84 KB
/
0028-RP2040-add-pwm-driver.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
From 074599cf1116d7343e6de80f7fa47fe8d01d38ed Mon Sep 17 00:00:00 2001
From: Martino Facchin <m.facchin@arduino.cc>
Date: Tue, 23 Feb 2021 16:48:23 +0100
Subject: [PATCH 028/110] RP2040: add pwm driver
---
.../TARGET_RP2040/objects.h | 9 +
.../TARGET_RP2040/pwmout_api.c | 198 ++++++++++++++++++
targets/targets.json | 1 +
3 files changed, 208 insertions(+)
create mode 100644 targets/TARGET_RASPBERRYPI/TARGET_RP2040/pwmout_api.c
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/objects.h b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/objects.h
index 061cee5908..5e5f2f0d6a 100644
--- a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/objects.h
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/objects.h
@@ -101,6 +101,15 @@ struct spi_s {
spi_inst_t * dev;
};
+struct pwmout_s {
+ PinName pin;
+ uint8_t slice;
+ uint8_t channel;
+ uint16_t period;
+ float percent;
+ pwm_config cfg;
+};
+
struct flash_s {
/* nothing to be stored for now */
uint32_t dummy;
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pwmout_api.c b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pwmout_api.c
new file mode 100644
index 0000000000..5f5d847995
--- /dev/null
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/pwmout_api.c
@@ -0,0 +1,198 @@
+/*
+ * Copyright (c) 2018 Nordic Semiconductor ASA
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list
+ * of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
+ * integrated circuit in a product or a software update for such product, must reproduce
+ * the above copyright notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without specific prior
+ * written permission.
+ *
+ * 4. This software, with or without modification, must only be used with a
+ * Nordic Semiconductor ASA integrated circuit.
+ *
+ * 5. Any software provided in binary or object form under this license must not be reverse
+ * engineered, decompiled, modified and/or disassembled.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#if DEVICE_PWMOUT
+
+#include "hal/pwmout_api.h"
+#include "PeripheralPins.h"
+#include "pinmap.h"
+#include "hardware/pwm.h"
+#include "hardware/clocks.h"
+
+const uint count_top = 1000;
+
+/** Initialize the pwm out peripheral and configure the pin
+ *
+ * Parameter obj The pwmout object to initialize
+ * Parameter pin The pwmout pin to initialize
+ */
+void pwmout_init(pwmout_t *obj, PinName pin)
+{
+ MBED_ASSERT(obj);
+
+ /* Populate PWM object with default values. */
+ obj->slice = pwm_gpio_to_slice_num(pin);
+ obj->channel = pwm_gpio_to_channel(pin);
+ obj->pin = pin;
+ obj->period = 0;
+ obj->percent = 0.5f;
+
+ obj->cfg = pwm_get_default_config();
+ pwm_config_set_wrap(&(obj->cfg), count_top);
+
+ pwm_init(obj->slice, &(obj->cfg), false);
+ gpio_set_function(pin, GPIO_FUNC_PWM);
+}
+
+/** Deinitialize the pwmout object
+ *
+ * Parameter obj The pwmout object
+ */
+void pwmout_free(pwmout_t *obj)
+{
+ MBED_ASSERT(obj);
+ pwm_set_enabled(obj->slice, false);
+}
+
+/** Set the output duty-cycle in range <0.0f, 1.0f>
+ *
+ * pulse 0.0f represents 0 percentage, 1.0f represents 100 percent.
+ * Parameter obj The pwmout object
+ * Parameter percent The floating-point percentage number
+ */
+void pwmout_write(pwmout_t *obj, float percent)
+{
+ obj->percent = percent;
+ pwm_set_gpio_level(obj->pin, percent * (count_top + 1));
+}
+
+/** Read the current float-point output duty-cycle
+ *
+ * Parameter obj The pwmout object
+ * Return A floating-point output duty-cycle
+ */
+float pwmout_read(pwmout_t *obj)
+{
+ /* Return percentage stored in object instead of calculating the value.
+ * This prevents floating point rounding errors.
+ */
+ return obj->percent;
+}
+
+/** Set the PWM period specified in seconds, keeping the duty cycle the same
+ *
+ * Periods smaller than microseconds (the lowest resolution) are set to zero.
+ * Parameter obj The pwmout object
+ * Parameter seconds The floating-point seconds period
+ */
+void pwmout_period(pwmout_t *obj, float period)
+{
+ /* Set new period. */
+ pwmout_period_us(obj, period * 1000000);
+}
+
+/** Set the PWM period specified in miliseconds, keeping the duty cycle the same
+ *
+ * Parameter obj The pwmout object
+ * Parameter ms The milisecond period
+ */
+void pwmout_period_ms(pwmout_t *obj, int period)
+{
+ /* Set new period. */
+ pwmout_period_us(obj, period * 1000);
+}
+
+/** Set the PWM period specified in microseconds, keeping the duty cycle the same
+ *
+ * Parameter obj The pwmout object
+ * Parameter us The microsecond period
+ */
+void pwmout_period_us(pwmout_t *obj, int period)
+{
+ obj->period = period;
+
+ // min_period should be 8us
+ uint32_t min_period = 1000000 * count_top / clock_get_hz(clk_sys);
+
+ pwm_config_set_clkdiv(&(obj->cfg), (float)period / (float)min_period);
+ pwm_init(obj->slice, &(obj->cfg), false);
+}
+
+int pwmout_read_period_us(pwmout_t *obj)
+{
+ return obj->period;
+}
+
+/** Set the PWM pulsewidth specified in seconds, keeping the period the same.
+ *
+ * Parameter obj The pwmout object
+ * Parameter seconds The floating-point pulsewidth in seconds
+ */
+void pwmout_pulsewidth(pwmout_t *obj, float pulse)
+{
+ pwmout_pulsewidth_us(obj, pulse * 1000000);
+}
+
+/** Set the PWM pulsewidth specified in miliseconds, keeping the period the same.
+ *
+ * Parameter obj The pwmout object
+ * Parameter ms The floating-point pulsewidth in miliseconds
+ */
+void pwmout_pulsewidth_ms(pwmout_t *obj, int pulse)
+{
+ pwmout_pulsewidth_us(obj, pulse * 1000);
+}
+
+/** Set the PWM pulsewidth specified in microseconds, keeping the period the same.
+ *
+ * Parameter obj The pwmout object
+ * Parameter us The floating-point pulsewidth in microseconds
+ */
+void pwmout_pulsewidth_us(pwmout_t *obj, int pulse)
+{
+ /* Cap pulsewidth to period. */
+ if (pulse > obj->period) {
+ pulse = obj->period;
+ }
+
+ obj->percent = (float) pulse / (float) obj->period;
+
+ /* Restart instance with new values. */
+ pwmout_write(obj, obj->percent);
+}
+
+int pwmout_read_pulsewidth_us(pwmout_t *obj) {
+ return (obj->period) * (obj->percent);
+}
+
+const PinMap *pwmout_pinmap()
+{
+ return PinMap_PWM_OUT;
+}
+
+#endif // DEVICE_PWMOUT
diff --git a/targets/targets.json b/targets/targets.json
index c9601649d8..5d7c81b24a 100644
--- a/targets/targets.json
+++ b/targets/targets.json
@@ -8976,6 +8976,7 @@
"I2C",
"PORT_IN",
"PORT_OUT",
+ "PWMOUT",
"SERIAL",
"SPI",
"USBDEVICE"
--
2.34.1