打印
[LKS32 软件]

【凌鸥创芯电机开发板LKS32MC071CBT8评测报告】+RT-Thread移植

[复制链接]
476|9
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
夜声|  楼主 | 2023-7-9 17:31 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
一、前言


       由于前面出现了一点意外,今天才拿到开发板,已经到了评测的结尾期,下午下载好了LKS32MC071的相关资料,以及支持的FOC,然后连上为这块板子买的电源和电机,然后打开FOC代码,熟悉了一下,当然一下就熟悉了不可能的,不过看到代码中的驱动库和一些功能,熟悉肯定需要一段时间,根据芯片的片上资源以及使用计划,本篇文章讲解如何移植RT-Thread。

二、RT-Thread Nano简介


      RT-Thread Nano 是一个极简版的硬实时内核,它是由 C 语言开发,采用面向对象的编程思维,具有良好的代码风格,是一款可裁剪的、抢占式实时多任务的 RTOS。其内存资源占用极小,功能包括任务处理、软件定时器、信号量、邮箱和实时调度等相对完整的实时操作系统特性。
在资源足够的情况下可移植finsh组件,如下为RT-Thread软件框图:

RT-Thread Nano 支持可裁剪,通过rtconfig.h进行,在本次使用过程中也会用到,需要裁剪一些功能。
同时最关心的就是RT-Thread Nano的使用资源情况了,对 RAM 与 ROM 的开销非常小,在支持 semaphore 和 mailbox 特性,并运行两个线程 (main 线程 + idle 线程) 情况下,ROM 和 RAM 依然保持着极小的尺寸,RAM 占用约 1K 左右,ROM 占用 4K 左右,但实际可能不止,接下来就开始RT-Thread的移植。

三、RT-Thread Nano移植


准备工作:
下载好RT-Thread Nano版本的源码,或者使用keil中下好RT-Thread实时操作系统的支持包(这种方式方便使用)。
准备好基础工程,本次使用的是UART的工程,带有LED操作,满足移植条件,用于调试观察。
移植过程:
添加RT-Thread Nano源码

添加心跳配置,在board.c文件中,首先需要在源码board.c中添加#include "hardware_init.h"头文件,在board.c中需要使用systick的配置。以下为RT-Thread操作系统的心跳配置。

在systick_Handler中添加心跳回调函数rt_os_tick_callback();

RTOS裁剪,由于芯片资源比较少,需要对RTOS进行裁剪。点击rtconfig.h进行配置,选择Configuration Wizard

在Memory Management Configuation中关闭动态内存池管理

其他按需求配置即可,总的配置文件如下:
/* RT-Thread config file */
#ifndef __RTTHREAD_CFG_H__
#define __RTTHREAD_CFG_H__
// <<< Use Configuration Wizard in Context Menu >>>
// <h>Basic Configuration
// <o>Maximal level of thread priority <8-256>
//  <i>Default: 32
#define RT_THREAD_PRIORITY_MAX  32
// <o>OS tick per second
//  <i>Default: 1000   (1ms)
#define RT_TICK_PER_SECOND  1000
// <o>Alignment size for CPU architecture data access
//  <i>Default: 4
#define RT_ALIGN_SIZE   4
// <o>the max length of object name<2-16>
//  <i>Default: 8
#define RT_NAME_MAX    8
// <c1>Using RT-Thread components initialization
//  <i>Using RT-Thread components initialization
#define RT_USING_COMPONENTS_INIT
// </c>
#define RT_USING_USER_MAIN
// <o>the stack size of main thread<1-4086>
//  <i>Default: 512
#define RT_MAIN_THREAD_STACK_SIZE     256
// </h>
// <h>Debug Configuration
// <c1>enable kernel debug configuration
//  <i>Default: enable kernel debug configuration
//#define RT_DEBUG
// </c>
// <o>enable components initialization debug configuration<0-1>
//  <i>Default: 0
#define RT_DEBUG_INIT 0
// <c1>thread stack over flow detect
//  <i> Diable Thread stack over flow detect
//#define RT_USING_OVERFLOW_CHECK
// </c>
// </h>
// <h>Hook Configuration
// <c1>using hook
//  <i>using hook
//#define RT_USING_HOOK
// </c>
// <c1>using idle hook
//  <i>using idle hook
//#define RT_USING_IDLE_HOOK
// </c>
// </h>
// <e>Software timers Configuration
// <i> Enables user timers
#define RT_USING_TIMER_SOFT         0
#if RT_USING_TIMER_SOFT == 0
    #undef RT_USING_TIMER_SOFT
#endif
// <o>The priority level of timer thread <0-31>
//  <i>Default: 4
#define RT_TIMER_THREAD_PRIO        4
// <o>The stack size of timer thread <0-8192>
//  <i>Default: 512
#define RT_TIMER_THREAD_STACK_SIZE  512
// </e>
// <h>IPC(Inter-process communication) Configuration
// <c1>Using Semaphore
//  <i>Using Semaphore
#define RT_USING_SEMAPHORE
// </c>
// <c1>Using Mutex
//  <i>Using Mutex
//#define RT_USING_MUTEX
// </c>
// <c1>Using Event
//  <i>Using Event
//#define RT_USING_EVENT
// </c>
// <c1>Using MailBox
//  <i>Using MailBox
#define RT_USING_MAILBOX
// </c>
// <c1>Using Message Queue
//  <i>Using Message Queue
//#define RT_USING_MESSAGEQUEUE
// </c>
// </h>
// <h>Memory Management Configuration
// <c1>Memory Pool Management
//  <i>Memory Pool Management
//#define RT_USING_MEMPOOL
// </c>
// <c1>Dynamic Heap Management(Algorithm: small memory )
//  <i>Dynamic Heap Management
//#define RT_USING_HEAP
//#define RT_USING_SMALL_MEM
// </c>
// <c1>using tiny size of memory
//  <i>using tiny size of memory
//#define RT_USING_TINY_SIZE
// </c>
// </h>
// <h>Console Configuration
// <c1>Using console
//  <i>Using console
//#define RT_USING_CONSOLE
// </c>
// <o>the buffer size of console <1-1024>
//  <i>the buffer size of console
//  <i>Default: 128  (128Byte)
#define RT_CONSOLEBUF_SIZE          256
// </h>
// <h>FinSH Configuration
// <c1>include finsh config
//  <i>Select this choice if you using FinSH
//#include "finsh_config.h"
// </c>
// </h>
// <h>Device Configuration
// <c1>using device framework
//  <i>using device framework
//#define RT_USING_DEVICE
// </c>
// </h>
// <<< end of configuration section >>>
#endif

四、程序设计


        这里主要进行验证程序的设计,首先main.c中添加#include <rtthread.h>头文件,在主函数中创建两个线程,线程1和线程2,线程1主要是LED1 250ms翻转,并且打印调试信息,线程2为500ms打印调试信息。如下所示:
#include "lks32mc07x_lib.h"
#include "hardware_init.h"
#include "rtthread.h"
  struct rt_thread thread1;
struct rt_thread thread2;
char thread1_stack[512];
char thread2_stack[512];
void thread1_entry(void *param)
{
while (1)
{
led1_blink();
    printf("thread1 is running\r\n");
rt_thread_mdelay(250);
}
}
  void thread2_entry(void *param)
{
while (1)
{
printf("thread2 is running\r\n");
rt_thread_mdelay(500);
}
}
  void thread1_init(void)
{
rt_err_t fd= rt_thread_init(&thread1,"thread1",&thread1_entry,0,&thread1_stack[0],sizeof(thread1_stack),10,10);
if(fd < 0)
{
printf("thread1 init is fail \r\n");
}
else
{
printf("thread1 init is success \r\n");
}
rt_thread_startup(&thread1);
}
void thread2_init(void)
{
rt_err_t fd= rt_thread_init(&thread2,"thread2",&thread2_entry,0,&thread2_stack[0],sizeof(thread2_stack),10,10);
if(fd < 0)
{
printf("thread2 init is fail \r\n");
}
else
{
printf("thread2 init is success \r\n");
}
rt_thread_startup(&thread2);
}
int main(void)
{
Hardware_init(); /* 硬件初始化 */
thread1_init();
thread2_init();
}
编译下载,可以看到调试信息,线程1和线程2创建成功信息,线程1打印两次,线程2打印一次。如下所示

LED1正常闪烁运行


五、总结


       本次在较小资源的芯片上实现了RT-Thread操作系统,并创建了两个线程,以及调试信息输出。研究了一下电机的驱动,当目前这部分还没开始着手弄,这部分后面慢慢的分享吧,移植完了RT-Thread,说一下感受,个人觉得库封装的不好,我是做应用开发的,也是从事电机控制相关工作,不过我们用的功率特别大,在控制软件中,将底层驱动与应用控制进行了分成,还有算法之间,看了一下LKS32MC071的库函数,接口封装不全,应用程序和平台没有解耦,个人觉得这样不太好。当然可能应用和芯片资源不一样,无妨。

使用特权

评论回复
沙发
chenjun89| | 2023-7-10 07:52 | 只看该作者
RT-rtread这几年发展不错。

使用特权

评论回复
板凳
a20084666| | 2023-7-10 09:04 | 只看该作者
无码



使用特权

评论回复
地板
ynndmalh21| | 2023-7-18 18:42 | 只看该作者
太棒了,有代码就好了。

使用特权

评论回复
5
tpgf| | 2023-8-3 16:14 | 只看该作者
RT-Thread Nano是RT-Thread的精简版,只有内核、shell(msh)、设备驱动三大功能

使用特权

评论回复
6
qcliu| | 2023-8-3 16:44 | 只看该作者
RT-Thread Nano是以Keil5的pack形式发布的

使用特权

评论回复
7
drer| | 2023-8-3 16:56 | 只看该作者
Nano Pack 可以通过在 Keil MDK IDE 内进行安装,也可以手动安装。

使用特权

评论回复
8
coshi| | 2023-8-3 17:29 | 只看该作者
在 Manage Rum-Time Environment 里 "Software Component" 栏找到 RTOS,Variant 栏选择 RT-Thread,然后勾选 kernel,点击 "OK" 就添加 RT-Thread 内核到工程了

使用特权

评论回复
9
kxsi| | 2023-8-4 10:02 | 只看该作者
RT-Thread Nano这个系统大概需要占用多大的空间呢

使用特权

评论回复
10
wiba| | 2023-8-4 10:45 | 只看该作者
RT-Thread Nano pack 包括device drivers、kernel 和 shell (msh) 三部分功能

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

26

主题

84

帖子

2

粉丝