VSF Documented
cortex_m_generic.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2022 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18#ifndef __CORTEX_M_GENERIC_H__
19#define __CORTEX_M_GENERIC_H__
20
21/*============================ INCLUDES ======================================*/
22
23#include "hal/vsf_hal_cfg.h"
24
25#define __VSF_HEADER_ONLY_SHOW_ARCH_INFO__
26#include "hal/driver/driver.h"
27#undef __VSF_HEADER_ONLY_SHOW_ARCH_INFO__
28
29#include "./SysTick/systick.h"
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35/*============================ MACROS ========================================*/
36
37#ifndef __LITTLE_ENDIAN
38# define __LITTLE_ENDIAN 1
39#endif
40#ifndef __BYTE_ORDER
41# define __BYTE_ORDER __LITTLE_ENDIAN
42#endif
43
44#if __ARM_ARCH == 6 || __TARGET_ARCH_6_M == 1 || __TARGET_ARCH_6S_M == 1
45# ifndef VSF_ARCH_PRI_NUM
46# define VSF_ARCH_PRI_NUM 4
47# undef VSF_ARCH_PRI_BIT
48# define VSF_ARCH_PRI_BIT 2
49# endif
50
51# ifndef VSF_ARCH_PRI_BIT
52# define VSF_ARCH_PRI_BIT 2
53# endif
54#elif __ARM_ARCH >= 7 || __TARGET_ARCH_7_M == 1 || __TARGET_ARCH_7E_M == 1
55# ifndef VSF_ARCH_PRI_NUM
56# define VSF_ARCH_PRI_NUM 16
57# undef VSF_ARCH_PRI_BIT
58# define VSF_ARCH_PRI_BIT 4
59# endif
60
61# ifndef VSF_ARCH_PRI_BIT
62# define VSF_ARCH_PRI_BIT 4
63# endif
64#endif
65
66#ifndef VSF_ARCH_CFG_CALLSTACK_TRACE
67# define VSF_ARCH_CFG_CALLSTACK_TRACE ENABLED
68#endif
69
70// software interrupt provided by arch
71#define VSF_ARCH_SWI_NUM 1
72
73#ifndef VSF_SYSTIMER_CFG_IMPL_MODE
75# define VSF_SYSTIMER_CFG_IMPL_MODE VSF_SYSTIMER_IMPL_WITH_NORMAL_TIMER
76#endif
77#define __VSF_ARCH_SYSTIMER_BITS 24
78
79/*============================ MACROFIED FUNCTIONS ===========================*/
80
81#define vsf_arch_wakeup()
82
83#if (__ARM_ARCH >= 7) || (__TARGET_ARCH_7_M == 1) || (__TARGET_ARCH_7E_M == 1)
84
85# if __ARM_ARCH == 7
86# define __STREX_FAIL 0U
87# else
88# define __STREX_FAIL 1U
89# endif
90
91# define vsf_atom32_op(__ptr, ...) \
92 ({ \
93 int32_t _; \
94 do { \
95 _ = (int32_t)__LDREXW((volatile uint32_t *)(__ptr)); \
96 } while ((__STREXW((uint32_t)(__VA_ARGS__), (volatile uint32_t *)(__ptr))) != __STREX_FAIL);\
97 _; \
98 })
99
100# define vsf_atom16_op(__ptr, ...) \
101 ({ \
102 int16_t _; \
103 do { \
104 _ = (int16_t)__LDREXH((volatile uint16_t *)(__ptr)); \
105 } while ((__STREXH((uint16_t)(__VA_ARGS__), (volatile uint16_t *)(__ptr))) != __STREX_FAIL);\
106 _; \
107 })
108
109# define vsf_atom8_op(__ptr, ...) \
110 ({ \
111 int8_t _; \
112 do { \
113 _ = (int8_t)__LDREXB((volatile uint8_t *)(__ptr)); \
114 } while ((__STREXB((uint8_t)(__VA_ARGS__), (volatile uint8_t *)(__ptr))) != __STREX_FAIL);\
115 _; \
116 })
117
118# define vsf_spinlock_t uint32_t
119# define vsf_spinlock_init(__plock) \
120 do { \
121 *(__plock) = 0; \
122 } while (0)
123# define vsf_spin_lock(__plock) \
124 do { \
125 vsf_spinlock_t VSF_MACRO_SAFE_NAME(value) = __LDREXW((volatile vsf_spinlock_t *)(__plock));\
126 if (VSF_MACRO_SAFE_NAME(value) != 0) { \
127 __WFE(); \
128 } else if ((__STREXW(1, (volatile vsf_spinlock_t *)(__plock))) == __STREX_FAIL) {\
129 break; \
130 } \
131 } while (1)
132# define vsf_spin_unlock(__plock) \
133 do { \
134 *(__plock) = 0; \
135 __SEV(); \
136 } while (0)
137# define vsf_spin_trylock(__plock) (vsf_spin_lock(__plock), 1)
138#endif
139
140/*============================ TYPES =========================================*/
141
143
144#if /*__MPU_PRESENT && */__ARM_ARCH == 7
145typedef enum vsf_arch_mpu_feature_t {
146 // compatible with MPU_RASR definitation
147 // MPU_RASR.S
148 VSF_ARCH_MPU_SHARABLE = 1ul << 18,
149 VSF_ARCH_MPU_NON_SHARABLE = 0ul << 18,
150
151 // MPU_RASR.XN
152 VSF_ARCH_MPU_EXECUTABLE = 0ul << 28,
153 VSF_ARCH_MPU_NON_EXECUTABLE = 1ul << 28,
154
155 // access permission
156 // MPU_RASR.AP
157 VSF_ARCH_MPU_ACCESS_NO = 0ul << 24,
158 VSF_ARCH_MPU_ACCESS_FULL = 3ul << 24,
159 VSF_ARCH_MPU_ACCESS_READONLY = 6ul << 24,
160
161 // MPU_RASR.C | MPU_RASR.B | MPU_RASR.TEX
162 VSF_ARCH_MPU_CACHABLE_WRITE_THROUGH_NOALLOC = (1ul << 17) | (0ul << 16) | (0ul << 19),
163 VSF_ARCH_MPU_CACHABLE_WRITE_BACK_NOALLOC = (1ul << 17) | (1ul << 16) | (0ul << 19),
164 VSF_ARCH_MPU_CACHABLE_WRITE_BACK_ALLOC = (1ul << 17) | (1ul << 16) | (1ul << 19),
165 VSF_ARCH_MPU_NON_CACHABLE = 0ul << 17,
166} vsf_arch_mpu_feature_t;
167#endif
168
169#define __VSF_ARCH_PRI_INDEX(__N, __UNUSED) \
170 __vsf_arch_prio_index_##__N = (__N),
171
172enum {
175};
176
177#define __VSF_ARCH_PRI(__N, __BIT) \
178 VSF_ARCH_PRIO_##__N = \
179 ((VSF_ARCH_PRI_NUM - 1 - __vsf_arch_prio_index_##__N)) & 0xFF, \
180 vsf_arch_prio_##__N = \
181 ((VSF_ARCH_PRI_NUM - 1 - __vsf_arch_prio_index_##__N)) & 0xFF,
182
183
184typedef enum vsf_arch_prio_t {
185 // avoid vsf_arch_prio_t to be optimized to 8bit
190
195
197
201
202#ifndef VSF_APPLET_USE_ARCH_ABI
203# define VSF_APPLET_USE_ARCH_ABI ENABLED
204#endif
205#if VSF_APPLET_USE_ARCH_ABI == ENABLED
206// functions in section 4 of "Run-Time ABI for the ARM Architecture"
207// refer to: https://github.com/ARM-software/abi-aa/releases/download/2023Q1/rtabi32.pdf
208typedef struct vsf_arch_abi_vplt_t {
210
274# ifndef __VSF_APPLET__
276# endif
277#endif
278
279/*============================ GLOBAL VARIABLES ==============================*/
280/*============================ LOCAL VARIABLES ===============================*/
281/*============================ PROTOTYPES ====================================*/
282
283static VSF_CAL_ALWAYS_INLINE void vsf_arch_set_stack(uintptr_t stack, uint32_t stack_size)
284{
285 __set_MSP(stack);
286// SPLIM MUST be set to 0 in Reset_Handler, so stack overflow check will be bypassed.
287// setjmp/longjmp may not save SPLIM register, so it should be disabled.
288//#ifdef __set_MSPLIM
289// __set_MSPLIM(stack - stack_size);
290//#endif
291}
292
293static VSF_CAL_ALWAYS_INLINE uintptr_t vsf_arch_get_stack(void)
294{
295 return __get_MSP();
296}
297
298static VSF_CAL_ALWAYS_INLINE uintptr_t vsf_arch_get_lr(void)
299{
300 register uintptr_t result;
301 __asm__ __volatile__ ("MOV %0, LR" : "=r"(result));
302 return result;
303}
304
305#if VSF_ARCH_USE_THREAD_REG == ENABLED && !defined(__cplusplus)
306// for c++17, register storage class specifier is not supported
307static VSF_CAL_ALWAYS_INLINE uintptr_t vsf_arch_set_thread_reg(uintptr_t value)
308{
309 register uintptr_t result;
310 __asm__ __volatile__ ("MOV %0, r9" : "=r"(result));
311 __asm__ __volatile__ ("MOV r9, %0" : : "r"(value));
312 return result;
313}
314static VSF_CAL_ALWAYS_INLINE uintptr_t vsf_arch_get_thread_reg(void)
315{
316 register uintptr_t result;
317 __asm__ __volatile__ ("MOV %0, r9" : "=r"(result));
318 return result;
319}
320#endif
321
322#if VSF_ARCH_CFG_CALLSTACK_TRACE == ENABLED
324extern uint_fast16_t vsf_arch_get_callstack(uintptr_t sp, uintptr_t *callstack, uint_fast16_t callstack_num);
325#endif
326
327#if /*__MPU_PRESENT && */__ARM_ARCH == 7
328// if size is 0, means 4G
329extern void vsf_arch_mpu_disable(void);
330extern void vsf_arch_mpu_enable(void);
331extern void vsf_arch_mpu_config_region(uint32_t idx, uint32_t baseaddr, uint32_t size, vsf_arch_mpu_feature_t feature);
332extern void vsf_arch_mpu_clear_region(uint32_t idx);
333
334// the region added later will have higher priority
335// if size is 0, means 4G
336extern void vsf_arch_mpu_add_region(uint32_t baseaddr, uint32_t size, vsf_arch_mpu_feature_t feature);
337#endif
338
339#ifdef __cplusplus
340}
341#endif
342
343#endif
344/* EOF */
345
#define __volatile__
Definition __compiler.h:56
#define VSF_ARCH_PRI_NUM
Definition arm9_generic.h:46
vsf_arch_prio_t
Definition cortex_a_generic.h:88
int __aeabi_d2iz(double)
double __aeabi_l2d(long long)
long long __aeabi_llsr(long long, int)
int __aeabi_fcmpun(float, float)
long long __aeabi_f2lz(float)
int __aeabi_dcmpun(double, double)
long long __aeabi_uread8(void *)
void __aeabi_cdcmple(double, double)
double __aeabi_dmul(double, double)
unsigned long long __aeabi_d2ulz(double)
float __aeabi_fdiv(float, float)
lldiv_t __aeabi_ldivmod(long long, long long)
int __aeabi_fcmpgt(float, float)
unsigned __aeabi_f2uiz(float)
uidiv_return __aeabi_uidivmod(unsigned, unsigned)
int __aeabi_fcmplt(float, float)
float __aeabi_i2f(int)
double __aeabi_f2d(float)
unsigned __aeabi_d2uiz(double)
void __aeabi_cfrcmple(float, float)
int __aeabi_dcmpge(double, double)
long long __aeabi_llsl(long long, int)
float __aeabi_ui2f(unsigned)
float __aeabi_fadd(float, float)
int __aeabi_dcmpgt(double, double)
double __aeabi_i2d(int)
float __aeabi_ul2f(unsigned long long)
void __aeabi_cdrcmple(double, double)
double __aeabi_dadd(double, double)
int __aeabi_dcmpeq(double, double)
void __aeabi_cdcmpeq(double, double)
long long __aeabi_lmul(long long, long long)
double __aeabi_drsub(double, double)
double __aeabi_ddiv(double, double)
int __aeabi_dcmplt(double, double)
long long __aeabi_d2lz(double)
void __aeabi_cfcmple(float, float)
unsigned __aeabi_uidiv(unsigned, unsigned)
float __aeabi_fsub(float, float)
unsigned long long __aeabi_f2ulz(float)
double __aeabi_ui2d(unsigned)
int __aeabi_uread4(void *)
int __aeabi_fcmple(float, float)
double __aeabi_dsub(double, double)
float __aeabi_l2f(long long)
int __aeabi_idiv(int, int)
int __aeabi_fcmpeq(float, float)
double __aeabi_ul2d(unsigned long long)
int __aeabi_ulcmp(unsigned long long, unsigned long long)
float __aeabi_fmul(float, float)
long long __aeabi_lasr(long long, int)
int __aeabi_dcmple(double, double)
div_t __aeabi_idivmod(int, int)
float __aeabi_frsub(float, float)
int __aeabi_f2iz(float)
void __aeabi_cfcmpeq(float, float)
ulldiv_t __aeabi_uldivmod(unsigned long long, unsigned long long)
long long __aeabi_uwrite8(long long, void *)
int __aeabi_lcmp(long long, long long)
int __aeabi_uwrite4(int, void *)
int __aeabi_fcmpge(float, float)
float __aeabi_d2f(double)
@ __VSF_ARCH_PRIO_LEAST_MAX
Definition cortex_m_generic.h:186
@ VSF_ARCH_PRIO_ENABLE_ALL
Definition cortex_m_generic.h:191
@ vsf_arch_prio_enable_all
Definition cortex_m_generic.h:192
@ __VSF_ARCH_PRIO_LEAST_MIN
Definition cortex_m_generic.h:187
@ VSF_ARCH_PRIO_INVALID
Definition cortex_m_generic.h:188
@ vsf_arch_prio_highest
Definition cortex_m_generic.h:198
@ VSF_ARCH_PRIO_DISABLE_ALL
Definition cortex_m_generic.h:193
@ vsf_arch_prio_invalid
Definition cortex_m_generic.h:189
@ VSF_ARCH_PRIO_HIGHEST
Definition cortex_m_generic.h:199
@ vsf_arch_prio_disable_all
Definition cortex_m_generic.h:194
#define __VSF_ARCH_PRI(__N, __BIT)
Definition cortex_m_generic.h:177
void vsf_arch_add_text_region(vsf_arch_text_region_t *region)
Definition cortex_m_generic.c:582
@ __vsf_arch_prio_index_number
Definition cortex_m_generic.h:174
uint64_t vsf_systimer_tick_t
Definition cortex_m_generic.h:142
uint_fast16_t vsf_arch_get_callstack(uintptr_t sp, uintptr_t *callstack, uint_fast16_t callstack_num)
Definition cortex_m_generic.c:592
#define __VSF_ARCH_PRI_INDEX(__N, __UNUSED)
Definition cortex_m_generic.h:169
__VSF_VPLT_DECORATOR__ vsf_arch_abi_vplt_t vsf_arch_abi_vplt
Definition cortex_m_generic.c:734
#define VSF_ARCH_PRI_BIT
Definition mcs51_generic.h:42
uint32_t uintptr_t
Definition stdint.h:38
unsigned uint32_t
Definition stdint.h:9
unsigned long long uint64_t
Definition stdint.h:11
unsigned short uint_fast16_t
Definition stdint.h:25
#define INT16_MAX
Definition stdint.h:50
#define INT16_MIN
Definition stdint.h:45
Definition cortex_m_generic.h:208
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_ui2d)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_f2lz)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_idiv)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_i2d)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_ul2f)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_dcmpgt)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_lmul)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_l2d)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_fcmple)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_lasr)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_uwrite8)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_dmul)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_d2f)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_dadd)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_dcmplt)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_uread4)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_fcmplt)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_fcmpeq)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_dsub)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_uldivmod)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_f2d)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_fcmpgt)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_dcmpun)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_i2f)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_dcmpge)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_drsub)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_idivmod)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_uread8)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_d2lz)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_fdiv)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_llsr)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_cdcmple)
vsf_vplt_info_t info
Definition cortex_m_generic.h:209
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_fcmpge)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_ldivmod)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_d2uiz)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_f2ulz)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_ulcmp)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_ui2f)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_dcmple)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_cdcmpeq)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_cfcmple)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_ul2d)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_uidiv)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_cfrcmple)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_fsub)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_f2uiz)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_fmul)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_d2ulz)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_frsub)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_ddiv)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_cfcmpeq)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_uwrite4)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_f2iz)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_cdrcmple)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_d2iz)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_uidivmod)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_fcmpun)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_llsl)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_lcmp)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_fadd)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_dcmpeq)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(__aeabi_l2f)
Definition vsf_arch_abstraction.h:60
Definition vsf_cfg.h:95
vk_av_control_value_t value
Definition vsf_audio.h:171
#define __VSF_VPLT_DECORATOR__
Definition vsf_cfg.h:93
uint32_t size
Definition vsf_memfs.h:50
#define VSF_MREPEAT(__COUNT, __MACRO, __PARAM)
Definition vsf_repeat_macro.h:51
Generated from commit: vsfteam/vsf@b2e9e8a