Untitled1


1.cubemx建立工程

选择合适的芯片版本,编辑芯片管脚配置如下
在这里插入图片描述
激活uart1,配置串口数据输出及全局中断,选择ide选择mdk-arm,注意生成代码时勾选如下选项
在这里插入图片描述

2、keli引入rt-threand及修改相关代码

首先从网上下载rt-threand keli包如下图所示
在这里插入图片描述
之后将该包导入keli5。在打开项目后,选择如下选项
在这里插入图片描述
在其中选择引入rt-threand
引入后项目结构如图所示
在这里插入图片描述
将项目中自动生成的异常处理函数 HardFault_Handler() 和悬挂处理函数 PendSV_Handler()删除(通常在stm32f1xx_it.h中)。之后需要在 board.c 中实现 系统时钟配置(为 MCU、外设提供工作时钟)与 os_tick 的配置 (为操作系统提供心跳 / 节拍)。

代码如下所示

/**
 * This function will initial your board.
 */
/* board.c */

/* timer 定时器中断服务函数调用 rt_os_tick_callback function,cortex-m 架构使用 SysTick_Handler() */
void rt_os_tick_callback(void)
{
    rt_interrupt_enter();
    
    rt_tick_increase();

    rt_interrupt_leave();
}

/* cortex-m 架构使用 SysTick_Handler() */
void SysTick_Handler()
{
    rt_os_tick_callback();
}
void rt_hw_board_init(void)
{
  /*
   * TODO 1: OS Tick Configuration
   * Enable the hardware timer and call the rt_os_tick_callback function
   * periodically with the frequency RT_TICK_PER_SECOND.
   */

  /* 1、系统、时钟初始化 */
  HAL_Init(); // 初始化 HAL 库
  SystemClock_Config(); // 配置系统时钟
  SystemCoreClockUpdate(); // 对系统时钟进行更新

  /* 2、OS Tick 频率配置,RT_TICK_PER_SECOND = 1000 表示 1ms 触发一次中断 */
  SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);

  /* Call components board initial (use INIT_BOARD_EXPORT()) */
#ifdef RT_USING_COMPONENTS_INIT
  rt_components_board_init();
#endif

#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
  rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
#endif
}

由于 SysTick_Handler() 中断服务例程由用户在 board.c 中重新实现,做了系统 OS_Tick,所以还需要删除工程里中原本已经实现的 SysTick_Handler() ,避免在编译时产生重复定义。
启用系统堆,修改rtconfig.h如下所示
在这里插入图片描述
同时,在borad.c中,可以调整系统堆空间大小,堆空间大小根据芯片ram大小调整。本次调整大小如下所示
在这里插入图片描述
将while循环前代码修改为如下所示:

		MX_GPIO_Init();
		MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
		rt_thread_startup(rt_thread_create("led1",led1,0,256,1,10));
		rt_thread_mdelay(500);
		rt_thread_startup(rt_thread_create("led2",led2,0,256,2,10));
		rt_thread_mdelay(500);
		rt_thread_startup(rt_thread_create("sendchar",sendchar,0,256,3,10));

相应函数实现如下所示

void led1(void* p){
  while (1)
  {
		HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET);
		rt_thread_delay(200);
		HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);
		rt_thread_delay(500);
//		rt_schedule();
  }
}

void led2(void* p){
  while (1)
  {
		HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5,GPIO_PIN_SET);
		rt_thread_delay(500);
		HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5,GPIO_PIN_RESET);
		rt_thread_delay(200);
//		rt_schedule();
  }
}
/**
 * @brief		重定向printf函数到串口
 * 
 * @param		None						
 * @retval		None
 * 
 * @attention	None		
 * 
 */
int fputc(int ch, FILE * f)
{
	rt_enter_critical();
	HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xffff);///<普通串口发送数据
  rt_exit_critical(); 
  return ch;
}
void sendchar(void *p){
		while(1){
			
			rt_enter_critical();
			printf("hello world\n");
			rt_exit_critical();
			rt_thread_delay(500);
		}
}

二、使用步骤

虚拟示波器显示结果如下:
在这里插入图片描述
串口输出信息如下:
在这里插入图片描述


总结

rt-threand作为物联网中一个rtos系统,移植相对于usos要简单许多,入门难度也要简单许多,在ucos中需要修改很多才能实现的功能通过rt-threand可以相对简单的实现。

参考

rt-threand官方文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个 MATLAB 代码文件的模板,用于定义一个函数。函数名称为 "untitled1",输入参数可以是任意数量和类型的,输出参数也可以是任意数量和类型的。函数体需要根据具体的需求进行修改。 以下是模板代码: function varargout = untitled1(varargin) % UNTITLED1 MATLAB code for untitled1.fig % UNTITLED1, by itself, creates a new UNTITLED1 or raises the existing % singleton*. % % H = UNTITLED1 returns the handle to a new UNTITLED1 or the handle to % the existing singleton*. % % UNTITLED1('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in UNTITLED1.M with the given input arguments. % % UNTITLED1('Property','Value',...) creates a new UNTITLED1 or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before untitled1_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to untitled1_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help untitled1 % Last Modified by GUIDE v2.5 03-Jul-2021 19:01:56 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @untitled1_OpeningFcn, ... 'gui_OutputFcn', @untitled1_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before untitled1 is made visible. function untitled1_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to untitled1 (see VARARGIN) % Choose default command line output for untitled1 handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes untitled1 wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = untitled1_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; % --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hint: get(hObject,'Value') returns toggle state of pushbutton1 % --- Executes on selection change in popupmenu1. function popupmenu1_Callback(hObject, eventdata, handles) % hObject handle to popupmenu1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array % contents{get(hObject,'Value')} returns selected item from popupmenu1 % --- Executes during object creation, after setting all properties. function popupmenu1_CreateFcn(hObject, eventdata, handles) % hObject handle to popupmenu1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: popupmenu controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in checkbox1. function checkbox1_Callback(hObject, eventdata, handles) % hObject handle to checkbox1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hint: get(hObject,'Value') returns toggle state of checkbox1 % --- Executes on button press in radiobutton1. function radiobutton1_Callback(hObject, eventdata, handles) % hObject handle to radiobutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hint: get(hObject,'Value') returns toggle state of radiobutton1 % --- Executes on button press in togglebutton1. function togglebutton1_Callback(hObject, eventdata, handles) % hObject handle to togglebutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hint: get(hObject,'Value') returns toggle state of togglebutton1 % --- Executes on slider movement. function slider1_Callback(hObject, eventdata, handles) % hObject handle to slider1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hint: get(hObject,'Value') returns position of slider % --- Executes during object creation, after setting all properties. function slider1_CreateFcn(hObject, eventdata, handles) % hObject handle to slider1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: slider controls usually have a light gray background. if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor',[.9 .9 .9]); end % --- Executes on key press with focus on pushbutton1 and none of its controls. function pushbutton1_KeyPressFcn(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata structure with the following fields (see MATLAB.UI.CONTROL.UICONTROL) % EventName: string 'KeyPress' % Indices: row vector % Key: character vector containing the key(s) that was pressed % Modifier: cell array containing a modifier key string of 'alt', 'ctrl', % or 'shift', if it was pressed or {} if it was not % handles structure with handles and user data (see GUIDATA) % Hint: get(hObject,'Value') returns toggle state of pushbutton1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值