3030#include " platform/mbed_critical.h"
3131#include " platform/mbed_poll.h"
3232#include " drivers/UARTSerial.h"
33+ #include " drivers/RawSerial.h"
3334#include " hal/us_ticker_api.h"
3435#include " hal/lp_ticker_api.h"
3536#include < stdlib.h>
@@ -104,6 +105,7 @@ extern const char __stderr_name[] = "/stderr";
104105unsigned char *mbed_heap_start = 0 ;
105106uint32_t mbed_heap_size = 0 ;
106107
108+ #if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
107109/* newlib has the filehandle field in the FILE struct as a short, so
108110 * we can't just return a Filehandle* from _open and instead have to
109111 * put it in a filehandles array and return the index into that array
@@ -112,12 +114,14 @@ static FileHandle *filehandles[OPEN_MAX] = { FILE_HANDLE_RESERVED, FILE_HANDLE_R
112114static char stdio_in_prev[OPEN_MAX];
113115static char stdio_out_prev[OPEN_MAX];
114116static SingletonPtr<PlatformMutex> filehandle_mutex;
117+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
115118
116119namespace mbed {
117120void mbed_set_unbuffered_stream (std::FILE *_file);
118121
119122void remove_filehandle (FileHandle *file)
120123{
124+ #if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
121125 filehandle_mutex->lock ();
122126 /* Remove all open filehandles for this */
123127 for (unsigned int fh_i = 0 ; fh_i < sizeof (filehandles) / sizeof (*filehandles); fh_i++) {
@@ -126,9 +130,11 @@ void remove_filehandle(FileHandle *file)
126130 }
127131 }
128132 filehandle_mutex->unlock ();
133+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
129134}
130135}
131136
137+ #if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
132138#if DEVICE_SERIAL
133139extern int stdio_uart_inited;
134140extern serial_t stdio_uart;
@@ -391,9 +397,11 @@ static int reserve_filehandle()
391397
392398 return fh_i;
393399}
400+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
394401
395402int mbed::bind_to_fd (FileHandle *fh)
396403{
404+ #if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
397405 int fildes = reserve_filehandle ();
398406 if (fildes < 0 ) {
399407 return fildes;
@@ -404,19 +412,27 @@ int mbed::bind_to_fd(FileHandle *fh)
404412 stdio_out_prev[fildes] = 0 ;
405413
406414 return fildes;
415+ #else
416+ return -1 ;
417+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
407418}
408419
409420static int unbind_from_fd (int fd, FileHandle *fh)
410421{
422+ #if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
411423 if (filehandles[fd] == fh) {
412424 filehandles[fd] = NULL ;
413425 return 0 ;
414426 } else {
415427 errno = EBADF;
416428 return -1 ;
417429 }
430+ #else
431+ return -1 ;
432+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
418433}
419434
435+
420436#ifndef __IAR_SYSTEMS_ICC__
421437/* IAR provides fdopen itself */
422438extern " C" std::FILE *fdopen (int fildes, const char *mode)
@@ -456,6 +472,40 @@ std::FILE *fdopen(FileHandle *fh, const char *mode)
456472}
457473}
458474
475+ #if MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY
476+ /* * Write the contents of a buffer to a serial interface
477+ *
478+ *
479+ * * if blocking, block until all data is written
480+ * * if no data can be written, and nonblocking set, return -EAGAIN
481+ * * if some data can be written, and nonblocking set, write partial
482+ *
483+ * @param buffer The buffer to write from
484+ * @param length The number of bytes to read
485+ * @return The number of bytes written, negative error on failure
486+ */
487+ static ssize_t mbed_serial_write (const void *buffer, size_t size)
488+ {
489+ ssize_t ret = 0 ;
490+ const unsigned char *buf = static_cast <const unsigned char *>(buffer);
491+ static RawSerial console (
492+ STDIO_UART_TX,
493+ STDIO_UART_RX,
494+ MBED_CONF_PLATFORM_STDIO_BAUD_RATE
495+ );
496+
497+ for (size_t i = 0 ; i < size; i++) {
498+ ret = console.putc (buf[i]);
499+ if (ret) {
500+ break ;
501+ }
502+ }
503+
504+ return ret;
505+ }
506+ #endif // MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY
507+
508+
459509/* @brief standard c library fopen() retargeting function.
460510 *
461511 * This function is invoked by the standard c library retargeting to handle fopen()
@@ -469,6 +519,7 @@ std::FILE *fdopen(FileHandle *fh, const char *mode)
469519 * */
470520extern " C" FILEHANDLE PREFIX (_open)(const char *name, int openflags)
471521{
522+ #if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
472523#if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
473524#if !defined(MBED_CONF_RTOS_PRESENT)
474525 // valid only for mbed 2
@@ -516,8 +567,13 @@ extern "C" FILEHANDLE PREFIX(_open)(const char *name, int openflags)
516567 }
517568#endif
518569 return open (name, openflags_to_posix (openflags));
570+ #else
571+ // Everything goes to the same output
572+ return 1 ;
573+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
519574}
520575
576+ #if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
521577extern " C" int open (const char *name, int oflag, ...)
522578{
523579 int fildes = reserve_filehandle ();
@@ -554,12 +610,18 @@ extern "C" int open(const char *name, int oflag, ...)
554610
555611 return fildes;
556612}
613+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
557614
558615extern " C" int PREFIX (_close)(FILEHANDLE fh)
559616{
617+ #if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
560618 return close (fh);
619+ #else
620+ return 0 ;
621+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
561622}
562623
624+ #if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
563625extern " C" int close (int fildes)
564626{
565627 FileHandle *fhc = mbed_file_handle (fildes);
@@ -588,6 +650,7 @@ static bool convert_crlf(int fd)
588650 return false ;
589651#endif
590652}
653+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
591654
592655#if defined(__ICCARM__)
593656extern " C" size_t __write (int fh, const unsigned char *buffer, size_t length)
@@ -603,6 +666,7 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign
603666 }
604667#endif
605668
669+ #if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
606670 if (length > SSIZE_MAX) {
607671 errno = EINVAL;
608672 return -1 ;
@@ -669,11 +733,15 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign
669733#else
670734 return written;
671735#endif
736+
737+ #else
738+ return mbed_serial_write (buffer, length);
739+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
672740}
673741
674742extern " C" ssize_t write (int fildes, const void *buf, size_t length)
675743{
676-
744+ # if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
677745 FileHandle *fhc = mbed_file_handle (fildes);
678746 if (fhc == NULL ) {
679747 errno = EBADF;
@@ -687,6 +755,9 @@ extern "C" ssize_t write(int fildes, const void *buf, size_t length)
687755 } else {
688756 return ret;
689757 }
758+ #else
759+ return mbed_serial_write (buf, length);
760+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
690761}
691762
692763#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
@@ -698,7 +769,11 @@ extern "C" void PREFIX(_exit)(int return_code)
698769extern " C" void _ttywrch (int ch)
699770{
700771 char c = ch;
772+ #if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
701773 write (STDOUT_FILENO, &c, 1 );
774+ #else
775+ mbed_serial_write (&c, 1 );
776+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
702777}
703778#endif
704779
@@ -710,6 +785,7 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int
710785{
711786#endif
712787
788+ #if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
713789#if defined(MBED_TRAP_ERRORS_ENABLED) && MBED_TRAP_ERRORS_ENABLED && defined(MBED_CONF_RTOS_PRESENT)
714790 if (core_util_is_isr_active () || !core_util_are_interrupts_enabled ()) {
715791 MBED_ERROR1 (MBED_MAKE_ERROR (MBED_MODULE_PLATFORM, MBED_ERROR_CODE_PROHIBITED_IN_ISR_CONTEXT), " Error - reading from a file in an ISR or critical section\r\n " , fh);
@@ -764,8 +840,13 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int
764840#else
765841 return bytes_read;
766842#endif
843+ #else
844+ // Not supported
845+ return -1 ;
846+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
767847}
768848
849+ #if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
769850extern " C" ssize_t read (int fildes, void *buf, size_t length)
770851{
771852 FileHandle *fhc = mbed_file_handle (fildes);
@@ -782,7 +863,7 @@ extern "C" ssize_t read(int fildes, void *buf, size_t length)
782863 return ret;
783864 }
784865}
785-
866+ # endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
786867
787868#ifdef __ARMCC_VERSION
788869extern " C" int PREFIX (_istty)(FILEHANDLE fh)
@@ -795,6 +876,7 @@ extern "C" int _isatty(FILEHANDLE fh)
795876
796877extern " C" int isatty (int fildes)
797878{
879+ #if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
798880 FileHandle *fhc = mbed_file_handle (fildes);
799881 if (fhc == NULL ) {
800882 errno = EBADF;
@@ -808,6 +890,10 @@ extern "C" int isatty(int fildes)
808890 } else {
809891 return tty;
810892 }
893+ #else
894+ // Is not attached to an interactive device
895+ return 0 ;
896+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
811897}
812898
813899extern " C"
@@ -819,6 +905,7 @@ long __lseek(int fh, long offset, int whence)
819905int _lseek (FILEHANDLE fh, int offset, int whence)
820906#endif
821907{
908+ #if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
822909#if defined(__ARMCC_VERSION)
823910 int whence = SEEK_SET;
824911#endif
@@ -832,8 +919,13 @@ int _lseek(FILEHANDLE fh, int offset, int whence)
832919 return -1 ;
833920 }
834921 return off;
922+ #else
923+ // Not supported
924+ return -1 ;
925+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
835926}
836927
928+ #if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
837929extern " C" off_t lseek (int fildes, off_t offset, int whence)
838930{
839931 FileHandle *fhc = mbed_file_handle (fildes);
@@ -873,9 +965,11 @@ extern "C" int PREFIX(_ensure)(FILEHANDLE fh)
873965 return fsync (fh);
874966}
875967#endif
968+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
876969
877970extern " C" int fsync (int fildes)
878971{
972+ #if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
879973 FileHandle *fhc = mbed_file_handle (fildes);
880974 if (fhc == NULL ) {
881975 errno = EBADF;
@@ -889,11 +983,15 @@ extern "C" int fsync(int fildes)
889983 } else {
890984 return 0 ;
891985 }
986+ #else
987+ return -1 ;
988+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
892989}
893990
894991#ifdef __ARMCC_VERSION
895992extern " C" long PREFIX (_flen)(FILEHANDLE fh)
896993{
994+ #if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
897995 FileHandle *fhc = mbed_file_handle (fh);
898996 if (fhc == NULL ) {
899997 errno = EBADF;
@@ -910,6 +1008,10 @@ extern "C" long PREFIX(_flen)(FILEHANDLE fh)
9101008 return -1 ;
9111009 }
9121010 return size;
1011+ #else
1012+ // Not supported
1013+ return -1 ;
1014+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
9131015}
9141016
9151017// Do not compile this code for TFM secure target
@@ -983,7 +1085,7 @@ extern "C" __value_in_regs struct __initial_stackheap __user_setup_stackheap(uin
9831085
9841086#endif
9851087
986-
1088+ # if !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
9871089#if !defined(__ARMCC_VERSION) && !defined(__ICCARM__)
9881090extern " C" int _fstat (int fh, struct stat *st)
9891091{
@@ -1060,6 +1162,7 @@ extern "C" int poll(struct pollfd fds[], nfds_t nfds, int timeout)
10601162 }
10611163 return ret;
10621164}
1165+ #endif // !(MBED_CONF_PLATFORM_STDIO_CONSOLE_ONLY)
10631166
10641167namespace std {
10651168extern " C" int remove (const char *path)
0 commit comments