1 /*
2  * Copyright (c) 2018 - 2024, 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 NRF_FICR_HAS_CODE_PAGE_SIZE || NRF_FICR_HAS_INFO_CODE_PAGE_SIZE
108 /**
109  * @brief Function for getting the size of the code memory page.
110  *
111  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
112  *
113  * @return Code memory page size in bytes.
114  */
115 NRF_STATIC_INLINE uint32_t nrf_ficr_codepagesize_get(NRF_FICR_Type const * p_reg);
116 #endif
117 
118 #if NRF_FICR_HAS_CODE_SIZE || NRF_FICR_HAS_INFO_CODE_SIZE
119 /**
120  * @brief Function for getting the size of the code memory rendered as number of pages.
121  *
122  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
123  *
124  * @return Code memory size rendered as number of pages.
125  */
126 NRF_STATIC_INLINE uint32_t nrf_ficr_codesize_get(NRF_FICR_Type const * p_reg);
127 #endif
128 
129 #if NRF_FICR_HAS_DEVICE_ID || NRF_FICR_HAS_INFO_DEVICE_ID
130 /**
131  * @brief Function for getting the unique device identifier.
132  *
133  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
134  * @param[in] reg_id Register index.
135  *
136  * @return Unique device identifier.
137  */
138 NRF_STATIC_INLINE uint32_t nrf_ficr_deviceid_get(NRF_FICR_Type const * p_reg, uint32_t reg_id);
139 #endif
140 
141 #if NRF_FICR_HAS_NFC_TAGHEADER || NRF_FICR_HAS_NFC_TAGHEADER_ARRAY
142 /**
143  * @brief Function for getting the default header values for the NFC tag.
144  *
145  * @param[in] p_reg        Pointer to the structure of registers of the peripheral.
146  * @param[in] tagheader_id Tag header index.
147  *
148  * @return The default header value of the NFC tag for the specified header index.
149  */
150 NRF_STATIC_INLINE uint32_t nrf_ficr_nfc_tagheader_get(NRF_FICR_Type const * p_reg,
151                                                       uint32_t              tagheader_id);
152 #endif
153 
154 #ifndef NRF_DECLARE_ONLY
155 
156 #if NRF_FICR_HAS_CODE_PAGE_SIZE || NRF_FICR_HAS_INFO_CODE_PAGE_SIZE
nrf_ficr_codepagesize_get(NRF_FICR_Type const * p_reg)157 NRF_STATIC_INLINE uint32_t nrf_ficr_codepagesize_get(NRF_FICR_Type const * p_reg)
158 {
159 #if NRF_FICR_HAS_INFO_CODE_PAGE_SIZE
160     return p_reg->INFO.CODEPAGESIZE;
161 #else
162     return p_reg->CODEPAGESIZE;
163 #endif
164 }
165 #endif
166 
167 #if NRF_FICR_HAS_CODE_SIZE || NRF_FICR_HAS_INFO_CODE_SIZE
nrf_ficr_codesize_get(NRF_FICR_Type const * p_reg)168 NRF_STATIC_INLINE uint32_t nrf_ficr_codesize_get(NRF_FICR_Type const * p_reg)
169 {
170 #if NRF_FICR_HAS_INFO_CODE_SIZE
171     return p_reg->INFO.CODESIZE;
172 #else
173     return p_reg->CODESIZE;
174 #endif
175 }
176 #endif
177 
178 #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)179 NRF_STATIC_INLINE uint32_t nrf_ficr_deviceid_get(NRF_FICR_Type const * p_reg, uint32_t reg_id)
180 {
181 #if NRF_FICR_HAS_INFO_DEVICE_ID
182     return p_reg->INFO.DEVICEID[reg_id];
183 #else
184     return p_reg->DEVICEID[reg_id];
185 #endif
186 }
187 #endif
188 
189 #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)190 NRF_STATIC_INLINE uint32_t nrf_ficr_nfc_tagheader_get(NRF_FICR_Type const * p_reg,
191                                                       uint32_t              tagheader_id)
192 {
193 #if NRF_FICR_HAS_NFC_TAGHEADER_ARRAY
194     switch(tagheader_id) {
195         case 0:
196             return p_reg->NFC.TAGHEADER[0];
197         case 1:
198             return p_reg->NFC.TAGHEADER[1];
199         case 2:
200             return p_reg->NFC.TAGHEADER[2];
201         case 3:
202             return p_reg->NFC.TAGHEADER[3];
203         default:
204             return 0;
205     }
206 #else
207     switch(tagheader_id) {
208         case 0:
209             return p_reg->NFC.TAGHEADER0;
210         case 1:
211             return p_reg->NFC.TAGHEADER1;
212         case 2:
213             return p_reg->NFC.TAGHEADER2;
214         case 3:
215             return p_reg->NFC.TAGHEADER3;
216         default:
217             return 0;
218     }
219 #endif
220 }
221 #endif
222 
223 #endif // NRF_DECLARE_ONLY
224 
225 /** @} */
226 
227 #ifdef __cplusplus
228 }
229 #endif
230 
231 #endif // NRF_FICR_H__
232