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