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:55 UTC
[05/10] incubator-mynewt-core git commit: sys/console: bring back
console/minimal
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