Use RT-Thread's FinSH to program hardware

Guide: Due to the rise of the Internet of Things (IoT), programming hardware has become more and more common. RT-Thread enables you to communicate with the device from the Linux command line with FinSH,

article Word Count: 6259, long read about: 7 minutes

https: //linux.cn/article-12667-1.html

Author: Alan Smithee

translation Author: Xingyu.Wang

RT-Thread is an open-source real-time operating system for programming Internet of Things (IoT) devices. FinSH is the command line component of RT-Thread, which provides a set of operation interface, which enables users to communicate with the device from the command line. It is mainly used for debugging or viewing system information.

Normally, development and debugging use hardware debugger and log to display. But in some cases, these two methods are not very useful, because they are abstracted from the content of the operation, and they may be difficult to parse. However, RT-Thread is a multi-threaded system, which is very helpful when you want to know the status of a running thread or manually control the current status of the system. Because it is multi-threaded, you can have an interactive shell, you can directly enter commands on the device, call functions to obtain the information you need, or control the behavior of the program. If you are only accustomed to modern operating systems such as Linux or BSD, this may seem common to you, but for hardware hackers, it is extremely extravagant, far more than simply connecting a serial cable directly to the circuit board A trace of wrong approach.

FinSH has two modes.

◈ C language interpreter mode, called c-style.

◈ The traditional command line mode is called msh (module shell).

In C language interpreter mode, FinSH can parse and execute most C language expressions, and use function calls to access functions and global variables on the system. It can also create variables from the command line.

In msh mode, the operation of FinSH is similar to traditional shells such as Bash.

GNU command standard

When we were developing FinSH, we learned that before writing a command-line application, you need to be familiar with the GNU command-line standard. This standard practice framework helps bring familiarity to the interface, which helps developers feel comfortable and efficient when using it.

A complete GNU command mainly consists of four parts.

1. Command name (executable file): the name of the command line program;

2. Subcommand: the name of the subfunction of the command program.

3. Options: configuration options for subcommand functions.

4. Parameters: The corresponding parameters of the subcommand function configuration options.

You can see this in any command. Take Git as an example:

can be decomposed into:

GNU command line standards

The executable commands are, the subcommands are, the options used are, and the parameters are. Another example is

: The executable command of

is, the subcommand is, the option is, and the parameter is.

Imagine that you want to use RT-Thread to write a command line program that conforms to GNU standards. FinSH has everything you need and will run your code as expected. Even better, you can rely on this compliance, so you can confidently port your favorite Linux programs.

Write an elegant command line program

Below is an example of RT-Thread running commands, RT-Thread developers use this command every day:

As you can see, it looks familiar and acts like You may already run most POSIX applications on Linux or BSD. It will help when using incorrect or insufficient syntax, and it supports long and short options. This kind of communicationThe user interface used is familiar to anyone who has used a Unix terminal. There are many types of

options, which can be divided into two categories according to their length.

1. Short option: consists of a hyphen and a letter, such as the option in.

2. Long option: consists of two hyphens plus words or letters, for example, the option in.

You can divide these options into three categories, depending on whether they have parameters.

1. No parameters: There can be no parameters after this option.

2. Parameter required: There must be a parameter after the option.

3. Optional parameters: there can be parameters after the options, but they are not required.

As you expect from most Linux commands, FinSH's option parsing is very flexible. It can distinguish between an option and a parameter based on a space or an equal sign as a delimiter, or just extract the option itself and assume that the following content is a parameter (in other words, there is no delimiter at all).

Use optparse

If you have ever written a command line program, you might know that, generally speaking, your language of choice has a library or module called optparse. It is provided to the programmer, so the options entered as part of the command (such as or) can be parsed with other parts of the command. This can help your code get an option from a subcommand or parameter.

When writing a command for FinSH, the package hopes to use this format:

You can use the long form or the short form, or use both forms to implement options. For example:

After creating the options, write the command and description of each option and its parameters:

The next step is to parse. Although you haven't implemented its function yet, the parsed code frame is the same:

Here is the function header file:

Then, compile and download it to the device.

Output

Hardware hacker

programming the hardware seems scary, but with the development of the Internet of Things, it becomes more and more common. Not everything can or should run on the Raspberry Pi, but in RT-Thread, FinSH can keep you familiar with Linux.

If you are curious about coding on bare metal, try RT-Thread.

via: https: //opensource.com/article/20/9/hardware-command-line

Author: Alan Smithee topics: lujun9972 Translator: wxy proofread: wxy

LCTT original article by the compiler, Linux China is proud