Skip to content

Instantly share code, notes, and snippets.

@yoneken
Created October 3, 2013 15:52
Show Gist options
  • Save yoneken/6812144 to your computer and use it in GitHub Desktop.
Save yoneken/6812144 to your computer and use it in GitHub Desktop.
RT test applicatioin for BeagleBone Black.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sched.h>
#include <sys/mman.h>
#include <string.h>
#include <unistd.h>
#define MY_PRIORITY (49) /* we use 49 as the PRREMPT_RT use 50
as the priority of kernel tasklets
and interrupt handler by default */
#define MAX_SAFE_STACK (8*1024) /* The maximum stack size which is
guaranteed safe to access without
faulting */
#define NSEC_PER_SEC (1000000000) /* The number of nsecs per sec. */
void stack_prefault(void) {
unsigned char dummy[MAX_SAFE_STACK];
memset(dummy, 0, MAX_SAFE_STACK);
return;
}
int main(int argc, char* argv[])
{
FILE *PortHandle = NULL;
char *LEDBrightness = "/sys/class/leds/beaglebone:green:usr3/brightness";
struct timespec t;
struct sched_param param;
int i, interval = 1000000; /* 1ms*/
/* Initialize for GPIO */
if((PortHandle = fopen("/sys/class/gpio/export", "ab")) == NULL){
printf("Unable to register gpio port.\n");
return 1;
}
fwrite("60", sizeof(char), 2, PortHandle);
fclose(PortHandle);
if((PortHandle = fopen("/sys/class/gpio/gpio60/direction", "rb+")) == NULL){
printf("Unable to register gpio direction.\n");
return 1;
}
fwrite("out", sizeof(char), 3, PortHandle);
fclose(PortHandle);
/* Declare ourself as a real time task */
param.sched_priority = MY_PRIORITY;
if(sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
perror("sched_setscheduler failed");
exit(-1);
}
/* Lock memory */
if(mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
perror("mlockall failed");
exit(-2);
}
/* Pre-fault our stack */
stack_prefault();
clock_gettime(CLOCK_MONOTONIC ,&t);
/* start after one second */
t.tv_sec++;
for(i=0;i<120000;i++) {
/* wait until next shot */
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
/* do the stuff */
if((PortHandle = fopen("/sys/class/gpio/gpio60/value", "rb+")) == NULL){
printf("Unable to write gpio value.\n");
return 1;
}
if(i%20 == 0){
fwrite("1", sizeof(char), 1, PortHandle);
}else{
fwrite("0", sizeof(char), 1, PortHandle);
}
fclose(PortHandle);
/* calculate next shot */
t.tv_nsec += interval;
while (t.tv_nsec >= NSEC_PER_SEC) {
t.tv_nsec -= NSEC_PER_SEC;
t.tv_sec++;
}
}
/* Deiitialize for GPIO */
if((PortHandle = fopen("/sys/class/gpio/unexport", "ab")) == NULL){
printf("Unable to unregister gpio port.\n");
return 1;
}
fwrite("60", sizeof(char), 2, PortHandle);
fclose(PortHandle);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment