Pavona Software APIs
dif_adc_ctrl.h
Go to the documentation of this file.
1// Copyright lowRISC contributors (OpenTitan project).
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
5#ifndef OPENTITAN_SW_DEVICE_LIB_DIF_DIF_ADC_CTRL_H_
6#define OPENTITAN_SW_DEVICE_LIB_DIF_DIF_ADC_CTRL_H_
7
8/**
9 * @file
10 * @brief <a href="/book/hw/ip/adc_ctrl/">ADC Controller</a> Device Interface
11 * Functions
12 */
13
14#include "hw/top/adc_ctrl_regs.h" // Generated.
16
17#ifdef __cplusplus
18extern "C" {
19#endif // __cplusplus
20
21/**
22 * Helper X macro for defining enums and case statements related to ADC
23 * Controller channels. If an additional channel is ever added to the hardware,
24 * this list can be updated.
25 */
26#define DIF_ADC_CTRL_CHANNEL_LIST(X) \
27 X(0) \
28 X(1)
29
30/**
31 * Helper X macro for defining enums and case statements related to ADC
32 * Controller filters. If an additional filter is ever added to the hardware,
33 * this list can be updated.
34 */
35#define DIF_ADC_CTRL_FILTER_LIST(X) \
36 X(0) \
37 X(1) \
38 X(2) \
39 X(3) \
40 X(4) \
41 X(5) \
42 X(6) \
43 X(7)
44
45/**
46 * Helper macro for defining a `dif_adc_ctrl_channel_t` enumeration constant.
47 * @channel_ ADC Controller channel of the enumeration constant.
48 */
49#define DIF_ADC_CTRL_CHANNEL_ENUM_INIT_(channel_) \
50 kDifAdcCtrlChannel##channel_ = channel_,
51
52/**
53 * An ADC Controller Channel.
54 */
58
59#undef DIF_ADC_CTRL_CHANNEL_ENUM_INIT_
60
61/**
62 * Helper macro for defining a `dif_adc_ctrl_filter_t` enumeration constant.
63 * @filter_ ADC Controller filter of the enumeration constant.
64 */
65#define DIF_ADC_CTRL_FILTER_ENUM_INIT_(filter_) \
66 kDifAdcCtrlFilter##filter_ = filter_,
67
68/**
69 * An ADC Controller filter.
70 *
71 * Each channel has a separate instance of each filter. For example, if there
72 * are two channels and eight filters, there would be a total of 16 filter
73 * instances that may be configured.
74 */
80
81#undef DIF_ADC_CTRL_FILTER_ENUM_INIT_
82
83/**
84 * Helper macro for defining a `dif_adc_ctrl_irq_cause_t` enumeration constant.
85 * @filter_cause_ ADC Controller IRQ filter cause of the enumeration constant.
86 */
87#define DIF_ADC_CTRL_IRQ_CAUSE_ENUM_INIT_(filter_cause_) \
88 kDifAdcCtrlIrqCauseFilter##filter_cause_ = 1U << filter_cause_,
89
90/**
91 * An ADC Controller IRQ cause.
92 *
93 * The ADC Controller can only generate a single interrupt (the `debug_cable`
94 * interrupt). However, depending on how the ADC Controller is configured, there
95 * are several causes that could trigger this interrupt. These include filter
96 * matches (when in Normal Power Scan mode), or sample completion (when in
97 * Oneshot mode).
98 */
101 /**
102 * Sample ready cause in Oneshot mode.
103 */
105 << ADC_CTRL_ADC_INTR_STATUS_TRANS_BIT,
106 /**
107 * Sample ready cause in Oneshot mode.
108 */
109 kDifAdcCtrlIrqCauseOneshot = 1U << ADC_CTRL_ADC_INTR_STATUS_ONESHOT_BIT,
110 /**
111 * All IRQ causes ORed together.
112 *
113 * This is useful when clearing all IRQ causes at once, to initialize the ADC
114 * Controller.
115 */
116 kDifAdcCtrlIrqCauseAll = (1U << (ADC_CTRL_ADC_INTR_STATUS_ONESHOT_BIT + 1)) -
117 1,
119
120#undef DIF_ADC_CTRL_IRQ_CAUSE_ENUM_INIT_
121
122/**
123 * Operation mode of the ADC Controller.
124 */
125typedef enum dif_adc_ctrl_mode {
126 /**
127 * Low Power (Continuous) Scan mode.
128 *
129 * In Low Power Scan mode, the ADC periodically samples enabled channels, and
130 * upon matching a set of enabled filters, a set number of times, will
131 * transition to Normal Power Scan mode. If no filters are enabled, then the
132 * ADC controller will never transition to Normal Power Scan mode.
133 */
135 /**
136 * Normal Power (Continuous) Scan mode.
137 *
138 * In Normal Power Scan mode, the ADC samples enabled channels as fast as
139 * possible, and upon matching a set of enabled filters, a set number of
140 * consecutive times, may trigger a system wakeup and/or IRQ. Similar to Low
141 * Power Scan mode, if no filters are enabled, then a system wakeup and/or IRQ
142 * will never be triggered.
143 */
145 /**
146 * Oneshot mode.
147 *
148 * In Oneshot mode, an ADC channel is triggered to take a single sample, upon
149 * being enabled, and optionally, raises an interrupt upon completion. Unlike
150 * the Scan modes, in Oneshot mode, the ADC Controller does not attempt to
151 * filter samples. Rather, an IRQ may be raised immediately upon the sample
152 * being ready, regardless of what the sample is. After the sample is
153 * completed the ADC is powered down, until another sample is triggered,
154 * either by toggling the channel's enable bit on and off, or by resetting the
155 * sampling FSM.
156 */
159
160/**
161 * Runtime configuration for an ADC Controller.
162 */
163typedef struct dif_adc_ctrl_config {
164 /**
165 * The sampling mode to configure the ADC Controller in.
166 */
168 /**
169 * The time to allow the ADC to power up.
170 *
171 * Units: always-on clock cycles
172 */
174 /**
175 * The sampling period when in Low Power Scan mode, i.e., how often the ADC
176 * Controller wakes up the ADC to take a sample.
177 *
178 * Units: always-on clock cycles
179 *
180 * Only relevant in Low Power Scan mode.
181 */
183 /**
184 * The number of filter-matching samples to count in Low Power Scan mode
185 * before switching to Normal Power Scan mode.
186 *
187 * Only relevant in Low Power Scan mode.
188 */
190 /**
191 * The number of filter-matching samples to count in Normal Power Scan mode
192 * before triggering a system wakeup and/or interrupt.
193 */
196
197/**
198 * Runtime configuration for an ADC Controller filter.
199 */
201 /**
202 * The ADC Controller filter this configuration applies to.
203 */
205 /**
206 * The minimum voltage (inclusive) of the range defined by this filter.
207 *
208 * Valid range: [0, 1024)
209 * Units: 2.148 mV (i.e., range / 2 ^ 10)
210 */
211 uint16_t min_voltage;
212 /**
213 * The maximum voltage (inclusive) of the range defined by this filter.
214 *
215 * Valid range: [0, 1024)
216 * Units: 2.148 mV (i.e., range / 2 ^ 10)
217 */
218 uint16_t max_voltage;
219 /**
220 * Where a filter hit is classfied as an (inclusive) in-range hit, or
221 * (exclusive) out-of-range hit.
222 */
224 /**
225 * Whether to generate a system wakeup on a filter match after saturating the
226 * `num_normal_power_samples` threshold in Normal Power Scan mode.
227 */
229 /**
230 * Whether to generate a `debug_cable` interrupt on a filter match after
231 * saturating the `num_normal_power_samples` threshold in Normal Power Scan
232 * mode.
233 */
236
237/**
238 * Configures an ADC Controller.
239 *
240 * @param adc_ctrl An adc_ctrl handle.
241 * @param config Runtime configuration parameters.
242 * @return The result of the operation.
243 */
246 dif_adc_ctrl_config_t config);
247
248/**
249 * Configures a channel filter.
250 *
251 * This should be invoked for each desired filter _before_ the sampling sequence
252 * is enabled via `dif_adc_ctrl_set_enabled()`.
253 *
254 * This only applies in Low / Normal Power Scan sampling modes.
255 *
256 * @param adc_ctrl An adc_ctrl handle.
257 * @param channel The channel of the filter to configure.
258 * @param config Runtime configuration parameters for the filter.
259 * @param enabled The enablement state to configure the filter in.
260 * @return The result of the operation.
261 */
266 dif_toggle_t enabled);
267
268/**
269 * Sets the enablement state of the ADC Controller.
270 *
271 * Enabling the ADC Controller powers it up, while disabling the ADC Controller
272 * powers it down and resets the sampling FSM. After powering up, sampling
273 * begins, regardless of the operation mode.
274 *
275 * @param adc_ctrl An adc_ctrl handle.
276 * @param enabled The enablement state to configure the ADC Controller in.
277 * @return The result of the operation.
278 */
281 dif_toggle_t enabled);
282
283/**
284 * Gets the enablement state of the ADC Controller.
285 *
286 * If the ADC Controller is enabled, it is powered up, or being powered up.
287 *
288 * @param adc_ctrl An adc_ctrl handle.
289 * @param[out] is_enabled The enablement state of the ADC Controller.
290 * @return The result of the operation.
291 */
294 dif_toggle_t *is_enabled);
295
296/**
297 * Sets the enablement state of the specified filter for the specified channel.
298 *
299 * @param adc_ctrl An adc_ctrl handle.
300 * @param channel The channel the filter resides in.
301 * @param filter The filter to set the enablement state of.
302 * @param enabled The enablement state to configure the filter in.
303 * @return The result of the operation.
304 */
309 dif_toggle_t enabled);
310
311/**
312 * Gets the enablement state of the specified filter for the specified channel.
313 *
314 * @param adc_ctrl An adc_ctrl handle.
315 * @param channel The channel the filter resides in.
316 * @param filter The filter to get the enablement state of.
317 * @param[out] is_enabled The enablement state of the filter.
318 * @return The result of the operation.
319 */
324 dif_toggle_t *enabled);
325
326/**
327 * Get the sampled value from the specified channel that triggered the IRQ.
328 *
329 * Values are 10-bits in the range from 0V to 2.2V. Based on this, the
330 * resolution (and units) of the sample are in increments of 2.148mV.
331 *
332 * @param adc_ctrl An adc_ctrl handle.
333 * @param channel The channel to read the sample from.
334 * @param[out] value The value of the sample.
335 * @return The result of the operation.
336 */
340 uint16_t *value);
341
342/**
343 * Get the latest sampled value from the specified channel.
344 *
345 * Since in Normal Power Scan mode, sampling continues even after an IRQ has
346 * been raised, the value returned by this function may be different than the
347 * value returned by `dif_adc_ctrl_get_irq_value()`.
348 *
349 * Values are 10-bits in the range from 0V to 2.2V. Based on this, the
350 * resolution (and units) of the sample are in increments of 2.148mV.
351 *
352 * @param adc_ctrl An adc_ctrl handle.
353 * @param channel The channel to read the sample from.
354 * @param[out] value The value of the sample.
355 * @return The result of the operation.
356 */
360 uint16_t *value);
361
362/**
363 * Reset all ADC Controller FSMs and counters, and if enabled, begin sampling
364 * sequence.
365 *
366 * @param adc_ctrl An adc_ctrl handle.
367 * @return The result of the operation.
368 */
371
372/**
373 * Gets the cause(s) of a `debug_cable` IRQ.
374 *
375 * IRQs can be triggered by filter matches in Normal Power Scan mode (after
376 * saturating the `num_normal_power_samples` threshold), or after a single
377 * sample capture in Oneshot mode.
378 *
379 * @param adc_ctrl An adc_ctrl handle.
380 * @param[out] causes The causes of the IRQ (one or more
381 * `dif_adc_ctrl_irq_cause_t`s ORed together).
382 * @return The result of the operation.
383 */
386 uint32_t *causes);
387
388/**
389 * Gets the filter status.
390 *
391 * @param adc_ctrl An adc_ctrl handle.
392 * @param[out] status The current filter status.
393 * @return The result of the operation.
394 */
397 uint32_t *status);
398
399/**
400 * Clears the cause(s) of a `debug_cable` IRQ.
401 *
402 * TODO(lowrisc/opentitan#11354): future releases of the HW should hide the
403 * filter and interrupt status registers behind the standardized IRQ registers.
404 * For now, the autogenerated `dif_adc_ctrl_irq_acknowledge[_all]()` DIF may be
405 * used to clear the main IRQ status register, while this DIF may be used to
406 * clear the local cause / filter status registers.
407 *
408 * @param adc_ctrl An adc_ctrl handle.
409 * @param causes The causes of the IRQ (one or more `dif_adc_ctrl_irq_cause_t`s
410 * ORed together).
411 * @return The result of the operation.
412 */
415 uint32_t causes);
416
417/**
418 * Sets the enablement of generating system wakeups on a filter match.
419 *
420 * Only relevant in Normal Power Scan mode (and Low Power Scan mode, which can
421 * transition to Normal Power Scan mode).
422 *
423 * @param adc_ctrl An adc_ctrl handle.
424 * @param filter A filter to enable wakeup triggering for.
425 * @param enabled The enablement state to set.
426 * @return The result of the operation.
427 */
430 const dif_adc_ctrl_t *adc_ctrl, dif_adc_ctrl_filter_t filter,
431 dif_toggle_t enabled);
432
433/**
434 * Gets the enablement of generating system wakeups on a filter match.
435 *
436 * @param adc_ctrl An adc_ctrl handle.
437 * @param filter A filter to enable wakeup triggering for.
438 * @param[out] is_enabled The enablement state retrieved.
439 * @return The result of the operation.
440 */
443 const dif_adc_ctrl_t *adc_ctrl, dif_adc_ctrl_filter_t filter,
444 dif_toggle_t *is_enabled);
445
446/**
447 * Sets the enablement of generating a `debug_cable` IRQ for given cause(s).
448 *
449 * Causes can be filter matches (in Normal Power Scan mode), or when a sample is
450 * complete (in Oneshot mode).
451 *
452 * @param adc_ctrl An adc_ctrl handle.
453 * @param causes Causes (one or more `dif_adc_ctrl_irq_cause_t`s ORed together)
454 * to generate the `debug_cable` IRQ for.
455 * @param enabled The enablement state to set.
456 * @return The result of the operation.
457 */
460 uint32_t causes,
461 dif_toggle_t enabled);
462
463/**
464 * Gets the causes that will generate a `debug_cable` IRQ.
465 *
466 * @param adc_ctrl An adc_ctrl handle.
467 * @param[out] enabled_causes Causes (one or more `dif_adc_ctrl_irq_cause_t`s
468 * ORed together) that will generate the
469 * `debug_cable` IRQ.
470 * @return The result of the operation.
471 */
474 uint32_t *enabled_causes);
475
476/**
477 * Wait enough time for CDC synchronization between block and CSRs.
478 *
479 * Wait long enough for any CDC synchronization between the AON part
480 * of the block and the CSRs to be complete. This is particularly important
481 * for the FILTER_STATUS register: this register can be updated by the HW
482 * and any update may take a while to become visible by the SW.
483 *
484 * @param adc_ctrl An adc_ctrl handle.
485 * @param aon_freq_hz Frequency of the AON clock in Hz.
486 * @return The result of the operation.
487 */
489 uint32_t aon_freq_hz);
490
491#ifdef __cplusplus
492} // extern "C"
493#endif // __cplusplus
494
495#endif // OPENTITAN_SW_DEVICE_LIB_DIF_DIF_ADC_CTRL_H_