summaryrefslogtreecommitdiff
path: root/bsp/hpmicro/libraries/hpm_sdk/drivers/inc/hpm_common.h
blob: 218f9af87c37cc0a346c8935dc2b17218b647621 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Copyright (c) 2021 hpmicro
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

#ifndef _HPM_COMMON_H
#define _HPM_COMMON_H

#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>

/**
 *
 * @brief COMMON driver APIs
 * @defgroup common_interface COMMON driver APIs
 * @{
 *
 */

#define __R volatile const /* Define "read-only" permission */
#define __RW volatile      /* Define "read-write" permission */
#define __W volatile       /* Define "write-only" permission */

#ifndef __I
#define __I __R
#endif

#ifndef __IO
#define __IO __RW
#endif

#ifndef __O
#define __O __W
#endif

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a)   (sizeof(a) / sizeof((a)[0]))
#endif

#ifndef MAX
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif

#define HPM_BITSMASK(val,offset) ((uint32_t)(val) << (offset))
#define IS_HPM_BITMASK_SET(val,mask) (((uint32_t)(val) & (uint32_t)(mask)) != 0U)
#define IS_HPM_BIT_SET(val,offset) (((uint32_t)(val) & (1UL << (offset))) != 0U)
#define IS_HPM_BITMASK_CLR(val,mask) (((uint32_t)(val) & (uint32_t)(mask)) == 0U)
#define IS_HPM_BIT_CLR(val,offset) (((uint32_t)(val) & (1UL << (offset))) == 0U)

#define HPM_BREAK_IF(cond)   if(cond) {break;}
#define HPM_CONTINUE_IF(cond) if(cond) {continue;}

#define HPM_CHECK_RET(x)               \
    do                                 \
    {                                  \
        stat = (x);                    \
        if (status_success != stat)    \
        {                              \
            return stat;               \
        }                              \
    } while (false)

#define SIZE_1KB (1024UL)
#define SIZE_1MB (1048576UL)

typedef uint32_t hpm_stat_t;

/* @brief Enum definition for the Status group
 * Rule:
 *  [Group] 0-999 for the SoC driver and the corresponding components
 *       1000 or above for the application status group
 *  [Code]  Valid value: 0-999
 *
 * */
#define MAKE_STATUS(group,code) ((uint32_t)(group)*1000U + (uint32_t)(code))
/* @brief System status group definitions */
enum
{
    status_group_common = 0,
    status_group_uart = 1,
    status_group_i2c = 2,
    status_group_spi = 3,
    status_group_usb = 4,
    status_group_i2s = 5,
    status_group_xpi = 6,
    status_group_l1c,
    status_group_dma,
    status_group_dram,
    status_group_sdp,
    status_group_xpi_nor,
    status_group_otp,
    status_group_lcdc,
    status_group_mbx,
    status_group_rng,
    status_group_pdma,
    status_group_wdg,
    status_group_pmic_sec,
    status_group_can,
    status_group_sdxc,
    status_group_pcfg,
    status_group_clk,
    status_group_pllctl,
    status_group_pllctlv2,
    status_group_ffa,

    status_group_middleware_start = 500,
    status_group_sdmmc = status_group_middleware_start,
    status_group_audio_codec,
    status_group_dma_manager,
};

/* @brief Common status code definitions */
enum
{
    status_success = MAKE_STATUS(status_group_common, 0),
    status_fail = MAKE_STATUS(status_group_common, 1),
    status_invalid_argument = MAKE_STATUS(status_group_common, 2),
    status_timeout = MAKE_STATUS(status_group_common, 3),
};

#if defined(__GNUC__)

/* alway_inline */
#define ATTR_ALWAYS_INLINE __attribute__((always_inline))

/* weak */
#define ATTR_WEAK __attribute__((weak))

/* alignment */
#define ATTR_ALIGN(alignment) __attribute__((aligned(alignment)))

/* place var_declare at section_name, e.x. PLACE_AT(".target_section", var); */
#define ATTR_PLACE_AT(section_name) __attribute__((section(section_name)))

#define ATTR_PLACE_AT_WITH_ALIGNMENT(section_name, alignment) \
ATTR_PLACE_AT(section_name) ATTR_ALIGN(alignment)

#define ATTR_PLACE_AT_NONCACHEABLE ATTR_PLACE_AT(".noncacheable")
#define ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(alignment) \
    ATTR_PLACE_AT_NONCACHEABLE ATTR_ALIGN(alignment)

#define ATTR_PLACE_AT_NONCACHEABLE_BSS ATTR_PLACE_AT(".noncacheable.bss")
#define ATTR_PLACE_AT_NONCACHEABLE_BSS_WITH_ALIGNMENT(alignment) \
    ATTR_PLACE_AT_NONCACHEABLE_BSS ATTR_ALIGN(alignment)

/* initialize variable x with y using PLACE_AT_NONCACHEABLE_INIT(x) = {y}; */
#define ATTR_PLACE_AT_NONCACHEABLE_INIT ATTR_PLACE_AT(".noncacheable.init")
#define ATTR_PLACE_AT_NONCACHEABLE_INIT_WITH_ALIGNMENT(alignment) \
    ATTR_PLACE_AT_NONCACHEABLE_INIT ATTR_ALIGN(alignment)

#define ATTR_RAMFUNC ATTR_PLACE_AT(".fast")
#define ATTR_RAMFUNC_WITH_ALIGNMENT(alignment) \
    ATTR_RAMFUNC ATTR_ALIGN(alignment)

#else
#error Unknown toolchain
#endif

#ifdef __cplusplus
extern "C" {
#endif


/**
 * @brief   Count bits set to 1
 *
 * @param value Data to be counted
 *
 * @return number of bits set to 1
 */
static inline uint32_t count_set_bits(uint32_t value)
{
    if (value == 0) {
        return 0;
    }
    return 1 + count_set_bits(value & (value - 1));
}

/**
 * @brief   Count bits set to 1 from least significant bit
 *
 * @param value Data to be counted
 *
 * @return number of bits set to 1
 * @return 0xFFFFFFFF if no bit was set to 1
 */
static inline uint32_t get_first_set_bit_from_lsb(uint32_t value)
{
    uint32_t i = 0;
    if (!value) {
        return 0xFFFFFFFFUL;
    }
    while (value && !(value & 0x1)) {
        value >>= 1;
        i++;
    }
    return i;
}

/**
 * @brief   Count bits set to 1 from most significant bit
 *
 * @param value Data to be counted
 *
 * @return number of bits set to 1
 * @return 0xFFFFFFFF if no bit was set to 1
 */
static inline uint32_t get_first_set_bit_from_msb(uint32_t value)
{
    uint32_t i = 31;
    if (!value) {
        return 0xFFFFFFFFUL;
    }
    while (value && !(value & 0x80000000)) {
        value <<= 1;
        value &= ~1;
        i--;
    }
    return i;
}

#ifdef __cplusplus
}
#endif

/**
 * @}
 */
#endif /* _HPM_COMMON_H */