基于Ada语言的RTEMS应用开发

RTEMS支持C语言和Ada语言的应用程序开发,不同于C语言开发,Ada应用程序开发需要在进行系统编译时添加配置参数。我们在这边介绍使用Ada语言调用C接口的开发方法。

安装前的准备

环境要求,GCC版本不早于4.9.3,与GCC版本对应的GNAT (例如GNAT-4.9)
在编译系统时,需要添加Ada配置选项

../rtems-git/configure --target=powerpc-rtems4.11 \
--enable-rtemsbsp= mpc8313erdb --enable-posix \
--enable-ada --prefix=$HOME/opt/rtems-4.11-latest/

修改PowerPC头文件内容

添加对应型号的PowerPC开发板全局变量的定义
修改
$HOME/opt/rtems-4.11-latest/powerpc-rtems4.11/moc8313erdb/lib/include/rtems路径下的powerpc,添加如下代码

#elif defined(mpc8313erdb)
#define PPC_I_CACHE 16384
#define PPC_D_CACHE 16384
#define PPC_CACHE_ALIGNMENT 32
#define PPC_INTERRUPT_MAX 125
#define PPC_USE_MULTIPLE 1

下载并编译Ada代码

$ git clone git://git.rtems.org/ada-examples.git
$ cd ada-examples
$ export RTEMS_MAKEFILE_PATH=$HOME/opt/rtems-4.11-latest/powerpc-rtems4.11/moc8313erdb/
$ cd hello_world_ada
$ make

Example

如下的Ada代码调用了C程序的getTime函数

with Text_IO; use Text_IO;
with System;
with Interfaces.C;
procedure Use_Of_Import is 
procedure Imported_Function;
pragma Import (C, Imported_Function, "getTime"); 
begin Imported_Function; 
end Use_Of_Import;

getTime函数的定义如下所示,该函数调用RTEMS系统的rtems_clock_set和rtems_clock_get_tod函数。

#if defined(__rtems__)
#include <rtems.h>
#endif
#include <stdio.h> 
#include <stdlib.h>
#include <unistd.h> 
int getTime()
{
    rtems_status_code status;
    rtems_time_of_day time;  
    int count;
    time.year   = 1988;
    time.month  = 12;
    time.day    = 31;
    time.hour   = 9;
    time.minute = 5;
    time.second = 7;
    time.ticks  = 0;
    status = rtems_clock_set( &time );
    printf("RTEMS CLOCK SET STATUS:%d\n",status);
    sleep(3);
    status = rtems_clock_get_tod( &time ); 
    printf("RTEMS CLOCK GET STATUS:%d\n",status);
    printf("rtems_clock_get_tod Minute:%d, Second:%d \n", time.minute,time.second);
sleep(3);
    status = rtems_clock_get_tod( &time ); 
    printf("RTEMS CLOCK GET STATUS:%d\n",status);
    printf("rtems_clock_get_tod Minute:%d, Second:%d \n", time.minute,time.second);
    sleep(4);
    status = rtems_clock_get_tod( &time ); 
    printf("RTEMS CLOCK GET STATUS:%d\n",status);
    printf("rtems_clock_get_tod Minute:%d, Second:%d \n", time.minute,time.second);
    sleep(5);
while(1){
}
    return 0;
}

如下是演示程序的Makefile文件,其中Ada程序中调用的getTime函数定义在demo_c.c中,编译前需要设置环境变量RTEMS_MAKEFILE_PATH,该环境变量与其他程序编译需要设置的环境变量相同

PROGRAM=ada_demo
include $(RTEMS_MAKEFILE_PATH)/Makefile.inc
include $(RTEMS_CUSTOM)
include $(PROJECT_ROOT)/make/leaf.cfg
# stack size for the first Ada thread
CFLAGS +=-DGNAT_MAIN_STACKSPACE=100
EXTRA_OBJS = demo_c.o
EXTRA_GNATFLAGS=-fstack-check
include ../Makefile.shared
empty.o: demo_c.c
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值