Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4933

Advanced users • Re: "Realtime" GPIO with bcm2835 (AirSpayce)library, how to turn scheduling back on

$
0
0
Are there any example pieces of code which do this which I can look at? Simple ones which just show how this is done, rather than trying to understand this from looking at big pieces of kernel code that do loads of other stuff too. Thanks
Before investing a lot of time into refactoring your code, I recommend just adding some GPIO test code to the `sysfs_write` of the kernel module I posted earlier, and then kicking it off from terminal with `sudo sh -c "echo 1 > /sys/kernel/my_critical_proc/initiate"`.

If you want to create a C program to initiate the kernel module, then something like this should work:

Code:

#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>int main() {    int fd;    char write_byte = '1';    char read_byte;    while (1) {        // Open the file with read/write permissions        fd = open("/sys/kernel/my_critical_proc/initiate", O_RDWR);        if (fd == -1) {            perror("open");            exit(1);        }        // Write the value to the file        if (write(fd, &write_byte, sizeof(write_byte)) == -1) {            perror("write");            exit(1);        }        // Sleep for 1 second to wait for the kernel module to finish processing        sleep(1);        // Read one byte from the file        if (read(fd, &read_byte, 1) == -1) {            perror("read");            exit(1);        }        // Print the read byte (optional)        printf("Read byte: %c\n", read_byte);        // Close the file        close(fd);    }    return 0;}
Check your scope to see if you're noticing any improvement. If you do notice an improvement, begin moving your *real* critical procedure code to the `sysfs_write` function of the kernel module.

Think of the kernel module as a function that you call by writing to the/sys/kernel/my_critical_proc/initiate file, and get the return value of by reading from that file.

When writing to the file, anything you write will be passed via the `syfs_write`'s `const char *buf, size_t count` parameters. Inside the `sysfs_write` function, use the typical features of C to read whatever it was the calling program wrote via `buf` and do your dynamic processing using that.

If you need to read data back from the kernel module, write data to a `static` variable in `sysfs_write`, and then in the `sysfs_read` function, copy that variable to the `char *buf` as demonstrated with the `sprintf` function in the kernel module example.

Statistics: Posted by JinShil — Sun Jan 28, 2024 8:20 am



Viewing all articles
Browse latest Browse all 4933

Trending Articles