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

相关文章

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

Springboot整合Redis主从实践

《Springboot整合Redis主从实践》:本文主要介绍Springboot整合Redis主从的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言原配置现配置测试LettuceConnectionFactory.setShareNativeConnect

统一返回JsonResult踩坑的记录

《统一返回JsonResult踩坑的记录》:本文主要介绍统一返回JsonResult踩坑的记录,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录统一返回jsonResult踩坑定义了一个统一返回类在使用时,JsonResult没有get/set方法时响应总结统一返回