1 | /* Copyright 2018 Canaan Inc.
|
---|
2 | *
|
---|
3 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | * you may not use this file except in compliance with the License.
|
---|
5 | * You may obtain a copy of the License at
|
---|
6 | *
|
---|
7 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | *
|
---|
9 | * Unless required by applicable law or agreed to in writing, software
|
---|
10 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | * See the License for the specific language governing permissions and
|
---|
13 | * limitations under the License.
|
---|
14 | */
|
---|
15 | #ifndef _DRIVER_UTILS_H
|
---|
16 | #define _DRIVER_UTILS_H
|
---|
17 |
|
---|
18 | #ifdef __cplusplus
|
---|
19 | #include <cstdbool>
|
---|
20 | #include <cstddef>
|
---|
21 | #include <cstdint>
|
---|
22 | #else /* __cplusplus */
|
---|
23 | #include <stdbool.h>
|
---|
24 | #include <stddef.h>
|
---|
25 | #include <stdint.h>
|
---|
26 | #include <stdio.h>
|
---|
27 | #endif /* __cplusplus */
|
---|
28 |
|
---|
29 | #ifdef __cplusplus
|
---|
30 | extern "C" {
|
---|
31 | #endif /* __cplusplus */
|
---|
32 |
|
---|
33 | #define KENDRYTE_MIN(a, b) ((a) > (b) ? (b) : (a))
|
---|
34 | #define KENDRYTE_MAX(a, b) ((a) > (b) ? (a) : (b))
|
---|
35 |
|
---|
36 | #define FIX_CACHE 1
|
---|
37 |
|
---|
38 | #ifdef __ASSEMBLY__
|
---|
39 | #define KENDRYTE_CAST(type, ptr) ptr
|
---|
40 | #else /* __ASSEMBLY__ */
|
---|
41 | /**
|
---|
42 | * @brief Cast the pointer to specified pointer type.
|
---|
43 | *
|
---|
44 | * @param[in] type The pointer type to cast to
|
---|
45 | * @param[in] ptr The pointer to apply the type cast to
|
---|
46 | */
|
---|
47 | #define KENDRYTE_CAST(type, ptr) ((type)(ptr))
|
---|
48 | #endif /* __ASSEMBLY__ */
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * @addtogroup UTIL_RW_FUNC Memory Read/Write Utilities
|
---|
52 | *
|
---|
53 | * This section implements read and write functionality for various
|
---|
54 | * memory untis. The memory unit terms used for these functions are
|
---|
55 | * consistent with those used in the ARM Architecture Reference Manual
|
---|
56 | * ARMv7-A and ARMv7-R edition manual. The terms used for units of memory are:
|
---|
57 | *
|
---|
58 | * Unit of Memory | Abbreviation | Size in Bits
|
---|
59 | * :---------------|:-------------|:------------:
|
---|
60 | * Byte | byte | 8
|
---|
61 | * Half Word | hword | 16
|
---|
62 | * Word | word | 32
|
---|
63 | * Double Word | dword | 64
|
---|
64 | *
|
---|
65 | */
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * @brief Write the 8 bit byte to the destination address in device memory.
|
---|
69 | *
|
---|
70 | * @param[in] dest Write destination pointer address
|
---|
71 | * @param[in] src 8 bit data byte to write to memory
|
---|
72 | */
|
---|
73 | #define kendryte_write_byte(dest, src) \
|
---|
74 | (*KENDRYTE_CAST(volatile uint8_t *, (dest)) = (src))
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * @brief Read and return the 8 bit byte from the source address in device memory.
|
---|
78 | *
|
---|
79 | * @param[in] src Read source pointer address
|
---|
80 | *
|
---|
81 | * @return 8 bit data byte value
|
---|
82 | */
|
---|
83 | #define kendryte_read_byte(src) (*KENDRYTE_CAST(volatile uint8_t *, (src)))
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * @brief Write the 16 bit half word to the destination address in device memory.
|
---|
87 | *
|
---|
88 | * @param[in] dest Write destination pointer address
|
---|
89 | * @param[in] src 16 bit data half word to write to memory
|
---|
90 | */
|
---|
91 | #define kendryte_write_hword(dest, src) \
|
---|
92 | (*KENDRYTE_CAST(volatile uint16_t *, (dest)) = (src))
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * @brief Read and return the 16 bit half word from the source address in device
|
---|
96 | *
|
---|
97 | * @param[in] src Read source pointer address
|
---|
98 | *
|
---|
99 | * @return 16 bit data half word value
|
---|
100 | */
|
---|
101 | #define kendryte_read_hword(src) (*KENDRYTE_CAST(volatile uint16_t *, (src)))
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * @brief Write the 32 bit word to the destination address in device memory.
|
---|
105 | *
|
---|
106 | * @param[in] dest Write destination pointer address
|
---|
107 | * @param[in] src 32 bit data word to write to memory
|
---|
108 | */
|
---|
109 | #define kendryte_write_word(dest, src) \
|
---|
110 | (*KENDRYTE_CAST(volatile uint32_t *, (dest)) = (src))
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * @brief Read and return the 32 bit word from the source address in device memory.
|
---|
114 | *
|
---|
115 | * @param[in] src Read source pointer address
|
---|
116 | *
|
---|
117 | * @return 32 bit data half word value
|
---|
118 | */
|
---|
119 | #define kendryte_read_word(src) (*KENDRYTE_CAST(volatile uint32_t *, (src)))
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * @brief Write the 64 bit double word to the destination address in device memory.
|
---|
123 | *
|
---|
124 | * @param[in] dest Write destination pointer address
|
---|
125 | * @param[in] src 64 bit data word to write to memory
|
---|
126 | */
|
---|
127 | #define kendryte_write_dword(dest, src) \
|
---|
128 | (*KENDRYTE_CAST(volatile uint64_t *, (dest)) = (src))
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * @brief Read and return the 64 bit double word from the source address in device
|
---|
132 | *
|
---|
133 | * @param[in] src Read source pointer address
|
---|
134 | *
|
---|
135 | * @return 64 bit data half word value
|
---|
136 | */
|
---|
137 | #define kendryte_read_dword(src) (*KENDRYTE_CAST(volatile uint64_t *, (src)))
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * @brief Set selected bits in the 8 bit byte at the destination address in device
|
---|
141 | *
|
---|
142 | * @param[in] dest Destination pointer address
|
---|
143 | * @param[in] bits Bits to set in destination byte
|
---|
144 | */
|
---|
145 | #define kendryte_setbits_byte(dest, bits) \
|
---|
146 | (kendryte_write_byte(dest, kendryte_read_byte(dest) | (bits)))
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * @brief Clear selected bits in the 8 bit byte at the destination address in device
|
---|
150 | *
|
---|
151 | * @param[in] dest Destination pointer address
|
---|
152 | * @param[in] bits Bits to clear in destination byte
|
---|
153 | */
|
---|
154 | #define kendryte_clrbits_byte(dest, bits) \
|
---|
155 | (kendryte_write_byte(dest, kendryte_read_byte(dest) & ~(bits)))
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * @brief Change or toggle selected bits in the 8 bit byte at the destination address
|
---|
159 | *
|
---|
160 | * @param[in] dest Destination pointer address
|
---|
161 | * @param[in] bits Bits to change in destination byte
|
---|
162 | */
|
---|
163 | #define kendryte_xorbits_byte(dest, bits) \
|
---|
164 | (kendryte_write_byte(dest, kendryte_read_byte(dest) ^ (bits)))
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * @brief Replace selected bits in the 8 bit byte at the destination address in device
|
---|
168 | *
|
---|
169 | * @param[in] dest Destination pointer address
|
---|
170 | * @param[in] msk Bits to replace in destination byte
|
---|
171 | * @param[in] src Source bits to write to cleared bits in destination byte
|
---|
172 | */
|
---|
173 | #define kendryte_replbits_byte(dest, msk, src) \
|
---|
174 | (kendryte_write_byte(dest, (kendryte_read_byte(dest) & ~(msk)) | ((src) & (msk))))
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * @brief Set selected bits in the 16 bit halfword at the destination address in
|
---|
178 | *
|
---|
179 | * @param[in] dest Destination pointer address
|
---|
180 | * @param[in] bits Bits to set in destination halfword
|
---|
181 | */
|
---|
182 | #define kendryte_setbits_hword(dest, bits) \
|
---|
183 | (kendryte_write_hword(dest, kendryte_read_hword(dest) | (bits)))
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * @brief Clear selected bits in the 16 bit halfword at the destination address in
|
---|
187 | *
|
---|
188 | * @param[in] dest Destination pointer address
|
---|
189 | * @param[in] bits Bits to clear in destination halfword
|
---|
190 | */
|
---|
191 | #define kendryte_clrbits_hword(dest, bits) \
|
---|
192 | (kendryte_write_hword(dest, kendryte_read_hword(dest) & ~(bits)))
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * @brief Change or toggle selected bits in the 16 bit halfword at the destination
|
---|
196 | *
|
---|
197 | * @param[in] dest Destination pointer address
|
---|
198 | * @param[in] bits Bits to change in destination halfword
|
---|
199 | */
|
---|
200 | #define kendryte_xorbits_hword(dest, bits) \
|
---|
201 | (kendryte_write_hword(dest, kendryte_read_hword(dest) ^ (bits)))
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * @brief Replace selected bits in the 16 bit halfword at the destination address in
|
---|
205 | *
|
---|
206 | * @param[in] dest Destination pointer address
|
---|
207 | * @param[in] msk Bits to replace in destination byte
|
---|
208 | * @param[in] src Source bits to write to cleared bits in destination halfword
|
---|
209 | */
|
---|
210 | #define kendryte_replbits_hword(dest, msk, src) \
|
---|
211 | (kendryte_write_hword(dest, (kendryte_read_hword(dest) & ~(msk)) | ((src) & (msk))))
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * @brief Set selected bits in the 32 bit word at the destination address in device
|
---|
215 | *
|
---|
216 | * @param[in] dest Destination pointer address
|
---|
217 | * @param[in] bits Bits to set in destination word
|
---|
218 | */
|
---|
219 | #define kendryte_setbits_word(dest, bits) \
|
---|
220 | (kendryte_write_word(dest, kendryte_read_word(dest) | (bits)))
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * @brief Clear selected bits in the 32 bit word at the destination address in device
|
---|
224 | *
|
---|
225 | * @param[in] dest Destination pointer address
|
---|
226 | * @param[in] bits Bits to clear in destination word
|
---|
227 | */
|
---|
228 | #define kendryte_clrbits_word(dest, bits) \
|
---|
229 | (kendryte_write_word(dest, kendryte_read_word(dest) & ~(bits)))
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * @brief Change or toggle selected bits in the 32 bit word at the destination address
|
---|
233 | *
|
---|
234 | * @param[in] dest Destination pointer address
|
---|
235 | * @param[in] bits Bits to change in destination word
|
---|
236 | */
|
---|
237 | #define kendryte_xorbits_word(dest, bits) \
|
---|
238 | (kendryte_write_word(dest, kendryte_read_word(dest) ^ (bits)))
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * @brief Replace selected bits in the 32 bit word at the destination address in
|
---|
242 | *
|
---|
243 | * @param[in] dest Destination pointer address
|
---|
244 | * @param[in] msk Bits to replace in destination word
|
---|
245 | * @param[in] src Source bits to write to cleared bits in destination word
|
---|
246 | */
|
---|
247 | #define kendryte_replbits_word(dest, msk, src) \
|
---|
248 | (kendryte_write_word(dest, (kendryte_read_word(dest) & ~(msk)) | ((src) & (msk))))
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * @brief Set selected bits in the 64 bit doubleword at the destination address in
|
---|
252 | *
|
---|
253 | * @param[in] dest Destination pointer address
|
---|
254 | * @param[in] bits Bits to set in destination doubleword
|
---|
255 | */
|
---|
256 | #define kendryte_setbits_dword(dest, bits) \
|
---|
257 | (kendryte_write_dword(dest, kendryte_read_dword(dest) | (bits)))
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * @brief Clear selected bits in the 64 bit doubleword at the destination address in
|
---|
261 | *
|
---|
262 | * @param[in] dest Destination pointer address
|
---|
263 | * @param[in] bits Bits to clear in destination doubleword
|
---|
264 | */
|
---|
265 | #define kendryte_clrbits_dword(dest, bits) \
|
---|
266 | (kendryte_write_dword(dest, kendryte_read_dword(dest) & ~(bits)))
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * @brief Change or toggle selected bits in the 64 bit doubleword at the destination
|
---|
270 | *
|
---|
271 | * @param[in] dest Destination pointer address
|
---|
272 | * @param[in] bits Bits to change in destination doubleword
|
---|
273 | */
|
---|
274 | #define kendryte_xorbits_dword(dest, bits) \
|
---|
275 | (kendryte_write_dword(dest, kendryte_read_dword(dest) ^ (bits)))
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * @brief Replace selected bits in the 64 bit doubleword at the destination address in
|
---|
279 | *
|
---|
280 | * @param[in] dest Destination pointer address
|
---|
281 | * @param[in] msk its to replace in destination doubleword
|
---|
282 | * @param[in] src Source bits to write to cleared bits in destination word
|
---|
283 | */
|
---|
284 | #define kendryte_replbits_dword(dest, msk, src) \
|
---|
285 | (kendryte_write_dword(dest, (kendryte_read_dword(dest) & ~(msk)) | ((src) & (msk))))
|
---|
286 |
|
---|
287 | #define configASSERT(x) \
|
---|
288 | if((x) == 0) \
|
---|
289 | { \
|
---|
290 | printf("(%s:%d) %s\r\n", __FILE__, __LINE__, #x); \
|
---|
291 | for(;;) \
|
---|
292 | ; \
|
---|
293 | }
|
---|
294 |
|
---|
295 | /**
|
---|
296 | * @brief Set value by mask
|
---|
297 | *
|
---|
298 | * @param[in] bits The one be set
|
---|
299 | * @param[in] mask mask value
|
---|
300 | * @param[in] value The value to set
|
---|
301 | */
|
---|
302 | void set_bit(volatile uint32_t *bits, uint32_t mask, uint32_t value);
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * @brief Set value by mask
|
---|
306 | *
|
---|
307 | * @param[in] bits The one be set
|
---|
308 | * @param[in] mask Mask value
|
---|
309 | * @param[in] offset Mask's offset
|
---|
310 | * @param[in] value The value to set
|
---|
311 | */
|
---|
312 | void set_bit_offset(volatile uint32_t *bits, uint32_t mask, size_t offset, uint32_t value);
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * @brief Set bit for gpio, only set one bit
|
---|
316 | *
|
---|
317 | * @param[in] bits The one be set
|
---|
318 | * @param[in] idx Offset value
|
---|
319 | * @param[in] value The value to set
|
---|
320 | */
|
---|
321 | void set_gpio_bit(volatile uint32_t *bits, size_t idx, uint32_t value);
|
---|
322 |
|
---|
323 | /**
|
---|
324 | * @brief Get bits value of mask
|
---|
325 | *
|
---|
326 | * @param[in] bits The source data
|
---|
327 | * @param[in] mask Mask value
|
---|
328 | * @param[in] offset Mask's offset
|
---|
329 | *
|
---|
330 | * @return The bits value of mask
|
---|
331 | */
|
---|
332 | uint32_t get_bit(volatile uint32_t *bits, uint32_t mask, size_t offset);
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * @brief Get a bit value by offset
|
---|
336 | *
|
---|
337 | * @param[in] bits The source data
|
---|
338 | * @param[in] offset Bit's offset
|
---|
339 | *
|
---|
340 | *
|
---|
341 | * @return The bit value
|
---|
342 | */
|
---|
343 | uint32_t get_gpio_bit(volatile uint32_t *bits, size_t offset);
|
---|
344 |
|
---|
345 | uint32_t is_memory_cache(uintptr_t address);
|
---|
346 | #ifdef __cplusplus
|
---|
347 | }
|
---|
348 | #endif /* __cplusplus */
|
---|
349 | #endif /* _DRIVER_COMMON_H */
|
---|