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 __BYTE_ORDER
38# define __BYTE_ORDER __LITTLE_ENDIAN
39#endif
40
41#if __ARM_ARCH == 6 || __TARGET_ARCH_6_M == 1 || __TARGET_ARCH_6S_M == 1
42# ifndef VSF_ARCH_PRI_NUM
43# define VSF_ARCH_PRI_NUM 4
44# undef VSF_ARCH_PRI_BIT
45# define VSF_ARCH_PRI_BIT 2
46# endif
47
48# ifndef VSF_ARCH_PRI_BIT
49# define VSF_ARCH_PRI_BIT 2
50# endif
51#elif __ARM_ARCH >= 7 || __TARGET_ARCH_7_M == 1 || __TARGET_ARCH_7E_M == 1
52# ifndef VSF_ARCH_PRI_NUM
53# define VSF_ARCH_PRI_NUM 16
54# undef VSF_ARCH_PRI_BIT
55# define VSF_ARCH_PRI_BIT 4
56# endif
57
58# ifndef VSF_ARCH_PRI_BIT
59# define VSF_ARCH_PRI_BIT 4
60# endif
61#endif
62
63#ifndef VSF_ARCH_CFG_CALLSTACK_TRACE
64# define VSF_ARCH_CFG_CALLSTACK_TRACE ENABLED
65#endif
66
67// software interrupt provided by arch
68#define VSF_ARCH_SWI_NUM 1
69
70#ifndef VSF_SYSTIMER_CFG_IMPL_MODE
72# define VSF_SYSTIMER_CFG_IMPL_MODE VSF_SYSTIMER_IMPL_WITH_NORMAL_TIMER
73#endif
74#define __VSF_ARCH_SYSTIMER_BITS 24
75
76/*============================ MACROFIED FUNCTIONS ===========================*/
77
78#define vsf_arch_wakeup()
79
80#if (__ARM_ARCH >= 7) || (__TARGET_ARCH_7_M == 1) || (__TARGET_ARCH_7E_M == 1)
81
82# if __ARM_ARCH == 7
83# define __STREX_FAIL 0U
84# else
85# define __STREX_FAIL 1U
86# endif
87
88# define vsf_atom32_op(__ptr, ...) \
89 ({ \
90 int32_t _; \
91 do { \
92 _ = (int32_t)__LDREXW((volatile uint32_t *)(__ptr)); \
93 } while ((__STREXW((uint32_t)(__VA_ARGS__), (volatile uint32_t *)(__ptr))) != __STREX_FAIL);\
94 _; \
95 })
96
97# define vsf_atom16_op(__ptr, ...) \
98 ({ \
99 int16_t _; \
100 do { \
101 _ = (int16_t)__LDREXH((volatile uint16_t *)(__ptr)); \
102 } while ((__STREXH((uint16_t)(__VA_ARGS__), (volatile uint16_t *)(__ptr))) != __STREX_FAIL);\
103 _; \
104 })
105
106# define vsf_atom8_op(__ptr, ...) \
107 ({ \
108 int8_t _; \
109 do { \
110 _ = (int8_t)__LDREXB((volatile uint8_t *)(__ptr)); \
111 } while ((__STREXB((uint8_t)(__VA_ARGS__), (volatile uint8_t *)(__ptr))) != __STREX_FAIL);\
112 _; \
113 })
114
115# define vsf_spinlock_t uint32_t
116# define vsf_spinlock_init(__plock) \
117 do { \
118 *(__plock) = 0; \
119 } while (0)
120# define vsf_spin_lock(__plock) \
121 do { \
122 vsf_spinlock_t VSF_MACRO_SAFE_NAME(value) = __LDREXW((volatile vsf_spinlock_t *)(__plock));\
123 if (VSF_MACRO_SAFE_NAME(value) != 0) { \
124 __WFE(); \
125 } else if ((__STREXW(1, (volatile vsf_spinlock_t *)(__plock))) == __STREX_FAIL) {\
126 break; \
127 } \
128 } while (1)
129# define vsf_spin_unlock(__plock) \
130 do { \
131 *(__plock) = 0; \
132 __SEV(); \
133 } while (0)
134# define vsf_spin_trylock(__plock) (vsf_spin_lock(__plock), 1)
135#endif
136
137/*============================ TYPES =========================================*/
138
140
141#if /*__MPU_PRESENT && */__ARM_ARCH == 7
142typedef enum vsf_arch_mpu_feature_t {
143 // compatible with MPU_RASR definitation
144 // MPU_RASR.S
145 VSF_ARCH_MPU_SHARABLE = 1ul << 18,
146 VSF_ARCH_MPU_NON_SHARABLE = 0ul << 18,
147
148 // MPU_RASR.XN
149 VSF_ARCH_MPU_EXECUTABLE = 0ul << 28,
150 VSF_ARCH_MPU_NON_EXECUTABLE = 1ul << 28,
151
152 // access permission
153 // MPU_RASR.AP
154 VSF_ARCH_MPU_ACCESS_NO = 0ul << 24,
155 VSF_ARCH_MPU_ACCESS_FULL = 3ul << 24,
156 VSF_ARCH_MPU_ACCESS_READONLY = 6ul << 24,
157
158 // MPU_RASR.C | MPU_RASR.B | MPU_RASR.TEX
159 VSF_ARCH_MPU_CACHABLE_WRITE_THROUGH_NOALLOC = (1ul << 17) | (0ul << 16) | (0ul << 19),
160 VSF_ARCH_MPU_CACHABLE_WRITE_BACK_NOALLOC = (1ul << 17) | (1ul << 16) | (0ul << 19),
161 VSF_ARCH_MPU_CACHABLE_WRITE_BACK_ALLOC = (1ul << 17) | (1ul << 16) | (1ul << 19),
162 VSF_ARCH_MPU_NON_CACHABLE = 0ul << 17,
163} vsf_arch_mpu_feature_t;
164#endif
165
166#define __VSF_ARCH_PRI_INDEX(__N, __UNUSED) \
167 __vsf_arch_prio_index_##__N = (__N),
168
169enum {
172};
173
174#define __VSF_ARCH_PRI(__N, __BIT) \
175 VSF_ARCH_PRIO_##__N = \
176 ((VSF_ARCH_PRI_NUM - 1 - __vsf_arch_prio_index_##__N)) & 0xFF, \
177 vsf_arch_prio_##__N = \
178 ((VSF_ARCH_PRI_NUM - 1 - __vsf_arch_prio_index_##__N)) & 0xFF,
179
180
181typedef enum vsf_arch_prio_t {
182 // avoid vsf_arch_prio_t to be optimized to 8bit
187
192
194
198
199#ifndef VSF_APPLET_USE_ARCH_ABI
200# define VSF_APPLET_USE_ARCH_ABI ENABLED
201#endif
202#if VSF_APPLET_USE_ARCH_ABI == ENABLED
203// functions in section 4 of "Run-Time ABI for the ARM Architecture"
204// refer to: https://github.com/ARM-software/abi-aa/releases/download/2023Q1/rtabi32.pdf
205typedef struct vsf_arch_abi_vplt_t {
207
271# ifndef __VSF_APPLET__
273# endif
274#endif
275
276/*============================ GLOBAL VARIABLES ==============================*/
277/*============================ LOCAL VARIABLES ===============================*/
278/*============================ PROTOTYPES ====================================*/
279
280static VSF_CAL_ALWAYS_INLINE void vsf_arch_set_stack(uintptr_t stack, uint32_t stack_size)
281{
282 __set_MSP(stack);
283// SPLIM MUST be set to 0 in Reset_Handler, so stack overflow check will be bypassed.
284// setjmp/longjmp may not save SPLIM register, so it should be disabled.
285//#ifdef __set_MSPLIM
286// __set_MSPLIM(stack - stack_size);
287//#endif
288}
289
290static VSF_CAL_ALWAYS_INLINE uintptr_t vsf_arch_get_stack(void)
291{
292 return __get_MSP();
293}
294
295static VSF_CAL_ALWAYS_INLINE uintptr_t vsf_arch_get_lr(void)
296{
297 register uintptr_t result;
298 __asm__ __volatile__ ("MOV %0, LR" : "=r"(result));
299 return result;
300}
301
302// __get_LR maybe not exists in CMSIS_CORE
303#ifndef __get_LR
304# define __get_LR vsf_arch_get_lr
305#endif
306
307#if VSF_ARCH_USE_THREAD_REG == ENABLED && !defined(__cplusplus)
308// for c++17, register storage class specifier is not supported
309static VSF_CAL_ALWAYS_INLINE uintptr_t vsf_arch_set_thread_reg(uintptr_t value)
310{
311 register uintptr_t result;
312 __asm__ __volatile__ ("MOV %0, r9" : "=r"(result));
313 __asm__ __volatile__ ("MOV r9, %0" : : "r"(value));
314 return result;
315}
316static VSF_CAL_ALWAYS_INLINE uintptr_t vsf_arch_get_thread_reg(void)
317{
318 register uintptr_t result;
319 __asm__ __volatile__ ("MOV %0, r9" : "=r"(result));
320 return result;
321}
322#endif
323
324#if VSF_ARCH_CFG_CALLSTACK_TRACE == ENABLED
326extern uint_fast16_t vsf_arch_get_callstack(uintptr_t sp, uintptr_t *callstack, uint_fast16_t callstack_num);
327#endif
328
329#if /*__MPU_PRESENT && */__ARM_ARCH == 7
330// if size is 0, means 4G
331extern void vsf_arch_mpu_disable(void);
332extern void vsf_arch_mpu_enable(void);
333extern void vsf_arch_mpu_config_region(uint32_t idx, uint32_t baseaddr, uint32_t size, vsf_arch_mpu_feature_t feature);
334extern void vsf_arch_mpu_clear_region(uint32_t idx);
335
336// the region added later will have higher priority
337// if size is 0, means 4G
338extern void vsf_arch_mpu_add_region(uint32_t baseaddr, uint32_t size, vsf_arch_mpu_feature_t feature);
339#endif
340
341#ifdef __cplusplus
342}
343#endif
344
345#endif
346/* EOF */
347
#define __volatile__
Definition __compiler.h:56
#define VSF_ARCH_PRI_NUM
Definition arm9_generic.h:43
vsf_arch_prio_t
Definition cortex_a_generic.h:85
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:183
@ VSF_ARCH_PRIO_ENABLE_ALL
Definition cortex_m_generic.h:188
@ vsf_arch_prio_enable_all
Definition cortex_m_generic.h:189
@ __VSF_ARCH_PRIO_LEAST_MIN
Definition cortex_m_generic.h:184
@ VSF_ARCH_PRIO_INVALID
Definition cortex_m_generic.h:185
@ vsf_arch_prio_highest
Definition cortex_m_generic.h:195
@ VSF_ARCH_PRIO_DISABLE_ALL
Definition cortex_m_generic.h:190
@ vsf_arch_prio_invalid
Definition cortex_m_generic.h:186
@ VSF_ARCH_PRIO_HIGHEST
Definition cortex_m_generic.h:196
@ vsf_arch_prio_disable_all
Definition cortex_m_generic.h:191
#define __VSF_ARCH_PRI(__N, __BIT)
Definition cortex_m_generic.h:174
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:171
uint64_t vsf_systimer_tick_t
Definition cortex_m_generic.h:139
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:166
__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:39
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:205
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:206
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:67
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@85be636