forked from lowRISC/opentitan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dif_adc_ctrl.h
479 lines (442 loc) · 16.4 KB
/
dif_adc_ctrl.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// 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_ADC_CTRL_H_
#define OPENTITAN_SW_DEVICE_LIB_DIF_DIF_ADC_CTRL_H_
/**
* @file
* @brief <a href="/hw/ip/adc_ctrl/doc/">ADC Controller</a> Device Interface
* Functions
*/
#include "adc_ctrl_regs.h" // Generated.
#include "sw/device/lib/dif/autogen/dif_adc_ctrl_autogen.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
/**
* Helper X macro for defining enums and case statements related to ADC
* Controller channels. If an additional channel is ever added to the hardware,
* this list can be updated.
*/
#define DIF_ADC_CTRL_CHANNEL_LIST(X) \
X(0) \
X(1)
/**
* Helper X macro for defining enums and case statements related to ADC
* Controller filters. If an additional filter is ever added to the hardware,
* this list can be updated.
*/
#define DIF_ADC_CTRL_FILTER_LIST(X) \
X(0) \
X(1) \
X(2) \
X(3) \
X(4) \
X(5) \
X(6) \
X(7)
/**
* Helper macro for defining a `dif_adc_ctrl_channel_t` enumeration constant.
* @channel_ ADC Controller channel of the enumeration constant.
*/
#define DIF_ADC_CTRL_CHANNEL_ENUM_INIT_(channel_) \
kDifAdcCtrlChannel##channel_ = channel_,
/**
* An ADC Controller Channel.
*/
typedef enum dif_adc_ctrl_channel {
DIF_ADC_CTRL_CHANNEL_LIST(DIF_ADC_CTRL_CHANNEL_ENUM_INIT_)
} dif_adc_ctrl_channel_t;
#undef DIF_ADC_CTRL_CHANNEL_ENUM_INIT_
/**
* Helper macro for defining a `dif_adc_ctrl_filter_t` enumeration constant.
* @filter_ ADC Controller filter of the enumeration constant.
*/
#define DIF_ADC_CTRL_FILTER_ENUM_INIT_(filter_) \
kDifAdcCtrlFilter##filter_ = filter_,
/**
* An ADC Controller filter.
*
* Each channel has a separate instance of each filter. For example, if there
* are two channels and eight filters, there would be a total of 16 filter
* instances that may be configured.
*/
typedef enum dif_adc_ctrl_filter {
DIF_ADC_CTRL_FILTER_LIST(DIF_ADC_CTRL_FILTER_ENUM_INIT_)
kDifAdcCtrlTrans,
} dif_adc_ctrl_filter_t;
#undef DIF_ADC_CTRL_FILTER_ENUM_INIT_
/**
* Helper macro for defining a `dif_adc_ctrl_irq_cause_t` enumeration constant.
* @filter_cause_ ADC Controller IRQ filter cause of the enumeration constant.
*/
#define DIF_ADC_CTRL_IRQ_CAUSE_ENUM_INIT_(filter_cause_) \
kDifAdcCtrlIrqCauseFilter##filter_cause_ = 1U << filter_cause_,
/**
* An ADC Controller IRQ cause.
*
* The ADC Controller can only generate a single interrupt (the `debug_cable`
* interrupt). However, depending on how the ADC Controller is configured, there
* are several causes that could trigger this interrupt. These include filter
* matches (when in Normal Power Scan mode), or sample completion (when in
* Oneshot mode).
*/
typedef enum dif_adc_ctrl_irq_cause {
DIF_ADC_CTRL_FILTER_LIST(DIF_ADC_CTRL_IRQ_CAUSE_ENUM_INIT_)
/**
* Sample ready cause in Oneshot mode.
*/
kDifAdcCtrlIrqCauseTrans = 1U << ADC_CTRL_ADC_INTR_STATUS_TRANS_BIT,
/**
* Sample ready cause in Oneshot mode.
*/
kDifAdcCtrlIrqCauseOneshot = 1U << ADC_CTRL_ADC_INTR_STATUS_ONESHOT_BIT,
/**
* All IRQ causes ORed together.
*
* This is useful when clearing all IRQ causes at once, to initialize the ADC
* Controller.
*/
kDifAdcCtrlIrqCauseAll =
(1U << (ADC_CTRL_ADC_INTR_STATUS_ONESHOT_BIT + 1)) - 1,
} dif_adc_ctrl_irq_cause_t;
#undef DIF_ADC_CTRL_IRQ_CAUSE_ENUM_INIT_
/**
* Operation mode of the ADC Controller.
*/
typedef enum dif_adc_ctrl_mode {
/**
* Low Power (Continuous) Scan mode.
*
* In Low Power Scan mode, the ADC periodically samples enabled channels, and
* upon matching a set of enabled filters, a set number of times, will
* transition to Normal Power Scan mode. If no filters are enabled, then the
* ADC controller will never transition to Normal Power Scan mode.
*/
kDifAdcCtrlLowPowerScanMode = 0,
/**
* Normal Power (Continuous) Scan mode.
*
* In Normal Power Scan mode, the ADC samples enabled channels as fast as
* possible, and upon matching a set of enabled filters, a set number of
* consecutive times, may trigger a system wakeup and/or IRQ. Similar to Low
* Power Scan mode, if no filters are enabled, then a system wakeup and/or IRQ
* will never be triggered.
*/
kDifAdcCtrlNormalPowerScanMode = 1,
/**
* Oneshot mode.
*
* In Oneshot mode, an ADC channel is triggered to take a single sample, upon
* being enabled, and optionally, raises an interrupt upon completion. Unlike
* the Scan modes, in Oneshot mode, the ADC Controller does not attempt to
* filter samples. Rather, an IRQ may be raised immediately upon the sample
* being ready, regardless of what the sample is. After the sample is
* completed the ADC is powered down, until another sample is triggered,
* either by toggling the channel's enable bit on and off, or by resetting the
* sampling FSM.
*/
kDifAdcCtrlOneshotMode = 2,
} dif_adc_ctrl_mode_t;
/**
* Runtime configuration for an ADC Controller.
*/
typedef struct dif_adc_ctrl_config {
/**
* The sampling mode to configure the ADC Controller in.
*/
dif_adc_ctrl_mode_t mode;
/**
* The time to allow the ADC to power up.
*
* Units: always-on clock cycles
*/
uint8_t power_up_time_aon_cycles;
/**
* The sampling period when in Low Power Scan mode, i.e., how often the ADC
* Controller wakes up the ADC to take a sample.
*
* Units: always-on clock cycles
*
* Only relevant in Low Power Scan mode.
*/
uint32_t wake_up_time_aon_cycles;
/**
* The number of filter-matching samples to count in Low Power Scan mode
* before switching to Normal Power Scan mode.
*
* Only relevant in Low Power Scan mode.
*/
uint8_t num_low_power_samples;
/**
* The number of filter-matching samples to count in Normal Power Scan mode
* before triggering a system wakeup and/or interrupt.
*/
uint16_t num_normal_power_samples;
} dif_adc_ctrl_config_t;
/**
* Runtime configuration for an ADC Controller filter.
*/
typedef struct dif_adc_ctrl_filter_config {
/**
* The ADC Controller filter this configuration applies to.
*/
dif_adc_ctrl_filter_t filter;
/**
* The minimum voltage (inclusive) of the range defined by this filter.
*
* Valid range: [0, 1024)
* Units: 2.148 mV (i.e., range / 2 ^ 10)
*/
uint16_t min_voltage;
/**
* The maximum voltage (inclusive) of the range defined by this filter.
*
* Valid range: [0, 1024)
* Units: 2.148 mV (i.e., range / 2 ^ 10)
*/
uint16_t max_voltage;
/**
* Where a filter hit is classfied as an (inclusive) in-range hit, or
* (exclusive) out-of-range hit.
*/
bool in_range;
/**
* Whether to generate a system wakeup on a filter match after saturating the
* `num_normal_power_samples` threshold in Normal Power Scan mode.
*/
bool generate_wakeup_on_match;
/**
* Whether to generate a `debug_cable` interrupt on a filter match after
* saturating the `num_normal_power_samples` threshold in Normal Power Scan
* mode.
*/
bool generate_irq_on_match;
} dif_adc_ctrl_filter_config_t;
/**
* Configures an ADC Controller.
*
* @param adc_ctrl An adc_ctrl handle.
* @param config Runtime configuration parameters.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_adc_ctrl_configure(const dif_adc_ctrl_t *adc_ctrl,
dif_adc_ctrl_config_t config);
/**
* Configures a channel filter.
*
* This should be invoked for each desired filter _before_ the sampling sequence
* is enabled via `dif_adc_ctrl_set_enabled()`.
*
* This only applies in Low / Normal Power Scan sampling modes.
*
* @param adc_ctrl An adc_ctrl handle.
* @param channel The channel of the filter to configure.
* @param config Runtime configuration parameters for the filter.
* @param enabled The enablement state to configure the filter in.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_adc_ctrl_configure_filter(const dif_adc_ctrl_t *adc_ctrl,
dif_adc_ctrl_channel_t channel,
dif_adc_ctrl_filter_config_t config,
dif_toggle_t enabled);
/**
* Sets the enablement state of the ADC Controller.
*
* Enabling the ADC Controller powers it up, while disabling the ADC Controller
* powers it down and resets the sampling FSM. After powering up, sampling
* begins, regardless of the operation mode.
*
* @param adc_ctrl An adc_ctrl handle.
* @param enabled The enablement state to configure the ADC Controller in.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_adc_ctrl_set_enabled(const dif_adc_ctrl_t *adc_ctrl,
dif_toggle_t enabled);
/**
* Gets the enablement state of the ADC Controller.
*
* If the ADC Controller is enabled, it is powered up, or being powered up.
*
* @param adc_ctrl An adc_ctrl handle.
* @param[out] is_enabled The enablement state of the ADC Controller.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_adc_ctrl_get_enabled(const dif_adc_ctrl_t *adc_ctrl,
dif_toggle_t *is_enabled);
/**
* Sets the enablement state of the specified filter for the specified channel.
*
* @param adc_ctrl An adc_ctrl handle.
* @param channel The channel the filter resides in.
* @param filter The filter to set the enablement state of.
* @param enabled The enablement state to configure the filter in.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_adc_ctrl_filter_set_enabled(const dif_adc_ctrl_t *adc_ctrl,
dif_adc_ctrl_channel_t channel,
dif_adc_ctrl_filter_t filter,
dif_toggle_t enabled);
/**
* Gets the enablement state of the specified filter for the specified channel.
*
* @param adc_ctrl An adc_ctrl handle.
* @param channel The channel the filter resides in.
* @param filter The filter to get the enablement state of.
* @param[out] is_enabled The enablement state of the filter.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_adc_ctrl_filter_get_enabled(const dif_adc_ctrl_t *adc_ctrl,
dif_adc_ctrl_channel_t channel,
dif_adc_ctrl_filter_t filter,
dif_toggle_t *enabled);
/**
* Get the sampled value from the specified channel that triggered the IRQ.
*
* Values are 10-bits in the range from 0V to 2.2V. Based on this, the
* resolution (and units) of the sample are in increments of 2.148mV.
*
* @param adc_ctrl An adc_ctrl handle.
* @param channel The channel to read the sample from.
* @param[out] value The value of the sample.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_adc_ctrl_get_triggered_value(const dif_adc_ctrl_t *adc_ctrl,
dif_adc_ctrl_channel_t channel,
uint16_t *value);
/**
* Get the latest sampled value from the specified channel.
*
* Since in Normal Power Scan mode, sampling continues even after an IRQ has
* been raised, the value returned by this function may be different than the
* value returned by `dif_adc_ctrl_get_irq_value()`.
*
* Values are 10-bits in the range from 0V to 2.2V. Based on this, the
* resolution (and units) of the sample are in increments of 2.148mV.
*
* @param adc_ctrl An adc_ctrl handle.
* @param channel The channel to read the sample from.
* @param[out] value The value of the sample.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_adc_ctrl_get_latest_value(const dif_adc_ctrl_t *adc_ctrl,
dif_adc_ctrl_channel_t channel,
uint16_t *value);
/**
* Reset all ADC Controller FSMs and counters, and if enabled, begin sampling
* sequence.
*
* @param adc_ctrl An adc_ctrl handle.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_adc_ctrl_reset(const dif_adc_ctrl_t *adc_ctrl);
/**
* Gets the cause(s) of a `debug_cable` IRQ.
*
* IRQs can be triggered by filter matches in Normal Power Scan mode (after
* saturating the `num_normal_power_samples` threshold), or after a single
* sample capture in Oneshot mode.
*
* @param adc_ctrl An adc_ctrl handle.
* @param[out] causes The causes of the IRQ (one or more
* `dif_adc_ctrl_irq_cause_t`s ORed together).
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_adc_ctrl_irq_get_causes(const dif_adc_ctrl_t *adc_ctrl,
uint32_t *causes);
/**
* Gets the filter status.
*
* @param adc_ctrl An adc_ctrl handle.
* @param[out] status The current filter status.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_adc_ctrl_get_filter_status(const dif_adc_ctrl_t *adc_ctrl,
uint32_t *status);
/**
* Clears the cause(s) of a `debug_cable` IRQ.
*
* TODO(lowRISC/opentitan:#11354): future releases of the HW should hide the
* filter and interrupt status registers behind the standardized IRQ registers.
* For now, the autogenerated `dif_adc_ctrl_irq_acknowledge[_all]()` DIF may be
* used to clear the main IRQ status register, while this DIF may be used to
* clear the local cause / filter status registers.
*
* @param adc_ctrl An adc_ctrl handle.
* @param causes The causes of the IRQ (one or more `dif_adc_ctrl_irq_cause_t`s
* ORed together).
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_adc_ctrl_irq_clear_causes(const dif_adc_ctrl_t *adc_ctrl,
uint32_t causes);
/**
* Sets the enablement of generating system wakeups on a filter match.
*
* Only relevant in Normal Power Scan mode (and Low Power Scan mode, which can
* transition to Normal Power Scan mode).
*
* @param adc_ctrl An adc_ctrl handle.
* @param filter A filter to enable wakeup triggering for.
* @param enabled The enablement state to set.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_adc_ctrl_filter_match_wakeup_set_enabled(
const dif_adc_ctrl_t *adc_ctrl, dif_adc_ctrl_filter_t filter,
dif_toggle_t enabled);
/**
* Gets the enablement of generating system wakeups on a filter match.
*
* @param adc_ctrl An adc_ctrl handle.
* @param filter A filter to enable wakeup triggering for.
* @param[out] is_enabled The enablement state retrieved.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_adc_ctrl_filter_match_wakeup_get_enabled(
const dif_adc_ctrl_t *adc_ctrl, dif_adc_ctrl_filter_t filter,
dif_toggle_t *is_enabled);
/**
* Sets the enablement of generating a `debug_cable` IRQ for given cause(s).
*
* Causes can be filter matches (in Normal Power Scan mode), or when a sample is
* complete (in Oneshot mode).
*
* @param adc_ctrl An adc_ctrl handle.
* @param causes Causes (one or more `dif_adc_ctrl_irq_cause_t`s ORed together)
* to generate the `debug_cable` IRQ for.
* @param enabled The enablement state to set.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_adc_ctrl_irq_cause_set_enabled(const dif_adc_ctrl_t *adc_ctrl,
uint32_t causes,
dif_toggle_t enabled);
/**
* Gets the causes that will generate a `debug_cable` IRQ.
*
* @param adc_ctrl An adc_ctrl handle.
* @param[out] enabled_causes Causes (one or more `dif_adc_ctrl_irq_cause_t`s
* ORed together) that will generate the
* `debug_cable` IRQ.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_result_t dif_adc_ctrl_irq_cause_get_enabled(const dif_adc_ctrl_t *adc_ctrl,
uint32_t *enabled_causes);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
#endif // OPENTITAN_SW_DEVICE_LIB_DIF_DIF_ADC_CTRL_H_