/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
*
© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "tx_api.h"
........
........
........
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
TX_THREAD my_thread_1;
TX_THREAD my_thread_2;
uint8_t pData1[]="I am thread1 ";
uint8_t pData2[]="I am thread2 ";
/* USER CODE END PV */
........
........
........
int main(void)
{
/* USER CODE BEGIN 1 */
uint8_t pData[]="=========ThreadX========= \n";
/* USER CODE END 1 */
........
........
........
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_TIM1_Init();
MX_TIM2_Init();
MX_TIM3_Init();
MX_SPI3_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Transmit(&huart1, pData, sizeof(pData), HAL_MAX_DELAY);
tx_kernel_enter(); //threadx 入口
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/* USER CODE BEGIN 4 */
void thread1_entry(ULONG entry_input)
{
while(1)
{
HAL_UART_Transmit(&huart1, pData1, sizeof(pData1), HAL_MAX_DELAY);
tx_thread_sleep(1000);// 线程睡眠1000 timer_ticks
}
}
void thread2_entry(ULONG entry_input)
{
while(1)
{
HAL_UART_Transmit(&huart1, pData2, sizeof(pData2), HAL_MAX_DELAY);
tx_thread_sleep(500);// 线程睡眠500 timer_ticks
}
}
void tx_application_define(void *first_unused_memory)
{
/*线程1*/
tx_thread_create(
&my_thread_1,//线程控制块指针
"my_thread1",//线程名字
thread1_entry, //线程入口函数
0,//线程入口参数
first_unused_memory, //线程的起始地址(这里偷懒,没有进行分配,直接使用未用的起始地址)
1024,//内存区域大小K
3,//优先级3 (0~TX_MAX_PRIORITES-1)0 表示最高优先级
3,//禁用抢占的最高优先级
TX_NO_TIME_SLICE,//时间切片值范围为 1 ~ 0xFFFF(TX_NO_TIME_SLICE = 0)
TX_AUTO_START //线程自动启动
);
/*线程2*/
tx_thread_create(
&my_thread_2,//线程控制块指针
"my_thread2",//线程名字
thread2_entry, //线程入口函数
0,//线程入口参数
first_unused_memory+1024, //线程的起始地址+1024 (-被前面线程用掉了)
1024,//内存区域大小K
1,//优先级3 (0~TX_MAX_PRIORITES-1)0 表示最高优先级
1,//禁用抢占的最高优先级
TX_NO_TIME_SLICE,//时间切片值范围为 1 ~ 0xFFFF(TX_NO_TIME_SLICE = 0)
TX_AUTO_START //线程自动启动
);
}
/* USER CODE END 4 */