-
Notifications
You must be signed in to change notification settings - Fork 792
/
dif_gpio.h
441 lines (405 loc) · 12.6 KB
/
dif_gpio.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
// Copyright lowRISC contributors.
// 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_GPIO_H_
#define OPENTITAN_SW_DEVICE_LIB_DIF_DIF_GPIO_H_
/**
* @file
* @brief <a href="/hw/ip/gpio/doc/">GPIO</a> Device Interface Functions
*/
#include <stddef.h>
#include <stdint.h>
#include "sw/device/lib/base/macros.h"
#include "sw/device/lib/base/mmio.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
/**
* A toggle state: enabled, or disabled.
*
* This enum may be used instead of a `bool` when describing an enabled/disabled
* state.
*
* This enum may be used with `dif_gpio_toggle_vec_t` to set individual bits
* within it; `dif_gpio_toggle_t`'s variants are guaranteed to be compatible
* with `dif_gpio_toggle_vec_t`.
*/
typedef enum dif_gpio_toggle {
/*
* The "enabled" state.
*/
kDifGpioToggleEnabled = true,
/**
* The "disabled" state.
*/
kDifGpioToggleDisabled = false,
} dif_gpio_toggle_t;
/**
* Hardware instantiation parameters for GPIO.
*
* This struct describes information about the underlying hardware that is
* not determined until the hardware design is used as part of a top-level
* design.
*/
typedef struct dif_gpio_params {
/**
* The base address for the GPIO hardware registers.
*/
mmio_region_t base_addr;
} dif_gpio_params_t;
/**
* A handle to GPIO.
*
* This type should be treated as opaque by users.
*/
typedef struct dif_gpio {
dif_gpio_params_t params;
} dif_gpio_t;
/**
* The result of a GPIO operation.
*/
typedef enum dif_gpio_result {
/**
* Indicates that the operation succeeded.
*/
kDifGpioOk = 0,
/**
* Indicates some unspecified failure.
*/
kDifGpioError = 1,
/**
* Indicates that some parameter passed into a function failed a
* precondition.
*
* When this value is returned, no hardware operations occurred.
*/
kDifGpioBadArg = 2,
} dif_gpio_result_t;
/**
* A GPIO interrupt request trigger.
*
* Each GPIO pin has an associated interrupt that can be independently
* configured
* to be edge and/or level sensitive. This enum defines supported configurations
* for
* these interrupts.
*/
typedef enum dif_gpio_irq_trigger {
/**
* Trigger on rising edge.
*/
kDifGpioIrqTriggerEdgeRising,
/**
* Trigger on falling edge.
*/
kDifGpioIrqTriggerEdgeFalling,
/**
* Trigger when input is low.
*/
kDifGpioIrqTriggerLevelLow,
/**
* Trigger when input is high.
*/
kDifGpioIrqTriggerLevelHigh,
/**
* Trigger on rising and falling edges.
*/
kDifGpioIrqTriggerEdgeRisingFalling,
/**
* Trigger on rising edge or when the input is low.
*/
kDifGpioIrqTriggerEdgeRisingLevelLow,
/**
* Trigger on falling edge or when the input is high.
*/
kDifGpioIrqTriggerEdgeFallingLevelHigh,
} dif_gpio_irq_trigger_t;
/**
* A GPIO pin index, ranging from 0 to 31.
*
* This type serves as the GPIO interrupt request type.
*/
typedef uint32_t dif_gpio_pin_t;
/**
* State for all 32 GPIO pins, given as bit fields.
*
* The Nth bit represents the state of the Nth pin.
*
* This type is also used as a vector of `dif_gpio_toggle_t`s, to indicate
* toggle state across all 32 pins. A set bit corresponds to
* `kDifGpioToggleEnabled`.
*
* It is also used with `dif_gpio_irq_disable_all()` and
* `dif_gpio_irq_restore_all()`.
*/
typedef uint32_t dif_gpio_state_t;
/**
* A mask for selecting GPIO pins.
*
* If the Nth bit is enabled, then the Nth pin is selected by the mask.
*/
typedef uint32_t dif_gpio_mask_t;
/**
* Creates a new handle for GPIO.
*
* This function does not actuate the hardware.
*
* @param params Hardware instantiation parameters.
* @param[out] gpio Out param for the initialized handle.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_init(dif_gpio_params_t params, dif_gpio_t *gpio);
/**
* Resets a GPIO device.
*
* Resets the given GPIO device by setting its configuration registers to
* reset values. Disables interrupts, output, and input filter.
*
* @param gpio A GPIO handle.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_reset(const dif_gpio_t *gpio);
/**
* Returns whether a particular pin's interrupt is currently pending.
*
* @param gpio A GPIO handle.
* @param pin A GPIO pin.
* @param[out] is_pending Out-param for whether the interrupt is pending.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_irq_is_pending(const dif_gpio_t *gpio,
dif_gpio_pin_t pin, bool *is_pending);
/**
* Returns a GPIO state representing which pins have interrupts enabled.
*
* @param gpio A GPIO handle.
* @param[out] is_pending Out-param for which interrupts are pending.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_irq_is_pending_all(const dif_gpio_t *gpio,
dif_gpio_state_t *is_pending);
/**
* Acknowledges a particular pin's interrupt, indicating to the hardware that it
* has
* been successfully serviced.
*
* @param gpio A GPIO handle.
* @param pin A GPIO pin.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_irq_acknowledge(const dif_gpio_t *gpio,
dif_gpio_pin_t pin);
/**
* Checks whether a particular pin's interrupt is currently enabled or disabled.
*
* @param gpio A GPIO handle.
* @param pin A GPIO pin.
* @param[out] state Out-param toggle state of the interrupt.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_irq_get_enabled(const dif_gpio_t *gpio,
dif_gpio_pin_t pin,
dif_gpio_toggle_t *state);
/**
* Sets whether a particular pin's interrupt is currently enabled or disabled.
*
* @param gpio A GPIO handle.
* @param pin A GPIO pin.
* @param state The new toggle state for the interrupt.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_irq_set_enabled(const dif_gpio_t *gpio,
dif_gpio_pin_t pin,
dif_gpio_toggle_t state);
/**
* Sets whether a particular pin's interrupt is currently enabled or disabled.
*
* @param gpio A GPIO handle.
* @param mask Mask that identifies the pins whose interrupt triggers will be
* configured.
* @param state The new toggle state for the interrupt.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_irq_set_enabled_masked(const dif_gpio_t *gpio,
dif_gpio_mask_t mask,
dif_gpio_toggle_t state);
/**
* Forces a particular pin's interrupt, causing it to be serviced as if hardware
* had asserted it.
*
* @param gpio A GPIO handle.
* @param pin A GPIO pin.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_irq_force(const dif_gpio_t *gpio,
dif_gpio_pin_t pin);
/**
* Disables all interrupts, optionally snapshotting all toggle state for later
* restoration.
*
* @param gpio A GPIO handle.
* @param[out] snapshot Out-param for the snapshot; may be `NULL`.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_irq_disable_all(const dif_gpio_t *gpio,
dif_gpio_state_t *snapshot);
/**
* Restores interrupts from the given snapshot.
*
* This function can be used with `dif_gpio_irq_disable_all()` to temporary
* interrupt save-and-restore.
*
* @param gpio A GPIO handle.
* @param snapshot A snapshot to restore from.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_irq_restore_all(const dif_gpio_t *gpio,
const dif_gpio_state_t *snapshot);
/**
* Configures interrupt triggers for a set of pins.
*
* This function configures interrupt triggers, i.e. rising-edge, falling-edge,
* level-high, and level-low, for the pins given by the mask. Note that
* interrupt of the pin must also be enabled to generate interrupts.
*
* @param gpio A GPIO handle.
* @param mask Mask that identifies the pins whose interrupt triggers will be
* configured.
* @param trigger New configuration of interrupt triggers.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_irq_set_trigger(const dif_gpio_t *gpio,
dif_gpio_mask_t mask,
dif_gpio_irq_trigger_t trigger);
/**
* Reads from a pin.
*
* The value returned by this function is independent of the output enable
* setting and includes the effects of the input noise filter and the load on
* the pin.
*
* @param gpio A GPIO handle.
* @param pin A GPIO pin.
* @param[out] state Pin value.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_read(const dif_gpio_t *gpio, dif_gpio_pin_t pin,
bool *state);
/**
* Reads from all pins.
*
* The value returned by this function is independent of the output enable
* setting and includes the effects of the input noise filter and the load on
* the pins.
*
* @param gpio A GPIO handle.
* @param[out] state Pin values.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_read_all(const dif_gpio_t *gpio,
dif_gpio_state_t *state);
/**
* Writes to a pin.
*
* The actual value on the pin depends on the output enable setting.
*
* @param gpio A GPIO handle.
* @param pin A GPIO pin.
* @param state Value to write.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_write(const dif_gpio_t *gpio, dif_gpio_pin_t pin,
bool state);
/**
* Writes to all pins.
*
* The actual values on the pins depend on the output enable setting.
*
* @param gpio A GPIO handle.
* @param state Value to write.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_write_all(const dif_gpio_t *gpio,
dif_gpio_state_t state);
/**
* Writes to the pins identified by a mask.
*
* The actual values on the pins depend on the output enable setting.
*
* @param gpio A GPIO handle.
* @param mask Mask that identifies the pins to write to.
* @param state Value to write.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_write_masked(const dif_gpio_t *gpio,
dif_gpio_mask_t mask,
dif_gpio_state_t state);
/**
* Sets output enable mode of a pin.
*
* @param gpio A GPIO handle.
* @param pin A GPIO pin.
* @param state Output mode of the pin.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_output_set_enabled(const dif_gpio_t *gpio,
dif_gpio_pin_t pin,
dif_gpio_toggle_t state);
/**
* Sets output modes of all pins.
*
* @param gpio A GPIO handle.
* @param state Output modes of the pins.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_output_set_enabled_all(const dif_gpio_t *gpio,
dif_gpio_state_t state);
/**
* Sets the output modes of the pins identified by a mask.
*
* @param gpio A GPIO handle.
* @param mask Mask that identifies the pins whose output modes will be set.
* @param state Output modes of the pins.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_output_set_enabled_masked(const dif_gpio_t *gpio,
dif_gpio_mask_t mask,
dif_gpio_state_t state);
/**
* Enable noise filter for GPIO inputs.
*
* When enabled, changes in the pin value will be ignored unless stable
* for 16 cycles.
*
* @param gpio A GPIO handle.
* @param mask Mask that identifies pins to set the filter state of.
* @param state The new toggle state for the filter.
* @return The result of the operation.
*/
OT_WARN_UNUSED_RESULT
dif_gpio_result_t dif_gpio_input_noise_filter_set_enabled(
const dif_gpio_t *gpio, dif_gpio_mask_t mask, dif_gpio_toggle_t state);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
#endif // OPENTITAN_SW_DEVICE_LIB_DIF_DIF_GPIO_H_