我用 C 语言为 beaglebone black 编写了一个示例 pwm 函数。每当我在其他模块或 main() 中进行函数调用时,我都会遇到分段错误。请帮助我在哪里犯了错误以及如何处理这个问题。
下面是代码。
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;
}
`