I wrote a sample pwm function in C for beaglebone black. Whenever I make a function call in other modules or in main(), I end up in segmentation fault. Kindly help where I am making the mistake and how to deal with this issue. Below is the code.
int trigger_pwm_output(unsigned input_no )
{
FILE *pwm,*duty,*period,*run;``
uint16_t input=0;
uint8_t input_no=0,input_state=0;
unsigned int duty_cycle =500000;
pwm = fopen("/sys/devices/bone_capemgr.9/slots", "w");
fseek(pwm,0,SEEK_SET);
fprintf(pwm,"am33xx_pwm");
fflush(pwm);
switch(input_no)
{
case 0:
fprintf(pwm,"bone_pwm_P8_13");
fflush(pwm);
period = fopen("/sys/devices/ocp.3/pwm_test_P8_13.15/period", "w");
fseek(period,0,SEEK_SET);
fprintf(period,"%d",500000);
fflush(period);
duty = fopen("/sys/devices/ocp.3/pwm_test_P8_13.15/duty", "w");
fseek(duty,0,SEEK_SET);
run = fopen("/sys/devices/ocp.3/pwm_test_P8_13.15/run", "w");
fseek(run,0,SEEK_SET);
fprintf(run,"%d",0);
fflush(run);
fseek(run,0,SEEK_SET);
count++;
do
{
duty_cycle += 10;
fprintf(duty,"%d",duty_cycle);
}while(count > 0) || (count < 10));
fflush(duty);
fprintf(run,"%d",1);
fflush(run);
fclose(pwm);
fclose(duty);
fclose(period);
fclose(run);
break;
case 1:
fprintf(pwm,"bone_pwm_P8_19");
fflush(pwm);
period = fopen("/sys/devices/ocp.3/pwm_test_P8_19.16/period", "w");
fseek(period,0,SEEK_SET);
fprintf(period,"%d",500000);
fflush(period);
duty = fopen("/sys/devices/ocp.3/pwm_test_P8_19.16/duty", "w");
fseek(duty,0,SEEK_SET);
run = fopen("/sys/devices/ocp.3/pwm_test_P8_19.16/run", "w");
fseek(run,0,SEEK_SET);
fprintf(run,"%d",0);
fflush(run);
fseek(run,0,SEEK_SET);
--count;
do
{
duty_cycle += 10;
fprintf(duty,"%d",duty_cycle);
}while(count <10);
fflush(duty);
fprintf(run,"%d",1);
fflush(run);
fclose(pwm);
fclose(duty);
fclose(period);
fclose(run);
break;
}
return 0;
}
`
input_no
and an ordinary local variable of the same name (and different type). – John Bollingerfopen()
calls fails, in which case it will return a null pointer. When you subsequently try to perform I/O via such a pointer, a segfault is a likely (but by no means guaranteed) result. – John BollingerFILE* fp=fopen("bla.txt","w");if(fp==NULL){printf("failed to openfile\n");exit(1);}
. It is likely the case, since you try to open files in the directory/sys
and it likely requires root privileges. – franciswhile(count > 0) || (count < 10))
– Octopus