ThreadX笔记
创始人
2024-03-21 20:35:12

1、任务优先级

任务优先级,数值越小优先级越高

2、ThreadX专用任务创建函数

void  tx_application_define(void *first_unused_memory)
  • 该函数需要用户自己定义,ThreadX启动会自动调用该函数,用户可以在该函数中创建自己的任务。
  • first_unused_memory,未使用的地址空间,方便用户创建自己的动态内存管理。ThreadX提供了给用户创建内存池、内存块等进行动态内存分配的功能。

3、ThreadX系统没有定义空闲线程

  • 如需要空闲线程需要用户自己定义
    ThreadX内核没有空闲任务,那么没有任务执行的时候它都在干什么:
    ports/cortex_m4/gnu/src/tx_thread_schedule.S文件中有定义:
@    /* The following is the idle wait processing... in this case, no threads are ready for execution and the
@       system will simply be idle until an interrupt occurs that makes a thread ready. Note that interrupts
@       are disabled to allow use of WFI for waiting for a thread to arrive.  */
@
__tx_ts_wait:CPSID   i                                       @ Disable interruptsLDR     r1, [r2]                                @ Pickup the next thread to execute pointerSTR     r1, [r0]                                @ Store it in the current pointerCBNZ    r1, __tx_ts_ready                       @ If non-NULL, a new thread is ready!

没有任务时ThreadX都在这里执行死循环。

4、ThreadX内核移植中__RAM_segment_used_end__的作用

  • __RAM_segment_used_end__的含义就是当前已经使用RAM区的末尾地址。
  • 其值会通过void tx_application_define(void *first_unused_memory)函数的first_unused_memory参数传递给用户使用。

5、使用GCC移植需要在链接脚本中定义__RAM_segment_used_end__

  /* User_heap_stack section, used to check that there is enough RAM left */._user_heap_stack :{. = ALIGN(8);PROVIDE ( end = . );PROVIDE ( _end = . );. = . + _Min_Heap_Size;. = . + _Min_Stack_Size;. = ALIGN(8);__RAM_segment_used_end__ = .;} >RAM

6、修改系统时钟和OS Tick

在Threadx移植文件下ports/cortex_m4/gnu/src/tx_initialize_low_level.s中定义:

SYSTEM_CLOCK      =   180000000
SYSTICK_CYCLES    =   ((SYSTEM_CLOCK / 1000) -1)
  • SYSTEM_CLOCK, 系统时钟180MHZ
  • SYSTICK_CYCLES 后面的1000表示OS Tick时钟为1000HZ,也就是OS Tick是1ms

7、临界段保护—中断锁

#define TX_INTERRUPT_SAVE_AREA  unsigned int interrupt_save;#define TX_DISABLE                              interrupt_save =  __disable_interrupts();
#define TX_RESTORE                              __restore_interrupts(interrupt_save);

使用方法:

TX_INTERRUPT_SAVE_AREA  TX_DISABLE                              
xxx 临界段
TX_RESTORE                              

8、临界段保护——任务锁

一般RTOS任务锁实现有两种方法:

  • 调度器加锁,给调度器加锁的话,就无法实现任务切换,高优先级任务也就无法抢占低优先级任务的执行,同时高优先级任务也是无法向低优先级任务切换的uCos、RT-Thread等就是用这种方式实现。
  • 关闭给OS提供Tick的定时器,没有心跳系统就不能进行调度工作。

但ThreadX有更好的实现方式:抢占阈值抢占阈值是ThreadX独有的高级功能。

  • 抢占阈值允许任务指定禁止抢占的优先级上限。优先级高于上限的任务仍可以执行抢占,但不允许优先级低于上限的任务执行抢占
  • 可以将抢占阈值设置为0来禁止所有任务抢占

比如一个任务的优先级是 5,我们希望执行某代码期间禁止优先级 0-4 的任务抢占:

TX_THREAD my_thread; 
UINT my_old_threshold; 
UINT status; status = tx_thread_preemption_change(&my_thread, 0, &my_old_threshold); 用户可以在此处执行关键代码。 status = tx_thread_preemption_change(&my_thread, 5, &my_old_threshold); 

9、ThreadX 动态加载

ThreadX支持动态加载App执行,类似安卓App
ThreadX动态APP玩法调用主程序的API也很方便,这样APP就可以仅写应用代码即可,驱动之类的都在主程序实现:
https://www.armbbs.cn/forum.php?mod=viewthread&tid=112170&highlight=threadx

10、绝对延时和相对延时

相对延时:

tx_thread_sleep(100); 

通过相对延时延时实现绝对延时:

static void TaskFunc(ULONG thread_input) 
{ UINT delay, nextTime; const UINT frequency = 200; /* 获取 frequency 个时钟节拍后的时间 */ nextTime= tx_time_get() + frequency; while(1) { LedToggle(); delay = nextTime - tx_time_get(); nextTime += frequency ; if(delay <= frequency) { tx_thread_sleep(delay); } } 
} 

相关内容

热门资讯

易点天下启动H股上市前期筹备工... 12月5日晚间,易点天下(301171)公告称,公司董事会授权管理层启动H股上市的前期筹备工作,授权...
“中招”流感该如何应对?专家提... 转自:央视新闻客户端  近期,气温波动幅度大,容易造成人体免疫力低下。今天,国家卫生健康委举行新闻发...
网飞Netflix宣布以827... 转自:中国基金报  12月5日晚间,Netflix(奈飞)宣布收购华纳兄弟探索公司,Netflix将...
香港大埔宏福苑火灾79名伤者全... 据新华社香港12月5日电 香港特区行政长官李家超和特区政府多位局长5日联合召开记者会。其中医务卫生局...
湖南平江通报老年妇女遭家暴:成... 湖南省岳阳市平江县联合处置组12月6日发布情况通报:12月4日11时许,我县公安局接到报警称,一老年...