作者:
wtchen (沒有存在感的人)
2015-06-16 17:41:07雖然我問過相關的問題,不過遇到瓶頸只好再問詳細點
我寫了一個module想用rpi的gpio控制一個含3個LED的小電路
/dev 下也有LED_0 LED_1 LED_2
我希望一般user可以寫入這三個file(用0或1)去控制LED的開關
程式碼如下:
https://gist.github.com/gnitnaw/b116f358fa688897fe00
之前不管是用一般user還是root都不能改變/dev/LED_(0,1,2)
改掉一些bug以後,用root可以control了,一般user還是不行
(寫到現在還沒開始ioctl...唉...我的學習速度真慢)
之後可能在kernel學習過程中會有更多問題要上來請教,希望不會違反板規
然後我也會回饋自己所學(拙作請勿見怪)。
我寫了個小程式嘗試去write /dev/LDE_0 :
int main(void)
{
char path[PATH_SIZE], buf[BUF_SIZE];
int i = 0, fd = 0;
snprintf(path, sizeof(path), "/dev/LED_0");
fd = open(path, O_WRONLY);
if (fd < 0) {
perror("Error opening GPIO pin");
exit(EXIT_FAILURE);
}
printf("Set GPIO pins to output, logic level :\n");
strncpy(buf, "1", 1);
buf[1] = '\0';
if (write(fd, buf, sizeof(buf)) < 0) {
perror("write, set pin output");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}