HPM SDK
HPMicro Software Development Kit
hpm_common.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2023 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef _HPM_COMMON_H
9 #define _HPM_COMMON_H
10 
11 #include <assert.h>
12 #include <stdbool.h>
13 #include <stdint.h>
14 #include <string.h>
15 #include <stdlib.h>
16 
25 #define __R volatile const /* Define "read-only" permission */
26 #define __RW volatile /* Define "read-write" permission */
27 #define __W volatile /* Define "write-only" permission */
28 
29 #ifndef __I
30 #define __I __R
31 #endif
32 
33 #ifndef __IO
34 #define __IO __RW
35 #endif
36 
37 #ifndef __O
38 #define __O __W
39 #endif
40 
41 #ifndef ARRAY_SIZE
42 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
43 #endif
44 
45 #ifndef MAX
46 #define MAX(a, b) ((a) > (b) ? (a) : (b))
47 #endif
48 #ifndef MIN
49 #define MIN(a, b) ((a) < (b) ? (a) : (b))
50 #endif
51 
52 #ifndef HPM_ALIGN_DOWN
53 #define HPM_ALIGN_DOWN(a, n) ((uint32_t)(a) & ~(n-1U))
54 #endif
55 
56 #ifndef HPM_ALIGN_UP
57 #define HPM_ALIGN_UP(a, n) (((uint32_t)(a) + (n-1U)) & ~(n-1U))
58 #endif
59 
60 #define HPM_BITSMASK(val, offset) ((uint32_t)(val) << (offset))
61 #define IS_HPM_BITMASK_SET(val, mask) (((uint32_t)(val) & (uint32_t)(mask)) != 0U)
62 #define IS_HPM_BIT_SET(val, offset) (((uint32_t)(val) & (1UL << (offset))) != 0U)
63 #define IS_HPM_BITMASK_CLR(val, mask) (((uint32_t)(val) & (uint32_t)(mask)) == 0U)
64 #define IS_HPM_BIT_CLR(val, offset) (((uint32_t)(val) & (1UL << (offset))) == 0U)
65 
66 #define HPM_BREAK_IF(cond) if (cond) { break; }
67 #define HPM_CONTINUE_IF(cond) if (cond) { continue; }
68 
69 #define HPM_DIV_ROUND_CLOSEST(x, div) (((x) + (div) / 2) / (div))
70 #define HPM_DIV_ROUND_UP(x, div) (((x) + (div) - 1) / (div))
71 
72 #define HPM_NUM_TO_EVEN_CEILING(x) ((x + 1) & 0xFFFFFFFEUL)
73 #define HPM_NUM_TO_EVEN_FLOOR(x) ((x) & 0xFFFFFFFEUL)
74 
75 #define HPM_CHECK_RET(x) \
76  do { \
77  stat = (x); \
78  if (status_success != stat) { \
79  return stat; \
80  } \
81  } while (false)
82 
83 #define SIZE_1KB (1024UL)
84 #define SIZE_1MB (1048576UL)
85 
86 #define BIT0_MASK (0x00000001UL)
87 #define BIT1_MASK (0x00000002UL)
88 #define BIT2_MASK (0x00000004UL)
89 #define BIT3_MASK (0x00000008UL)
90 #define BIT4_MASK (0x00000010UL)
91 #define BIT5_MASK (0x00000020UL)
92 #define BIT6_MASK (0x00000040UL)
93 #define BIT7_MASK (0x00000080UL)
94 #define BIT8_MASK (0x00000100UL)
95 #define BIT9_MASK (0x00000200UL)
96 #define BIT10_MASK (0x00000400UL)
97 #define BIT11_MASK (0x00000800UL)
98 #define BIT12_MASK (0x00001000UL)
99 #define BIT13_MASK (0x00002000UL)
100 #define BIT14_MASK (0x00004000UL)
101 #define BIT15_MASK (0x00008000UL)
102 #define BIT16_MASK (0x00010000UL)
103 #define BIT17_MASK (0x00020000UL)
104 #define BIT18_MASK (0x00040000UL)
105 #define BIT19_MASK (0x00080000UL)
106 #define BIT20_MASK (0x00100000UL)
107 #define BIT21_MASK (0x00200000UL)
108 #define BIT22_MASK (0x00400000UL)
109 #define BIT23_MASK (0x00800000UL)
110 #define BIT24_MASK (0x01000000UL)
111 #define BIT25_MASK (0x02000000UL)
112 #define BIT26_MASK (0x04000000UL)
113 #define BIT27_MASK (0x08000000UL)
114 #define BIT28_MASK (0x10000000UL)
115 #define BIT29_MASK (0x20000000UL)
116 #define BIT30_MASK (0x40000000UL)
117 #define BIT31_MASK (0x80000000UL)
118 
119 typedef uint32_t hpm_stat_t;
120 
121 /* @brief Enum definition for the Status group
122  * Rule:
123  * [Group] 0-999 for the SoC driver and the corresponding components
124  * 1000 or above for the application status group
125  * [Code] Valid value: 0-999
126  *
127  */
128 #define MAKE_STATUS(group, code) ((uint32_t)(group)*1000U + (uint32_t)(code))
129 /* @brief System status group definitions */
130 enum {
159 
166 };
167 
168 /* @brief Common status code definitions */
169 enum {
174 };
175 
176 #if defined(__GNUC__)
177 
178 /* alway_inline */
179 #define ATTR_ALWAYS_INLINE __attribute__((always_inline))
180 
181 /* weak */
182 #define ATTR_WEAK __attribute__((weak))
183 
184 /* alignment */
185 #define ATTR_ALIGN(alignment) __attribute__((aligned(alignment)))
186 
187 /* place var_declare at section_name, e.x. PLACE_AT(".target_section", var); */
188 #define ATTR_PLACE_AT(section_name) __attribute__((section(section_name)))
189 
190 #define ATTR_PLACE_AT_WITH_ALIGNMENT(section_name, alignment) \
191 ATTR_PLACE_AT(section_name) ATTR_ALIGN(alignment)
192 
193 #define ATTR_PLACE_AT_NONCACHEABLE ATTR_PLACE_AT(".noncacheable.bss")
194 #define ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(alignment) \
195  ATTR_PLACE_AT_NONCACHEABLE ATTR_ALIGN(alignment)
196 
197 #define ATTR_PLACE_AT_NONCACHEABLE_BSS ATTR_PLACE_AT(".noncacheable.bss")
198 #define ATTR_PLACE_AT_NONCACHEABLE_BSS_WITH_ALIGNMENT(alignment) \
199  ATTR_PLACE_AT_NONCACHEABLE_BSS ATTR_ALIGN(alignment)
200 
201 /* initialize variable x with y using PLACE_AT_NONCACHEABLE_INIT(x) = {y}; */
202 #define ATTR_PLACE_AT_NONCACHEABLE_INIT ATTR_PLACE_AT(".noncacheable.init")
203 #define ATTR_PLACE_AT_NONCACHEABLE_INIT_WITH_ALIGNMENT(alignment) \
204  ATTR_PLACE_AT_NONCACHEABLE_INIT ATTR_ALIGN(alignment)
205 
206 #define ATTR_RAMFUNC ATTR_PLACE_AT(".fast")
207 #define ATTR_RAMFUNC_WITH_ALIGNMENT(alignment) \
208  ATTR_RAMFUNC ATTR_ALIGN(alignment)
209 
210 #define ATTR_SHARE_MEM ATTR_PLACE_AT(".sh_mem")
211 
212 #define NOP() __asm volatile("nop")
213 #define WFI() __asm volatile("wfi")
214 
215 #define HPM_ATTR_MACHINE_INTERRUPT __attribute__ ((section(".isr_vector"), interrupt("machine"), aligned(4)))
216 #define HPM_ATTR_SUPERVISOR_INTERRUPT __attribute__ ((section(".isr_s_vector"), interrupt("supervisor"), aligned(4)))
217 
218 #elif defined(__ICCRISCV__)
219 
220 
221 /* alway_inline */
222 #define ATTR_ALWAYS_INLINE __attribute__((always_inline))
223 
224 /* weak */
225 #define ATTR_WEAK __weak
226 
227 /* alignment */
228 #define ATTR_ALIGN(alignment) __attribute__((aligned(alignment)))
229 
230 /* place var_declare at section_name, e.x. PLACE_AT(".target_section", var); */
231 #define ATTR_PLACE_AT(section_name) __attribute__((section(section_name)))
232 
233 #define ATTR_PLACE_AT_WITH_ALIGNMENT(section_name, alignment) \
234 ATTR_PLACE_AT(section_name) ATTR_ALIGN(alignment)
235 
236 #define ATTR_PLACE_AT_NONCACHEABLE ATTR_PLACE_AT(".noncacheable.bss")
237 #define ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(alignment) \
238  ATTR_PLACE_AT_NONCACHEABLE ATTR_ALIGN(alignment)
239 
240 #define ATTR_PLACE_AT_NONCACHEABLE_BSS ATTR_PLACE_AT(".noncacheable.bss")
241 #define ATTR_PLACE_AT_NONCACHEABLE_BSS_WITH_ALIGNMENT(alignment) \
242  ATTR_PLACE_AT_NONCACHEABLE_BSS ATTR_ALIGN(alignment)
243 
244 /* initialize variable x with y using PLACE_AT_NONCACHEABLE_INIT(x) = {y}; */
245 #define ATTR_PLACE_AT_NONCACHEABLE_INIT ATTR_PLACE_AT(".noncacheable.init")
246 #define ATTR_PLACE_AT_NONCACHEABLE_INIT_WITH_ALIGNMENT(alignment) \
247  ATTR_PLACE_AT_NONCACHEABLE_INIT ATTR_ALIGN(alignment)
248 
249 #define ATTR_RAMFUNC ATTR_PLACE_AT(".fast")
250 #define ATTR_RAMFUNC_WITH_ALIGNMENT(alignment) \
251  ATTR_RAMFUNC ATTR_ALIGN(alignment)
252 
253 #define ATTR_SHARE_MEM ATTR_PLACE_AT(".sh_mem")
254 
255 #define NOP() __asm volatile("nop")
256 #define WFI() __asm volatile("wfi")
257 
258 #define HPM_ATTR_MACHINE_INTERRUPT __machine __interrupt
259 #define HPM_ATTR_SUPERVISOR_INTERRUPT __supervisor __interrupt
260 
261 #ifndef __TIMEVAL_DEFINED
262 #define __TIMEVAL_DEFINED 1
263 struct timeval {
264  long tv_sec; /* Seconds since the Epoch */
265  long tv_usec; /* Microseconds */
266 };
267 #endif
268 
269 #else
270 #error Unknown toolchain
271 #endif
272 
273 #ifdef __cplusplus
274 extern "C" {
275 #endif
276 
277 
285 static inline uint32_t count_set_bits(uint32_t value)
286 {
287  if (value == 0) {
288  return 0;
289  }
290  return 1 + count_set_bits(value & (value - 1));
291 }
292 
301 static inline uint32_t get_first_set_bit_from_lsb(uint32_t value)
302 {
303  uint32_t i = 0;
304  if (!value) {
305  return 0xFFFFFFFFUL;
306  }
307  while (value && !(value & 0x1)) {
308  value >>= 1;
309  i++;
310  }
311  return i;
312 }
313 
322 static inline uint32_t get_first_set_bit_from_msb(uint32_t value)
323 {
324  uint32_t i = 31;
325  if (!value) {
326  return 0xFFFFFFFFUL;
327  }
328  while (value && !(value & 0x80000000)) {
329  value <<= 1;
330  value &= ~1;
331  i--;
332  }
333  return i;
334 }
335 
336 #ifdef __cplusplus
337 }
338 #endif
339 
343 #endif /* _HPM_COMMON_H */
static uint32_t get_first_set_bit_from_lsb(uint32_t value)
Count bits set to 1 from least significant bit.
Definition: hpm_common.h:301
uint32_t hpm_stat_t
Definition: hpm_common.h:119
static uint32_t get_first_set_bit_from_msb(uint32_t value)
Count bits set to 1 from most significant bit.
Definition: hpm_common.h:322
static uint32_t count_set_bits(uint32_t value)
Count bits set to 1.
Definition: hpm_common.h:285
#define MAKE_STATUS(group, code)
Definition: hpm_common.h:128
@ status_timeout
Definition: hpm_common.h:173
@ status_invalid_argument
Definition: hpm_common.h:172
@ status_success
Definition: hpm_common.h:170
@ status_fail
Definition: hpm_common.h:171
@ status_group_common
Definition: hpm_common.h:131
@ status_group_usb
Definition: hpm_common.h:135
@ status_group_ewdg
Definition: hpm_common.h:158
@ status_group_i2c
Definition: hpm_common.h:133
@ status_group_audio_codec
Definition: hpm_common.h:162
@ status_group_spi
Definition: hpm_common.h:134
@ status_group_middleware_start
Definition: hpm_common.h:160
@ status_group_mbx
Definition: hpm_common.h:145
@ status_group_can
Definition: hpm_common.h:150
@ status_group_pllctl
Definition: hpm_common.h:154
@ status_group_femc
Definition: hpm_common.h:140
@ status_group_sdmmc
Definition: hpm_common.h:161
@ status_group_ffa
Definition: hpm_common.h:156
@ status_group_uart
Definition: hpm_common.h:132
@ status_group_dma_manager
Definition: hpm_common.h:163
@ status_group_xpi
Definition: hpm_common.h:137
@ status_group_i2s
Definition: hpm_common.h:136
@ status_group_otp
Definition: hpm_common.h:143
@ status_group_mcan
Definition: hpm_common.h:157
@ status_group_pllctlv2
Definition: hpm_common.h:155
@ status_group_sdxc
Definition: hpm_common.h:151
@ status_group_pdma
Definition: hpm_common.h:147
@ status_group_spi_nor_flash
Definition: hpm_common.h:164
@ status_group_pmic_sec
Definition: hpm_common.h:149
@ status_group_clk
Definition: hpm_common.h:153
@ status_group_wdg
Definition: hpm_common.h:148
@ status_group_dma
Definition: hpm_common.h:139
@ status_group_lcdc
Definition: hpm_common.h:144
@ status_group_pcfg
Definition: hpm_common.h:152
@ status_group_touch
Definition: hpm_common.h:165
@ status_group_sdp
Definition: hpm_common.h:141
@ status_group_l1c
Definition: hpm_common.h:138
@ status_group_xpi_nor
Definition: hpm_common.h:142
@ status_group_rng
Definition: hpm_common.h:146