[National Technology N32G457 Review] 3. Porting small embedded shell tool nr_micro_shell
[Copy link]
【Transplantation Preparation】
When debugging and maintaining, it is often necessary to interact with the microcontroller to obtain or set certain parameters or perform certain operations. nr_micro_shell is a basic command line tool written for MCUs with fewer resources to meet this demand. Although the RT_Thread component already provides a powerful finsh command line interaction tool, finsh is still a bit bulky for microcontrollers with less ROM and RAM resources. On these platforms, if you still want to retain basic command line interaction functions, nr_micro_shell is a good choice.
nr_micro_shell has the following advantages
1. It takes up little resources, is simple to use, and is flexible and convenient. The use process only involves two functions, shell_init() and shell(). Whether using RTOS or bare metal, the tool can be easily applied without additional coding work.
2. Good interactive experience. Completely similar to the Linux shell command line, when the serial terminal supports ANSI (such as Hypertrm terminal), it not only supports basic command line interaction, but also provides Tab key command completion, query history commands, and arrow keys to move the cursor to modify functions.
3. Good scalability. nr_micro_shell provides users with standard function prototypes for custom commands. Users only need to write command functions according to the commands and register the command functions to use the commands.
Comparison of resource usage between nr_micro_shell and finsh (finsh does not use msh) under the same configuration :
You can download the source code through the git link .
【Transplantation steps】
Step 1. After obtaining the source code package, add it to the third-party component directory in the previously created template project, as shown below:
Step 2. Open Project.uvprojx and add the shell source code to the project
Step 3. Open the Magic Wand configuration project and add the header file path of the shell source code
Step 4. Open the underlying driver source file of serial port 1, rewrite log_init(), enable the serial port receive interrupt, and add shell_init(); if sending, you still need to redirect shell_printf to printf; the following processing needs to be done in the receive interrupt:
/**
* [url=home.php?mod=space&uid=159083]@brief[/url] This function handles USARTy global interrupt request.
*/
void LOG_USARTx_IRQHandler(void)
{
if (USART_GetIntStatus(LOG_USARTx, USART_INT_RXDNE) != RESET)
{
/* Read one byte from the receive data register */
shell(USART_ReceiveData(LOG_USARTx));
}
}
Step 5. The most important step is to customize and register commands. You can add them directly in const static_cmd_st static_cmd[], where {"\0", NULL} cannot be removed. You can also use NR_SHELL_CMD_EXPORT to add them. In this experiment, the following three custom commands are added to query the version information of nr_micro_shell and control the status of the onboard LED light.
/**
* @brief ls command
*/
void shell_ls_cmd(char argc, char *argv)
{
unsigned int i = 0;
if (argc > 1)
{
if (!strcmp("cmd", &argv[argv[1]]))
{
for (i = 0; nr_shell.static_cmd[i].fp != NULL; i++)
{
shell_printf("%s", nr_shell.static_cmd[i].cmd);
shell_printf("\r\n");
}
}
else if (!strcmp("-v", &argv[argv[1]]))
{
shell_printf("ls version 1.0.\r\n");
}
else if (!strcmp("-h", &argv[argv[1]]))
{
shell_printf("useage: ls [options]\r\n");
shell_printf("options: \r\n");
shell_printf("\t -h \t: show help\r\n");
shell_printf("\t -v \t: show version\r\n");
shell_printf("\t cmd \t: show all commands\r\n");
}
}
else
{
shell_printf("ls need more arguments!\r\n");
}
}
/**
* @brief test command
*/
void shell_test_cmd(char argc, char *argv)
{
unsigned int i;
shell_printf("test command:\r\n");
for (i = 0; i < argc; i++)
{
shell_printf("paras %d: %s\r\n", i, &(argv[argv[i]]));
}
}
/**
* @brief blink_led_cmd
* blink 0 turn off the led
* blink 1 turn on the led
*/
void blink_led_cmd(char argc, char *argv)
{
if(!strcmp("1", &argv[argv[1]]))
{
LedOn(PORT_GROUP1, LED1_PIN);
LedOn(PORT_GROUP2, LED2_PIN);
LedOn(PORT_GROUP2, LED3_PIN);
shell_printf("leds are ON\r\n");
}
else if(!strcmp("0", &argv[argv[1]]))
{
LedOff(PORT_GROUP1, LED1_PIN);
LedOff(PORT_GROUP2, LED2_PIN);
LedOff(PORT_GROUP2, LED3_PIN);
shell_printf("leds are OFF\r\n");
}
}
#ifdef NR_SHELL_USING_EXPORT_CMD
NR_SHELL_CMD_EXPORT(ls, shell_ls_cmd);
NR_SHELL_CMD_EXPORT(test, shell_test_cmd);
#else
const static_cmd_st static_cmd[] =
{
{"ls", shell_ls_cmd},
{"test", shell_test_cmd},
{"blink", blink_led_cmd},
{"\0", NULL}
};
#endif
Step 6. Finally, compile and download to the development board. When using the j-link cdc serial port, connect to the development board according to the following interface definition:
Open the serial port tool or shell terminal assistant, use custom commands for debugging, and you can use the shell to switch the onboard LED status normally
This transplantation experiment is over. Please refer to the contents of the attachment package for details.
2. N32G45x_Nr_Micro_Shell.zip
(1.18 MB, downloads: 40)
|