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

Troubleshooting • RP1 Register Direct Acess

$
0
0
I'm writing module for kernel that toggles one pin. Because i'm in kernel space i need to access iternal pins(cat /sys/kernel/debug/gpio). I checked RP1 Peripherals but still cant figure out.

Code:

#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/io.h>#include <linux/delay.h>#define PERI_BASE                0x40000000 // Peripheral base address for Raspberry Pi 5#define GPIO_BASE                (PERI_BASE + 0x0D0000) // Address of the GPIO controller#define PAGE_SIZE                (4 * 1024)#define BLOCK_SIZE               (4 * 1024)// Define GPIO registers#define GPSET0                   (GPIO_BASE + 0x1C) // Set GPIO pin high#define GPCLR0                   (GPIO_BASE + 0x28) // Set GPIO pin low#define GPLEV0                   (GPIO_BASE + 0x34) // Read GPIO pin level#define GPFSEL0                  (GPIO_BASE + 0x00) // Function select register// Define pins to use#define GPIO_START               573#define GPIO_END                 580static void __iomem *gpio_map;static int __init gpio_module_init(void) {    int gpio;    printk(KERN_INFO "Initializing GPIO module\n");    // Map memory for accessing GPIO    gpio_map = ioremap(GPIO_BASE, BLOCK_SIZE);    if (!gpio_map) {        printk(KERN_ERR "Failed to map GPIO memory\n");        return -ENOMEM; // Memory mapping error    }    // Configure GPIO pins as outputs    for (gpio = GPIO_START; gpio <= GPIO_END; gpio++) {        int fsel_offset = (gpio - GPIO_START) * 3; // Calculate the bit offset for the pin in GPFSEL0        *(volatile unsigned int *)(gpio_map + GPFSEL0) |= (1 << fsel_offset); // Set pin mode to output    }    for (int i = 0; i < 10; i++) {        for (gpio = GPIO_START; gpio <= GPIO_END; gpio++) {            *(volatile unsigned int *)(gpio_map + GPSET0) = (1 << gpio); // Set high level            *(volatile unsigned int *)(gpio_map + GPCLR0) = (1 << gpio); // Set low level        }    }    return 0; // Successful initialization}static void __exit gpio_module_exit(void) {    printk(KERN_INFO "Exiting GPIO module\n");        // Unmap memory    if (gpio_map) {        iounmap(gpio_map);    }}module_init(gpio_module_init);module_exit(gpio_module_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("Yuichi");MODULE_DESCRIPTION("A simple GPIO module for Raspberry Pi 5 using direct register access");
Everytime failing to map.

Statistics: Posted by YuichiRP1 — Sat Dec 28, 2024 9:12 am — Replies 0 — Views 61



Viewing all articles
Browse latest Browse all 3466

Trending Articles