Update to new build system.
[clinton/Smoothieware.git] / build / mbed_custom.c
1 /* Copyright 2011 Adam Green (http://mbed.org/users/AdamGreen/)
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 */
15 /* Provide routines which hook the MRI debug monitor into GCC4MBED projects. */
16 #include <string.h>
17 #include <sys/types.h>
18 #include <errno.h>
19 #include <mri.h>
20 #include <cmsis.h>
21
22 extern unsigned int __bss_start__;
23 extern unsigned int __bss_end__;
24 extern unsigned int __end__;
25
26 int main(void);
27 void __libc_init_array(void);
28 void software_init_hook(void) __attribute((weak));
29 void exit(int ErrorCode);
30 void _start(void)
31 {
32 int bssSize = (int)&__bss_end__ - (int)&__bss_start__;
33 int mainReturnValue;
34
35 memset(&__bss_start__, 0, bssSize);
36
37 if (MRI_ENABLE)
38 {
39 __mriInit(MRI_INIT_PARAMETERS);
40 if (MRI_BREAK_ON_INIT)
41 __debugbreak();
42 }
43
44 software_init_hook();
45 __libc_init_array();
46 mainReturnValue = main();
47 exit(mainReturnValue);
48 }
49
50
51 int __real__read(int file, char *ptr, int len);
52 int __wrap__read(int file, char *ptr, int len)
53 {
54 if (MRI_SEMIHOST_STDIO && file < 3)
55 return __mriNewlib_SemihostRead(file, ptr, len);
56 return __real__read(file, ptr, len);
57 }
58
59
60 int __real__write(int file, char *ptr, int len);
61 int __wrap__write(int file, char *ptr, int len)
62 {
63 if (MRI_SEMIHOST_STDIO && file < 3)
64 return __mriNewlib_SemihostWrite(file, ptr, len);
65 return __real__write(file, ptr, len);
66 }
67
68
69 int __real__isatty(int file);
70 int __wrap__isatty(int file)
71 {
72 /* Hardcoding the stdin/stdout/stderr handles to be interactive tty devices, unlike mbed.ar */
73 if (file < 3)
74 return 1;
75 return __real__isatty(file);
76 }
77
78
79 int __wrap_semihost_connected(void)
80 {
81 /* MRI makes it look like there is no mbed interface attached since it disables the JTAG portion but MRI does
82 support some of the mbed semihost calls when it is running so force it to return -1, indicating that the
83 interface is attached. */
84 return -1;
85 }
86
87
88
89 void abort(void)
90 {
91 if (MRI_ENABLE)
92 __debugbreak();
93
94 exit(1);
95 }
96
97
98 void __cxa_pure_virtual(void)
99 {
100 abort();
101 }
102
103 /* Trap calls to malloc/free/realloc in ISR. */
104 void __malloc_lock(void)
105 {
106 if (__get_IPSR() != 0)
107 __debugbreak();
108 }
109
110 void __malloc_unlock(void)
111 {
112 }
113
114
115 /* Turn off the errno macro and use actual external global variable instead. */
116 #undef errno
117 extern int errno;
118
119 /* Dynamic memory allocation related syscalls. */
120 caddr_t _sbrk(int incr)
121 {
122 static unsigned char* heap = (unsigned char*)&__end__;
123 unsigned char* prev_heap = heap;
124 unsigned char* new_heap = heap + incr;
125
126 if (new_heap >= (unsigned char*)__get_MSP()) {
127 errno = ENOMEM;
128 return (caddr_t)-1;
129 }
130
131 heap = new_heap;
132 return (caddr_t) prev_heap;
133 }