我正在运行启动到终端的linux(没有gui)。
我有一个ZyBo电路板,它有一个ArmV7处理器。我写了一个C程序来输出PMOD上的时钟和相应的数据序列。 PMOD的切换速度高达50MHz。但是,我的程序创建的时钟只有115 Hz的最大频率。我需要这个程序尽可能快地输出,因为PMOD I使用的能力是50MHz。
我使用以下代码行编译了我的程序:
gcc -ofast (c_program)
。
#include <stdio.h>
#include <stdlib.h>
#define ARRAYSIZE 511
//________________________________________
//macro for the SIGNAL PMOD
//________________________________________
//DATA
//ZYBO Use Pin JE1
#define INIT_SIGNAL system("echo 54 > /sys/class/gpio/export"); system("echo out > /sys/class/gpio/gpio54/direction");
#define SIGNAL_ON system("echo 1 > /sys/class/gpio/gpio54/value");
#define SIGNAL_OFF system("echo 0 > /sys/class/gpio/gpio54/value");
//________________________________________
//macro for the "CLOCK" PMOD
//________________________________________
//CLOCK
//ZYBO Use Pin JE4
#define INIT_MYCLOCK system("echo 57 > /sys/class/gpio/export"); system("echo out > /sys/class/gpio/gpio57/direction");
#define MYCLOCK_ON system("echo 1 > /sys/class/gpio/gpio57/value");
#define MYCLOCK_OFF system("echo 0 > /sys/class/gpio/gpio57/value");
int main(void){
int myarray[ARRAYSIZE] = {//hard coded array for signal data
1,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0,1,0,0,1,0,1,0,0,1,1,0,0,1,1,0,1,0,0,0,0,0,1,0,0,1,1,1,0,0,1,1,1,0,1,1,1,1,0,0,1,0,0,0,1,0,1,0,0,1,1,1,0,0,1,0,1,0,1,0,0,1,0,1,1,0,1,0,1,1,0,0,1,1,1,1,0,0,1,0,1,0,0,1,1,1,1,1,1,0,0,1,0,0,1,1,0,1,0,0,0,0,1,0,0,0,1,1,0,0,1,0,1,1,1,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,1,0,1,1,1,1,0,1,1,0,1,0,0,1,0,0,0,1,0,1,0,0,1,0,0,0,1,0,0,0,1,0,1,0,1,0,1,0,1,1,0,0,0,0,0,0,0,0,1,0,1,1,0,1,1,1,1,1,0,0,1,1,1,0,0,1,1,0,1,1,0,1,1,1,0,0,1,1,1,1,1,0,0,1,0,1,0,1,0,1,1,0,1,0,0,0,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,0,0,0,0,1,1,1,0,1,1,1,1,0,1,1,0,1,0,1,0,1,0,0,1,0,1,1,1,0,1,1,1,0,0,1,1,1,0,1,0,0,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,0,0,1,0,1,1,0,1,0,1,1,1,0,0,0,0,0,1,0,0,0,1,0,1,1,1,1,1,1,1,0,0,0,0,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,0,0,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,1,1,1,0,1,0,1,1,1,0,0,0,1,0,1,0,1,0,0,1,1,0,0,1,1,0,1,0,0,1,0,0,1,0,1,1,1,1,1,1,0,1,1,0,1,0,1,1,1,1,1,1,0,0,1,1,0,1,1,0,0,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0,1,1,1,1,1,1,1,0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0
};
INIT_SIGNAL
INIT_MYCLOCK;
//infinite loop
int i;
do{
i = 0;
do{
/*
1020 is chosen because it is twice the size needed allowing for the changes in the clock.
(511= 0-510, 510*2= 1020 ==> 0-1020 needed, so 1021 it is)
*/
if((i%2)==0)
{
MYCLOCK_ON;
if(myarray[i/2] == 1){
SIGNAL_ON;
}else{
SIGNAL_OFF;
}
}
else if((i%2)==1)
{
MYCLOCK_OFF;
//dont need to change the signal since it will just stay at whatever it was.
}
++i;
} while(i < 1021);
} while(1);
return 0;
}
如何使我的可执行程序输出至少达到MegaHertz的大小?
答案 0 :(得分:1)
您正试图通过使用system
函数(即创建进程)来调用时钟来调用另一个可执行文件(/bin/echo
),目的是将值写入{{{ 1}}文件系统。难怪你无法实现超过数百赫兹的速度。
至少应该写入/sys
文件系统中的伪文件,直接打开它们并在程序中写入,而不是通过/sys
。这可能无法让您进入MHz范围,但它肯定会比您所拥有的速度快数百倍。
答案 1 :(得分:0)
使用FILE * fopen ( const char * filename, const char * mode );
直接打开文件,然后使用int fprintf ( FILE * stream, const char * format, ... );
http://www.cplusplus.com/reference/cstdio/fopen/
http://www.cplusplus.com/reference/cstdio/fprintf/
如果您需要更高的速度,请查看open()系统调用和mmap()