]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/common/retarget.cpp
1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2015 ARM Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "FileHandle.h"
18 #include "FileSystemLike.h"
20 #include "serial_api.h"
21 #include "toolchain.h"
22 #include "semihost_api.h"
23 #include "mbed_interface.h"
24 #if DEVICE_STDIO_MESSAGES
29 #if defined(__ARMCC_VERSION)
31 # define PREFIX(x) _sys##x
32 # define OPEN_MAX _SYS_OPEN
34 # pragma import(__use_full_stdio)
37 #elif defined(__ICCARM__)
39 # define PREFIX(x) _##x
42 # define STDIN_FILENO 0
43 # define STDOUT_FILENO 1
44 # define STDERR_FILENO 2
47 # include <sys/stat.h>
48 # include <sys/unistd.h>
49 # include <sys/syslimits.h>
55 #if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
56 // Before version 5.03, we were using a patched version of microlib with proper names
57 extern const char __stdin_name
[] = ":tt";
58 extern const char __stdout_name
[] = ":tt";
59 extern const char __stderr_name
[] = ":tt";
62 extern const char __stdin_name
[] = "/stdin";
63 extern const char __stdout_name
[] = "/stdout";
64 extern const char __stderr_name
[] = "/stderr";
67 /* newlib has the filehandle field in the FILE struct as a short, so
68 * we can't just return a Filehandle* from _open and instead have to
69 * put it in a filehandles array and return the index into that array
70 * (or rather index+3, as filehandles 0-2 are stdin/out/err).
72 static FileHandle
*filehandles
[OPEN_MAX
];
74 FileHandle::~FileHandle() {
75 /* Remove all open filehandles for this */
76 for (unsigned int fh_i
= 0; fh_i
< sizeof(filehandles
)/sizeof(*filehandles
); fh_i
++) {
77 if (filehandles
[fh_i
] == this) {
78 filehandles
[fh_i
] = NULL
;
84 extern int stdio_uart_inited
;
85 extern serial_t stdio_uart
;
88 static void init_serial() {
90 if (stdio_uart_inited
) return;
91 serial_init(&stdio_uart
, STDIO_UART_TX
, STDIO_UART_RX
);
95 static inline int openmode_to_posix(int openmode
) {
97 #ifdef __ARMCC_VERSION
98 if (openmode
& OPEN_PLUS
) {
100 } else if(openmode
& OPEN_W
) {
102 } else if(openmode
& OPEN_A
) {
103 posix
= O_WRONLY
|O_APPEND
;
107 /* a, w, a+, w+ all create if file does not already exist */
108 if (openmode
& (OPEN_A
|OPEN_W
)) {
111 /* w and w+ truncate */
112 if (openmode
& OPEN_W
) {
115 #elif defined(__ICCARM__)
116 switch (openmode
& _LLIO_RDWRMASK
) {
117 case _LLIO_RDONLY
: posix
= O_RDONLY
; break;
118 case _LLIO_WRONLY
: posix
= O_WRONLY
; break;
119 case _LLIO_RDWR
: posix
= O_RDWR
; break;
121 if (openmode
& _LLIO_CREAT
) posix
|= O_CREAT
;
122 if (openmode
& _LLIO_APPEND
) posix
|= O_APPEND
;
123 if (openmode
& _LLIO_TRUNC
) posix
|= O_TRUNC
;
128 extern "C" FILEHANDLE
PREFIX(_open
)(const char* name
, int openmode
) {
129 #if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
130 // Before version 5.03, we were using a patched version of microlib with proper names
131 // This is the workaround that the microlib author suggested us
133 if (!std::strcmp(name
, ":tt")) return n
++;
136 /* Use the posix convention that stdin,out,err are filehandles 0,1,2.
138 if (std::strcmp(name
, __stdin_name
) == 0) {
141 } else if (std::strcmp(name
, __stdout_name
) == 0) {
144 } else if (std::strcmp(name
, __stderr_name
) == 0) {
150 // find the first empty slot in filehandles
152 for (fh_i
= 0; fh_i
< sizeof(filehandles
)/sizeof(*filehandles
); fh_i
++) {
153 if (filehandles
[fh_i
] == NULL
) break;
155 if (fh_i
>= sizeof(filehandles
)/sizeof(*filehandles
)) {
161 /* FILENAME: ":0x12345678" describes a FileLike* */
162 if (name
[0] == ':') {
164 sscanf(name
, ":%p", &p
);
165 res
= (FileHandle
*)p
;
167 /* FILENAME: "/file_system/file_name" */
173 else if (path
.isFile()) {
176 FileSystemLike
*fs
= path
.fileSystem();
177 if (fs
== NULL
) return -1;
178 int posix_mode
= openmode_to_posix(openmode
);
179 res
= fs
->open(path
.fileName(), posix_mode
); /* NULL if fails */
183 if (res
== NULL
) return -1;
184 filehandles
[fh_i
] = res
;
186 return fh_i
+ 3; // +3 as filehandles 0-2 are stdin/out/err
189 extern "C" int PREFIX(_close
)(FILEHANDLE fh
) {
190 if (fh
< 3) return 0;
192 FileHandle
* fhc
= filehandles
[fh
-3];
193 filehandles
[fh
-3] = NULL
;
194 if (fhc
== NULL
) return -1;
199 #if defined(__ICCARM__)
200 extern "C" size_t __write (int fh
, const unsigned char *buffer
, size_t length
) {
202 extern "C" int PREFIX(_write
)(FILEHANDLE fh
, const unsigned char *buffer
, unsigned int length
, int mode
) {
204 int n
; // n is the number of bytes written
207 if (!stdio_uart_inited
) init_serial();
208 for (unsigned int i
= 0; i
< length
; i
++) {
209 serial_putc(&stdio_uart
, buffer
[i
]);
214 FileHandle
* fhc
= filehandles
[fh
-3];
215 if (fhc
== NULL
) return -1;
217 n
= fhc
->write(buffer
, length
);
219 #ifdef __ARMCC_VERSION
226 #if defined(__ICCARM__)
227 extern "C" size_t __read (int fh
, unsigned char *buffer
, size_t length
) {
229 extern "C" int PREFIX(_read
)(FILEHANDLE fh
, unsigned char *buffer
, unsigned int length
, int mode
) {
231 int n
; // n is the number of bytes read
233 // only read a character at a time from stdin
235 if (!stdio_uart_inited
) init_serial();
236 *buffer
= serial_getc(&stdio_uart
);
240 FileHandle
* fhc
= filehandles
[fh
-3];
241 if (fhc
== NULL
) return -1;
243 n
= fhc
->read(buffer
, length
);
245 #ifdef __ARMCC_VERSION
252 #ifdef __ARMCC_VERSION
253 extern "C" int PREFIX(_istty
)(FILEHANDLE fh
)
255 extern "C" int _isatty(FILEHANDLE fh
)
258 /* stdin, stdout and stderr should be tty */
259 if (fh
< 3) return 1;
261 FileHandle
* fhc
= filehandles
[fh
-3];
262 if (fhc
== NULL
) return -1;
264 return fhc
->isatty();
268 #if defined(__ARMCC_VERSION)
269 int _sys_seek(FILEHANDLE fh
, long position
)
270 #elif defined(__ICCARM__)
271 long __lseek(int fh
, long offset
, int whence
)
273 int _lseek(FILEHANDLE fh
, int offset
, int whence
)
276 if (fh
< 3) return 0;
278 FileHandle
* fhc
= filehandles
[fh
-3];
279 if (fhc
== NULL
) return -1;
281 #if defined(__ARMCC_VERSION)
282 return fhc
->lseek(position
, SEEK_SET
);
284 return fhc
->lseek(offset
, whence
);
288 #ifdef __ARMCC_VERSION
289 extern "C" int PREFIX(_ensure
)(FILEHANDLE fh
) {
290 if (fh
< 3) return 0;
292 FileHandle
* fhc
= filehandles
[fh
-3];
293 if (fhc
== NULL
) return -1;
298 extern "C" long PREFIX(_flen
)(FILEHANDLE fh
) {
299 if (fh
< 3) return 0;
301 FileHandle
* fhc
= filehandles
[fh
-3];
302 if (fhc
== NULL
) return -1;
309 #if !defined(__ARMCC_VERSION) && !defined(__ICCARM__)
310 extern "C" int _fstat(int fd
, struct stat
*st
) {
311 if ((STDOUT_FILENO
== fd
) || (STDERR_FILENO
== fd
) || (STDIN_FILENO
== fd
)) {
312 st
->st_mode
= S_IFCHR
;
322 extern "C" int remove(const char *path
) {
324 FileSystemLike
*fs
= fp
.fileSystem();
325 if (fs
== NULL
) return -1;
327 return fs
->remove(fp
.fileName());
330 extern "C" int rename(const char *oldname
, const char *newname
) {
331 FilePath
fpOld(oldname
);
332 FilePath
fpNew(newname
);
333 FileSystemLike
*fsOld
= fpOld
.fileSystem();
334 FileSystemLike
*fsNew
= fpNew
.fileSystem();
336 /* rename only if both files are on the same FS */
337 if (fsOld
!= fsNew
|| fsOld
== NULL
) return -1;
339 return fsOld
->rename(fpOld
.fileName(), fpNew
.fileName());
342 extern "C" char *tmpnam(char *s
) {
346 extern "C" FILE *tmpfile() {
351 #ifdef __ARMCC_VERSION
352 extern "C" char *_sys_command_string(char *cmd
, int len
) {
357 extern "C" DIR *opendir(const char *path
) {
358 /* root dir is FileSystemLike */
359 if (path
[0] == '/' && path
[1] == 0) {
360 return FileSystemLike::opendir();
364 FileSystemLike
* fs
= fp
.fileSystem();
365 if (fs
== NULL
) return NULL
;
367 return fs
->opendir(fp
.fileName());
370 extern "C" struct dirent
*readdir(DIR *dir
) {
371 return dir
->readdir();
374 extern "C" int closedir(DIR *dir
) {
375 return dir
->closedir();
378 extern "C" void rewinddir(DIR *dir
) {
382 extern "C" off_t
telldir(DIR *dir
) {
383 return dir
->telldir();
386 extern "C" void seekdir(DIR *dir
, off_t off
) {
390 extern "C" int mkdir(const char *path
, mode_t mode
) {
392 FileSystemLike
*fs
= fp
.fileSystem();
393 if (fs
== NULL
) return -1;
395 return fs
->mkdir(fp
.fileName(), mode
);
398 #if defined(TOOLCHAIN_GCC)
399 /* prevents the exception handling name demangling code getting pulled in */
400 #include "mbed_error.h"
401 namespace __gnu_cxx
{
402 void __verbose_terminate_handler() {
406 extern "C" WEAK
void __cxa_pure_virtual(void);
407 extern "C" WEAK
void __cxa_pure_virtual(void) {
413 // ****************************************************************************
414 // mbed_main is a function that is called before main()
415 // mbed_sdk_init() is also a function that is called before main(), but unlike
416 // mbed_main(), it is not meant for user code, but for the SDK itself to perform
417 // initializations before main() is called.
419 extern "C" WEAK
void mbed_main(void);
420 extern "C" WEAK
void mbed_main(void) {
423 extern "C" WEAK
void mbed_sdk_init(void);
424 extern "C" WEAK
void mbed_sdk_init(void) {
427 #if defined(TOOLCHAIN_ARM)
428 extern "C" int $Super$$
main(void);
430 extern "C" int $Sub$$
main(void) {
433 return $Super$$
main();
435 #elif defined(TOOLCHAIN_GCC)
436 extern "C" int __real_main(void);
438 extern "C" int __wrap_main(void) {
441 return __real_main();
443 #elif defined(TOOLCHAIN_IAR)
444 // IAR doesn't have the $Super/$Sub mechanism of armcc, nor something equivalent
445 // to ld's --wrap. It does have a --redirect, but that doesn't help, since redirecting
446 // 'main' to another symbol looses the original 'main' symbol. However, its startup
447 // code will call a function to setup argc and argv (__iar_argc_argv) if it is defined.
448 // Since mbed doesn't use argc/argv, we use this function to call our mbed_main.
449 extern "C" void __iar_argc_argv() {
455 // Provide implementation of _sbrk (low-level dynamic memory allocation
456 // routine) for GCC_ARM which compares new heap pointer with MSP instead of
457 // SP. This make it compatible with RTX RTOS thread stacks.
458 #if defined(TOOLCHAIN_GCC_ARM)
459 // Linker defined symbol used by _sbrk to indicate where heap should start.
460 extern "C" int __end__
;
462 #if defined(TARGET_CORTEX_A)
463 extern "C" uint32_t __HeapLimit
;
466 // Turn off the errno macro and use actual global variable instead.
468 extern "C" int errno
;
471 register unsigned char * stack_ptr
__asm ("sp");
473 // Dynamic memory allocation related syscall.
474 extern "C" caddr_t
_sbrk(int incr
) {
475 static unsigned char* heap
= (unsigned char*)&__end__
;
476 unsigned char* prev_heap
= heap
;
477 unsigned char* new_heap
= heap
+ incr
;
479 #if defined(TARGET_ARM7)
480 if (new_heap
>= stack_ptr
) {
481 #elif defined(TARGET_CORTEX_A)
482 if (new_heap
>= (unsigned char*)&__HeapLimit
) { /* __HeapLimit is end of heap section */
484 if (new_heap
>= (unsigned char*)__get_MSP()) {
491 return (caddr_t
) prev_heap
;
496 #ifdef TOOLCHAIN_GCC_CW
497 // TODO: Ideally, we would like to define directly "_ExitProcess"
498 extern "C" void mbed_exit(int return_code
) {
499 #elif defined TOOLCHAIN_GCC_ARM
500 extern "C" void _exit(int return_code
) {
503 extern "C" void exit(int return_code
) {
506 #if DEVICE_STDIO_MESSAGES
512 if (mbed_interface_connected()) {
523 #if !defined(TOOLCHAIN_GCC_ARM) && !defined(TOOLCHAIN_GCC_CW)
530 void mbed_set_unbuffered_stream(FILE *_file
) {
531 #if defined (__ICCARM__)
533 std::setvbuf(_file
,buf
,_IONBF
,NULL
);
539 int mbed_getc(FILE *_file
){
540 #if defined (__ICCARM__)
541 /*This is only valid for unbuffered streams*/
542 int res
= std::fgetc(_file
);
544 _file
->_Mode
= (unsigned short)(_file
->_Mode
& ~ 0x1000);/* Unset read mode */
545 _file
->_Rend
= _file
->_Wend
;
546 _file
->_Next
= _file
->_Wend
;
550 return std::fgetc(_file
);
554 char* mbed_gets(char*s
, int size
, FILE *_file
){
555 #if defined (__ICCARM__)
556 /*This is only valid for unbuffered streams*/
557 char *str
= fgets(s
,size
,_file
);
559 _file
->_Mode
= (unsigned short)(_file
->_Mode
& ~ 0x1000);/* Unset read mode */
560 _file
->_Rend
= _file
->_Wend
;
561 _file
->_Next
= _file
->_Wend
;
565 return std::fgets(s
,size
,_file
);