extras/stdin_uart_interrupt: stdin via IRQ driven RX driver on UART0
See examples/terminal/ for usage
This commit is contained in:
parent
5301174290
commit
4cfe40d348
8 changed files with 306 additions and 1 deletions
12
examples/terminal/FreeRTOSConfig.h
Normal file
12
examples/terminal/FreeRTOSConfig.h
Normal file
|
@ -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<FreeRTOSConfig.h>
|
||||
|
3
examples/terminal/Makefile
Normal file
3
examples/terminal/Makefile
Normal file
|
@ -0,0 +1,3 @@
|
|||
PROGRAM=terminal
|
||||
EXTRA_COMPONENTS=extras/stdin_uart_interrupt
|
||||
include ../../common.mk
|
117
examples/terminal/terminal.c
Normal file
117
examples/terminal/terminal.c
Normal file
|
@ -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 <stdint.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <esp8266.h>
|
||||
#include <esp/uart.h>
|
||||
#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<argc; i++) {
|
||||
uint8_t gpio_num = atoi(argv[i]);
|
||||
gpio_enable(gpio_num, GPIO_OUTPUT);
|
||||
gpio_write(gpio_num, true);
|
||||
printf("On %d\n", gpio_num);
|
||||
}
|
||||
} else {
|
||||
printf("Error: missing gpio number.\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void cmd_off(uint32_t argc, char *argv[])
|
||||
{
|
||||
if (argc >= 2) {
|
||||
for(int i=1; i<argc; i++) {
|
||||
uint8_t gpio_num = atoi(argv[i]);
|
||||
gpio_enable(gpio_num, GPIO_OUTPUT);
|
||||
gpio_write(gpio_num, false);
|
||||
printf("Off %d\n", gpio_num);
|
||||
}
|
||||
} else {
|
||||
printf("Error: missing gpio number.\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void cmd_help(uint32_t argc, char *argv[])
|
||||
{
|
||||
printf("on <gpio number> [ <gpio number>]+ Set gpio to 1\n");
|
||||
printf("off <gpio number> [ <gpio number>]+ Set gpio to 0\n");
|
||||
printf("sleep Take a nap\n");
|
||||
printf("\nExample:\n");
|
||||
printf(" on 0<enter> switches on gpio 0\n");
|
||||
printf(" on 0 2 4<enter> 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 "<command> <argument 1> <argument 2> ... <argument N>"
|
||||
// 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<enter>' 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();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue