From 4cfe40d348853acd6a85ffea9d56f56f8558517a Mon Sep 17 00:00:00 2001 From: Johan Kanflo Date: Fri, 7 Aug 2015 22:41:13 +0200 Subject: [PATCH] extras/stdin_uart_interrupt: stdin via IRQ driven RX driver on UART0 See examples/terminal/ for usage --- core/newlib_syscalls.c | 2 +- examples/terminal/FreeRTOSConfig.h | 12 ++ examples/terminal/Makefile | 3 + examples/terminal/terminal.c | 117 ++++++++++++++++++ extras/stdin_uart_interrupt/README.txt | 11 ++ extras/stdin_uart_interrupt/component.mk | 13 ++ .../stdin_uart_interrupt.c | 117 ++++++++++++++++++ .../stdin_uart_interrupt.h | 32 +++++ 8 files changed, 306 insertions(+), 1 deletion(-) create mode 100644 examples/terminal/FreeRTOSConfig.h create mode 100644 examples/terminal/Makefile create mode 100644 examples/terminal/terminal.c create mode 100644 extras/stdin_uart_interrupt/README.txt create mode 100644 extras/stdin_uart_interrupt/component.mk create mode 100644 extras/stdin_uart_interrupt/stdin_uart_interrupt.c create mode 100644 extras/stdin_uart_interrupt/stdin_uart_interrupt.h diff --git a/core/newlib_syscalls.c b/core/newlib_syscalls.c index ae15aff..b414481 100644 --- a/core/newlib_syscalls.c +++ b/core/newlib_syscalls.c @@ -52,7 +52,7 @@ long _write_r(struct _reent *r, int fd, const char *ptr, int len ) } /* syscall implementation for stdio read from UART */ -long _read_r( struct _reent *r, int fd, char *ptr, int len ) +__attribute__((weak)) long _read_r( struct _reent *r, int fd, char *ptr, int len ) { int ch, i; diff --git a/examples/terminal/FreeRTOSConfig.h b/examples/terminal/FreeRTOSConfig.h new file mode 100644 index 0000000..0a9a6b7 --- /dev/null +++ b/examples/terminal/FreeRTOSConfig.h @@ -0,0 +1,12 @@ +/* Terminal FreeRTOSConfig overrides. + + This is intended as an example of overriding some of the default FreeRTOSConfig settings, + which are otherwise found in FreeRTOS/Source/include/FreeRTOSConfig.h +*/ + +/* The serial driver depends on counting semaphores */ +#define configUSE_COUNTING_SEMAPHORES 1 + +/* Use the defaults for everything else */ +#include_next + diff --git a/examples/terminal/Makefile b/examples/terminal/Makefile new file mode 100644 index 0000000..f3c8bad --- /dev/null +++ b/examples/terminal/Makefile @@ -0,0 +1,3 @@ +PROGRAM=terminal +EXTRA_COMPONENTS=extras/stdin_uart_interrupt +include ../../common.mk diff --git a/examples/terminal/terminal.c b/examples/terminal/terminal.c new file mode 100644 index 0000000..f7c5923 --- /dev/null +++ b/examples/terminal/terminal.c @@ -0,0 +1,117 @@ +/* Serial terminal example + * UART RX is interrupt driven + * Implements a simple GPIO terminal for setting and clearing GPIOs + * + * This sample code is in the public domain. + */ + +#include +#include +#include +#include +#include +#include +#include "FreeRTOS.h" +#include "task.h" + +#define MAX_ARGC (10) +#define BUFFER_SIZE (81) + +static void cmd_on(uint32_t argc, char *argv[]) +{ + if (argc >= 2) { + for(int i=1; i= 2) { + for(int i=1; i [ ]+ Set gpio to 1\n"); + printf("off [ ]+ Set gpio to 0\n"); + printf("sleep Take a nap\n"); + printf("\nExample:\n"); + printf(" on 0 switches on gpio 0\n"); + printf(" on 0 2 4 switches on gpios 0, 2 and 4\n"); +} + +static void cmd_sleep(uint32_t argc, char *argv[]) +{ + printf("Type away while I take a 2 second nap (ie. let you test the UART HW FIFO\n"); + vTaskDelay(2000 / portTICK_RATE_MS); +} + +static void handle_command(char *cmd) +{ + char *argv[MAX_ARGC]; + int argc = 1; + char *temp, *rover; + memset((void*) argv, 0, sizeof(argv)); + argv[0] = cmd; + rover = cmd; + // Split string " ... " + // into argv, argc style + while(argc < MAX_ARGC && (temp = strstr(rover, " "))) { + rover = &(temp[1]); + argv[argc++] = rover; + *temp = 0; + } + + if (strlen(argv[0]) > 0) { + if (strcmp(argv[0], "help") == 0) cmd_help(argc, argv); + else if (strcmp(argv[0], "on") == 0) cmd_on(argc, argv); + else if (strcmp(argv[0], "off") == 0) cmd_off(argc, argv); + else if (strcmp(argv[0], "sleep") == 0) cmd_sleep(argc, argv); + else printf("Unknown command %s, try 'help'\n", argv[0]); + } +} + +static void gpiomon() +{ + char ch; + char cmd[81]; + int i = 0; + printf("\n\n\nWelcome to gpiomon. Type 'help' for, well, help\n"); + printf("%% "); + while(1) { + if (read(0, (void*)&ch, 1)) { // 0 is stdin + printf("%c", ch); + if (ch == '\n' || ch == '\r') { + cmd[i] = 0; + i = 0; + printf("\n"); + handle_command((char*) cmd); + printf("%% "); + } else { + if (i < sizeof(cmd)) cmd[i++] = ch; + } + } + } +} + +void user_init(void) +{ + uart_set_baud(0, 115200); + setbuf(stdout, NULL); + gpiomon(); +} diff --git a/extras/stdin_uart_interrupt/README.txt b/extras/stdin_uart_interrupt/README.txt new file mode 100644 index 0000000..35f5fa3 --- /dev/null +++ b/extras/stdin_uart_interrupt/README.txt @@ -0,0 +1,11 @@ +This module adds interrupt driven receive on UART 0. Using semaphores, a thread +calling read(...) when no data is available will block in an RTOS expected +manner until data arrives. + +This allows for a background thread running a serial terminal in your program +for debugging and state inspection consuming no CPU cycles at all. Not using +this module will make that thread while(1) until data arrives. + +No code changes are needed for adding this module, all you need to do is to add +it to EXTRA_COMPONENTS and add the directive configUSE_COUNTING_SEMAPHORES from +FreeRTOSConfig.h in examples/terminal to your project. \ No newline at end of file diff --git a/extras/stdin_uart_interrupt/component.mk b/extras/stdin_uart_interrupt/component.mk new file mode 100644 index 0000000..e00cf81 --- /dev/null +++ b/extras/stdin_uart_interrupt/component.mk @@ -0,0 +1,13 @@ +# Component makefile for extras/stdin_uart_interrupt +# +# See examples/terminal for usage. Well, actually there is no need to see it +# for 'usage' as this module is a drop-in replacement for the original polled +# version of reading from the UART. + +INC_DIRS += $(ROOT)extras/stdin_uart_interrupt + +# args for passing into compile rule generation +extras/stdin_uart_interrupt_INC_DIR = $(ROOT)extras/stdin_uart_interrupt +extras/stdin_uart_interrupt_SRC_DIR = $(ROOT)extras/stdin_uart_interrupt + +$(eval $(call component_compile_rules,extras/stdin_uart_interrupt)) diff --git a/extras/stdin_uart_interrupt/stdin_uart_interrupt.c b/extras/stdin_uart_interrupt/stdin_uart_interrupt.c new file mode 100644 index 0000000..cee7b2f --- /dev/null +++ b/extras/stdin_uart_interrupt/stdin_uart_interrupt.c @@ -0,0 +1,117 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015 Johan Kanflo (github.com/kanflo) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include +#include + +#if (configUSE_COUNTING_SEMAPHORES == 0) + #error "You need to define configUSE_COUNTING_SEMAPHORES in a local FreeRTOSConfig.h, see examples/terminal/FreeRTOSConfig.h" +#endif + +// IRQ driven UART RX driver for ESP8266 written for use with esp-open-rtos +// TODO: Handle UART1 + +#ifndef UART0 +#define UART0 (0) +#endif + +#define UART0_RX_SIZE (128) // ESP8266 UART HW FIFO size + +static xSemaphoreHandle uart0_sem = NULL; +static bool inited = false; +static void uart0_rx_init(void); + +IRAM void uart0_rx_handler(void) +{ + // TODO: Handle UART1, see reg 0x3ff20020, bit2, bit0 represents uart1 and uart0 respectively + if (!UART(UART0).INT_STATUS & UART_INT_STATUS_RXFIFO_FULL) { + return; + } +// printf(" [%08x (%d)]\n", READ_PERI_REG(UART_INT_ST(UART0)), READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)); + if (UART(UART0).INT_STATUS & UART_INT_STATUS_RXFIFO_FULL) { + UART(UART0).INT_CLEAR = UART_INT_CLEAR_RXFIFO_FULL; + if (UART(UART0).STATUS & (UART_STATUS_RXFIFO_COUNT_M << UART_STATUS_RXFIFO_COUNT_S)) { + long int xHigherPriorityTaskWoken; + _xt_isr_mask(1 << INUM_UART); + _xt_clear_ints(1< + +// Return number of characters waiting in UART0 +uint32_t uart0_num_char(void); +#endif