Repository: incubator-mynewt-core Updated Branches: refs/heads/develop 859540e34 -> 35b0a91b1
console/minimal; Add a minimal line-based console. 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/e3a43b02 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/e3a43b02 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/e3a43b02 Branch: refs/heads/develop Commit: e3a43b024418b15945c646f41f4298c8a8353fe8 Parents: 859540e Author: Marko Kiiskila <ma...@runtime.io> Authored: Thu Feb 9 15:24:49 2017 -0800 Committer: Marko Kiiskila <ma...@runtime.io> Committed: Thu Feb 9 15:24:49 2017 -0800 ---------------------------------------------------------------------- sys/console/minimal/include/console/console.h | 62 +++++ 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 +++ 6 files changed, 487 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e3a43b02/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..384b6a9 --- /dev/null +++ b/sys/console/minimal/include/console/console.h @@ -0,0 +1,62 @@ +/* + * 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 void console_printf(const char *fmt, ...) + __attribute__ ((format (printf, 1, 2)));; +static void inline +console_printf(const char *fmt, ...) +{ +} + +#define console_is_midline 0 + +#ifdef __cplusplus +} +#endif + +#endif /* __CONSOLE_H__ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e3a43b02/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/e3a43b02/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 new file mode 100644 index 0000000..97f6a05 --- /dev/null +++ b/sys/console/minimal/include/console/ticks.h @@ -0,0 +1,51 @@ +/* + * 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/e3a43b02/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..40e8980 --- /dev/null +++ b/sys/console/minimal/pkg.yml @@ -0,0 +1,33 @@ +# +# 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 <d...@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/e3a43b02/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 new file mode 100644 index 0000000..d837878 --- /dev/null +++ b/sys/console/minimal/src/cons_tty.c @@ -0,0 +1,253 @@ +/* + * 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/e3a43b02/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..8b4732b --- /dev/null +++ b/sys/console/minimal/syscfg.yml @@ -0,0 +1,33 @@ +# 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