须知: 如果想更详细了解LiteOS各内核模块,可以访问LiteOS开发指南。
使用方法
用户可以直接参考4.运行实例来运行demos/kernel下各个实例,也可以直接参考华为云上的沙盒实验来操作3.可操作的测试实例介绍中的各个实例。
具体的实例介绍在2.基本实例介绍中,下面是对各个实例的概述:
任务
los_api_task.c/.h实现了任务实例。
内存管理
los_api_dynamic_mem.c/.h实现了动态内存实例
los_api_static_mem.c/.h实现了静态内存实例
中断
los_api_interrupt.c/.h实现了中断实例。
IPC通信
los_api_msgqueue.c/.h实现了消息队列实例。
los_api_event.c/.h实现了事件实例。
los_api_mutex.c/.h实现了互斥锁实例。
los_api_sem.c/.h实现了信号量实例。
时间管理
los_api_systick.c/.h实现了tick时间实例。
los_api_timer.c/.h实现了软件定时器实例。
双向链表
los_api_list.c/.h实现了双向链表实例
任务实例:los_api_task.c/los_api_task.h
任务是竞争系统资源的最小运行单元。任务可以使用或等待CPU、使用内存空间等系统资源,并独立于其它任务运行。
本实例执行以下步骤:
实例结果
Kernel task demo start to run.
Create high priority task successfully.
Create low priority task successfully.
Enter high priority task handler.
Enter low priority task handler.
High priority task LOS_TaskDelay successfully.
High priority task LOS_TaskSuspend successfully.
High priority task LOS_TaskResume successfully.
Kernel task demo finished.
Huawei LiteOS的内存管理分为静态内存管理和动态内存管理,提供内存初始化、分配、释放等功能。
动态内存管理实例:los_api_dynamic_mem.c/los_api_dynamic_mem.h
当用户需要使用内存时,可以通过操作系统的动态内存申请函数申请指定大小的内存,动态内存管理模块会从系统配置的一块连续内存(内存池)中为用户分配指定大小的内存块。一旦使用完毕,用户再通过动态内存释放函数归还所占用内存,使之可以重复使用。
本实例执行以下步骤:
实例结果
Kernel dynamic memory demo start to run.
Mempool init successfully.
Mem alloc successfully.
*mem = 828.
Mem free successfully.
Kernel dynamic memory demo finished.
静态内存管理实例:los_api_static_mem.c/los_api_static_mem.h
静态内存池由一个控制块和若干相同大小的内存块构成。控制块位于内存池头部,用于内存块管理。内存块大小在内存池初始化时设定,初始化后内存块的大小不可变更。所以静态内存池是由若干大小相同的内存块构成的。内存块的申请和释放以块大小为粒度。
本实例执行以下步骤:
实例结果
Kernel static memory demo start to run.
Mem box init successfully.
Mem box alloc successfully.
*mem = 828.
Clear data ok, *mem = 0.
Mem box free successfully.
Kernel static memory demo finished.
中断实例:los_api_interrupt.c/los_api_interrupt.h
中断是指出现需要时,CPU暂停执行当前程序,转而执行新程序的过程。即在程序运行过程中,出现了一个必须由CPU立即处理的事务。此时,CPU暂时中止当前程序的执行转而处理这个事务,这个过程就叫做中断。
本实例以软件中断为例来说明中断使用的一般步骤:
实例结果
Kernel interrupt demo start to run.
User IRQ test successfully.
Kernel interrupt demo successfully.
说明: 目前的中断测试代码提供了基本框架,中断硬件初始化代码请用户根据开发板硬件情况在ExampleExti0Init()函数中自行实现。
以下以野火挑战者F429开发板为例来说明硬件中断使用的一般步骤:
实例结果
按下PA0引脚对应的按键(即野火挑战者F429开发板上的对应的KEY1按键)串口输出如下:
Button IRQ test successfully.
注意事项: 当前LiteOS系统对于Cortex-M系列芯片,前16个中断号为系统预留,不开放给用户使用。
IPC通信提供消息队列、事件、互斥锁和信号量功能。
消息队列实例:los_api_msgqueue.c/los_api_msgqueue.h
队列又称消息队列,是一种常用于任务间通信的数据结构,能够接收来自任务或中断的不固定长度的消息,并根据不同的接口选择消息是否存放在自己空间。任务能够从队列里读取消息,当队列中的消息为空时,挂起读取任务;当队列中有新消息时,挂起的读取任务被唤醒并处理新消息。
本实例执行以下步骤:
实例结果
Kernel message queue demo start to run.
Create the queue successfully.
Receive message : test is message 0.
Receive message : test is message 1.
Receive message : test is message 2.
Receive message : test is message 3.
Receive message : test is message 4.
Receive message failed, error: 200061d.
Delete the queue successfully.
Kernel message queue demo finished.
事件实例:los_api_event.c/los_api_event.h
事件是一种实现任务间通信的机制,可用于实现任务间的同步。一个任务可以等待多个事件的发生:可以是任意一个事件发生时唤醒任务进行事件处理,也可以是几个事件都发生后才唤醒任务进行事件处理。
本实例执行以下步骤:
实例结果
Kernel event demo start to run.
Read event task wait event 0x1.
Write event.
Read the event : 0x1.
Current event id : 1
Current event id : 0
Kernel event demo finished.
互斥锁实例:los_api_mutex.c/los_api_mutex.h
互斥锁又称互斥型信号量,是一种特殊的二值性信号量,用于实现对共享资源的独占式处理。
本实例执行以下步骤:
实例结果
Kernel mutex demo start to run.
Task2 try to get the mutex, wait forever.
Task2 get mutex g_demoMux01 and suspend 100 ticks.
Task1 try to get mutex, wait 10 ticks.
Task1 timeout and try to get mutex, wait forever.
Task2 resumed and post the g_demoMux01.
Task1 wait forever, got mutex g_demoMux01 successfully.
Kernel mutex demo finished.
信号量实例:los_api_sem.c/los_api_sem.h
信号量(Semaphore)是一种实现任务间通信的机制,实现任务之间同步或临界资源的互斥访问。常用于协助一组相互竞争的任务来访问临界资源。
本实例执行以下步骤:
实例结果
Kernel semaphore demo start to run.
Task2 try get semaphore g_demoSemId wait forever.
Task1 try get semphore g_demoSemId, timeout 10 ticks.
Task2 get semaphore g_demoSemId and then delay 20ticks.
Task1 timeout and try get semphore g_demoSemId wait forever.
Task2 post semaphore g_demoSemId.
Task1 wait_forever and got semphore g_demoSemId successfully.
Kernel semaphore demo finished.
时间管理以系统时钟为基础。时间管理提供给应用程序所有和时间有关的服务。下面包括时钟管理和软件定时器两个实例。
时钟管理实例:los_api_systick.c/los_api_systick.h
本实例执行以下步骤:
实例结果
Kernel systick demo start to run.
LOS_MS2Tick tick = 1000.
LOS_Tick2MS ms = 1000.
LOS_CyclePerTickGet = 180000.
LOS_TickCountGet = 1132.
LOS_TickCountGet after delay = 1335.
Kernel systick demo finished.
软件定时器实例:los_api_timer.c/los_api_timer.h
软件定时器,是基于系统Tick时钟中断且由软件来模拟的定时器,当经过设定的Tick时钟计数后会触发用户定义的回调函数。定时精度与系统Tick时钟的周期有关。
本实例执行以下步骤:
实例结果
Kernel swtimer demo start to run.
Create software timer1 successfully.
Create software timer2 successfully.
Start software timer1 successfully.
Stop software timer1 successfully.
LOS_TickCountGet g_demoTimerCount1 = 1.
LOS_TickCountGet tickLast1 = 2559.
Delete software timer1 failed.
Start software timer2 successfully.
LOS_TickCountGet g_demoTimerCount2 = 1.
LOS_TickCountGet tickLast2 = 2670.
LOS_TickCountGet g_demoTimerCount2 = 2.
LOS_TickCountGet tickLast2 = 2770.
LOS_TickCountGet g_demoTimerCount2 = 3.
LOS_TickCountGet tickLast2 = 2870.
LOS_TickCountGet g_demoTimerCount2 = 4.
LOS_TickCountGet tickLast2 = 2970.
LOS_TickCountGet g_demoTimerCount2 = 5.
LOS_TickCountGet tickLast2 = 3070.
LOS_TickCountGet g_demoTimerCount2 = 6.
LOS_TickCountGet tickLast2 = 3170.
LOS_TickCountGet g_demoTimerCount2 = 7.
LOS_TickCountGet tickLast2 = 3270.
LOS_TickCountGet g_demoTimerCount2 = 8.
LOS_TickCountGet tickLast2 = 3370.
LOS_TickCountGet g_demoTimerCount2 = 9.
LOS_TickCountGet tickLast2 = 3470.
LOS_TickCountGet g_demoTimerCount2 = 10.
LOS_TickCountGet tickLast2 = 3570.
Kernel software timer demo finished.
双向链表实例:los_api_list.c/los_api_list.h
双向链表是指含有往前和往后两个方向的链表,即每个结点中除存放下一个节点指针外,还增加一个指向其前一个节点的指针。其头指针head是唯一确定的。
本实例执行以下步骤:
实例结果
Kernel list demo start to run.
Init list.
Node add and tail add.
Add node successfully.
Add tail successfully.
Delete node1.
Delete node1 successfully.
Kernel list demo finished.
测试环境
本实例仅支持在linux下编译,qemu仿真的realview-pbx-a9中运行。
测试任务使用的简单实例:los_debug_task.c/los_debug_task.h
本实例执行以下步骤:
实例结果
Kernel debug task start to run.
Create the task successfully.
Task information:
Task name:task1
Task id:6
Task status:4
Task priority:4
Start Scheduling.
Task running. Interval:1000
Task running. Interval:1000
Task running. Interval:1000
Task running. Interval:1000
Task running. Interval:1000
Delete the task successfully.
Kernel debug task finished.
测试队列使用的简单实例:los_debug_queue.c/los_debug_queue.h
本实例执行以下步骤:
实例结果
Kernel debug queue start to run.
Create the queue successfully.
Send message ok, message: message
Receive message : message.
Queue size : 10
Current queue usage : 0
Send message ok, message: message
Receive message : message.
Queue size : 10
Current queue usage : 0
Send message ok, message: message
Receive message : message.
Queue size : 10
Current queue usage : 0
Send message ok, message: message
Receive message : message.
Queue size : 10
Current queue usage : 0
Send message ok, message: message
Receive message : message.
Queue size : 10
Current queue usage : 0
Delete the queue successfully.
Kernel debug queue finished.
测试事件使用的简单实例:los_debug_event.c/los_debug_event.h
本实例执行以下步骤:
实例结果
Kernel debug event start to run.
Init the event successfully.
Write event.
Read the event : 0x1.
Read the event : 0x1.
Read the event : 0x1.
Clear the event flag.
Read the event timeout.
Read the event timeout.
Delete the event successfully.
Kernel debug event finished.
测试互斥锁使用的简单实例:los_debug_mutex.c/los_debug_mutex.h
本实例执行以下步骤:
实例结果
Kernel debug mutex start to run.
Mutex init.
Create the mutex successfully.
High priority task get the mutex successfully.
High priority task release the mutex.
Low priority task get the mutex successfully.
Low priority task release the mutex.
High priority task get the mutex successfully.
High priority task release the mutex.
Low priority task get the mutex successfully.
Low priority task release the mutex.
High priority task get the mutex successfully.
High priority task release the mutex.
Low priority task get the mutex successfully.
Low priority task release the mutex.
High priority task get the mutex successfully.
High priority task release the mutex.
Low priority task get the mutex successfully.
Low priority task release the mutex.
High priority task get the mutex successfully.
High priority task release the mutex.
Low priority task get the mutex successfully.
Low priority task release the mutex.
Delete the mutex successfully.
Kernel debug mutex finished.
测试信号量使用的简单实例:los_debug_sem.c/los_debug_sem.h
本实例执行以下步骤:
实例结果
Kernel debug semphore start to run.
Create the semphore.
Create the semphore successfully.
High priority task run.
Low priority task run.
Release the semaphore twice.
High priority task get the semphore successfully.
Low priority task get the semphore successfully.
High priority task release the semphore.
Low priority task release the semphore.
High priority task get the semphore successfully.
Low priority task get the semphore successfully.
High priority task release the semphore.
Low priority task release the semphore.
High priority task get the semphore successfully.
Low priority task get the semphore successfully.
High priority task release the semphore.
Low priority task release the semphore.
High priority task get the semphore successfully.
Low priority task get the semphore successfully.
High priority task release the semphore.
Low priority task release the semphore.
High priority task get the semphore successfully.
Low priority task get the semphore successfully.
High priority task release the semphore.
Low priority task release the semphore.
Delete the semaphore successfully.
Kernel debug semphore finished.
这里提供两种方式来运行各个实例。
实例:los_inspect_entry.c/los_inspect_entry.h
执行make menuconfig打开宏开关:
Demos --> Kernel Demo --> Enable Kernel Demo --> Kernel Demo Entry --> InspectEntry
实例:los_demo_entry.c/los_demo_entry.h
执行make menuconfig打开需要测试功能的宏开关,例如要测试task实例,则选择“DemoEntry”, 并选择实例:
Demos --> Kernel Demo --> Enable Kernel Demo --> Kernel Demo Entry --> DemoEntry --> Run Kernel Task Demo
用户可以按照LiteOS Studio STM32工程示例对代码进行编译烧写测试。
用户也可以按照沙盒实验指南来修改3.可操作的测试实例介绍中代码。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。