Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use FinSH of RT-Thread to program hardware

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use RT-Thread 's FinSH for hardware programming, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Due to the rise of the Internet of things (IoT), hardware programming is becoming more and more common. RT-Thread allows you to communicate with the device from the Linux command line using FinSH.

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 that enables the user to communicate with the device from the command line. It is mainly used to debug or view system information.

Typically, development and debugging use hardware debuggers and printf logs to display. In some cases, however, these two methods are not very useful because they are abstracted from what is running, and they can be difficult to parse. However, RT-Thread is a multithreaded system, which is helpful when you want to know the status of a running thread, or to manually control the current state of the system. Because it is multithreaded, you can have an interactive shell where you can enter commands directly on the device, call functions to get the information you need, or control the behavior of the program. If you are only used to modern operating systems such as Linux or BSD, this may seem common to you, but for hardware hackers, it is far more extravagant than connecting a serial cable directly to the circuit board to get a glimmer of error.

There are two modes for FinSH.

The C language interpreter pattern, called c-style.

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

In C language interpreter mode, FinSH can parse 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 shell such as Bash.

GNU command standard

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

A complete GNU command consists of four main parts.

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Command name (executable): the name of the command line program

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

Options: configuration options for subcommand functions.

Parameters: the corresponding parameters for the configuration option of the subcommand function.

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

Git reset-hard HEAD~1

This can be broken down as follows:

GNU command line standards

The executable command is git, the subcommand is reset, the option used is-- head, and the argument is HEAD~1.

Another example:

Systemctl enable-now firewalld

The executable command is systemctl, the subcommand is enable, the option is-- now, and the argument is firewalld.

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

Write an elegant command line program

Here is an example of a RT-Thread run command that RT-Thread developers use every day:

Usage: env.py package [- h] [--force-update] [--update] [--list] [--wizard] [--upgrade] [--printenv] optional arguments:-h,-- help show this help message and exit-- force-update force update and clean packages, install or remove the packages by your settings in menuconfig-- update update packages Install or remove the packages by your settings in menuconfig-list list target packages-wizard create a new package with wizard-upgrade upgrade local packages list and ENV scripts from git repo-printenv print environmental variables to check

As you can see, it looks familiar and behaves like most POSIX applications you might already run on Linux or BSD. It helps when incorrect or inadequate syntax is used, and it supports long and short options. This general user interface is familiar to anyone who has used a Unix terminal.

Option category

There are many kinds of options, which can be divided into two categories according to their length.

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Short option: consists of a hyphen plus a letter, such as the-h option in pkgs-h.

Long option: consists of two hyphens plus words or letters, for example, the-- target option in scons- target-mdk5.

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

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

No parameters: this option cannot be followed by parameters.

Parameter required: there must be a parameter after the option.

Parameters are optional: you can have parameters after the option, but they are not required.

As you would expect from most Linux commands, FinSH's option parsing is very flexible. It can distinguish an option from a parameter based on spaces or equal signs as delimiters, or simply by extracting the option itself and assuming that what follows is a parameter (in other words, there is no delimiter at all).

Wavplay-v 50

Wavplay-v50

Wavplay-vol=50

Use optparse

If you have ever written a command line program, you may know that, in general, the language of your choice has a library or module called optparse. It is provided to programmers, so options entered as part of a command, such as-v or-- verbose, can be parsed with the rest of the command. This can help your code get an option from a subcommand or parameter.

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

MSH_CMD_EXPORT_ALIAS (pkgs, pkgs, this is test cmd.)

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

Static struct optparse_long long_opts [] = {{"help", 'hype, OPTPARSE_NONE}, / / Long command: help, corresponding to short command h, without arguments. {force-update, 0, OPTPARSE_NONE}, / / Long comman: force-update, without arguments {"update", 0, OPTPARSE_NONE}, {"list", 0, OPTPARSE_NONE}, {"wizard", 0, OPTPARSE_NONE}, {"upgrade", 0, OPTPARSE_NONE}, {"printenv", 0, OPTPARSE_NONE}, {NULL, 0 OPTPARSE_NONE}}

After you have created the options, write commands and descriptions for each option and its parameters:

Static void usage (void) {rt_kprintf ("usage: env.py package [- h] [--force-update] [--update] [--list] [--wizard]\ n"); rt_kprintf ("[--upgrade] [--printenv]\ n\ n"); rt_kprintf ("optional arguments:\ n") Rt_kprintf ("- h,-- help show this help message and exit\ n"); rt_kprintf ("--force-update force update and clean packages, install or remove the\ n"); rt_kprintf ("packages by your settings in menuconfig\ n"); rt_kprintf ("- update update packages, install or remove the packages by your\ n") Rt_kprintf ("settings in menuconfig\ n"); rt_kprintf ("- list list target packages\ n"); rt_kprintf ("- wizard create a new package with wizard\ n"); rt_kprintf ("- upgrade upgrade local packages list and ENV scripts from git repo\ n"); rt_kprintf ("- printenv print environmental variables to check\ n");}

The next step is parsing. Although you haven't implemented its functionality yet, the parsed code framework is the same:

Int pkgs (int argc, char * * argv) {int ch; int option_index; struct optparse options; if (argc = = 1) {usage (); return RT_EOK;} optparse_init (& options, argv); while ((ch = optparse_long (& options, long_opts, & option_index)! =-1) {ch = ch; rt_kprintf ("\ n") Rt_kprintf ("optopt =% c\ n", options.optopt); rt_kprintf ("optarg =% s\ n", options.optarg); rt_kprintf ("optind =% d\ n", options.optind); rt_kprintf ("option_index =% d\ n", option_index);} rt_kprintf ("\ n"); return RT_EOK;}

Here is the function header file:

# include "optparse.h" # include "finsh.h"

It is then compiled and downloaded to the device.

Output

Hardware hacker

Programming hardware may seem scary, but with the development of the Internet of things, it is becoming more and more common. Not everything can or should run on raspberry pie, but RT-Thread,FinSH allows you to maintain a familiar Linux feel.

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

These are all the contents of the article "how to program hardware with RT-Thread 's FinSH". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report