forked from lowRISC/opentitan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dif_pwm.h
330 lines (307 loc) · 11 KB
/
dif_pwm.h
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0
#ifndef OPENTITAN_SW_DEVICE_LIB_DIF_DIF_PWM_H_
#define OPENTITAN_SW_DEVICE_LIB_DIF_DIF_PWM_H_
/**
* @file
* @brief <a href="/hw/ip/pwm/doc/">PWM</a> Device Interface Functions
*
* The PWM block contains a 16-bit "phase counter" that controls each channel's
* output signal. The "phase counter" acts as a point of reference for a single
* PWM "pulse cycle", that is further broken into "beats", depending on the
* `beats_per_cycle` parameter. Specifically, a "pulse cycle" may contain [2,
* 2^16] "beats". Within a "pulse cycle", users can configure the duty cycle in
* number of "beats". Additionally, the duration of a single "beat", is computed
* by dividing the core clock frequency by `clock_divisor + 1`.
*
* PWM "pulse cycle" defined by 16-bit phase counter
* ___________________________________________________________
* | |
* v v
* |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| ... |-|-|-|-|
*
* |-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-| ... |-|-|-|-|
* min beat size = 1 phase counter tick --> 2^16 "beats" / "pulse cycle"
*
* |---------------------------------------------- ... | ... --|
* max beat size = 2^15 phase counter ticks --> 2 "beats" / "pulse cycle"
*/
#include <stdint.h>
#include "sw/device/lib/dif/autogen/dif_pwm_autogen.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
/**
* Helper X macro for defining enums and case statements related to PWM
* channels. If an additional channel is ever added to the hardware, this list
* can be updated.
*/
#define DIF_PWM_CHANNEL_LIST(X) \
X(0) \
X(1) \
X(2) \
X(3) \
X(4) \
X(5)
/**
* Helper macro for defining a `dif_pwm_channel_t` enumeration constant.
* @channel_ PWM channel of the enumeration constant.
*/
#define PWM_CHANNEL_ENUM_INIT_(channel_) \
kDifPwmChannel##channel_ = 1U << channel_,
/**
* A PWM channel.
*/
typedef enum dif_pwm_channel {
DIF_PWM_CHANNEL_LIST(PWM_CHANNEL_ENUM_INIT_)
} dif_pwm_channel_t;
#undef PWM_CHANNEL_ENUM_INIT_
/**
* Runtime configuration for PWM.
*
* This struct describes runtime configuration for one-time configuration of the
* PWM "pulse cycle" and "beat" durations that impact all PWM channels.
*/
typedef struct dif_pwm_config {
/**
* The core clock frequency divisor that determines the period of a single
* "beat" within a PWM "pulse cycle".
*
* Valid range: [0, 2^26)
*
* A value of zero, configures the "beat" period to the core clock period.
*/
uint32_t clock_divisor;
/**
* The total number of "beats" in a "pulse cycle", including both "on" and
* "off" beats in a "pulse cycle".
*
* Valid range: [2, 2^16]
*
* Note: while any value in the range is acceptable, values will be rounded
* down to closest power-of-two.
*
* A "beat" represents a unit of time of the PWM output signal. Higher values
* provide higher duty cycle resolutions, at the expense of longer "pulse
* cycles", while lower values provide shorter "pulse cycles", at the expense
* of lower duty cycle resolutions (since duty cycles are configured in
* "beats" / "pulse cycle").
*/
uint32_t beats_per_pulse_cycle;
} dif_pwm_config_t;
/**
* A PWM channel mode.
*/
typedef enum dif_pwm_mode {
/**
* The PWM duty cycle is set by the firmware and remains constant.
*/
kDifPwmModeFirmware = 0,
/**
* The PWM duty cycle linearly sweeps between both primary and secondary
* firmware-configured values, based on a firmware-configured step size.
*/
kDifPwmModeHeartbeat = 1,
/**
* The PWM duty cycle alternates between both primary and secondary
* firmware-configured values, based on two firmware-configured durations.
*/
kDifPwmModeBlink = 2,
} dif_pwm_mode_t;
/**
* A PWM channel polarity.
*/
typedef enum dif_pwm_polarity {
/**
* A PWM signal is active-high.
*/
kDifPwmPolarityActiveHigh = 0,
/**
* A PWM signal is active-low.
*/
kDifPwmPolarityActiveLow = 1,
} dif_pwm_polarity_t;
/**
* Runtime configuration for a specific PWM channel.
*
* This struct describes runtime configuration for one-time configuration of a
* specific PWM channel.
*/
typedef struct dif_pwm_channel_config {
/**
* Primary duty cycle, in number of "beats" / "pulse cycle".
*
* Valid range: [0, beats_per_pulse_cycle)
*
* Note: the raw value written to the `A_*` bitfield in each PWM channel's
* `DUTY_CYCLE_*` CSR is in units of "phase counter ticks", not "beats".
* However, the hardware only takes into account the first `DC_RESN` + 1
* MSBs of the raw duty cycle value to determine the number of "beats"
* for a given duty cycle. To make this configuration easier, the
* software manages the conversion from "beats_per_cycle" to
* "phase_counter_ticks" under the hood.
*/
uint16_t duty_cycle_a;
/**
* Secondary duty cycle, in number of "beats" / "pulse cycle", that is only
* relevant in heartbeat and blink modes.
*
* Valid range: [0, beats_per_pulse_cycle)
*
* Note: above notes for `duty_cycle_a` apply here too.
*/
uint16_t duty_cycle_b;
/**
* Phase delay at the beginning of a "pulse cycle" to delay the active
* duty cycle "beats" for, in number of "beats".
*
* Valid range: [0, beats_per_pulse_cycle)
*/
uint16_t phase_delay;
/**
* The operation mode to configure the channel in, see `dif_pwm_mode_t`.
*/
dif_pwm_mode_t mode;
/**
* The polarity to configure the channel in, see `dif_pwm_polarity_t`.
*/
dif_pwm_polarity_t polarity;
/**
* One of two blink parameters that only impact the "Heartbeat" and "Blink"
* operation modes.
*
* The meaning of this parameter is different based on the operation mode:
* - Heartbeat mode: determines the number of "pulse cycles" between
* increments/decrements to the duty cycle.
*
* - Blink mode: determines the number of "pulse cycles" to pulse at duty
* cycle A, before switching to duty cycle B.
*/
uint16_t blink_parameter_x;
/**
* One of two blink parameters that only impact the "Heartbeat" and "Blink"
* operation modes.
*
* The meaning of this parameter is different based on the operation mode:
* - Heartbeat mode: determines the increment/decrement amount in number
* of duty cycle "beats".
*
* - Blink mode: determines the number of "pulse cycles" to pulse at duty
* cycle B, before switching to duty cycle A.
*
* Note: the raw value written to the `blink_parameter_x` bitfield in each PWM
* channel's `BLINK_PARAM_*` CSR is in units of "phase counter ticks", not
* "beats". However, for ease of configuration, the software manages this
* conversion under the hood.
*/
uint16_t blink_parameter_y;
} dif_pwm_channel_config_t;
/**
* Configures "phase cycle" and "beat" durations of all PWM channels.
*
* Since changes to `CLK_DIV` and `DC_RESN` are only allowed when the PWM is
* disabled, this function has the side effect of temporarily disabling all PWM
* channels while configurations are updated, before restoring the original
* enablement state.
*
* This function should only need to be called once for the lifetime of
* `handle`.
*
* @param pwm A PWM handle.
* @param config Runtime configuration parameters.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_pwm_configure(const dif_pwm_t *pwm, dif_pwm_config_t config);
/**
* Configures a single PWM channel.
*
* Since changes to `CLK_DIV` and `DC_RESN` are only allowed when the PWM is
* disabled, this function has the side effect of temporarily disabling the
* PWM while configurations are updated, before returning the block to its
* original enablement state.
*
* This function should only need to be called once for each PWM channel that
* will be used.
*
* @param pwm A PWM handle.
* @param channel A PWM channel to configure.
* @param config Runtime configuration parameters for the channel.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_pwm_configure_channel(const dif_pwm_t *pwm,
dif_pwm_channel_t channel,
dif_pwm_channel_config_t config);
/**
* Sets the enablement state of the PWM phase counter, which controls the
* enablement of all PWM channels.
*
* @param pwm A PWM handle.
* @param enabled The enablement state to configure the PWM phase counter in.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_pwm_phase_cntr_set_enabled(const dif_pwm_t *pwm,
dif_toggle_t enabled);
/**
* Gets the enablement state of the PWM phase counter, which controls the
* enablement of all PWM channels.
*
* @param pwm A PWM handle.
* @param[out] is_enabled The enablement state of the PWM phase counter.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_pwm_phase_cntr_get_enabled(const dif_pwm_t *pwm,
dif_toggle_t *is_enabled);
/**
* Sets the enablement states of one or more PWM channels.
*
* @param pwm A PWM handle.
* @param channels The channels to enable (one or more `dif_pmw_channel_t`s
* ORed together.)
* @param enabled The enablement state to set.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_pwm_channel_set_enabled(const dif_pwm_t *pwm,
uint32_t channels,
dif_toggle_t enabled);
/**
* Gets the enablement state of one PWM channel.
*
* @param pwm A PWM handle.
* @param channel The PWM channel to get the enablement state of.
* @param[out] is_enabled The enablement state of the PWM channel.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_pwm_channel_get_enabled(const dif_pwm_t *pwm,
dif_pwm_channel_t channel,
dif_toggle_t *is_enabled);
/**
* Locks PWM configurations.
*
* This function is reentrant: calling it while locked will have no effect and
* return `kDifOk`.
*
* @param pwm A PWM handle.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_pwm_lock(const dif_pwm_t *pwm);
/**
* Checks whether PWM configurations are locked.
*
* @param pwm A PWM handle.
* @param[out] is_locked Out-param for the locked state.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_pwm_is_locked(const dif_pwm_t *pwm, bool *is_locked);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
#endif // OPENTITAN_SW_DEVICE_LIB_DIF_DIF_PWM_H_