Software APIs
nv_counter_testutils.h
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_TESTING_NV_COUNTER_TESTUTILS_H_
6 #define OPENTITAN_SW_DEVICE_LIB_TESTING_NV_COUNTER_TESTUTILS_H_
7 
8 #include <stdint.h>
9 
10 #include "sw/device/lib/base/status.h"
12 
13 /**
14  * Returns the value of a non-volatile counter in flash.
15  *
16  * @param counter Counter ID, [0, 2].
17  * @param[out] Value of the non-volatile counter
18  * @return The result of the operation.
19  */
21 status_t flash_ctrl_testutils_counter_get(size_t counter, uint32_t *value);
22 
23 /**
24  * Increments a non-volatile counter in flash.
25  *
26  * @param flash_state A flash_ctrl state handle.
27  * @param counter Counter ID, [0, 2].
28  * @return The result of the operation.
29  */
31 status_t flash_ctrl_testutils_counter_increment(
32  dif_flash_ctrl_state_t *flash_state, size_t counter);
33 
34 /**
35  * Sets a non-volatile counter to at least `val`.
36  *
37  * This function simply writes zeros to the corresponding flash word without any
38  * checks and is intended for contexts where performance is critical, e.g. ISRs.
39  * The value of the counter will not change if it is already greater than or
40  * equal to `val`.
41  *
42  * @param flash_state A flash_ctrl state handle.
43  * @param counter Counter ID, [0, 2].
44  * @param val Counter value.
45  * @return The result of the operation.
46  */
48 status_t flash_ctrl_testutils_counter_set_at_least(
49  dif_flash_ctrl_state_t *flash_state, size_t counter, uint32_t val);
50 
51 /**
52  * At the beginning of the simulation (Verilator, VCS,etc.),
53  * the content of the flash might be all-zeros, and thus,
54  * the NVM counter's inital value might be 256.
55  * In that case, flash_ctrl_testutils_counter_set_at_least() will not increment
56  * This function can be used to initialize a NVM counter to zero by filling
57  * its flash region with non-zero values.
58  *
59  * @param flash_state A flash_ctrl handle.
60  * @param counter The ID of the NVM counter, [0, 2].
61  * @param The result of the operation.
62  **/
64 status_t flash_ctrl_testutils_counter_init_zero(
65  dif_flash_ctrl_state_t *flash_state, size_t counter);
66 
67 #endif // OPENTITAN_SW_DEVICE_LIB_TESTING_NV_COUNTER_TESTUTILS_H_