UINT tx_thread_create(TX_THREAD *thread_ptr,
CHAR *name_ptr, VOID (*entry_function)(ULONG),
ULONG entry_input, VOID *stack_start,
ULONG stack_size, UINT priority,
UINT preempt_threshold, ULONG time_slice,
UINT auto_start);
返回值
UINT tx_thread_delete(TX_THREAD *thread_ptr);
返回值
UINT tx_thread_preemption_change(TX_THREAD *thread_ptr,
UINT new_threshold, UINT *old_threshold);
返回值
UINT tx_thread_priority_change(TX_THREAD *thread_ptr,
UINT new_priority, UINT *old_priority);
返回值
VOID tx_thread_relinquish(VOID);
以相同或更高的优先级将处理器控制放弃给其他随时可以运行的线程。
UINT tx_thread_reset(TX_THREAD *thread_ptr);
返回值
TX_SUCCESS (0x00) 成功重置线程。
TX_NOT_DONE (0x20) 指定的线程未处于TX_COMPLETED或者TX_TERMINATED状态。
TX_THREAD_ERROR (0x0E) 无效线程指针。
TX_CALLER_ERROR服务(0x13)无效调用。
UINT tx_thread_resume(TX_THREAD *thread_ptr);
返回值
UINT tx_thread_sleep(ULONG timer_ticks);
返回值
UINT tx_thread_suspend(TX_THREAD *thread_ptr);
返回值
UINT tx_thread_terminate(TX_THREAD *thread_ptr);
返回值
UINT tx_thread_time_slice_change(TX_THREAD *thread_ptr,
ULONG new_time_slice, ULONG *old_time_slice);
返回值
/*
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Change Logs:
* Date Author Notes
* 2020-08-15 Psycho_real the first version
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "tx_api.h"
/* USER CODE END Includes */
......
/* USER CODE BEGIN PV */
TX_THREAD my_thread_1;
TX_THREAD my_thread_2;
uint8_t pData[] = "=========ThreadX=========\n";
uint8_t pData1[] = "I am thread1 ";
uint8_t pData2[] = "I am thread2 ";
/* USER CODE END PV */
......
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
......
/* USER CODE BEGIN 2 */
tx_kernel_enter(); //threadx 入口
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1);
/* USER CODE END 3 */
}
/* USER CODE BEGIN 4 */
void thread1_entry(ULONG entry_input)
{
INT count = 0;
uint8_t init_data[]="start now";
while (1)
{
HAL_UART_Transmit(&huart1, pData1, sizeof(pData1), HAL_MAX_DELAY);
if(count == 0)
{
HAL_UART_Transmit(&huart1, init_data, sizeof(init_data), HAL_MAX_DELAY);
}
count++;
//HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_7|GPIO_PIN_8);
tx_thread_sleep(1000); // 线程睡眠1000 timer_ticks
}
}
void thread2_entry(ULONG entry_input)
{
INT count = 0;
while (1)
{
HAL_UART_Transmit(&huart1, pData2, sizeof(pData2), HAL_MAX_DELAY);
if (count == 3)
{
/*挂起线程1*/
tx_thread_suspend(&my_thread_1);
}
else if (count == 6)
{
/*恢复线程1*/
tx_thread_resume(&my_thread_1);
}
else if (count == 9)
{
/*终止线程1*/
tx_thread_terminate(&my_thread_1);
}
else if (count == 12)
{
/*重置线程1*/
tx_thread_reset(&my_thread_1);
/*恢复线程1*/
tx_thread_resume(&my_thread_1);
}
else if (count == 13)
{
/*终止线程1-2*/
tx_thread_terminate(&my_thread_1);
tx_thread_terminate(&my_thread_2);
}
else
{
;
}
count++;
tx_thread_sleep(1000); // 线程睡眠500 timer_ticks
}
}
void my_entry_exit_notify(TX_THREAD *thread_ptr, UINT condition)
{
uint8_t entry_data[] = " thread1-entry ";
uint8_t exit_data[] = " thread1-exit ";
/* Determine if the thread was entered or exited. */
if (condition == TX_THREAD_ENTRY)
{
/* Thread entry! */
HAL_UART_Transmit(&huart1, entry_data, sizeof(pData2), HAL_MAX_DELAY);
//HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9);
}
if (condition == TX_THREAD_EXIT)
{
/* Thread exit! */
HAL_UART_Transmit(&huart1, exit_data, sizeof(pData2), HAL_MAX_DELAY);
}
}
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//线程自动启动
);
/*线程进入和退出时通知*/
tx_thread_entry_exit_notify(&my_thread_1, my_entry_exit_notify);
}
/* USER CODE END 4 */
......