You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2015/11/24 21:39:51 UTC

[05/11] incubator-mynewt-larva git commit: Make it possible to use baselibc with arch native on OSX.

Make it possible to use baselibc with arch native on OSX.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/335e91b2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/335e91b2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/335e91b2

Branch: refs/heads/master
Commit: 335e91b26d5efbcb6f78b4ecb33b4ee53826e94f
Parents: 73101a1
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Nov 24 11:34:13 2015 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Tue Nov 24 11:34:13 2015 -0800

----------------------------------------------------------------------
 hw/bsp/native/src/sbrk.c        |  56 +++++++++++++++++++
 hw/mcu/native/src/hal_flash.c   | 101 ++++++++++++-----------------------
 hw/mcu/native/src/hal_system.c  |   2 +-
 hw/mcu/native/src/hal_uart.c    |  17 +++---
 libs/os/src/arch/sim/os_fault.c |  30 +++++++++++
 5 files changed, 132 insertions(+), 74 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/335e91b2/hw/bsp/native/src/sbrk.c
----------------------------------------------------------------------
diff --git a/hw/bsp/native/src/sbrk.c b/hw/bsp/native/src/sbrk.c
new file mode 100644
index 0000000..154bd39
--- /dev/null
+++ b/hw/bsp/native/src/sbrk.c
@@ -0,0 +1,56 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <sys/mman.h>
+#include <unistd.h>
+#include <errno.h>
+
+extern int getpagesize(void);
+
+static void *cont;
+static int sys_pagesize;
+static int cont_left;
+
+void *
+_sbrk(int incr)
+{
+    void *result;
+
+    if (!sys_pagesize) {
+        sys_pagesize = getpagesize();
+    }
+    if (cont && incr <= cont_left) {
+        result = cont;
+        cont_left -= incr;
+        if (cont_left) {
+            cont = (char *)cont + incr;
+        } else {
+            cont = NULL;
+        }
+        return result;
+    }
+    result = mmap(NULL, incr, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED,
+      -1, 0);
+    if (result) {
+        cont_left = sys_pagesize - (incr % sys_pagesize);
+        if (cont_left) {
+            cont = (char *)result + incr;
+        } else {
+            cont = NULL;
+        }
+    }
+    return result;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/335e91b2/hw/mcu/native/src/hal_flash.c
----------------------------------------------------------------------
diff --git a/hw/mcu/native/src/hal_flash.c b/hw/mcu/native/src/hal_flash.c
index 5251e5b..64ff1f1 100644
--- a/hw/mcu/native/src/hal_flash.c
+++ b/hw/mcu/native/src/hal_flash.c
@@ -14,7 +14,8 @@
  * limitations under the License.
  */
 
-#include <stdio.h>
+#include <sys/mman.h>
+#include <fcntl.h>
 #include <assert.h>
 #include <string.h>
 #include <inttypes.h>
@@ -23,7 +24,8 @@
 #include "mcu/mcu_sim.h"
 
 char *native_flash_file;
-static FILE *file;
+static int file;
+static void *file_loc;
 
 static int native_flash_init(void);
 static int native_flash_read(uint32_t address, void *dst, uint32_t length);
@@ -66,38 +68,41 @@ const struct hal_flash native_flash_dev = {
 static void
 flash_native_erase(uint32_t addr, uint32_t len)
 {
-    static uint8_t buf[256];
-    uint32_t end;
-    int chunk_sz;
-    int rc;
-
-    end = addr + len;
-    memset(buf, 0xff, sizeof buf);
+    memset(file_loc + addr, 0xff, len);
+}
 
-    rc = fseek(file, addr, SEEK_SET);
-    assert(rc == 0);
+static void
+flash_native_file_open(char *name)
+{
+    int created = 0;
+    extern char *tmpnam(char *s);
+    extern int ftruncate(int fd, off_t length);
 
-    while (addr < end) {
-        if (end - addr < sizeof buf) {
-            chunk_sz = end - addr;
-        } else {
-            chunk_sz = sizeof buf;
+    if (!name) {
+        name = tmpnam(NULL);
+    }
+    file = open(name, O_RDWR);
+    if (file < 0) {
+        file = open(name, O_RDWR | O_CREAT);
+        assert(file > 0);
+        created = 1;
+        if (ftruncate(file, native_flash_dev.hf_size) < 0) {
+            assert(0);
         }
-        rc = fwrite(buf, chunk_sz, 1, file);
-        assert(rc == 1);
-
-        addr += chunk_sz;
     }
-    fflush(file);
+    file_loc = mmap(0, native_flash_dev.hf_size,
+          PROT_READ | PROT_WRITE, MAP_SHARED, file, 0);
+    assert(file_loc != MAP_FAILED);
+    if (created) {
+        flash_native_erase(0, native_flash_dev.hf_size);
+    }
 }
 
 static void
 flash_native_ensure_file_open(void)
 {
-    if (file == NULL) {
-        file = tmpfile();
-        assert(file != NULL);
-        flash_native_erase(0, native_flash_dev.hf_size);
+    if (file == 0) {
+        flash_native_file_open(NULL);
     }
 }
 
@@ -120,8 +125,6 @@ flash_native_write_internal(uint32_t address, const void *src, uint32_t length,
 
     flash_native_ensure_file_open();
 
-    fseek(file, address, SEEK_SET);
-
     cur = address;
     while (cur < end) {
         if (end - cur < sizeof buf) {
@@ -142,11 +145,8 @@ flash_native_write_internal(uint32_t address, const void *src, uint32_t length,
         cur += chunk_sz;
     }
 
-    fseek(file, address, SEEK_SET);
-    rc = fwrite(src, length, 1, file);
-    assert(rc == 1);
+    memcpy((char *)file_loc + address, src, length);
 
-    fflush(file);
     return 0;
 }
 
@@ -156,37 +156,10 @@ native_flash_write(uint32_t address, const void *src, uint32_t length)
     return flash_native_write_internal(address, src, length, 0);
 }
 
-static int
-flash_native_overwrite(uint32_t address, const void *src, uint32_t length)
-{
-    return flash_native_write_internal(address, src, length, 1);
-}
-
 int
 flash_native_memset(uint32_t offset, uint8_t c, uint32_t len)
 {
-    uint8_t buf[256];
-    int chunk_sz;
-    int rc;
-
-    memset(buf, c, sizeof buf);
-
-    while (len > 0) {
-        if (len > sizeof buf) {
-            chunk_sz = sizeof buf;
-        } else {
-            chunk_sz = len;
-        }
-
-        rc = flash_native_overwrite(offset, buf, chunk_sz);
-        if (rc != 0) {
-            return rc;
-        }
-
-        offset += chunk_sz;
-        len -= chunk_sz;
-    }
-
+    memset(file_loc + offset, c, len);
     return 0;
 }
 
@@ -194,8 +167,7 @@ static int
 native_flash_read(uint32_t address, void *dst, uint32_t length)
 {
     flash_native_ensure_file_open();
-    fseek(file, address, SEEK_SET);
-    fread(dst, length, 1, file);
+    memcpy(dst, (char *)file_loc + address, length);
 
     return 0;
 }
@@ -248,12 +220,7 @@ static int
 native_flash_init(void)
 {
     if (native_flash_file) {
-        file = fopen(native_flash_file, "r+");
-        if (!file) {
-            file = fopen(native_flash_file, "w+");
-            assert(file);
-            flash_native_erase(0, native_flash_dev.hf_size);
-        }
+        flash_native_file_open(native_flash_file);
     }
     return 0;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/335e91b2/hw/mcu/native/src/hal_system.c
----------------------------------------------------------------------
diff --git a/hw/mcu/native/src/hal_system.c b/hw/mcu/native/src/hal_system.c
index 96f7a6d..eb64b60 100644
--- a/hw/mcu/native/src/hal_system.c
+++ b/hw/mcu/native/src/hal_system.c
@@ -40,7 +40,7 @@ usage(char *progname, int rc)
       "     -f flash_file tells where binary flash file is located. It gets\n"
       "        created if it doesn't already exist.\n";
 
-    write(fileno(stderr), msg, strlen(msg));
+    write(2, msg, strlen(msg));
     exit(rc);
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/335e91b2/hw/mcu/native/src/hal_uart.c
----------------------------------------------------------------------
diff --git a/hw/mcu/native/src/hal_uart.c b/hw/mcu/native/src/hal_uart.c
index 241823d..2f66016 100644
--- a/hw/mcu/native/src/hal_uart.c
+++ b/hw/mcu/native/src/hal_uart.c
@@ -130,11 +130,13 @@ set_nonblock(int fd)
 
     flags = fcntl(fd, F_GETFL);
     if (flags == -1) {
-        perror("fcntl(F_GETFL) fail");
+        const char msg[] = "fcntl(F_GETFL) fail";
+        write(1, msg, sizeof(msg));
         return;
     }
     if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
-        perror("fcntl(F_SETFL) fail");
+        const char msg[] = "fcntl(F_SETFL) fail";
+        write(1, msg, sizeof(msg));
         return;
     }
 }
@@ -145,7 +147,8 @@ uart_set_attr(int fd)
     struct termios tios;
 
     if (tcgetattr(fd, &tios)) {
-        perror("tcgetattr() failed");
+        const char msg[] = "tcgetattr() failed";
+        write(1, msg, sizeof(msg));
         return -1;
     }
 
@@ -155,7 +158,8 @@ uart_set_attr(int fd)
     tios.c_oflag = 0;
     tios.c_lflag = 0;
     if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
-        perror("tcsetattr() failed");
+        const char msg[] = "tcsetattr() failed";
+        write(1, msg, sizeof(msg));
         return -1;
     }
     return 0;
@@ -170,7 +174,8 @@ uart_pty(int port)
     char msg[64];
 
     if (openpty(&fd, &loop_slave, pty_name, NULL, NULL) < 0) {
-        perror("openpty() failed");
+        const char msg[] = "openpty() failed";
+        write(1, msg, sizeof(msg));
         return -1;
     }
 
@@ -179,7 +184,7 @@ uart_pty(int port)
     }
 
     snprintf(msg, sizeof(msg), "uart%d at %s\n", port, pty_name);
-    write(fileno(stdout), msg, strlen(msg));
+    write(1, msg, strlen(msg));
     return fd;
 err:
     close(fd);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/335e91b2/libs/os/src/arch/sim/os_fault.c
----------------------------------------------------------------------
diff --git a/libs/os/src/arch/sim/os_fault.c b/libs/os/src/arch/sim/os_fault.c
new file mode 100644
index 0000000..8bd8fbe
--- /dev/null
+++ b/libs/os/src/arch/sim/os_fault.c
@@ -0,0 +1,30 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "os/os.h"
+#include "os_priv.h"
+
+void
+__assert_func(const char *file, int line, const char *func, const char *e)
+{
+    char msg[256];
+
+    snprintf(msg, sizeof(msg), "assert at %s:%d\n", file, line);
+    write(1, msg, strlen(msg));
+}