stm32开发之threadx整合letter-shell 组件记录

2024-04-16 05:36

本文主要是介绍stm32开发之threadx整合letter-shell 组件记录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

  1. 使用过rt-thread的shell 命令交互的方式,觉得比较方便,所以在threadx中也移植个shell的组件。这里使用的是letter-shell
  2. letter-shell 核心的逻辑在于组件通过链接文件自动初始化或自动添加的两种方式,方便开发
  3. 源码仓库

实验(核心代码)

在这里插入图片描述

shell 线程组件

/** Copyright (c) 2024-2024,shchl** SPDX-License-Identifier: Apache-2.0** Change Logs:* Date           Author       Notes* 2024-4-16     shchl   first version*/
#include "includes.h"#if 1#include "shell.h"
#include "tx_initialize.h"#define SHELL_STACK_SIZE 2048
#define SHELL_THREAD_PRIORITY 18/*
*******************************************************************************************************
*                               外部引入变量
*******************************************************************************************************
*/
extern TX_MUTEX console_lock;/*控制台互斥锁变量*/
extern ULONG _tx_thread_system_state; /*tx 系统状态,保证编译器不报错,因为这个是在汇编文件中定义的*/
/*
*******************************************************************************************************
*                               变量
*******************************************************************************************************
*/
TX_THREAD shell_thread;
/*
*********************************************************************************************************
*                                       静态全局变量
*********************************************************************************************************
*/
static Shell g_shell; /*全局shell*/
static char shell_cache_buf[TX_LOG_BUF_SZ];/*
*********************************************************************************************************
*                                      函数声明
*********************************************************************************************************
*/
static signed short console_write_handle(char *data, unsigned short len);static signed short console_read_handle(char *dst, unsigned short read_len);static int console_lock_handle(struct shell_def *self);static int console_unlock_handle(struct shell_def *self);static void shell_thread_entry(ULONG input);
/*
*********************************************************************************************************
*                                      外部函数
*********************************************************************************************************
*/
/*** @brief 控制台shell 组件初始化* @return*/
int app_shell_console_component_init() {g_shell.write = console_write_handle;g_shell.read = console_read_handle;g_shell.lock = console_lock_handle;g_shell.unlock = console_unlock_handle;shellInit(&g_shell, shell_cache_buf, TX_LOG_BUF_SZ);/*创建shell 线程*/tx_thread_create(&shell_thread, "shell",shell_thread_entry, 0,app_malloc(SHELL_STACK_SIZE), SHELL_STACK_SIZE,SHELL_THREAD_PRIORITY, SHELL_THREAD_PRIORITY,TX_NO_TIME_SLICE, TX_AUTO_START);return TX_SUCCESS;
}TX_APP_DEFINE_EXPORT(app_shell_console_component_init); /*shell 控制台组件初始化*/
/*
*********************************************************************************************************
*                                      内部函数
*********************************************************************************************************
*/static signed short console_write_handle(char *data, unsigned short len) {comSendBuf(COM1, (uint8_t *) data, len);return (signed short) len;}static signed short console_read_handle(char *dst, unsigned short read_len) {signed short readCnt = 0;while (read_len) {if (comGetChar(COM1, (uint8_t *) dst) == 1) {readCnt++;read_len--;} else {break;}}return readCnt;
}static int console_lock_handle(struct shell_def *self) {if (TX_THREAD_GET_SYSTEM_STATE() == TX_INITIALIZE_IS_FINISHED) { /*还没初始化*//*初始化完成才使用互斥量*/return tx_mutex_get(&console_lock, TX_WAIT_FOREVER);}return TX_SUCCESS;
}static int console_unlock_handle(struct shell_def *self) {if (TX_THREAD_GET_SYSTEM_STATE() == TX_INITIALIZE_IS_FINISHED) { /*还没初始化*//*初始化完成才使用互斥量*/return tx_mutex_put(&console_lock);}return TX_SUCCESS;
}static void shell_thread_entry(ULONG input) {while (1) {shellTask(&g_shell);tx_thread_sleep(10);}
}#endif

CPU 状态信息(使用shell 交互来打印)

/** Copyright (c) 2024-2024,shchl** SPDX-License-Identifier: Apache-2.0** Change Logs:* Date           Author       Notes* 2024-4-4     shchl   first version*/
#include "includes.h"
/*
*******************************************************************************************************
*                               任务相关宏定义
*******************************************************************************************************
*/
#define APP_TASK_CPU_STAT_PRIO 30
#define APP_TASK_CPU_STAT_STK_SIZE 1024
/*
*******************************************************************************************************
*                               外部引入变量
*******************************************************************************************************
*/
#if defined(TX_EXECUTION_PROFILE_ENABLE)
extern EXECUTION_TIME _tx_execution_idle_time_total;
extern EXECUTION_TIME _tx_execution_thread_time_total;
extern EXECUTION_TIME _tx_execution_isr_time_total;
#endif/*
*******************************************************************************************************
*                               变量
*******************************************************************************************************
*/
__IO double OSCPUUsage;    /* CPU百分比 */
/*
*********************************************************************************************************
*                                       静态全局变量
*********************************************************************************************************
*/
static TX_THREAD cpu_stat_task_thread;
/*
*********************************************************************************************************
*                                      函数声明
*********************************************************************************************************
*/
static VOID task_cpu_stat_entry(ULONG input);
/*
*********************************************************************************************************
*                                      外部函数
*********************************************************************************************************
*/
/*** @brief cpu 状态任务* @param first_thread 第一个启动的任务线程首地址*/
int tx_task_cpu_stat_create() {tx_thread_create(&cpu_stat_task_thread,              /* 任务控制块地址 */"app_cpu_stat",               /* 任务名 */task_cpu_stat_entry,                  /* 启动任务函数地址 */0,                             /* 传递给任务的参数 */app_malloc(APP_TASK_CPU_STAT_STK_SIZE),            /* 堆栈基地址 */APP_TASK_CPU_STAT_STK_SIZE,    /* 堆栈空间大小 */APP_TASK_CPU_STAT_PRIO,        /* 任务优先级*/APP_TASK_CPU_STAT_PRIO,        /* 任务抢占阀值 */TX_NO_TIME_SLICE,               /* 不开启时间片 */TX_AUTO_START);                 /* 创建后立即启动 */return TX_SUCCESS;
}TX_THREAD_EXPORT(tx_task_cpu_stat_create);/*
*********************************************************************************************************
*	函 数 名: app_task_info_out
*	功能说明: 将ThreadX任务信息通过串口打印出来
*	形    参:无
*	返 回 值: 无
*********************************************************************************************************
*/void app_task_info_out(void) {TX_THREAD *p_tcb = _tx_thread_identify(); /* 定义一个任务控制块指针,并指向当前线程 *//* 打印标题 */
//    tx_log("调用线程======[%s]\r\n", p_tcb->tx_thread_name);
#if defined(TX_EXECUTION_PROFILE_ENABLE)tx_log("CPU利用率 = %5.2f%%\r\n", OSCPUUsage);tx_log("任务执行时间 = %.9fs\r\n", (double) _tx_execution_thread_time_total / SystemCoreClock);tx_log("空闲执行时间 = %.9fs\r\n", (double) _tx_execution_idle_time_total / SystemCoreClock);tx_log("中断执行时间 = %.9fs\r\n", (double) _tx_execution_isr_time_total / SystemCoreClock);tx_log("系统总执行时间 = %.9fs\r\n", (double) (_tx_execution_thread_time_total + \_tx_execution_idle_time_total + \_tx_execution_isr_time_total) / SystemCoreClock);
#endiftx_log("===============================================================\r\n");
//    tx_log(" 任务优先级 任务栈大小 当前使用栈  最大栈使用 状态   任务名\r\n");tx_log("   Prio     StackSize   CurStack    MaxStack    State   Taskname\r\n");/* 遍历任务控制列表TCB list),打印所有的任务的优先级和名称 */while (p_tcb != (TX_THREAD *) 0) {tx_log("   %2d        %5d      %5d       %5d    %5d      %s\r\n",p_tcb->tx_thread_priority,p_tcb->tx_thread_stack_size,(int) p_tcb->tx_thread_stack_end - (int) p_tcb->tx_thread_stack_ptr,(int) p_tcb->tx_thread_stack_end - (int) p_tcb->tx_thread_stack_highest_ptr,p_tcb->tx_thread_state,p_tcb->tx_thread_name);p_tcb = p_tcb->tx_thread_created_next;if (p_tcb == _tx_thread_identify()) break;}
}/*
*********************************************************************************************************
*                                      内部函数
*********************************************************************************************************
*/
static VOID task_cpu_stat_entry(ULONG input) {
#if defined(TX_EXECUTION_PROFILE_ENABLE)EXECUTION_TIME TolTime, IdleTime, deltaTolTime, deltaIdleTime;uint32_t uiCount = 0;(void) input;/* 计算CPU利用率 */IdleTime = _tx_execution_idle_time_total;TolTime = _tx_execution_thread_time_total + _tx_execution_isr_time_total + _tx_execution_idle_time_total;while (1) {/* CPU利用率统计 */uiCount++;if (uiCount == 20) {uiCount = 0;deltaIdleTime = _tx_execution_idle_time_total - IdleTime;deltaTolTime =_tx_execution_thread_time_total + _tx_execution_isr_time_total + _tx_execution_idle_time_total -TolTime;OSCPUUsage = (double) deltaIdleTime / deltaTolTime;OSCPUUsage = 100 - OSCPUUsage * 100;IdleTime = _tx_execution_idle_time_total;TolTime = _tx_execution_thread_time_total + _tx_execution_isr_time_total + _tx_execution_idle_time_total;}tx_thread_sleep(10);}
#elsewhile (1) {bsp_Idle();tx_thread_sleep(10);}#endif
}#ifdef SHELL_USING_CMD_EXPORT/*shell 脚本来管理*/
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_DISABLE_RETURN,ps, app_task_info_out, "cpu statue info print");#endif

测试结果

在这里插入图片描述

在这里插入图片描述

总结

  1. 在使用shell 和threadx 组合的时候,shell 加锁和解锁时,判断os是否启动,没有启动直接返回即可

这篇关于stm32开发之threadx整合letter-shell 组件记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907911

相关文章

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

Java实现远程执行Shell指令

《Java实现远程执行Shell指令》文章介绍使用JSch在SpringBoot项目中实现远程Shell操作,涵盖环境配置、依赖引入及工具类编写,详解分号和双与号执行多指令的区别... 目录软硬件环境说明编写执行Shell指令的工具类总结jsch(Java Secure Channel)是SSH2的一个纯J

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

基于Java开发一个极简版敏感词检测工具

《基于Java开发一个极简版敏感词检测工具》这篇文章主要为大家详细介绍了如何基于Java开发一个极简版敏感词检测工具,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录你是否还在为敏感词检测头疼一、极简版Java敏感词检测工具的3大核心优势1.1 优势1:DFA算法驱动,效率提升10

Spring Boot 整合 SSE(Server-Sent Events)实战案例(全网最全)

《SpringBoot整合SSE(Server-SentEvents)实战案例(全网最全)》本文通过实战案例讲解SpringBoot整合SSE技术,涵盖实现原理、代码配置、异常处理及前端交互,... 目录Spring Boot 整合 SSE(Server-Sent Events)1、简述SSE与其他技术的对

Java整合Protocol Buffers实现高效数据序列化实践

《Java整合ProtocolBuffers实现高效数据序列化实践》ProtocolBuffers是Google开发的一种语言中立、平台中立、可扩展的结构化数据序列化机制,类似于XML但更小、更快... 目录一、Protocol Buffers简介1.1 什么是Protocol Buffers1.2 Pro

springboot整合mqtt的步骤示例详解

《springboot整合mqtt的步骤示例详解》MQTT(MessageQueuingTelemetryTransport)是一种轻量级的消息传输协议,适用于物联网设备之间的通信,本文介绍Sprin... 目录1、引入依赖包2、yml配置3、创建配置4、自定义注解6、使用示例使用场景:mqtt可用于消息发

Python开发简易网络服务器的示例详解(新手入门)

《Python开发简易网络服务器的示例详解(新手入门)》网络服务器是互联网基础设施的核心组件,它本质上是一个持续运行的程序,负责监听特定端口,本文将使用Python开发一个简单的网络服务器,感兴趣的小... 目录网络服务器基础概念python内置服务器模块1. HTTP服务器模块2. Socket服务器模块