Skip to content

Commit 16f16f8

Browse files
committed
[finsh] Make define user shell command and variable easier
- Add example "FinSH" to demo user shell command and variable - Enable FINSH_USING_DESCRIPTION by default
1 parent 9d6870f commit 16f16f8

File tree

12 files changed

+234
-31
lines changed

12 files changed

+234
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Currently most of the optional components are removed for the sake of simplicity
66

77

88
## Available Components ##
9-
* FinSH
9+
* FinSH (Support history and autocompletion)
1010

1111
## Supported Architectures ##
1212
* SAM (ARM Cortex-M3, Tested with Arduino Due)

examples/FinSH/FinSH.ino

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <rtt.h>
2+
3+
// ATTENTION: To compile this Sketch, please copy the following line to "shell_cmd.h" (without //)
4+
// ADD_SHELL_CMD(led, Turn on/off builtin LED, led, long, rt_uint32_t id, rt_int32_t val)
5+
6+
// ATTENTION: To compile this Sketch, please also copy the following 2 lines to "shell_var.h" (without //)
7+
// ADD_SHELL_VAR(id, LED ID, led_id, finsh_type_uint)
8+
// ADD_SHELL_VAR(state, LED state, led_state, finsh_type_uchar)
9+
10+
// After upload, send the following command through "Serial Monitor" to see the result.
11+
// led(0, 1)
12+
// led(0, 0)
13+
// led(id, state)
14+
// state
15+
// state=0
16+
// led(id, state)
17+
18+
extern "C" {
19+
20+
rt_uint32_t led_id = 0;
21+
rt_uint8_t led_state = 1;
22+
23+
rt_uint32_t led(rt_uint32_t id, rt_uint8_t state) {
24+
rt_kprintf("led%d=%d\n", id, state);
25+
if (id != 0) {
26+
return 1;
27+
}
28+
if (state) {
29+
digitalWrite(LED_BUILTIN, HIGH);
30+
} else {
31+
digitalWrite(LED_BUILTIN, LOW);
32+
}
33+
return 0;
34+
}
35+
36+
}
37+
38+
void setup() {
39+
pinMode(LED_BUILTIN, OUTPUT);
40+
RT_T.begin();
41+
}
42+
43+
// this function is not called
44+
void loop() {
45+
// no code should be here
46+
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=RT-Thread
2-
version=0.2.0
2+
version=0.2.1
33
author=Bernard Xiong <[email protected]>
44
maintainer=onelife <[email protected]>
55
sentence=Real Time Operating System porting for Arduino SAM and SAMD boards

src/components/finsh/cmd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,8 +1122,8 @@ void list_prefix(char *prefix)
11221122
}
11231123
#endif
11241124

1125-
#if defined(FINSH_USING_SYMTAB) && !defined(FINSH_USING_MSH_ONLY)
1126-
static int dummy = 0;
1125+
#if (defined(FINSH_USING_SYMTAB) || defined(RT_USING_FINSH)) && !defined(FINSH_USING_MSH_ONLY)
1126+
int dummy = 0;
11271127
FINSH_VAR_EXPORT(dummy, finsh_type_int, dummy variable for finsh)
11281128
#endif
11291129

src/components/finsh/finsh.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ extern struct finsh_syscall_item *global_syscall_list;
9191
struct finsh_sysvar
9292
{
9393
const char* name; /* the name of variable */
94-
#if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
94+
#if defined(FINSH_USING_DESCRIPTION) && (defined(FINSH_USING_SYMTAB) || defined(CONFIG_ARDUINO))
9595
const char* desc; /* description of system variable */
9696
#endif
9797
uint8_t type; /* the type of variable */

src/components/finsh/finsh_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ typedef long (*syscall_func)(void);
2121
struct finsh_syscall
2222
{
2323
const char* name; /* the name of system call */
24-
#if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
24+
#if defined(FINSH_USING_DESCRIPTION) && (defined(FINSH_USING_SYMTAB) || defined(CONFIG_ARDUINO))
2525
const char* desc; /* description of system call */
2626
#endif
2727
syscall_func func; /* the function address of system call */

src/components/finsh/msh.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ int msh_help(int argc, char **argv)
7676
FINSH_NEXT_SYSCALL(index))
7777
{
7878
if (strncmp(index->name, "__cmd_", 6) != 0) continue;
79-
#if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
79+
#if defined(FINSH_USING_DESCRIPTION) && (defined(FINSH_USING_SYMTAB) || defined(CONFIG_ARDUINO))
8080
rt_kprintf("%-16s - %s\n", &index->name[6], index->desc);
8181
#else
8282
rt_kprintf("%s ", &index->name[6]);

src/components/finsh/symbol.c

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,93 @@
1414

1515
#include "finsh.h"
1616

17+
#ifdef FINSH_USING_SYMTAB
18+
struct finsh_syscall *_syscall_table_begin = NULL;
19+
struct finsh_syscall *_syscall_table_end = NULL;
20+
struct finsh_sysvar *_sysvar_table_begin = NULL;
21+
struct finsh_sysvar *_sysvar_table_end = NULL;
22+
23+
#elif defined(CONFIG_ARDUINO)
24+
#ifdef ADD_SHELL_CMD
25+
#undef ADD_SHELL_CMD
26+
#endif
27+
// insert prototype
28+
#define ADD_SHELL_CMD(name, desc, fn, r_type, ...) \
29+
extern r_type fn(__VA_ARGS__);
30+
#include "shell_cmd.h"
31+
#undef ADD_SHELL_CMD
32+
// insert entry
33+
#ifdef FINSH_USING_DESCRIPTION
34+
#define ADD_SHELL_CMD(name, desc, fn, r_type, ...) \
35+
{#name, #desc, (syscall_func)fn},
36+
#else
37+
#define ADD_SHELL_CMD(name, desc, fn, r_type, ...) \
38+
{#name, (syscall_func)fn},
39+
#endif
40+
struct finsh_syscall _syscall_table[] = {
41+
#include "shell_cmd.h"
42+
};
43+
#undef ADD_SHELL_CMD
44+
45+
#ifdef ADD_SHELL_VAR
46+
#undef ADD_SHELL_VAR
47+
#endif
48+
// insert prototype
49+
#define finsh_type_unknown void*
50+
#define finsh_type_void void
51+
#define finsh_type_voidp void*
52+
#define finsh_type_char char
53+
#define finsh_type_uchar unsigned char
54+
#define finsh_type_charp char*
55+
#define finsh_type_short short
56+
#define finsh_type_ushort unsigned short
57+
#define finsh_type_shortp short*
58+
#define finsh_type_int int
59+
#define finsh_type_uint unsigned int
60+
#define finsh_type_intp int*
61+
#define finsh_type_long long
62+
#define finsh_type_ulong unsigned long
63+
#define finsh_type_longp unsigned*
64+
#define ADD_SHELL_VAR(name, desc, var, type) \
65+
extern type var;
66+
#include "shell_var.h"
67+
#undef finsh_type_unknown
68+
#undef finsh_type_void
69+
#undef finsh_type_voidp
70+
#undef finsh_type_char
71+
#undef finsh_type_uchar
72+
#undef finsh_type_charp
73+
#undef finsh_type_short
74+
#undef finsh_type_ushort
75+
#undef finsh_type_shortp
76+
#undef finsh_type_int
77+
#undef finsh_type_uint
78+
#undef finsh_type_intp
79+
#undef finsh_type_long
80+
#undef finsh_type_ulong
81+
#undef finsh_type_longp
82+
#undef ADD_SHELL_VAR
83+
// insert entry
84+
#ifdef FINSH_USING_DESCRIPTION
85+
#define ADD_SHELL_VAR(name, desc, var, type) \
86+
{#name, #desc, type, &var},
87+
#else
88+
#define ADD_SHELL_VAR(name, desc, var, type) \
89+
{#name, type, &var},
90+
#endif
91+
struct finsh_sysvar _sysvar_table[] = {
92+
#include "shell_var.h"
93+
};
94+
#undef ADD_SHELL_VAR
95+
96+
struct finsh_syscall *_syscall_table_begin = &_syscall_table[0];
97+
struct finsh_syscall *_syscall_table_end = \
98+
&_syscall_table[sizeof(_syscall_table) / sizeof(struct finsh_syscall)];
99+
struct finsh_sysvar *_sysvar_table_begin = &_sysvar_table[0];
100+
struct finsh_sysvar *_sysvar_table_end = \
101+
&_sysvar_table[sizeof(_sysvar_table) / sizeof(struct finsh_sysvar)];
102+
103+
#else /* FINSH_USING_SYMTAB */
17104
long hello(void);
18105
long version(void);
19106
long list(void);
@@ -27,24 +114,6 @@ long list_msgqueue(void);
27114
long list_mempool(void);
28115
long list_timer(void);
29116

30-
#ifdef CONFIG_ARDUINO
31-
#define CONVERT_TO_CMD(f) \
32-
void f(void); \
33-
long f##_(void) { \
34-
f(); \
35-
return 0; \
36-
}
37-
#define INSERT_CMD(f) {#f, f##_}
38-
39-
CONVERT_TO_CMD(list_mem);
40-
#endif /* CONFIG_ARDUINO */
41-
42-
#ifdef FINSH_USING_SYMTAB
43-
struct finsh_syscall *_syscall_table_begin = NULL;
44-
struct finsh_syscall *_syscall_table_end = NULL;
45-
struct finsh_sysvar *_sysvar_table_begin = NULL;
46-
struct finsh_sysvar *_sysvar_table_end = NULL;
47-
#else
48117
struct finsh_syscall _syscall_table[] =
49118
{
50119
{"hello", hello},
@@ -73,16 +142,12 @@ struct finsh_syscall _syscall_table[] =
73142
{"list_memp", list_mempool},
74143
#endif
75144
{"list_timer", list_timer},
76-
#ifdef CONFIG_ARDUINO
77-
INSERT_CMD(list_mem),
78-
#endif
79145
};
80146
struct finsh_syscall *_syscall_table_begin = &_syscall_table[0];
81147
struct finsh_syscall *_syscall_table_end = &_syscall_table[sizeof(_syscall_table) / sizeof(struct finsh_syscall)];
82-
83148
struct finsh_sysvar *_sysvar_table_begin = NULL;
84149
struct finsh_sysvar *_sysvar_table_end = NULL;
85-
#endif
150+
#endif /* FINSH_USING_SYMTAB */
86151

87152
#endif /* RT_USING_FINSH */
88153

src/include/rtdef.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ typedef int (*init_fn_t)(void);
233233
#define MSH_CMD_EXPORT_ALIAS(command, alias, desc)
234234
#elif !defined(FINSH_USING_SYMTAB)
235235
#define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc)
236+
#if defined(CONFIG_ARDUINO)
237+
#define FINSH_VAR_EXPORT(name, type, desc)
238+
#endif
236239
#endif
237240

238241
/* event length */

src/rtconfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
#if (CONFIG_USING_FINSH)
7777
#define RT_USING_FINSH
7878
// #define FINSH_USING_SYMTAB /* Not supported as no access to linker script */
79-
// #define FINSH_USING_DESCRIPTION /* Bundle option with FINSH_USING_SYMTAB */
79+
#define FINSH_USING_DESCRIPTION /* Show command description with list() */
8080
#define FINSH_USING_HISTORY
8181
#define FINSH_THREAD_PRIORITY (20)
8282
#define FINSH_THREAD_STACK_SIZE (2 * 1024)

0 commit comments

Comments
 (0)