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 2017/04/26 17:45:51 UTC

[01/10] incubator-mynewt-core git commit: drivers/rtt: add syscfg option to define output buffer size

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/master 6ac1b82ef -> 7a1e0a8e0


drivers/rtt: add syscfg option to define output buffer size

bletiny's shell requires larger buffer - 8k - to work properly
with debug logs enabled


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

Branch: refs/heads/master
Commit: f7acf777efe151b77f8cf8ef7e1408b032d1abee
Parents: d36cccb
Author: Micha\u0142 Narajowski <mi...@codecoup.pl>
Authored: Thu Apr 6 16:00:59 2017 +0200
Committer: Micha\u0142 Narajowski <mi...@codecoup.pl>
Committed: Mon Apr 24 09:27:37 2017 -0700

----------------------------------------------------------------------
 hw/drivers/rtt/include/rtt/SEGGER_RTT_Conf.h |  4 +++-
 hw/drivers/rtt/syscfg.yml                    | 24 +++++++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f7acf777/hw/drivers/rtt/include/rtt/SEGGER_RTT_Conf.h
----------------------------------------------------------------------
diff --git a/hw/drivers/rtt/include/rtt/SEGGER_RTT_Conf.h b/hw/drivers/rtt/include/rtt/SEGGER_RTT_Conf.h
index 6a7a3ae..4daaa8f 100644
--- a/hw/drivers/rtt/include/rtt/SEGGER_RTT_Conf.h
+++ b/hw/drivers/rtt/include/rtt/SEGGER_RTT_Conf.h
@@ -63,6 +63,8 @@ Revision: $Rev: 4351 $
 #ifndef SEGGER_RTT_CONF_H
 #define SEGGER_RTT_CONF_H
 
+#include "syscfg/syscfg.h"
+
 #ifdef __IAR_SYSTEMS_ICC__
   #include <intrinsics.h>
 #endif
@@ -77,7 +79,7 @@ Revision: $Rev: 4351 $
 #define SEGGER_RTT_MAX_NUM_UP_BUFFERS             (3)     // Max. number of up-buffers (T->H) available on this target    (Default: 3)
 #define SEGGER_RTT_MAX_NUM_DOWN_BUFFERS           (3)     // Max. number of down-buffers (H->T) available on this target  (Default: 3)
 
-#define BUFFER_SIZE_UP                            (1024)  // Size of the buffer for terminal output of target, up to host (Default: 1k)
+#define BUFFER_SIZE_UP                            (MYNEWT_VAL(RTT_BUFFER_SIZE_UP))  // Size of the buffer for terminal output of target, up to host (Default: 1k)
 #define BUFFER_SIZE_DOWN                          (16)    // Size of the buffer for terminal input to target from host (Usually keyboard input) (Default: 16)
 
 #define SEGGER_RTT_PRINTF_BUFFER_SIZE             (64u)    // Size of buffer for RTT printf to bulk-send chars via RTT     (Default: 64)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f7acf777/hw/drivers/rtt/syscfg.yml
----------------------------------------------------------------------
diff --git a/hw/drivers/rtt/syscfg.yml b/hw/drivers/rtt/syscfg.yml
new file mode 100644
index 0000000..3463eec
--- /dev/null
+++ b/hw/drivers/rtt/syscfg.yml
@@ -0,0 +1,24 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+#
+
+# Package: hw/drivers/rtt
+
+syscfg.defs:
+    RTT_BUFFER_SIZE_UP:
+        description: 'Size of the output buffer'
+        value: 1024


[10/10] incubator-mynewt-core git commit: This closes #230.

Posted by ma...@apache.org.
This closes #230.

Merge branch 'new-console' of https://github.com/michal-narajowski/incubator-mynewt-core


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/7a1e0a8e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/7a1e0a8e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/7a1e0a8e

Branch: refs/heads/master
Commit: 7a1e0a8e0c9d5c01efb35c6d16782a95f5d56516
Parents: 6ac1b82 5008f7c
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Apr 26 10:20:20 2017 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Wed Apr 26 10:20:20 2017 -0700

----------------------------------------------------------------------
 apps/bleprph/syscfg.yml                       |    1 +
 hw/drivers/rtt/include/rtt/SEGGER_RTT.h       |  244 ++++
 hw/drivers/rtt/include/rtt/SEGGER_RTT_Conf.h  |  267 ++++
 hw/drivers/rtt/pkg.yml                        |   28 +
 hw/drivers/rtt/src/SEGGER_RTT.c               | 1462 ++++++++++++++++++++
 hw/drivers/rtt/src/SEGGER_RTT_printf.c        |  511 +++++++
 hw/drivers/rtt/syscfg.yml                     |   24 +
 kernel/os/include/os/os_eventq.h              |    1 +
 kernel/os/src/os_eventq.c                     |   14 +
 sys/console/full/include/console/console.h    |   19 +-
 sys/console/full/include/console/prompt.h     |   21 +-
 sys/console/full/pkg.yml                      |    3 +
 sys/console/full/src/cons_fmt.c               |  112 --
 sys/console/full/src/cons_tty.c               |  574 --------
 sys/console/full/src/console.c                |  535 +++++++
 sys/console/full/src/console_fmt.c            |  103 ++
 sys/console/full/src/console_priv.h           |   38 +
 sys/console/full/src/prompt.c                 |   57 -
 sys/console/full/src/rtt_console.c            |   96 ++
 sys/console/full/src/ticks.c                  |    2 +-
 sys/console/full/src/uart_console.c           |  205 +++
 sys/console/full/syscfg.yml                   |   45 +-
 sys/console/minimal/include/console/console.h |   39 +-
 sys/console/minimal/include/console/ticks.h   |   51 -
 sys/console/minimal/pkg.yml                   |    3 +
 sys/console/minimal/src/cons_tty.c            |  253 ----
 sys/console/minimal/src/console.c             |  303 ++++
 sys/console/minimal/src/console_priv.h        |   38 +
 sys/console/minimal/src/rtt_console.c         |   96 ++
 sys/console/minimal/src/uart_console.c        |  205 +++
 sys/console/minimal/syscfg.yml                |   32 +-
 sys/console/stub/include/console/console.h    |   42 +-
 32 files changed, 4329 insertions(+), 1095 deletions(-)
----------------------------------------------------------------------



[05/10] incubator-mynewt-core git commit: sys/console: bring back console/minimal

Posted by ma...@apache.org.
sys/console: bring back console/minimal


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/5008f7c9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/5008f7c9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/5008f7c9

Branch: refs/heads/master
Commit: 5008f7c98a760b0e66d59b05be7ba6356281bca8
Parents: c7b6f80
Author: Micha\u0142 Narajowski <mi...@codecoup.pl>
Authored: Thu Apr 20 08:32:03 2017 +0200
Committer: Micha\u0142 Narajowski <mi...@codecoup.pl>
Committed: Tue Apr 25 17:49:15 2017 -0700

----------------------------------------------------------------------
 sys/console/minimal/include/console/console.h |  80 ++++++
 sys/console/minimal/include/console/prompt.h  |  55 ++++
 sys/console/minimal/pkg.yml                   |  36 +++
 sys/console/minimal/src/console.c             | 303 +++++++++++++++++++++
 sys/console/minimal/src/console_priv.h        |  38 +++
 sys/console/minimal/src/rtt_console.c         |  96 +++++++
 sys/console/minimal/src/uart_console.c        | 205 ++++++++++++++
 sys/console/minimal/syscfg.yml                |  49 ++++
 8 files changed, 862 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5008f7c9/sys/console/minimal/include/console/console.h
----------------------------------------------------------------------
diff --git a/sys/console/minimal/include/console/console.h b/sys/console/minimal/include/console/console.h
new file mode 100644
index 0000000..ede7269
--- /dev/null
+++ b/sys/console/minimal/include/console/console.h
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+#ifndef __CONSOLE_H__
+#define __CONSOLE_H__
+
+#include <inttypes.h>
+#include "syscfg/syscfg.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct os_eventq;
+
+struct console_input {
+    char line[MYNEWT_VAL(CONSOLE_MAX_INPUT_LEN)];
+};
+
+typedef void (*console_rx_cb)(void);
+
+int console_init(console_rx_cb rx_cb);
+int console_is_init(void);
+
+void console_write(const char *str, int cnt);
+#if MYNEWT_VAL(CONSOLE_COMPAT)
+int console_read(char *str, int cnt, int *newline);
+#endif
+
+void console_blocking_mode(void);
+void console_non_blocking_mode(void);
+
+static void inline
+console_echo(int on)
+{
+}
+
+static int console_printf(const char *fmt, ...)
+    __attribute__ ((format (printf, 1, 2)));;
+static int inline
+console_printf(const char *fmt, ...)
+{
+    return 0;
+}
+
+void console_set_queues(struct os_eventq *avail_queue,
+                        struct os_eventq *cmd_queue);
+
+static void inline
+console_set_completion_cb(uint8_t (*completion)(char *str, uint8_t len))
+{
+}
+
+int console_handle_char(uint8_t byte);
+
+extern int console_is_midline;
+extern int console_out(int character);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __CONSOLE_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5008f7c9/sys/console/minimal/include/console/prompt.h
----------------------------------------------------------------------
diff --git a/sys/console/minimal/include/console/prompt.h b/sys/console/minimal/include/console/prompt.h
new file mode 100644
index 0000000..6de36f5
--- /dev/null
+++ b/sys/console/minimal/include/console/prompt.h
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+#ifndef __CONSOLE_PROMPT_H__
+#define __CONSOLE_PROMPT_H__
+
+#include <stdarg.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* print console prompt */
+static void inline
+console_print_prompt(void)
+{
+}
+
+/* set the console prompt character */
+static void inline
+console_set_prompt(char ch)
+{
+}
+
+static void inline
+console_no_prompt(void)
+{
+}
+
+static void inline
+console_yes_prompt(void)
+{
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __CONSOLE_PROMPT_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5008f7c9/sys/console/minimal/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/console/minimal/pkg.yml b/sys/console/minimal/pkg.yml
new file mode 100644
index 0000000..862b46b
--- /dev/null
+++ b/sys/console/minimal/pkg.yml
@@ -0,0 +1,36 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+#
+
+pkg.name: sys/console/minimal
+pkg.description: Text-based IO interface (minimized).
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps:
+    - hw/hal
+    - kernel/os
+pkg.deps.CONSOLE_UART:
+    - hw/drivers/uart
+pkg.deps.CONSOLE_RTT:
+    - hw/drivers/rtt
+pkg.apis: console
+
+pkg.init:
+    console_pkg_init: 20

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5008f7c9/sys/console/minimal/src/console.c
----------------------------------------------------------------------
diff --git a/sys/console/minimal/src/console.c b/sys/console/minimal/src/console.c
new file mode 100644
index 0000000..0dbccaf
--- /dev/null
+++ b/sys/console/minimal/src/console.c
@@ -0,0 +1,303 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 <assert.h>
+#include <inttypes.h>
+#include <ctype.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "syscfg/syscfg.h"
+#include "os/os.h"
+#include "sysinit/sysinit.h"
+#include "console/console.h"
+#include "console_priv.h"
+
+/* Indicates whether the previous line of output was completed. */
+int console_is_midline;
+
+#define CONSOLE_NLIP_PKT_START1 (6)
+#define CONSOLE_NLIP_PKT_START2 (9)
+#define CONSOLE_NLIP_DATA_START1 (4)
+#define CONSOLE_NLIP_DATA_START2 (20)
+
+#define NLIP_PKT_START1  (1 << 0)
+#define NLIP_PKT_START2  (1 << 1)
+#define NLIP_DATA_START1 (1 << 2)
+#define NLIP_DATA_START2 (1 << 3)
+
+#if MYNEWT_VAL(CONSOLE_COMPAT)
+#define CONSOLE_COMPAT_MAX_CMD_QUEUED 1
+static struct console_input buf[CONSOLE_COMPAT_MAX_CMD_QUEUED];
+static struct os_event shell_console_ev[CONSOLE_COMPAT_MAX_CMD_QUEUED];
+static console_rx_cb console_compat_rx_cb; /* callback that input is ready */
+static struct os_eventq compat_avail_queue;
+static struct os_eventq compat_lines_queue;
+#endif
+
+static int nlip_state;
+static int echo = MYNEWT_VAL(CONSOLE_ECHO);
+
+static uint8_t cur, end;
+static struct os_eventq *avail_queue;
+static struct os_eventq *lines_queue;
+
+void
+console_write(const char *str, int cnt)
+{
+    int i;
+
+    for (i = 0; i < cnt; i++) {
+        if (console_out((int)str[i]) == EOF) {
+            break;
+        }
+    }
+}
+
+#if MYNEWT_VAL(CONSOLE_COMPAT)
+int
+console_read(char *str, int cnt, int *newline)
+{
+    struct os_event *ev;
+    struct console_input *cmd;
+    size_t len;
+
+    *newline = 0;
+    ev = os_eventq_get_no_wait(lines_queue);
+    if (!ev) {
+        return 0;
+    }
+    cmd = ev->ev_arg;
+    len = strlen(cmd->line);
+    strncpy(str, cmd->line, len+1);
+    os_eventq_put(avail_queue, ev);
+    *newline = 1;
+    return len;
+}
+#endif
+
+void
+console_blocking_mode(void)
+{
+#if MYNEWT_VAL(CONSOLE_UART)
+    uart_console_blocking_mode();
+#endif
+}
+
+void
+console_non_blocking_mode(void)
+{
+#if MYNEWT_VAL(CONSOLE_UART)
+    uart_console_non_blocking_mode();
+#endif
+}
+
+static void
+insert_char(char *pos, char c, uint8_t end)
+{
+    if (cur + end >= MYNEWT_VAL(CONSOLE_MAX_INPUT_LEN) - 1) {
+        return;
+    }
+
+    if (echo) {
+        /* Echo back to console */
+        console_out(c);
+    }
+
+    *pos = c;
+    return;
+
+}
+
+static int
+handle_nlip(uint8_t byte)
+{
+    if (((nlip_state & NLIP_PKT_START1) &&
+         (nlip_state & NLIP_PKT_START2)) ||
+        ((nlip_state & NLIP_DATA_START1) &&
+         (nlip_state & NLIP_DATA_START2)))
+    {
+        return 1;
+    }
+
+    if ((nlip_state & NLIP_PKT_START1) &&
+        (byte == CONSOLE_NLIP_PKT_START2)) {
+        nlip_state |= NLIP_PKT_START2;
+        return 1;
+    } else if ((nlip_state & NLIP_DATA_START1) &&
+               (byte == CONSOLE_NLIP_DATA_START2)) {
+        nlip_state |= NLIP_DATA_START2;
+        return 1;
+    } else {
+        nlip_state = 0;
+        return 0;
+    }
+}
+
+int
+console_handle_char(uint8_t byte)
+{
+#if !MYNEWT_VAL(CONSOLE_INPUT)
+    return 0;
+#endif
+
+    static struct os_event *ev;
+    static struct console_input *input;
+
+    if (!avail_queue || !lines_queue) {
+        return 0;
+    }
+
+    if (!ev) {
+        ev = os_eventq_get_no_wait(avail_queue);
+        if (!ev)
+            return 0;
+        input = ev->ev_arg;
+    }
+
+    if (handle_nlip(byte))  {
+        if (byte == '\n') {
+            insert_char(&input->line[cur++], byte, end);
+            input->line[cur] = '\0';
+            cur = 0;
+            end = 0;
+            os_eventq_put(lines_queue, ev);
+            nlip_state = 0;
+
+#if MYNEWT_VAL(CONSOLE_COMPAT)
+            if (console_compat_rx_cb) {
+                console_compat_rx_cb();
+            }
+#endif
+
+            input = NULL;
+            ev = NULL;
+            console_echo(1);
+            return 0;
+        /* Ignore characters if there's no more buffer space */
+        } else if (byte == CONSOLE_NLIP_PKT_START2) {
+            /* Disable echo to not flood the UART */
+            console_echo(0);
+            insert_char(&input->line[cur++], CONSOLE_NLIP_PKT_START1, end);
+        } else if (byte == CONSOLE_NLIP_DATA_START2) {
+            /* Disable echo to not flood the UART */
+            console_echo(0);
+            insert_char(&input->line[cur++], CONSOLE_NLIP_DATA_START1, end);
+        }
+
+        insert_char(&input->line[cur++], byte, end);
+
+        return 0;
+    }
+
+    /* Handle special control characters */
+    if (!isprint(byte)) {
+        switch (byte) {
+        case CONSOLE_NLIP_PKT_START1:
+            nlip_state |= NLIP_PKT_START1;
+            break;
+        case CONSOLE_NLIP_DATA_START1:
+            nlip_state |= NLIP_DATA_START1;
+            break;
+        case '\r':
+            input->line[cur + end] = '\0';
+            console_out('\r');
+            console_out('\n');
+            cur = 0;
+            end = 0;
+            os_eventq_put(lines_queue, ev);
+
+#if MYNEWT_VAL(CONSOLE_COMPAT)
+            if (console_compat_rx_cb) {
+                console_compat_rx_cb();
+            }
+#endif
+
+            input = NULL;
+            ev = NULL;
+            break;
+        default:
+            break;
+        }
+
+        return 0;
+    }
+
+    /* Ignore characters if there's no more buffer space */
+    if (cur + end < sizeof(input->line) - 1) {
+        insert_char(&input->line[cur++], byte, end);
+    }
+    return 0;
+}
+
+int
+console_is_init(void)
+{
+#if MYNEWT_VAL(CONSOLE_UART)
+    return uart_console_is_init();
+#endif
+#if MYNEWT_VAL(CONSOLE_RTT)
+    return rtt_console_is_init();
+#endif
+    return 0;
+}
+
+void
+console_set_queues(struct os_eventq *avail, struct os_eventq *lines)
+{
+    avail_queue = avail;
+    lines_queue = lines;
+}
+
+#if MYNEWT_VAL(CONSOLE_COMPAT)
+int
+console_init(console_rx_cb rx_cb)
+{
+    int i;
+
+    os_eventq_init(&compat_lines_queue);
+    os_eventq_init(&compat_avail_queue);
+    console_set_queues(&compat_avail_queue, &compat_lines_queue);
+
+    for (i = 0; i < CONSOLE_COMPAT_MAX_CMD_QUEUED; i++) {
+        shell_console_ev[i].ev_arg = &buf[i];
+        os_eventq_put(avail_queue, &shell_console_ev[i]);
+    }
+    console_compat_rx_cb = rx_cb;
+    return 0;
+}
+#endif
+
+void
+console_pkg_init(void)
+{
+    int rc = 0;
+
+    /* Ensure this function only gets called by sysinit. */
+    SYSINIT_ASSERT_ACTIVE();
+
+#if MYNEWT_VAL(CONSOLE_UART)
+    rc = uart_console_init();
+#endif
+#if MYNEWT_VAL(CONSOLE_RTT)
+    rc = rtt_console_init();
+#endif
+    SYSINIT_PANIC_ASSERT(rc == 0);
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5008f7c9/sys/console/minimal/src/console_priv.h
----------------------------------------------------------------------
diff --git a/sys/console/minimal/src/console_priv.h b/sys/console/minimal/src/console_priv.h
new file mode 100644
index 0000000..5d448c4
--- /dev/null
+++ b/sys/console/minimal/src/console_priv.h
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+#ifndef __CONSOLE_PRIV_H__
+#define __CONSOLE_PRIV_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int uart_console_is_init(void);
+int uart_console_init(void);
+void uart_console_blocking_mode(void);
+void uart_console_non_blocking_mode(void);
+int rtt_console_is_init(void);
+int rtt_console_init(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __CONSOLE_PRIV_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5008f7c9/sys/console/minimal/src/rtt_console.c
----------------------------------------------------------------------
diff --git a/sys/console/minimal/src/rtt_console.c b/sys/console/minimal/src/rtt_console.c
new file mode 100644
index 0000000..63067c1
--- /dev/null
+++ b/sys/console/minimal/src/rtt_console.c
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(CONSOLE_RTT)
+#include <ctype.h>
+
+#include "os/os.h"
+#include "os/os_cputime.h"
+#include "rtt/SEGGER_RTT.h"
+#include "console/console.h"
+#include "console_priv.h"
+
+#if MYNEWT_VAL(CONSOLE_INPUT)
+static struct hal_timer rtt_timer;
+#endif
+
+static const char CR = '\r';
+
+int
+console_out(int character)
+{
+    char c = (char)character;
+
+    if ('\n' == c) {
+        SEGGER_RTT_WriteNoLock(0, &CR, 1);
+        console_is_midline = 0;
+    } else {
+        console_is_midline = 1;
+    }
+
+    SEGGER_RTT_WriteNoLock(0, &c, 1);
+
+    return character;
+}
+
+#if MYNEWT_VAL(CONSOLE_INPUT)
+void
+rtt(void *arg)
+{
+    int key;
+    int i = 0;
+    uint32_t timeout;
+
+    key = SEGGER_RTT_GetKey();
+    if (key >= 0) {
+        console_handle_char((char)key);
+        i = 0;
+    }
+    /* These values were selected to keep the shell responsive
+     * and at the same time reduce context switches.
+     * Min sleep is 50ms and max is 250ms.
+     */
+    if (i < 5) {
+        ++i;
+    }
+    timeout = 50000 * i;
+    os_cputime_timer_relative(&rtt_timer, timeout);
+}
+#endif
+
+int
+rtt_console_is_init(void)
+{
+    return 1;
+}
+
+int
+rtt_console_init(void)
+{
+#if MYNEWT_VAL(CONSOLE_INPUT)
+    os_cputime_timer_init(&rtt_timer, rtt, NULL);
+    /* start after a second */
+    os_cputime_timer_relative(&rtt_timer, 1000000);
+#endif
+    return 0;
+}
+
+#endif /* MYNEWT_VAL(CONSOLE_RTT) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5008f7c9/sys/console/minimal/src/uart_console.c
----------------------------------------------------------------------
diff --git a/sys/console/minimal/src/uart_console.c b/sys/console/minimal/src/uart_console.c
new file mode 100644
index 0000000..fcf6c6f
--- /dev/null
+++ b/sys/console/minimal/src/uart_console.c
@@ -0,0 +1,205 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(CONSOLE_UART)
+#include <ctype.h>
+#include <assert.h>
+
+#include "os/os.h"
+#include "uart/uart.h"
+#include "bsp/bsp.h"
+
+#include "console/console.h"
+#include "console_priv.h"
+
+#define CONSOLE_HEAD_INC(cr)    (((cr)->cr_head + 1) & ((cr)->cr_size - 1))
+#define CONSOLE_TAIL_INC(cr)    (((cr)->cr_tail + 1) & ((cr)->cr_size - 1))
+
+static struct uart_dev *uart_dev;
+static struct console_ring cr_tx;
+/* must be after console_ring */
+static uint8_t cr_tx_buf[MYNEWT_VAL(CONSOLE_UART_TX_BUF_SIZE)];
+typedef void (*console_write_char)(struct uart_dev*, uint8_t);
+static console_write_char write_char_cb;
+
+struct console_ring {
+    uint8_t cr_head;
+    uint8_t cr_tail;
+    uint16_t cr_size;
+    uint8_t *cr_buf;
+};
+
+static void
+console_add_char(struct console_ring *cr, char ch)
+{
+    cr->cr_buf[cr->cr_head] = ch;
+    cr->cr_head = CONSOLE_HEAD_INC(cr);
+}
+
+static uint8_t
+console_pull_char(struct console_ring *cr)
+{
+    uint8_t ch;
+
+    ch = cr->cr_buf[cr->cr_tail];
+    cr->cr_tail = CONSOLE_TAIL_INC(cr);
+    return ch;
+}
+
+static void
+console_queue_char(struct uart_dev *uart_dev, uint8_t ch)
+{
+    int sr;
+
+    OS_ENTER_CRITICAL(sr);
+    while (CONSOLE_HEAD_INC(&cr_tx) == cr_tx.cr_tail) {
+        /* TX needs to drain */
+        uart_start_tx(uart_dev);
+        OS_EXIT_CRITICAL(sr);
+        if (os_started()) {
+            os_time_delay(1);
+        }
+        OS_ENTER_CRITICAL(sr);
+    }
+    console_add_char(&cr_tx, ch);
+    OS_EXIT_CRITICAL(sr);
+}
+
+/*
+ * Flush cnt characters from console output queue.
+ */
+static void
+console_tx_flush(int cnt)
+{
+    int i;
+    uint8_t byte;
+
+    for (i = 0; i < cnt; i++) {
+        if (cr_tx.cr_head == cr_tx.cr_tail) {
+            /*
+             * Queue is empty.
+             */
+            break;
+        }
+        byte = console_pull_char(&cr_tx);
+        uart_blocking_tx(uart_dev, byte);
+    }
+}
+
+void
+uart_console_blocking_mode(void)
+{
+    int sr;
+
+    OS_ENTER_CRITICAL(sr);
+    if (write_char_cb) {
+        write_char_cb = uart_blocking_tx;
+
+        console_tx_flush(MYNEWT_VAL(CONSOLE_UART_TX_BUF_SIZE));
+    }
+    OS_EXIT_CRITICAL(sr);
+}
+
+void
+uart_console_non_blocking_mode(void)
+{
+    int sr;
+
+    OS_ENTER_CRITICAL(sr);
+    if (write_char_cb) {
+        write_char_cb = console_queue_char;
+    }
+    OS_EXIT_CRITICAL(sr);
+}
+
+int
+console_out(int c)
+{
+    if ('\n' == c) {
+        write_char_cb(uart_dev, '\r');
+        console_is_midline = 0;
+    } else {
+        console_is_midline = 1;
+    }
+    write_char_cb(uart_dev, c);
+    uart_start_tx(uart_dev);
+
+    return c;
+}
+
+/*
+ * Interrupts disabled when console_tx_char/console_rx_char are called.
+ * Characters sent only in blocking mode.
+ */
+static int
+console_tx_char(void *arg)
+{
+    if (cr_tx.cr_head == cr_tx.cr_tail) {
+        /*
+         * No more data.
+         */
+        return -1;
+    }
+    return console_pull_char(&cr_tx);
+}
+
+/*
+ * Interrupts disabled when console_tx_char/console_rx_char are called.
+ */
+static int
+console_rx_char(void *arg, uint8_t byte)
+{
+    return console_handle_char(byte);
+}
+
+int
+uart_console_is_init(void)
+{
+    return uart_dev != NULL;
+}
+
+int
+uart_console_init(void)
+{
+    struct uart_conf uc = {
+        .uc_speed = MYNEWT_VAL(CONSOLE_UART_BAUD),
+        .uc_databits = 8,
+        .uc_stopbits = 1,
+        .uc_parity = UART_PARITY_NONE,
+        .uc_flow_ctl = MYNEWT_VAL(CONSOLE_UART_FLOW_CONTROL),
+        .uc_tx_char = console_tx_char,
+        .uc_rx_char = console_rx_char,
+    };
+
+    cr_tx.cr_size = MYNEWT_VAL(CONSOLE_UART_TX_BUF_SIZE);
+    cr_tx.cr_buf = cr_tx_buf;
+    write_char_cb = console_queue_char;
+
+    if (!uart_dev) {
+        uart_dev = (struct uart_dev *)os_dev_open(CONSOLE_UART,
+          OS_TIMEOUT_NEVER, &uc);
+        if (!uart_dev) {
+            return -1;
+        }
+    }
+    return 0;
+}
+#endif /* MYNEWT_VAL(CONSOLE_UART) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5008f7c9/sys/console/minimal/syscfg.yml
----------------------------------------------------------------------
diff --git a/sys/console/minimal/syscfg.yml b/sys/console/minimal/syscfg.yml
new file mode 100644
index 0000000..f1f25f2
--- /dev/null
+++ b/sys/console/minimal/syscfg.yml
@@ -0,0 +1,49 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+#
+
+# Package: sys/console/minimal
+
+syscfg.defs:
+    CONSOLE_UART:
+        description: 'Set console output to UART'
+        value: 1
+    CONSOLE_RTT:
+        description: 'Set console output to RTT'
+        value: 0
+    CONSOLE_INPUT:
+        description: 'Enable console input'
+        value: 1
+    CONSOLE_ECHO:
+        description: 'Default console echo'
+        value: 1
+    CONSOLE_COMPAT:
+        description: 'Console backward compatibility'
+        value: 1
+    CONSOLE_MAX_INPUT_LEN:
+        description: 'Maximum input line length'
+        value: 256
+
+    CONSOLE_UART_BAUD:
+        description: 'Console UART baud rate.'
+        value: '115200'
+    CONSOLE_UART_FLOW_CONTROL:
+        description: 'Console UART flow control.'
+        value: 'UART_FLOW_CTL_NONE'
+    CONSOLE_UART_TX_BUF_SIZE:
+        description: 'UART console transmit buffer size; must be power of 2.'
+        value: 32
\ No newline at end of file


[09/10] incubator-mynewt-core git commit: apps/bleprph: disable console input to save flash

Posted by ma...@apache.org.
apps/bleprph: disable console input to save flash


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/27ff3195
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/27ff3195
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/27ff3195

Branch: refs/heads/master
Commit: 27ff31954fe450ae2d0d2282db7c0492995b277a
Parents: 6f73953
Author: Micha\u0142 Narajowski <mi...@codecoup.pl>
Authored: Thu Mar 23 13:40:04 2017 +0100
Committer: Micha\u0142 Narajowski <mi...@codecoup.pl>
Committed: Tue Apr 25 17:49:15 2017 -0700

----------------------------------------------------------------------
 apps/bleprph/syscfg.yml | 1 +
 1 file changed, 1 insertion(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27ff3195/apps/bleprph/syscfg.yml
----------------------------------------------------------------------
diff --git a/apps/bleprph/syscfg.yml b/apps/bleprph/syscfg.yml
index 9bc5db5..d9f69bb 100644
--- a/apps/bleprph/syscfg.yml
+++ b/apps/bleprph/syscfg.yml
@@ -21,6 +21,7 @@
 syscfg.vals:
     # Use INFO log level to reduce code size.  DEBUG is too large for nRF51.
     LOG_LEVEL: 1
+    CONSOLE_INPUT: 0
 
     # Disable central and observer roles.
     BLE_ROLE_BROADCASTER: 1


[08/10] incubator-mynewt-core git commit: sys/console: switch to blocking mode for tab completion

Posted by ma...@apache.org.
sys/console: switch to blocking mode for tab completion

This patch solves an issue with tab completion and async console.
Tab completion is triggered in interrupt context and it outputs
text into the buffer. As the buffer fills up, the console tries to
wait until the uart pulls out the data from the buffer, but since
it all happens in the interrupt context, the uart cannot issue another
interrupt to pull the data thus freezing the console.

To avoid this we switch the console to blocking mode until the
completion is finished.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/6f739538
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/6f739538
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/6f739538

Branch: refs/heads/master
Commit: 6f739538e1eacf35948bcbd53702306cda869e14
Parents: 4863008
Author: Micha\u0142 Narajowski <mi...@codecoup.pl>
Authored: Thu Apr 13 10:40:19 2017 +0200
Committer: Micha\u0142 Narajowski <mi...@codecoup.pl>
Committed: Tue Apr 25 17:49:15 2017 -0700

----------------------------------------------------------------------
 sys/console/full/include/console/console.h |  1 +
 sys/console/full/src/console.c             | 10 ++++++++++
 sys/console/full/src/console_priv.h        |  1 +
 sys/console/full/src/uart_console.c        | 12 ++++++++++++
 sys/console/stub/include/console/console.h |  5 +++++
 5 files changed, 29 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6f739538/sys/console/full/include/console/console.h
----------------------------------------------------------------------
diff --git a/sys/console/full/include/console/console.h b/sys/console/full/include/console/console.h
index 0f37b79..3126c5c 100644
--- a/sys/console/full/include/console/console.h
+++ b/sys/console/full/include/console/console.h
@@ -42,6 +42,7 @@ void console_write(const char *str, int cnt);
 int console_read(char *str, int cnt, int *newline);
 #endif
 void console_blocking_mode(void);
+void console_non_blocking_mode(void);
 void console_echo(int on);
 
 int console_printf(const char *fmt, ...)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6f739538/sys/console/full/src/console.c
----------------------------------------------------------------------
diff --git a/sys/console/full/src/console.c b/sys/console/full/src/console.c
index 18ff755..f4a0257 100644
--- a/sys/console/full/src/console.c
+++ b/sys/console/full/src/console.c
@@ -120,6 +120,14 @@ console_blocking_mode(void)
 #endif
 }
 
+void
+console_non_blocking_mode(void)
+{
+#if MYNEWT_VAL(CONSOLE_UART)
+    uart_console_non_blocking_mode();
+#endif
+}
+
 static inline void
 cursor_forward(unsigned int count)
 {
@@ -368,7 +376,9 @@ console_handle_char(uint8_t byte)
             break;
         case '\t':
             if (completion_cb && !end) {
+                console_blocking_mode();
                 cur += completion_cb(input->line, cur);
+                console_non_blocking_mode();
             }
             break;
         default:

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6f739538/sys/console/full/src/console_priv.h
----------------------------------------------------------------------
diff --git a/sys/console/full/src/console_priv.h b/sys/console/full/src/console_priv.h
index d307b5d..5d448c4 100644
--- a/sys/console/full/src/console_priv.h
+++ b/sys/console/full/src/console_priv.h
@@ -27,6 +27,7 @@ extern "C" {
 int uart_console_is_init(void);
 int uart_console_init(void);
 void uart_console_blocking_mode(void);
+void uart_console_non_blocking_mode(void);
 int rtt_console_is_init(void);
 int rtt_console_init(void);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6f739538/sys/console/full/src/uart_console.c
----------------------------------------------------------------------
diff --git a/sys/console/full/src/uart_console.c b/sys/console/full/src/uart_console.c
index 44943d2..fcf6c6f 100644
--- a/sys/console/full/src/uart_console.c
+++ b/sys/console/full/src/uart_console.c
@@ -118,6 +118,18 @@ uart_console_blocking_mode(void)
     OS_EXIT_CRITICAL(sr);
 }
 
+void
+uart_console_non_blocking_mode(void)
+{
+    int sr;
+
+    OS_ENTER_CRITICAL(sr);
+    if (write_char_cb) {
+        write_char_cb = console_queue_char;
+    }
+    OS_EXIT_CRITICAL(sr);
+}
+
 int
 console_out(int c)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6f739538/sys/console/stub/include/console/console.h
----------------------------------------------------------------------
diff --git a/sys/console/stub/include/console/console.h b/sys/console/stub/include/console/console.h
index d60aaf8..b54ae1e 100644
--- a/sys/console/stub/include/console/console.h
+++ b/sys/console/stub/include/console/console.h
@@ -63,6 +63,11 @@ console_blocking_mode(void)
 }
 
 static void inline
+console_non_blocking_mode(void)
+{
+}
+
+static void inline
 console_echo(int on)
 {
 }


[03/10] incubator-mynewt-core git commit: drivers/rtt: add SEGGER RTT package

Posted by ma...@apache.org.
drivers/rtt: add SEGGER RTT package


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

Branch: refs/heads/master
Commit: d36cccb2d7cb278b27dc6476e6cd6720f6459f86
Parents: 10d350c
Author: Micha\u0142 Narajowski <mi...@codecoup.pl>
Authored: Mon Feb 20 14:07:00 2017 +0100
Committer: Micha\u0142 Narajowski <mi...@codecoup.pl>
Committed: Mon Apr 24 09:27:37 2017 -0700

----------------------------------------------------------------------
 hw/drivers/rtt/include/rtt/SEGGER_RTT.h      |  244 ++++
 hw/drivers/rtt/include/rtt/SEGGER_RTT_Conf.h |  265 ++++
 hw/drivers/rtt/pkg.yml                       |   28 +
 hw/drivers/rtt/src/SEGGER_RTT.c              | 1462 +++++++++++++++++++++
 hw/drivers/rtt/src/SEGGER_RTT_printf.c       |  511 +++++++
 5 files changed, 2510 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d36cccb2/hw/drivers/rtt/include/rtt/SEGGER_RTT.h
----------------------------------------------------------------------
diff --git a/hw/drivers/rtt/include/rtt/SEGGER_RTT.h b/hw/drivers/rtt/include/rtt/SEGGER_RTT.h
new file mode 100644
index 0000000..b4417a0
--- /dev/null
+++ b/hw/drivers/rtt/include/rtt/SEGGER_RTT.h
@@ -0,0 +1,244 @@
+/*********************************************************************
+*               SEGGER MICROCONTROLLER GmbH & Co. KG                 *
+*       Solutions for real time microcontroller applications         *
+**********************************************************************
+*                                                                    *
+*       (c) 2014 - 2016  SEGGER Microcontroller GmbH & Co. KG        *
+*                                                                    *
+*       www.segger.com     Support: support@segger.com               *
+*                                                                    *
+**********************************************************************
+*                                                                    *
+*       SEGGER RTT * Real Time Transfer for embedded targets         *
+*                                                                    *
+**********************************************************************
+*                                                                    *
+* All rights reserved.                                               *
+*                                                                    *
+* SEGGER strongly recommends to not make any changes                 *
+* to or modify the source code of this software in order to stay     *
+* compatible with the RTT protocol and J-Link.                       *
+*                                                                    *
+* Redistribution and use in source and binary forms, with or         *
+* without modification, are permitted provided that the following    *
+* conditions are met:                                                *
+*                                                                    *
+* o Redistributions of source code must retain the above copyright   *
+*   notice, this list of conditions and the following disclaimer.    *
+*                                                                    *
+* o Redistributions in binary form must reproduce the above          *
+*   copyright notice, this list of conditions and the following      *
+*   disclaimer in the documentation and/or other materials provided  *
+*   with the distribution.                                           *
+*                                                                    *
+* o Neither the name of SEGGER Microcontroller GmbH & Co. KG         *
+*   nor the names of its contributors may be used to endorse or      *
+*   promote products derived from this software without specific     *
+*   prior written permission.                                        *
+*                                                                    *
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND             *
+* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,        *
+* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF           *
+* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE           *
+* DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR *
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR           *
+* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  *
+* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;    *
+* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF      *
+* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT          *
+* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE  *
+* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH   *
+* DAMAGE.                                                            *
+*                                                                    *
+**********************************************************************
+---------------------------END-OF-HEADER------------------------------
+File    : SEGGER_RTT.h
+Purpose : Implementation of SEGGER real-time transfer which allows
+          real-time communication on targets which support debugger 
+          memory accesses while the CPU is running.
+Revision: $Rev: 4351 $
+----------------------------------------------------------------------
+*/
+
+#ifndef SEGGER_RTT_H
+#define SEGGER_RTT_H
+
+#include "rtt/SEGGER_RTT_Conf.h"
+
+/*********************************************************************
+*
+*       Defines, fixed
+*
+**********************************************************************
+*/
+
+/*********************************************************************
+*
+*       Types
+*
+**********************************************************************
+*/
+
+//
+// Description for a circular buffer (also called "ring buffer")
+// which is used as up-buffer (T->H)
+//
+typedef struct {
+  const     char*    sName;         // Optional name. Standard names so far are: "Terminal", "SysView", "J-Scope_t4i4"
+            char*    pBuffer;       // Pointer to start of buffer
+            unsigned SizeOfBuffer;  // Buffer size in bytes. Note that one byte is lost, as this implementation does not fill up the buffer in order to avoid the problem of being unable to distinguish between full and empty.
+            unsigned WrOff;         // Position of next item to be written by either target.
+  volatile  unsigned RdOff;         // Position of next item to be read by host. Must be volatile since it may be modified by host.
+            unsigned Flags;         // Contains configuration flags
+} SEGGER_RTT_BUFFER_UP;
+
+//
+// Description for a circular buffer (also called "ring buffer")
+// which is used as down-buffer (H->T)
+//
+typedef struct {
+  const     char*    sName;         // Optional name. Standard names so far are: "Terminal", "SysView", "J-Scope_t4i4"
+            char*    pBuffer;       // Pointer to start of buffer
+            unsigned SizeOfBuffer;  // Buffer size in bytes. Note that one byte is lost, as this implementation does not fill up the buffer in order to avoid the problem of being unable to distinguish between full and empty.
+  volatile  unsigned WrOff;         // Position of next item to be written by host. Must be volatile since it may be modified by host.
+            unsigned RdOff;         // Position of next item to be read by target (down-buffer).
+            unsigned Flags;         // Contains configuration flags
+} SEGGER_RTT_BUFFER_DOWN;
+
+//
+// RTT control block which describes the number of buffers available
+// as well as the configuration for each buffer
+//
+//
+typedef struct {
+  char                    acID[16];                                 // Initialized to "SEGGER RTT"
+  int                     MaxNumUpBuffers;                          // Initialized to SEGGER_RTT_MAX_NUM_UP_BUFFERS (type. 2)
+  int                     MaxNumDownBuffers;                        // Initialized to SEGGER_RTT_MAX_NUM_DOWN_BUFFERS (type. 2)
+  SEGGER_RTT_BUFFER_UP    aUp[SEGGER_RTT_MAX_NUM_UP_BUFFERS];       // Up buffers, transferring information up from target via debug probe to host
+  SEGGER_RTT_BUFFER_DOWN  aDown[SEGGER_RTT_MAX_NUM_DOWN_BUFFERS];   // Down buffers, transferring information down from host via debug probe to target
+} SEGGER_RTT_CB;
+
+/*********************************************************************
+*
+*       Global data
+*
+**********************************************************************
+*/
+extern SEGGER_RTT_CB _SEGGER_RTT;
+
+/*********************************************************************
+*
+*       RTT API functions
+*
+**********************************************************************
+*/
+#ifdef __cplusplus
+  extern "C" {
+#endif
+int          SEGGER_RTT_AllocDownBuffer         (const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
+int          SEGGER_RTT_AllocUpBuffer           (const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
+int          SEGGER_RTT_ConfigUpBuffer          (unsigned BufferIndex, const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
+int          SEGGER_RTT_ConfigDownBuffer        (unsigned BufferIndex, const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
+int          SEGGER_RTT_GetKey                  (void);
+unsigned     SEGGER_RTT_HasData                 (unsigned BufferIndex);
+int          SEGGER_RTT_HasKey                  (void);
+void         SEGGER_RTT_Init                    (void);
+unsigned     SEGGER_RTT_Read                    (unsigned BufferIndex,       void* pBuffer, unsigned BufferSize);
+unsigned     SEGGER_RTT_ReadNoLock              (unsigned BufferIndex,       void* pData,   unsigned BufferSize);
+int          SEGGER_RTT_SetNameDownBuffer       (unsigned BufferIndex, const char* sName);
+int          SEGGER_RTT_SetNameUpBuffer         (unsigned BufferIndex, const char* sName);
+int          SEGGER_RTT_SetFlagsDownBuffer      (unsigned BufferIndex, unsigned Flags);
+int          SEGGER_RTT_SetFlagsUpBuffer        (unsigned BufferIndex, unsigned Flags);
+int          SEGGER_RTT_WaitKey                 (void);
+unsigned     SEGGER_RTT_Write                   (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
+unsigned     SEGGER_RTT_WriteNoLock             (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
+unsigned     SEGGER_RTT_WriteSkipNoLock         (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
+unsigned     SEGGER_RTT_WriteString             (unsigned BufferIndex, const char* s);
+void         SEGGER_RTT_WriteWithOverwriteNoLock(unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
+//
+// Function macro for performance optimization
+//
+#define      SEGGER_RTT_HASDATA(n)       (_SEGGER_RTT.aDown[n].WrOff - _SEGGER_RTT.aDown[n].RdOff)
+
+/*********************************************************************
+*
+*       RTT "Terminal" API functions
+*
+**********************************************************************
+*/
+int     SEGGER_RTT_SetTerminal        (char TerminalId);
+int     SEGGER_RTT_TerminalOut        (char TerminalId, const char* s);
+
+/*********************************************************************
+*
+*       RTT printf functions (require SEGGER_RTT_printf.c)
+*
+**********************************************************************
+*/
+int SEGGER_RTT_printf(unsigned BufferIndex, const char * sFormat, ...);
+#ifdef __cplusplus
+  }
+#endif
+
+/*********************************************************************
+*
+*       Defines
+*
+**********************************************************************
+*/
+
+//
+// Operating modes. Define behavior if buffer is full (not enough space for entire message)
+//
+#define SEGGER_RTT_MODE_NO_BLOCK_SKIP         (0U)     // Skip. Do not block, output nothing. (Default)
+#define SEGGER_RTT_MODE_NO_BLOCK_TRIM         (1U)     // Trim: Do not block, output as much as fits.
+#define SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL    (2U)     // Block: Wait until there is space in the buffer.
+#define SEGGER_RTT_MODE_MASK                  (3U)
+
+//
+// Control sequences, based on ANSI.
+// Can be used to control color, and clear the screen
+//
+#define RTT_CTRL_RESET                ""         // Reset to default colors
+#define RTT_CTRL_CLEAR                ""         // Clear screen, reposition cursor to top left
+
+#define RTT_CTRL_TEXT_BLACK           ""
+#define RTT_CTRL_TEXT_RED             ""
+#define RTT_CTRL_TEXT_GREEN           ""
+#define RTT_CTRL_TEXT_YELLOW          ""
+#define RTT_CTRL_TEXT_BLUE            ""
+#define RTT_CTRL_TEXT_MAGENTA         ""
+#define RTT_CTRL_TEXT_CYAN            ""
+#define RTT_CTRL_TEXT_WHITE           ""
+
+#define RTT_CTRL_TEXT_BRIGHT_BLACK    ""
+#define RTT_CTRL_TEXT_BRIGHT_RED      ""
+#define RTT_CTRL_TEXT_BRIGHT_GREEN    ""
+#define RTT_CTRL_TEXT_BRIGHT_YELLOW   ""
+#define RTT_CTRL_TEXT_BRIGHT_BLUE     ""
+#define RTT_CTRL_TEXT_BRIGHT_MAGENTA  ""
+#define RTT_CTRL_TEXT_BRIGHT_CYAN     ""
+#define RTT_CTRL_TEXT_BRIGHT_WHITE    ""
+
+#define RTT_CTRL_BG_BLACK             ""
+#define RTT_CTRL_BG_RED               ""
+#define RTT_CTRL_BG_GREEN             ""
+#define RTT_CTRL_BG_YELLOW            ""
+#define RTT_CTRL_BG_BLUE              ""
+#define RTT_CTRL_BG_MAGENTA           ""
+#define RTT_CTRL_BG_CYAN              ""
+#define RTT_CTRL_BG_WHITE             ""
+
+#define RTT_CTRL_BG_BRIGHT_BLACK      ""
+#define RTT_CTRL_BG_BRIGHT_RED        ""
+#define RTT_CTRL_BG_BRIGHT_GREEN      ""
+#define RTT_CTRL_BG_BRIGHT_YELLOW     ""
+#define RTT_CTRL_BG_BRIGHT_BLUE       ""
+#define RTT_CTRL_BG_BRIGHT_MAGENTA    ""
+#define RTT_CTRL_BG_BRIGHT_CYAN       ""
+#define RTT_CTRL_BG_BRIGHT_WHITE      ""
+
+
+#endif
+
+/*************************** End of file ****************************/

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d36cccb2/hw/drivers/rtt/include/rtt/SEGGER_RTT_Conf.h
----------------------------------------------------------------------
diff --git a/hw/drivers/rtt/include/rtt/SEGGER_RTT_Conf.h b/hw/drivers/rtt/include/rtt/SEGGER_RTT_Conf.h
new file mode 100644
index 0000000..6a7a3ae
--- /dev/null
+++ b/hw/drivers/rtt/include/rtt/SEGGER_RTT_Conf.h
@@ -0,0 +1,265 @@
+/*********************************************************************
+*               SEGGER MICROCONTROLLER GmbH & Co. KG                 *
+*       Solutions for real time microcontroller applications         *
+**********************************************************************
+*                                                                    *
+*       (c) 2014 - 2016  SEGGER Microcontroller GmbH & Co. KG        *
+*                                                                    *
+*       www.segger.com     Support: support@segger.com               *
+*                                                                    *
+**********************************************************************
+*                                                                    *
+*       SEGGER RTT * Real Time Transfer for embedded targets         *
+*                                                                    *
+**********************************************************************
+*                                                                    *
+* All rights reserved.                                               *
+*                                                                    *
+* SEGGER strongly recommends to not make any changes                 *
+* to or modify the source code of this software in order to stay     *
+* compatible with the RTT protocol and J-Link.                       *
+*                                                                    *
+* Redistribution and use in source and binary forms, with or         *
+* without modification, are permitted provided that the following    *
+* conditions are met:                                                *
+*                                                                    *
+* o Redistributions of source code must retain the above copyright   *
+*   notice, this list of conditions and the following disclaimer.    *
+*                                                                    *
+* o Redistributions in binary form must reproduce the above          *
+*   copyright notice, this list of conditions and the following      *
+*   disclaimer in the documentation and/or other materials provided  *
+*   with the distribution.                                           *
+*                                                                    *
+* o Neither the name of SEGGER Microcontroller GmbH & Co. KG         *
+*   nor the names of its contributors may be used to endorse or      *
+*   promote products derived from this software without specific     *
+*   prior written permission.                                        *
+*                                                                    *
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND             *
+* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,        *
+* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF           *
+* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE           *
+* DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR *
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR           *
+* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  *
+* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;    *
+* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF      *
+* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT          *
+* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE  *
+* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH   *
+* DAMAGE.                                                            *
+*                                                                    *
+**********************************************************************
+---------------------------END-OF-HEADER------------------------------
+File    : SEGGER_RTT_Conf.h
+Purpose : Implementation of SEGGER real-time transfer (RTT) which 
+          allows real-time communication on targets which support 
+          debugger memory accesses while the CPU is running.
+Revision: $Rev: 4351 $
+----------------------------------------------------------------------
+*/
+
+#ifndef SEGGER_RTT_CONF_H
+#define SEGGER_RTT_CONF_H
+
+#ifdef __IAR_SYSTEMS_ICC__
+  #include <intrinsics.h>
+#endif
+
+/*********************************************************************
+*
+*       Defines, configurable
+*
+**********************************************************************
+*/
+
+#define SEGGER_RTT_MAX_NUM_UP_BUFFERS             (3)     // Max. number of up-buffers (T->H) available on this target    (Default: 3)
+#define SEGGER_RTT_MAX_NUM_DOWN_BUFFERS           (3)     // Max. number of down-buffers (H->T) available on this target  (Default: 3)
+
+#define BUFFER_SIZE_UP                            (1024)  // Size of the buffer for terminal output of target, up to host (Default: 1k)
+#define BUFFER_SIZE_DOWN                          (16)    // Size of the buffer for terminal input to target from host (Usually keyboard input) (Default: 16)
+
+#define SEGGER_RTT_PRINTF_BUFFER_SIZE             (64u)    // Size of buffer for RTT printf to bulk-send chars via RTT     (Default: 64)
+
+#define SEGGER_RTT_MODE_DEFAULT                   SEGGER_RTT_MODE_NO_BLOCK_SKIP // Mode for pre-initialized terminal channel (buffer 0)
+
+//
+// Target is not allowed to perform other RTT operations while string still has not been stored completely.
+// Otherwise we would probably end up with a mixed string in the buffer.
+// If using  RTT from within interrupts, multiple tasks or multi processors, define the SEGGER_RTT_LOCK() and SEGGER_RTT_UNLOCK() function here.
+// 
+// SEGGER_RTT_MAX_INTERRUPT_PRIORITY can be used in the sample lock routines on Cortex-M3/4.
+// Make sure to mask all interrupts which can send RTT data, i.e. generate SystemView events, or cause task switches.
+// When high-priority interrupts must not be masked while sending RTT data, SEGGER_RTT_MAX_INTERRUPT_PRIORITY needs to be adjusted accordingly.
+// (Higher priority = lower priority number)
+// Default value for embOS: 128u
+// Default configuration in FreeRTOS: configMAX_SYSCALL_INTERRUPT_PRIORITY: ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
+// In case of doubt mask all interrupts: 1 << (8 - BASEPRI_PRIO_BITS) i.e. 1 << 5 when 3 bits are implemented in NVIC
+// or define SEGGER_RTT_LOCK() to completely disable interrupts.
+// 
+
+#define SEGGER_RTT_MAX_INTERRUPT_PRIORITY         (0x20)   // Interrupt priority to lock on SEGGER_RTT_LOCK on Cortex-M3/4 (Default: 0x20)
+
+/*********************************************************************
+*
+*       RTT lock configuration for SEGGER Embedded Studio, 
+*       Rowley CrossStudio and GCC
+*/
+#if (defined __SES_ARM) || (defined __CROSSWORKS_ARM) || (defined __GNUC__)
+  #ifdef __ARM_ARCH_6M__
+    #define SEGGER_RTT_LOCK()   {                                                                   \
+                                    unsigned int LockState;                                         \
+                                  __asm volatile ("mrs   %0, primask  \n\t"                         \
+                                                  "mov   r1, $1     \n\t"                           \
+                                                  "msr   primask, r1  \n\t"                         \
+                                                  : "=r" (LockState)                                \
+                                                  :                                                 \
+                                                  : "r1"                                            \
+                                                  );                            
+    
+    #define SEGGER_RTT_UNLOCK()   __asm volatile ("msr   primask, %0  \n\t"                         \
+                                                  :                                                 \
+                                                  : "r" (LockState)                                 \
+                                                  :                                                 \
+                                                  );                                                \
+                                }                                             
+                                  
+  #elif (defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__))
+    #ifndef   SEGGER_RTT_MAX_INTERRUPT_PRIORITY
+      #define SEGGER_RTT_MAX_INTERRUPT_PRIORITY   (0x20)
+    #endif
+    #define SEGGER_RTT_LOCK()   {                                                                   \
+                                    unsigned int LockState;                                         \
+                                  __asm volatile ("mrs   %0, basepri  \n\t"                         \
+                                                  "mov   r1, %1       \n\t"                         \
+                                                  "msr   basepri, r1  \n\t"                         \
+                                                  : "=r" (LockState)                                \
+                                                  : "i"(SEGGER_RTT_MAX_INTERRUPT_PRIORITY)          \
+                                                  : "r1"                                            \
+                                                  );                            
+    
+    #define SEGGER_RTT_UNLOCK()   __asm volatile ("msr   basepri, %0  \n\t"                         \
+                                                  :                                                 \
+                                                  : "r" (LockState)                                 \
+                                                  :                                                 \
+                                                  );                                                \
+                                }
+  
+  #elif defined(__ARM_ARCH_7A__)
+    #define SEGGER_RTT_LOCK() {                                                \
+                                 unsigned int LockState;                       \
+                                 __asm volatile ("mrs r1, CPSR \n\t"           \
+                                                 "mov %0, r1 \n\t"             \
+                                                 "orr r1, r1, #0xC0 \n\t"      \
+                                                 "msr CPSR_c, r1 \n\t"         \
+                                                 : "=r" (LockState)            \
+                                                 :                             \
+                                                 : "r1"                        \
+                                                 );
+
+    #define SEGGER_RTT_UNLOCK() __asm volatile ("mov r0, %0 \n\t"              \
+                                                "mrs r1, CPSR \n\t"            \
+                                                "bic r1, r1, #0xC0 \n\t"       \
+                                                "and r0, r0, #0xC0 \n\t"       \
+                                                "orr r1, r1, r0 \n\t"          \
+                                                "msr CPSR_c, r1 \n\t"          \
+                                                :                              \
+                                                : "r" (LockState)              \
+                                                : "r0", "r1"                   \
+                                                );                             \
+                            }
+#else
+    #define SEGGER_RTT_LOCK()  
+    #define SEGGER_RTT_UNLOCK()
+  #endif
+#endif
+
+/*********************************************************************
+*
+*       RTT lock configuration for IAR EWARM
+*/
+#ifdef __ICCARM__
+  #if (defined (__ARM6M__) && (__CORE__ == __ARM6M__))
+    #define SEGGER_RTT_LOCK()   {                                                                   \
+                                  unsigned int LockState;                                           \
+                                  LockState = __get_PRIMASK();                                      \
+                                  __set_PRIMASK(1);                           
+                                    
+    #define SEGGER_RTT_UNLOCK()   __set_PRIMASK(LockState);                                         \
+                                }
+  #elif ((defined (__ARM7EM__) && (__CORE__ == __ARM7EM__)) || (defined (__ARM7M__) && (__CORE__ == __ARM7M__)))
+    #ifndef   SEGGER_RTT_MAX_INTERRUPT_PRIORITY
+      #define SEGGER_RTT_MAX_INTERRUPT_PRIORITY   (0x20)
+    #endif
+    #define SEGGER_RTT_LOCK()   {                                                                   \
+                                  unsigned int LockState;                                           \
+                                  LockState = __get_BASEPRI();                                      \
+                                  __set_BASEPRI(SEGGER_RTT_MAX_INTERRUPT_PRIORITY);                           
+                                    
+    #define SEGGER_RTT_UNLOCK()   __set_BASEPRI(LockState);                                         \
+                                }  
+  #endif
+#endif
+
+/*********************************************************************
+*
+*       RTT lock configuration for IAR RX
+*/
+#ifdef __ICCRX__
+  #define SEGGER_RTT_LOCK()   {                                                                     \
+                                unsigned long LockState;                                            \
+                                LockState = __get_interrupt_state();                                \
+                                __disable_interrupt();                           
+                                  
+  #define SEGGER_RTT_UNLOCK()   __set_interrupt_state(LockState);                                   \
+                              }
+#endif
+
+/*********************************************************************
+*
+*       RTT lock configuration for KEIL ARM
+*/
+#ifdef __CC_ARM
+  #if (defined __TARGET_ARCH_6S_M)
+    #define SEGGER_RTT_LOCK()   {                                                                   \
+                                  unsigned int LockState;                                           \
+                                  register unsigned char PRIMASK __asm( "primask");                 \
+                                  LockState = PRIMASK;                                              \
+                                  PRIMASK = 1u;                                                     \
+                                  __schedule_barrier();
+
+    #define SEGGER_RTT_UNLOCK()   PRIMASK = LockState;                                              \
+                                  __schedule_barrier();                                             \
+                                }
+  #elif (defined(__TARGET_ARCH_7_M) || defined(__TARGET_ARCH_7E_M))
+    #ifndef   SEGGER_RTT_MAX_INTERRUPT_PRIORITY
+      #define SEGGER_RTT_MAX_INTERRUPT_PRIORITY   (0x20)
+    #endif
+    #define SEGGER_RTT_LOCK()   {                                                                   \
+                                  unsigned int LockState;                                           \
+                                  register unsigned char BASEPRI __asm( "basepri");                 \
+                                  LockState = BASEPRI;                                              \
+                                  BASEPRI = SEGGER_RTT_MAX_INTERRUPT_PRIORITY;                      \
+                                  __schedule_barrier();
+
+    #define SEGGER_RTT_UNLOCK()   BASEPRI = LockState;                                              \
+                                  __schedule_barrier();                                             \
+                                }
+  #endif
+#endif
+
+/*********************************************************************
+*
+*       RTT lock configuration fallback
+*/
+#ifndef   SEGGER_RTT_LOCK
+  #define SEGGER_RTT_LOCK()                // Lock RTT (nestable)   (i.e. disable interrupts)
+#endif
+
+#ifndef   SEGGER_RTT_UNLOCK
+  #define SEGGER_RTT_UNLOCK()              // Unlock RTT (nestable) (i.e. enable previous interrupt lock state)
+#endif
+
+#endif
+/*************************** End of file ****************************/

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d36cccb2/hw/drivers/rtt/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/drivers/rtt/pkg.yml b/hw/drivers/rtt/pkg.yml
new file mode 100644
index 0000000..43dfe4a
--- /dev/null
+++ b/hw/drivers/rtt/pkg.yml
@@ -0,0 +1,28 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+#
+
+pkg.name: hw/drivers/rtt
+pkg.description: Segger RTT driver
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+pkg.deps:
+pkg.req_apis: 
+pkg.init:
+    rtt_pkg_init: 19
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d36cccb2/hw/drivers/rtt/src/SEGGER_RTT.c
----------------------------------------------------------------------
diff --git a/hw/drivers/rtt/src/SEGGER_RTT.c b/hw/drivers/rtt/src/SEGGER_RTT.c
new file mode 100644
index 0000000..4e5aeec
--- /dev/null
+++ b/hw/drivers/rtt/src/SEGGER_RTT.c
@@ -0,0 +1,1462 @@
+/*********************************************************************
+*               SEGGER MICROCONTROLLER GmbH & Co. KG                 *
+*       Solutions for real time microcontroller applications         *
+**********************************************************************
+*                                                                    *
+*       (c) 2014 - 2016  SEGGER Microcontroller GmbH & Co. KG        *
+*                                                                    *
+*       www.segger.com     Support: support@segger.com               *
+*                                                                    *
+**********************************************************************
+*                                                                    *
+*       SEGGER RTT * Real Time Transfer for embedded targets         *
+*                                                                    *
+**********************************************************************
+*                                                                    *
+* All rights reserved.                                               *
+*                                                                    *
+* SEGGER strongly recommends to not make any changes                 *
+* to or modify the source code of this software in order to stay     *
+* compatible with the RTT protocol and J-Link.                       *
+*                                                                    *
+* Redistribution and use in source and binary forms, with or         *
+* without modification, are permitted provided that the following    *
+* conditions are met:                                                *
+*                                                                    *
+* o Redistributions of source code must retain the above copyright   *
+*   notice, this list of conditions and the following disclaimer.    *
+*                                                                    *
+* o Redistributions in binary form must reproduce the above          *
+*   copyright notice, this list of conditions and the following      *
+*   disclaimer in the documentation and/or other materials provided  *
+*   with the distribution.                                           *
+*                                                                    *
+* o Neither the name of SEGGER Microcontroller GmbH & Co. KG         *
+*   nor the names of its contributors may be used to endorse or      *
+*   promote products derived from this software without specific     *
+*   prior written permission.                                        *
+*                                                                    *
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND             *
+* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,        *
+* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF           *
+* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE           *
+* DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR *
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR           *
+* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  *
+* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;    *
+* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF      *
+* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT          *
+* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE  *
+* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH   *
+* DAMAGE.                                                            *
+*                                                                    *
+**********************************************************************
+---------------------------END-OF-HEADER------------------------------
+File    : SEGGER_RTT.c
+Purpose : Implementation of SEGGER real-time transfer (RTT) which
+          allows real-time communication on targets which support
+          debugger memory accesses while the CPU is running.
+Revision: $Rev: 4351 $
+
+Additional information:
+          Type "int" is assumed to be 32-bits in size
+          H->T    Host to target communication
+          T->H    Target to host communication
+
+          RTT channel 0 is always present and reserved for Terminal usage.
+          Name is fixed to "Terminal"
+
+          Effective buffer size: SizeOfBuffer - 1
+
+          WrOff == RdOff:       Buffer is empty
+          WrOff == (RdOff - 1): Buffer is full
+          WrOff >  RdOff:       Free space includes wrap-around
+          WrOff <  RdOff:       Used space includes wrap-around
+          (WrOff == (SizeOfBuffer - 1)) && (RdOff == 0):  
+                                Buffer full and wrap-around after next byte
+
+
+----------------------------------------------------------------------
+*/
+
+#include "rtt/SEGGER_RTT.h"
+#include "sysinit/sysinit.h"
+
+#include <string.h>                 // for memcpy
+
+/*********************************************************************
+*
+*       Configuration, default values
+*
+**********************************************************************
+*/
+
+#ifndef   BUFFER_SIZE_UP
+  #define BUFFER_SIZE_UP                                  1024  // Size of the buffer for terminal output of target, up to host
+#endif
+
+#ifndef   BUFFER_SIZE_DOWN
+  #define BUFFER_SIZE_DOWN                                16    // Size of the buffer for terminal input to target from host (Usually keyboard input)
+#endif
+
+#ifndef   SEGGER_RTT_MAX_NUM_UP_BUFFERS
+  #define SEGGER_RTT_MAX_NUM_UP_BUFFERS                    2    // Number of up-buffers (T->H) available on this target
+#endif
+
+#ifndef   SEGGER_RTT_MAX_NUM_DOWN_BUFFERS
+  #define SEGGER_RTT_MAX_NUM_DOWN_BUFFERS                  2    // Number of down-buffers (H->T) available on this target
+#endif
+
+#ifndef SEGGER_RTT_BUFFER_SECTION
+  #if defined(SEGGER_RTT_SECTION)
+    #define SEGGER_RTT_BUFFER_SECTION SEGGER_RTT_SECTION
+  #endif
+#endif
+
+#ifndef   SEGGER_RTT_ALIGNMENT
+  #define SEGGER_RTT_ALIGNMENT                            0
+#endif
+
+#ifndef   SEGGER_RTT_BUFFER_ALIGNMENT
+  #define SEGGER_RTT_BUFFER_ALIGNMENT                     0
+#endif
+
+#ifndef   SEGGER_RTT_MODE_DEFAULT
+  #define SEGGER_RTT_MODE_DEFAULT                         SEGGER_RTT_MODE_NO_BLOCK_SKIP
+#endif
+
+#ifndef   SEGGER_RTT_LOCK
+  #define SEGGER_RTT_LOCK()
+#endif
+
+#ifndef   SEGGER_RTT_UNLOCK
+  #define SEGGER_RTT_UNLOCK()
+#endif
+
+#ifndef   STRLEN
+  #define STRLEN(a)                                       strlen((a))
+#endif
+
+#ifndef   MEMCPY
+  #define MEMCPY(pDest, pSrc, NumBytes)                   memcpy((pDest), (pSrc), (NumBytes))
+#endif
+
+#ifndef   MIN
+  #define MIN(a, b)         (((a) < (b)) ? (a) : (b))
+#endif
+
+#ifndef   MAX
+  #define MAX(a, b)         (((a) > (b)) ? (a) : (b))
+#endif
+//
+// For some environments, NULL may not be defined until certain headers are included
+//
+#ifndef NULL
+  #define NULL 0
+#endif
+
+/*********************************************************************
+*
+*       Defines, fixed
+*
+**********************************************************************
+*/
+#if (defined __ICCARM__) || (defined __ICCRX__)
+  #define RTT_PRAGMA(P) _Pragma(#P)
+#endif
+
+#if SEGGER_RTT_ALIGNMENT || SEGGER_RTT_BUFFER_ALIGNMENT
+  #if (defined __GNUC__)
+    #define SEGGER_RTT_ALIGN(Var, Alignment) Var __attribute__ ((aligned (Alignment)))
+  #elif (defined __ICCARM__) || (defined __ICCRX__)
+    #define PRAGMA(A) _Pragma(#A)
+#define SEGGER_RTT_ALIGN(Var, Alignment) RTT_PRAGMA(data_alignment=Alignment) \
+                                  Var
+  #elif (defined __CC_ARM__)
+    #define SEGGER_RTT_ALIGN(Var, Alignment) Var __attribute__ ((aligned (Alignment)))
+  #else
+    #error "Alignment not supported for this compiler."
+  #endif
+#else
+  #define SEGGER_RTT_ALIGN(Var, Alignment) Var
+#endif
+
+#if defined(SEGGER_RTT_SECTION) || defined (SEGGER_RTT_BUFFER_SECTION)
+  #if (defined __GNUC__)
+    #define SEGGER_RTT_PUT_SECTION(Var, Section) __attribute__ ((section (Section))) Var
+  #elif (defined __ICCARM__) || (defined __ICCRX__)
+#define SEGGER_RTT_PUT_SECTION(Var, Section) RTT_PRAGMA(location=Section) \
+                                        Var
+  #elif (defined __CC_ARM__)
+    #define SEGGER_RTT_PUT_SECTION(Var, Section) __attribute__ ((section (Section), zero_init))  Var
+  #else
+    #error "Section placement not supported for this compiler."
+  #endif
+#else
+  #define SEGGER_RTT_PUT_SECTION(Var, Section) Var
+#endif
+
+
+#if SEGGER_RTT_ALIGNMENT
+  #define SEGGER_RTT_CB_ALIGN(Var)  SEGGER_RTT_ALIGN(Var, SEGGER_RTT_ALIGNMENT)
+#else
+  #define SEGGER_RTT_CB_ALIGN(Var)  Var
+#endif
+
+#if SEGGER_RTT_BUFFER_ALIGNMENT
+  #define SEGGER_RTT_BUFFER_ALIGN(Var)  SEGGER_RTT_ALIGN(Var, SEGGER_RTT_BUFFER_ALIGNMENT)
+#else
+  #define SEGGER_RTT_BUFFER_ALIGN(Var)  Var
+#endif
+
+
+#if defined(SEGGER_RTT_SECTION)
+  #define SEGGER_RTT_PUT_CB_SECTION(Var) SEGGER_RTT_PUT_SECTION(Var, SEGGER_RTT_SECTION)
+#else
+  #define SEGGER_RTT_PUT_CB_SECTION(Var) Var
+#endif
+
+#if defined(SEGGER_RTT_BUFFER_SECTION)
+  #define SEGGER_RTT_PUT_BUFFER_SECTION(Var) SEGGER_RTT_PUT_SECTION(Var, SEGGER_RTT_BUFFER_SECTION)
+#else
+  #define SEGGER_RTT_PUT_BUFFER_SECTION(Var) Var
+#endif
+
+/*********************************************************************
+*
+*       Static const data
+*
+**********************************************************************
+*/
+
+static unsigned char _aTerminalId[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
+
+/*********************************************************************
+*
+*       Static data
+*
+**********************************************************************
+*/
+//
+// RTT Control Block and allocate buffers for channel 0
+//
+SEGGER_RTT_PUT_CB_SECTION(SEGGER_RTT_CB_ALIGN(SEGGER_RTT_CB _SEGGER_RTT));
+
+SEGGER_RTT_PUT_BUFFER_SECTION(SEGGER_RTT_BUFFER_ALIGN(static char _acUpBuffer  [BUFFER_SIZE_UP]));
+SEGGER_RTT_PUT_BUFFER_SECTION(SEGGER_RTT_BUFFER_ALIGN(static char _acDownBuffer[BUFFER_SIZE_DOWN]));
+
+static char _ActiveTerminal;
+
+/*********************************************************************
+*
+*       Static functions
+*
+**********************************************************************
+*/
+
+/*********************************************************************
+*
+*       _DoInit()
+*
+*  Function description
+*    Initializes the control block an buffers.
+*    May only be called via INIT() to avoid overriding settings.
+*
+*/
+#define INIT()  do {                                            \
+                  if (_SEGGER_RTT.acID[0] == '\0') { _DoInit(); }  \
+                } while (0)
+static void _DoInit(void) {
+  SEGGER_RTT_CB* p;
+  //
+  // Initialize control block
+  //
+  p = &_SEGGER_RTT;
+  p->MaxNumUpBuffers    = SEGGER_RTT_MAX_NUM_UP_BUFFERS;
+  p->MaxNumDownBuffers  = SEGGER_RTT_MAX_NUM_DOWN_BUFFERS;
+  //
+  // Initialize up buffer 0
+  //
+  p->aUp[0].sName         = "Terminal";
+  p->aUp[0].pBuffer       = _acUpBuffer;
+  p->aUp[0].SizeOfBuffer  = sizeof(_acUpBuffer);
+  p->aUp[0].RdOff         = 0u;
+  p->aUp[0].WrOff         = 0u;
+  p->aUp[0].Flags         = SEGGER_RTT_MODE_DEFAULT;
+  //
+  // Initialize down buffer 0
+  //
+  p->aDown[0].sName         = "Terminal";
+  p->aDown[0].pBuffer       = _acDownBuffer;
+  p->aDown[0].SizeOfBuffer  = sizeof(_acDownBuffer);
+  p->aDown[0].RdOff         = 0u;
+  p->aDown[0].WrOff         = 0u;
+  p->aDown[0].Flags         = SEGGER_RTT_MODE_DEFAULT;
+  //
+  // Finish initialization of the control block.
+  // Copy Id string in three steps to make sure "SEGGER RTT" is not found
+  // in initializer memory (usually flash) by J-Link
+  //
+  strcpy(&p->acID[7], "RTT");
+  strcpy(&p->acID[0], "SEGGER");
+  p->acID[6] = ' ';
+}
+
+/*********************************************************************
+*
+*       _WriteBlocking()
+*
+*  Function description
+*    Stores a specified number of characters in SEGGER RTT ring buffer
+*    and updates the associated write pointer which is periodically
+*    read by the host.
+*    The caller is responsible for managing the write chunk sizes as
+*    _WriteBlocking() will block until all data has been posted successfully.
+*
+*  Parameters
+*    pRing        Ring buffer to post to.
+*    pBuffer      Pointer to character array. Does not need to point to a \0 terminated string.
+*    NumBytes     Number of bytes to be stored in the SEGGER RTT control block.
+*
+*  Return value
+*    >= 0 - Number of bytes written into buffer.
+*/
+static unsigned _WriteBlocking(SEGGER_RTT_BUFFER_UP* pRing, const char* pBuffer, unsigned NumBytes) {
+  unsigned NumBytesToWrite;
+  unsigned NumBytesWritten;
+  unsigned RdOff;
+  unsigned WrOff;
+  //
+  // Write data to buffer and handle wrap-around if necessary
+  //
+  NumBytesWritten = 0u;
+  WrOff = pRing->WrOff;
+  do {
+    RdOff = pRing->RdOff;                         // May be changed by host (debug probe) in the meantime
+    if (RdOff > WrOff) {
+      NumBytesToWrite = RdOff - WrOff - 1u;
+    } else {
+      NumBytesToWrite = pRing->SizeOfBuffer - (WrOff - RdOff + 1u);
+    }
+    NumBytesToWrite = MIN(NumBytesToWrite, (pRing->SizeOfBuffer - WrOff));      // Number of bytes that can be written until buffer wrap-around
+    NumBytesToWrite = MIN(NumBytesToWrite, NumBytes);
+    memcpy(pRing->pBuffer + WrOff, pBuffer, NumBytesToWrite);
+    NumBytesWritten += NumBytesToWrite;
+    pBuffer         += NumBytesToWrite;
+    NumBytes        -= NumBytesToWrite;
+    WrOff           += NumBytesToWrite;
+    if (WrOff == pRing->SizeOfBuffer) {
+      WrOff = 0u;
+    }
+    pRing->WrOff = WrOff;
+  } while (NumBytes);
+  //
+  return NumBytesWritten;
+}
+
+/*********************************************************************
+*
+*       _WriteNoCheck()
+*
+*  Function description
+*    Stores a specified number of characters in SEGGER RTT ring buffer
+*    and updates the associated write pointer which is periodically
+*    read by the host.
+*    It is callers responsibility to make sure data actually fits in buffer.
+*
+*  Parameters
+*    pRing        Ring buffer to post to.
+*    pBuffer      Pointer to character array. Does not need to point to a \0 terminated string.
+*    NumBytes     Number of bytes to be stored in the SEGGER RTT control block.
+*
+*  Notes
+*    (1) If there might not be enough space in the "Up"-buffer, call _WriteBlocking
+*/
+static void _WriteNoCheck(SEGGER_RTT_BUFFER_UP* pRing, const char* pData, unsigned NumBytes) {
+  unsigned NumBytesAtOnce;
+  unsigned WrOff;
+  unsigned Rem;
+
+  WrOff = pRing->WrOff;
+  Rem = pRing->SizeOfBuffer - WrOff;
+  if (Rem > NumBytes) {
+    //
+    // All data fits before wrap around
+    //
+    memcpy(pRing->pBuffer + WrOff, pData, NumBytes);
+    pRing->WrOff = WrOff + NumBytes;
+  } else {
+    //
+    // We reach the end of the buffer, so need to wrap around
+    //
+    NumBytesAtOnce = Rem;
+    memcpy(pRing->pBuffer + WrOff, pData, NumBytesAtOnce);
+    NumBytesAtOnce = NumBytes - Rem;
+    memcpy(pRing->pBuffer, pData + Rem, NumBytesAtOnce);
+    pRing->WrOff = NumBytesAtOnce;
+  }
+}
+
+/*********************************************************************
+*
+*       _PostTerminalSwitch()
+*
+*  Function description
+*    Switch terminal to the given terminal ID.  It is the caller's
+*    responsibility to ensure the terminal ID is correct and there is
+*    enough space in the buffer for this to complete successfully.
+*
+*  Parameters
+*    pRing        Ring buffer to post to.
+*    TerminalId   Terminal ID to switch to.
+*/
+static void _PostTerminalSwitch(SEGGER_RTT_BUFFER_UP* pRing, unsigned char TerminalId) {
+  char ac[2];
+
+  ac[0] = 0xFFu;
+  ac[1] = _aTerminalId[TerminalId];  // Caller made already sure that TerminalId does not exceed our terminal limit
+  _WriteBlocking(pRing, ac, 2u);
+}
+
+/*********************************************************************
+*
+*       _GetAvailWriteSpace()
+*
+*  Function description
+*    Returns the number of bytes that can be written to the ring
+*    buffer without blocking.
+*
+*  Parameters
+*    pRing        Ring buffer to check.
+*
+*  Return value
+*    Number of bytes that are free in the buffer.
+*/
+static unsigned _GetAvailWriteSpace(SEGGER_RTT_BUFFER_UP* pRing) {
+  unsigned RdOff;
+  unsigned WrOff;
+  unsigned r;
+  //
+  // Avoid warnings regarding volatile access order.  It's not a problem
+  // in this case, but dampen compiler enthusiasm.
+  //
+  RdOff = pRing->RdOff;
+  WrOff = pRing->WrOff;
+  if (RdOff <= WrOff) {
+    r = pRing->SizeOfBuffer - 1u - WrOff + RdOff;
+  } else {
+    r = RdOff - WrOff - 1u;
+  }
+  return r;
+}
+
+/*********************************************************************
+*
+*       Public code
+*
+**********************************************************************
+*/
+/*********************************************************************
+*
+*       SEGGER_RTT_ReadNoLock()
+*
+*  Function description
+*    Reads characters from SEGGER real-time-terminal control block
+*    which have been previously stored by the host.
+*    Do not lock against interrupts and multiple access.
+*
+*  Parameters
+*    BufferIndex  Index of Down-buffer to be used (e.g. 0 for "Terminal").
+*    pBuffer      Pointer to buffer provided by target application, to copy characters from RTT-down-buffer to.
+*    BufferSize   Size of the target application buffer.
+*
+*  Return value
+*    Number of bytes that have been read.
+*/
+unsigned SEGGER_RTT_ReadNoLock(unsigned BufferIndex, void* pData, unsigned BufferSize) {
+  unsigned                NumBytesRem;
+  unsigned                NumBytesRead;
+  unsigned                RdOff;
+  unsigned                WrOff;
+  unsigned char*          pBuffer;
+  SEGGER_RTT_BUFFER_DOWN* pRing;
+  //
+  INIT();
+  pRing = &_SEGGER_RTT.aDown[BufferIndex];
+  pBuffer = (unsigned char*)pData;
+  RdOff = pRing->RdOff;
+  WrOff = pRing->WrOff;
+  NumBytesRead = 0u;
+  //
+  // Read from current read position to wrap-around of buffer, first
+  //
+  if (RdOff > WrOff) {
+    NumBytesRem = pRing->SizeOfBuffer - RdOff;
+    NumBytesRem = MIN(NumBytesRem, BufferSize);
+    memcpy(pBuffer, pRing->pBuffer + RdOff, NumBytesRem);
+    NumBytesRead += NumBytesRem;
+    pBuffer      += NumBytesRem;
+    BufferSize   -= NumBytesRem;
+    RdOff        += NumBytesRem;
+    //
+    // Handle wrap-around of buffer
+    //
+    if (RdOff == pRing->SizeOfBuffer) {
+      RdOff = 0u;
+    }
+  }
+  //
+  // Read remaining items of buffer
+  //
+  NumBytesRem = WrOff - RdOff;
+  NumBytesRem = MIN(NumBytesRem, BufferSize);
+  if (NumBytesRem > 0u) {
+    memcpy(pBuffer, pRing->pBuffer + RdOff, NumBytesRem);
+    NumBytesRead += NumBytesRem;
+    pBuffer      += NumBytesRem;
+    BufferSize   -= NumBytesRem;
+    RdOff        += NumBytesRem;
+  }
+  if (NumBytesRead) {
+    pRing->RdOff = RdOff;
+  }
+  //
+  return NumBytesRead;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_Read
+*
+*  Function description
+*    Reads characters from SEGGER real-time-terminal control block
+*    which have been previously stored by the host.
+*
+*  Parameters
+*    BufferIndex  Index of Down-buffer to be used (e.g. 0 for "Terminal").
+*    pBuffer      Pointer to buffer provided by target application, to copy characters from RTT-down-buffer to.
+*    BufferSize   Size of the target application buffer.
+*
+*  Return value
+*    Number of bytes that have been read.
+*/
+unsigned SEGGER_RTT_Read(unsigned BufferIndex, void* pBuffer, unsigned BufferSize) {
+  unsigned NumBytesRead;
+  //
+  SEGGER_RTT_LOCK();
+  //
+  // Call the non-locking read function
+  //
+  NumBytesRead = SEGGER_RTT_ReadNoLock(BufferIndex, pBuffer, BufferSize);
+  //
+  // Finish up.
+  //
+  SEGGER_RTT_UNLOCK();
+  //
+  return NumBytesRead;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_WriteWithOverwriteNoLock
+*
+*  Function description
+*    Stores a specified number of characters in SEGGER RTT
+*    control block.
+*    SEGGER_RTT_WriteWithOverwriteNoLock does not lock the application 
+*    and overwrites data if the data does not fit into the buffer.
+*
+*  Parameters
+*    BufferIndex  Index of "Up"-buffer to be used (e.g. 0 for "Terminal").
+*    pBuffer      Pointer to character array. Does not need to point to a \0 terminated string.
+*    NumBytes     Number of bytes to be stored in the SEGGER RTT control block.
+*
+*  Notes
+*    (1) If there is not enough space in the "Up"-buffer, data is overwritten.
+*    (2) For performance reasons this function does not call Init()
+*        and may only be called after RTT has been initialized.
+*        Either by calling SEGGER_RTT_Init() or calling another RTT API function first.
+*    (3) Do not use SEGGER_RTT_WriteWithOverwriteNoLock if a J-Link 
+*        connection reads RTT data.
+*/
+void SEGGER_RTT_WriteWithOverwriteNoLock(unsigned BufferIndex, const void* pBuffer, unsigned NumBytes) {
+  const char*           pData;
+  SEGGER_RTT_BUFFER_UP* pRing;
+  unsigned              Avail;
+
+  pData = (const char *)pBuffer;
+  //
+  // Get "to-host" ring buffer and copy some elements into local variables.
+  //
+  pRing = &_SEGGER_RTT.aUp[BufferIndex];
+  //
+  // Check if we will overwrite data and need to adjust the RdOff.
+  //
+  if (pRing->WrOff == pRing->RdOff) {
+    Avail = pRing->SizeOfBuffer - 1u;
+  } else if ( pRing->WrOff < pRing->RdOff) {
+    Avail = pRing->RdOff - pRing->WrOff - 1u;
+  } else {
+    Avail = pRing->RdOff - pRing->WrOff - 1u + pRing->SizeOfBuffer;
+  }
+  if (NumBytes > Avail) {
+    pRing->RdOff += (NumBytes - Avail);
+    while (pRing->RdOff >= pRing->SizeOfBuffer) {
+      pRing->RdOff -= pRing->SizeOfBuffer;
+    }
+  }
+  //
+  // Write all data, no need to check the RdOff, but possibly handle multiple wrap-arounds
+  //
+  Avail = pRing->SizeOfBuffer - pRing->WrOff;
+  do {
+    if (Avail > NumBytes) {
+      //
+      // Last round
+      //
+#if 1 // memcpy() is good for large amounts of data, but the overhead is too big for small amounts. Use a simple byte loop instead.
+      char* pDst;
+      pDst = pRing->pBuffer + pRing->WrOff;
+      pRing->WrOff += NumBytes;
+      do {
+        *pDst++ = *pData++;
+      } while (--NumBytes);
+#else
+      memcpy(pRing->pBuffer + WrOff, pData, NumBytes);
+      pRing->WrOff += NumBytes;
+#endif
+      break;  //Alternatively: NumBytes = 0;
+    } else {
+      //
+      //  Wrap-around necessary, write until wrap-around and reset WrOff
+      //
+      memcpy(pRing->pBuffer + pRing->WrOff, pData, Avail);
+      pData += Avail;
+      pRing->WrOff = 0;
+      NumBytes -= Avail;
+      Avail = (pRing->SizeOfBuffer - 1);
+    }
+  } while (NumBytes);
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_WriteSkipNoLock
+*
+*  Function description
+*    Stores a specified number of characters in SEGGER RTT
+*    control block which is then read by the host.
+*    SEGGER_RTT_WriteSkipNoLock does not lock the application and
+*    skips all data, if the data does not fit into the buffer.
+*
+*  Parameters
+*    BufferIndex  Index of "Up"-buffer to be used (e.g. 0 for "Terminal").
+*    pBuffer      Pointer to character array. Does not need to point to a \0 terminated string.
+*    NumBytes     Number of bytes to be stored in the SEGGER RTT control block.
+*
+*  Return value
+*    Number of bytes which have been stored in the "Up"-buffer.
+*
+*  Notes
+*    (1) If there is not enough space in the "Up"-buffer, all data is dropped.
+*    (2) For performance reasons this function does not call Init()
+*        and may only be called after RTT has been initialized.
+*        Either by calling SEGGER_RTT_Init() or calling another RTT API function first.
+*/
+unsigned SEGGER_RTT_WriteSkipNoLock(unsigned BufferIndex, const void* pBuffer, unsigned NumBytes) {
+  const char*           pData;
+  SEGGER_RTT_BUFFER_UP* pRing;
+  unsigned              Avail;
+  unsigned              RdOff;
+  unsigned              WrOff;
+  unsigned              Rem;
+
+  pData = (const char *)pBuffer;
+  //
+  // Get "to-host" ring buffer and copy some elements into local variables.
+  //
+  pRing = &_SEGGER_RTT.aUp[BufferIndex];
+  RdOff = pRing->RdOff;
+  WrOff = pRing->WrOff;
+  //
+  // Handle the most common cases fastest.
+  // Which is:
+  //    RdOff <= WrOff -> Space until wrap around is free.
+  //  AND
+  //    WrOff + NumBytes < SizeOfBuffer -> No Wrap around necessary.
+  //
+  //  OR
+  //
+  //    RdOff > WrOff -> Space until RdOff - 1 is free.
+  //  AND
+  //    WrOff + NumBytes < RdOff -> Data fits into buffer
+  //
+  if (RdOff <= WrOff) {
+    //
+    // Get space until WrOff will be at wrap around.
+    //
+    Avail = pRing->SizeOfBuffer - 1u - WrOff ;
+    if (Avail >= NumBytes) {
+#if 1 // memcpy() is good for large amounts of data, but the overhead is too big for small amounts. Use a simple byte loop instead.
+      char* pDst;
+      pDst = pRing->pBuffer + WrOff;
+      WrOff += NumBytes;
+      do {
+        *pDst++ = *pData++;
+      } while (--NumBytes);
+      pRing->WrOff = WrOff + NumBytes;
+#else
+      memcpy(pRing->pBuffer + WrOff, pData, NumBytes);
+      pRing->WrOff = WrOff + NumBytes;
+#endif
+      return 1;
+    }
+    //
+    // If data did not fit into space until wrap around calculate complete space in buffer.
+    //
+    Avail += RdOff;
+    //
+    // If there is still no space for the whole of this output, don't bother.
+    //
+    if (Avail >= NumBytes) {
+      //
+      //  OK, we have enough space in buffer. Copy in one or 2 chunks
+      //
+      Rem = pRing->SizeOfBuffer - WrOff;      // Space until end of buffer
+      if (Rem > NumBytes) {
+        memcpy(pRing->pBuffer + WrOff, pData, NumBytes);
+        pRing->WrOff = WrOff + NumBytes;
+      } else {
+        //
+        // We reach the end of the buffer, so need to wrap around
+        //
+        memcpy(pRing->pBuffer + WrOff, pData, Rem);
+        memcpy(pRing->pBuffer, pData + Rem, NumBytes - Rem);
+        pRing->WrOff = NumBytes - Rem;
+      }
+      return 1;
+    }
+  } else {
+    Avail = RdOff - WrOff - 1u;
+    if (Avail >= NumBytes) {
+      memcpy(pRing->pBuffer + WrOff, pData, NumBytes);
+      pRing->WrOff = WrOff + NumBytes;
+      return 1;
+    }
+  }
+  //
+  // If we reach this point no data has been written
+  //
+  return 0;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_WriteNoLock
+*
+*  Function description
+*    Stores a specified number of characters in SEGGER RTT
+*    control block which is then read by the host.
+*    SEGGER_RTT_WriteNoLock does not lock the application.
+*
+*  Parameters
+*    BufferIndex  Index of "Up"-buffer to be used (e.g. 0 for "Terminal").
+*    pBuffer      Pointer to character array. Does not need to point to a \0 terminated string.
+*    NumBytes     Number of bytes to be stored in the SEGGER RTT control block.
+*
+*  Return value
+*    Number of bytes which have been stored in the "Up"-buffer.
+*
+*  Notes
+*    (1) If there is not enough space in the "Up"-buffer, remaining characters of pBuffer are dropped.
+*    (2) For performance reasons this function does not call Init()
+*        and may only be called after RTT has been initialized.
+*        Either by calling SEGGER_RTT_Init() or calling another RTT API function first.
+*/
+unsigned SEGGER_RTT_WriteNoLock(unsigned BufferIndex, const void* pBuffer, unsigned NumBytes) {
+  unsigned              Status;
+  unsigned              Avail;
+  const char*           pData;
+  SEGGER_RTT_BUFFER_UP* pRing;
+
+  pData = (const char *)pBuffer;
+  //
+  // Get "to-host" ring buffer.
+  //
+  pRing = &_SEGGER_RTT.aUp[BufferIndex];
+  //
+  // How we output depends upon the mode...
+  //
+  switch (pRing->Flags) {
+  case SEGGER_RTT_MODE_NO_BLOCK_SKIP:
+    //
+    // If we are in skip mode and there is no space for the whole
+    // of this output, don't bother.
+    //
+    Avail = _GetAvailWriteSpace(pRing);
+    if (Avail < NumBytes) {
+      Status = 0u;
+    } else {
+      Status = NumBytes;
+      _WriteNoCheck(pRing, pData, NumBytes);
+    }
+    break;
+  case SEGGER_RTT_MODE_NO_BLOCK_TRIM:
+    //
+    // If we are in trim mode, trim to what we can output without blocking.
+    //
+    Avail = _GetAvailWriteSpace(pRing);
+    Status = Avail < NumBytes ? Avail : NumBytes;
+    _WriteNoCheck(pRing, pData, Status);
+    break;
+  case SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL:
+    //
+    // If we are in blocking mode, output everything.
+    //
+    Status = _WriteBlocking(pRing, pData, NumBytes);
+    break;
+  default:
+    Status = 0u;
+    break;
+  }
+  //
+  // Finish up.
+  //
+  return Status;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_Write
+*
+*  Function description
+*    Stores a specified number of characters in SEGGER RTT
+*    control block which is then read by the host.
+*
+*  Parameters
+*    BufferIndex  Index of "Up"-buffer to be used (e.g. 0 for "Terminal").
+*    pBuffer      Pointer to character array. Does not need to point to a \0 terminated string.
+*    NumBytes     Number of bytes to be stored in the SEGGER RTT control block.
+*
+*  Return value
+*    Number of bytes which have been stored in the "Up"-buffer.
+*
+*  Notes
+*    (1) If there is not enough space in the "Up"-buffer, remaining characters of pBuffer are dropped.
+*/
+unsigned SEGGER_RTT_Write(unsigned BufferIndex, const void* pBuffer, unsigned NumBytes) {
+  unsigned Status;
+  //
+  INIT();
+  SEGGER_RTT_LOCK();
+  //
+  // Call the non-locking write function
+  //
+  Status = SEGGER_RTT_WriteNoLock(BufferIndex, pBuffer, NumBytes);
+  //
+  // Finish up.
+  //
+  SEGGER_RTT_UNLOCK();
+  //
+  return Status;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_WriteString
+*
+*  Function description
+*    Stores string in SEGGER RTT control block.
+*    This data is read by the host.
+*
+*  Parameters
+*    BufferIndex  Index of "Up"-buffer to be used (e.g. 0 for "Terminal").
+*    s            Pointer to string.
+*
+*  Return value
+*    Number of bytes which have been stored in the "Up"-buffer.
+*
+*  Notes
+*    (1) If there is not enough space in the "Up"-buffer, depending on configuration,
+*        remaining characters may be dropped or RTT module waits until there is more space in the buffer.
+*    (2) String passed to this function has to be \0 terminated
+*    (3) \0 termination character is *not* stored in RTT buffer
+*/
+unsigned SEGGER_RTT_WriteString(unsigned BufferIndex, const char* s) {
+  unsigned Len;
+
+  Len = STRLEN(s);
+  return SEGGER_RTT_Write(BufferIndex, s, Len);
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_GetKey
+*
+*  Function description
+*    Reads one character from the SEGGER RTT buffer.
+*    Host has previously stored data there.
+*
+*  Return value
+*    <  0 -   No character available (buffer empty).
+*    >= 0 -   Character which has been read. (Possible values: 0 - 255)
+*
+*  Notes
+*    (1) This function is only specified for accesses to RTT buffer 0.
+*/
+int SEGGER_RTT_GetKey(void) {
+  char c;
+  int r;
+
+  r = (int)SEGGER_RTT_Read(0u, &c, 1u);
+  if (r == 1) {
+    r = (int)(unsigned char)c;
+  } else {
+    r = -1;
+  }
+  return r;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_WaitKey
+*
+*  Function description
+*    Waits until at least one character is avaible in the SEGGER RTT buffer.
+*    Once a character is available, it is read and this function returns.
+*
+*  Return value
+*    >=0 -   Character which has been read.
+*
+*  Notes
+*    (1) This function is only specified for accesses to RTT buffer 0
+*    (2) This function is blocking if no character is present in RTT buffer
+*/
+int SEGGER_RTT_WaitKey(void) {
+  int r;
+
+  do {
+    r = SEGGER_RTT_GetKey();
+  } while (r < 0);
+  return r;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_HasKey
+*
+*  Function description
+*    Checks if at least one character for reading is available in the SEGGER RTT buffer.
+*
+*  Return value
+*    == 0 -     No characters are available to read.
+*    == 1 -     At least one character is available.
+*
+*  Notes
+*    (1) This function is only specified for accesses to RTT buffer 0
+*/
+int SEGGER_RTT_HasKey(void) {
+  unsigned RdOff;
+  int r;
+
+  INIT();
+  RdOff = _SEGGER_RTT.aDown[0].RdOff;
+  if (RdOff != _SEGGER_RTT.aDown[0].WrOff) {
+    r = 1;
+  } else {
+    r = 0;
+  }
+  return r;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_HasData
+*
+*  Function description
+*    Check if there is data from the host in the given buffer.
+*
+*  Return value:
+*  ==0:  No data
+*  !=0:  Data in buffer
+*
+*/
+unsigned SEGGER_RTT_HasData(unsigned BufferIndex) {
+  SEGGER_RTT_BUFFER_DOWN* pRing;
+  unsigned                v;
+
+  pRing = &_SEGGER_RTT.aDown[BufferIndex];
+  v = pRing->WrOff;
+  return v - pRing->RdOff;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_AllocDownBuffer
+*
+*  Function description
+*    Run-time configuration of the next down-buffer (H->T).
+*    The next buffer, which is not used yet is configured.
+*    This includes: Buffer address, size, name, flags, ...
+*
+*  Parameters
+*    sName        Pointer to a constant name string.
+*    pBuffer      Pointer to a buffer to be used.
+*    BufferSize   Size of the buffer.
+*    Flags        Operating modes. Define behavior if buffer is full (not enough space for entire message).
+*
+*  Return value
+*    >= 0 - O.K. Buffer Index
+*     < 0 - Error
+*/
+int SEGGER_RTT_AllocDownBuffer(const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags) {
+  int BufferIndex;
+
+  INIT();
+  SEGGER_RTT_LOCK();
+  BufferIndex = 0;
+  do {
+    if (_SEGGER_RTT.aDown[BufferIndex].pBuffer == NULL) {
+      break;
+    }
+    BufferIndex++;
+  } while (BufferIndex < _SEGGER_RTT.MaxNumDownBuffers);
+  if (BufferIndex < _SEGGER_RTT.MaxNumDownBuffers) {
+    _SEGGER_RTT.aDown[BufferIndex].sName        = sName;
+    _SEGGER_RTT.aDown[BufferIndex].pBuffer      = (char*)pBuffer;
+    _SEGGER_RTT.aDown[BufferIndex].SizeOfBuffer = BufferSize;
+    _SEGGER_RTT.aDown[BufferIndex].RdOff        = 0u;
+    _SEGGER_RTT.aDown[BufferIndex].WrOff        = 0u;
+    _SEGGER_RTT.aDown[BufferIndex].Flags        = Flags;
+  } else {
+    BufferIndex = -1;
+  }
+  SEGGER_RTT_UNLOCK();
+  return BufferIndex;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_AllocUpBuffer
+*
+*  Function description
+*    Run-time configuration of the next up-buffer (T->H).
+*    The next buffer, which is not used yet is configured.
+*    This includes: Buffer address, size, name, flags, ...
+*
+*  Parameters
+*    sName        Pointer to a constant name string.
+*    pBuffer      Pointer to a buffer to be used.
+*    BufferSize   Size of the buffer.
+*    Flags        Operating modes. Define behavior if buffer is full (not enough space for entire message).
+*
+*  Return value
+*    >= 0 - O.K. Buffer Index
+*     < 0 - Error
+*/
+int SEGGER_RTT_AllocUpBuffer(const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags) {
+  int BufferIndex;
+
+  INIT();
+  SEGGER_RTT_LOCK();
+  BufferIndex = 0;
+  do {
+    if (_SEGGER_RTT.aUp[BufferIndex].pBuffer == NULL) {
+      break;
+    }
+    BufferIndex++;
+  } while (BufferIndex < _SEGGER_RTT.MaxNumUpBuffers);
+  if (BufferIndex < _SEGGER_RTT.MaxNumUpBuffers) {
+    _SEGGER_RTT.aUp[BufferIndex].sName        = sName;
+    _SEGGER_RTT.aUp[BufferIndex].pBuffer      = (char*)pBuffer;
+    _SEGGER_RTT.aUp[BufferIndex].SizeOfBuffer = BufferSize;
+    _SEGGER_RTT.aUp[BufferIndex].RdOff        = 0u;
+    _SEGGER_RTT.aUp[BufferIndex].WrOff        = 0u;
+    _SEGGER_RTT.aUp[BufferIndex].Flags        = Flags;
+  } else {
+    BufferIndex = -1;
+  }
+  SEGGER_RTT_UNLOCK();
+  return BufferIndex;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_ConfigUpBuffer
+*
+*  Function description
+*    Run-time configuration of a specific up-buffer (T->H).
+*    Buffer to be configured is specified by index.
+*    This includes: Buffer address, size, name, flags, ...
+*
+*  Parameters
+*    BufferIndex  Index of the buffer to configure.
+*    sName        Pointer to a constant name string.
+*    pBuffer      Pointer to a buffer to be used.
+*    BufferSize   Size of the buffer.
+*    Flags        Operating modes. Define behavior if buffer is full (not enough space for entire message).
+*
+*  Return value
+*    >= 0 - O.K.
+*     < 0 - Error
+*
+*  Additional information
+*    Buffer 0 is configured on compile-time.
+*    May only be called once per buffer.
+*    Buffer name and flags can be reconfigured using the appropriate functions.
+*/
+int SEGGER_RTT_ConfigUpBuffer(unsigned BufferIndex, const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags) {
+  int r;
+
+  INIT();
+  if (BufferIndex < (unsigned)_SEGGER_RTT.MaxNumUpBuffers) {
+    SEGGER_RTT_LOCK();
+    if (BufferIndex > 0u) {
+      _SEGGER_RTT.aUp[BufferIndex].sName        = sName;
+      _SEGGER_RTT.aUp[BufferIndex].pBuffer      = (char*)pBuffer;
+      _SEGGER_RTT.aUp[BufferIndex].SizeOfBuffer = BufferSize;
+      _SEGGER_RTT.aUp[BufferIndex].RdOff        = 0u;
+      _SEGGER_RTT.aUp[BufferIndex].WrOff        = 0u;
+    }
+    _SEGGER_RTT.aUp[BufferIndex].Flags          = Flags;
+    SEGGER_RTT_UNLOCK();
+    r =  0;
+  } else {
+    r = -1;
+  }
+  return r;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_ConfigDownBuffer
+*
+*  Function description
+*    Run-time configuration of a specific down-buffer (H->T).
+*    Buffer to be configured is specified by index.
+*    This includes: Buffer address, size, name, flags, ...
+*
+*  Parameters
+*    BufferIndex  Index of the buffer to configure.
+*    sName        Pointer to a constant name string.
+*    pBuffer      Pointer to a buffer to be used.
+*    BufferSize   Size of the buffer.
+*    Flags        Operating modes. Define behavior if buffer is full (not enough space for entire message).
+*
+*  Return value
+*    >= 0  O.K.
+*     < 0  Error
+*
+*  Additional information
+*    Buffer 0 is configured on compile-time.
+*    May only be called once per buffer.
+*    Buffer name and flags can be reconfigured using the appropriate functions.
+*/
+int SEGGER_RTT_ConfigDownBuffer(unsigned BufferIndex, const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags) {
+  int r;
+
+  INIT();
+  if (BufferIndex < (unsigned)_SEGGER_RTT.MaxNumDownBuffers) {
+    SEGGER_RTT_LOCK();
+    if (BufferIndex > 0u) {
+      _SEGGER_RTT.aDown[BufferIndex].sName        = sName;
+      _SEGGER_RTT.aDown[BufferIndex].pBuffer      = (char*)pBuffer;
+      _SEGGER_RTT.aDown[BufferIndex].SizeOfBuffer = BufferSize;
+      _SEGGER_RTT.aDown[BufferIndex].RdOff        = 0u;
+      _SEGGER_RTT.aDown[BufferIndex].WrOff        = 0u;
+    }
+    _SEGGER_RTT.aDown[BufferIndex].Flags          = Flags;
+    SEGGER_RTT_UNLOCK();
+    r =  0;
+  } else {
+    r = -1;
+  }
+  return r;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_SetNameUpBuffer
+*
+*  Function description
+*    Run-time configuration of a specific up-buffer name (T->H).
+*    Buffer to be configured is specified by index.
+*
+*  Parameters
+*    BufferIndex  Index of the buffer to renamed.
+*    sName        Pointer to a constant name string.
+*
+*  Return value
+*    >= 0  O.K.
+*     < 0  Error
+*/
+int SEGGER_RTT_SetNameUpBuffer(unsigned BufferIndex, const char* sName) {
+  int r;
+
+  INIT();
+  if (BufferIndex < (unsigned)_SEGGER_RTT.MaxNumUpBuffers) {
+    SEGGER_RTT_LOCK();
+    _SEGGER_RTT.aUp[BufferIndex].sName = sName;
+    SEGGER_RTT_UNLOCK();
+    r =  0;
+  } else {
+    r = -1;
+  }
+  return r;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_SetNameDownBuffer
+*
+*  Function description
+*    Run-time configuration of a specific Down-buffer name (T->H).
+*    Buffer to be configured is specified by index.
+*
+*  Parameters
+*    BufferIndex  Index of the buffer to renamed.
+*    sName        Pointer to a constant name string.
+*
+*  Return value
+*    >= 0  O.K.
+*     < 0  Error
+*/
+int SEGGER_RTT_SetNameDownBuffer(unsigned BufferIndex, const char* sName) {
+  int r;
+
+  INIT();
+  if (BufferIndex < (unsigned)_SEGGER_RTT.MaxNumDownBuffers) {
+    SEGGER_RTT_LOCK();
+    _SEGGER_RTT.aDown[BufferIndex].sName = sName;
+    SEGGER_RTT_UNLOCK();
+    r =  0;
+  } else {
+    r = -1;
+  }
+  return r;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_SetFlagsUpBuffer
+*
+*  Function description
+*    Run-time configuration of specific up-buffer flags (T->H).
+*    Buffer to be configured is specified by index.
+*
+*  Parameters
+*    BufferIndex  Index of the buffer.
+*    Flags        Flags to set for the buffer.
+*
+*  Return value
+*    >= 0  O.K.
+*     < 0  Error
+*/
+int SEGGER_RTT_SetFlagsUpBuffer(unsigned BufferIndex, unsigned Flags) {
+  int r;
+
+  INIT();
+  if (BufferIndex < (unsigned)_SEGGER_RTT.MaxNumUpBuffers) {
+    SEGGER_RTT_LOCK();
+    _SEGGER_RTT.aUp[BufferIndex].Flags = Flags;
+    SEGGER_RTT_UNLOCK();
+    r =  0;
+  } else {
+    r = -1;
+  }
+  return r;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_SetFlagsDownBuffer
+*
+*  Function description
+*    Run-time configuration of specific Down-buffer flags (T->H).
+*    Buffer to be configured is specified by index.
+*
+*  Parameters
+*    BufferIndex  Index of the buffer to renamed.
+*    Flags        Flags to set for the buffer.
+*
+*  Return value
+*    >= 0  O.K.
+*     < 0  Error
+*/
+int SEGGER_RTT_SetFlagsDownBuffer(unsigned BufferIndex, unsigned Flags) {
+  int r;
+
+  INIT();
+  if (BufferIndex < (unsigned)_SEGGER_RTT.MaxNumDownBuffers) {
+    SEGGER_RTT_LOCK();
+    _SEGGER_RTT.aDown[BufferIndex].Flags = Flags;
+    SEGGER_RTT_UNLOCK();
+    r =  0;
+  } else {
+    r = -1;
+  }
+  return r;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_Init
+*
+*  Function description
+*    Initializes the RTT Control Block.
+*    Should be used in RAM targets, at start of the application.
+*
+*/
+void SEGGER_RTT_Init (void) {
+  _DoInit();
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_SetTerminal
+*
+*  Function description
+*    Sets the terminal to be used for output on channel 0.
+*
+*  Parameters
+*    TerminalId  Index of the terminal.
+*
+*  Return value
+*    >= 0  O.K.
+*     < 0  Error (e.g. if RTT is configured for non-blocking mode and there was no space in the buffer to set the new terminal Id)
+*/
+int SEGGER_RTT_SetTerminal (char TerminalId) {
+  char                  ac[2];
+  SEGGER_RTT_BUFFER_UP* pRing;
+  unsigned Avail;
+  int r;
+  //
+  INIT();
+  //
+  r = 0;
+  ac[0] = 0xFFU;
+  if ((unsigned char)TerminalId < (unsigned char)sizeof(_aTerminalId)) { // We only support a certain number of channels
+    ac[1] = _aTerminalId[(unsigned char)TerminalId];
+    pRing = &_SEGGER_RTT.aUp[0];    // Buffer 0 is always reserved for terminal I/O, so we can use index 0 here, fixed
+    SEGGER_RTT_LOCK();    // Lock to make sure that no other task is writing into buffer, while we are and number of free bytes in buffer does not change downwards after checking and before writing
+    if ((pRing->Flags & SEGGER_RTT_MODE_MASK) == SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL) {
+      _ActiveTerminal = TerminalId;
+      _WriteBlocking(pRing, ac, 2u);
+    } else {                                                                            // Skipping mode or trim mode? => We cannot trim this command so handling is the same for both modes
+      Avail = _GetAvailWriteSpace(pRing);
+      if (Avail >= 2) {
+        _ActiveTerminal = TerminalId;    // Only change active terminal in case of success
+        _WriteNoCheck(pRing, ac, 2u);
+      } else {
+        r = -1;
+      }
+    }
+    SEGGER_RTT_UNLOCK();
+  } else {
+    r = -1;
+  }
+  return r;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_TerminalOut
+*
+*  Function description
+*    Writes a string to the given terminal
+*     without changing the terminal for channel 0.
+*
+*  Parameters
+*    TerminalId   Index of the terminal.
+*    s            String to be printed on the terminal.
+*
+*  Return value
+*    >= 0 - Number of bytes written.
+*     < 0 - Error.
+*
+*/
+int SEGGER_RTT_TerminalOut (char TerminalId, const char* s) {
+  int                   Status;
+  unsigned              FragLen;
+  unsigned              Avail;
+  SEGGER_RTT_BUFFER_UP* pRing;
+  //
+  INIT();
+  //
+  // Validate terminal ID.
+  //
+  if (TerminalId < (char)sizeof(_aTerminalId)) { // We only support a certain number of channels
+    //
+    // Get "to-host" ring buffer.
+    //
+    pRing = &_SEGGER_RTT.aUp[0];
+    //
+    // Need to be able to change terminal, write data, change back.
+    // Compute the fixed and variable sizes.
+    //
+    FragLen = strlen(s);
+    //
+    // How we output depends upon the mode...
+    //
+    SEGGER_RTT_LOCK();
+    Avail = _GetAvailWriteSpace(pRing);
+    switch (pRing->Flags & SEGGER_RTT_MODE_MASK) {
+    case SEGGER_RTT_MODE_NO_BLOCK_SKIP:
+      //
+      // If we are in skip mode and there is no space for the whole
+      // of this output, don't bother switching terminals at all.
+      //
+      if (Avail < (FragLen + 4u)) {
+        Status = 0;
+      } else {
+        _PostTerminalSwitch(pRing, TerminalId);
+        Status = (int)_WriteBlocking(pRing, s, FragLen);
+        _PostTerminalSwitch(pRing, _ActiveTerminal);
+      }
+      break;
+    case SEGGER_RTT_MODE_NO_BLOCK_TRIM:
+      //
+      // If we are in trim mode and there is not enough space for everything,
+      // trim the output but always include the terminal switch.  If no room
+      // for terminal switch, skip that totally.
+      //
+      if (Avail < 4u) {
+        Status = -1;
+      } else {
+        _PostTerminalSwitch(pRing, TerminalId);
+        Status = (int)_WriteBlocking(pRing, s, (FragLen < (Avail - 4u)) ? FragLen : (Avail - 4u));
+        _PostTerminalSwitch(pRing, _ActiveTerminal);
+      }
+      break;
+    case SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL:
+      //
+      // If we are in blocking mode, output everything.
+      //
+      _PostTerminalSwitch(pRing, TerminalId);
+      Status = (int)_WriteBlocking(pRing, s, FragLen);
+      _PostTerminalSwitch(pRing, _ActiveTerminal);
+      break;
+    default:
+      Status = -1;
+      break;
+    }
+    //
+    // Finish up.
+    //
+    SEGGER_RTT_UNLOCK();
+  } else {
+    Status = -1;
+  }
+  return Status;
+}
+
+
+void
+rtt_pkg_init(void)
+{
+    /* Ensure this function only gets called by sysinit. */
+    SYSINIT_ASSERT_ACTIVE();
+    SEGGER_RTT_Init();
+}
+
+/*************************** End of file ****************************/


[02/10] incubator-mynewt-core git commit: drivers/rtt: add SEGGER RTT package

Posted by ma...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d36cccb2/hw/drivers/rtt/src/SEGGER_RTT_printf.c
----------------------------------------------------------------------
diff --git a/hw/drivers/rtt/src/SEGGER_RTT_printf.c b/hw/drivers/rtt/src/SEGGER_RTT_printf.c
new file mode 100644
index 0000000..995a883
--- /dev/null
+++ b/hw/drivers/rtt/src/SEGGER_RTT_printf.c
@@ -0,0 +1,511 @@
+/*********************************************************************
+*               SEGGER MICROCONTROLLER GmbH & Co. KG                 *
+*       Solutions for real time microcontroller applications         *
+**********************************************************************
+*                                                                    *
+*       (c) 2014 - 2016  SEGGER Microcontroller GmbH & Co. KG        *
+*                                                                    *
+*       www.segger.com     Support: support@segger.com               *
+*                                                                    *
+**********************************************************************
+*                                                                    *
+*       SEGGER RTT * Real Time Transfer for embedded targets         *
+*                                                                    *
+**********************************************************************
+*                                                                    *
+* All rights reserved.                                               *
+*                                                                    *
+* SEGGER strongly recommends to not make any changes                 *
+* to or modify the source code of this software in order to stay     *
+* compatible with the RTT protocol and J-Link.                       *
+*                                                                    *
+* Redistribution and use in source and binary forms, with or         *
+* without modification, are permitted provided that the following    *
+* conditions are met:                                                *
+*                                                                    *
+* o Redistributions of source code must retain the above copyright   *
+*   notice, this list of conditions and the following disclaimer.    *
+*                                                                    *
+* o Redistributions in binary form must reproduce the above          *
+*   copyright notice, this list of conditions and the following      *
+*   disclaimer in the documentation and/or other materials provided  *
+*   with the distribution.                                           *
+*                                                                    *
+* o Neither the name of SEGGER Microcontroller GmbH & Co. KG         *
+*   nor the names of its contributors may be used to endorse or      *
+*   promote products derived from this software without specific     *
+*   prior written permission.                                        *
+*                                                                    *
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND             *
+* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,        *
+* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF           *
+* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE           *
+* DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR *
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR           *
+* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  *
+* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;    *
+* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF      *
+* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT          *
+* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE  *
+* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH   *
+* DAMAGE.                                                            *
+*                                                                    *
+**********************************************************************
+---------------------------END-OF-HEADER------------------------------
+File    : SEGGER_RTT_printf.c
+Purpose : Replacement for printf to write formatted data via RTT
+Revision: $Rev: 4351 $
+----------------------------------------------------------------------
+*/
+#include "rtt/SEGGER_RTT.h"
+#include "rtt/SEGGER_RTT_Conf.h"
+
+/*********************************************************************
+*
+*       Defines, configurable
+*
+**********************************************************************
+*/
+
+#ifndef SEGGER_RTT_PRINTF_BUFFER_SIZE
+  #define SEGGER_RTT_PRINTF_BUFFER_SIZE (64)
+#endif
+
+#include <stdlib.h>
+#include <stdarg.h>
+
+
+#define FORMAT_FLAG_LEFT_JUSTIFY   (1u << 0)
+#define FORMAT_FLAG_PAD_ZERO       (1u << 1)
+#define FORMAT_FLAG_PRINT_SIGN     (1u << 2)
+#define FORMAT_FLAG_ALTERNATE      (1u << 3)
+
+/*********************************************************************
+*
+*       Types
+*
+**********************************************************************
+*/
+
+typedef struct {
+  char*     pBuffer;
+  unsigned  BufferSize;
+  unsigned  Cnt;
+
+  int   ReturnValue;
+
+  unsigned RTTBufferIndex;
+} SEGGER_RTT_PRINTF_DESC;
+
+/*********************************************************************
+*
+*       Function prototypes
+*
+**********************************************************************
+*/
+int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList);
+
+/*********************************************************************
+*
+*       Static code
+*
+**********************************************************************
+*/
+/*********************************************************************
+*
+*       _StoreChar
+*/
+static void _StoreChar(SEGGER_RTT_PRINTF_DESC * p, char c) {
+  unsigned Cnt;
+
+  Cnt = p->Cnt;
+  if ((Cnt + 1u) <= p->BufferSize) {
+    *(p->pBuffer + Cnt) = c;
+    p->Cnt = Cnt + 1u;
+    p->ReturnValue++;
+  }
+  //
+  // Write part of string, when the buffer is full
+  //
+  if (p->Cnt == p->BufferSize) {
+    if (SEGGER_RTT_Write(p->RTTBufferIndex, p->pBuffer, p->Cnt) != p->Cnt) {
+      p->ReturnValue = -1;
+    } else {
+      p->Cnt = 0u;
+    }
+  }
+}
+
+/*********************************************************************
+*
+*       _PrintUnsigned
+*/
+static void _PrintUnsigned(SEGGER_RTT_PRINTF_DESC * pBufferDesc, unsigned v, unsigned Base, unsigned NumDigits, unsigned FieldWidth, unsigned FormatFlags) {
+  static const char _aV2C[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
+  unsigned Div;
+  unsigned Digit;
+  unsigned Number;
+  unsigned Width;
+  char c;
+
+  Number = v;
+  Digit = 1u;
+  //
+  // Get actual field width
+  //
+  Width = 1u;
+  while (Number >= Base) {
+    Number = (Number / Base);
+    Width++;
+  }
+  if (NumDigits > Width) {
+    Width = NumDigits;
+  }
+  //
+  // Print leading chars if necessary
+  //
+  if ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u) {
+    if (FieldWidth != 0u) {
+      if (((FormatFlags & FORMAT_FLAG_PAD_ZERO) == FORMAT_FLAG_PAD_ZERO) && (NumDigits == 0u)) {
+        c = '0';
+      } else {
+        c = ' ';
+      }
+      while ((FieldWidth != 0u) && (Width < FieldWidth)) {
+        FieldWidth--;
+        _StoreChar(pBufferDesc, c);
+        if (pBufferDesc->ReturnValue < 0) {
+          break;
+        }
+      }
+    }
+  }
+  if (pBufferDesc->ReturnValue >= 0) {
+    //
+    // Compute Digit.
+    // Loop until Digit has the value of the highest digit required.
+    // Example: If the output is 345 (Base 10), loop 2 times until Digit is 100.
+    //
+    while (1) {
+      if (NumDigits > 1u) {       // User specified a min number of digits to print? => Make sure we loop at least that often, before checking anything else (> 1 check avoids problems with NumDigits being signed / unsigned)
+        NumDigits--;
+      } else {
+        Div = v / Digit;
+        if (Div < Base) {        // Is our divider big enough to extract the highest digit from value? => Done
+          break;
+        }
+      }
+      Digit *= Base;
+    }
+    //
+    // Output digits
+    //
+    do {
+      Div = v / Digit;
+      v -= Div * Digit;
+      _StoreChar(pBufferDesc, _aV2C[Div]);
+      if (pBufferDesc->ReturnValue < 0) {
+        break;
+      }
+      Digit /= Base;
+    } while (Digit);
+    //
+    // Print trailing spaces if necessary
+    //
+    if ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == FORMAT_FLAG_LEFT_JUSTIFY) {
+      if (FieldWidth != 0u) {
+        while ((FieldWidth != 0u) && (Width < FieldWidth)) {
+          FieldWidth--;
+          _StoreChar(pBufferDesc, ' ');
+          if (pBufferDesc->ReturnValue < 0) {
+            break;
+          }
+        }
+      }
+    }
+  }
+}
+
+/*********************************************************************
+*
+*       _PrintInt
+*/
+static void _PrintInt(SEGGER_RTT_PRINTF_DESC * pBufferDesc, int v, unsigned Base, unsigned NumDigits, unsigned FieldWidth, unsigned FormatFlags) {
+  unsigned Width;
+  int Number;
+
+  Number = (v < 0) ? -v : v;
+
+  //
+  // Get actual field width
+  //
+  Width = 1u;
+  while (Number >= (int)Base) {
+    Number = (Number / (int)Base);
+    Width++;
+  }
+  if (NumDigits > Width) {
+    Width = NumDigits;
+  }
+  if ((FieldWidth > 0u) && ((v < 0) || ((FormatFlags & FORMAT_FLAG_PRINT_SIGN) == FORMAT_FLAG_PRINT_SIGN))) {
+    FieldWidth--;
+  }
+
+  //
+  // Print leading spaces if necessary
+  //
+  if ((((FormatFlags & FORMAT_FLAG_PAD_ZERO) == 0u) || (NumDigits != 0u)) && ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u)) {
+    if (FieldWidth != 0u) {
+      while ((FieldWidth != 0u) && (Width < FieldWidth)) {
+        FieldWidth--;
+        _StoreChar(pBufferDesc, ' ');
+        if (pBufferDesc->ReturnValue < 0) {
+          break;
+        }
+      }
+    }
+  }
+  //
+  // Print sign if necessary
+  //
+  if (pBufferDesc->ReturnValue >= 0) {
+    if (v < 0) {
+      v = -v;
+      _StoreChar(pBufferDesc, '-');
+    } else if ((FormatFlags & FORMAT_FLAG_PRINT_SIGN) == FORMAT_FLAG_PRINT_SIGN) {
+      _StoreChar(pBufferDesc, '+');
+    } else {
+
+    }
+    if (pBufferDesc->ReturnValue >= 0) {
+      //
+      // Print leading zeros if necessary
+      //
+      if (((FormatFlags & FORMAT_FLAG_PAD_ZERO) == FORMAT_FLAG_PAD_ZERO) && ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u) && (NumDigits == 0u)) {
+        if (FieldWidth != 0u) {
+          while ((FieldWidth != 0u) && (Width < FieldWidth)) {
+            FieldWidth--;
+            _StoreChar(pBufferDesc, '0');
+            if (pBufferDesc->ReturnValue < 0) {
+              break;
+            }
+          }
+        }
+      }
+      if (pBufferDesc->ReturnValue >= 0) {
+        //
+        // Print number without sign
+        //
+        _PrintUnsigned(pBufferDesc, (unsigned)v, Base, NumDigits, FieldWidth, FormatFlags);
+      }
+    }
+  }
+}
+
+/*********************************************************************
+*
+*       Public code
+*
+**********************************************************************
+*/
+/*********************************************************************
+*
+*       SEGGER_RTT_vprintf
+*
+*  Function description
+*    Stores a formatted string in SEGGER RTT control block.
+*    This data is read by the host.
+*
+*  Parameters
+*    BufferIndex  Index of "Up"-buffer to be used. (e.g. 0 for "Terminal")
+*    sFormat      Pointer to format string
+*    pParamList   Pointer to the list of arguments for the format string
+*
+*  Return values
+*    >= 0:  Number of bytes which have been stored in the "Up"-buffer.
+*     < 0:  Error
+*/
+int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList) {
+  char c;
+  SEGGER_RTT_PRINTF_DESC BufferDesc;
+  int v;
+  unsigned NumDigits;
+  unsigned FormatFlags;
+  unsigned FieldWidth;
+  char acBuffer[SEGGER_RTT_PRINTF_BUFFER_SIZE];
+
+  BufferDesc.pBuffer        = acBuffer;
+  BufferDesc.BufferSize     = SEGGER_RTT_PRINTF_BUFFER_SIZE;
+  BufferDesc.Cnt            = 0u;
+  BufferDesc.RTTBufferIndex = BufferIndex;
+  BufferDesc.ReturnValue    = 0;
+
+  do {
+    c = *sFormat;
+    sFormat++;
+    if (c == 0u) {
+      break;
+    }
+    if (c == '%') {
+      //
+      // Filter out flags
+      //
+      FormatFlags = 0u;
+      v = 1;
+      do {
+        c = *sFormat;
+        switch (c) {
+        case '-': FormatFlags |= FORMAT_FLAG_LEFT_JUSTIFY; sFormat++; break;
+        case '0': FormatFlags |= FORMAT_FLAG_PAD_ZERO;     sFormat++; break;
+        case '+': FormatFlags |= FORMAT_FLAG_PRINT_SIGN;   sFormat++; break;
+        case '#': FormatFlags |= FORMAT_FLAG_ALTERNATE;    sFormat++; break;
+        default:  v = 0; break;
+        }
+      } while (v);
+      //
+      // filter out field with
+      //
+      FieldWidth = 0u;
+      do {
+        c = *sFormat;
+        if ((c < '0') || (c > '9')) {
+          break;
+        }
+        sFormat++;
+        FieldWidth = (FieldWidth * 10u) + ((unsigned)c - '0');
+      } while (1);
+
+      //
+      // Filter out precision (number of digits to display)
+      //
+      NumDigits = 0u;
+      c = *sFormat;
+      if (c == '.') {
+        sFormat++;
+        do {
+          c = *sFormat;
+          if ((c < '0') || (c > '9')) {
+            break;
+          }
+          sFormat++;
+          NumDigits = NumDigits * 10u + ((unsigned)c - '0');
+        } while (1);
+      }
+      //
+      // Filter out length modifier
+      //
+      c = *sFormat;
+      do {
+        if ((c == 'l') || (c == 'h')) {
+          sFormat++;
+          c = *sFormat;
+        } else {
+          break;
+        }
+      } while (1);
+      //
+      // Handle specifiers
+      //
+      switch (c) {
+      case 'c': {
+        char c0;
+        v = va_arg(*pParamList, int);
+        c0 = (char)v;
+        _StoreChar(&BufferDesc, c0);
+        break;
+      }
+      case 'd':
+        v = va_arg(*pParamList, int);
+        _PrintInt(&BufferDesc, v, 10u, NumDigits, FieldWidth, FormatFlags);
+        break;
+      case 'u':
+        v = va_arg(*pParamList, int);
+        _PrintUnsigned(&BufferDesc, (unsigned)v, 10u, NumDigits, FieldWidth, FormatFlags);
+        break;
+      case 'x':
+      case 'X':
+        v = va_arg(*pParamList, int);
+        _PrintUnsigned(&BufferDesc, (unsigned)v, 16u, NumDigits, FieldWidth, FormatFlags);
+        break;
+      case 's':
+        {
+          const char * s = va_arg(*pParamList, const char *);
+          do {
+            c = *s;
+            s++;
+            if (c == '\0') {
+              break;
+            }
+           _StoreChar(&BufferDesc, c);
+          } while (BufferDesc.ReturnValue >= 0);
+        }
+        break;
+      case 'p':
+        v = va_arg(*pParamList, int);
+        _PrintUnsigned(&BufferDesc, (unsigned)v, 16u, 8u, 8u, 0u);
+        break;
+      case '%':
+        _StoreChar(&BufferDesc, '%');
+        break;
+      default:
+        break;
+      }
+      sFormat++;
+    } else {
+      _StoreChar(&BufferDesc, c);
+    }
+  } while (BufferDesc.ReturnValue >= 0);
+
+  if (BufferDesc.ReturnValue > 0) {
+    //
+    // Write remaining data, if any
+    //
+    if (BufferDesc.Cnt != 0u) {
+      SEGGER_RTT_Write(BufferIndex, acBuffer, BufferDesc.Cnt);
+    }
+    BufferDesc.ReturnValue += (int)BufferDesc.Cnt;
+  }
+  return BufferDesc.ReturnValue;
+}
+
+/*********************************************************************
+*
+*       SEGGER_RTT_printf
+*
+*  Function description
+*    Stores a formatted string in SEGGER RTT control block.
+*    This data is read by the host.
+*
+*  Parameters
+*    BufferIndex  Index of "Up"-buffer to be used. (e.g. 0 for "Terminal")
+*    sFormat      Pointer to format string, followed by the arguments for conversion
+*
+*  Return values
+*    >= 0:  Number of bytes which have been stored in the "Up"-buffer.
+*     < 0:  Error
+*
+*  Notes
+*    (1) Conversion specifications have following syntax:
+*          %[flags][FieldWidth][.Precision]ConversionSpecifier
+*    (2) Supported flags:
+*          -: Left justify within the field width
+*          +: Always print sign extension for signed conversions
+*          0: Pad with 0 instead of spaces. Ignored when using '-'-flag or precision
+*        Supported conversion specifiers:
+*          c: Print the argument as one char
+*          d: Print the argument as a signed integer
+*          u: Print the argument as an unsigned integer
+*          x: Print the argument as an hexadecimal integer
+*          s: Print the string pointed to by the argument
+*          p: Print the argument as an 8-digit hexadecimal integer. (Argument shall be a pointer to void.)
+*/
+int SEGGER_RTT_printf(unsigned BufferIndex, const char * sFormat, ...) {
+  int r;
+  va_list ParamList;
+
+  va_start(ParamList, sFormat);
+  r = SEGGER_RTT_vprintf(BufferIndex, sFormat, &ParamList);
+  va_end(ParamList);
+  return r;
+}
+/*************************** End of file ****************************/


[07/10] incubator-mynewt-core git commit: sys/console: fix newtmgr over shell

Posted by ma...@apache.org.
sys/console: fix newtmgr over shell


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

Branch: refs/heads/master
Commit: c7b6f80d4e3a5165f84a4c078c872b4a0526d6fb
Parents: 27ff319
Author: Micha\u0142 Narajowski <mi...@codecoup.pl>
Authored: Mon Apr 24 16:13:40 2017 -0700
Committer: Micha\u0142 Narajowski <mi...@codecoup.pl>
Committed: Tue Apr 25 17:49:15 2017 -0700

----------------------------------------------------------------------
 sys/console/full/src/console.c | 88 ++++++++++++++++++++++++++++++++++---
 sys/console/full/syscfg.yml    |  3 ++
 2 files changed, 86 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c7b6f80d/sys/console/full/src/console.c
----------------------------------------------------------------------
diff --git a/sys/console/full/src/console.c b/sys/console/full/src/console.c
index f4a0257..1bc4c3e 100644
--- a/sys/console/full/src/console.c
+++ b/sys/console/full/src/console.c
@@ -51,6 +51,16 @@
 #define ESC_ANSI_VAL    (1 << 3)
 #define ESC_ANSI_VAL_2  (1 << 4)
 
+#define CONSOLE_NLIP_PKT_START1 (6)
+#define CONSOLE_NLIP_PKT_START2 (9)
+#define CONSOLE_NLIP_DATA_START1 (4)
+#define CONSOLE_NLIP_DATA_START2 (20)
+
+#define NLIP_PKT_START1  (1 << 0)
+#define NLIP_PKT_START2  (1 << 1)
+#define NLIP_DATA_START1 (1 << 2)
+#define NLIP_DATA_START2 (1 << 3)
+
 /* Indicates whether the previous line of output was completed. */
 int console_is_midline;
 
@@ -64,7 +74,8 @@ static struct os_eventq compat_lines_queue;
 #endif
 
 static int esc_state;
-static int echo = 1;
+static int nlip_state;
+static int echo = MYNEWT_VAL(CONSOLE_ECHO);
 static unsigned int ansi_val, ansi_val_2;
 
 static uint8_t cur, end;
@@ -161,6 +172,10 @@ insert_char(char *pos, char c, uint8_t end)
 {
     char tmp;
 
+    if (cur + end >= MYNEWT_VAL(CONSOLE_MAX_INPUT_LEN) - 1) {
+        return;
+    }
+
     if (echo) {
         /* Echo back to console */
         console_out(c);
@@ -302,6 +317,31 @@ ansi_cmd:
     esc_state &= ~ESC_ANSI;
 }
 
+static int
+handle_nlip(uint8_t byte)
+{
+    if (((nlip_state & NLIP_PKT_START1) &&
+         (nlip_state & NLIP_PKT_START2)) ||
+        ((nlip_state & NLIP_DATA_START1) &&
+         (nlip_state & NLIP_DATA_START2)))
+    {
+        return 1;
+    }
+
+    if ((nlip_state & NLIP_PKT_START1) &&
+        (byte == CONSOLE_NLIP_PKT_START2)) {
+        nlip_state |= NLIP_PKT_START2;
+        return 1;
+    } else if ((nlip_state & NLIP_DATA_START1) &&
+               (byte == CONSOLE_NLIP_DATA_START2)) {
+        nlip_state |= NLIP_DATA_START2;
+        return 1;
+    } else {
+        nlip_state = 0;
+        return 0;
+    }
+}
+
 int
 console_handle_char(uint8_t byte)
 {
@@ -323,6 +363,41 @@ console_handle_char(uint8_t byte)
         input = ev->ev_arg;
     }
 
+    if (handle_nlip(byte))  {
+        if (byte == '\n') {
+            insert_char(&input->line[cur++], byte, end);
+            input->line[cur] = '\0';
+            cur = 0;
+            end = 0;
+            os_eventq_put(lines_queue, ev);
+            nlip_state = 0;
+
+#if MYNEWT_VAL(CONSOLE_COMPAT)
+            if (console_compat_rx_cb) {
+                console_compat_rx_cb();
+            }
+#endif
+
+            input = NULL;
+            ev = NULL;
+            console_echo(1);
+            return 0;
+        /* Ignore characters if there's no more buffer space */
+        } else if (byte == CONSOLE_NLIP_PKT_START2) {
+            /* Disable echo to not flood the UART */
+            console_echo(0);
+            insert_char(&input->line[cur++], CONSOLE_NLIP_PKT_START1, end);
+        } else if (byte == CONSOLE_NLIP_DATA_START2) {
+            /* Disable echo to not flood the UART */
+            console_echo(0);
+            insert_char(&input->line[cur++], CONSOLE_NLIP_DATA_START1, end);
+        }
+
+        insert_char(&input->line[cur++], byte, end);
+
+        return 0;
+    }
+
     /* Handle ANSI escape mode */
     if (esc_state & ESC_ANSI) {
         handle_ansi(byte, input->line);
@@ -349,6 +424,12 @@ console_handle_char(uint8_t byte)
     if (!isprint(byte)) {
         handle_ansi(byte, input->line);
         switch (byte) {
+        case CONSOLE_NLIP_PKT_START1:
+            nlip_state |= NLIP_PKT_START1;
+            break;
+        case CONSOLE_NLIP_DATA_START1:
+            nlip_state |= NLIP_DATA_START1;
+            break;
         case DEL:
             if (cur > 0) {
                 del_char(&input->line[--cur], end);
@@ -388,10 +469,7 @@ console_handle_char(uint8_t byte)
         return 0;
     }
 
-    /* Ignore characters if there's no more buffer space */
-    if (cur + end < sizeof(input->line) - 1) {
-        insert_char(&input->line[cur++], byte, end);
-    }
+    insert_char(&input->line[cur++], byte, end);
     return 0;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c7b6f80d/sys/console/full/syscfg.yml
----------------------------------------------------------------------
diff --git a/sys/console/full/syscfg.yml b/sys/console/full/syscfg.yml
index 45fc2ed..402892d 100644
--- a/sys/console/full/syscfg.yml
+++ b/sys/console/full/syscfg.yml
@@ -31,6 +31,9 @@ syscfg.defs:
     CONSOLE_TICKS:
         description: 'Print OS Ticks'
         value: 1
+    CONSOLE_ECHO:
+        description: 'Default console echo'
+        value: 1
     CONSOLE_COMPAT:
         description: 'Console backward compatibility'
         value: 1


[06/10] incubator-mynewt-core git commit: sys/console: migrated to new console functionality

Posted by ma...@apache.org.
sys/console: migrated to new console functionality

This patch brings improved console functionality.
Some of the features:
* support for different console backends; UART and RTT for now
* improved ANSI codes handling
* backward compatible API, so it doesn't break existing apps
although the old API is deprecated and will be deleted in the future
* support for tab completion, which will be added later
* ability to compile out input handling code to save flash


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/4863008f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/4863008f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/4863008f

Branch: refs/heads/master
Commit: 4863008f847b25d86a7f180d7a7635682bf03420
Parents: f7acf77
Author: Micha\u0142 Narajowski <mi...@codecoup.pl>
Authored: Thu Apr 20 07:51:19 2017 +0200
Committer: Micha\u0142 Narajowski <mi...@codecoup.pl>
Committed: Tue Apr 25 17:49:15 2017 -0700

----------------------------------------------------------------------
 sys/console/full/include/console/console.h    |  18 +-
 sys/console/full/include/console/prompt.h     |  21 +-
 sys/console/full/pkg.yml                      |   3 +
 sys/console/full/src/cons_fmt.c               | 112 ----
 sys/console/full/src/cons_tty.c               | 574 ---------------------
 sys/console/full/src/console.c                | 447 ++++++++++++++++
 sys/console/full/src/console_fmt.c            | 103 ++++
 sys/console/full/src/console_priv.h           |  37 ++
 sys/console/full/src/prompt.c                 |  57 --
 sys/console/full/src/rtt_console.c            |  96 ++++
 sys/console/full/src/ticks.c                  |   2 +-
 sys/console/full/src/uart_console.c           | 193 +++++++
 sys/console/full/syscfg.yml                   |  42 +-
 sys/console/minimal/include/console/console.h |  63 ---
 sys/console/minimal/include/console/prompt.h  |  55 --
 sys/console/minimal/include/console/ticks.h   |  51 --
 sys/console/minimal/pkg.yml                   |  33 --
 sys/console/minimal/src/cons_tty.c            | 253 ---------
 sys/console/minimal/syscfg.yml                |  33 --
 sys/console/stub/include/console/console.h    |  37 +-
 20 files changed, 970 insertions(+), 1260 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/full/include/console/console.h
----------------------------------------------------------------------
diff --git a/sys/console/full/include/console/console.h b/sys/console/full/include/console/console.h
index c7ae04c..0f37b79 100644
--- a/sys/console/full/include/console/console.h
+++ b/sys/console/full/include/console/console.h
@@ -16,28 +16,44 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+
 #ifndef __CONSOLE_H__
 #define __CONSOLE_H__
 
-#include <stdarg.h>
+#include <inttypes.h>
+#include "syscfg/syscfg.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+struct os_eventq;
+
+struct console_input {
+    char line[MYNEWT_VAL(CONSOLE_MAX_INPUT_LEN)];
+};
+
 typedef void (*console_rx_cb)(void);
 
 int console_init(console_rx_cb rx_cb);
 int console_is_init(void);
 void console_write(const char *str, int cnt);
+#if MYNEWT_VAL(CONSOLE_COMPAT)
 int console_read(char *str, int cnt, int *newline);
+#endif
 void console_blocking_mode(void);
 void console_echo(int on);
 
 int console_printf(const char *fmt, ...)
     __attribute__ ((format (printf, 1, 2)));;
 
+void console_set_queues(struct os_eventq *avail_queue,
+                        struct os_eventq *cmd_queue);
+void console_set_completion_cb(uint8_t (*completion)(char *str, uint8_t len));
+int console_handle_char(uint8_t byte);
+
 extern int console_is_midline;
+extern int console_out(int character);
 
 #ifdef __cplusplus
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/full/include/console/prompt.h
----------------------------------------------------------------------
diff --git a/sys/console/full/include/console/prompt.h b/sys/console/full/include/console/prompt.h
index d4bf8bf..ab5518b 100644
--- a/sys/console/full/include/console/prompt.h
+++ b/sys/console/full/include/console/prompt.h
@@ -28,13 +28,26 @@ extern "C" {
 #endif
 
 /* print console prompt */
-void console_print_prompt();
+static void inline
+console_print_prompt(void)
+{
+}
+
 /* set the console prompt character */
-void console_set_prompt(char);
+static void inline
+console_set_prompt(char ch)
+{
+}
 
-void console_no_prompt(void);
+static void inline
+console_no_prompt(void)
+{
+}
 
-void console_yes_prompt(void);
+static void inline
+console_yes_prompt(void)
+{
+}
 
 #ifdef __cplusplus
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/full/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/console/full/pkg.yml b/sys/console/full/pkg.yml
index e56c7fd..d69fbb0 100644
--- a/sys/console/full/pkg.yml
+++ b/sys/console/full/pkg.yml
@@ -26,7 +26,10 @@ pkg.keywords:
 pkg.deps:
     - hw/hal
     - kernel/os
+pkg.deps.CONSOLE_UART:
     - hw/drivers/uart
+pkg.deps.CONSOLE_RTT:
+    - hw/drivers/rtt
 pkg.apis: console
 
 pkg.init:

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/full/src/cons_fmt.c
----------------------------------------------------------------------
diff --git a/sys/console/full/src/cons_fmt.c b/sys/console/full/src/cons_fmt.c
deleted file mode 100644
index cd848c6..0000000
--- a/sys/console/full/src/cons_fmt.c
+++ /dev/null
@@ -1,112 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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 <stdarg.h>
-#include <stdio.h>
-#include "syscfg/syscfg.h"
-#include "console/console.h"
-#include "console/ticks.h"
-#include "os/os_time.h"
-
-#define CONS_OUTPUT_MAX_LINE	128
-
-
-#if MYNEWT_VAL(BASELIBC_PRESENT)
-size_t console_file_write(FILE *p, const char *str, size_t cnt);
-
-static const struct File_methods console_file_ops = {
-    .write = console_file_write,
-    .read = NULL
-};
-
-static const FILE console_file = {
-    .vmt = &console_file_ops
-};
-
-/**
- * Prints the specified format string to the console.
- *
- * @return                      The number of characters that would have been
- *                                  printed if the console buffer were
- *                                  unlimited.  This return value is analogous
- *                                  to that of snprintf.
- */
-int
-console_printf(const char *fmt, ...)
-{
-    va_list args;
-    int num_chars;
-
-    num_chars = 0;
-
-    if (console_get_ticks()) {
-        /* Prefix each line with a timestamp. */
-        if (!console_is_midline) {
-            num_chars += fprintf((FILE *)&console_file, "%lu:",
-                                 (unsigned long)os_time_get());
-        }
-    }
-    va_start(args, fmt);
-    num_chars += vfprintf((FILE *)&console_file, fmt, args);
-    va_end(args);
-
-    return num_chars;
-}
-
-#else
-
-/**
- * Prints the specified format string to the console.
- *
- * @return                      The number of characters that would have been
- *                                  printed if the console buffer were
- *                                  unlimited.  This return value is analogous
- *                                  to that of snprintf.
- */
-int
-console_printf(const char *fmt, ...)
-{
-    va_list args;
-    char buf[CONS_OUTPUT_MAX_LINE];
-    int num_chars;
-    int len;
-
-    num_chars = 0;
-
-    if (console_get_ticks()) {
-        /* Prefix each line with a timestamp. */
-        if (!console_is_midline) {
-            len = snprintf(buf, sizeof(buf), "%lu:",
-                           (unsigned long)os_time_get());
-            num_chars += len;
-            console_write(buf, len);
-        }
-    }
-
-    va_start(args, fmt);
-    len = vsnprintf(buf, sizeof(buf), fmt, args);
-    num_chars += len;
-    if (len >= sizeof(buf)) {
-        len = sizeof(buf) - 1;
-    }
-    console_write(buf, len);
-    va_end(args);
-
-    return num_chars;
-}
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/full/src/cons_tty.c
----------------------------------------------------------------------
diff --git a/sys/console/full/src/cons_tty.c b/sys/console/full/src/cons_tty.c
deleted file mode 100644
index 50870f1..0000000
--- a/sys/console/full/src/cons_tty.c
+++ /dev/null
@@ -1,574 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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 <inttypes.h>
-#include <assert.h>
-#include "syscfg/syscfg.h"
-#include "sysinit/sysinit.h"
-#include "os/os.h"
-#include "uart/uart.h"
-#include "bsp/bsp.h"
-
-#include "console/console.h"
-#include "console/prompt.h"
-
-/** Indicates whether the previous line of output was completed. */
-int console_is_midline;
-
-#define CONSOLE_RX_CHUNK        16
-
-#if MYNEWT_VAL(CONSOLE_HIST_ENABLE)
-#define CONSOLE_HIST_SZ         32
-#endif
-
-#define CONSOLE_DEL             0x7f    /* del character */
-#define CONSOLE_ESC             0x1b    /* esc character */
-#define CONSOLE_LEFT            'D'     /* esc-[-D emitted when moving left */
-#define CONSOLE_UP              'A'     /* esc-[-A moving up */
-#define CONSOLE_RIGHT           'C'     /* esc-[-C moving right */
-#define CONSOLE_DOWN            'B'     /* esc-[-B moving down */
-
-#define CONSOLE_HEAD_INC(cr)    (((cr)->cr_head + 1) & ((cr)->cr_size - 1))
-#define CONSOLE_TAIL_INC(cr)    (((cr)->cr_tail + 1) & ((cr)->cr_size - 1))
-
-typedef void (*console_write_char)(char);
-void console_print_prompt(void);
-
-struct console_ring {
-    uint8_t cr_head;
-    uint8_t cr_tail;
-    uint16_t cr_size;
-    uint8_t *cr_buf;
-};
-
-struct console_tty {
-    struct uart_dev *ct_dev;
-
-    struct console_ring ct_tx;
-    /* must be after console_ring */
-    uint8_t ct_tx_buf[MYNEWT_VAL(CONSOLE_TX_BUF_SIZE)];
-
-    struct console_ring ct_rx;
-    /* must be after console_ring */
-    uint8_t ct_rx_buf[MYNEWT_VAL(CONSOLE_RX_BUF_SIZE)];
-
-    console_rx_cb ct_rx_cb; /* callback that input is ready */
-    console_write_char ct_write_char;
-    uint8_t ct_echo_off:1;
-    uint8_t ct_esc_seq:2;
-} console_tty;
-
-#if MYNEWT_VAL(CONSOLE_HIST_ENABLE)
-struct console_hist {
-    uint8_t ch_head;
-    uint8_t ch_tail;
-    uint8_t ch_size;
-    uint8_t ch_curr;
-    uint8_t ch_buf[CONSOLE_HIST_SZ][MYNEWT_VAL(CONSOLE_RX_BUF_SIZE)];
-} console_hist;
-#endif
-
-static void
-console_add_char(struct console_ring *cr, char ch)
-{
-    cr->cr_buf[cr->cr_head] = ch;
-    cr->cr_head = CONSOLE_HEAD_INC(cr);
-}
-
-static uint8_t
-console_pull_char(struct console_ring *cr)
-{
-    uint8_t ch;
-
-    ch = cr->cr_buf[cr->cr_tail];
-    cr->cr_tail = CONSOLE_TAIL_INC(cr);
-    return ch;
-}
-
-static int
-console_pull_char_head(struct console_ring *cr)
-{
-    if (cr->cr_head != cr->cr_tail) {
-        cr->cr_head = (cr->cr_head - 1) & (cr->cr_size - 1);
-        return 0;
-    } else {
-        return -1;
-    }
-}
-
-static void
-console_queue_char(char ch)
-{
-    struct console_tty *ct = &console_tty;
-    int sr;
-
-    OS_ENTER_CRITICAL(sr);
-    while (CONSOLE_HEAD_INC(&ct->ct_tx) == ct->ct_tx.cr_tail) {
-        /* TX needs to drain */
-        uart_start_tx(ct->ct_dev);
-        OS_EXIT_CRITICAL(sr);
-        if (os_started()) {
-            os_time_delay(1);
-        }
-        OS_ENTER_CRITICAL(sr);
-    }
-    console_add_char(&ct->ct_tx, ch);
-    OS_EXIT_CRITICAL(sr);
-}
-
-#if MYNEWT_VAL(CONSOLE_HIST_ENABLE)
-static void
-console_hist_init(void)
-{
-    struct console_hist *ch = &console_hist;
-
-    ch->ch_head = 0;
-    ch->ch_tail = 0;
-    ch->ch_curr = 0;
-    ch->ch_size = CONSOLE_HIST_SZ;
-}
-
-static void
-console_hist_add(struct console_ring *rx)
-{
-    struct console_hist *ch = &console_hist;
-    uint8_t *str = ch->ch_buf[ch->ch_head];
-    uint8_t tail;
-    uint8_t empty = 1;
-
-    tail = rx->cr_tail;
-    while (tail != rx->cr_head) {
-        *str = rx->cr_buf[tail];
-        if (*str != ' ' && *str != '\t' && *str != '\n') {
-            empty = 0;
-        }
-        if (*str == '\n') {
-            *str = '\0';
-            /* don't save empty history */
-            if (empty) {
-                return;
-            }
-            break;
-        }
-        str++;
-        tail = (tail + 1) % MYNEWT_VAL(CONSOLE_RX_BUF_SIZE);
-    }
-
-    ch->ch_head = (ch->ch_head + 1) & (ch->ch_size - 1);
-    ch->ch_curr = ch->ch_head;
-
-    /* buffer full, start overwriting old history */
-    if (ch->ch_head == ch->ch_tail) {
-        ch->ch_tail = (ch->ch_tail + 1) & (ch->ch_size - 1);
-    }
-}
-
-static int
-console_hist_move(struct console_ring *rx, uint8_t *tx_buf, uint8_t direction)
-{
-    struct console_hist *ch = &console_hist;
-    uint8_t *str = NULL;
-    int space = 0;
-    int i;
-    uint8_t limit = direction == CONSOLE_UP ? ch->ch_tail : ch->ch_head;
-
-    /* no more history to return in this direction */
-    if (ch->ch_curr == limit) {
-        return 0;
-    }
-
-    if (direction == CONSOLE_UP) {
-        ch->ch_curr = (ch->ch_curr - 1) & (ch->ch_size - 1);
-    } else {
-        ch->ch_curr = (ch->ch_curr + 1) & (ch->ch_size - 1);
-    }
-
-    /* consume all chars */
-    while (console_pull_char_head(rx) == 0) {
-        /* do nothing */
-    }
-
-    str = ch->ch_buf[ch->ch_curr];
-    for (i = 0; i < MYNEWT_VAL(CONSOLE_RX_BUF_SIZE); ++i) {
-        if (str[i] == '\0') {
-            break;
-        }
-        tx_buf[i] = str[i];
-        console_add_char(rx, str[i]);
-        space++;
-    }
-
-    return space;
-}
-#endif
-
-static void
-console_blocking_tx(char ch)
-{
-    struct console_tty *ct = &console_tty;
-
-    uart_blocking_tx(ct->ct_dev, ch);
-}
-
-/*
- * Flush cnt characters from console output queue.
- */
-static void
-console_tx_flush(struct console_tty *ct, int cnt)
-{
-    int i;
-    uint8_t byte;
-
-    for (i = 0; i < cnt; i++) {
-        if (ct->ct_tx.cr_head == ct->ct_tx.cr_tail) {
-            /*
-             * Queue is empty.
-             */
-            break;
-        }
-        byte = console_pull_char(&ct->ct_tx);
-        console_blocking_tx(byte);
-    }
-}
-
-void
-console_blocking_mode(void)
-{
-    struct console_tty *ct = &console_tty;
-    int sr;
-
-    OS_ENTER_CRITICAL(sr);
-    if (ct->ct_write_char) {
-        ct->ct_write_char = console_blocking_tx;
-
-        console_tx_flush(ct, MYNEWT_VAL(CONSOLE_TX_BUF_SIZE));
-    }
-    OS_EXIT_CRITICAL(sr);
-}
-
-void
-console_echo(int on)
-{
-    struct console_tty *ct = &console_tty;
-
-    ct->ct_echo_off = !on;
-}
-
-size_t
-console_file_write(void *arg, const char *str, size_t cnt)
-{
-    struct console_tty *ct = &console_tty;
-    int i;
-
-    if (!ct->ct_write_char) {
-        return cnt;
-    }
-    for (i = 0; i < cnt; i++) {
-        if (str[i] == '\n') {
-            ct->ct_write_char('\r');
-        }
-        ct->ct_write_char(str[i]);
-    }
-    if (cnt > 0) {
-        console_is_midline = str[cnt - 1] != '\n';
-    }
-    uart_start_tx(ct->ct_dev);
-    return cnt;
-}
-
-void
-console_write(const char *str, int cnt)
-{
-    console_file_write(NULL, str, cnt);
-}
-
-int
-console_read(char *str, int cnt, int *newline)
-{
-    struct console_tty *ct = &console_tty;
-    struct console_ring *cr = &ct->ct_rx;
-    int sr;
-    int i;
-    uint8_t ch;
-
-    *newline = 0;
-    OS_ENTER_CRITICAL(sr);
-    for (i = 0; i < cnt; i++) {
-        if (cr->cr_head == cr->cr_tail) {
-            break;
-        }
-
-        if ((i & (CONSOLE_RX_CHUNK - 1)) == (CONSOLE_RX_CHUNK - 1)) {
-            /*
-             * Make a break from blocking interrupts during the copy.
-             */
-            OS_EXIT_CRITICAL(sr);
-            OS_ENTER_CRITICAL(sr);
-        }
-
-        ch = console_pull_char(cr);
-        if (ch == '\n') {
-            *str = '\0';
-            *newline = 1;
-            break;
-        }
-        *str++ = ch;
-    }
-    OS_EXIT_CRITICAL(sr);
-    if (i > 0 || *newline) {
-        uart_start_rx(ct->ct_dev);
-    }
-    return i;
-}
-
-/*
- * Interrupts disabled when console_tx_char/console_rx_char are called.
- */
-static int
-console_tx_char(void *arg)
-{
-    struct console_tty *ct = (struct console_tty *)arg;
-    struct console_ring *cr = &ct->ct_tx;
-
-    if (cr->cr_head == cr->cr_tail) {
-        /*
-         * No more data.
-         */
-        return -1;
-    }
-    return console_pull_char(cr);
-}
-
-static int
-console_buf_space(struct console_ring *cr)
-{
-    int space;
-
-    space = (cr->cr_tail - cr->cr_head) & (cr->cr_size - 1);
-    return space - 1;
-}
-
-static int
-console_rx_char(void *arg, uint8_t data)
-{
-    struct console_tty *ct = (struct console_tty *)arg;
-    struct console_ring *tx = &ct->ct_tx;
-    struct console_ring *rx = &ct->ct_rx;
-    int tx_space = 0;
-    int i;
-#if MYNEWT_VAL(CONSOLE_HIST_ENABLE)
-    uint8_t tx_buf[MYNEWT_VAL(CONSOLE_RX_BUF_SIZE)];
-#else
-    uint8_t tx_buf[3];
-#endif
-
-    if (CONSOLE_HEAD_INC(&ct->ct_rx) == ct->ct_rx.cr_tail) {
-        /*
-         * RX queue full. Reader must drain this.
-         */
-        if (ct->ct_rx_cb) {
-            ct->ct_rx_cb();
-        }
-        return -1;
-    }
-
-    /* echo */
-    switch (data) {
-    case '\r':
-    case '\n':
-        /*
-         * linefeed
-         */
-        tx_buf[0] = '\n';
-        tx_buf[1] = '\r';
-        tx_space = 2;
-        console_add_char(rx, '\n');
-#if MYNEWT_VAL(CONSOLE_HIST_ENABLE)
-        console_hist_add(rx);
-#endif
-        if (ct->ct_rx_cb) {
-            ct->ct_rx_cb();
-        }
-        break;
-    case CONSOLE_ESC:
-        ct->ct_esc_seq = 1;
-        goto out;
-    case '[':
-        if (ct->ct_esc_seq == 1) {
-            ct->ct_esc_seq = 2;
-            goto out;
-        } else {
-            goto queue_char;
-        }
-        break;
-    case CONSOLE_LEFT:
-        if (ct->ct_esc_seq == 2) {
-            goto backspace;
-        } else {
-            goto queue_char;
-        }
-        break;
-    case CONSOLE_UP:
-    case CONSOLE_DOWN:
-        if (ct->ct_esc_seq != 2) {
-            goto queue_char;
-        }
-#if MYNEWT_VAL(CONSOLE_HIST_ENABLE)
-        tx_space = console_hist_move(rx, tx_buf, data);
-        tx_buf[tx_space] = 0;
-        ct->ct_esc_seq = 0;
-        /*
-         * when moving up, stop on oldest history entry
-         * when moving down, let it delete input before leaving...
-         */
-        if (data == CONSOLE_UP && tx_space == 0) {
-            goto out;
-        }
-        if (!ct->ct_echo_off) {
-            /* HACK: clean line by backspacing up to maximum possible space */
-            for (i = 0; i < MYNEWT_VAL(CONSOLE_TX_BUF_SIZE); i++) {
-                if (console_buf_space(tx) < 3) {
-                    console_tx_flush(ct, 3);
-                }
-                console_add_char(tx, '\b');
-                console_add_char(tx, ' ');
-                console_add_char(tx, '\b');
-                uart_start_tx(ct->ct_dev);
-            }
-            if (tx_space == 0) {
-                goto out;
-            }
-        } else {
-            goto queue_char;
-        }
-        break;
-#else
-        ct->ct_esc_seq = 0;
-        goto out;
-#endif
-    case CONSOLE_RIGHT:
-        if (ct->ct_esc_seq == 2) {
-            data = ' '; /* add space */
-        }
-        goto queue_char;
-    case '\b':
-    case CONSOLE_DEL:
-backspace:
-        /*
-         * backspace
-         */
-        ct->ct_esc_seq = 0;
-        if (console_pull_char_head(rx) == 0) {
-            /*
-             * Only wipe out char if we can pull stuff off from head.
-             */
-            tx_buf[0] = '\b';
-            tx_buf[1] = ' ';
-            tx_buf[2] = '\b';
-            tx_space = 3;
-        } else {
-            goto out;
-        }
-        break;
-    default:
-queue_char:
-        tx_buf[0] = data;
-        tx_space = 1;
-        ct->ct_esc_seq = 0;
-        console_add_char(rx, data);
-        break;
-    }
-    if (!ct->ct_echo_off) {
-        if (console_buf_space(tx) < tx_space) {
-            console_tx_flush(ct, tx_space);
-        }
-        for (i = 0; i < tx_space; i++) {
-            console_add_char(tx, tx_buf[i]);
-        }
-        uart_start_tx(ct->ct_dev);
-    }
-out:
-    return 0;
-}
-
-int
-console_is_init(void)
-{
-    struct console_tty *ct = &console_tty;
-
-    return (ct->ct_dev != NULL);
-}
-
-static int is_power_of_two (unsigned int x)
-{
-  return ((x != 0) && !(x & (x - 1)));
-}
-
-int
-console_init(console_rx_cb rx_cb)
-{
-    struct console_tty *ct = &console_tty;
-    struct uart_conf uc = {
-        .uc_speed = MYNEWT_VAL(CONSOLE_BAUD),
-        .uc_databits = 8,
-        .uc_stopbits = 1,
-        .uc_parity = UART_PARITY_NONE,
-        .uc_flow_ctl = MYNEWT_VAL(CONSOLE_FLOW_CONTROL),
-        .uc_tx_char = console_tx_char,
-        .uc_rx_char = console_rx_char,
-        .uc_cb_arg = ct
-    };
-
-    ct->ct_rx_cb = rx_cb;
-    if (!ct->ct_dev) {
-        ct->ct_tx.cr_size = MYNEWT_VAL(CONSOLE_TX_BUF_SIZE);
-        ct->ct_tx.cr_buf = ct->ct_tx_buf;
-        ct->ct_rx.cr_size = MYNEWT_VAL(CONSOLE_RX_BUF_SIZE);
-        ct->ct_rx.cr_buf = ct->ct_rx_buf;
-        ct->ct_write_char = console_queue_char;
-
-        ct->ct_dev = (struct uart_dev *)os_dev_open(CONSOLE_UART,
-          OS_TIMEOUT_NEVER, &uc);
-        if (!ct->ct_dev) {
-            return -1;
-        }
-        ct->ct_echo_off = ! MYNEWT_VAL(CONSOLE_ECHO);
-    }
-
-    /* must be a power of 2 */
-    assert(is_power_of_two(MYNEWT_VAL(CONSOLE_RX_BUF_SIZE)));
-
-#if MYNEWT_VAL(CONSOLE_HIST_ENABLE)
-    console_hist_init();
-#endif
-
-    return 0;
-}
-
-void
-console_pkg_init(void)
-{
-    int rc;
-
-    /* Ensure this function only gets called by sysinit. */
-    SYSINIT_ASSERT_ACTIVE();
-
-    rc = console_init(NULL);
-    SYSINIT_PANIC_ASSERT(rc == 0);
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/full/src/console.c
----------------------------------------------------------------------
diff --git a/sys/console/full/src/console.c b/sys/console/full/src/console.c
new file mode 100644
index 0000000..18ff755
--- /dev/null
+++ b/sys/console/full/src/console.c
@@ -0,0 +1,447 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 <assert.h>
+#include <inttypes.h>
+#include <ctype.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "syscfg/syscfg.h"
+#include "os/os.h"
+#include "sysinit/sysinit.h"
+#include "console/console.h"
+#include "console/ticks.h"
+#include "console_priv.h"
+
+/* Control characters */
+#define ESC                0x1b
+#define DEL                0x7f
+
+/* ANSI escape sequences */
+#define ANSI_ESC           '['
+#define ANSI_UP            'A'
+#define ANSI_DOWN          'B'
+#define ANSI_FORWARD       'C'
+#define ANSI_BACKWARD      'D'
+#define ANSI_END           'F'
+#define ANSI_HOME          'H'
+#define ANSI_DEL           '~'
+
+#define ESC_ESC         (1 << 0)
+#define ESC_ANSI        (1 << 1)
+#define ESC_ANSI_FIRST  (1 << 2)
+#define ESC_ANSI_VAL    (1 << 3)
+#define ESC_ANSI_VAL_2  (1 << 4)
+
+/* Indicates whether the previous line of output was completed. */
+int console_is_midline;
+
+#if MYNEWT_VAL(CONSOLE_COMPAT)
+#define CONSOLE_COMPAT_MAX_CMD_QUEUED 1
+static struct console_input buf[CONSOLE_COMPAT_MAX_CMD_QUEUED];
+static struct os_event shell_console_ev[CONSOLE_COMPAT_MAX_CMD_QUEUED];
+static console_rx_cb console_compat_rx_cb; /* callback that input is ready */
+static struct os_eventq compat_avail_queue;
+static struct os_eventq compat_lines_queue;
+#endif
+
+static int esc_state;
+static int echo = 1;
+static unsigned int ansi_val, ansi_val_2;
+
+static uint8_t cur, end;
+static struct os_eventq *avail_queue;
+static struct os_eventq *lines_queue;
+static uint8_t (*completion_cb)(char *line, uint8_t len);
+
+void
+console_echo(int on)
+{
+    echo = on;
+}
+
+void
+console_write(const char *str, int cnt)
+{
+    int i;
+
+    for (i = 0; i < cnt; i++) {
+        if (console_out((int)str[i]) == EOF) {
+            break;
+        }
+    }
+}
+
+#if MYNEWT_VAL(CONSOLE_COMPAT)
+int
+console_read(char *str, int cnt, int *newline)
+{
+    struct os_event *ev;
+    struct console_input *cmd;
+    size_t len;
+
+    *newline = 0;
+    ev = os_eventq_get_no_wait(lines_queue);
+    if (!ev) {
+        return 0;
+    }
+    cmd = ev->ev_arg;
+    len = strlen(cmd->line);
+    strncpy(str, cmd->line, len+1);
+    os_eventq_put(avail_queue, ev);
+    *newline = 1;
+    return len;
+}
+#endif
+
+void
+console_blocking_mode(void)
+{
+#if MYNEWT_VAL(CONSOLE_UART)
+    uart_console_blocking_mode();
+#endif
+}
+
+static inline void
+cursor_forward(unsigned int count)
+{
+    console_printf("\x1b[%uC", count);
+}
+
+static inline void
+cursor_backward(unsigned int count)
+{
+    console_printf("\x1b[%uD", count);
+}
+
+static inline void
+cursor_save(void)
+{
+    console_out(ESC);
+    console_out('[');
+    console_out('s');
+}
+
+static inline void
+cursor_restore(void)
+{
+    console_out(ESC);
+    console_out('[');
+    console_out('u');
+}
+
+static void
+insert_char(char *pos, char c, uint8_t end)
+{
+    char tmp;
+
+    if (echo) {
+        /* Echo back to console */
+        console_out(c);
+    }
+
+    if (end == 0) {
+        *pos = c;
+        return;
+    }
+
+    tmp = *pos;
+    *(pos++) = c;
+
+    cursor_save();
+
+    while (end-- > 0) {
+        console_out(tmp);
+        c = *pos;
+        *(pos++) = tmp;
+        tmp = c;
+    }
+
+    /* Move cursor back to right place */
+    cursor_restore();
+}
+
+static void
+del_char(char *pos, uint8_t end)
+{
+    console_out('\b');
+
+    if (end == 0) {
+        console_out(' ');
+        console_out('\b');
+        return;
+    }
+
+    cursor_save();
+
+    while (end-- > 0) {
+        *pos = *(pos + 1);
+        console_out(*(pos++));
+    }
+
+    console_out(' ');
+
+    /* Move cursor back to right place */
+    cursor_restore();
+}
+
+static void
+handle_ansi(uint8_t byte, char *line)
+{
+    if (esc_state & ESC_ANSI_FIRST) {
+        esc_state &= ~ESC_ANSI_FIRST;
+        if (!isdigit(byte)) {
+            ansi_val = 1;
+            goto ansi_cmd;
+        }
+
+        esc_state |= ESC_ANSI_VAL;
+        ansi_val = byte - '0';
+        ansi_val_2 = 0;
+        return;
+    }
+
+    if (esc_state & ESC_ANSI_VAL) {
+        if (isdigit(byte)) {
+            if (esc_state & ESC_ANSI_VAL_2) {
+                ansi_val_2 *= 10;
+                ansi_val_2 += byte - '0';
+            } else {
+                ansi_val *= 10;
+                ansi_val += byte - '0';
+            }
+            return;
+        }
+
+        /* Multi value sequence, e.g. Esc[Line;ColumnH */
+        if (byte == ';' && !(esc_state & ESC_ANSI_VAL_2)) {
+            esc_state |= ESC_ANSI_VAL_2;
+            return;
+        }
+
+        esc_state &= ~ESC_ANSI_VAL;
+        esc_state &= ~ESC_ANSI_VAL_2;
+    }
+
+ansi_cmd:
+    switch (byte) {
+    case ANSI_BACKWARD:
+        if (ansi_val > cur) {
+            break;
+        }
+
+        end += ansi_val;
+        cur -= ansi_val;
+        cursor_backward(ansi_val);
+        break;
+    case ANSI_FORWARD:
+        if (ansi_val > end) {
+            break;
+        }
+
+        end -= ansi_val;
+        cur += ansi_val;
+        cursor_forward(ansi_val);
+        break;
+    case ANSI_HOME:
+        if (!cur) {
+            break;
+        }
+
+        cursor_backward(cur);
+        end += cur;
+        cur = 0;
+        break;
+    case ANSI_END:
+        if (!end) {
+            break;
+        }
+
+        cursor_forward(end);
+        cur += end;
+        end = 0;
+        break;
+    case ANSI_DEL:
+        if (!end) {
+            break;
+        }
+
+        cursor_forward(1);
+        del_char(&line[cur], --end);
+        break;
+    default:
+        break;
+    }
+
+    esc_state &= ~ESC_ANSI;
+}
+
+int
+console_handle_char(uint8_t byte)
+{
+#if !MYNEWT_VAL(CONSOLE_INPUT)
+    return 0;
+#endif
+
+    static struct os_event *ev;
+    static struct console_input *input;
+
+    if (!avail_queue || !lines_queue) {
+        return 0;
+    }
+
+    if (!ev) {
+        ev = os_eventq_get_no_wait(avail_queue);
+        if (!ev)
+            return 0;
+        input = ev->ev_arg;
+    }
+
+    /* Handle ANSI escape mode */
+    if (esc_state & ESC_ANSI) {
+        handle_ansi(byte, input->line);
+        return 0;
+    }
+
+    /* Handle escape mode */
+    if (esc_state & ESC_ESC) {
+        esc_state &= ~ESC_ESC;
+        handle_ansi(byte, input->line);
+        switch (byte) {
+        case ANSI_ESC:
+            esc_state |= ESC_ANSI;
+            esc_state |= ESC_ANSI_FIRST;
+            break;
+        default:
+            break;
+        }
+
+        return 0;
+    }
+
+    /* Handle special control characters */
+    if (!isprint(byte)) {
+        handle_ansi(byte, input->line);
+        switch (byte) {
+        case DEL:
+            if (cur > 0) {
+                del_char(&input->line[--cur], end);
+            }
+            break;
+        case ESC:
+            esc_state |= ESC_ESC;
+            break;
+        case '\r':
+            input->line[cur + end] = '\0';
+            console_out('\r');
+            console_out('\n');
+            cur = 0;
+            end = 0;
+            os_eventq_put(lines_queue, ev);
+
+#if MYNEWT_VAL(CONSOLE_COMPAT)
+            if (console_compat_rx_cb) {
+                console_compat_rx_cb();
+            }
+#endif
+
+            input = NULL;
+            ev = NULL;
+            break;
+        case '\t':
+            if (completion_cb && !end) {
+                cur += completion_cb(input->line, cur);
+            }
+            break;
+        default:
+            break;
+        }
+
+        return 0;
+    }
+
+    /* Ignore characters if there's no more buffer space */
+    if (cur + end < sizeof(input->line) - 1) {
+        insert_char(&input->line[cur++], byte, end);
+    }
+    return 0;
+}
+
+int
+console_is_init(void)
+{
+#if MYNEWT_VAL(CONSOLE_UART)
+    return uart_console_is_init();
+#endif
+#if MYNEWT_VAL(CONSOLE_RTT)
+    return rtt_console_is_init();
+#endif
+    return 0;
+}
+
+void
+console_set_queues(struct os_eventq *avail, struct os_eventq *lines)
+{
+    avail_queue = avail;
+    lines_queue = lines;
+}
+
+void
+console_set_completion_cb(uint8_t (*completion)(char *str, uint8_t len))
+{
+    completion_cb = completion;
+}
+
+#if MYNEWT_VAL(CONSOLE_COMPAT)
+int
+console_init(console_rx_cb rx_cb)
+{
+    int i;
+
+    os_eventq_init(&compat_lines_queue);
+    os_eventq_init(&compat_avail_queue);
+    console_set_queues(&compat_avail_queue, &compat_lines_queue);
+
+    for (i = 0; i < CONSOLE_COMPAT_MAX_CMD_QUEUED; i++) {
+        shell_console_ev[i].ev_arg = &buf[i];
+        os_eventq_put(avail_queue, &shell_console_ev[i]);
+    }
+    console_compat_rx_cb = rx_cb;
+    return 0;
+}
+#endif
+
+void
+console_pkg_init(void)
+{
+    int rc = 0;
+
+    /* Ensure this function only gets called by sysinit. */
+    SYSINIT_ASSERT_ACTIVE();
+
+#if MYNEWT_VAL(CONSOLE_UART)
+    rc = uart_console_init();
+#endif
+#if MYNEWT_VAL(CONSOLE_RTT)
+    rc = rtt_console_init();
+#endif
+    SYSINIT_PANIC_ASSERT(rc == 0);
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/full/src/console_fmt.c
----------------------------------------------------------------------
diff --git a/sys/console/full/src/console_fmt.c b/sys/console/full/src/console_fmt.c
new file mode 100644
index 0000000..32b68e3
--- /dev/null
+++ b/sys/console/full/src/console_fmt.c
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 <stdarg.h>
+#include <stdio.h>
+#include "syscfg/syscfg.h"
+#include "console/console.h"
+#include "console/ticks.h"
+#include "os/os_time.h"
+
+#define CONS_OUTPUT_MAX_LINE    128
+
+
+#if MYNEWT_VAL(BASELIBC_PRESENT)
+
+/**
+ * Prints the specified format string to the console.
+ *
+ * @return                      The number of characters that would have been
+ *                                  printed if the console buffer were
+ *                                  unlimited.  This return value is analogous
+ *                                  to that of snprintf.
+ */
+int
+console_printf(const char *fmt, ...)
+{
+    va_list args;
+    int num_chars;
+
+    num_chars = 0;
+
+    if (console_get_ticks()) {
+        /* Prefix each line with a timestamp. */
+        if (!console_is_midline) {
+            num_chars += printf("%06lu ", (unsigned long)os_time_get());
+        }
+    }
+
+    va_start(args, fmt);
+    num_chars += vprintf(fmt, args);
+    va_end(args);
+
+    return num_chars;
+}
+
+
+#else
+
+/**
+ * Prints the specified format string to the console.
+ *
+ * @return                      The number of characters that would have been
+ *                                  printed if the console buffer were
+ *                                  unlimited.  This return value is analogous
+ *                                  to that of snprintf.
+ */
+int
+console_printf(const char *fmt, ...)
+{
+    va_list args;
+    char buf[CONS_OUTPUT_MAX_LINE];
+    int num_chars;
+    int len;
+
+    num_chars = 0;
+
+    if (console_get_ticks()) {
+        /* Prefix each line with a timestamp. */
+        if (!console_is_midline) {
+            len = snprintf(buf, sizeof(buf), "%06lu ",
+                           (unsigned long)os_time_get());
+            num_chars += len;
+            console_write(buf, len);
+        }
+    }
+
+    va_start(args, fmt);
+    len = vsnprintf(buf, sizeof(buf), fmt, args);
+    num_chars += len;
+    if (len >= sizeof(buf)) {
+        len = sizeof(buf) - 1;
+    }
+    console_write(buf, len);
+    va_end(args);
+
+    return num_chars;
+}
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/full/src/console_priv.h
----------------------------------------------------------------------
diff --git a/sys/console/full/src/console_priv.h b/sys/console/full/src/console_priv.h
new file mode 100644
index 0000000..d307b5d
--- /dev/null
+++ b/sys/console/full/src/console_priv.h
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+#ifndef __CONSOLE_PRIV_H__
+#define __CONSOLE_PRIV_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int uart_console_is_init(void);
+int uart_console_init(void);
+void uart_console_blocking_mode(void);
+int rtt_console_is_init(void);
+int rtt_console_init(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __CONSOLE_PRIV_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/full/src/prompt.c
----------------------------------------------------------------------
diff --git a/sys/console/full/src/prompt.c b/sys/console/full/src/prompt.c
deleted file mode 100644
index 251c249..0000000
--- a/sys/console/full/src/prompt.c
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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 "console/console.h"
-#include "console/prompt.h"
-#include <syscfg/syscfg.h>
-
-/* console prompt, always followed by a space */
-static char console_prompt[] = " > ";
-static char do_prompt = MYNEWT_VAL(CONSOLE_PROMPT);
-
-
-/* set the prompt character, leave the space */
-void
-console_set_prompt(char p)
-{
-    do_prompt = 1;
-    console_prompt[1] = p;
-}
-
-void
-console_no_prompt(void)
-{
-    do_prompt = 0;
-}
-
-void
-console_yes_prompt(void)
-{
-    do_prompt = 1;
-}
-
-/* print the prompt to the console */
-void
-console_print_prompt(void)
-{
-    if (do_prompt) {
-        console_printf("%s", console_prompt);
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/full/src/rtt_console.c
----------------------------------------------------------------------
diff --git a/sys/console/full/src/rtt_console.c b/sys/console/full/src/rtt_console.c
new file mode 100644
index 0000000..63067c1
--- /dev/null
+++ b/sys/console/full/src/rtt_console.c
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(CONSOLE_RTT)
+#include <ctype.h>
+
+#include "os/os.h"
+#include "os/os_cputime.h"
+#include "rtt/SEGGER_RTT.h"
+#include "console/console.h"
+#include "console_priv.h"
+
+#if MYNEWT_VAL(CONSOLE_INPUT)
+static struct hal_timer rtt_timer;
+#endif
+
+static const char CR = '\r';
+
+int
+console_out(int character)
+{
+    char c = (char)character;
+
+    if ('\n' == c) {
+        SEGGER_RTT_WriteNoLock(0, &CR, 1);
+        console_is_midline = 0;
+    } else {
+        console_is_midline = 1;
+    }
+
+    SEGGER_RTT_WriteNoLock(0, &c, 1);
+
+    return character;
+}
+
+#if MYNEWT_VAL(CONSOLE_INPUT)
+void
+rtt(void *arg)
+{
+    int key;
+    int i = 0;
+    uint32_t timeout;
+
+    key = SEGGER_RTT_GetKey();
+    if (key >= 0) {
+        console_handle_char((char)key);
+        i = 0;
+    }
+    /* These values were selected to keep the shell responsive
+     * and at the same time reduce context switches.
+     * Min sleep is 50ms and max is 250ms.
+     */
+    if (i < 5) {
+        ++i;
+    }
+    timeout = 50000 * i;
+    os_cputime_timer_relative(&rtt_timer, timeout);
+}
+#endif
+
+int
+rtt_console_is_init(void)
+{
+    return 1;
+}
+
+int
+rtt_console_init(void)
+{
+#if MYNEWT_VAL(CONSOLE_INPUT)
+    os_cputime_timer_init(&rtt_timer, rtt, NULL);
+    /* start after a second */
+    os_cputime_timer_relative(&rtt_timer, 1000000);
+#endif
+    return 0;
+}
+
+#endif /* MYNEWT_VAL(CONSOLE_RTT) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/full/src/ticks.c
----------------------------------------------------------------------
diff --git a/sys/console/full/src/ticks.c b/sys/console/full/src/ticks.c
index 1a442aa..06d1c8f 100644
--- a/sys/console/full/src/ticks.c
+++ b/sys/console/full/src/ticks.c
@@ -18,9 +18,9 @@
  */
 
 
+#include "syscfg/syscfg.h"
 #include "console/console.h"
 #include "console/prompt.h"
-#include "syscfg/syscfg.h"
 
 static char do_ticks = MYNEWT_VAL(CONSOLE_TICKS);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/full/src/uart_console.c
----------------------------------------------------------------------
diff --git a/sys/console/full/src/uart_console.c b/sys/console/full/src/uart_console.c
new file mode 100644
index 0000000..44943d2
--- /dev/null
+++ b/sys/console/full/src/uart_console.c
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(CONSOLE_UART)
+#include <ctype.h>
+#include <assert.h>
+
+#include "os/os.h"
+#include "uart/uart.h"
+#include "bsp/bsp.h"
+
+#include "console/console.h"
+#include "console_priv.h"
+
+#define CONSOLE_HEAD_INC(cr)    (((cr)->cr_head + 1) & ((cr)->cr_size - 1))
+#define CONSOLE_TAIL_INC(cr)    (((cr)->cr_tail + 1) & ((cr)->cr_size - 1))
+
+static struct uart_dev *uart_dev;
+static struct console_ring cr_tx;
+/* must be after console_ring */
+static uint8_t cr_tx_buf[MYNEWT_VAL(CONSOLE_UART_TX_BUF_SIZE)];
+typedef void (*console_write_char)(struct uart_dev*, uint8_t);
+static console_write_char write_char_cb;
+
+struct console_ring {
+    uint8_t cr_head;
+    uint8_t cr_tail;
+    uint16_t cr_size;
+    uint8_t *cr_buf;
+};
+
+static void
+console_add_char(struct console_ring *cr, char ch)
+{
+    cr->cr_buf[cr->cr_head] = ch;
+    cr->cr_head = CONSOLE_HEAD_INC(cr);
+}
+
+static uint8_t
+console_pull_char(struct console_ring *cr)
+{
+    uint8_t ch;
+
+    ch = cr->cr_buf[cr->cr_tail];
+    cr->cr_tail = CONSOLE_TAIL_INC(cr);
+    return ch;
+}
+
+static void
+console_queue_char(struct uart_dev *uart_dev, uint8_t ch)
+{
+    int sr;
+
+    OS_ENTER_CRITICAL(sr);
+    while (CONSOLE_HEAD_INC(&cr_tx) == cr_tx.cr_tail) {
+        /* TX needs to drain */
+        uart_start_tx(uart_dev);
+        OS_EXIT_CRITICAL(sr);
+        if (os_started()) {
+            os_time_delay(1);
+        }
+        OS_ENTER_CRITICAL(sr);
+    }
+    console_add_char(&cr_tx, ch);
+    OS_EXIT_CRITICAL(sr);
+}
+
+/*
+ * Flush cnt characters from console output queue.
+ */
+static void
+console_tx_flush(int cnt)
+{
+    int i;
+    uint8_t byte;
+
+    for (i = 0; i < cnt; i++) {
+        if (cr_tx.cr_head == cr_tx.cr_tail) {
+            /*
+             * Queue is empty.
+             */
+            break;
+        }
+        byte = console_pull_char(&cr_tx);
+        uart_blocking_tx(uart_dev, byte);
+    }
+}
+
+void
+uart_console_blocking_mode(void)
+{
+    int sr;
+
+    OS_ENTER_CRITICAL(sr);
+    if (write_char_cb) {
+        write_char_cb = uart_blocking_tx;
+
+        console_tx_flush(MYNEWT_VAL(CONSOLE_UART_TX_BUF_SIZE));
+    }
+    OS_EXIT_CRITICAL(sr);
+}
+
+int
+console_out(int c)
+{
+    if ('\n' == c) {
+        write_char_cb(uart_dev, '\r');
+        console_is_midline = 0;
+    } else {
+        console_is_midline = 1;
+    }
+    write_char_cb(uart_dev, c);
+    uart_start_tx(uart_dev);
+
+    return c;
+}
+
+/*
+ * Interrupts disabled when console_tx_char/console_rx_char are called.
+ * Characters sent only in blocking mode.
+ */
+static int
+console_tx_char(void *arg)
+{
+    if (cr_tx.cr_head == cr_tx.cr_tail) {
+        /*
+         * No more data.
+         */
+        return -1;
+    }
+    return console_pull_char(&cr_tx);
+}
+
+/*
+ * Interrupts disabled when console_tx_char/console_rx_char are called.
+ */
+static int
+console_rx_char(void *arg, uint8_t byte)
+{
+    return console_handle_char(byte);
+}
+
+int
+uart_console_is_init(void)
+{
+    return uart_dev != NULL;
+}
+
+int
+uart_console_init(void)
+{
+    struct uart_conf uc = {
+        .uc_speed = MYNEWT_VAL(CONSOLE_UART_BAUD),
+        .uc_databits = 8,
+        .uc_stopbits = 1,
+        .uc_parity = UART_PARITY_NONE,
+        .uc_flow_ctl = MYNEWT_VAL(CONSOLE_UART_FLOW_CONTROL),
+        .uc_tx_char = console_tx_char,
+        .uc_rx_char = console_rx_char,
+    };
+
+    cr_tx.cr_size = MYNEWT_VAL(CONSOLE_UART_TX_BUF_SIZE);
+    cr_tx.cr_buf = cr_tx_buf;
+    write_char_cb = console_queue_char;
+
+    if (!uart_dev) {
+        uart_dev = (struct uart_dev *)os_dev_open(CONSOLE_UART,
+          OS_TIMEOUT_NEVER, &uc);
+        if (!uart_dev) {
+            return -1;
+        }
+    }
+    return 0;
+}
+#endif /* MYNEWT_VAL(CONSOLE_UART) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/full/syscfg.yml
----------------------------------------------------------------------
diff --git a/sys/console/full/syscfg.yml b/sys/console/full/syscfg.yml
index 68eedaf..45fc2ed 100644
--- a/sys/console/full/syscfg.yml
+++ b/sys/console/full/syscfg.yml
@@ -19,27 +19,31 @@
 # Package: sys/console/full
 
 syscfg.defs:
-    CONSOLE_BAUD:
+    CONSOLE_UART:
+        description: 'Set console output to UART'
+        value: 1
+    CONSOLE_RTT:
+        description: 'Set console output to RTT'
+        value: 0
+    CONSOLE_INPUT:
+        description: 'Enable console input'
+        value: 1
+    CONSOLE_TICKS:
+        description: 'Print OS Ticks'
+        value: 1
+    CONSOLE_COMPAT:
+        description: 'Console backward compatibility'
+        value: 1
+    CONSOLE_MAX_INPUT_LEN:
+        description: 'Maximum input line length'
+        value: 256
+
+    CONSOLE_UART_BAUD:
         description: 'Console UART baud rate.'
         value: '115200'
-    CONSOLE_FLOW_CONTROL:
+    CONSOLE_UART_FLOW_CONTROL:
         description: 'Console UART flow control.'
         value: 'UART_FLOW_CTL_NONE'
-    CONSOLE_TX_BUF_SIZE:
-        description: 'Console transmit buffer size; must be power of 2.'
+    CONSOLE_UART_TX_BUF_SIZE:
+        description: 'UART console transmit buffer size; must be power of 2.'
         value: 32
-    CONSOLE_RX_BUF_SIZE:
-        description: 'Console receive buffer size.'
-        value: 128
-    CONSOLE_TICKS:
-        description: 'Print OS Ticks'
-        value: '1'
-    CONSOLE_PROMPT:
-        description: 'Default console prompt '
-        value: '0'
-    CONSOLE_ECHO:
-        description: 'Default console echo '
-        value: '1'
-    CONSOLE_HIST_ENABLE:
-        description: 'Console history '
-        value: 0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/minimal/include/console/console.h
----------------------------------------------------------------------
diff --git a/sys/console/minimal/include/console/console.h b/sys/console/minimal/include/console/console.h
deleted file mode 100644
index 61f6534..0000000
--- a/sys/console/minimal/include/console/console.h
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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.
- */
-#ifndef __CONSOLE_H__
-#define __CONSOLE_H__
-
-#include <stdarg.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-typedef void (*console_rx_cb)(void);
-
-int console_init(console_rx_cb rx_cb);
-static int inline
-console_is_init(void)
-{
-    return 0;
-}
-
-void console_write(const char *str, int cnt);
-int console_read(char *str, int cnt, int *newline);
-static void inline
-console_blocking_mode(void)
-{
-}
-
-static void inline
-console_echo(int on)
-{
-}
-
-static int console_printf(const char *fmt, ...)
-    __attribute__ ((format (printf, 1, 2)));;
-static int inline
-console_printf(const char *fmt, ...)
-{
-    return 0;
-}
-
-#define console_is_midline  0
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __CONSOLE_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/minimal/include/console/prompt.h
----------------------------------------------------------------------
diff --git a/sys/console/minimal/include/console/prompt.h b/sys/console/minimal/include/console/prompt.h
deleted file mode 100644
index 6de36f5..0000000
--- a/sys/console/minimal/include/console/prompt.h
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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.
- */
-
-#ifndef __CONSOLE_PROMPT_H__
-#define __CONSOLE_PROMPT_H__
-
-#include <stdarg.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* print console prompt */
-static void inline
-console_print_prompt(void)
-{
-}
-
-/* set the console prompt character */
-static void inline
-console_set_prompt(char ch)
-{
-}
-
-static void inline
-console_no_prompt(void)
-{
-}
-
-static void inline
-console_yes_prompt(void)
-{
-}
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __CONSOLE_PROMPT_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/minimal/include/console/ticks.h
----------------------------------------------------------------------
diff --git a/sys/console/minimal/include/console/ticks.h b/sys/console/minimal/include/console/ticks.h
deleted file mode 100644
index 97f6a05..0000000
--- a/sys/console/minimal/include/console/ticks.h
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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.
- */
-
-#ifndef __CONSOLE_TICKS_H__
-#define __CONSOLE_TICKS_H__
-
-#include <stdarg.h>
-
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-
-static void inline
-console_no_ticks(void)
-{
-}
-
-static void inline
-console_yes_ticks(void)
-{
-}
-
-static char inline
-console_get_ticks(void)
-{
-    return 0;
-}
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __CONSOLE_PROMPT_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/minimal/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/console/minimal/pkg.yml b/sys/console/minimal/pkg.yml
deleted file mode 100644
index 40e8980..0000000
--- a/sys/console/minimal/pkg.yml
+++ /dev/null
@@ -1,33 +0,0 @@
-#
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-#
-
-pkg.name: sys/console/minimal
-pkg.description: Text-based IO interface (minimized).
-pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
-pkg.homepage: "http://mynewt.apache.org/"
-pkg.keywords:
-
-pkg.deps:
-    - hw/hal
-    - kernel/os
-    - hw/drivers/uart
-pkg.apis: console
-
-pkg.init:
-    console_pkg_init: 20

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/minimal/src/cons_tty.c
----------------------------------------------------------------------
diff --git a/sys/console/minimal/src/cons_tty.c b/sys/console/minimal/src/cons_tty.c
deleted file mode 100644
index d837878..0000000
--- a/sys/console/minimal/src/cons_tty.c
+++ /dev/null
@@ -1,253 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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.
- */
-
-/*
- * Minimal line-based console implementation. Does not include
- * echo, command line history, prompt or console_printf().
- * Only offers console_write() and console_read().
- */
-#include <inttypes.h>
-#include <assert.h>
-#include "syscfg/syscfg.h"
-#include "sysinit/sysinit.h"
-#include "os/os.h"
-#include "uart/uart.h"
-#include "bsp/bsp.h"
-
-#include "console/console.h"
-#include "console/prompt.h"
-
-#define CONSOLE_RX_CHUNK        16
-
-#define CONSOLE_HEAD_INC(cr)    (((cr)->cr_head + 1) & ((cr)->cr_size - 1))
-#define CONSOLE_TAIL_INC(cr)    (((cr)->cr_tail + 1) & ((cr)->cr_size - 1))
-
-struct console_ring {
-    uint8_t cr_head;
-    uint8_t cr_tail;
-    uint16_t cr_size;
-    uint8_t *cr_buf;
-};
-
-struct console_tty {
-    struct uart_dev *ct_dev;
-
-    struct console_ring ct_tx;
-    /* must be after console_ring */
-    uint8_t ct_tx_buf[MYNEWT_VAL(CONSOLE_TX_BUF_SIZE)];
-
-    struct console_ring ct_rx;
-    /* must be after console_ring */
-    uint8_t ct_rx_buf[MYNEWT_VAL(CONSOLE_RX_BUF_SIZE)];
-
-    console_rx_cb ct_rx_cb; /* callback that input is ready */
-} console_tty;
-
-static void
-console_add_char(struct console_ring *cr, char ch)
-{
-    cr->cr_buf[cr->cr_head] = ch;
-    cr->cr_head = CONSOLE_HEAD_INC(cr);
-}
-
-static uint8_t
-console_pull_char(struct console_ring *cr)
-{
-    uint8_t ch;
-
-    ch = cr->cr_buf[cr->cr_tail];
-    cr->cr_tail = CONSOLE_TAIL_INC(cr);
-    return ch;
-}
-
-static void
-console_queue_char(char ch)
-{
-    struct console_tty *ct = &console_tty;
-    int sr;
-
-    OS_ENTER_CRITICAL(sr);
-    while (CONSOLE_HEAD_INC(&ct->ct_tx) == ct->ct_tx.cr_tail) {
-        /* TX needs to drain */
-        uart_start_tx(ct->ct_dev);
-        OS_EXIT_CRITICAL(sr);
-        OS_ENTER_CRITICAL(sr);
-    }
-    console_add_char(&ct->ct_tx, ch);
-    OS_EXIT_CRITICAL(sr);
-}
-
-void
-console_write(const char *str, int cnt)
-{
-    struct console_tty *ct = &console_tty;
-    int i;
-
-    for (i = 0; i < cnt; i++) {
-        if (str[i] == '\n') {
-            console_queue_char('\r');
-        }
-        console_queue_char(str[i]);
-    }
-    uart_start_tx(ct->ct_dev);
-}
-
-int
-console_read(char *str, int cnt, int *newline)
-{
-    struct console_tty *ct = &console_tty;
-    struct console_ring *cr = &ct->ct_rx;
-    int sr;
-    int i;
-    uint8_t ch;
-
-    *newline = 0;
-    OS_ENTER_CRITICAL(sr);
-    for (i = 0; i < cnt; i++) {
-        if (cr->cr_head == cr->cr_tail) {
-            break;
-        }
-
-        if ((i & (CONSOLE_RX_CHUNK - 1)) == (CONSOLE_RX_CHUNK - 1)) {
-            /*
-             * Make a break from blocking interrupts during the copy.
-             */
-            OS_EXIT_CRITICAL(sr);
-            OS_ENTER_CRITICAL(sr);
-        }
-
-        ch = console_pull_char(cr);
-        if (ch == '\n') {
-            *str = '\0';
-            *newline = 1;
-            break;
-        }
-        *str++ = ch;
-    }
-    OS_EXIT_CRITICAL(sr);
-    if (i > 0 || *newline) {
-        uart_start_rx(ct->ct_dev);
-    }
-    return i;
-}
-
-/*
- * Interrupts disabled when console_tx_char/console_rx_char are called.
- */
-static int
-console_tx_char(void *arg)
-{
-    struct console_tty *ct = (struct console_tty *)arg;
-    struct console_ring *cr = &ct->ct_tx;
-
-    if (cr->cr_head == cr->cr_tail) {
-        /*
-         * No more data.
-         */
-        return -1;
-    }
-    return console_pull_char(cr);
-}
-
-static int
-console_rx_char(void *arg, uint8_t data)
-{
-    struct console_tty *ct = (struct console_tty *)arg;
-    struct console_ring *rx = &ct->ct_rx;
-
-    if (CONSOLE_HEAD_INC(&ct->ct_rx) == ct->ct_rx.cr_tail) {
-        /*
-         * RX queue full. Reader must drain this.
-         */
-        if (ct->ct_rx_cb) {
-            ct->ct_rx_cb();
-        }
-        return -1;
-    }
-
-    /* echo */
-    switch (data) {
-    case '\r':
-    case '\n':
-        /*
-         * linefeed
-         */
-        console_add_char(rx, '\n');
-        if (ct->ct_rx_cb) {
-            ct->ct_rx_cb();
-        }
-        break;
-    default:
-        console_add_char(rx, data);
-        break;
-    }
-    return 0;
-}
-
-static int is_power_of_two (unsigned int x)
-{
-  return ((x != 0) && !(x & (x - 1)));
-}
-
-int
-console_init(console_rx_cb rx_cb)
-{
-    struct console_tty *ct = &console_tty;
-    struct uart_conf uc = {
-        .uc_speed = MYNEWT_VAL(CONSOLE_BAUD),
-        .uc_databits = 8,
-        .uc_stopbits = 1,
-        .uc_parity = UART_PARITY_NONE,
-        .uc_flow_ctl = MYNEWT_VAL(CONSOLE_FLOW_CONTROL),
-        .uc_tx_char = console_tx_char,
-        .uc_rx_char = console_rx_char,
-        .uc_cb_arg = ct
-    };
-
-    ct->ct_rx_cb = rx_cb;
-    if (!ct->ct_dev) {
-        ct->ct_tx.cr_size = MYNEWT_VAL(CONSOLE_TX_BUF_SIZE);
-        ct->ct_tx.cr_buf = ct->ct_tx_buf;
-        ct->ct_rx.cr_size = MYNEWT_VAL(CONSOLE_RX_BUF_SIZE);
-        ct->ct_rx.cr_buf = ct->ct_rx_buf;
-
-        ct->ct_dev = (struct uart_dev *)os_dev_open(CONSOLE_UART,
-          OS_TIMEOUT_NEVER, &uc);
-        if (!ct->ct_dev) {
-            return -1;
-        }
-    }
-
-    /* must be a power of 2 */
-    assert(is_power_of_two(MYNEWT_VAL(CONSOLE_RX_BUF_SIZE)));
-
-    return 0;
-}
-
-void
-console_pkg_init(void)
-{
-    int rc;
-
-    /* Ensure this function only gets called by sysinit. */
-    SYSINIT_ASSERT_ACTIVE();
-
-    rc = console_init(NULL);
-    SYSINIT_PANIC_ASSERT(rc == 0);
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/minimal/syscfg.yml
----------------------------------------------------------------------
diff --git a/sys/console/minimal/syscfg.yml b/sys/console/minimal/syscfg.yml
deleted file mode 100644
index 8b4732b..0000000
--- a/sys/console/minimal/syscfg.yml
+++ /dev/null
@@ -1,33 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
-#
-
-# Package: sys/console/minimal
-
-syscfg.defs:
-    CONSOLE_BAUD:
-        description: 'Console UART baud rate.'
-        value: '115200'
-    CONSOLE_FLOW_CONTROL:
-        description: 'Console UART flow control.'
-        value: 'UART_FLOW_CTL_NONE'
-    CONSOLE_TX_BUF_SIZE:
-        description: 'Console transmit buffer size; must be power of 2.'
-        value: 32
-    CONSOLE_RX_BUF_SIZE:
-        description: 'Console receive buffer size.'
-        value: 128

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4863008f/sys/console/stub/include/console/console.h
----------------------------------------------------------------------
diff --git a/sys/console/stub/include/console/console.h b/sys/console/stub/include/console/console.h
index 3c83b9d..d60aaf8 100644
--- a/sys/console/stub/include/console/console.h
+++ b/sys/console/stub/include/console/console.h
@@ -19,12 +19,18 @@
 #ifndef __CONSOLE_H__
 #define __CONSOLE_H__
 
-#include <stdarg.h>
+#include <inttypes.h>
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+struct os_eventq;
+
+struct console_input {
+    char line[0];
+};
+
 typedef void (*console_rx_cb)(void);
 
 static int inline
@@ -34,11 +40,16 @@ console_is_init(void)
 }
 
 static int inline
-console_init(console_rx_cb rxcb)
+console_init(console_rx_cb rx_cb)
 {
     return 0;
 }
 
+static void inline
+console_write(const char *str, int cnt)
+{
+}
+
 static int inline
 console_read(char *str, int cnt, int *newline)
 {
@@ -52,7 +63,7 @@ console_blocking_mode(void)
 }
 
 static void inline
-console_write(const char *str, int cnt)
+console_echo(int on)
 {
 }
 
@@ -66,8 +77,26 @@ console_printf(const char *fmt, ...)
 }
 
 static void inline
-console_echo(int on)
+console_set_queues(struct os_eventq *avail_queue,
+                   struct os_eventq *cmd_queue)
+{
+}
+
+static void inline
+console_set_completion_cb(uint8_t (*completion)(char *str, uint8_t len))
+{
+}
+
+static int inline
+console_handle_char(uint8_t byte)
 {
+    return 0;
+}
+
+static int inline
+console_out(int character)
+{
+    return 0;
 }
 
 #define console_is_midline  (0)


[04/10] incubator-mynewt-core git commit: kernel/os: Add os_eventq_get_no_wait function

Posted by ma...@apache.org.
kernel/os: Add os_eventq_get_no_wait function

This function is needed in interrupt context for quick queue
polls.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/10d350c5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/10d350c5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/10d350c5

Branch: refs/heads/master
Commit: 10d350c5ebb751a371fae7c0d2c73a80af4c2d45
Parents: 271ab1b
Author: Micha\u0142 Narajowski <mi...@codecoup.pl>
Authored: Fri Jan 27 16:01:20 2017 +0100
Committer: Micha\u0142 Narajowski <mi...@codecoup.pl>
Committed: Mon Apr 24 09:27:37 2017 -0700

----------------------------------------------------------------------
 kernel/os/include/os/os_eventq.h |  1 +
 kernel/os/src/os_eventq.c        | 14 ++++++++++++++
 2 files changed, 15 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/10d350c5/kernel/os/include/os/os_eventq.h
----------------------------------------------------------------------
diff --git a/kernel/os/include/os/os_eventq.h b/kernel/os/include/os/os_eventq.h
index 2ae779c..812d822 100644
--- a/kernel/os/include/os/os_eventq.h
+++ b/kernel/os/include/os/os_eventq.h
@@ -49,6 +49,7 @@ struct os_eventq {
 void os_eventq_init(struct os_eventq *);
 int os_eventq_inited(const struct os_eventq *evq);
 void os_eventq_put(struct os_eventq *, struct os_event *);
+struct os_event *os_eventq_get_no_wait(struct os_eventq *evq);
 struct os_event *os_eventq_get(struct os_eventq *);
 void os_eventq_run(struct os_eventq *evq);
 struct os_event *os_eventq_poll(struct os_eventq **, int, os_time_t);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/10d350c5/kernel/os/src/os_eventq.c
----------------------------------------------------------------------
diff --git a/kernel/os/src/os_eventq.c b/kernel/os/src/os_eventq.c
index 626d560..ed9aa37 100644
--- a/kernel/os/src/os_eventq.c
+++ b/kernel/os/src/os_eventq.c
@@ -96,6 +96,20 @@ os_eventq_put(struct os_eventq *evq, struct os_event *ev)
     }
 }
 
+struct os_event *
+os_eventq_get_no_wait(struct os_eventq *evq)
+{
+    struct os_event *ev;
+
+    ev = STAILQ_FIRST(&evq->evq_list);
+    if (ev) {
+        STAILQ_REMOVE(&evq->evq_list, ev, os_event, ev_next);
+        ev->ev_queued = 0;
+    }
+
+    return ev;
+}
+
 /**
  * Pull a single item from an event queue.  This function blocks until there
  * is an item on the event queue to read.