]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MPU_M23_Nuvoton_NuMaker_PFM_M2351_IAR_GCC/Nuvoton_Code/Device/Nuvoton/M2351/Source/GCC/semihosting.h
Add Cortex M23 GCC and IAR ports. Add demo projects for Nuvoton NuMaker-PFM-2351.
[freertos] / FreeRTOS / Demo / CORTEX_MPU_M23_Nuvoton_NuMaker_PFM_M2351_IAR_GCC / Nuvoton_Code / Device / Nuvoton / M2351 / Source / GCC / semihosting.h
1
2 #ifndef ARM_SEMIHOSTING_H_
3 #define ARM_SEMIHOSTING_H_
4
5 // ----------------------------------------------------------------------------
6
7 // Semihosting operations.
8 enum OperationNumber
9 {
10     // Regular operations
11     SEMIHOSTING_EnterSVC = 0x17,
12     SEMIHOSTING_ReportException = 0x18,
13     SEMIHOSTING_SYS_CLOSE = 0x02,
14     SEMIHOSTING_SYS_CLOCK = 0x10,
15     SEMIHOSTING_SYS_ELAPSED = 0x30,
16     SEMIHOSTING_SYS_ERRNO = 0x13,
17     SEMIHOSTING_SYS_FLEN = 0x0C,
18     SEMIHOSTING_SYS_GET_CMDLINE = 0x15,
19     SEMIHOSTING_SYS_HEAPINFO = 0x16,
20     SEMIHOSTING_SYS_ISERROR = 0x08,
21     SEMIHOSTING_SYS_ISTTY = 0x09,
22     SEMIHOSTING_SYS_OPEN = 0x01,
23     SEMIHOSTING_SYS_READ = 0x06,
24     SEMIHOSTING_SYS_READC = 0x07,
25     SEMIHOSTING_SYS_REMOVE = 0x0E,
26     SEMIHOSTING_SYS_RENAME = 0x0F,
27     SEMIHOSTING_SYS_SEEK = 0x0A,
28     SEMIHOSTING_SYS_SYSTEM = 0x12,
29     SEMIHOSTING_SYS_TICKFREQ = 0x31,
30     SEMIHOSTING_SYS_TIME = 0x11,
31     SEMIHOSTING_SYS_TMPNAM = 0x0D,
32     SEMIHOSTING_SYS_WRITE = 0x05,
33     SEMIHOSTING_SYS_WRITEC = 0x03,
34     SEMIHOSTING_SYS_WRITE0 = 0x04,
35
36     // Codes returned by SEMIHOSTING_ReportException
37     ADP_Stopped_ApplicationExit = ((2 << 16) + 38),
38     ADP_Stopped_RunTimeError = ((2 << 16) + 35),
39
40 };
41
42 // ----------------------------------------------------------------------------
43
44 // SWI numbers and reason codes for RDI (Angel) monitors.
45 #define AngelSWI_ARM                    0x123456
46 #ifdef __thumb__
47 #define AngelSWI                        0xAB
48 #else
49 #define AngelSWI                        AngelSWI_ARM
50 #endif
51 // For thumb only architectures use the BKPT instruction instead of SWI.
52 #if defined(__ARM_ARCH_7M__)     \
53     || defined(__ARM_ARCH_7EM__) \
54     || defined(__ARM_ARCH_6M__) \
55         || defined(__ARM_ARCH_8M_BASE__)
56 #define AngelSWIInsn                    "bkpt"
57 #define AngelSWIAsm                     bkpt
58 #else
59 #define AngelSWIInsn                    "swi"
60 #define AngelSWIAsm                     swi
61 #endif
62
63 #if defined(OS_DEBUG_SEMIHOSTING_FAULTS)
64 // Testing the local semihosting handler cannot use another BKPT, since this
65 // configuration cannot trigger HaedFault exceptions while the debugger is
66 // connected, so we use an illegal op code, that will trigger an
67 // UsageFault exception.
68 #define AngelSWITestFault       "setend be"
69 #define AngelSWITestFaultOpCode (0xB658)
70 #endif
71
72 static inline int
73 __attribute__ ((always_inline))
74 call_host (int reason, void* arg)
75 {
76     int value;
77     asm volatile (
78
79         " mov r0, %[rsn]  \n"
80         " mov r1, %[arg]  \n"
81 #if defined(OS_DEBUG_SEMIHOSTING_FAULTS)
82         " " AngelSWITestFault " \n"
83 #else
84         " " AngelSWIInsn " %[swi] \n"
85 #endif
86         " mov %[val], r0"
87
88         : [val] "=r" (value) /* Outputs */
89         : [rsn] "r" (reason), [arg] "r" (arg), [swi] "i" (AngelSWI) /* Inputs */
90         : "r0", "r1", "r2", "r3", "ip", "lr", "memory", "cc"
91         // Clobbers r0 and r1, and lr if in supervisor mode
92     );
93
94     // Accordingly to page 13-77 of ARM DUI 0040D other registers
95     // can also be clobbered. Some memory positions may also be
96     // changed by a system call, so they should not be kept in
97     // registers. Note: we are assuming the manual is right and
98     // Angel is respecting the APCS.
99     return value;
100 }
101
102 // ----------------------------------------------------------------------------
103
104 // Function used in _exit() to return the status code as Angel exception.
105 static inline void
106 __attribute__ ((always_inline,noreturn))
107 report_exception (int reason)
108 {
109     call_host (SEMIHOSTING_ReportException, (void*) reason);
110
111     for (;;)
112         ;
113 }
114
115 // ----------------------------------------------------------------------------
116
117 #endif // ARM_SEMIHOSTING_H_