1 /*
2  * Copyright (c) 2018 - 2025, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  *    list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  *    contributors may be used to endorse or promote products derived from this
19  *    software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef NRF_FICR_H__
35 #define NRF_FICR_H__
36 
37 #include <nrfx.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /**
44  * @defgroup nrf_ficr_hal FICR HAL
45  * @{
46  * @ingroup nrf_icr
47  * @brief   Hardware access layer (HAL) for getting data from
48  *          the Factory Information Configuration Registers (FICR).
49  */
50 
51 #if defined(FICR_CODEPAGESIZE_CODEPAGESIZE_Msk) || defined(NRF51) || defined(__NRFX_DOXYGEN__)
52 /** @brief Symbol indicating whether FICR CODEPAGESIZE register is present. */
53 #define NRF_FICR_HAS_CODE_PAGE_SIZE 1
54 #else
55 #define NRF_FICR_HAS_CODE_PAGE_SIZE 0
56 #endif
57 
58 #if defined(FICR_INFO_CODEPAGESIZE_CODEPAGESIZE_Msk) || defined(__NRFX_DOXYGEN__)
59 /** @brief Symbol indicating whether FICR INFO.CODEPAGESIZE register is present. */
60 #define NRF_FICR_HAS_INFO_CODE_PAGE_SIZE 1
61 #else
62 #define NRF_FICR_HAS_INFO_CODE_PAGE_SIZE 0
63 #endif
64 
65 #if defined(FICR_CODESIZE_CODESIZE_Msk) || defined(NRF51) || defined(__NRFX_DOXYGEN__)
66 /** @brief Symbol indicating whether FICR CODESIZE register is present. */
67 #define NRF_FICR_HAS_CODE_SIZE 1
68 #else
69 #define NRF_FICR_HAS_CODE_SIZE 0
70 #endif
71 
72 #if defined(FICR_INFO_CODESIZE_CODESIZE_Msk) || defined(__NRFX_DOXYGEN__)
73 /** @brief Symbol indicating whether FICR INFO.CODESIZE register is present. */
74 #define NRF_FICR_HAS_INFO_CODE_SIZE 1
75 #else
76 #define NRF_FICR_HAS_INFO_CODE_SIZE 0
77 #endif
78 
79 #if defined(FICR_DEVICEID_DEVICEID_Msk) || defined(NRF51) || defined(__NRFX_DOXYGEN__)
80 /** @brief Symbol indicating whether FICR DEVICEID[n] registers are present. */
81 #define NRF_FICR_HAS_DEVICE_ID 1
82 #else
83 #define NRF_FICR_HAS_DEVICE_ID 0
84 #endif
85 
86 #if defined(FICR_INFO_DEVICEID_DEVICEID_Msk) || defined(__NRFX_DOXYGEN__)
87 /** @brief Symbol indicating whether FICR INFO.DEVICEID[n] registers are present. */
88 #define NRF_FICR_HAS_INFO_DEVICE_ID 1
89 #else
90 #define NRF_FICR_HAS_INFO_DEVICE_ID 0
91 #endif
92 
93 #if defined(FICR_NFC_TAGHEADER0_MFGID_Msk) || defined(__NRFX_DOXYGEN__)
94 /** @brief Symbol indicating whether FICR NFC.TAGHEADERn registers are present. */
95 #define NRF_FICR_HAS_NFC_TAGHEADER 1
96 #else
97 #define NRF_FICR_HAS_NFC_TAGHEADER 0
98 #endif
99 
100 #if defined(FICR_NFC_TAGHEADER_UD0_Msk) || defined(__NRFX_DOXYGEN__)
101 /** @brief Symbol indicating whether FICR NFC registers have tagheader array layout. */
102 #define NRF_FICR_HAS_NFC_TAGHEADER_ARRAY 1
103 #else
104 #define NRF_FICR_HAS_NFC_TAGHEADER_ARRAY 0
105 #endif
106 
107 #if defined(FICR_DEVICEADDR_DEVICEADDR_Msk) || defined(NRF51) || defined(__NRFX_DOXYGEN__)
108 /** @brief Symbol indicating whether FICR DEVICEADDR[n] registers are present. */
109 #define NRF_FICR_HAS_DEVICE_ADDR 1
110 #else
111 #define NRF_FICR_HAS_DEVICE_ADDR 0
112 #endif
113 
114 #if defined(FICR_BLE_ADDR_ADDR_Msk) || defined(__NRFX_DOXYGEN__)
115 /** @brief Symbol indicating whether FICR BLE.ADDR[n] registers are present. */
116 #define NRF_FICR_HAS_BLE_ADDR 1
117 #else
118 #define NRF_FICR_HAS_BLE_ADDR 0
119 #endif
120 
121 #if defined(FICR_ER_ER_Msk) || defined(NRF51) || defined(__NRFX_DOXYGEN__)
122 /** @brief Symbol indicating whether FICR ER[n] registers are present. */
123 #define NRF_FICR_HAS_ER 1
124 #else
125 #define NRF_FICR_HAS_ER 0
126 #endif
127 
128 #if defined(FICR_BLE_ER_ER_Msk) || defined(__NRFX_DOXYGEN__)
129 /** @brief Symbol indicating whether FICR BLE.ER[n] registers are present. */
130 #define NRF_FICR_HAS_BLE_ER 1
131 #else
132 #define NRF_FICR_HAS_BLE_ER 0
133 #endif
134 
135 #if defined(FICR_IR_IR_Msk) || defined(NRF51) || defined(__NRFX_DOXYGEN__)
136 /** @brief Symbol indicating whether FICR IR[n] registers are present. */
137 #define NRF_FICR_HAS_IR 1
138 #else
139 #define NRF_FICR_HAS_IR 0
140 #endif
141 
142 #if defined(FICR_BLE_IR_IR_Msk) || defined(__NRFX_DOXYGEN__)
143 /** @brief Symbol indicating whether FICR BLE.IR[n] registers are present. */
144 #define NRF_FICR_HAS_BLE_IR 1
145 #else
146 #define NRF_FICR_HAS_BLE_IR 0
147 #endif
148 
149 #if NRF_FICR_HAS_CODE_PAGE_SIZE || NRF_FICR_HAS_INFO_CODE_PAGE_SIZE
150 /**
151  * @brief Function for getting the size of the code memory page.
152  *
153  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
154  *
155  * @return Code memory page size in bytes.
156  */
157 NRF_STATIC_INLINE uint32_t nrf_ficr_codepagesize_get(NRF_FICR_Type const * p_reg);
158 #endif
159 
160 #if NRF_FICR_HAS_CODE_SIZE || NRF_FICR_HAS_INFO_CODE_SIZE
161 /**
162  * @brief Function for getting the size of the code memory rendered as number of pages.
163  *
164  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
165  *
166  * @return Code memory size rendered as number of pages.
167  */
168 NRF_STATIC_INLINE uint32_t nrf_ficr_codesize_get(NRF_FICR_Type const * p_reg);
169 #endif
170 
171 #if NRF_FICR_HAS_DEVICE_ID || NRF_FICR_HAS_INFO_DEVICE_ID
172 /**
173  * @brief Function for getting the unique device identifier.
174  *
175  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
176  * @param[in] reg_id Register index.
177  *
178  * @return Unique device identifier.
179  */
180 NRF_STATIC_INLINE uint32_t nrf_ficr_deviceid_get(NRF_FICR_Type const * p_reg, uint32_t reg_id);
181 #endif
182 
183 #if NRF_FICR_HAS_NFC_TAGHEADER || NRF_FICR_HAS_NFC_TAGHEADER_ARRAY
184 /**
185  * @brief Function for getting the default header values for the NFC tag.
186  *
187  * @param[in] p_reg        Pointer to the structure of registers of the peripheral.
188  * @param[in] tagheader_id Tag header index.
189  *
190  * @return The default header value of the NFC tag for the specified header index.
191  */
192 NRF_STATIC_INLINE uint32_t nrf_ficr_nfc_tagheader_get(NRF_FICR_Type const * p_reg,
193                                                       uint32_t              tagheader_id);
194 #endif
195 
196 #if NRF_FICR_HAS_DEVICE_ADDR || NRF_FICR_HAS_BLE_ADDR
197 /**
198  * @brief Function for getting the unique device address.
199  *
200  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
201  * @param[in] reg_id Register index.
202  *
203  * @return Unique device address.
204  */
205 NRF_STATIC_INLINE uint32_t nrf_ficr_deviceaddr_get(NRF_FICR_Type const * p_reg, uint32_t reg_id);
206 #endif
207 
208 #if NRF_FICR_HAS_ER || NRF_FICR_HAS_BLE_ER
209 /**
210  * @brief Function for getting the unique encryption root.
211  *
212  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
213  * @param[in] reg_id Register index.
214  *
215  * @return Unique encryption root.
216  */
217 NRF_STATIC_INLINE uint32_t nrf_ficr_er_get(NRF_FICR_Type const * p_reg, uint32_t reg_id);
218 #endif
219 
220 #if NRF_FICR_HAS_IR || NRF_FICR_HAS_BLE_IR
221 /**
222  * @brief Function for getting the unique identity root.
223  *
224  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
225  * @param[in] reg_id Register index.
226  *
227  * @return Unique identity root.
228  */
229 NRF_STATIC_INLINE uint32_t nrf_ficr_ir_get(NRF_FICR_Type const * p_reg, uint32_t reg_id);
230 #endif
231 
232 #ifndef NRF_DECLARE_ONLY
233 
234 #if NRF_FICR_HAS_CODE_PAGE_SIZE || NRF_FICR_HAS_INFO_CODE_PAGE_SIZE
nrf_ficr_codepagesize_get(NRF_FICR_Type const * p_reg)235 NRF_STATIC_INLINE uint32_t nrf_ficr_codepagesize_get(NRF_FICR_Type const * p_reg)
236 {
237 #if NRF_FICR_HAS_INFO_CODE_PAGE_SIZE
238     return p_reg->INFO.CODEPAGESIZE;
239 #else
240     return p_reg->CODEPAGESIZE;
241 #endif
242 }
243 #endif
244 
245 #if NRF_FICR_HAS_CODE_SIZE || NRF_FICR_HAS_INFO_CODE_SIZE
nrf_ficr_codesize_get(NRF_FICR_Type const * p_reg)246 NRF_STATIC_INLINE uint32_t nrf_ficr_codesize_get(NRF_FICR_Type const * p_reg)
247 {
248 #if NRF_FICR_HAS_INFO_CODE_SIZE
249     return p_reg->INFO.CODESIZE;
250 #else
251     return p_reg->CODESIZE;
252 #endif
253 }
254 #endif
255 
256 #if NRF_FICR_HAS_DEVICE_ID || NRF_FICR_HAS_INFO_DEVICE_ID
nrf_ficr_deviceid_get(NRF_FICR_Type const * p_reg,uint32_t reg_id)257 NRF_STATIC_INLINE uint32_t nrf_ficr_deviceid_get(NRF_FICR_Type const * p_reg, uint32_t reg_id)
258 {
259 #if NRF_FICR_HAS_INFO_DEVICE_ID
260     return p_reg->INFO.DEVICEID[reg_id];
261 #else
262     return p_reg->DEVICEID[reg_id];
263 #endif
264 }
265 #endif
266 
267 #if NRF_FICR_HAS_NFC_TAGHEADER || NRF_FICR_HAS_NFC_TAGHEADER_ARRAY
nrf_ficr_nfc_tagheader_get(NRF_FICR_Type const * p_reg,uint32_t tagheader_id)268 NRF_STATIC_INLINE uint32_t nrf_ficr_nfc_tagheader_get(NRF_FICR_Type const * p_reg,
269                                                       uint32_t              tagheader_id)
270 {
271 #if NRF_FICR_HAS_NFC_TAGHEADER_ARRAY
272     switch(tagheader_id) {
273         case 0:
274             return p_reg->NFC.TAGHEADER[0];
275         case 1:
276             return p_reg->NFC.TAGHEADER[1];
277         case 2:
278             return p_reg->NFC.TAGHEADER[2];
279         case 3:
280             return p_reg->NFC.TAGHEADER[3];
281         default:
282             return 0;
283     }
284 #else
285     switch(tagheader_id) {
286         case 0:
287             return p_reg->NFC.TAGHEADER0;
288         case 1:
289             return p_reg->NFC.TAGHEADER1;
290         case 2:
291             return p_reg->NFC.TAGHEADER2;
292         case 3:
293             return p_reg->NFC.TAGHEADER3;
294         default:
295             return 0;
296     }
297 #endif
298 }
299 #endif
300 
301 #if NRF_FICR_HAS_DEVICE_ADDR || NRF_FICR_HAS_BLE_ADDR
nrf_ficr_deviceaddr_get(NRF_FICR_Type const * p_reg,uint32_t reg_id)302 NRF_STATIC_INLINE uint32_t nrf_ficr_deviceaddr_get(NRF_FICR_Type const * p_reg, uint32_t reg_id)
303 {
304 #if NRF_FICR_HAS_BLE_ADDR
305     return p_reg->BLE.ADDR[reg_id];
306 #else
307     return p_reg->DEVICEADDR[reg_id];
308 #endif
309 }
310 #endif
311 
312 #if NRF_FICR_HAS_ER || NRF_FICR_HAS_BLE_ER
nrf_ficr_er_get(NRF_FICR_Type const * p_reg,uint32_t reg_id)313 NRF_STATIC_INLINE uint32_t nrf_ficr_er_get(NRF_FICR_Type const * p_reg, uint32_t reg_id)
314 {
315 #if NRF_FICR_HAS_BLE_ER
316     return p_reg->BLE.ER[reg_id];
317 #else
318     return p_reg->ER[reg_id];
319 #endif
320 }
321 #endif
322 
323 #if NRF_FICR_HAS_IR || NRF_FICR_HAS_BLE_IR
nrf_ficr_ir_get(NRF_FICR_Type const * p_reg,uint32_t reg_id)324 NRF_STATIC_INLINE uint32_t nrf_ficr_ir_get(NRF_FICR_Type const * p_reg, uint32_t reg_id)
325 {
326 #if NRF_FICR_HAS_BLE_IR
327     return p_reg->BLE.IR[reg_id];
328 #else
329     return p_reg->IR[reg_id];
330 #endif
331 }
332 #endif
333 
334 #endif // NRF_DECLARE_ONLY
335 
336 /** @} */
337 
338 #ifdef __cplusplus
339 }
340 #endif
341 
342 #endif // NRF_FICR_H__
343