mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Revision:
186:707f6e361f3e
Parent:
184:08ed48f1de7f
Child:
187:0387e8f68319
--- a/platform/mbed_retarget.cpp	Thu Apr 19 17:12:19 2018 +0100
+++ b/platform/mbed_retarget.cpp	Fri Jun 22 16:45:37 2018 +0100
@@ -72,6 +72,16 @@
 
 #define FILE_HANDLE_RESERVED    ((FileHandle*)0xFFFFFFFF)
 
+/**
+ * Macros for setting console flow control.
+ */
+#define CONSOLE_FLOWCONTROL_RTS     1
+#define CONSOLE_FLOWCONTROL_CTS     2
+#define CONSOLE_FLOWCONTROL_RTSCTS  3
+#define mbed_console_concat_(x) CONSOLE_FLOWCONTROL_##x
+#define mbed_console_concat(x) mbed_console_concat_(x)
+#define CONSOLE_FLOWCONTROL mbed_console_concat(MBED_CONF_TARGET_CONSOLE_UART_FLOW_CONTROL)
+
 using namespace mbed;
 
 #if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
@@ -116,7 +126,6 @@
 #if DEVICE_SERIAL
 extern int stdio_uart_inited;
 extern serial_t stdio_uart;
-#endif
 
 /* Private FileHandle to implement backwards-compatible functionality of
  * direct HAL serial access for default stdin/stdout/stderr.
@@ -147,6 +156,13 @@
     if (stdio_uart_inited) return;
     serial_init(&stdio_uart, tx, rx);
     serial_baud(&stdio_uart, baud);
+#if   CONSOLE_FLOWCONTROL == CONSOLE_FLOWCONTROL_RTS
+    serial_set_flow_control(&stdio_uart, FlowControlRTS, STDIO_UART_RTS, NC);
+#elif CONSOLE_FLOWCONTROL == CONSOLE_FLOWCONTROL_CTS
+    serial_set_flow_control(&stdio_uart, FlowControlCTS, NC, STDIO_UART_CTS);
+#elif CONSOLE_FLOWCONTROL == CONSOLE_FLOWCONTROL_RTSCTS
+    serial_set_flow_control(&stdio_uart, FlowControlRTSCTS, STDIO_UART_RTS, STDIO_UART_CTS);
+#endif
 }
 
 ssize_t DirectSerial::write(const void *buffer, size_t size) {
@@ -176,6 +192,7 @@
     }
     return revents;
 }
+#endif
 
 class Sink : public FileHandle {
 public:
@@ -216,6 +233,13 @@
 #if DEVICE_SERIAL
 #  if MBED_CONF_PLATFORM_STDIO_BUFFERED_SERIAL
     static UARTSerial console(STDIO_UART_TX, STDIO_UART_RX, MBED_CONF_PLATFORM_STDIO_BAUD_RATE);
+#   if   CONSOLE_FLOWCONTROL == CONSOLE_FLOWCONTROL_RTS
+        console.set_flow_control(SerialBase::RTS, STDIO_UART_RTS, NC);
+#   elif CONSOLE_FLOWCONTROL == CONSOLE_FLOWCONTROL_CTS
+        console.set_flow_control(SerialBase::CTS, NC, STDIO_UART_CTS);
+#   elif CONSOLE_FLOWCONTROL == CONSOLE_FLOWCONTROL_RTSCTS
+        console.set_flow_control(SerialBase::RTSCTS, STDIO_UART_RTS, STDIO_UART_CTS);
+#   endif
 #  else
     static DirectSerial console(STDIO_UART_TX, STDIO_UART_RX, MBED_CONF_PLATFORM_STDIO_BAUD_RATE);
 #  endif
@@ -320,16 +344,16 @@
 }
 
 int mbed::bind_to_fd(FileHandle *fh) {
-    int fh_i = reserve_filehandle();
-    if (fh_i < 0) {
-        return fh_i;
+    int fildes = reserve_filehandle();
+    if (fildes < 0) {
+        return fildes;
     }
 
-    filehandles[fh_i] = fh;
-    stdio_in_prev[fh_i] = 0;
-    stdio_out_prev[fh_i] = 0;
+    filehandles[fildes] = fh;
+    stdio_in_prev[fildes] = 0;
+    stdio_out_prev[fildes] = 0;
 
-    return fh_i;
+    return fildes;
 }
 
 static int unbind_from_fd(int fd, FileHandle *fh) {
@@ -440,9 +464,9 @@
 }
 
 extern "C" int open(const char *name, int oflag, ...) {
-    int fh_i = reserve_filehandle();
-    if (fh_i < 0) {
-        return fh_i;
+    int fildes = reserve_filehandle();
+    if (fildes < 0) {
+        return fildes;
     }
 
     FileHandle *res = NULL;
@@ -452,7 +476,7 @@
         /* The first part of the filename (between first 2 '/') is not a
          * registered mount point in the namespace.
          */
-        return handle_open_errors(-ENODEV, fh_i);
+        return handle_open_errors(-ENODEV, fildes);
     }
 
     if (path.isFile()) {
@@ -460,28 +484,28 @@
     } else {
         FileSystemHandle *fs = path.fileSystem();
         if (fs == NULL) {
-            return handle_open_errors(-ENODEV, fh_i);
+            return handle_open_errors(-ENODEV, fildes);
         }
         int err = fs->open(&res, path.fileName(), oflag);
         if (err) {
-            return handle_open_errors(err, fh_i);
+            return handle_open_errors(err, fildes);
         }
     }
 
-    filehandles[fh_i] = res;
-    stdio_in_prev[fh_i] = 0;
-    stdio_out_prev[fh_i] = 0;
+    filehandles[fildes] = res;
+    stdio_in_prev[fildes] = 0;
+    stdio_out_prev[fildes] = 0;
 
-    return fh_i;
+    return fildes;
 }
 
 extern "C" int PREFIX(_close)(FILEHANDLE fh) {
     return close(fh);
 }
 
-extern "C" int close(int fh) {
-    FileHandle* fhc = get_fhc(fh);
-    filehandles[fh] = NULL;
+extern "C" int close(int fildes) {
+    FileHandle* fhc = get_fhc(fildes);
+    filehandles[fildes] = NULL;
     if (fhc == NULL) {
         errno = EBADF;
         return -1;
@@ -514,7 +538,7 @@
 
 #if defined(MBED_TRAP_ERRORS_ENABLED) && MBED_TRAP_ERRORS_ENABLED && defined(MBED_CONF_RTOS_PRESENT)
     if (core_util_is_isr_active() || !core_util_are_interrupts_enabled()) {
-        error("Error - writing to a file in an ISR or critical section\r\n");
+        MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_PROHIBITED_IN_ISR_CONTEXT), "Error - writing to a file in an ISR or critical section\r\n", fh);
     }
 #endif
 
@@ -586,9 +610,9 @@
 #endif
 }
 
-extern "C" ssize_t write(int fh, const void *buf, size_t length) {
+extern "C" ssize_t write(int fildes, const void *buf, size_t length) {
 
-    FileHandle* fhc = get_fhc(fh);
+    FileHandle* fhc = get_fhc(fildes);
     if (fhc == NULL) {
         errno = EBADF;
         return -1;
@@ -622,7 +646,7 @@
 
 #if defined(MBED_TRAP_ERRORS_ENABLED) && MBED_TRAP_ERRORS_ENABLED && defined(MBED_CONF_RTOS_PRESENT)
     if (core_util_is_isr_active() || !core_util_are_interrupts_enabled()) {
-        error("Error - reading from a file in an ISR or critical section\r\n");
+        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);
     }
 #endif
 
@@ -676,9 +700,9 @@
 #endif
 }
 
-extern "C" ssize_t read(int fh, void *buf, size_t length) {
+extern "C" ssize_t read(int fildes, void *buf, size_t length) {
 
-    FileHandle* fhc = get_fhc(fh);
+    FileHandle* fhc = get_fhc(fildes);
     if (fhc == NULL) {
         errno = EBADF;
         return -1;
@@ -703,8 +727,8 @@
     return isatty(fh);
 }
 
-extern "C" int isatty(int fh) {
-    FileHandle* fhc = get_fhc(fh);
+extern "C" int isatty(int fildes) {
+    FileHandle* fhc = get_fhc(fildes);
     if (fhc == NULL) {
         errno = EBADF;
         return 0;
@@ -741,8 +765,8 @@
     return off;
 }
 
-extern "C" off_t lseek(int fh, off_t offset, int whence) {
-    FileHandle* fhc = get_fhc(fh);
+extern "C" off_t lseek(int fildes, off_t offset, int whence) {
+    FileHandle* fhc = get_fhc(fildes);
     if (fhc == NULL) {
         errno = EBADF;
         return -1;
@@ -762,8 +786,8 @@
 }
 #endif
 
-extern "C" int fsync(int fh) {
-    FileHandle* fhc = get_fhc(fh);
+extern "C" int fsync(int fildes) {
+    FileHandle* fhc = get_fhc(fildes);
     if (fhc == NULL) {
         errno = EBADF;
         return -1;
@@ -826,8 +850,8 @@
 }
 #endif
 
-extern "C" int fstat(int fh, struct stat *st) {
-    FileHandle* fhc = get_fhc(fh);
+extern "C" int fstat(int fildes, struct stat *st) {
+    FileHandle* fhc = get_fhc(fildes);
     if (fhc == NULL) {
         errno = EBADF;
         return -1;
@@ -838,6 +862,41 @@
     return 0;
 }
 
+extern "C" int fcntl(int fildes, int cmd, ...) {
+    FileHandle *fhc = get_fhc(fildes);
+    if (fhc == NULL) {
+        errno = EBADF;
+        return -1;
+    }
+
+    switch (cmd) {
+        case F_GETFL: {
+            int flags = 0;
+            if (fhc->is_blocking()) {
+                flags |= O_NONBLOCK;
+            }
+            return flags;
+        }
+        case F_SETFL: {
+            va_list ap;
+            va_start(ap, cmd);
+            int flags = va_arg(ap, int);
+            va_end(ap);
+            int ret = fhc->set_blocking(flags & O_NONBLOCK);
+            if (ret < 0) {
+                errno = -ret;
+                return -1;
+            }
+            return 0;
+        }
+
+        default: {
+            errno = EINVAL;
+            return -1;
+        }
+    }
+}
+
 extern "C" int poll(struct pollfd fds[], nfds_t nfds, int timeout)
 {
     if (nfds > OPEN_MAX) {
@@ -1029,7 +1088,7 @@
 #include "mbed_error.h"
 namespace __gnu_cxx {
     void __verbose_terminate_handler() {
-        error("Exception");
+        MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_CLIB_EXCEPTION),"Exception", 0);
     }
 }
 extern "C" WEAK void __cxa_pure_virtual(void);
@@ -1315,7 +1374,7 @@
 
 #endif
 
-#if defined(MBED_MEM_TRACING_ENABLED) && (defined(__CC_ARM) || defined(__ICCARM__))
+#if defined(MBED_MEM_TRACING_ENABLED) && (defined(__CC_ARM) || defined(__ICCARM__) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)))
 
 // If the memory tracing is enabled, the wrappers in mbed_alloc_wrappers.cpp
 // provide the implementation for these. Note: this needs to use the wrappers
@@ -1328,7 +1387,7 @@
 {
     void *buffer = malloc_wrapper(count, MBED_CALLER_ADDR());
     if (NULL == buffer) {
-        error("Operator new out of memory\r\n");
+        MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OUT_OF_MEMORY), "Operator new out of memory\r\n", count);
     }
     return buffer;
 }
@@ -1372,7 +1431,7 @@
 {
     void *buffer = malloc_wrapper(_REENT, count, MBED_CALLER_ADDR());
     if (NULL == buffer) {
-        error("Operator new out of memory\r\n");
+        MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OUT_OF_MEMORY), "Operator new out of memory\r\n", count);
     }
     return buffer;
 }
@@ -1381,7 +1440,7 @@
 {
     void *buffer = malloc_wrapper(_REENT, count, MBED_CALLER_ADDR());
     if (NULL == buffer) {
-        error("Operator new[] out of memory\r\n");
+        MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OUT_OF_MEMORY), "Operator new out of memory\r\n", count);
     }
     return buffer;
 }
@@ -1412,7 +1471,7 @@
 {
     void *buffer = malloc(count);
     if (NULL == buffer) {
-        error("Operator new out of memory\r\n");
+        MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OUT_OF_MEMORY), "Operator new out of memory\r\n", count);
     }
     return buffer;
 }
@@ -1421,7 +1480,7 @@
 {
     void *buffer = malloc(count);
     if (NULL == buffer) {
-        error("Operator new[] out of memory\r\n");
+        MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OUT_OF_MEMORY), "Operator new[] out of memory\r\n", count);
     }
     return buffer;
 }