1 /*
2  * Copyright (c) 2019 - 2020, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *    list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  *    contributors may be used to endorse or promote products derived from this
17  *    software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef NRF_BPROT_H__
33 #define NRF_BPROT_H__
34 
35 #include <nrfx.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /**
42  * @defgroup nrf_bprot_hal BPROT HAL
43  * @{
44  * @ingroup nrf_bprot
45  * @brief   Hardware access layer for managing the Block Protection (BPROT) mechanism.
46  */
47 
48 /**
49  * @brief Function for enabling protection for specified non-volatile memory blocks.
50  *
51  * Blocks are arranged into groups of 32 blocks each. Each block size is 4 kB.
52  * Any attempt to write or erase a protected block will result in hard fault.
53  * The memory block protection can be disabled only by resetting the device.
54  *
55  * @param[in] p_reg      Pointer to the structure of registers of the peripheral.
56  * @param[in] group_idx  Non-volatile memory group containing memory blocks to protect.
57  * @param[in] block_mask Non-volatile memory blocks to protect. Each bit in bitmask represents
58  *                       one memory block in the specified group.
59  */
60 NRF_STATIC_INLINE void nrf_bprot_nvm_blocks_protection_enable(NRF_BPROT_Type * p_reg,
61                                                               uint8_t          group_idx,
62                                                               uint32_t         block_mask);
63 
64 /**
65  * @brief Function for setting the non-volatile memory (NVM) protection during debug.
66  *
67  * NVM protection is disabled by default while debugging.
68  *
69  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
70  * @param[in] enable True if NVM protection during debug is to be enabled, false otherwise.
71  */
72 NRF_STATIC_INLINE void nrf_bprot_nvm_protection_in_debug_set(NRF_BPROT_Type * p_reg,
73                                                              bool             enable);
74 
75 #ifndef NRF_DECLARE_ONLY
76 
nrf_bprot_nvm_blocks_protection_enable(NRF_BPROT_Type * p_reg,uint8_t group_idx,uint32_t block_mask)77 NRF_STATIC_INLINE void nrf_bprot_nvm_blocks_protection_enable(NRF_BPROT_Type * p_reg,
78                                                               uint8_t          group_idx,
79                                                               uint32_t         block_mask)
80 {
81     switch (group_idx)
82     {
83         case 0:
84             p_reg->CONFIG0 = block_mask;
85             break;
86 
87         case 1:
88             p_reg->CONFIG1 = block_mask;
89             break;
90 
91 #if defined(BPROT_CONFIG2_REGION64_Pos)
92         case 2:
93             p_reg->CONFIG2 = block_mask;
94             break;
95 #endif
96 
97 #if defined(BPROT_CONFIG3_REGION96_Pos)
98         case 3:
99             p_reg->CONFIG3 = block_mask;
100             break;
101 #endif
102 
103         default:
104             NRFX_ASSERT(false);
105             break;
106     }
107 }
108 
nrf_bprot_nvm_protection_in_debug_set(NRF_BPROT_Type * p_reg,bool enable)109 NRF_STATIC_INLINE void nrf_bprot_nvm_protection_in_debug_set(NRF_BPROT_Type * p_reg,
110                                                              bool             enable)
111 {
112     p_reg->DISABLEINDEBUG =
113         (enable ? 0 : BPROT_DISABLEINDEBUG_DISABLEINDEBUG_Msk);
114 }
115 
116 #endif // NRF_DECLARE_ONLY
117 
118 /** @} */
119 
120 #ifdef __cplusplus
121 }
122 #endif
123 
124 #endif // NRF_BPROT_H__
125