1、用stm32CubeMax新建Keil工程LED工程,也可以用示例中的LED工程进行创建。
2、在当前工程目录下面新建一个用于存放OS的文件夹,把TobudOS源码中的arch、kernel、osal拷入工程之中,并且新建一个TOS_Config用于存放配置文件。
3、在工程中新建三个工程分组OS/arch、OS/kernel、OS/config
4、把kernel\core下面所有的C文件加入工程之中。
5、把arch/arm/arm-v8m/cortex-m33/armcc下面的prot_C、ports.S加入工程中,以及把arch/arm/arm-v8m\common下面的tos_cpu.c、tos_fault.c,以及osal\cmsis_os目录下的cmsis_os、os_tick.c加入工程组中。把config下面的tos_config.h也加入CONFIG组之中。
6、添回头文件路径:
7、打开stm32u5xx_it.c,加入#include "tos_k.h "、#include "stm32u5xx_it.h"头文件的引用。注释掉PendSV_Handler,或者用__weak。
8、添加心跳到systickhandle中:
void SysTick_Handler(void)
{
/* USER CODE BEGIN SysTick_IRQn 0 */
/* USER CODE END SysTick_IRQn 0 */
HAL_IncTick();
/* USER CODE BEGIN SysTick_IRQn 1 */
if(tos_knl_is_running()) {
tos_knl_irq_enter();
tos_tick_handler();
tos_knl_irq_leave();
}
/* USER CODE END SysTick_IRQn 1 */
}
9、打开配置文件,修改内容如下:
#ifndef _TOS_CONFIG_H_
#define _TOS_CONFIG_H_
#include "main.h"
#include "stm32u5xx.h" // 目标芯片头文件,视特定芯片而定
#include <stdio.h>
#define TOS_CFG_TASK_PRIO_MAX 10u
#define TOS_CFG_ROUND_ROBIN_EN 1u
#define TOS_CFG_OBJECT_VERIFY_EN 1u
#define TOS_CFG_OBJ_DYNAMIC_CREATE_EN 1u
#define TOS_CFG_EVENT_EN 1u
#define TOS_CFG_MMBLK_EN 1u
#define TOS_CFG_MMHEAP_EN 1u
#define TOS_CFG_MMHEAP_DEFAULT_POOL_SIZE 0x8000
#define TOS_CFG_MUTEX_EN 1u
#define TOS_CFG_TIMER_EN 1u
#define TOS_CFG_PWR_MGR_EN 0u
#define TOS_CFG_TICKLESS_EN 0u
#define TOS_CFG_SEM_EN 1u
#define TOS_CFG_TASK_STACK_DRAUGHT_DEPTH_DETACT_EN 1u
#define TOS_CFG_FAULT_BACKTRACE_EN 0u
#define TOS_CFG_IDLE_TASK_STK_SIZE 512u
#define TOS_CFG_CPU_TICK_PER_SECOND 1000u
#define TOS_CFG_CPU_CLOCK (SystemCoreClock)
#define TOS_CFG_TIMER_AS_PROC 1u
#define TOS_CFG_MAIL_QUEUE_EN 1u
#endif
10、在main.c中添加测试函数如下:
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "tos_k.h "
#include "cmsis_os.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
static GPIO_InitTypeDef GPIO_InitStruct;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void SystemPower_Config(void);
/* USER CODE BEGIN PFP */
//task1
#define TASK1_STK_SIZE 512
void task1(void *pdata);
osThreadDef(task1, osPriorityNormal, 1, TASK1_STK_SIZE);
//task2
#define TASK2_STK_SIZE 512
void task2(void *pdata);
osThreadDef(task2, osPriorityNormal, 1, TASK2_STK_SIZE);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void task1(void *pdata)
{
while(1)
{
HAL_GPIO_TogglePin(LED1_GPIO_PORT, LED1_PIN);
osDelay(200);
}
}
void task2(void *pdata) {
while(1) {
HAL_GPIO_TogglePin(LED2_GPIO_PORT, LED2_PIN);
osDelay(1000);
}
}
/* USER CODE END 0 */
/**
* [url=home.php?mod=space&uid=159083]@brief[/url] The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* STM32U5xx HAL library initialization:
- Configure the Flash prefetch
- Configure the Systick to generate an interrupt each 1 msec
- Set NVIC Group Priority to 3
- Low Level Initialization
*/
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* Configure the System Power */
SystemPower_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_ICACHE_Init();
/* USER CODE BEGIN 2 */
/* -1- Enable GPIO Clock (to be able to program the configuration registers) */
LED1_GPIO_CLK_ENABLE();
LED2_GPIO_CLK_ENABLE();
/* -2- Configure IO in output push-pull mode to drive external LEDs */
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = LED1_PIN;
HAL_GPIO_Init(LED1_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = LED2_PIN;
HAL_GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStruct);
osKernelInitialize();
osThreadCreate(osThread(task1),NULL);
osThreadCreate(osThread(task2),NULL);
osKernelStart();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_GPIO_TogglePin(LED1_GPIO_PORT, LED1_PIN);
/* Insert delay 100 ms */
HAL_Delay(100);
HAL_GPIO_TogglePin(LED2_GPIO_PORT, LED2_PIN);
/* Insert delay 100 ms */
HAL_Delay(100);
}
/* USER CODE END 3 */
}
11、下载到开发板后就可以实现两个任务来控制两个不同的任务来翻转LED1与LED2了。
【总结】
TobudOS是最近加入开源基金会的开源系统。实现非常小的内存与ROM的实时操作系统。
附工程源代码: