RTX v5学习

1 篇文章 0 订阅

https://arm-software.github.io/CMSIS_5/RTOS2/html/rtx5_impl.html

RTX v5熟悉

1.RTX5 工程创建

2. RTX5特性:启动方法、内存管理、调度、中断等。

3.新特性整理

1)keil rtx调试方法:system and thread viewer、Event Viewer

2)rtx配置项说明:System Configuration、Thread Configuration、Timer Configuration等

3)SVC:当os需要操作硬件时,可使用svc产生软中断操作。

4)特权与非特权:任务支持特权访问,通过配置CONTOL,核心外设MPU,NVIC, SCB 和 STK,具体查看权威手册,确定是否需要特权模式下才能访问。

5)os不提供临界段和中断锁

6)Tick-less Low-Power Operation(低功耗机制):滴答时钟不使能,osKernelSuspend获取空闲时间,控制rtc唤醒时间,然后通过osKernelResume校准恢复。

7)零中断延时,ISR的中断相应时间和没有使用RTX系统一样(RTX内核库无关闭中断的操作,见第5点)。

8)Freertos的中断优先级必须在freertos的优先级定义范围内才可以使用安全函数,RTX无此限制。

9)注意事项:

1、任务栈空间必须 8 字节对齐, 可以将任务栈数组定义成 uint64_t 类型即可

2、用户线程不要操作NVIC,一定要改变可通过SVC方式操作。

 

http://www.keil.com/pack/doc/CMSIS/RTOS2/html/rtx5_impl.html

https://blog.csdn.net/qq_29350001/article/details/81630311 systick实现us延时

调试方法

system and thread viewer

Timer Number: 0 表示使用芯片的滴答定时器, 1 表示使用外设定时器。

Tick Timer: RTX 的时钟节拍周期。

Roud Robin Timeout:时间片溢出时间, 即时间片调度时任务分配的时间片大小。

Stack Size:任务栈大小,单位字节。

Stack Overflow Check: 设置是否支持栈溢出检测。

Tack Usage: 任务创建情况, Available 表示可以创建的最大任务数, Used 表示实际创建的。

User Timers:软件定时器个数,Available 表示可以创建的最大任个数,Used 表示实际创建的个数。

下面是任务部分功能介绍:

ID:任务的 ID 标示。

Name:任务的函数名。

Priority: 任务优先级。

State:任务状态。

Delay: 任务延迟时间。

Event Value: 任务当前的事件标志数值。

Event Mask: 任务等待的事件标志数值。

Event Viewer

硬件需要SWO引脚支持

 

RTX配置

RTX_Config.h

 

System Configuration

 

Name

#define

Description

Global Dynamic Memory size [bytes]

OS_DYNAMIC_MEM_SIZE

Defines the combined global dynamic memory size for the Global Memory Pool. Default value is 4096. Value range is [0-1073741824] bytes, in multiples of 8 bytes.

Kernel Tick Frequency (Hz)

OS_TICK_FREQ

Defines base time unit for delays and timeouts in Hz. Default: 1000Hz = 1ms period.

Round-Robin Thread switching

OS_ROBIN_ENABLE

Enables Round-Robin Thread switching. 选择是否使能时间片调度,选上单选框表示使能时间片调度,取消单选框表示不使用时间片调度

Round-Robin Timeout

OS_ROBIN_TIMEOUT

Defines how long a thread will execute before a thread switch. Default value is 5. Value range is [1-1000]. 表示时间片的大小, 单位是系统时钟节拍个数

ISR FIFO Queue

OS_ISR_FIFO_QUEUE

RTOS Functions called from ISR store requests to this buffer. Default value is 16 entries. Value range is [4-256] entries in multiples of 4. 表示 ISR FIFO 队列大小。 中断服务程序中调用以 isr_ 开头的函数时,会将请求类型存到此缓冲中 ,以便在中断处理程序退出后稍后进行处理。

Object Memory usage counters

OS_OBJ_MEM_USAGE

Enables object memory usage counters to evaluate the maximum memory pool requirements individually for each RTOS object type. 使能对象内存使用情况计数器,分别评估每种RTOS对象类型的最大内存池要求。

 

Thread Configuration

 

Option

#define

Description

Object specific Memory allocation

OS_THREAD_OBJ_MEM

Enables object specific memory allocation. See Object-specific Memory Pools.

Number of user Threads

OS_THREAD_NUM

Defines maximum number of user threads that can be active at the same time. Applies to user threads with system provided memory for control blocks. Default value is 1. Value range is [1-1000].

Number of user Threads with default Stack size

OS_THREAD_DEF_STACK_NUM

Defines maximum number of user threads with default stack size and applies to user threads with 0 stack size specified. Value range is [0-1000].

Total Stack size [bytes] for user Threads with user-provided Stack size

OS_THREAD_USER_STACK_SIZE

Defines the combined stack size for user threads with user-provided stack size. Default value is 0. Value range is [0-1073741824] Bytes, in multiples of 8.

Default Thread Stack size [bytes]

OS_STACK_SIZE

Defines stack size for threads with zero stack size specified. Default value is 200. Value range is [96-1073741824] Bytes, in multiples of 8.

Idle Thread Stack size [bytes]

OS_IDLE_THREAD_STACK_SIZE

Defines stack size for Idle thread. Default value is 200. Value range is [72-1073741824] bytes, in multiples of 8.

Idle Thread TrustZone Module ID

OS_IDLE_THREAD_TZ_MOD_ID

Defines the TrustZone Module ID the Idle Thread shall use. This needs to be set to a non-zero value if the Idle Thread need to call secure functions. Default value is 0. 定义空闲线程将使用的TrustZone模块ID。 如果空闲线程需要调用安全功能,则需要将其设置为非零值。 预设值为0。

Stack overrun checking

OS_STACK_CHECK

Enable stack overrun checks at thread switch.

Stack usage watermark

OS_STACK_WATERMARK

Initialize thread stack with watermark pattern for analyzing stack usage. Enabling this option increases significantly the execution time of thread creation. 创建线程时,RTX5用水印图案(0xCC)初始化线程堆栈。 这使调试器可以确定每个线程的最大堆栈使用率,通常在开发期间使用。 启用此选项会大大增加osThreadNew的执行时间(取决于线程堆栈的大小)。

Processor mode for Thread execution

OS_PRIVILEGE_MODE

Controls the processor mode. Default value is Privileged mode. Value range is [0=Unprivileged; 1=Privileged] mode. RTX5允许以非特权或特权处理器模式执行线程。 在非特权处理器模式下,应用程序软件: 对MSR和MRS指令的访问受到限制,并且不能使用CPS指令。 无法访问system timer, NVIC, or system control block.。 可能限制了对内存或外围设备的访问权限。 在特权处理器模式下,应用程序软件可以使用所有指令,并可以访问所有资源。

OS_THREAD_NUM = 6

OS_THREAD_DEF_STACK_NUM = 0

表示所有6个线程使用的堆栈空间大小为OS_THREAD_USER_STACK_SIZE。

OS_THREAD_NUM = 6

OS_THREAD_DEF_STACK_NUM = 1

表示所有5个线程使用的堆栈空间大小为OS_THREAD_USER_STACK_SIZE,1个线程使用的堆栈空间大小为OS_STACK_SIZE。

 

Timer Configuration

 

Name

#define

Description

Object specific Memory allocation

OS_TIMER_OBJ_MEM

Enables object specific memory allocation.

Number of Timer objects

OS_TIMER_NUM

Defines maximum number of objects that can be active at the same time. Applies to objects with system provided memory for control blocks. Value range is [1-1000].

Timer Thread Priority

OS_TIMER_THREAD_PRIO

Defines priority for timer thread. Default value is 40. Value range is [8-48], in multiples of 8. The numbers have the following priority correlation: 8=Low; 16=Below Normal; 24=Normal; 32=Above Normal; 40=High; 48=Realtime

Timer Thread Stack size [bytes]

OS_TIMER_THREAD_STACK_SIZE

Defines stack size for Timer thread. May be set to 0 when timers are not used. Default value is 200. Value range is [0-1073741824], in multiples of 8.

Timer Thread TrustZone Module ID

OS_TIMER_THREAD_TZ_MOD_ID

Defines the TrustZone Module ID the Timer Thread shall use. This needs to be set to a non-zero value if any Timer Callbacks need to call secure functions. Default value is 0.

Timer Callback Queue entries

OS_TIMER_CB_QUEUE

Number of concurrent active timer callback functions. May be set to 0 when timers are not used. Default value is 4. Value range is [0-256].

 

Event Flags Configuration

 

中断可以使用的系统函数

osKernelGetInfo, osKernelGetState, osKernelGetTickCount, osKernelGetTickFreq, osKernelGetSysTimerCount, osKernelGetSysTimerFreq
osThreadGetId, osThreadFlagsSet
osEventFlagsSet, osEventFlagsClear, osEventFlagsGet, osEventFlagsWait
osSemaphoreAcquire, osSemaphoreRelease, osSemaphoreGetCount
osMemoryPoolAlloc, osMemoryPoolFree, osMemoryPoolGetCapacity, osMemoryPoolGetBlockSize, osMemoryPoolGetCount, osMemoryPoolGetSpace
osMessageQueuePut, osMessageQueueGet, osMessageQueueGetCapacity, osMessageQueueGetMsgSize, osMessageQueueGetCount, osMessageQueueGetSpace

内存管理

  • Global Memory Pool uses a single global memory pool for all objects. It is easy to configure, but may have the disadvantage for memory fragmentation when objects with different sizes are created and destroyed.
  • Object-specific Memory Pools uses a fixed-size memory pool for each object type. The method is time deterministic and avoids memory fragmentation.每个object使用
  • Static Object Memory reserves memory during compile time and completely avoids that a system can be out of memory. This is typically a required for some safety critical systems.

SVC

当os需要操作硬件时,使用svc产生软中断,防止用户程序直接访问硬件。

SVC在Arm Cortex-M内核的特权处理程序模式下运行。 SVC函数接受参数并可以返回值。这些功能的使用方式与其他功能相同;但是,它们是通过SVC指令间接执行的。执行SVC指令时,控制器将切换到特权处理程序模式。

在此模式下不禁止中断。为了保护SVC函数免受中断影响,需要在代码中包含禁用/启用内部函数__disable_irq()和__enable_irq()

使用SVC功能访问受保护的外围设备,例如,配置NVIC和中断。

 

注意事项:

1、任务栈空间必须 8 字节对齐, 可以将任务栈数组定义成 uint64_t 类型即可

2、NVIC在任务启动后,不要修改。

3、任务栈是8字节对齐。

区别:

支持任务特权访问,了解特权与非特权访问机制(contorl),系统架构更加安全和健壮,MSP 、PSP

无临界断:比如此时某个任务正在调用系统 API 函数,而且此时中断正好关闭了,也就是进入到了临界区中,这个时候如果有一个紧急的中断事件被触发,这个中断就不能得到及时执行,必须等到中断开启才可以得到执行,如果关中断时间超过了紧急中断能够容忍的限度,危害是可想而知的。 像 uCOS-II, uCOS-III 和 FreeRTOS 的源码中都是有临界段的

无中段锁:操作系统未提供开关中断函数,按照裸机方式操作

任务锁:防止当前执行的任务被高优先级的任务打断,有两种方法,给调度器加锁或者关闭RTOS内核定时器,RTX通过关闭内核定时器方式实现的

 

Tick-less Low-Power Operation:无滴答时钟时的低功耗模式,osKernelSuspend和osKernelResume,比如通过rtc控制唤醒。

RTX5提供了无滴答操作的扩展,这对于使用广泛的低功耗模式(同时也禁用了SysTick计时器)的应用很有用。为了在这种省电模式下提供时间间隔,唤醒定时器用于导出定时器间隔。 CMSIS-RTOS2函数osKernelSuspend和osKernelResume控制无滴答操作。

使用此功能,RTX5线程调度程序可以停止定期的内核滴答中断。当所有活动线程都挂起时,系统进入掉电状态并计算在此掉电模式下可以停留多长时间。在掉电模式下,可以关闭处理器和外围设备。只有唤醒定时器必须保持通电,因为该定时器负责在掉电时间到期后唤醒系统。

无滴答操作由osRtxIdleThread线程控制。唤醒超时值是在系统进入掉电模式之前设置的。函数osKernelSuspend计算以RTX计时器计时为单位的唤醒超时。此值用于设置在系统掉电模式下运行的唤醒定时器。

一旦系统恢复操作(通过唤醒超时或其他中断),则RTX5线程调度程序将使用osKernelResume函数启动。参数sleep_time指定系统处于掉电模式的时间(以RTX计时器为单位)。

#include "msp.h"                        // Device header
/*----------------------------------------------------------------------------
 *      MSP432 Low-Power Extension Functions
 *---------------------------------------------------------------------------*/
static void MSP432_LP_Entry(void) {
  /* Enable PCM rude mode, which allows to device to enter LPM3 without waiting for peripherals */
  PCM->CTL1 = PCM_CTL1_KEY_VAL | PCM_CTL1_FORCE_LPM_ENTRY;       
  /* Enable all SRAM bank retentions prior to going to LPM3  */
  SYSCTL->SRAM_BANKRET |= SYSCTL_SRAM_BANKRET_BNK7_RET;
  __enable_interrupt();
  NVIC_EnableIRQ(RTC_C_IRQn);
  /* Do not wake up on exit from ISR */
  SCB->SCR |= SCB_SCR_SLEEPONEXIT_Msk;
  /* Setting the sleep deep bit */
  SCB->SCR |= (SCB_SCR_SLEEPDEEP_Msk);  
}
 
static volatile unsigned int tc;
static volatile unsigned int tc_wakeup;
 
void RTC_C_IRQHandler(void)
{
  if (tc++ > tc_wakeup) 
  {
    SCB->SCR &= ~SCB_SCR_SLEEPONEXIT_Msk;    
    NVIC_DisableIRQ(RTC_C_IRQn);
    NVIC_ClearPendingIRQ(RTC_C_IRQn);
    return;
  }
  if (RTC_C->PS0CTL & RTC_C_PS0CTL_RT0PSIFG)
  {
    RTC_C->CTL0 = RTC_C_KEY_VAL;                 // Unlock RTC key protected registers
    RTC_C->PS0CTL &= ~RTC_C_PS0CTL_RT0PSIFG;
    RTC_C->CTL0 = 0;
    SCB->SCR |= (SCB_SCR_SLEEPDEEP_Msk);
  }
}
 
uint32_t g_enable_sleep = 0;
 
void osRtxIdleThread (void) {
 
  for (;;) {
    tc_wakeup = osKernelSuspend();
    /* Is there some time to sleep? */
    if (tc_wakeup > 0) {
      tc = 0;
      /* Enter the low power state */
      MSP432_LP_Entry();
      __WFE();
    }
    /* Adjust the kernel ticks with the amount of ticks slept */
    osKernelResume (tc);
  }
}

 

参考

安富莱RTX教程

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值